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