src/eric7/Templates/TemplateMultipleVariablesDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2005 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog for entering multiple template variables.
8 """
9
10 from PyQt6.QtCore import QSize, Qt
11 from PyQt6.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, Qt.AlignmentFlag.AlignTop)
59 if var.find(":") >= 0:
60 formatStr = var[1:-1].split(":")[1]
61 if formatStr in ["ml", "rl"]:
62 t = QTextEdit(self.top)
63 t.setTabChangesFocus(True)
64 else:
65 t = QLineEdit(self.top)
66 else:
67 t = QLineEdit(self.top)
68 self.grid.addWidget(t, row, 1)
69 self.variablesEntries[var] = t
70 # add a spacer to make the entries aligned at the top
71 spacer = QSpacerItem(
72 20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
73 self.grid.addItem(spacer, self.grid.rowCount(), 1)
74 self.variablesEntries[variables[0]].setFocus()
75 self.top.adjustSize()
76
77 # generate the buttons
78 layout1 = QHBoxLayout()
79 layout1.setContentsMargins(0, 0, 0, 0)
80 layout1.setSpacing(6)
81 layout1.setObjectName("layout1")
82
83 spacer1 = QSpacerItem(
84 40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
85 layout1.addItem(spacer1)
86
87 self.okButton = QPushButton(self)
88 self.okButton.setObjectName("okButton")
89 self.okButton.setDefault(True)
90 layout1.addWidget(self.okButton)
91
92 self.cancelButton = QPushButton(self)
93 self.cancelButton.setObjectName("cancelButton")
94 layout1.addWidget(self.cancelButton)
95
96 spacer2 = QSpacerItem(
97 40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
98 layout1.addItem(spacer2)
99
100 self.TemplateMultipleVariablesDialogLayout.addLayout(layout1)
101
102 # set the texts of the standard widgets
103 self.setWindowTitle(self.tr("Enter Template Variables"))
104 self.okButton.setText(self.tr("&OK"))
105 self.cancelButton.setText(self.tr("&Cancel"))
106
107 # polish up the dialog
108 self.resize(QSize(400, 480).expandedTo(self.minimumSizeHint()))
109
110 self.okButton.clicked.connect(self.accept)
111 self.cancelButton.clicked.connect(self.reject)
112
113 def getVariables(self):
114 """
115 Public method to get the values for all variables.
116
117 @return dictionary with the variable as a key and its value (string)
118 """
119 values = {}
120 for var, textEdit in list(self.variablesEntries.items()):
121 try:
122 values[var] = textEdit.text()
123 except AttributeError:
124 values[var] = textEdit.toPlainText()
125 return values

eric ide

mercurial