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.
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)