Skip to content

Commit

Permalink
ENH #70 connect checkbox to plot update - does not yet work for unche…
Browse files Browse the repository at this point in the history
…cking boxes
  • Loading branch information
rodolakis committed Feb 9, 2024
1 parent adf2ec4 commit b1196d2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 17 deletions.
3 changes: 1 addition & 2 deletions mdaviz/chartview.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def addCurve(self, *args, **kwargs):
# Add to the dictionary
label = kwargs.get("label", None)
self.line2D[label] = plot_obj[0], args[0], args[1]
print(f"Updating dict: {list(self.line2D.keys())}")
# Add to the comboBox
self.addIemCurveBox(label)

Expand Down Expand Up @@ -342,4 +341,4 @@ def clearCursorInfo(self):
"press middle click"
)
self.parent.findChild(QtWidgets.QLineEdit, "diff_text").setText("n/a")
self.parent.findChild(QtWidgets.QLineEdit, "midpoint_text").setText("n/a")
self.parent.findChild(QtWidgets.QLineEdit, "midpoint_text").setText("n/a")
79 changes: 67 additions & 12 deletions mdaviz/mda_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,56 @@ def setup(self):

# # ------------ Folder & file selection methods:

def onCheckboxStateChange(self, selections):
"""Slot: data field (for plotting) changes."""
from .chartview import ChartViewMpl
from .select_fields_table_view import to_datasets_mpl

# Get the matplotlib chartview widget, if exists:
layoutMpl = self.mda_file_visualization.plotPageMpl.layout()
if layoutMpl.count() != 1: # in case something changes ...
raise RuntimeError("Expected exactly one widget in this layout!")
widgetMpl = layoutMpl.itemAt(0).widget()

mode = self.select_fields_tableview.mode()

# Exceptions:
if not selections.get("Y"): # no DET selected
widgetMpl.clearPlot()
return
if not selections.get("X"): # if no POS, default to index
selections["X"] = 0

# Get info for the file & POS/DET selection:
detsDict = self.select_fields_tableview.detsDict()
fileName = self.select_fields_tableview.fileName()
datasets_mpl, options_mpl = to_datasets_mpl(fileName, detsDict, selections)

print(f"\nonCheckboxStateChange called: {mode} {fileName} with {selections}")

if mode in ("Auto-replace"):
if not isinstance(widgetMpl, ChartViewMpl):
widgetMpl = ChartViewMpl(self, **options_mpl) # Make a blank chart.
else:
widgetMpl.clearPlot()
for ds, ds_options in datasets_mpl:
widgetMpl.plot(*ds, **ds_options)
self.mda_file_visualization.setPlot(widgetMpl)

elif mode in ("Auto-add"):
if not isinstance(widgetMpl, ChartViewMpl):
widgetMpl = ChartViewMpl(self, **options_mpl) # Make a blank chart.
for ds, ds_options in datasets_mpl:
widgetMpl.plot(*ds, **ds_options)
self.mda_file_visualization.setPlot(widgetMpl)

elif mode in ("Auto-off"):
return

def doFileSelected(self, index):
"""
Display field table view, file metadata, and plot first pos & det depending on mode.
Display field table view, file metadata, and plot first pos & det depending on mode.
If the graph is blank, selecting a file:
- auto-add: automatically plots the 1st pos/det
- auto-replace : same
Expand All @@ -83,12 +129,11 @@ def doFileSelected(self, index):
- auto-add: automatically add to plot the same pos/det that was already plotted
- auto-replace : automatically replace plot wiht the same pos/det that was already plotted
- auto-off: nothing happens
Args:
index (int): file index
"""



model = self.mda_folder_tableview.tableView.model()
if model is not None:
self.select_fields_tableview.displayTable(index.row())
Expand All @@ -98,10 +143,22 @@ def doFileSelected(self, index):
self.select_fields_tableview.selected.disconnect()
except TypeError: # No slots connected yet
pass
self.select_fields_tableview.selected.connect(partial(self.doPlot,comment="Button triggered."))
self.select_fields_tableview.selected.connect(
partial(self.doPlot, comment="Button triggered.")
)
try:
self.select_fields_tableview.tableView.model().checkboxStateChanged.disconnect()
except TypeError: # No slots connected yet
pass
self.select_fields_tableview.tableView.model().checkboxStateChanged.connect(
self.onCheckboxStateChange
)

