1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include <QtGui>
42 
43 #include "window.h"
44 
45 //! [0]
Window()46 Window::Window()
47 {
48     createSpinBoxes();
49     createDateTimeEdits();
50     createDoubleSpinBoxes();
51 
52     QHBoxLayout *layout = new QHBoxLayout;
53     layout->addWidget(spinBoxesGroup);
54     layout->addWidget(editsGroup);
55     layout->addWidget(doubleSpinBoxesGroup);
56     setLayout(layout);
57 
58     setWindowTitle(tr("Spin Boxes"));
59 }
60 //! [0]
61 
62 //! [1]
createSpinBoxes()63 void Window::createSpinBoxes()
64 {
65     spinBoxesGroup = new QGroupBox(tr("Spinboxes"));
66 
67     QLabel *integerLabel = new QLabel(tr("Enter a value between "
68         "%1 and %2:").arg(-20).arg(20));
69     QSpinBox *integerSpinBox = new QSpinBox;
70     integerSpinBox->setRange(-20, 20);
71     integerSpinBox->setSingleStep(1);
72     integerSpinBox->setValue(0);
73 //! [1]
74 
75 //! [2]
76     QLabel *zoomLabel = new QLabel(tr("Enter a zoom value between "
77         "%1 and %2:").arg(0).arg(1000));
78 //! [3]
79     QSpinBox *zoomSpinBox = new QSpinBox;
80     zoomSpinBox->setRange(0, 1000);
81     zoomSpinBox->setSingleStep(10);
82     zoomSpinBox->setSuffix("%");
83     zoomSpinBox->setSpecialValueText(tr("Automatic"));
84     zoomSpinBox->setValue(100);
85 //! [2] //! [3]
86 
87 //! [4]
88     QLabel *priceLabel = new QLabel(tr("Enter a price between "
89         "%1 and %2:").arg(0).arg(999));
90     QSpinBox *priceSpinBox = new QSpinBox;
91     priceSpinBox->setRange(0, 999);
92     priceSpinBox->setSingleStep(1);
93     priceSpinBox->setPrefix("$");
94     priceSpinBox->setValue(99);
95 //! [4] //! [5]
96 
97     QVBoxLayout *spinBoxLayout = new QVBoxLayout;
98     spinBoxLayout->addWidget(integerLabel);
99     spinBoxLayout->addWidget(integerSpinBox);
100     spinBoxLayout->addWidget(zoomLabel);
101     spinBoxLayout->addWidget(zoomSpinBox);
102     spinBoxLayout->addWidget(priceLabel);
103     spinBoxLayout->addWidget(priceSpinBox);
104     spinBoxesGroup->setLayout(spinBoxLayout);
105 }
106 //! [5]
107 
108 //! [6]
createDateTimeEdits()109 void Window::createDateTimeEdits()
110 {
111     editsGroup = new QGroupBox(tr("Date and time spin boxes"));
112 
113     QLabel *dateLabel = new QLabel;
114     QDateEdit *dateEdit = new QDateEdit(QDate::currentDate());
115     dateEdit->setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31));
116     dateLabel->setText(tr("Appointment date (between %0 and %1):")
117                        .arg(dateEdit->minimumDate().toString(Qt::ISODate))
118                        .arg(dateEdit->maximumDate().toString(Qt::ISODate)));
119 //! [6]
120 
121 //! [7]
122     QLabel *timeLabel = new QLabel;
123     QTimeEdit *timeEdit = new QTimeEdit(QTime::currentTime());
124     timeEdit->setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0));
125     timeLabel->setText(tr("Appointment time (between %0 and %1):")
126                        .arg(timeEdit->minimumTime().toString(Qt::ISODate))
127                        .arg(timeEdit->maximumTime().toString(Qt::ISODate)));
128 //! [7]
129 
130 //! [8]
131     meetingLabel = new QLabel;
132     meetingEdit = new QDateTimeEdit(QDateTime::currentDateTime());
133 //! [8]
134 
135 //! [9]
136     QLabel *formatLabel = new QLabel(tr("Format string for the meeting date "
137                                         "and time:"));
138     QComboBox *formatComboBox = new QComboBox;
139     formatComboBox->addItem("yyyy-MM-dd hh:mm:ss (zzz 'ms')");
140     formatComboBox->addItem("hh:mm:ss MM/dd/yyyy");
141     formatComboBox->addItem("hh:mm:ss dd/MM/yyyy");
142     formatComboBox->addItem("hh:mm:ss");
143     formatComboBox->addItem("hh:mm ap");
144 //! [9] //! [10]
145 
146     connect(formatComboBox, SIGNAL(activated(QString)),
147             this, SLOT(setFormatString(QString)));
148 //! [10]
149 
150     setFormatString(formatComboBox->currentText());
151 
152 //! [11]
153     QVBoxLayout *editsLayout = new QVBoxLayout;
154     editsLayout->addWidget(dateLabel);
155     editsLayout->addWidget(dateEdit);
156     editsLayout->addWidget(timeLabel);
157     editsLayout->addWidget(timeEdit);
158     editsLayout->addWidget(meetingLabel);
159     editsLayout->addWidget(meetingEdit);
160     editsLayout->addWidget(formatLabel);
161     editsLayout->addWidget(formatComboBox);
162     editsGroup->setLayout(editsLayout);
163 }
164 //! [11]
165 
166 //! [12]
setFormatString(const QString & formatString)167 void Window::setFormatString(const QString &formatString)
168 {
169     meetingEdit->setDisplayFormat(formatString);
170 //! [12] //! [13]
171     if (meetingEdit->displayedSections() & QDateTimeEdit::DateSections_Mask) {
172         meetingEdit->setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30));
173         meetingLabel->setText(tr("Meeting date (between %0 and %1):")
174             .arg(meetingEdit->minimumDate().toString(Qt::ISODate))
175 	    .arg(meetingEdit->maximumDate().toString(Qt::ISODate)));
176     } else {
177         meetingEdit->setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0));
178         meetingLabel->setText(tr("Meeting time (between %0 and %1):")
179             .arg(meetingEdit->minimumTime().toString(Qt::ISODate))
180 	    .arg(meetingEdit->maximumTime().toString(Qt::ISODate)));
181     }
182 }
183 //! [13]
184 
185 //! [14]
createDoubleSpinBoxes()186 void Window::createDoubleSpinBoxes()
187 {
188     doubleSpinBoxesGroup = new QGroupBox(tr("Double precision spinboxes"));
189 
190     QLabel *precisionLabel = new QLabel(tr("Number of decimal places "
191                                            "to show:"));
192     QSpinBox *precisionSpinBox = new QSpinBox;
193     precisionSpinBox->setRange(0, 100);
194     precisionSpinBox->setValue(2);
195 //! [14]
196 
197 //! [15]
198     QLabel *doubleLabel = new QLabel(tr("Enter a value between "
199         "%1 and %2:").arg(-20).arg(20));
200     doubleSpinBox = new QDoubleSpinBox;
201     doubleSpinBox->setRange(-20.0, 20.0);
202     doubleSpinBox->setSingleStep(1.0);
203     doubleSpinBox->setValue(0.0);
204 //! [15]
205 
206 //! [16]
207     QLabel *scaleLabel = new QLabel(tr("Enter a scale factor between "
208         "%1 and %2:").arg(0).arg(1000.0));
209     scaleSpinBox = new QDoubleSpinBox;
210     scaleSpinBox->setRange(0.0, 1000.0);
211     scaleSpinBox->setSingleStep(10.0);
212     scaleSpinBox->setSuffix("%");
213     scaleSpinBox->setSpecialValueText(tr("No scaling"));
214     scaleSpinBox->setValue(100.0);
215 //! [16]
216 
217 //! [17]
218     QLabel *priceLabel = new QLabel(tr("Enter a price between "
219         "%1 and %2:").arg(0).arg(1000));
220     priceSpinBox = new QDoubleSpinBox;
221     priceSpinBox->setRange(0.0, 1000.0);
222     priceSpinBox->setSingleStep(1.0);
223     priceSpinBox->setPrefix("$");
224     priceSpinBox->setValue(99.99);
225 
226     connect(precisionSpinBox, SIGNAL(valueChanged(int)),
227 //! [17]
228             this, SLOT(changePrecision(int)));
229 
230 //! [18]
231     QVBoxLayout *spinBoxLayout = new QVBoxLayout;
232     spinBoxLayout->addWidget(precisionLabel);
233     spinBoxLayout->addWidget(precisionSpinBox);
234     spinBoxLayout->addWidget(doubleLabel);
235     spinBoxLayout->addWidget(doubleSpinBox);
236     spinBoxLayout->addWidget(scaleLabel);
237     spinBoxLayout->addWidget(scaleSpinBox);
238     spinBoxLayout->addWidget(priceLabel);
239     spinBoxLayout->addWidget(priceSpinBox);
240     doubleSpinBoxesGroup->setLayout(spinBoxLayout);
241 }
242 //! [18]
243 
244 //! [19]
changePrecision(int decimals)245 void Window::changePrecision(int decimals)
246 {
247     doubleSpinBox->setDecimals(decimals);
248     scaleSpinBox->setDecimals(decimals);
249     priceSpinBox->setDecimals(decimals);
250 }
251 //! [19]
252