1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | |
53 | #include "dialog.h" |
54 | |
55 | Dialog::Dialog(QWidget *parent) |
56 | : QDialog(parent) |
57 | { |
58 | createRotatableGroupBox(); |
59 | createOptionsGroupBox(); |
60 | createButtonBox(); |
61 | |
62 | mainLayout = new QGridLayout; |
63 | mainLayout->addWidget(rotatableGroupBox, row: 0, column: 0); |
64 | mainLayout->addWidget(optionsGroupBox, row: 1, column: 0); |
65 | mainLayout->addWidget(buttonBox, row: 2, column: 0); |
66 | setLayout(mainLayout); |
67 | |
68 | mainLayout->setSizeConstraint(QLayout::SetMinimumSize); |
69 | |
70 | setWindowTitle(tr(s: "Dynamic Layouts" )); |
71 | } |
72 | |
73 | void Dialog::buttonsOrientationChanged(int index) |
74 | { |
75 | mainLayout->setSizeConstraint(QLayout::SetNoConstraint); |
76 | setMinimumSize(minw: 0, minh: 0); |
77 | |
78 | Qt::Orientation orientation = Qt::Orientation( |
79 | buttonsOrientationComboBox->itemData(index).toInt()); |
80 | |
81 | if (orientation == buttonBox->orientation()) |
82 | return; |
83 | |
84 | mainLayout->removeWidget(w: buttonBox); |
85 | |
86 | int spacing = mainLayout->spacing(); |
87 | |
88 | QSize oldSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing); |
89 | buttonBox->setOrientation(orientation); |
90 | QSize newSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing); |
91 | |
92 | if (orientation == Qt::Horizontal) { |
93 | mainLayout->addWidget(buttonBox, row: 2, column: 0); |
94 | resize(size() + QSize(-oldSizeHint.width(), newSizeHint.height())); |
95 | } else { |
96 | mainLayout->addWidget(buttonBox, row: 0, column: 3, rowSpan: 2, columnSpan: 1); |
97 | resize(size() + QSize(newSizeHint.width(), -oldSizeHint.height())); |
98 | } |
99 | |
100 | mainLayout->setSizeConstraint(QLayout::SetDefaultConstraint); |
101 | } |
102 | |
103 | void Dialog::rotateWidgets() |
104 | { |
105 | Q_ASSERT(rotatableWidgets.count() % 2 == 0); |
106 | |
107 | for (QWidget *widget : qAsConst(t&: rotatableWidgets)) |
108 | rotatableLayout->removeWidget(w: widget); |
109 | |
110 | rotatableWidgets.enqueue(t: rotatableWidgets.dequeue()); |
111 | |
112 | const int n = rotatableWidgets.count(); |
113 | for (int i = 0; i < n / 2; ++i) { |
114 | rotatableLayout->addWidget(rotatableWidgets[n - i - 1], row: 0, column: i); |
115 | rotatableLayout->addWidget(rotatableWidgets[i], row: 1, column: i); |
116 | } |
117 | } |
118 | |
119 | void Dialog::help() |
120 | { |
121 | QMessageBox::information(parent: this, title: tr(s: "Dynamic Layouts Help" ), |
122 | text: tr(s: "This example shows how to change layouts " |
123 | "dynamically." )); |
124 | } |
125 | |
126 | void Dialog::createRotatableGroupBox() |
127 | { |
128 | rotatableGroupBox = new QGroupBox(tr(s: "Rotatable Widgets" )); |
129 | |
130 | rotatableWidgets.enqueue(t: new QSpinBox); |
131 | rotatableWidgets.enqueue(t: new QSlider); |
132 | rotatableWidgets.enqueue(t: new QDial); |
133 | rotatableWidgets.enqueue(t: new QProgressBar); |
134 | |
135 | int n = rotatableWidgets.count(); |
136 | for (int i = 0; i < n; ++i) { |
137 | connect(sender: rotatableWidgets[i], SIGNAL(valueChanged(int)), |
138 | receiver: rotatableWidgets[(i + 1) % n], SLOT(setValue(int))); |
139 | } |
140 | |
141 | rotatableLayout = new QGridLayout; |
142 | rotatableGroupBox->setLayout(rotatableLayout); |
143 | |
144 | rotateWidgets(); |
145 | } |
146 | |
147 | void Dialog::createOptionsGroupBox() |
148 | { |
149 | optionsGroupBox = new QGroupBox(tr(s: "Options" )); |
150 | |
151 | buttonsOrientationLabel = new QLabel(tr(s: "Orientation of buttons:" )); |
152 | |
153 | buttonsOrientationComboBox = new QComboBox; |
154 | buttonsOrientationComboBox->addItem(atext: tr(s: "Horizontal" ), auserData: Qt::Horizontal); |
155 | buttonsOrientationComboBox->addItem(atext: tr(s: "Vertical" ), auserData: Qt::Vertical); |
156 | |
157 | connect(sender: buttonsOrientationComboBox, |
158 | signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), |
159 | receiver: this, |
160 | slot: &Dialog::buttonsOrientationChanged); |
161 | |
162 | optionsLayout = new QGridLayout; |
163 | optionsLayout->addWidget(buttonsOrientationLabel, row: 0, column: 0); |
164 | optionsLayout->addWidget(buttonsOrientationComboBox, row: 0, column: 1); |
165 | optionsLayout->setColumnStretch(column: 2, stretch: 1); |
166 | optionsGroupBox->setLayout(optionsLayout); |
167 | } |
168 | |
169 | void Dialog::createButtonBox() |
170 | { |
171 | buttonBox = new QDialogButtonBox; |
172 | |
173 | closeButton = buttonBox->addButton(button: QDialogButtonBox::Close); |
174 | helpButton = buttonBox->addButton(button: QDialogButtonBox::Help); |
175 | rotateWidgetsButton = buttonBox->addButton(text: tr(s: "Rotate &Widgets" ), |
176 | role: QDialogButtonBox::ActionRole); |
177 | |
178 | connect(sender: rotateWidgetsButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::rotateWidgets); |
179 | connect(sender: closeButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::close); |
180 | connect(sender: helpButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::help); |
181 | } |
182 | |
183 | |
184 | |