| 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, 0, 0); |
| 64 | mainLayout->addWidget(optionsGroupBox, 1, 0); |
| 65 | mainLayout->addWidget(buttonBox, 2, 0); |
| 66 | setLayout(mainLayout); |
| 67 | |
| 68 | mainLayout->setSizeConstraint(QLayout::SetMinimumSize); |
| 69 | |
| 70 | setWindowTitle(tr("Dynamic Layouts" )); |
| 71 | } |
| 72 | |
| 73 | void Dialog::buttonsOrientationChanged(int index) |
| 74 | { |
| 75 | mainLayout->setSizeConstraint(QLayout::SetNoConstraint); |
| 76 | setMinimumSize(0, 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(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, 2, 0); |
| 94 | resize(size() + QSize(-oldSizeHint.width(), newSizeHint.height())); |
| 95 | } else { |
| 96 | mainLayout->addWidget(buttonBox, 0, 3, 2, 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(rotatableWidgets)) |
| 108 | rotatableLayout->removeWidget(widget); |
| 109 | |
| 110 | rotatableWidgets.enqueue(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], 0, i); |
| 115 | rotatableLayout->addWidget(rotatableWidgets[i], 1, i); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void Dialog::help() |
| 120 | { |
| 121 | QMessageBox::information(this, tr("Dynamic Layouts Help" ), |
| 122 | tr("This example shows how to change layouts " |
| 123 | "dynamically." )); |
| 124 | } |
| 125 | |
| 126 | void Dialog::createRotatableGroupBox() |
| 127 | { |
| 128 | rotatableGroupBox = new QGroupBox(tr("Rotatable Widgets" )); |
| 129 | |
| 130 | rotatableWidgets.enqueue(new QSpinBox); |
| 131 | rotatableWidgets.enqueue(new QSlider); |
| 132 | rotatableWidgets.enqueue(new QDial); |
| 133 | rotatableWidgets.enqueue(new QProgressBar); |
| 134 | |
| 135 | int n = rotatableWidgets.count(); |
| 136 | for (int i = 0; i < n; ++i) { |
| 137 | connect(rotatableWidgets[i], SIGNAL(valueChanged(int)), |
| 138 | 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("Options" )); |
| 150 | |
| 151 | buttonsOrientationLabel = new QLabel(tr("Orientation of buttons:" )); |
| 152 | |
| 153 | buttonsOrientationComboBox = new QComboBox; |
| 154 | buttonsOrientationComboBox->addItem(tr("Horizontal" ), Qt::Horizontal); |
| 155 | buttonsOrientationComboBox->addItem(tr("Vertical" ), Qt::Vertical); |
| 156 | |
| 157 | connect(buttonsOrientationComboBox, |
| 158 | &QComboBox::currentIndexChanged, |
| 159 | this, |
| 160 | &Dialog::buttonsOrientationChanged); |
| 161 | |
| 162 | optionsLayout = new QGridLayout; |
| 163 | optionsLayout->addWidget(buttonsOrientationLabel, 0, 0); |
| 164 | optionsLayout->addWidget(buttonsOrientationComboBox, 0, 1); |
| 165 | optionsLayout->setColumnStretch(2, 1); |
| 166 | optionsGroupBox->setLayout(optionsLayout); |
| 167 | } |
| 168 | |
| 169 | void Dialog::createButtonBox() |
| 170 | { |
| 171 | buttonBox = new QDialogButtonBox; |
| 172 | |
| 173 | closeButton = buttonBox->addButton(QDialogButtonBox::Close); |
| 174 | helpButton = buttonBox->addButton(QDialogButtonBox::Help); |
| 175 | rotateWidgetsButton = buttonBox->addButton(tr("Rotate &Widgets" ), |
| 176 | QDialogButtonBox::ActionRole); |
| 177 | |
| 178 | connect(rotateWidgetsButton, &QPushButton::clicked, this, &Dialog::rotateWidgets); |
| 179 | connect(closeButton, &QPushButton::clicked, this, &Dialog::close); |
| 180 | connect(helpButton, &QPushButton::clicked, this, &Dialog::help); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |