1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
A basic Dialog class.
"""
import functools
import operator
from PyQt5.QtCore import QRegExp, QSize, Qt
from PyQt5.QtGui import QIcon, QPixmap, QRegExpValidator
from PyQt5.QtWidgets import (
QDialog, QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QStyle,
QWidget)
from . import Separator
__all__ = ['Dialog', 'TextDialog']
standardicons = {
'info': QStyle.SP_MessageBoxInformation,
'warning': QStyle.SP_MessageBoxWarning,
'critical': QStyle.SP_MessageBoxCritical,
'question': QStyle.SP_MessageBoxQuestion,
}
standardbuttons = {
'ok': QDialogButtonBox.Ok,
'open': QDialogButtonBox.Open,
'save': QDialogButtonBox.Save,
'cancel': QDialogButtonBox.Cancel,
'close': QDialogButtonBox.Close,
'discard': QDialogButtonBox.Discard,
'apply': QDialogButtonBox.Apply,
'reset': QDialogButtonBox.Reset,
'restoredefaults': QDialogButtonBox.RestoreDefaults,
'help': QDialogButtonBox.Help,
'saveall': QDialogButtonBox.SaveAll,
'yes': QDialogButtonBox.Yes,
'yestoall': QDialogButtonBox.YesToAll,
'no': QDialogButtonBox.No,
'notoall': QDialogButtonBox.NoToAll,
'abort': QDialogButtonBox.Abort,
'retry': QDialogButtonBox.Retry,
'ignore': QDialogButtonBox.Ignore,
}
class Dialog(QDialog):
"""A Dialog with basic layout features:
a main widget,
an icon or pixmap,
a separator,
buttons (provided by a QDialogButtonBox)
"""
def __init__(self,
parent = None,
message = "",
title = "",
icon = None,
iconSize = QSize(64, 64),
pixmap = None,
separator = True,
buttonOrientation = Qt.Horizontal,
buttons = ('ok', 'cancel'),
help = None,
**kwargs):
"""Initializes the dialog.
parent = a parent widget or None.
The following keyword arguments are recognized:
- message: the text to display in the message label
- title: the window title
- icon or pixmap: shown in the left area
- iconSize: size of the icon in the left (QSize, default: 64x64)
- separator: draw a separator line or not (default: True)
- buttonOrientation: Qt.Horizontal (default) or Qt.Vertical
- buttons: which buttons to use (default: Ok, Cancel)
- help: function to call when a help button is clicked.
Other keyword arguments are passed to QDialog.
"""
super(Dialog, self).__init__(parent, **kwargs)
self._icon = QIcon()
self._separatorWidget = Separator()
self._mainWidget = QWidget()
self._pixmap = QPixmap()
self._pixmapLabel = QLabel(self)
self._messageLabel = QLabel(self)
self._buttonBox = b = QDialogButtonBox(self)
b.accepted.connect(self.accept)
b.rejected.connect(self.reject)
layout = QGridLayout()
layout.setSpacing(10)
self.setLayout(layout)
# handle keyword args
self._buttonOrientation = buttonOrientation
self._iconSize = iconSize
self._separator = separator
if title:
self.setWindowTitle(title)
self.setMessage(message)
if icon:
self.setIcon(icon)
elif pixmap:
self.setPixmap(pixmap)
b.helpRequested.connect(help or self.helpRequest)
self.setStandardButtons(buttons)
self.reLayout()
def helpRequest(self):
"""Called when a help button is clicked."""
pass
def setButtonOrientation(self, orientation):
"""Sets the button orientation.
Qt.Horizontal (default) puts the buttons at the bottom of the dialog
in a horizontal row, Qt.Vertical puts the buttons at the right in a
vertical column.
"""
if orientation != self._buttonOrientation:
self._buttonOrientation = orientation
self._buttonBox.setOrientation(orientation)
self.reLayout()
def buttonOrientation(self):
"""Returns the button orientation."""
return self._buttonOrientation
def setIcon(self, icon):
"""Sets the icon to display in the left area.
May be:
- None or QIcon()
- one of 'info', 'warning', 'critical', 'question'
- a QStyle.StandardPixmap
- a QIcon.
"""
if icon in standardicons:
icon = standardicons[icon]
if isinstance(icon, QStyle.StandardPixmap):
icon = self.style().standardIcon(icon)
if icon is None:
icon = QIcon()
self._icon = icon
self.setPixmap(icon.pixmap(self._iconSize))
def icon(self):
"""Returns the currently set icon as a QIcon."""
return self._icon
def setIconSize(self, size):
"""Sets the icon size (QSize or int)."""
if isinstance(size, int):
size = QSize(size, size)
changed = size != self._iconSize
self._iconSize = size
if changed and not self._icon.isNull():
self.setPixmap(self._icon.pixmap(size))
def iconSize(self):
"""Returns the icon size (QSize)."""
return self._iconSize
def setPixmap(self, pixmap):
"""Sets the pixmap to display in the left area."""
changed = self._pixmap.isNull() != pixmap.isNull()
self._pixmap = pixmap
self._pixmapLabel.setPixmap(pixmap)
if not pixmap.isNull():
self._pixmapLabel.setFixedSize(pixmap.size())
if changed:
self.reLayout()
def pixmap(self):
"""Returns the currently set pixmap."""
return self._pixmap
def setMessage(self, text):
"""Sets the main text in the dialog."""
self._messageLabel.setText(text)
def message(self):
"""Returns the main text."""
return self._messageLabel.text()
def messageLabel(self):
"""Returns the QLabel displaying the message text."""
return self._messageLabel
def buttonBox(self):
"""Returns our QDialogButtonBox instance."""
return self._buttonBox
def setStandardButtons(self, buttons):
"""Convenience method to set standard buttons in the button box.
Accepts a sequence of string names from the standardbuttons constant,
or a QDialogButtonBox.StandardButtons value.
"""
if isinstance(buttons, (set, tuple, list)):
buttons = functools.reduce(operator.or_,
map(standardbuttons.get, buttons),
QDialogButtonBox.StandardButtons())
self._buttonBox.setStandardButtons(buttons)
def button(self, button):
"""Returns the given button.
May be a QDialogButtonBox.StandardButton or a key from standardbuttons.
"""
if button in standardbuttons:
button = standardbuttons[button]
return self._buttonBox.button(button)
def setSeparator(self, enabled):
"""Sets whether to show a line between contents and buttons."""
changed = self._separator != enabled
self._separator = enabled
if changed:
self.reLayout()
def hasSeparator(self):
"""Returns whether a separator line is shown."""
return self._separator
def setMainWidget(self, widget):
"""Sets the specified widget as our main widget."""
old = self._mainWidget
if old:
old.setParent(None)
self._mainWidget = widget
self.reLayout()
def mainWidget(self):
"""Returns the current main widget (an empty QWidget by default)."""
return self._mainWidget
def reLayout(self):
"""(Internal) Lays out all items in this dialog."""
layout = self.layout()
while layout.takeAt(0):
pass
if not self._pixmap.isNull():
col = 1
layout.addWidget(self._pixmapLabel, 0, 0, 2, 1)
else:
layout.setColumnStretch(1, 0)
col = 0
layout.setColumnStretch(col, 1)
self._pixmapLabel.setVisible(not self._pixmap.isNull())
layout.addWidget(self._messageLabel, 0, col)
layout.addWidget(self._mainWidget, 1, col)
if self._buttonOrientation == Qt.Horizontal:
if self._separator:
layout.addWidget(self._separatorWidget, 2, 0, 1, col+1)
layout.addWidget(self._buttonBox, 3, 0, 1, col+1)
else:
if self._separator:
layout.addWidget(self._separatorWidget, 0, col+1, 2, 1)
layout.addWidget(self._buttonBox, 0, col+2, 2, 1)
self._separatorWidget.setVisible(self._separator)
class TextDialog(Dialog):
"""A dialog with text string input and validation."""
def __init__(self, parent, *args, **kwargs):
super(TextDialog, self).__init__(parent, *args, **kwargs)
self._validateFunction = None
self.setMainWidget(QLineEdit())
self.lineEdit().setFocus()
def lineEdit(self):
"""Returns the QLineEdit widget."""
return self.mainWidget()
def setText(self, text):
"""Sets the text in the lineEdit()."""
self.lineEdit().setText(text)
def text(self):
"""Returns the text in the lineEdit()."""
return self.lineEdit().text()
def setValidateFunction(self, func):
"""Sets a function to run on every change in the lineEdit().
If the function returns True, the OK button is enabled, otherwise
disabled.
If func is None, an earlier validate function will be removed.
"""
old = self._validateFunction
self._validateFunction = func
if func:
self._validate(self.lineEdit().text())
if not old:
self.lineEdit().textChanged.connect(self._validate)
elif old:
self.lineEdit().textChanged.disconnect(self._validate)
self.button('ok').setEnabled(True)
def setValidateRegExp(self, regexp):
"""Sets a regular expression the text must match.
If the regular expression matches the full text, the OK button is
enabled, otherwise disabled.
If regexp is None, an earlier set regular expression is removed.
"""
validator = function = None
if regexp is not None:
rx = QRegExp(regexp)
validator = QRegExpValidator(rx, self.lineEdit())
function = rx.exactMatch
self.lineEdit().setValidator(validator)
self.setValidateFunction(function)
def _validate(self, text):
self.button('ok').setEnabled(self._validateFunction(text))
|