1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2018 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter some text.
8"""
9
10from PyQt5.QtWidgets import (
11    QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit
12)
13
14from E5Gui.E5LineEdit import E5ClearableLineEdit
15
16
17class E5TextInputDialog(QDialog):
18    """
19    Class implementing a dialog to enter some text.
20    """
21    def __init__(self, parent=None):
22        """
23        Constructor
24
25        @param parent reference to the parent widget
26        @type QWidget
27        """
28        super().__init__(parent)
29
30        self.setMaximumWidth(600)
31
32        self.__layout = QVBoxLayout(self)
33
34        self.__label = QLabel(self)
35        self.__layout.addWidget(self.__label)
36
37        self.__lineEdit = E5ClearableLineEdit(self)
38        self.__layout.addWidget(self.__lineEdit)
39
40        self.__buttonBox = QDialogButtonBox(
41            QDialogButtonBox.StandardButton.Ok |
42            QDialogButtonBox.StandardButton.Cancel, self)
43        self.__layout.addWidget(self.__buttonBox)
44
45        self.__buttonBox.accepted.connect(self.accept)
46        self.__buttonBox.rejected.connect(self.reject)
47
48        msh = self.minimumSizeHint()
49        self.resize(max(self.width(), msh.width()), msh.height())
50
51    def setTextEchoMode(self, echoMode):
52        """
53        Public method to set the echo mode of the line edit.
54
55        @param echoMode echo mode of the line edit
56        @type QLineEdit.EchoMode
57        """
58        self.__lineEdit.setEchoMode(echoMode)
59
60    def textEchoMode(self):
61        """
62        Public method to get the current echo mode of the line edit.
63
64        @return echo mode of the line edit
65        @rtype QLineEdit.EchoMode
66        """
67        return self.__lineEdit.echoMode()
68
69    def setTextValue(self, text):
70        """
71        Public method to set the text of the line edit.
72
73        @param text text for the line edit
74        @type str
75        """
76        self.__lineEdit.setText(text)
77
78    def textValue(self):
79        """
80        Public method to get the text of the line edit.
81
82        @return text of the line edit
83        @rtype str
84        """
85        return self.__lineEdit.text()
86
87    def setLabelText(self, text):
88        """
89        Public method to set the label text.
90
91        @param text label text
92        @type str
93        """
94        self.__label.setText(text)
95
96        msh = self.minimumSizeHint()
97        labelSizeHint = self.__label.sizeHint()
98        self.resize(max(self.width(), msh.width(), labelSizeHint.width()),
99                    msh.height())
100
101    def labelText(self):
102        """
103        Public method to get the current label text.
104
105        @return current label text
106        @rtype str
107        """
108        return self.label.text()
109
110
111def getText(parent, title, label, mode=QLineEdit.EchoMode.Normal, text="",
112            minimumWidth=300):
113    """
114    Function to get create a dialog to enter some text and return it.
115
116    @param parent reference to the parent widget
117    @type QWidget
118    @param title title of the dialog
119    @type str
120    @param label label of the dialog
121    @type str
122    @param mode echo mode of the line edit
123    @type QLineEdit.EchoMode
124    @param text initial text of the line edit
125    @type str
126    @param minimumWidth minimum width of the dialog
127    @type int
128    @return tuple containing a flag indicating the dialog was accepted and the
129        entered text
130    @rtype tuple of (bool, str)
131    """
132    dlg = E5TextInputDialog(parent)
133    dlg.setWindowTitle(title)
134    dlg.setLabelText(label)
135    dlg.setTextEchoMode(mode)
136    dlg.setTextValue(text)
137    dlg.setMinimumWidth(minimumWidth)
138
139    if dlg.exec() == QDialog.DialogCode.Accepted:
140        return True, dlg.textValue()
141    else:
142        return False, ""
143