examples/widgets/spinboxes/window.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 "window.h"
       
    45 
       
    46 //! [0]
       
    47 Window::Window()
       
    48 {
       
    49     createSpinBoxes();
       
    50     createDateTimeEdits();
       
    51     createDoubleSpinBoxes();
       
    52 
       
    53     QHBoxLayout *layout = new QHBoxLayout;
       
    54     layout->addWidget(spinBoxesGroup);
       
    55     layout->addWidget(editsGroup);
       
    56     layout->addWidget(doubleSpinBoxesGroup);
       
    57     setLayout(layout);
       
    58 
       
    59     setWindowTitle(tr("Spin Boxes"));
       
    60 }
       
    61 //! [0]
       
    62 
       
    63 //! [1]
       
    64 void Window::createSpinBoxes()
       
    65 {
       
    66     spinBoxesGroup = new QGroupBox(tr("Spinboxes"));
       
    67 
       
    68     QLabel *integerLabel = new QLabel(tr("Enter a value between "
       
    69         "%1 and %2:").arg(-20).arg(20));
       
    70     QSpinBox *integerSpinBox = new QSpinBox;
       
    71     integerSpinBox->setRange(-20, 20);
       
    72     integerSpinBox->setSingleStep(1);
       
    73     integerSpinBox->setValue(0);
       
    74 //! [1]
       
    75 
       
    76 //! [2]
       
    77     QLabel *zoomLabel = new QLabel(tr("Enter a zoom value between "
       
    78         "%1 and %2:").arg(0).arg(1000));
       
    79 //! [3]
       
    80     QSpinBox *zoomSpinBox = new QSpinBox;
       
    81     zoomSpinBox->setRange(0, 1000);
       
    82     zoomSpinBox->setSingleStep(10);
       
    83     zoomSpinBox->setSuffix("%");
       
    84     zoomSpinBox->setSpecialValueText(tr("Automatic"));
       
    85     zoomSpinBox->setValue(100);
       
    86 //! [2] //! [3]
       
    87 
       
    88 //! [4]
       
    89     QLabel *priceLabel = new QLabel(tr("Enter a price between "
       
    90         "%1 and %2:").arg(0).arg(999));
       
    91     QSpinBox *priceSpinBox = new QSpinBox;
       
    92     priceSpinBox->setRange(0, 999);
       
    93     priceSpinBox->setSingleStep(1);
       
    94     priceSpinBox->setPrefix("$");
       
    95     priceSpinBox->setValue(99);
       
    96 //! [4] //! [5]
       
    97 
       
    98     QVBoxLayout *spinBoxLayout = new QVBoxLayout;
       
    99     spinBoxLayout->addWidget(integerLabel);
       
   100     spinBoxLayout->addWidget(integerSpinBox);
       
   101     spinBoxLayout->addWidget(zoomLabel);
       
   102     spinBoxLayout->addWidget(zoomSpinBox);
       
   103     spinBoxLayout->addWidget(priceLabel);
       
   104     spinBoxLayout->addWidget(priceSpinBox);
       
   105     spinBoxesGroup->setLayout(spinBoxLayout);
       
   106 }
       
   107 //! [5]
       
   108 
       
   109 //! [6]
       
   110 void Window::createDateTimeEdits()
       
   111 {
       
   112     editsGroup = new QGroupBox(tr("Date and time spin boxes"));
       
   113 
       
   114     QLabel *dateLabel = new QLabel;
       
   115     QDateEdit *dateEdit = new QDateEdit(QDate::currentDate());
       
   116     dateEdit->setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31));
       
   117     dateLabel->setText(tr("Appointment date (between %0 and %1):")
       
   118                        .arg(dateEdit->minimumDate().toString(Qt::ISODate))
       
   119                        .arg(dateEdit->maximumDate().toString(Qt::ISODate)));
       
   120 //! [6]
       
   121 
       
   122 //! [7]
       
   123     QLabel *timeLabel = new QLabel;
       
   124     QTimeEdit *timeEdit = new QTimeEdit(QTime::currentTime());
       
   125     timeEdit->setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0));
       
   126     timeLabel->setText(tr("Appointment time (between %0 and %1):")
       
   127                        .arg(timeEdit->minimumTime().toString(Qt::ISODate))
       
   128                        .arg(timeEdit->maximumTime().toString(Qt::ISODate)));
       
   129 //! [7]
       
   130 
       
   131 //! [8]
       
   132     meetingLabel = new QLabel;
       
   133     meetingEdit = new QDateTimeEdit(QDateTime::currentDateTime());
       
   134 //! [8]
       
   135 
       
   136 //! [9]
       
   137     QLabel *formatLabel = new QLabel(tr("Format string for the meeting date "
       
   138                                         "and time:"));
       
   139     QComboBox *formatComboBox = new QComboBox;
       
   140     formatComboBox->addItem("yyyy-MM-dd hh:mm:ss (zzz 'ms')");
       
   141     formatComboBox->addItem("hh:mm:ss MM/dd/yyyy");
       
   142     formatComboBox->addItem("hh:mm:ss dd/MM/yyyy");
       
   143     formatComboBox->addItem("hh:mm:ss");
       
   144     formatComboBox->addItem("hh:mm ap");
       
   145 //! [9] //! [10]
       
   146 
       
   147     connect(formatComboBox, SIGNAL(activated(const QString &)),
       
   148             this, SLOT(setFormatString(const QString &)));
       
   149 //! [10]
       
   150 
       
   151     setFormatString(formatComboBox->currentText());
       
   152 
       
   153 //! [11]
       
   154     QVBoxLayout *editsLayout = new QVBoxLayout;
       
   155     editsLayout->addWidget(dateLabel);
       
   156     editsLayout->addWidget(dateEdit);
       
   157     editsLayout->addWidget(timeLabel);
       
   158     editsLayout->addWidget(timeEdit);
       
   159     editsLayout->addWidget(meetingLabel);
       
   160     editsLayout->addWidget(meetingEdit);
       
   161     editsLayout->addWidget(formatLabel);
       
   162     editsLayout->addWidget(formatComboBox);
       
   163     editsGroup->setLayout(editsLayout);
       
   164 }
       
   165 //! [11]
       
   166 
       
   167 //! [12]
       
   168 void Window::setFormatString(const QString &formatString)
       
   169 {
       
   170     meetingEdit->setDisplayFormat(formatString);
       
   171 //! [12] //! [13]
       
   172     if (meetingEdit->displayedSections() & QDateTimeEdit::DateSections_Mask) {
       
   173         meetingEdit->setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30));
       
   174         meetingLabel->setText(tr("Meeting date (between %0 and %1):")
       
   175             .arg(meetingEdit->minimumDate().toString(Qt::ISODate))
       
   176 	    .arg(meetingEdit->maximumDate().toString(Qt::ISODate)));
       
   177     } else {
       
   178         meetingEdit->setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0));
       
   179         meetingLabel->setText(tr("Meeting time (between %0 and %1):")
       
   180             .arg(meetingEdit->minimumTime().toString(Qt::ISODate))
       
   181 	    .arg(meetingEdit->maximumTime().toString(Qt::ISODate)));
       
   182     }
       
   183 }
       
   184 //! [13]
       
   185 
       
   186 //! [14]
       
   187 void Window::createDoubleSpinBoxes()
       
   188 {
       
   189     doubleSpinBoxesGroup = new QGroupBox(tr("Double precision spinboxes"));
       
   190 
       
   191     QLabel *precisionLabel = new QLabel(tr("Number of decimal places "
       
   192                                            "to show:"));
       
   193     QSpinBox *precisionSpinBox = new QSpinBox;
       
   194     precisionSpinBox->setRange(0, 100);
       
   195     precisionSpinBox->setValue(2);
       
   196 //! [14]
       
   197 
       
   198 //! [15]
       
   199     QLabel *doubleLabel = new QLabel(tr("Enter a value between "
       
   200         "%1 and %2:").arg(-20).arg(20));
       
   201     doubleSpinBox = new QDoubleSpinBox;
       
   202     doubleSpinBox->setRange(-20.0, 20.0);
       
   203     doubleSpinBox->setSingleStep(1.0);
       
   204     doubleSpinBox->setValue(0.0);
       
   205 //! [15]
       
   206 
       
   207 //! [16]
       
   208     QLabel *scaleLabel = new QLabel(tr("Enter a scale factor between "
       
   209         "%1 and %2:").arg(0).arg(1000.0));
       
   210     scaleSpinBox = new QDoubleSpinBox;
       
   211     scaleSpinBox->setRange(0.0, 1000.0);
       
   212     scaleSpinBox->setSingleStep(10.0);
       
   213     scaleSpinBox->setSuffix("%");
       
   214     scaleSpinBox->setSpecialValueText(tr("No scaling"));
       
   215     scaleSpinBox->setValue(100.0);
       
   216 //! [16]
       
   217 
       
   218 //! [17]
       
   219     QLabel *priceLabel = new QLabel(tr("Enter a price between "
       
   220         "%1 and %2:").arg(0).arg(1000));
       
   221     priceSpinBox = new QDoubleSpinBox;
       
   222     priceSpinBox->setRange(0.0, 1000.0);
       
   223     priceSpinBox->setSingleStep(1.0);
       
   224     priceSpinBox->setPrefix("$");
       
   225     priceSpinBox->setValue(99.99);
       
   226 
       
   227     connect(precisionSpinBox, SIGNAL(valueChanged(int)),
       
   228 //! [17]
       
   229             this, SLOT(changePrecision(int)));
       
   230 
       
   231 //! [18]
       
   232     QVBoxLayout *spinBoxLayout = new QVBoxLayout;
       
   233     spinBoxLayout->addWidget(precisionLabel);
       
   234     spinBoxLayout->addWidget(precisionSpinBox);
       
   235     spinBoxLayout->addWidget(doubleLabel);
       
   236     spinBoxLayout->addWidget(doubleSpinBox);
       
   237     spinBoxLayout->addWidget(scaleLabel);
       
   238     spinBoxLayout->addWidget(scaleSpinBox);
       
   239     spinBoxLayout->addWidget(priceLabel);
       
   240     spinBoxLayout->addWidget(priceSpinBox);
       
   241     doubleSpinBoxesGroup->setLayout(spinBoxLayout);
       
   242 }
       
   243 //! [18]
       
   244 
       
   245 //! [19]
       
   246 void Window::changePrecision(int decimals)
       
   247 {
       
   248     doubleSpinBox->setDecimals(decimals);
       
   249     scaleSpinBox->setDecimals(decimals);
       
   250     priceSpinBox->setDecimals(decimals);
       
   251 }
       
   252 //! [19]