# Try something else:
self.setStatus(f"\n\n======== Selected file: {self.mdaFileList()[index.row()]}")

self.setStatus(
f"\n\n======== Selected file: {self.mdaFileList()[index.row()]}"
)

first_pos_idx = self.select_fields_tableview.firstPos()
first_det_idx = self.select_fields_tableview.firstDet()
if first_pos_idx is not None and first_det_idx is not None:
Expand Down Expand Up @@ -159,11 +216,11 @@ def doRefresh(self):

# # ------------ Plot methods:

def doPlot(self, *args,**kwargs):
def doPlot(self, *args, **kwargs):
"""Slot: data field selected (for plotting) button is clicked."""
from .chartview import ChartViewMpl
from .select_fields_table_view import to_datasets_mpl

action = args[0]
selections = args[1]
print(f"\ndoPlot called: {args=}; {kwargs=}")
Expand Down Expand Up @@ -191,15 +248,13 @@ def doPlot(self, *args,**kwargs):
else:
widgetMpl.clearPlot()
for ds, ds_options in datasets_mpl:
print(f"{ds_options=}")
widgetMpl.plot(*ds, **ds_options)
self.mda_file_visualization.setPlot(widgetMpl)

elif action in ("add"):
if not isinstance(widgetMpl, ChartViewMpl):
widgetMpl = ChartViewMpl(self, **options_mpl) # Make a blank chart.
for ds, ds_options in datasets_mpl:
print(f"{ds_options=}")
widgetMpl.plot(*ds, **ds_options)
self.mda_file_visualization.setPlot(widgetMpl)

Expand Down
8 changes: 6 additions & 2 deletions mdaviz/select_fields_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class SelectFieldsTableModel(QtCore.QAbstractTableModel):
https://doc.qt.io/qtforpython-5/PySide2/QtCore/QAbstractTableModel.html
"""

# Signals in PyQt5 should be class attributes, not instance attributes, to work properly.
# They need to be defined at the class level so that PyQt can set them up correctly when instances of the class are created.

checkboxStateChanged = QtCore.pyqtSignal(dict) # emit field selection

def __init__(self, columns, fields, first_pos, first_det, parent=None):
self.selections = {first_pos: "X", first_det: "Y"} if first_det else {}

Expand Down Expand Up @@ -186,8 +191,7 @@ def setCheckbox(self, index, state):
changes = self.applySelectionRules(index, changes)
if changes:
self.updateCheckboxes()
# self.logCheckboxSelections()
# self.setStatus(self.plotFields()[1]) # plotter should call plotFields()
self.checkboxStateChanged.emit(self.plotFields()[0])

def applySelectionRules(self, index, changes=False):
"""Apply selection rules 2-4."""
Expand Down
8 changes: 7 additions & 1 deletion mdaviz/select_fields_table_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class SelectFieldsTableView(QtWidgets.QWidget):
ui_file = utils.getUiFileName(__file__)
selected = QtCore.pyqtSignal(str, dict)
fieldchange = QtCore.pyqtSignal(str, dict)

def __init__(self, parent):
self.parent = parent
Expand All @@ -54,14 +55,19 @@ def setup(self):
self.clearButton.clicked.connect(partial(self.responder, "clear"))
self.replaceButton.clicked.connect(partial(self.responder, "replace"))

# model = self.parent.select_fields_tableview.tableView.model()
# if model is not None:
# print("\nModel is not None")
# model.checkboxStateChanged.connect(self.onCheckboxStateChange)

options = ["Auto-add", "Auto-replace", "Auto-off"]
self._mode = options[0]
self.autoBox.addItems(options)
self.autoBox.currentTextChanged.connect(self.setMode)

def responder(self, action):
"""Modify the plot with the described action."""
print(f"/nResponder: {action=}")
print(f"\nResponder: {action=}")
self.selected.emit(action, self.tableView.model().plotFields()[0])

def file(self):
Expand Down

0 comments on commit b1196d2

Please sign in to comment.