Skip to content

Commit

Permalink
Merge pull request #308 from Alejo7896/window_save_button
Browse files Browse the repository at this point in the history
Added copy figure and save figure functionality to the pyqtgraph plot…
  • Loading branch information
astafan8 committed Jul 14, 2022
2 parents fd2b552 + b0bcfe7 commit 2d9995b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
3 changes: 1 addition & 2 deletions plottr/apps/autoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,12 @@ def autoplotDDH5(filepath: str = '', groupname: str = 'data') \
win = AutoPlotMainWindow(fc, loaderName='Data loader',
widgetOptions=widgetOptions,
monitor=True,
monitorInterval=5.0)
monitorInterval=0.0)
win.show()

fc.nodes()['Data loader'].filepath = filepath
fc.nodes()['Data loader'].groupname = groupname
win.refreshData()
win.setMonitorInterval(0.0)

return fc, win

Expand Down
49 changes: 49 additions & 0 deletions plottr/plot/pyqtgraph/autoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"""

import logging
from pathlib import Path
import time
from dataclasses import dataclass
from typing import List, Optional, Any

Expand All @@ -25,6 +27,7 @@

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
TIMESTRFORMAT = "%Y-%m-%dT%H%M%S"


class FigureWidget(QtWidgets.QWidget):
Expand Down Expand Up @@ -301,6 +304,8 @@ def _plotData(self, **kwargs: Any) -> None:
parent=self)
self.layout().addWidget(self.figConfig)
self.figConfig.optionsChanged.connect(self._refreshPlot)
self.figConfig.figCopied.connect(self.onfigCopied)
self.figConfig.figSaved.connect(self.onfigSaved)

if self.data.has_meta('title'):
self.fmWidget.setTitle(self.data.meta_val('title'))
Expand All @@ -309,6 +314,36 @@ def _plotData(self, **kwargs: Any) -> None:
def _refreshPlot(self) -> None:
self._plotData()

@Slot()
def onfigCopied(self) -> None:
"""
Gets triggered when figCopied signal is emitted from self.figConfig
Copy the current figuremaker widget to the clipboard.
"""
assert isinstance(self.fmWidget, FigureWidget)
screenshot = self.fmWidget.grab(rectangle=QtCore.QRect(QtCore.QPoint(0, 0), QtCore.QSize(-1, -1)))
clipboard = QtWidgets.QApplication.clipboard()
clipboard.setImage(screenshot.toImage())

@Slot()
def onfigSaved(self) -> None:
"""
Gets triggered when figSaved signal is emitted from self.figConfig
Save the current figuremaker widget to the data directory as a png
with a timestamp at the front.
"""
assert isinstance(self.fmWidget, FigureWidget)
assert isinstance(self.data, DataDictBase)
screenshot = self.fmWidget.grab(rectangle=QtCore.QRect(QtCore.QPoint(0, 0), QtCore.QSize(-1, -1)))
path = Path(self.data.meta_val('title'))
# add a timestamp here
t = time.localtime()
time_str = time.strftime(TIMESTRFORMAT, t)
filename = time_str+'_'+str(path.stem)+'.png'
screenshot.save(str(path.parent)+'/'+filename, format='PNG')

# TODO: Allow for the option to choose filetypes and the name/directory


@dataclass
class FigureOptions:
Expand All @@ -329,6 +364,10 @@ class FigureConfigToolBar(QtWidgets.QToolBar):

#: Signal() -- emitted when options have been changed in the GUI.
optionsChanged = Signal()
#: Signal() -- emitted when the copy figure button has been pressed
figCopied = Signal()
#: Signal() -- emitted when the save figure button has been pressed
figSaved = Signal()

def __init__(self, options: FigureOptions,
parent: Optional[QtWidgets.QWidget] = None) -> None:
Expand Down Expand Up @@ -370,6 +409,16 @@ def __init__(self, options: FigureOptions,
complexButton.setMenu(complexOptions)
self.addWidget(complexButton)

# Adding functionality to copy and save the graph
self.copyFig = self.addAction('Copy Figure', self._copyFig)
self.saveFig = self.addAction('Save Figure', self._saveFig)

def _setOption(self, option: str, value: Any) -> None:
setattr(self.options, option, value)
self.optionsChanged.emit()

def _copyFig(self) -> None:
self.figCopied.emit()

def _saveFig(self) -> None:
self.figSaved.emit()

0 comments on commit 2d9995b

Please sign in to comment.