Skip to content

Commit

Permalink
Merge pull request #1026 from nstelter-slac/notes_to_rules
Browse files Browse the repository at this point in the history
ENH: Add notes to rules
  • Loading branch information
YektaY authored Aug 29, 2023
2 parents bb77393 + 8bdd83b commit 32262f5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
46 changes: 46 additions & 0 deletions pydm/tests/widgets/test_rules_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,49 @@ def test_rules_editor_open_help(qtbot, monkeypatch):
lambda *args, **kwargs: '')
re.open_help()
re.cancelChanges()


def test_rules_editor_add_notes(qtbot, monkeypatch):
"""
Test adding notes for a widget's rules
Parameters
----------
qtbot : fixture
pytest-qt window for widget test
monkeypatch : fixture
To override dialog behaviors
"""
# Create the base widget
widget = DummyWidget()


# Create the rules data for the widget
rules_list = [{'name': 'Rule #1', 'property': 'Enable',
'initial_value': 'False',
'expression': 'ch[0] > 1',
'channels': [
{'channel': 'ca://MTEST:Float', 'trigger': True}]}]
# Add the rules to the widget
widget.rules = json.dumps(rules_list)

# Create a new Editor Window
re = RulesEditor(widget)
qtbot.addWidget(re)
re.show()

qtbot.waitExposed(re, timeout=5000)

re.lst_rules.setCurrentRow(0)

# make sure notes are saved and loaded correctly
# in the scope of the current rules-editor window
testing_text = "Testing adding note!"
re.open_notes_window()
re.notes_edit.setPlainText(testing_text)
re.save_notes()
re.open_notes_window()
loaded_text_from_curr_editor_instance = re.notes_edit.toPlainText()
assert loaded_text_from_curr_editor_instance == testing_text

re.cancelChanges()
2 changes: 1 addition & 1 deletion pydm/widgets/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def unregister_widget_rules(widget):
def register_widget_rules(widget):
"""
Given a widget to start from, traverse the tree of child widgets,
and try to unregister rules to any widgets.
and try to register rules to any widgets.
Parameters
----------
Expand Down
60 changes: 55 additions & 5 deletions pydm/widgets/rules_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import webbrowser

from qtpy import QtWidgets, QtCore, QtDesigner
from qtpy.QtCore import Qt
from ..utilities.iconfont import IconFont


Expand Down Expand Up @@ -190,24 +191,72 @@ def setup_ui(self):
expression_layout.addRow(lbl_initial, self.txt_initial_value)

lbl_expression = QtWidgets.QLabel("Expression:")
expr_help_layout = QtWidgets.QHBoxLayout()
expr_layout = QtWidgets.QHBoxLayout()
self.txt_expression = QtWidgets.QLineEdit()
self.txt_expression.editingFinished.connect(self.expression_changed)
expr_help_layout.addWidget(self.txt_expression)
expr_layout.addWidget(self.txt_expression)
expression_layout.addRow(lbl_expression, expr_layout)

notes_help_layout = QtWidgets.QHBoxLayout()
self.btn_help = QtWidgets.QPushButton()
self.btn_help.setAutoDefault(False)
self.btn_help.setDefault(False)
self.btn_help.setText("Help")
self.btn_help.setStyleSheet("background-color: rgb(176, 227, 255);")
self.btn_help.clicked.connect(functools.partial(self.open_help, open=True))
expr_help_layout.addWidget(self.btn_help)
expression_layout.addRow(lbl_expression, expr_help_layout)
self.btn_open_notes = QtWidgets.QPushButton("Notes")
notes_help_layout.addWidget(self.btn_open_notes)
self.btn_open_notes.clicked.connect(self.open_notes_window)
notes_help_layout.addWidget(self.btn_help)
expression_layout.addRow(notes_help_layout)

self.cmb_property.currentIndexChanged.connect(self.property_changed)
self.cmb_property.setCurrentText(self.default_property)

frm_edit_layout.addLayout(expression_layout)

def open_notes_window(self):
""" Window for reading and adding to a rule's notes"""
self.notes_window = QtWidgets.QDialog(self)
self.notes_window.setWindowTitle("Notes")

# Allow window to be resized and maximized
self.notes_window.setWindowFlag(Qt.WindowMinMaxButtonsHint)
notes_layout = QtWidgets.QVBoxLayout()

self.notes_title_label = QtWidgets.QLabel("Notes:")
notes_layout.addWidget(self.notes_title_label)

self.notes_edit = QtWidgets.QPlainTextEdit(self)

# Load notes saved in current instance of rules editor
item = self.lst_rules.currentItem()
idx = self.lst_rules.indexFromItem(item).row()
data = self.rules[idx]
curr_notes = data.get('notes', '')
self.notes_edit.setPlainText(curr_notes)
notes_layout.addWidget(self.notes_edit)

buttons_layout = QtWidgets.QHBoxLayout()
self.save_button = QtWidgets.QPushButton("Save")
self.save_button.clicked.connect(self.save_notes)
self.cancel_button = QtWidgets.QPushButton("Cancel")
self.cancel_button.clicked.connect(self.notes_window.close)
buttons_layout.addStretch()
buttons_layout.addWidget(self.cancel_button)
buttons_layout.addWidget(self.save_button)

notes_layout.addLayout(buttons_layout)
self.notes_window.setLayout(notes_layout)
self.notes_window.show()

def save_notes(self):
""" Save notes text and close the notes window """
item = self.lst_rules.currentItem()
idx = self.lst_rules.indexFromItem(item).row()
self.rules[idx]['notes'] = self.notes_edit.toPlainText()
self.notes_window.close()

def clear_form(self):
"""Clear the form and reset the fields."""
self.loading_data = True
Expand Down Expand Up @@ -271,7 +320,8 @@ def add_rule(self):
"property": self.default_property,
"initial_value": "",
"expression": "",
"channels": []
"channels": [],
"notes": ""
}
self.rules.append(data)
self.lst_rule_item = QtWidgets.QListWidgetItem()
Expand Down

0 comments on commit 32262f5

Please sign in to comment.