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