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