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 QDateTime, Qt, QTimer
46from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox, QDateTimeEdit,
47        QDial, QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
48        QProgressBar, QPushButton, QRadioButton, QScrollBar, QSizePolicy,
49        QSlider, QSpinBox, QStyleFactory, QTableWidget, QTabWidget, QTextEdit,
50        QVBoxLayout, QWidget)
51
52
53class WidgetGallery(QDialog):
54    def __init__(self, parent=None):
55        super(WidgetGallery, self).__init__(parent)
56
57        self.originalPalette = QApplication.palette()
58
59        styleComboBox = QComboBox()
60        styleComboBox.addItems(QStyleFactory.keys())
61
62        styleLabel = QLabel("&Style:")
63        styleLabel.setBuddy(styleComboBox)
64
65        self.useStylePaletteCheckBox = QCheckBox("&Use style's standard palette")
66        self.useStylePaletteCheckBox.setChecked(True)
67
68        disableWidgetsCheckBox = QCheckBox("&Disable widgets")
69
70        self.createTopLeftGroupBox()
71        self.createTopRightGroupBox()
72        self.createBottomLeftTabWidget()
73        self.createBottomRightGroupBox()
74        self.createProgressBar()
75
76        styleComboBox.activated[str].connect(self.changeStyle)
77        self.useStylePaletteCheckBox.toggled.connect(self.changePalette)
78        disableWidgetsCheckBox.toggled.connect(self.topLeftGroupBox.setDisabled)
79        disableWidgetsCheckBox.toggled.connect(self.topRightGroupBox.setDisabled)
80        disableWidgetsCheckBox.toggled.connect(self.bottomLeftTabWidget.setDisabled)
81        disableWidgetsCheckBox.toggled.connect(self.bottomRightGroupBox.setDisabled)
82
83        topLayout = QHBoxLayout()
84        topLayout.addWidget(styleLabel)
85        topLayout.addWidget(styleComboBox)
86        topLayout.addStretch(1)
87        topLayout.addWidget(self.useStylePaletteCheckBox)
88        topLayout.addWidget(disableWidgetsCheckBox)
89
90        mainLayout = QGridLayout()
91        mainLayout.addLayout(topLayout, 0, 0, 1, 2)
92        mainLayout.addWidget(self.topLeftGroupBox, 1, 0)
93        mainLayout.addWidget(self.topRightGroupBox, 1, 1)
94        mainLayout.addWidget(self.bottomLeftTabWidget, 2, 0)
95        mainLayout.addWidget(self.bottomRightGroupBox, 2, 1)
96        mainLayout.addWidget(self.progressBar, 3, 0, 1, 2)
97        mainLayout.setRowStretch(1, 1)
98        mainLayout.setRowStretch(2, 1)
99        mainLayout.setColumnStretch(0, 1)
100        mainLayout.setColumnStretch(1, 1)
101        self.setLayout(mainLayout)
102
103        self.setWindowTitle("Styles")
104        self.changeStyle('Windows')
105
106    def changeStyle(self, styleName):
107        QApplication.setStyle(QStyleFactory.create(styleName))
108        self.changePalette()
109
110    def changePalette(self):
111        if (self.useStylePaletteCheckBox.isChecked()):
112            QApplication.setPalette(QApplication.style().standardPalette())
113        else:
114            QApplication.setPalette(self.originalPalette)
115
116    def advanceProgressBar(self):
117        curVal = self.progressBar.value()
118        maxVal = self.progressBar.maximum()
119        self.progressBar.setValue(curVal + (maxVal - curVal) / 100)
120
121    def createTopLeftGroupBox(self):
122        self.topLeftGroupBox = QGroupBox("Group 1")
123
124        radioButton1 = QRadioButton("Radio button 1")
125        radioButton2 = QRadioButton("Radio button 2")
126        radioButton3 = QRadioButton("Radio button 3")
127        radioButton1.setChecked(True)
128
129        checkBox = QCheckBox("Tri-state check box")
130        checkBox.setTristate(True)
131        checkBox.setCheckState(Qt.PartiallyChecked)
132
133        layout = QVBoxLayout()
134        layout.addWidget(radioButton1)
135        layout.addWidget(radioButton2)
136        layout.addWidget(radioButton3)
137        layout.addWidget(checkBox)
138        layout.addStretch(1)
139        self.topLeftGroupBox.setLayout(layout)
140
141    def createTopRightGroupBox(self):
142        self.topRightGroupBox = QGroupBox("Group 2")
143
144        defaultPushButton = QPushButton("Default Push Button")
145        defaultPushButton.setDefault(True)
146
147        togglePushButton = QPushButton("Toggle Push Button")
148        togglePushButton.setCheckable(True)
149        togglePushButton.setChecked(True)
150
151        flatPushButton = QPushButton("Flat Push Button")
152        flatPushButton.setFlat(True)
153
154        layout = QVBoxLayout()
155        layout.addWidget(defaultPushButton)
156        layout.addWidget(togglePushButton)
157        layout.addWidget(flatPushButton)
158        layout.addStretch(1)
159        self.topRightGroupBox.setLayout(layout)
160
161    def createBottomLeftTabWidget(self):
162        self.bottomLeftTabWidget = QTabWidget()
163        self.bottomLeftTabWidget.setSizePolicy(QSizePolicy.Preferred,
164                QSizePolicy.Ignored)
165
166        tab1 = QWidget()
167        tableWidget = QTableWidget(10, 10)
168
169        tab1hbox = QHBoxLayout()
170        tab1hbox.setContentsMargins(5, 5, 5, 5)
171        tab1hbox.addWidget(tableWidget)
172        tab1.setLayout(tab1hbox)
173
174        tab2 = QWidget()
175        textEdit = QTextEdit()
176
177        textEdit.setPlainText("Twinkle, twinkle, little star,\n"
178                              "How I wonder what you are.\n"
179                              "Up above the world so high,\n"
180                              "Like a diamond in the sky.\n"
181                              "Twinkle, twinkle, little star,\n"
182                              "How I wonder what you are!\n")
183
184        tab2hbox = QHBoxLayout()
185        tab2hbox.setContentsMargins(5, 5, 5, 5)
186        tab2hbox.addWidget(textEdit)
187        tab2.setLayout(tab2hbox)
188
189        self.bottomLeftTabWidget.addTab(tab1, "&Table")
190        self.bottomLeftTabWidget.addTab(tab2, "Text &Edit")
191
192    def createBottomRightGroupBox(self):
193        self.bottomRightGroupBox = QGroupBox("Group 3")
194        self.bottomRightGroupBox.setCheckable(True)
195        self.bottomRightGroupBox.setChecked(True)
196
197        lineEdit = QLineEdit('s3cRe7')
198        lineEdit.setEchoMode(QLineEdit.Password)
199
200        spinBox = QSpinBox(self.bottomRightGroupBox)
201        spinBox.setValue(50)
202
203        dateTimeEdit = QDateTimeEdit(self.bottomRightGroupBox)
204        dateTimeEdit.setDateTime(QDateTime.currentDateTime())
205
206        slider = QSlider(Qt.Horizontal, self.bottomRightGroupBox)
207        slider.setValue(40)
208
209        scrollBar = QScrollBar(Qt.Horizontal, self.bottomRightGroupBox)
210        scrollBar.setValue(60)
211
212        dial = QDial(self.bottomRightGroupBox)
213        dial.setValue(30)
214        dial.setNotchesVisible(True)
215
216        layout = QGridLayout()
217        layout.addWidget(lineEdit, 0, 0, 1, 2)
218        layout.addWidget(spinBox, 1, 0, 1, 2)
219        layout.addWidget(dateTimeEdit, 2, 0, 1, 2)
220        layout.addWidget(slider, 3, 0)
221        layout.addWidget(scrollBar, 4, 0)
222        layout.addWidget(dial, 3, 1, 2, 1)
223        layout.setRowStretch(5, 1)
224        self.bottomRightGroupBox.setLayout(layout)
225
226    def createProgressBar(self):
227        self.progressBar = QProgressBar()
228        self.progressBar.setRange(0, 10000)
229        self.progressBar.setValue(0)
230
231        timer = QTimer(self)
232        timer.timeout.connect(self.advanceProgressBar)
233        timer.start(1000)
234
235
236if __name__ == '__main__':
237
238    import sys
239
240    app = QApplication(sys.argv)
241    gallery = WidgetGallery()
242    gallery.show()
243    sys.exit(app.exec_())
244