|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter data for the creation of a tag. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt |
|
11 from PyQt4.QtGui import QDialog, QDialogButtonBox, QVBoxLayout, QLabel |
|
12 |
|
13 from E5Gui.E5LineEdit import E5ClearableLineEdit |
|
14 |
|
15 |
|
16 class DjangoTagInputDialog(QDialog): |
|
17 """ |
|
18 Class implementing a dialog to enter data for the creation of a tag. |
|
19 """ |
|
20 def __init__(self, labels, defaults=None, parent=None): |
|
21 super(DjangoTagInputDialog, self).__init__(parent) |
|
22 |
|
23 assert 0 < len(labels) < 6 # max 5 entries allowed |
|
24 self.__inputs = [] |
|
25 |
|
26 self.__topLayout = QVBoxLayout(self) |
|
27 index = 0 |
|
28 for label in labels: |
|
29 self.__topLayout.addWidget(QLabel(label, self)) |
|
30 entry = E5ClearableLineEdit(self) |
|
31 if defaults and index < len(defaults): |
|
32 entry.setText(defaults[index]) |
|
33 self.__inputs.append(entry) |
|
34 self.__topLayout.addWidget(entry) |
|
35 index += 1 |
|
36 self.__buttonBox = QDialogButtonBox( |
|
37 QDialogButtonBox.Ok | QDialogButtonBox.Cancel, |
|
38 Qt.Horizontal, self) |
|
39 self.__topLayout.addWidget(self.__buttonBox) |
|
40 |
|
41 self.resize(400, self.minimumSizeHint().height()) |
|
42 self.setSizeGripEnabled(True) |
|
43 |
|
44 self.__buttonBox.accepted.connect(self.accept) |
|
45 self.__buttonBox.rejected.connect(self.reject) |
|
46 |
|
47 self.__inputs[0].selectAll() |
|
48 self.__inputs[0].setFocus() |
|
49 |
|
50 def getData(self): |
|
51 """ |
|
52 Public method to retrieve the entered data. |
|
53 |
|
54 @return tuple containing the text of all entries (tuple of string) |
|
55 """ |
|
56 data = [input.text().strip() for input in self.__inputs] |
|
57 return tuple(data) |
|
58 |
|
59 @staticmethod |
|
60 def getText(parent, title, labels, defaults=[]): |
|
61 """ |
|
62 Static method to create the dialog and return the entered data. |
|
63 |
|
64 @return tuple of a tuple containing the text of all entries |
|
65 (tuple of string) and a flag indicating the acceptance |
|
66 state (boolean) |
|
67 """ |
|
68 dlg = DjangoTagInputDialog(labels, defaults, parent) |
|
69 dlg.setWindowTitle(title) |
|
70 if dlg.exec_() == QDialog.Accepted: |
|
71 return dlg.getData(), True |
|
72 else: |
|
73 return tuple(), False |