1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2010 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 45import sys 46 47from PyQt5.QtCore import QDir, Qt 48from PyQt5.QtGui import QFont, QPalette 49from PyQt5.QtWidgets import (QApplication, QCheckBox, QColorDialog, QDialog, 50 QErrorMessage, QFileDialog, QFontDialog, QFrame, QGridLayout, 51 QInputDialog, QLabel, QLineEdit, QMessageBox, QPushButton) 52 53 54class Dialog(QDialog): 55 MESSAGE = "<p>Message boxes have a caption, a text, and up to three " \ 56 "buttons, each with standard or custom texts.</p>" \ 57 "<p>Click a button to close the message box. Pressing the Esc " \ 58 "button will activate the detected escape button (if any).</p>" 59 60 def __init__(self, parent=None): 61 super(Dialog, self).__init__(parent) 62 63 self.openFilesPath = '' 64 65 self.errorMessageDialog = QErrorMessage(self) 66 67 frameStyle = QFrame.Sunken | QFrame.Panel 68 69 self.integerLabel = QLabel() 70 self.integerLabel.setFrameStyle(frameStyle) 71 self.integerButton = QPushButton("QInputDialog.get&Int()") 72 73 self.doubleLabel = QLabel() 74 self.doubleLabel.setFrameStyle(frameStyle) 75 self.doubleButton = QPushButton("QInputDialog.get&Double()") 76 77 self.itemLabel = QLabel() 78 self.itemLabel.setFrameStyle(frameStyle) 79 self.itemButton = QPushButton("QInputDialog.getIte&m()") 80 81 self.textLabel = QLabel() 82 self.textLabel.setFrameStyle(frameStyle) 83 self.textButton = QPushButton("QInputDialog.get&Text()") 84 85 self.colorLabel = QLabel() 86 self.colorLabel.setFrameStyle(frameStyle) 87 self.colorButton = QPushButton("QColorDialog.get&Color()") 88 89 self.fontLabel = QLabel() 90 self.fontLabel.setFrameStyle(frameStyle) 91 self.fontButton = QPushButton("QFontDialog.get&Font()") 92 93 self.directoryLabel = QLabel() 94 self.directoryLabel.setFrameStyle(frameStyle) 95 self.directoryButton = QPushButton("QFileDialog.getE&xistingDirectory()") 96 97 self.openFileNameLabel = QLabel() 98 self.openFileNameLabel.setFrameStyle(frameStyle) 99 self.openFileNameButton = QPushButton("QFileDialog.get&OpenFileName()") 100 101 self.openFileNamesLabel = QLabel() 102 self.openFileNamesLabel.setFrameStyle(frameStyle) 103 self.openFileNamesButton = QPushButton("QFileDialog.&getOpenFileNames()") 104 105 self.saveFileNameLabel = QLabel() 106 self.saveFileNameLabel.setFrameStyle(frameStyle) 107 self.saveFileNameButton = QPushButton("QFileDialog.get&SaveFileName()") 108 109 self.criticalLabel = QLabel() 110 self.criticalLabel.setFrameStyle(frameStyle) 111 self.criticalButton = QPushButton("QMessageBox.critica&l()") 112 113 self.informationLabel = QLabel() 114 self.informationLabel.setFrameStyle(frameStyle) 115 self.informationButton = QPushButton("QMessageBox.i&nformation()") 116 117 self.questionLabel = QLabel() 118 self.questionLabel.setFrameStyle(frameStyle) 119 self.questionButton = QPushButton("QMessageBox.&question()") 120 121 self.warningLabel = QLabel() 122 self.warningLabel.setFrameStyle(frameStyle) 123 self.warningButton = QPushButton("QMessageBox.&warning()") 124 125 self.errorLabel = QLabel() 126 self.errorLabel.setFrameStyle(frameStyle) 127 self.errorButton = QPushButton("QErrorMessage.show&M&essage()") 128 129 self.integerButton.clicked.connect(self.setInteger) 130 self.doubleButton.clicked.connect(self.setDouble) 131 self.itemButton.clicked.connect(self.setItem) 132 self.textButton.clicked.connect(self.setText) 133 self.colorButton.clicked.connect(self.setColor) 134 self.fontButton.clicked.connect(self.setFont) 135 self.directoryButton.clicked.connect(self.setExistingDirectory) 136 self.openFileNameButton.clicked.connect(self.setOpenFileName) 137 self.openFileNamesButton.clicked.connect(self.setOpenFileNames) 138 self.saveFileNameButton.clicked.connect(self.setSaveFileName) 139 self.criticalButton.clicked.connect(self.criticalMessage) 140 self.informationButton.clicked.connect(self.informationMessage) 141 self.questionButton.clicked.connect(self.questionMessage) 142 self.warningButton.clicked.connect(self.warningMessage) 143 self.errorButton.clicked.connect(self.errorMessage) 144 145 self.native = QCheckBox() 146 self.native.setText("Use native file dialog.") 147 self.native.setChecked(True) 148 if sys.platform not in ("win32", "darwin"): 149 self.native.hide() 150 151 layout = QGridLayout() 152 layout.setColumnStretch(1, 1) 153 layout.setColumnMinimumWidth(1, 250) 154 layout.addWidget(self.integerButton, 0, 0) 155 layout.addWidget(self.integerLabel, 0, 1) 156 layout.addWidget(self.doubleButton, 1, 0) 157 layout.addWidget(self.doubleLabel, 1, 1) 158 layout.addWidget(self.itemButton, 2, 0) 159 layout.addWidget(self.itemLabel, 2, 1) 160 layout.addWidget(self.textButton, 3, 0) 161 layout.addWidget(self.textLabel, 3, 1) 162 layout.addWidget(self.colorButton, 4, 0) 163 layout.addWidget(self.colorLabel, 4, 1) 164 layout.addWidget(self.fontButton, 5, 0) 165 layout.addWidget(self.fontLabel, 5, 1) 166 layout.addWidget(self.directoryButton, 6, 0) 167 layout.addWidget(self.directoryLabel, 6, 1) 168 layout.addWidget(self.openFileNameButton, 7, 0) 169 layout.addWidget(self.openFileNameLabel, 7, 1) 170 layout.addWidget(self.openFileNamesButton, 8, 0) 171 layout.addWidget(self.openFileNamesLabel, 8, 1) 172 layout.addWidget(self.saveFileNameButton, 9, 0) 173 layout.addWidget(self.saveFileNameLabel, 9, 1) 174 layout.addWidget(self.criticalButton, 10, 0) 175 layout.addWidget(self.criticalLabel, 10, 1) 176 layout.addWidget(self.informationButton, 11, 0) 177 layout.addWidget(self.informationLabel, 11, 1) 178 layout.addWidget(self.questionButton, 12, 0) 179 layout.addWidget(self.questionLabel, 12, 1) 180 layout.addWidget(self.warningButton, 13, 0) 181 layout.addWidget(self.warningLabel, 13, 1) 182 layout.addWidget(self.errorButton, 14, 0) 183 layout.addWidget(self.errorLabel, 14, 1) 184 layout.addWidget(self.native, 15, 0) 185 self.setLayout(layout) 186 187 self.setWindowTitle("Standard Dialogs") 188 189 def setInteger(self): 190 i, ok = QInputDialog.getInt(self, "QInputDialog.getInt()", 191 "Percentage:", 25, 0, 100, 1) 192 if ok: 193 self.integerLabel.setText("%d%%" % i) 194 195 def setDouble(self): 196 d, ok = QInputDialog.getDouble(self, "QInputDialog.getDouble()", 197 "Amount:", 37.56, -10000, 10000, 2) 198 if ok: 199 self.doubleLabel.setText("$%g" % d) 200 201 def setItem(self): 202 items = ("Spring", "Summer", "Fall", "Winter") 203 204 item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()", 205 "Season:", items, 0, False) 206 if ok and item: 207 self.itemLabel.setText(item) 208 209 def setText(self): 210 text, ok = QInputDialog.getText(self, "QInputDialog.getText()", 211 "User name:", QLineEdit.Normal, QDir.home().dirName()) 212 if ok and text != '': 213 self.textLabel.setText(text) 214 215 def setColor(self): 216 color = QColorDialog.getColor(Qt.green, self) 217 if color.isValid(): 218 self.colorLabel.setText(color.name()) 219 self.colorLabel.setPalette(QPalette(color)) 220 self.colorLabel.setAutoFillBackground(True) 221 222 def setFont(self): 223 font, ok = QFontDialog.getFont(QFont(self.fontLabel.text()), self) 224 if ok: 225 self.fontLabel.setText(font.key()) 226 self.fontLabel.setFont(font) 227 228 def setExistingDirectory(self): 229 options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly 230 directory = QFileDialog.getExistingDirectory(self, 231 "QFileDialog.getExistingDirectory()", 232 self.directoryLabel.text(), options=options) 233 if directory: 234 self.directoryLabel.setText(directory) 235 236 def setOpenFileName(self): 237 options = QFileDialog.Options() 238 if not self.native.isChecked(): 239 options |= QFileDialog.DontUseNativeDialog 240 fileName, _ = QFileDialog.getOpenFileName(self, 241 "QFileDialog.getOpenFileName()", self.openFileNameLabel.text(), 242 "All Files (*);;Text Files (*.txt)", options=options) 243 if fileName: 244 self.openFileNameLabel.setText(fileName) 245 246 def setOpenFileNames(self): 247 options = QFileDialog.Options() 248 if not self.native.isChecked(): 249 options |= QFileDialog.DontUseNativeDialog 250 files, _ = QFileDialog.getOpenFileNames(self, 251 "QFileDialog.getOpenFileNames()", self.openFilesPath, 252 "All Files (*);;Text Files (*.txt)", options=options) 253 if files: 254 self.openFilesPath = files[0] 255 self.openFileNamesLabel.setText("[%s]" % ', '.join(files)) 256 257 def setSaveFileName(self): 258 options = QFileDialog.Options() 259 if not self.native.isChecked(): 260 options |= QFileDialog.DontUseNativeDialog 261 fileName, _ = QFileDialog.getSaveFileName(self, 262 "QFileDialog.getSaveFileName()", 263 self.saveFileNameLabel.text(), 264 "All Files (*);;Text Files (*.txt)", options=options) 265 if fileName: 266 self.saveFileNameLabel.setText(fileName) 267 268 def criticalMessage(self): 269 reply = QMessageBox.critical(self, "QMessageBox.critical()", 270 Dialog.MESSAGE, 271 QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore) 272 if reply == QMessageBox.Abort: 273 self.criticalLabel.setText("Abort") 274 elif reply == QMessageBox.Retry: 275 self.criticalLabel.setText("Retry") 276 else: 277 self.criticalLabel.setText("Ignore") 278 279 def informationMessage(self): 280 reply = QMessageBox.information(self, 281 "QMessageBox.information()", Dialog.MESSAGE) 282 if reply == QMessageBox.Ok: 283 self.informationLabel.setText("OK") 284 else: 285 self.informationLabel.setText("Escape") 286 287 def questionMessage(self): 288 reply = QMessageBox.question(self, "QMessageBox.question()", 289 Dialog.MESSAGE, 290 QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) 291 if reply == QMessageBox.Yes: 292 self.questionLabel.setText("Yes") 293 elif reply == QMessageBox.No: 294 self.questionLabel.setText("No") 295 else: 296 self.questionLabel.setText("Cancel") 297 298 def warningMessage(self): 299 msgBox = QMessageBox(QMessageBox.Warning, "QMessageBox.warning()", 300 Dialog.MESSAGE, QMessageBox.NoButton, self) 301 msgBox.addButton("Save &Again", QMessageBox.AcceptRole) 302 msgBox.addButton("&Continue", QMessageBox.RejectRole) 303 if msgBox.exec_() == QMessageBox.AcceptRole: 304 self.warningLabel.setText("Save Again") 305 else: 306 self.warningLabel.setText("Continue") 307 308 def errorMessage(self): 309 self.errorMessageDialog.showMessage("This dialog shows and remembers " 310 "error messages. If the checkbox is checked (as it is by " 311 "default), the shown message will be shown again, but if the " 312 "user unchecks the box the message will not appear again if " 313 "QErrorMessage.showMessage() is called with the same message.") 314 self.errorLabel.setText("If the box is unchecked, the message won't " 315 "appear again.") 316 317 318if __name__ == '__main__': 319 app = QApplication(sys.argv) 320 dialog = Dialog() 321 dialog.show() 322 sys.exit(app.exec_()) 323