| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
import logging |
| 25 |
import os |
| 26 |
from functools import partial |
| 27 |
from inspect import signature |
| 28 |
from collections import ChainMap |
| 29 |
|
| 30 |
from ..Qt import QtCore, QtWidgets, QtGui |
| 31 |
from ...experiment.sequencer import SequenceHandler, SequenceEvaluationError |
| 32 |
|
| 33 |
log = logging.getLogger(__name__) |
| 34 |
log.addHandler(logging.NullHandler()) |
| 35 |
|
| 36 |
|
| 37 |
class SequencerTreeModel(QtCore.QAbstractItemModel): |
| 38 |
""" Model for sequencer data |
| 39 |
|
| 40 |
:param header: List of string representing header data |
| 41 |
:param data: data associated with the model |
| 42 |
:param parent: A QWidget that QT will give ownership of this Widget to. |
| 43 |
""" |
| 44 |
|
| 45 |
def __init__(self, data, header=("Level", "Parameter", "Sequence"), parent=None): |
| 46 |
super().__init__(parent) |
| 47 |
|
| 48 |
self.header = header |
| 49 |
self.root = data |
| 50 |
|
| 51 |
def add_node(self, parameter, parent=None): |
| 52 |
""" Add a row in the sequencer """ |
| 53 |
if parent is None: |
| 54 |
parent = self.createIndex(-1, -1) |
| 55 |
|
| 56 |
idx = len(self.root.children(parent)) |
| 57 |
parent_seq_item = parent.internalPointer() |
| 58 |
|
| 59 |
self.beginInsertRows(parent, idx, idx) |
| 60 |
seq_item, child_row = self.root.add_node(parameter, parent_seq_item) |
| 61 |
self.endInsertRows() |
| 62 |
return self.createIndex(child_row, 0, seq_item) |
| 63 |
|
| 64 |
def remove_node(self, index): |
| 65 |
""" Remove a row in the sequencer """ |
| 66 |
|
| 67 |
children = self.rowCount(index) |
| 68 |
seq_item = index.internalPointer() |
| 69 |
# Remove children from last to first |
| 70 |
while (children > 0): |
| 71 |
child = children - 1 |
| 72 |
children_seq_item = self.root.get_children(seq_item, child) |
| 73 |
self.remove_node(self.createIndex(child, 0, children_seq_item)) |
| 74 |
children = self.rowCount(index) |
| 75 |
|
| 76 |
self.beginRemoveRows(index.parent(), index.row(), index.row()) |
| 77 |
parent_seq_item, parent_row = self.root.remove_node(seq_item) |
| 78 |
self.endRemoveRows() |
| 79 |
return self.createIndex(parent_row, 0, parent_seq_item) |
| 80 |
|
| 81 |
def flags(self, index): |
| 82 |
""" Set the flags for the item at the given QModelIndex. |
| 83 |
|
| 84 |
Here, we just set all indexes to enabled, and selectable. |
| 85 |
""" |
| 86 |
if not index.isValid(): |
| 87 |
return_value = QtCore.Qt.ItemFlag.NoItemFlags |
| 88 |
else: |
| 89 |
return_value = QtCore.Qt.ItemFlag.ItemIsEnabled | \ |
| 90 |
QtCore.Qt.ItemFlag.ItemIsSelectable |
| 91 |
if index.column() >= 1: |
| 92 |
return_value |= QtCore.Qt.ItemFlag.ItemIsEditable |
| 93 |
return return_value |
| 94 |
|
| 95 |
def data(self, index, role): |
| 96 |
""" Return the data to display for the given index and the given role. |
| 97 |
|
| 98 |
This method should not be called directly. |
| 99 |
This method is called implicitly by the QTreeView that is |
| 100 |
displaying us, as the way of finding out what to display where. |
| 101 |
""" |
| 102 |
if not index.isValid(): |
| 103 |
return |
| 104 |
|
| 105 |
elif not role == QtCore.Qt.ItemDataRole.DisplayRole: |
| 106 |
return |
| 107 |
|
| 108 |
data = index.internalPointer()[index.column()] |
| 109 |
|
| 110 |
if not isinstance(data, QtCore.QObject): |
| 111 |
data = str(data) |
| 112 |
|
| 113 |
return data |
| 114 |
|
| 115 |
def index(self, row, col, parent): |
| 116 |
""" Return a QModelIndex instance pointing the row and column underneath the parent given. |
| 117 |
This method should not be called directly. This method is called implicitly by the |
| 118 |
QTreeView that is displaying us, as the way of finding out what to display where. |
| 119 |
""" |
| 120 |
if not parent or not parent.isValid(): |
| 121 |
parent_data = None |
| 122 |
else: |
| 123 |
parent_data = parent.internalPointer() |
| 124 |
|
| 125 |
seq_item = self.root.get_children(parent_data, row) |
| 126 |
child = seq_item |
| 127 |
if child is None: |
| 128 |
return QtCore.QModelIndex() |
| 129 |
index = self.createIndex(row, col, child) |
| 130 |
return index |
| 131 |
|
| 132 |
def parent(self, index=None): |
| 133 |
""" Return the index of the parent of a given index. If index is not supplied, |
| 134 |
return an invalid QModelIndex. |
| 135 |
|
| 136 |
:param index: QModelIndex optional. |
| 137 |
:return: |
| 138 |
""" |
| 139 |
|
| 140 |
if not index or not index.isValid(): |
| 141 |
return QtCore.QModelIndex() |
| 142 |
|
| 143 |
child = index.internalPointer() |
| 144 |
parent, parent_row = self.root.get_parent(child) |
| 145 |
|
| 146 |
if parent is None: |
| 147 |
return QtCore.QModelIndex() |
| 148 |
|
| 149 |
index = self.createIndex(parent_row, 0, parent) |
| 150 |
return index |
| 151 |
|
| 152 |
def rowCount(self, parent): |
| 153 |
""" Return the number of children of a given parent. |
| 154 |
|
| 155 |
If an invalid QModelIndex is supplied, return the number of children under the root. |
| 156 |
|
| 157 |
:param parent: QModelIndex |
| 158 |
""" |
| 159 |
if parent.column() > 0: |
| 160 |
return 0 |
| 161 |
|
| 162 |
if not parent.isValid(): |
| 163 |
parent = None |
| 164 |
else: |
| 165 |
parent = parent.internalPointer() |
| 166 |
|
| 167 |
rows = len(self.root.children(parent)) |
| 168 |
return rows |
| 169 |
|
| 170 |
def columnCount(self, parent): |
| 171 |
""" Return the number of columns in the model header. |
| 172 |
|
| 173 |
The parent parameter exists only to support the signature of QAbstractItemModel. |
| 174 |
""" |
| 175 |
return len(self.header) |
| 176 |
|
| 177 |
def headerData(self, section, orientation, role): |
| 178 |
""" Return the header data for the given section, orientation and role. |
| 179 |
|
| 180 |
This method should not be called directly. |
| 181 |
This method is called implicitly by the QTreeView that is displaying us, |
| 182 |
as the way of finding out what to display where. |
| 183 |
""" |
| 184 |
if orientation == QtCore.Qt.Orientation.Horizontal and \ |
| 185 |
role == QtCore.Qt.ItemDataRole.DisplayRole: |
| 186 |
return self.header[section] |
| 187 |
|
| 188 |
def setData(self, index, value, role=QtCore.Qt.ItemDataRole.EditRole): |
| 189 |
return_value = False |
| 190 |
if role == QtCore.Qt.ItemDataRole.EditRole: |
| 191 |
return_value = self.root.set_data(index.internalPointer(), |
| 192 |
index.row(), |
| 193 |
index.column(), |
| 194 |
value) |
| 195 |
if return_value: |
| 196 |
self.dataChanged.emit(index, index, [role]) |
| 197 |
return return_value |
| 198 |
|
| 199 |
def visit_tree(self, parent): |
| 200 |
""" Return a generator to enumerate all the nodes in the tree """ |
| 201 |
parent_data = None |
| 202 |
if parent: |
| 203 |
parent_data = parent.internalPointer() |
| 204 |
|
| 205 |
for row, child in enumerate(self.root.children(parent_data)): |
| 206 |
node = self.index(row, 0, parent) |
| 207 |
if node.isValid(): |
| 208 |
yield node |
| 209 |
yield from self.visit_tree(node) |
| 210 |
|
| 211 |
def __iter__(self): |
| 212 |
yield from self.visit_tree(None) |
| 213 |
|
| 214 |
def save(self, file_obj): |
| 215 |
self.root.save(file_obj) |
| 216 |
|
| 217 |
def load(self, file_obj, append=False): |
| 218 |
# Since we are loading new data, following Qt documentation, |
| 219 |
# we call beginResetModel to inform that any previous data reported |
| 220 |
# from the model is now invalid and has to be queried for again. |
| 221 |
# This also means that the current item and any selected items will become invalid. |
| 222 |
self.beginResetModel() |
| 223 |
try: |
| 224 |
self.root.load(file_obj, append=append) |
| 225 |
except SequenceEvaluationError as e: |
| 226 |
log.error(f"Error during sequence loading: {e}") |
| 227 |
# Complete model reset operation |
| 228 |
self.endResetModel() |
| 229 |
|
| 230 |
|
| 231 |
class ComboBoxDelegate(QtWidgets.QStyledItemDelegate): |
| 232 |
def __init__(self, owner, choices): |
| 233 |
super().__init__(owner) |
| 234 |
self.items = choices |
| 235 |
|
| 236 |
def createEditor(self, parent, option, index): |
| 237 |
editor = QtWidgets.QComboBox(parent) |
| 238 |
editor.addItems(self.items) |
| 239 |
return editor |
| 240 |
|
| 241 |
def setEditorData(self, editor, index): |
| 242 |
value = index.data(QtCore.Qt.ItemDataRole.DisplayRole) |
| 243 |
num = self.items.index(value) |
| 244 |
editor.setCurrentIndex(num) |
| 245 |
|
| 246 |
def setModelData(self, editor, model, index): |
| 247 |
value = editor.currentText() |
| 248 |
model.setData(index, value, QtCore.Qt.ItemDataRole.EditRole) |
| 249 |
|
| 250 |
def updateEditorGeometry(self, editor, option, index): |
| 251 |
editor.setGeometry(option.rect) |
| 252 |
|
| 253 |
|
| 254 |
class ExpressionValidator(QtGui.QValidator): |
| 255 |
def validate(self, input_string, pos): |
| 256 |
return_value = QtGui.QValidator.State.Acceptable |
| 257 |
try: |
| 258 |
SequenceHandler.eval_string(input_string, log_enabled=False) |
| 259 |
except SequenceEvaluationError: |
| 260 |
return_value = QtGui.QValidator.State.Intermediate |
| 261 |
return (return_value, input_string, pos) |
| 262 |
|
| 263 |
|
| 264 |
class LineEditDelegate(QtWidgets.QStyledItemDelegate): |
| 265 |
def createEditor(self, parent, option, index): |
| 266 |
editor = QtWidgets.QLineEdit(parent) |
| 267 |
editor.setValidator(ExpressionValidator()) |
| 268 |
return editor |
| 269 |
|
| 270 |
def setEditorData(self, editor, index): |
| 271 |
value = index.data(QtCore.Qt.ItemDataRole.DisplayRole) |
| 272 |
editor.setText(value) |
| 273 |
|
| 274 |
def setModelData(self, editor, model, index): |
| 275 |
value = editor.text() |
| 276 |
model.setData(index, value, QtCore.Qt.ItemDataRole.EditRole) |
| 277 |
|
| 278 |
def updateEditorGeometry(self, editor, option, index): |
| 279 |
editor.setGeometry(option.rect) |
| 280 |
|
| 281 |
|
| 282 |
class SequencerTreeView(QtWidgets.QTreeView): |
| 283 |
|
| 284 |
def __init__(self, parent=None): |
| 285 |
super().__init__(parent) |
| 286 |
self.width = self.viewport().size().width() |
| 287 |
|
| 288 |
def save(self, filename=None): |
| 289 |
self.model().save(filename) |
| 290 |
|
| 291 |
def selectRow(self, index): |
| 292 |
selection_model = self.selectionModel() |
| 293 |
selection_model.select(index, |
| 294 |
QtCore.QItemSelectionModel.SelectionFlag.Clear) |
| 295 |
for column in range(self.model().columnCount(index)): |
| 296 |
idx = self.model().createIndex(index.row(), column, |
| 297 |
index.internalPointer()) |
| 298 |
selection_model.select(idx, |
| 299 |
QtCore.QItemSelectionModel.SelectionFlag.Select) |
| 300 |
|
| 301 |
def activate_persistent_editor(self): |
| 302 |
model = self.model() |
| 303 |
for item in model: |
| 304 |
index = model.index(item.row(), 1, model.parent(item)) |
| 305 |
self.openPersistentEditor(index) |
| 306 |
|
| 307 |
def setModel(self, model): |
| 308 |
super().setModel(model) |
| 309 |
self.setColumnWidth(0, int(0.7 * self.width)) |
| 310 |
self.setColumnWidth(1, int(0.9 * self.width)) |
| 311 |
self.setColumnWidth(2, int(0.9 * self.width)) |
| 312 |
self.model().layoutChanged.connect(self.activate_persistent_editor) |
| 313 |
self.model().modelReset.connect(self.activate_persistent_editor) |
| 314 |
|
| 315 |
|
| 316 |
class SequenceDialog(QtWidgets.QFileDialog): |
| 317 |
""" |
| 318 |
Widget that displays a dialog box for loading or saving a sequence tree. |
| 319 |
|
| 320 |
It also shows a preview of sequence tree in the dialog box |
| 321 |
|
| 322 |
:param save: True if we are saving a file. Default False. |
| 323 |
""" |
| 324 |
|
| 325 |
def __init__(self, save=False, parent=None): |
| 326 |
""" |
| 327 |
Generate a serialized form of the sequence tree |
| 328 |
|
| 329 |
:param save: True if we are saving a file. Default False. |
| 330 |
:param parent: Passed on to QtWidgets.QWidget. Default is None |
| 331 |
""" |
| 332 |
super().__init__(parent) |
| 333 |
self.save = save |
| 334 |
self.setOption(QtWidgets.QFileDialog.Option.DontUseNativeDialog, True) |
| 335 |
self._setup_ui() |
| 336 |
|
| 337 |
def _setup_ui(self): |
| 338 |
preview_tab = QtWidgets.QTabWidget() |
| 339 |
vbox = QtWidgets.QVBoxLayout() |
| 340 |
param_vbox = QtWidgets.QVBoxLayout() |
| 341 |
vbox_widget = QtWidgets.QWidget() |
| 342 |
param_vbox_widget = QtWidgets.QWidget() |
| 343 |
|
| 344 |
self.preview_param = SequencerTreeView(parent=self) |
| 345 |
triggers = QtWidgets.QAbstractItemView.EditTrigger.NoEditTriggers |
| 346 |
self.preview_param.setEditTriggers(triggers) |
| 347 |
param_vbox.addWidget(self.preview_param) |
| 348 |
vbox_widget.setLayout(vbox) |
| 349 |
param_vbox_widget.setLayout(param_vbox) |
| 350 |
preview_tab.addTab(param_vbox_widget, "Sequence Parameters") |
| 351 |
if not self.save: |
| 352 |
self.append_checkbox = QtWidgets.QCheckBox("Append to existing sequence") |
| 353 |
self.append_checkbox.setCheckState(QtCore.Qt.CheckState.Checked) |
| 354 |
self.layout().addWidget(self.append_checkbox) |
| 355 |
self.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFile) |
| 356 |
else: |
| 357 |
self.setAcceptMode(QtWidgets.QFileDialog.AcceptMode.AcceptSave) |
| 358 |
self.setFileMode(QtWidgets.QFileDialog.FileMode.AnyFile) |
| 359 |
self.layout().addWidget(preview_tab, 0, 5, 4, 1) |
| 360 |
self.layout().setColumnStretch(5, 1) |
| 361 |
self.setMinimumSize(900, 500) |
| 362 |
self.resize(900, 500) |
| 363 |
self.currentChanged.connect(self.update_preview) |
| 364 |
|
| 365 |
def update_preview(self, filename): |
| 366 |
if not os.path.isdir(filename) and filename != '': |
| 367 |
with open(filename, 'r') as file_object: |
| 368 |
data = SequenceHandler(file_obj=file_object) |
| 369 |
tree_model = SequencerTreeModel(data=data) |
| 370 |
self.preview_param.setModel(tree_model) |
| 371 |
self.preview_param.expandAll() |
| 372 |
|
| 373 |
|
| 374 |
class SequencerWidget(QtWidgets.QWidget): |
| 375 |
""" |
| 376 |
Widget that allows to generate a sequence of measurements |
| 377 |
|
| 378 |
It allows sweeping parameters and moreover, one can write a simple text file to easily load a |
| 379 |
sequence. Sequences can also be saved |
| 380 |
|
| 381 |
Currently requires a queue function of the |
| 382 |
:class:`ManagedWindow<pymeasure.display.windows.managed_window.ManagedWindow>` to have a |
| 383 |
"procedure" argument. |
| 384 |
|
| 385 |
:param inputs: List of strings representing the parameters name |
| 386 |
""" |
| 387 |
|
| 388 |
def __init__(self, inputs=None, sequence_file=None, parent=None): |
| 389 |
super().__init__(parent) |
| 390 |
self._parent = parent |
| 391 |
|
| 392 |
self._check_queue_signature() |
| 393 |
|
| 394 |
# if no explicit inputs are given, use the displayed parameters |
| 395 |
if inputs is not None: |
| 396 |
self._inputs = inputs |
| 397 |
else: |
| 398 |
self._inputs = self._parent.displays |
| 399 |
|
| 400 |
self._get_properties() |
| 401 |
self._setup_ui() |
| 402 |
self._layout() |
| 403 |
|
| 404 |
self.data = SequenceHandler(list(self.names_inv.keys())) |
| 405 |
self.tree.setModel(SequencerTreeModel(data=self.data)) |
| 406 |
if sequence_file is not None: |
| 407 |
self.load_sequence(filename=sequence_file) |
| 408 |
|
| 409 |
def _check_queue_signature(self): |
| 410 |
""" |
| 411 |
Check if the call signature of the implementation of the`ManagedWindow.queue` |
| 412 |
method accepts the `procedure` keyword argument, which is required for using |
| 413 |
the sequencer. |
| 414 |
""" |
| 415 |
|
| 416 |
call_signature = signature(self._parent.queue) |
| 417 |
|
| 418 |
if 'procedure' not in call_signature.parameters: |
| 419 |
raise AttributeError( |
| 420 |
"The queue method of of the ManagedWindow does not accept the 'procedure'" |
| 421 |
"keyword argument. Accepting this keyword argument is required when using" |
| 422 |
"the 'SequencerWidget'." |
| 423 |
) |
| 424 |
|
| 425 |
def _get_properties(self): |
| 426 |
""" |
| 427 |
Obtain the names of the input parameters. |
| 428 |
""" |
| 429 |
|
| 430 |
parameter_objects = self._parent.procedure_class().parameter_objects() |
| 431 |
|
| 432 |
self.names = {key: parameter.name |
| 433 |
for key, parameter |
| 434 |
in parameter_objects.items() |
| 435 |
if key in self._inputs} |
| 436 |
|
| 437 |
self.names_inv = {name: key for key, name in self.names.items()} |
| 438 |
self.names_choices = list(sorted(self.names_inv.keys())) |
| 439 |
|
| 440 |
def _setup_ui(self): |
| 441 |
self.tree = SequencerTreeView(self) |
| 442 |
self.tree.setHeaderHidden(False) |
| 443 |
self.tree.setItemDelegateForColumn(1, ComboBoxDelegate(self, self.names_choices)) |
| 444 |
self.tree.setItemDelegateForColumn(2, LineEditDelegate(self)) |
| 445 |
self.load_seq_button = QtWidgets.QPushButton("Load sequence") |
| 446 |
self.load_seq_button.clicked.connect(self.load_sequence) |
| 447 |
self.load_seq_button.setToolTip("Load a sequence from a file.") |
| 448 |
|
| 449 |
self.save_seq_button = QtWidgets.QPushButton("Save sequence") |
| 450 |
self.save_seq_button.clicked.connect(self.save_sequence) |
| 451 |
self.save_seq_button.setToolTip("Save a sequence to a file.") |
| 452 |
|
| 453 |
self.queue_button = QtWidgets.QPushButton("Queue sequence") |
| 454 |
self.queue_button.clicked.connect(self.queue_sequence) |
| 455 |
|
| 456 |
self.add_root_item_btn = QtWidgets.QPushButton("Add root item") |
| 457 |
self.add_root_item_btn.clicked.connect( |
| 458 |
partial(self._add_tree_item, level=0) |
| 459 |
) |
| 460 |
|
| 461 |
self.add_tree_item_btn = QtWidgets.QPushButton("Add item") |
| 462 |
self.add_tree_item_btn.clicked.connect(self._add_tree_item) |
| 463 |
|
| 464 |
self.remove_tree_item_btn = QtWidgets.QPushButton("Remove item") |
| 465 |
self.remove_tree_item_btn.clicked.connect(self._remove_selected_tree_item) |
| 466 |
|
| 467 |
def _layout(self): |
| 468 |
|
| 469 |
btn_box = QtWidgets.QHBoxLayout() |
| 470 |
btn_box.addWidget(self.load_seq_button) |
| 471 |
btn_box.addWidget(self.save_seq_button) |
| 472 |
|
| 473 |
btn_box_2 = QtWidgets.QHBoxLayout() |
| 474 |
btn_box_2.addWidget(self.add_root_item_btn) |
| 475 |
btn_box_2.addWidget(self.add_tree_item_btn) |
| 476 |
btn_box_2.addWidget(self.remove_tree_item_btn) |
| 477 |
|
| 478 |
btn_box_3 = QtWidgets.QHBoxLayout() |
| 479 |
btn_box_3.addWidget(self.queue_button) |
| 480 |
|
| 481 |
vbox = QtWidgets.QVBoxLayout(self) |
| 482 |
vbox.setSpacing(6) |
| 483 |
vbox.addLayout(btn_box) |
| 484 |
vbox.addWidget(self.tree) |
| 485 |
vbox.addLayout(btn_box_2) |
| 486 |
vbox.addLayout(btn_box_3) |
| 487 |
self.setLayout(vbox) |
| 488 |
|
| 489 |
def _add_tree_item(self, *, level=None, parameter=None): |
| 490 |
""" |
| 491 |
Add an item to the sequence tree. An item will be added as a child |
| 492 |
to the selected (existing) item, except when level is given. |
| 493 |
|
| 494 |
:param level: An integer value determining the level at which an |
| 495 |
item is added. If level is 0, a root item will be added. |
| 496 |
|
| 497 |
:param parameter: If given, the parameter field is pre-filled |
| 498 |
""" |
| 499 |
|
| 500 |
selected = self.tree.selectionModel().selection().indexes() |
| 501 |
|
| 502 |
if len(selected) >= 1 and level != 0: |
| 503 |
parent = selected[0] |
| 504 |
else: |
| 505 |
parent = None |
| 506 |
|
| 507 |
if parameter is None: |
| 508 |
parameter = self.names_choices[0] |
| 509 |
|
| 510 |
model = self.tree.model() |
| 511 |
node_index = model.add_node(parameter=parameter, parent=parent) |
| 512 |
self.tree.openPersistentEditor(model.index(node_index.row(), 1, parent)) |
| 513 |
self.tree.expandAll() |
| 514 |
|
| 515 |
self.tree.selectRow(node_index) |
| 516 |
|
| 517 |
def _remove_selected_tree_item(self): |
| 518 |
""" |
| 519 |
Remove the selected item (and any child items) from the sequence tree. |
| 520 |
""" |
| 521 |
|
| 522 |
selected = self.tree.selectionModel().selection().indexes() |
| 523 |
|
| 524 |
if len(selected) == 0: |
| 525 |
return |
| 526 |
|
| 527 |
node_index = self.tree.model().remove_node(selected[0]) |
| 528 |
|
| 529 |
if node_index.isValid(): |
| 530 |
self.tree.selectRow(node_index) |
| 531 |
|
| 532 |
def get_sequence(self): |
| 533 |
return self.data.parameters_sequence(self.names_inv) |
| 534 |
|
| 535 |
def queue_sequence(self): |
| 536 |
""" |
| 537 |
Obtain a list of parameters from the sequence tree, enter these into |
| 538 |
procedures, and queue these procedures. |
| 539 |
""" |
| 540 |
|
| 541 |
self.queue_button.setEnabled(False) |
| 542 |
|
| 543 |
try: |
| 544 |
sequence = self.get_sequence() |
| 545 |
except SequenceEvaluationError: |
| 546 |
log.error("Evaluation of one of the sequence strings went wrong, no sequence queued.") |
| 547 |
else: |
| 548 |
log.info( |
| 549 |
"Queuing %d measurements based on the entered sequences." % len(sequence) |
| 550 |
) |
| 551 |
|
| 552 |
for entry in sequence: |
| 553 |
QtWidgets.QApplication.processEvents() |
| 554 |
parameters = dict(ChainMap(*entry[::-1])) |
| 555 |
|
| 556 |
procedure = self._parent.make_procedure() |
| 557 |
procedure.set_parameters(parameters) |
| 558 |
self._parent.queue(procedure=procedure) |
| 559 |
|
| 560 |
finally: |
| 561 |
self.queue_button.setEnabled(True) |
| 562 |
|
| 563 |
def save_sequence(self): |
| 564 |
dialog = SequenceDialog(save=True) |
| 565 |
if dialog.exec(): |
| 566 |
filename = dialog.selectedFiles()[0] |
| 567 |
with open(filename, 'w') as file_object: |
| 568 |
self.tree.save(file_object) |
| 569 |
log.info('Saved sequence file %s' % filename) |
| 570 |
|
| 571 |
def load_sequence(self, *, filename=None): |
| 572 |
""" |
| 573 |
Load a sequence from a .txt file. |
| 574 |
|
| 575 |
:param filename: Filename (string) of the to-be-loaded file. |
| 576 |
""" |
| 577 |
append_flag = False |
| 578 |
|
| 579 |
if (filename is None) or (filename == ''): |
| 580 |
dialog = SequenceDialog() |
| 581 |
if dialog.exec(): |
| 582 |
append_flag = dialog.append_checkbox.checkState() == QtCore.Qt.CheckState.Checked |
| 583 |
filenames = dialog.selectedFiles() |
| 584 |
filename = filenames[0] |
| 585 |
else: |
| 586 |
return |
| 587 |
|
| 588 |
with open(filename, 'r') as file_object: |
| 589 |
self.tree.model().load(file_object, append=append_flag) |
| 590 |
self.tree.expandAll() |