class ValidatedDialog(QDialog): """ A dialog for creating a validated new value. Performs validation of name against a provided. Can be used to select from the list or for creating a new value that is not on the list. """ INVALID_COLOR = QColor(255, 235, 235) def __init__(self, title="Title", description="Description", unique_names=None, choose_from_list=False): QDialog.__init__(self) self.setModal(True) self.setWindowTitle(title) # self.setMinimumWidth(250) # self.setMinimumHeight(150) if unique_names is None: unique_names = [] self.unique_names = unique_names self.choose_from_list = choose_from_list self.layout = QFormLayout() self.layout.setSizeConstraint(QLayout.SetFixedSize) label = QLabel(description) label.setAlignment(Qt.AlignHCenter) self.layout.addRow(self.createSpace(5)) self.layout.addRow(label) self.layout.addRow(self.createSpace(10)) buttons = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) if choose_from_list: self.param_name_combo = QComboBox() self.connect(self.param_name_combo, SIGNAL('currentIndexChanged(QString)'), self.validateChoice) for item in unique_names: self.param_name_combo.addItem(item) self.layout.addRow("Job:", self.param_name_combo) else: self.param_name = QLineEdit(self) self.param_name.setFocus() self.connect(self.param_name, SIGNAL('textChanged(QString)'), self.validateName) self.validColor = self.param_name.palette().color( self.param_name.backgroundRole()) self.layout.addRow("Name:", self.param_name) self.layout.addRow(self.createSpace(10)) self.layout.addRow(buttons) self.connect(buttons, SIGNAL('accepted()'), self.accept) self.connect(buttons, SIGNAL('rejected()'), self.reject) self.setLayout(self.layout) def notValid(self, msg): """Called when the name is not valid.""" self.ok_button.setEnabled(False) palette = self.param_name.palette() palette.setColor(self.param_name.backgroundRole(), self.INVALID_COLOR) self.param_name.setToolTip(msg) self.param_name.setPalette(palette) def valid(self): """Called when the name is valid.""" self.ok_button.setEnabled(True) palette = self.param_name.palette() palette.setColor(self.param_name.backgroundRole(), self.validColor) self.param_name.setToolTip("") self.param_name.setPalette(palette) def validateName(self, value): """Called to perform validation of a name. For specific needs override this function and call valid() and notValid(msg).""" value = str(value) if value == "": self.notValid("Can not be empty!") elif not value.find(" ") == -1: self.notValid("No spaces allowed!") elif value in self.unique_names: self.notValid("Name must be unique!") else: self.valid() def validateChoice(self, choice): """Only called when using selection mode.""" self.ok_button.setEnabled(not choice == "") def getName(self): """Return the new name chosen by the user""" if self.choose_from_list: return str(self.param_name_combo.currentText()) else: return str(self.param_name.text()) def showAndTell(self): """Shows the dialog and returns the result""" if self.exec_(): return str(self.getName()).strip() return "" def createSpace(self, size=5): """Creates a widget that can be used as spacing on a panel.""" qw = QWidget() qw.setMinimumSize(QSize(size, size)) return qw
class ValidatedDialog(QDialog): """ A dialog for creating a validated new value. Performs validation of name against a provided. Can be used to select from the list or for creating a new value that is not on the list. """ INVALID_COLOR = QColor(255, 235, 235) def __init__(self, title = "Title", description = "Description", unique_names = None, choose_from_list=False): QDialog.__init__(self) self.setModal(True) self.setWindowTitle(title) # self.setMinimumWidth(250) # self.setMinimumHeight(150) if unique_names is None: unique_names = [] self.unique_names = unique_names self.choose_from_list = choose_from_list self.layout = QFormLayout() self.layout.setSizeConstraint(QLayout.SetFixedSize) label = QLabel(description) label.setAlignment(Qt.AlignHCenter) self.layout.addRow(self.createSpace(5)) self.layout.addRow(label) self.layout.addRow(self.createSpace(10)) buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) if choose_from_list: self.param_name_combo = QComboBox() self.connect(self.param_name_combo, SIGNAL('currentIndexChanged(QString)'), self.validateChoice) for item in unique_names: self.param_name_combo.addItem(item) self.layout.addRow("Job:", self.param_name_combo) else: self.param_name = QLineEdit(self) self.param_name.setFocus() self.connect(self.param_name, SIGNAL('textChanged(QString)'), self.validateName) self.validColor = self.param_name.palette().color(self.param_name.backgroundRole()) self.layout.addRow("Name:", self.param_name) self.layout.addRow(self.createSpace(10)) self.layout.addRow(buttons) self.connect(buttons, SIGNAL('accepted()'), self.accept) self.connect(buttons, SIGNAL('rejected()'), self.reject) self.setLayout(self.layout) def notValid(self, msg): """Called when the name is not valid.""" self.ok_button.setEnabled(False) palette = self.param_name.palette() palette.setColor(self.param_name.backgroundRole(), self.INVALID_COLOR) self.param_name.setToolTip(msg) self.param_name.setPalette(palette) def valid(self): """Called when the name is valid.""" self.ok_button.setEnabled(True) palette = self.param_name.palette() palette.setColor(self.param_name.backgroundRole(), self.validColor) self.param_name.setToolTip("") self.param_name.setPalette(palette) def validateName(self, value): """Called to perform validation of a name. For specific needs override this function and call valid() and notValid(msg).""" value = str(value) if value == "": self.notValid("Can not be empty!") elif not value.find(" ") == -1: self.notValid("No spaces allowed!") elif value in self.unique_names: self.notValid("Name must be unique!") else: self.valid() def validateChoice(self, choice): """Only called when using selection mode.""" self.ok_button.setEnabled(not choice == "") def getName(self): """Return the new name chosen by the user""" if self.choose_from_list: return str(self.param_name_combo.currentText()) else: return str(self.param_name.text()) def showAndTell(self): """Shows the dialog and returns the result""" if self.exec_(): return str(self.getName()).strip() return "" def createSpace(self, size = 5): """Creates a widget that can be used as spacing on a panel.""" qw = QWidget() qw.setMinimumSize(QSize(size, size)) return qw
class CustomDialog(QDialog): INVALID_COLOR = QColor(255, 235, 235) def __init__(self, title="Title", description="Description", parent=None): QDialog.__init__(self, parent) self._option_list = [] """ :type: list of QWidget """ self.setModal(True) self.setWindowTitle(title) self.layout = QFormLayout() self.layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.layout.setSizeConstraint(QLayout.SetFixedSize) label = QLabel(description) label.setAlignment(Qt.AlignHCenter) self.layout.addRow(self.createSpace(5)) self.layout.addRow(label) self.layout.addRow(self.createSpace(10)) self.ok_button = None self.setLayout(self.layout) def notValid(self, msg): """Called when the name is not valid.""" self.ok_button.setEnabled(False) def valid(self): """Called when the name is valid.""" self.ok_button.setEnabled(True) def optionValidationChanged(self): valid = True for option in self._option_list: if hasattr(option, "isValid"): if not option.isValid(): valid = False self.notValid("One or more options are incorrectly set!") if valid: self.valid() def showAndTell(self): """ Shows the dialog modally and returns the true or false (accept/reject) @rtype: bool """ self.optionValidationChanged() return self.exec_() def createSpace(self, size=5): """Creates a widget that can be used as spacing on a panel.""" qw = QWidget() qw.setMinimumSize(QSize(size, size)) return qw def addSpace(self, size=10): """ Add some vertical spacing """ space_widget = self.createSpace(size) self.layout.addRow("", space_widget) def addLabeledOption(self, label, option_widget): """ @type option_widget: QWidget """ self._option_list.append(option_widget) if hasattr(option_widget, "validationChanged"): option_widget.validationChanged.connect(self.optionValidationChanged) if hasattr(option_widget, "getValidationSupport"): validation_support = option_widget.getValidationSupport() validation_support.validationChanged.connect(self.optionValidationChanged) self.layout.addRow("%s:" % label, option_widget) def addWidget(self, widget, label=""): if not label.endswith(":"): label = "%s:" % label self.layout.addRow(label, widget) def addButtons(self): buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) self.layout.addRow(self.createSpace(10)) self.layout.addRow(buttons) self.connect(buttons, SIGNAL('accepted()'), self.accept) self.connect(buttons, SIGNAL('rejected()'), self.reject)
class CustomDialog(QDialog): INVALID_COLOR = QColor(255, 235, 235) def __init__(self, title="Title", description="Description", parent=None): QDialog.__init__(self, parent) self._option_list = [] """ :type: list of QWidget """ self.setModal(True) self.setWindowTitle(title) self.layout = QFormLayout() self.layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.layout.setSizeConstraint(QLayout.SetFixedSize) label = QLabel(description) label.setAlignment(Qt.AlignHCenter) self.layout.addRow(self.createSpace(5)) self.layout.addRow(label) self.layout.addRow(self.createSpace(10)) self.ok_button = None self.setLayout(self.layout) def notValid(self, msg): """Called when the name is not valid.""" self.ok_button.setEnabled(False) def valid(self): """Called when the name is valid.""" self.ok_button.setEnabled(True) def optionValidationChanged(self): valid = True for option in self._option_list: if hasattr(option, "isValid"): if not option.isValid(): valid = False self.notValid("One or more options are incorrectly set!") if valid: self.valid() def showAndTell(self): """ Shows the dialog modally and returns the true or false (accept/reject) @rtype: bool """ self.optionValidationChanged() return self.exec_() def createSpace(self, size=5): """Creates a widget that can be used as spacing on a panel.""" qw = QWidget() qw.setMinimumSize(QSize(size, size)) return qw def addSpace(self, size=10): """ Add some vertical spacing """ space_widget = self.createSpace(size) self.layout.addRow("", space_widget) def addLabeledOption(self, label, option_widget): """ @type option_widget: QWidget """ self._option_list.append(option_widget) if hasattr(option_widget, "validationChanged"): option_widget.validationChanged.connect( self.optionValidationChanged) if hasattr(option_widget, "getValidationSupport"): validation_support = option_widget.getValidationSupport() validation_support.validationChanged.connect( self.optionValidationChanged) self.layout.addRow("%s:" % label, option_widget) def addWidget(self, widget, label=""): if not label.endswith(":"): label = "%s:" % label self.layout.addRow(label, widget) def addButtons(self): buttons = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) self.layout.addRow(self.createSpace(10)) self.layout.addRow(buttons) self.connect(buttons, SIGNAL('accepted()'), self.accept) self.connect(buttons, SIGNAL('rejected()'), self.reject)
class CustomDialog(QDialog): INVALID_COLOR = QColor(255, 235, 235) def __init__(self, title = "Title", description = "Description", parent=None): QDialog.__init__(self, parent) self.__option_list = [] """ :type: list of HelpedWidget """ self.setModal(True) self.setWindowTitle(title) # self.setMinimumWidth(250) # self.setMinimumHeight(150) self.layout = QFormLayout() self.layout.setSizeConstraint(QLayout.SetFixedSize) label = QLabel(description) label.setAlignment(Qt.AlignHCenter) self.layout.addRow(self.createSpace(5)) self.layout.addRow(label) self.layout.addRow(self.createSpace(10)) self.ok_button = None self.setLayout(self.layout) def notValid(self, msg): """Called when the name is not valid.""" self.ok_button.setEnabled(False) def valid(self): """Called when the name is valid.""" self.ok_button.setEnabled(True) def optionValidationChanged(self): valid = True for option in self.__option_list: if not option.isValid(): valid = False self.notValid("One or more options are incorrectly set!") if valid: self.valid() def showAndTell(self): """ Shows the dialog modally and returns the true or false (accept/reject) @rtype: bool """ self.optionValidationChanged() return self.exec_() def createSpace(self, size = 5): """Creates a widget that can be used as spacing on a panel.""" qw = QWidget() qw.setMinimumSize(QSize(size, size)) return qw def addOption(self, option_widget): """ @type option_widget: HelpedWidget """ assert isinstance(option_widget, HelpedWidget) self.__option_list.append(option_widget) option_widget.validationChanged.connect(self.optionValidationChanged) self.layout.addRow(option_widget.getLabel(), option_widget) def addButtons(self): buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) self.ok_button = buttons.button(QDialogButtonBox.Ok) self.ok_button.setEnabled(False) self.layout.addRow(self.createSpace(10)) self.layout.addRow(buttons) self.connect(buttons, SIGNAL('accepted()'), self.accept) self.connect(buttons, SIGNAL('rejected()'), self.reject)
class RegSubmitDialog(QDialog): def __init__(self, parent, reg): flags = Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowSystemMenuHint QDialog.__init__(self, parent, flags) self._setupUi() self.reg = reg self.submitButton.clicked.connect(self.submitClicked) self.contributeButton.clicked.connect(self.contributeClicked) self.cancelButton.clicked.connect(self.reject) def _setupUi(self): self.setWindowTitle(tr("Enter your registration key")) self.resize(365, 126) self.verticalLayout = QVBoxLayout(self) self.promptLabel = QLabel(self) appname = str(QCoreApplication.instance().applicationName()) prompt = tr("Type the key you received when you contributed to $appname, as well as the " "e-mail used as a reference for the purchase.").replace('$appname', appname) self.promptLabel.setText(prompt) self.promptLabel.setAlignment(Qt.AlignLeading|Qt.AlignLeft|Qt.AlignTop) self.promptLabel.setWordWrap(True) self.verticalLayout.addWidget(self.promptLabel) self.formLayout = QFormLayout() self.formLayout.setSizeConstraint(QLayout.SetNoConstraint) self.formLayout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) self.formLayout.setLabelAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter) self.formLayout.setFormAlignment(Qt.AlignLeading|Qt.AlignLeft|Qt.AlignTop) self.label2 = QLabel(self) self.label2.setText(tr("Registration key:")) self.formLayout.setWidget(0, QFormLayout.LabelRole, self.label2) self.label3 = QLabel(self) self.label3.setText(tr("Registered e-mail:")) self.formLayout.setWidget(1, QFormLayout.LabelRole, self.label3) self.codeEdit = QLineEdit(self) self.formLayout.setWidget(0, QFormLayout.FieldRole, self.codeEdit) self.emailEdit = QLineEdit(self) self.formLayout.setWidget(1, QFormLayout.FieldRole, self.emailEdit) self.verticalLayout.addLayout(self.formLayout) self.horizontalLayout = QHBoxLayout() self.contributeButton = QPushButton(self) self.contributeButton.setText(tr("Contribute")) self.contributeButton.setAutoDefault(False) self.horizontalLayout.addWidget(self.contributeButton) spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem) self.cancelButton = QPushButton(self) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.cancelButton.sizePolicy().hasHeightForWidth()) self.cancelButton.setSizePolicy(sizePolicy) self.cancelButton.setText(tr("Cancel")) self.cancelButton.setAutoDefault(False) self.horizontalLayout.addWidget(self.cancelButton) self.submitButton = QPushButton(self) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.submitButton.sizePolicy().hasHeightForWidth()) self.submitButton.setSizePolicy(sizePolicy) self.submitButton.setText(tr("Submit")) self.submitButton.setAutoDefault(False) self.submitButton.setDefault(True) self.horizontalLayout.addWidget(self.submitButton) self.verticalLayout.addLayout(self.horizontalLayout) #--- Events def contributeClicked(self): self.reg.app.contribute() def submitClicked(self): code = self.codeEdit.text() email = self.emailEdit.text() if self.reg.app.set_registration(code, email, False): self.accept()