comparison examples/layouts/dynamiclayouts/dialog.d @ 101:4f909ae70e76

add dynamiclayouts example
author mandel
date Fri, 29 May 2009 13:25:41 +0000
parents
children 236401001115
comparison
equal deleted inserted replaced
100:dcd36d8db2da 101:4f909ae70e76
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 module dialog;
42
43
44 import qt.gui.QDialog;
45 import qt.gui.QGroupBox;
46 import qt.gui.QGridLayout;
47 import qt.gui.QLabel;
48 import qt.gui.QComboBox;
49 import qt.gui.QDialogButtonBox;
50 import qt.gui.QPushButton;
51 import qt.gui.QMessageBox;
52 import qt.gui.QSpinBox;
53 import qt.gui.QDial;
54 import qt.gui.QProgressBar;
55
56
57 class Dialog : public QDialog
58 {
59
60 public:
61
62 this(QWidget parent = null)
63 {
64 super(parent);
65 createRotableGroupBox();
66 createOptionsGroupBox();
67 createButtonBox();
68
69 mainLayout = new QGridLayout;
70 mainLayout.addWidget(rotableGroupBox, 0, 0);
71 mainLayout.addWidget(optionsGroupBox, 1, 0);
72 mainLayout.addWidget(buttonBox, 2, 0);
73 setLayout(mainLayout);
74
75 mainLayout.setSizeConstraint(QLayout.SetMinimumSize);
76
77 setWindowTitle(tr("Dynamic Layouts"));
78 }
79
80 private:
81
82 void buttonsOrientationChanged(int index)
83 {
84 mainLayout.setSizeConstraint(QLayout.SetNoConstraint);
85 setMinimumSize(0, 0);
86
87 Qt_Orientation orientation = cast(Qt_Orientation) buttonsOrientationComboBox.itemData(index).toInt();
88
89 if (orientation == buttonBox.orientation())
90 return;
91
92 mainLayout.removeWidget(buttonBox);
93
94 int spacing = mainLayout.spacing();
95
96 QSize oldSizeHint = buttonBox.sizeHint() + QSize(spacing, spacing);
97 buttonBox.setOrientation(orientation);
98 QSize newSizeHint = buttonBox.sizeHint() + QSize(spacing, spacing);
99
100 if (orientation == Qt_Orientation.Horizontal) {
101 mainLayout.addWidget(buttonBox, 2, 0);
102 resize(size() + QSize(-1 * oldSizeHint.width(), newSizeHint.height()));
103 } else {
104 mainLayout.addWidget(buttonBox, 0, 3, 2, 1);
105 resize(size() + QSize(newSizeHint.width(), -1 * oldSizeHint.height()));
106 }
107
108 mainLayout.setSizeConstraint(QLayout.SetDefaultConstraint);
109 }
110
111 void rotateWidgets()
112 {
113 assert(rotableWidgets.length % 2 == 0);
114
115 foreach (QWidget widget; rotableWidgets)
116 rotableLayout.removeWidget(widget);
117
118 rotableWidgets = rotableWidgets[1..$] ~ rotableWidgets[0];
119
120 int n = rotableWidgets.length;
121 for (int i = 0; i < n / 2; ++i) {
122 rotableLayout.addWidget(rotableWidgets[n - i - 1], 0, i);
123 rotableLayout.addWidget(rotableWidgets[i], 1, i);
124 }
125 }
126
127 void help()
128 {
129 QMessageBox.information(this, tr("Dynamic Layouts Help"),
130 tr("This example shows how to change layouts dynamically."));
131 }
132
133 private:
134
135 void createRotableGroupBox()
136 {
137 rotableGroupBox = new QGroupBox(tr("Rotable Widgets"));
138
139 rotableWidgets ~= new QSpinBox;
140 rotableWidgets ~= new QSlider;
141 rotableWidgets ~= new QDial;
142 rotableWidgets ~= new QProgressBar;
143
144 int n = rotableWidgets.length;
145 for (int i = 0; i < n; ++i) {
146 rotableWidgets[i].valueChanged.connect(&rotableWidgets[(i + 1) % n].setValue);
147 }
148
149 rotableLayout = new QGridLayout;
150 rotableGroupBox.setLayout(rotableLayout);
151
152 rotateWidgets();
153 }
154
155 void createOptionsGroupBox()
156 {
157 optionsGroupBox = new QGroupBox(tr("Options"));
158
159 buttonsOrientationLabel = new QLabel(tr("Orientation of buttons:"));
160
161 buttonsOrientationComboBox = new QComboBox;
162 buttonsOrientationComboBox.addItem(tr("Horizontal"), new QVariant(cast(ulong) Qt.Horizontal));
163 buttonsOrientationComboBox.addItem(tr("Vertical"), new QVariant(cast(ulong) Qt.Vertical));
164
165 buttonsOrientationComboBox.currentIndexChanged.connect(&this.buttonsOrientationChanged);
166
167 optionsLayout = new QGridLayout;
168 optionsLayout.addWidget(buttonsOrientationLabel, 0, 0);
169 optionsLayout.addWidget(buttonsOrientationComboBox, 0, 1);
170 optionsLayout.setColumnStretch(2, 1);
171 optionsGroupBox.setLayout(optionsLayout);
172 }
173
174 void createButtonBox()
175 {
176 buttonBox = new QDialogButtonBox;
177
178 closeButton = buttonBox.addButton(QDialogButtonBox.Close);
179 helpButton = buttonBox.addButton(QDialogButtonBox.Help);
180 rotateWidgetsButton = buttonBox.addButton(tr("Rotate &Widgets"), QDialogButtonBox.ActionRole);
181
182 rotateWidgetsButton.clicked.connect(&this.rotateWidgets);
183 closeButton.clicked.connect(&this.close);
184 helpButton.clicked.connect(&this.help);
185 }
186
187 QGroupBox rotableGroupBox;
188 QWidget[] rotableWidgets;
189
190 QGroupBox optionsGroupBox;
191 QLabel buttonsOrientationLabel;
192 QComboBox buttonsOrientationComboBox;
193
194 QDialogButtonBox buttonBox;
195 QPushButton closeButton;
196 QPushButton helpButton;
197 QPushButton rotateWidgetsButton;
198
199 QGridLayout mainLayout;
200 QGridLayout rotableLayout;
201 QGridLayout optionsLayout;
202 }