examples/layouts/dynamiclayouts/dialog.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "dialog.h"
       
    45 
       
    46 Dialog::Dialog(QWidget *parent)
       
    47     : QDialog(parent)
       
    48 {
       
    49     createRotableGroupBox();
       
    50     createOptionsGroupBox();
       
    51     createButtonBox();
       
    52 
       
    53     mainLayout = new QGridLayout;
       
    54     mainLayout->addWidget(rotableGroupBox, 0, 0);
       
    55     mainLayout->addWidget(optionsGroupBox, 1, 0);
       
    56     mainLayout->addWidget(buttonBox, 2, 0);
       
    57     setLayout(mainLayout);
       
    58 
       
    59     mainLayout->setSizeConstraint(QLayout::SetMinimumSize);
       
    60 
       
    61     setWindowTitle(tr("Dynamic Layouts"));
       
    62 }
       
    63 
       
    64 void Dialog::buttonsOrientationChanged(int index)
       
    65 {
       
    66     mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
       
    67     setMinimumSize(0, 0);
       
    68 
       
    69     Qt::Orientation orientation = Qt::Orientation(
       
    70             buttonsOrientationComboBox->itemData(index).toInt());
       
    71 
       
    72     if (orientation == buttonBox->orientation())
       
    73         return;
       
    74 
       
    75     mainLayout->removeWidget(buttonBox);
       
    76 
       
    77     int spacing = mainLayout->spacing();
       
    78 
       
    79     QSize oldSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing);
       
    80     buttonBox->setOrientation(orientation);
       
    81     QSize newSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing);
       
    82 
       
    83     if (orientation == Qt::Horizontal) {
       
    84         mainLayout->addWidget(buttonBox, 2, 0);
       
    85         resize(size() + QSize(-oldSizeHint.width(), newSizeHint.height()));
       
    86     } else {
       
    87         mainLayout->addWidget(buttonBox, 0, 3, 2, 1);
       
    88         resize(size() + QSize(newSizeHint.width(), -oldSizeHint.height()));
       
    89     }
       
    90 
       
    91     mainLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
       
    92 }
       
    93 
       
    94 void Dialog::rotateWidgets()
       
    95 {
       
    96     Q_ASSERT(rotableWidgets.count() % 2 == 0);
       
    97 
       
    98     foreach (QWidget *widget, rotableWidgets)
       
    99         rotableLayout->removeWidget(widget);
       
   100 
       
   101     rotableWidgets.enqueue(rotableWidgets.dequeue());
       
   102 
       
   103     const int n = rotableWidgets.count();
       
   104     for (int i = 0; i < n / 2; ++i) {
       
   105         rotableLayout->addWidget(rotableWidgets[n - i - 1], 0, i);
       
   106         rotableLayout->addWidget(rotableWidgets[i], 1, i);
       
   107     }
       
   108 }
       
   109 
       
   110 void Dialog::help()
       
   111 {
       
   112     QMessageBox::information(this, tr("Dynamic Layouts Help"),
       
   113                                tr("This example shows how to change layouts "
       
   114                                   "dynamically."));
       
   115 }
       
   116 
       
   117 void Dialog::createRotableGroupBox()
       
   118 {
       
   119     rotableGroupBox = new QGroupBox(tr("Rotable Widgets"));
       
   120 
       
   121     rotableWidgets.enqueue(new QSpinBox);
       
   122     rotableWidgets.enqueue(new QSlider);
       
   123     rotableWidgets.enqueue(new QDial);
       
   124     rotableWidgets.enqueue(new QProgressBar);
       
   125 
       
   126     int n = rotableWidgets.count();
       
   127     for (int i = 0; i < n; ++i) {
       
   128         connect(rotableWidgets[i], SIGNAL(valueChanged(int)),
       
   129                 rotableWidgets[(i + 1) % n], SLOT(setValue(int)));
       
   130     }
       
   131 
       
   132     rotableLayout = new QGridLayout;
       
   133     rotableGroupBox->setLayout(rotableLayout);
       
   134 
       
   135     rotateWidgets();
       
   136 }
       
   137 
       
   138 void Dialog::createOptionsGroupBox()
       
   139 {
       
   140     optionsGroupBox = new QGroupBox(tr("Options"));
       
   141 
       
   142     buttonsOrientationLabel = new QLabel(tr("Orientation of buttons:"));
       
   143 
       
   144     buttonsOrientationComboBox = new QComboBox;
       
   145     buttonsOrientationComboBox->addItem(tr("Horizontal"), Qt::Horizontal);
       
   146     buttonsOrientationComboBox->addItem(tr("Vertical"), Qt::Vertical);
       
   147 
       
   148     connect(buttonsOrientationComboBox, SIGNAL(currentIndexChanged(int)),
       
   149             this, SLOT(buttonsOrientationChanged(int)));
       
   150 
       
   151     optionsLayout = new QGridLayout;
       
   152     optionsLayout->addWidget(buttonsOrientationLabel, 0, 0);
       
   153     optionsLayout->addWidget(buttonsOrientationComboBox, 0, 1);
       
   154     optionsLayout->setColumnStretch(2, 1);
       
   155     optionsGroupBox->setLayout(optionsLayout);
       
   156 }
       
   157 
       
   158 void Dialog::createButtonBox()
       
   159 {
       
   160     buttonBox = new QDialogButtonBox;
       
   161 
       
   162     closeButton = buttonBox->addButton(QDialogButtonBox::Close);
       
   163     helpButton = buttonBox->addButton(QDialogButtonBox::Help);
       
   164     rotateWidgetsButton = buttonBox->addButton(tr("Rotate &Widgets"),
       
   165                                                QDialogButtonBox::ActionRole);
       
   166 
       
   167     connect(rotateWidgetsButton, SIGNAL(clicked()), this, SLOT(rotateWidgets()));
       
   168     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
       
   169     connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
       
   170 }