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