From 90857e7f27fb0a0523754e41646b4b3a0a73e1e4 Mon Sep 17 00:00:00 2001 From: Billy Su Date: Thu, 5 Apr 2018 00:25:36 +0800 Subject: [PATCH] Extract _choose_filename_dialog() as helper func To reduce the redundancy, extract the same code as a helper function _choose_filename_dialog() in PlotItem class --- pyqtgraph/graphicsItems/PlotItem/PlotItem.py | 45 ++++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py index 65e10439..fdf60399 100644 --- a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py +++ b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py @@ -694,16 +694,9 @@ class PlotItem(GraphicsWidget): ## Qt's SVG-writing capabilities are pretty terrible. def writeSvgCurves(self, fileName=None): if fileName is None: - self.fileDialog = FileDialog() - if PlotItem.lastFileDir is not None: - self.fileDialog.setDirectory(PlotItem.lastFileDir) - self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile) - self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) - self.fileDialog.show() - self.fileDialog.fileSelected.connect(self.writeSvg) + self._choose_filename_dialog(handler=self.writeSvg) return - #if fileName is None: - #fileName = QtGui.QFileDialog.getSaveFileName() + if isinstance(fileName, tuple): raise Exception("Not implemented yet..") fileName = str(fileName) @@ -780,7 +773,9 @@ class PlotItem(GraphicsWidget): def writeSvg(self, fileName=None): if fileName is None: - fileName = QtGui.QFileDialog.getSaveFileName() + self._choose_filename_dialog(handler=self.writeSvg) + return + fileName = str(fileName) PlotItem.lastFileDir = os.path.dirname(fileName) @@ -790,16 +785,9 @@ class PlotItem(GraphicsWidget): def writeImage(self, fileName=None): if fileName is None: - self.fileDialog = FileDialog() - if PlotItem.lastFileDir is not None: - self.fileDialog.setDirectory(PlotItem.lastFileDir) - self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile) - self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) - self.fileDialog.show() - self.fileDialog.fileSelected.connect(self.writeImage) + self._choose_filename_dialog(handler=self.writeImage) return - #if fileName is None: - #fileName = QtGui.QFileDialog.getSaveFileName() + if isinstance(fileName, tuple): raise Exception("Not implemented yet..") fileName = str(fileName) @@ -813,16 +801,9 @@ class PlotItem(GraphicsWidget): def writeCsv(self, fileName=None): if fileName is None: - self.fileDialog = FileDialog() - if PlotItem.lastFileDir is not None: - self.fileDialog.setDirectory(PlotItem.lastFileDir) - self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile) - self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) - self.fileDialog.show() - self.fileDialog.fileSelected.connect(self.writeCsv) + self._choose_filename_dialog(handler=self.writeCsv) return - #if fileName is None: - #fileName = QtGui.QFileDialog.getSaveFileName() + fileName = str(fileName) PlotItem.lastFileDir = os.path.dirname(fileName) @@ -1227,3 +1208,11 @@ class PlotItem(GraphicsWidget): #else: #self.autoBtn.show() + def _choose_filename_dialog(self, handler): + self.fileDialog = FileDialog() + if PlotItem.lastFileDir is not None: + self.fileDialog.setDirectory(PlotItem.lastFileDir) + self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile) + self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) + self.fileDialog.show() + self.fileDialog.fileSelected.connect(handler)