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
This commit is contained in:
Billy Su 2018-04-05 00:25:36 +08:00
parent 42d96d0c21
commit 90857e7f27

View File

@ -694,16 +694,9 @@ class PlotItem(GraphicsWidget):
## Qt's SVG-writing capabilities are pretty terrible. ## Qt's SVG-writing capabilities are pretty terrible.
def writeSvgCurves(self, fileName=None): def writeSvgCurves(self, fileName=None):
if fileName is None: if fileName is None:
self.fileDialog = FileDialog() self._choose_filename_dialog(handler=self.writeSvg)
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)
return return
#if fileName is None:
#fileName = QtGui.QFileDialog.getSaveFileName()
if isinstance(fileName, tuple): if isinstance(fileName, tuple):
raise Exception("Not implemented yet..") raise Exception("Not implemented yet..")
fileName = str(fileName) fileName = str(fileName)
@ -780,7 +773,9 @@ class PlotItem(GraphicsWidget):
def writeSvg(self, fileName=None): def writeSvg(self, fileName=None):
if fileName is None: if fileName is None:
fileName = QtGui.QFileDialog.getSaveFileName() self._choose_filename_dialog(handler=self.writeSvg)
return
fileName = str(fileName) fileName = str(fileName)
PlotItem.lastFileDir = os.path.dirname(fileName) PlotItem.lastFileDir = os.path.dirname(fileName)
@ -790,16 +785,9 @@ class PlotItem(GraphicsWidget):
def writeImage(self, fileName=None): def writeImage(self, fileName=None):
if fileName is None: if fileName is None:
self.fileDialog = FileDialog() self._choose_filename_dialog(handler=self.writeImage)
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)
return return
#if fileName is None:
#fileName = QtGui.QFileDialog.getSaveFileName()
if isinstance(fileName, tuple): if isinstance(fileName, tuple):
raise Exception("Not implemented yet..") raise Exception("Not implemented yet..")
fileName = str(fileName) fileName = str(fileName)
@ -813,16 +801,9 @@ class PlotItem(GraphicsWidget):
def writeCsv(self, fileName=None): def writeCsv(self, fileName=None):
if fileName is None: if fileName is None:
self.fileDialog = FileDialog() self._choose_filename_dialog(handler=self.writeCsv)
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)
return return
#if fileName is None:
#fileName = QtGui.QFileDialog.getSaveFileName()
fileName = str(fileName) fileName = str(fileName)
PlotItem.lastFileDir = os.path.dirname(fileName) PlotItem.lastFileDir = os.path.dirname(fileName)
@ -1227,3 +1208,11 @@ class PlotItem(GraphicsWidget):
#else: #else:
#self.autoBtn.show() #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)