| 209 | |
| 210 | |
| 211 | class FormWidget(QtWidgets.QWidget): |
| 212 | update_buttons = QtCore.Signal() |
| 213 | |
| 214 | def __init__(self, data, comment="", with_margin=False, parent=None): |
| 215 | """ |
| 216 | Parameters |
| 217 | ---------- |
| 218 | data : list of (label, value) pairs |
| 219 | The data to be edited in the form. |
| 220 | comment : str, optional |
| 221 | with_margin : bool, default: False |
| 222 | If False, the form elements reach to the border of the widget. |
| 223 | This is the desired behavior if the FormWidget is used as a widget |
| 224 | alongside with other widgets such as a QComboBox, which also do |
| 225 | not have a margin around them. |
| 226 | However, a margin can be desired if the FormWidget is the only |
| 227 | widget within a container, e.g. a tab in a QTabWidget. |
| 228 | parent : QWidget or None |
| 229 | The parent widget. |
| 230 | """ |
| 231 | super().__init__(parent) |
| 232 | self.data = copy.deepcopy(data) |
| 233 | self.widgets = [] |
| 234 | self.formlayout = QtWidgets.QFormLayout(self) |
| 235 | if not with_margin: |
| 236 | self.formlayout.setContentsMargins(0, 0, 0, 0) |
| 237 | if comment: |
| 238 | self.formlayout.addRow(QtWidgets.QLabel(comment)) |
| 239 | self.formlayout.addRow(QtWidgets.QLabel(" ")) |
| 240 | |
| 241 | def get_dialog(self): |
| 242 | """Return FormDialog instance""" |
| 243 | dialog = self.parent() |
| 244 | while not isinstance(dialog, QtWidgets.QDialog): |
| 245 | dialog = dialog.parent() |
| 246 | return dialog |
| 247 | |
| 248 | def setup(self): |
| 249 | for label, value in self.data: |
| 250 | if label is None and value is None: |
| 251 | # Separator: (None, None) |
| 252 | self.formlayout.addRow(QtWidgets.QLabel(" "), |
| 253 | QtWidgets.QLabel(" ")) |
| 254 | self.widgets.append(None) |
| 255 | continue |
| 256 | elif label is None: |
| 257 | # Comment |
| 258 | self.formlayout.addRow(QtWidgets.QLabel(value)) |
| 259 | self.widgets.append(None) |
| 260 | continue |
| 261 | elif tuple_to_qfont(value) is not None: |
| 262 | field = FontLayout(value, self) |
| 263 | elif (label.lower() not in BLACKLIST |
| 264 | and mcolors.is_color_like(value)): |
| 265 | field = ColorLayout(to_qcolor(value), self) |
| 266 | elif isinstance(value, str): |
| 267 | field = QtWidgets.QLineEdit(value, self) |
| 268 | elif isinstance(value, (list, tuple)): |