ProjectDjangoTagsMenu/DjangoTagInputDialog.py

Sat, 24 Apr 2021 11:43:22 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 24 Apr 2021 11:43:22 +0200
changeset 52
c264091162a2
parent 51
29c461f7bea0
child 55
5390ef66c327
permissions
-rw-r--r--

- implemented some code simplifications

# -*- coding: utf-8 -*-

# Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to enter data for the creation of a tag.
"""

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel

from E5Gui.E5LineEdit import E5ClearableLineEdit


class DjangoTagInputDialog(QDialog):
    """
    Class implementing a dialog to enter data for the creation of a tag.
    """
    def __init__(self, labels, defaults=None, parent=None):
        """
        Constructor
        
        @param labels list of labels for the entry fields (list of string)
        @param defaults list of default values for the entry fields (list
            of string)
        @param parent reference to the parent widget (QWidget)
        @exception RuntimeError raised to signal too many labels were given
        """
        super().__init__(parent)
        
        if len(labels) == 0 or len(labels) > 5:
            raise RuntimeError(
                "Illegal number of labels given (max. 5 allowed)")
        
        self.__inputs = []
        
        self.__topLayout = QVBoxLayout(self)
        for index, label in enumerate(labels):
            self.__topLayout.addWidget(QLabel(label, self))
            entry = E5ClearableLineEdit(self)
            if defaults and index < len(defaults):
                entry.setText(defaults[index])
            self.__inputs.append(entry)
            self.__topLayout.addWidget(entry)
        self.__buttonBox = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal, self)
        self.__topLayout.addWidget(self.__buttonBox)
        
        self.resize(400, self.minimumSizeHint().height())
        self.setSizeGripEnabled(True)
        
        self.__buttonBox.accepted.connect(self.accept)
        self.__buttonBox.rejected.connect(self.reject)
        
        self.__inputs[0].selectAll()
        self.__inputs[0].setFocus()
    
    def getData(self):
        """
        Public method to retrieve the entered data.
        
        @return tuple containing the text of all entries (tuple of string)
        """
        data = [input.text().strip() for input in self.__inputs]
        return tuple(data)
    
    @staticmethod
    def getText(parent, title, labels, defaults=None):
        """
        Public static method to create the dialog and return the entered data.
        
        @param parent reference to the parent widget (QWidget)
        @param title title of the dialog (string)
        @param labels list of labels for the entry fields (list of string)
        @param defaults list of default values for the entry fields (list
            of string)
        @return tuple of a tuple containing the text of all entries
            (tuple of string) and a flag indicating the acceptance
            state (boolean)
        """
        if defaults is None:
            defaults = []
        
        dlg = DjangoTagInputDialog(labels, defaults, parent)
        dlg.setWindowTitle(title)
        if dlg.exec() == QDialog.Accepted:
            return dlg.getData(), True
        else:
            return (), False

eric ide

mercurial