
import IconProvider
from Settings import DIALOGFILTER, OUTPUTDATA

from PySide2 import QtWidgets, QtGui


class FileDialog:

    def __init__(self, filter=None):

        self.names = []
        self.filter = filter

    def getFilename(self):
        '''
        getSaveFileName([parent=None[, caption=""[, dir=""[, filter=""[,
                    selectedFilter=""[, options=QFileDialog.Options()]]]]]])
        '''
        filename, _ = QtWidgets.QFileDialog.getSaveFileName(
            self,
            'Save File As',
            '',
            "ReStructuredText Files (*.rst *.txt)"
        )
        if filename:
            text = self.editor.toPlainText()
            try:
                f = open(filename, "wb")
                f.write(text)
                f.close()
                # self.rebuildHTML()
            except IOError:
                QtGui.QMessageBox.information(
                    self,
                    "Unable to open file: %s" % filename
                )

        return filename

    def dialog(self):

        dialog = QtWidgets.QFileDialog()

        provider = IconProvider.IconProvider()
        dialog.setIconProvider(provider)
        dialog.setNameFilter(DIALOGFILTER)
        dialog.setNameFilterDetailsVisible(True)
        dialog.setDirectory(OUTPUTDATA)
        # allow only to select one file
        dialog.setFileMode(QtWidgets.QFileDialog.AnyFile)
        # display also size and date
        dialog.setViewMode(QtWidgets.QFileDialog.Detail)
        # make it a save dialog
        dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
        # put default name in the save dialog
        dialog.selectFile(self.lineedit.text())

        # open custom file dialog using custom icons
        if dialog.exec_():
            self.names = dialog.selectedFiles()
            # filter = dialog.selectedFilter()

        if not self.names:
            return

@@ -1,25 +1,4 @@
-%0D%0Aimport IconProvider
 %0D%0Afr
@@ -77,15 +77,8 @@
 gets
-, QtGui
 %0D%0A%0D%0A
@@ -85,20 +85,16 @@
 %0D%0Aclass 
-File
 Dialog:%0D
@@ -126,20 +126,28 @@
  filter=
-None
+DIALOGFILTER
 ):%0D%0A%0D%0A  
@@ -169,16 +169,80 @@
 s = %5B%5D%0D%0A
+        # DIALOGFILTER = 'Airfoil contour files (*.dat *.txt)'%0D%0A
         
@@ -273,19 +273,20 @@
     def 
-get
+save
 Filename
@@ -294,97 +294,47 @@
 self
-):%0D%0A        '''%0D%0A        getSaveFileName(%5Bparent=None%5B, caption=%22%22%5B, dir=%22%22%5B, filter=%22%22%5B,
+, filename=None):%0D%0A        %22%22%22Summary%0D%0A
 %0D%0A  
@@ -331,32 +331,39 @@
 mary%0D%0A%0D%0A        
+Args:%0D%0A
             sele
@@ -362,256 +362,129 @@
     
-selectedFilter=%22%22%5B, options=QFileDialog.Options()%5D%5D%5D%5D%5D%5D)%0D%0A        '''%0D%0A        filename, _ = QtWidgets.QFileDialog.getSaveFileName(%0D%0A            self,%0D%0A            'Save File As',%0D%0A            '',%0D%0A            %22ReStructuredText Files (*.rst *.txt)%22
+filename (None, optional): If given, then it is displayed as%0D%0A            default value in the dialog%0D%0A%0D%0A        Returns:
 %0D%0A  
@@ -493,21 +493,46 @@
     
-)%0D%0A
     
-    if
+string: filename inlcuding path to
  fil
@@ -536,17 +536,16 @@
 filename
-:
 %0D%0A      
@@ -554,358 +554,240 @@
     
-text = self.editor.toPlainText()%0D%0A            try:%0D%0A                f = open(filename, %22wb%22)%0D%0A                f.write(text)%0D%0A                f.close()%0D%0A                # self.rebuildHTML()%0D%0A            except IOError:%0D%0A                QtGui.QMessageBox.information(%0D%0A                    self,%0D%0A                    %22Unable to open file: %25s%22 %25 filename
+string: filter which was selected%0D%0A        %22%22%22%0D%0A        filename, selected_filter = QtWidgets.QFileDialog.getSaveFileName(%0D%0A            None,%0D%0A            'Save File As',%0D%0A            OUTPUTDATA + filename,%0D%0A            self.filter,
 %0D%0A  
@@ -796,20 +796,18 @@
         
-    
+''
 )%0D%0A%0D%0A   
@@ -830,26 +830,49 @@
 name
-%0D%0A%0D%0A    def dialog
+, selected_filter%0D%0A%0D%0A    def openFilename
 (sel
@@ -876,26 +876,24 @@
 self):%0D%0A
-%0D%0A
         
 dialog =
@@ -888,434 +888,195 @@
     
-dialog = QtWidgets.QFileDialog()%0D%0A%0D%0A        provider = IconProvider.IconProvider()%0D%0A        dialog.setIconProvider(provider)%0D%0A        dialog.setNameFilter(DIALOGFILTER)%0D%0A        dialog.setNameFilterDetailsVisible(True)%0D%0A        dialog.setDirectory(OUTPUTDATA)%0D%0A        # allow only to select one file%0D%0A        dialog.setFileMode(QtWidgets.QFileDialog.AnyFile)%0D%0A        # display also size and date%0D%0A        dialog.setViewMode(
+%22%22%22Summary%0D%0A%0D%0A%0D%0A        Returns:%0D%0A            string: filename inlcuding path to filename%0D%0A            string: filter which was selected%0D%0A        %22%22%22%0D%0A        filename, selected_filter = 
 QtWi
@@ -1097,391 +1097,217 @@
 log.
-Detail)%0D%0A        # make it a save dialog%0D%0A        dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)%0D%0A        # put default name in the save dialog%0D%0A        dialog.selectFile(self.lineedit.text())%0D%0A%0D%0A        # open custom file dialog using custom icons%0D%0A        if dialog.exec_():%0D%0A            self.names = dialog.selectedFiles()%0D%0A            # filter = dialog.selectedF
+getOpenFileName(%0D%0A            None,%0D%0A            'Open File',%0D%0A            OUTPUTDATA,%0D%0A            self.filter,%0D%0A            '')%0D%0A%0D%0A        return filename, selected_filter%0D%0A%0D%0A    def setFilter(self, f
 ilter
-()%0D%0A
+):
 %0D%0A  
@@ -1316,44 +1316,26 @@
     
-if not self.names:%0D%0A            return
+self.filter = filter
 %0D%0A

put open/save dialogs in dedicated class