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