|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog for entering multiple template variables. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 class TemplateMultipleVariablesDialog(QDialog): |
|
14 """ |
|
15 Class implementing a dialog for entering multiple template variables. |
|
16 """ |
|
17 def __init__(self, variables, parent = None): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param variables list of template variable names (list of strings) |
|
22 @param parent parent widget of this dialog (QWidget) |
|
23 """ |
|
24 QDialog.__init__(self, parent) |
|
25 |
|
26 self.TemplateMultipleVariablesDialogLayout = QVBoxLayout(self) |
|
27 self.TemplateMultipleVariablesDialogLayout.setMargin(6) |
|
28 self.TemplateMultipleVariablesDialogLayout.setSpacing(6) |
|
29 self.TemplateMultipleVariablesDialogLayout.setObjectName(\ |
|
30 "TemplateMultipleVariablesDialogLayout") |
|
31 self.setLayout(self.TemplateMultipleVariablesDialogLayout) |
|
32 |
|
33 # generate the scrollarea |
|
34 self.variablesView = QScrollArea(self) |
|
35 self.variablesView.setObjectName("variablesView") |
|
36 self.TemplateMultipleVariablesDialogLayout.addWidget(self.variablesView) |
|
37 |
|
38 self.variablesView.setWidgetResizable(True) |
|
39 self.variablesView.setFrameStyle(QFrame.NoFrame) |
|
40 |
|
41 self.top = QWidget(self) |
|
42 self.variablesView.setWidget(self.top) |
|
43 self.grid = QGridLayout(self.top) |
|
44 self.grid.setMargin(0) |
|
45 self.grid.setSpacing(6) |
|
46 self.top.setLayout(self.grid) |
|
47 |
|
48 # populate the scrollarea with labels and text edits |
|
49 self.variablesEntries = {} |
|
50 row = 0 |
|
51 for var in variables: |
|
52 l = QLabel("%s:" % var, self.top) |
|
53 self.grid.addWidget(l, row, 0, Qt.Alignment(Qt.AlignTop)) |
|
54 if var.find(":") >= 0: |
|
55 formatStr = var[1:-1].split(":")[1] |
|
56 if formatStr in ["ml", "rl"]: |
|
57 t = QTextEdit(self.top) |
|
58 t.setTabChangesFocus(True) |
|
59 else: |
|
60 t = QLineEdit(self.top) |
|
61 else: |
|
62 t = QLineEdit(self.top) |
|
63 self.grid.addWidget(t, row, 1) |
|
64 self.variablesEntries[var] = t |
|
65 row += 1 |
|
66 # add a spacer to make the entries aligned at the top |
|
67 spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) |
|
68 self.grid.addItem(spacer, row, 1) |
|
69 self.variablesEntries[variables[0]].setFocus() |
|
70 self.top.adjustSize() |
|
71 |
|
72 # generate the buttons |
|
73 layout1 = QHBoxLayout() |
|
74 layout1.setMargin(0) |
|
75 layout1.setSpacing(6) |
|
76 layout1.setObjectName("layout1") |
|
77 |
|
78 spacer1 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) |
|
79 layout1.addItem(spacer1) |
|
80 |
|
81 self.okButton = QPushButton(self) |
|
82 self.okButton.setObjectName("okButton") |
|
83 self.okButton.setDefault(True) |
|
84 layout1.addWidget(self.okButton) |
|
85 |
|
86 self.cancelButton = QPushButton(self) |
|
87 self.cancelButton.setObjectName("cancelButton") |
|
88 layout1.addWidget(self.cancelButton) |
|
89 |
|
90 spacer2 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) |
|
91 layout1.addItem(spacer2) |
|
92 |
|
93 self.TemplateMultipleVariablesDialogLayout.addLayout(layout1) |
|
94 |
|
95 # set the texts of the standard widgets |
|
96 self.setWindowTitle(self.trUtf8("Enter Template Variables")) |
|
97 self.okButton.setText(self.trUtf8("&OK")) |
|
98 self.cancelButton.setText(self.trUtf8("&Cancel")) |
|
99 |
|
100 # polish up the dialog |
|
101 self.resize(QSize(400, 480).expandedTo(self.minimumSizeHint())) |
|
102 |
|
103 self.connect(self.okButton, SIGNAL("clicked()"), self.accept) |
|
104 self.connect(self.cancelButton, SIGNAL("clicked()"), self.reject) |
|
105 |
|
106 def getVariables(self): |
|
107 """ |
|
108 Public method to get the values for all variables. |
|
109 |
|
110 @return dictionary with the variable as a key and its value (string) |
|
111 """ |
|
112 values = {} |
|
113 for var, textEdit in self.variablesEntries.items(): |
|
114 try: |
|
115 values[var] = textEdit.text() |
|
116 except AttributeError: |
|
117 values[var] = textEdit.toPlainText() |
|
118 return values |