Plugins/WizardPlugins/FileDialogWizard/FileDialogWizardDialog.py

branch
Py2 comp.
changeset 3142
55030c09e142
parent 3056
9986ec0e559a
parent 3124
a01e410893ac
child 3145
a9de05d4a22f
--- a/Plugins/WizardPlugins/FileDialogWizard/FileDialogWizardDialog.py	Fri Dec 13 22:45:47 2013 +0100
+++ b/Plugins/WizardPlugins/FileDialogWizard/FileDialogWizardDialog.py	Fri Dec 13 23:39:14 2013 +0100
@@ -12,7 +12,7 @@
 import os
 
 from PyQt4.QtCore import pyqtSlot
-from PyQt4.QtGui import QDialog, QDialogButtonBox, QFileDialog
+from PyQt4.QtGui import QDialog, QDialogButtonBox, QFileDialog, QButtonGroup
 
 from E5Gui.E5Completers import E5FileCompleter, E5DirCompleter
 
@@ -28,10 +28,11 @@
     It displays a dialog for entering the parameters
     for the QFileDialog code generator.
     """
-    def __init__(self, parent=None):
+    def __init__(self, pyqtVariant, parent=None):
         """
         Constructor
         
+        @param pyqtVariant variant of PyQt (integer; 0, 4 or 5)
         @param parent parent widget (QWidget)
         """
         super(FileDialogWizardDialog, self).__init__(parent)
@@ -40,6 +41,27 @@
         self.eStartWithCompleter = E5FileCompleter(self.eStartWith)
         self.eWorkDirCompleter = E5DirCompleter(self.eWorkDir)
         
+        self.__pyqtVariant = pyqtVariant
+        
+        self.__typeButtonsGroup = QButtonGroup(self)
+        self.__typeButtonsGroup.setExclusive(True)
+        self.__typeButtonsGroup.addButton(self.rOpenFile, 1)
+        self.__typeButtonsGroup.addButton(self.rOpenFiles, 2)
+        self.__typeButtonsGroup.addButton(self.rSaveFile, 3)
+        self.__typeButtonsGroup.addButton(self.rfOpenFile, 11)
+        self.__typeButtonsGroup.addButton(self.rfOpenFiles, 12)
+        self.__typeButtonsGroup.addButton(self.rfSaveFile, 13)
+        self.__typeButtonsGroup.addButton(self.rDirectory, 20)
+        self.__typeButtonsGroup.buttonClicked[int].connect(
+            self.__toggleInitialFilterAndResult)
+        self.__toggleInitialFilterAndResult(1)
+        
+        self.pyqtComboBox.addItems(["PyQt4", "PyQt5"])
+        if self.__pyqtVariant == 5:
+            self.pyqtComboBox.setCurrentIndex(1)
+        else:
+            self.pyqtComboBox.setCurrentIndex(0)
+        
         self.rSaveFile.toggled[bool].connect(self.__toggleConfirmCheckBox)
         self.rfSaveFile.toggled[bool].connect(self.__toggleConfirmCheckBox)
         self.rDirectory.toggled[bool].connect(self.__toggleGroupsAndTest)
@@ -49,7 +71,7 @@
         
         self.bTest = self.buttonBox.addButton(
             self.trUtf8("Test"), QDialogButtonBox.ActionRole)
-        
+    
     def __adjustOptions(self, options):
         """
         Private method to adjust the file dialog options.
@@ -60,7 +82,30 @@
         if Globals.isLinuxPlatform():
             options |= QFileDialog.DontUseNativeDialog
         return options
+    
+    @pyqtSlot(str)
+    def on_pyqtComboBox_currentIndexChanged(self, txt):
+        """
+        Private slot to setup the dialog for the selected PyQt variant.
         
+        @param txt text of the selected combo box entry (string)
+        """
+        self.rfOpenFile.setEnabled(txt == "PyQt4")
+        self.rfOpenFiles.setEnabled(txt == "PyQt4")
+        self.rfSaveFile.setEnabled(txt == "PyQt4")
+        
+        if txt == "PyQt5":
+            if self.rfOpenFile.isChecked():
+                self.rOpenFile.setChecked(True)
+            elif self.rfOpenFiles.isChecked():
+                self.rOpenFiles.setChecked(True)
+            elif self.rfSaveFile.isChecked():
+                self.rSaveFile.setChecked(True)
+        
+        self.__pyqtVariant = 5 if txt == "PyQt5" else 4
+        
+        self.__toggleInitialFilterAndResult(self.__typeButtonsGroup.checkedId())
+    
     def on_buttonBox_clicked(self, button):
         """
         Private slot called by a button of the button box clicked.
@@ -81,36 +126,63 @@
             else:
                 options = QFileDialog.Options()
             options = self.__adjustOptions(options)
-            QFileDialog.getOpenFileName(
-                None,
-                self.eCaption.text(),
-                self.eStartWith.text(),
-                self.eFilters.text(),
-                options)
+            if self.rOpenFile.isChecked() and self.__pyqtVariant == 4:
+                QFileDialog.getOpenFileName(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    options)
+            else:
+                QFileDialog.getOpenFileNameAndFilter(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    self.eInitialFilter.text(),
+                    options)
         elif self.rOpenFiles.isChecked() or self.rfOpenFiles.isChecked():
             if not self.cSymlinks.isChecked():
                 options = QFileDialog.Options(QFileDialog.DontResolveSymlinks)
             else:
                 options = QFileDialog.Options()
             options = self.__adjustOptions(options)
-            QFileDialog.getOpenFileNames(
-                None,
-                self.eCaption.text(),
-                self.eStartWith.text(),
-                self.eFilters.text(),
-                options)
+            if self.rOpenFiles.isChecked() and self.__pyqtVariant == 4:
+                QFileDialog.getOpenFileNames(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    options)
+            else:
+                QFileDialog.getOpenFileNamesAndFilter(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    self.eInitialFilter.text(),
+                    options)
         elif self.rSaveFile.isChecked() or self.rfSaveFile.isChecked():
             if not self.cSymlinks.isChecked():
                 options = QFileDialog.Options(QFileDialog.DontResolveSymlinks)
             else:
                 options = QFileDialog.Options()
             options = self.__adjustOptions(options)
-            QFileDialog.getSaveFileName(
-                None,
-                self.eCaption.text(),
-                self.eStartWith.text(),
-                self.eFilters.text(),
-                options)
+            if self.rSaveFile.isChecked() and self.__pyqtVariant == 4:
+                QFileDialog.getSaveFileName(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    options)
+            else:
+                QFileDialog.getSaveFileNameAndFilter(
+                    None,
+                    self.eCaption.text(),
+                    self.eStartWith.text(),
+                    self.eFilters.text(),
+                    self.eInitialFilter.text(),
+                    options)
         elif self.rDirectory.isChecked():
             options = QFileDialog.Options()
             if not self.cSymlinks.isChecked():
@@ -125,14 +197,14 @@
                 self.eCaption.text(),
                 self.eWorkDir.text(),
                 options)
-        
+    
     def __toggleConfirmCheckBox(self):
         """
         Private slot to enable/disable the confirmation check box.
         """
         self.cConfirmOverwrite.setEnabled(
             self.rSaveFile.isChecked() or self.rfSaveFile.isChecked())
-        
+    
     def __toggleGroupsAndTest(self):
         """
         Private slot to enable/disable certain groups and the test button.
@@ -146,10 +218,29 @@
             self.dirPropertiesGroup.setEnabled(False)
             self.bTest.setDisabled(
                 self.cStartWith.isChecked() or self.cFilters.isChecked())
+    
+    def __toggleInitialFilterAndResult(self, id):
+        """
+        Private slot to enable/disable the initial filter elements and the
+        results entries.
         
-    def __getCode4(self, indLevel, indString):
+        @param id id of the clicked button (integer)
         """
-        Private method to get the source code for Qt4/Qt5.
+        if (self.__pyqtVariant == 4 and id in [11, 12, 13]) or \
+                (self.__pyqtVariant == 5 and id in [1, 2, 3]):
+            enable = True
+        else:
+            enable = False
+        self.lInitialFilter.setEnabled(enable)
+        self.eInitialFilter.setEnabled(enable)
+        self.cInitialFilter.setEnabled(enable)
+        
+        self.lFilterVariable.setEnabled(enable)
+        self.eFilterVariable.setEnabled(enable)
+    
+    def getCode(self, indLevel, indString):
+        """
+        Public method to get the source code for Qt4 and Qt5.
         
         @param indLevel indentation level (int)
         @param indString string used for indentation (space or tab) (string)
@@ -161,14 +252,49 @@
         estring = os.linesep + indLevel * indString
         
         # now generate the code
-        code = 'QFileDialog.'
+        if self.parentSelf.isChecked():
+            parent = "self"
+        elif self.parentNone.isChecked():
+            parent = "None"
+        elif self.parentOther.isChecked():
+            parent = self.parentEdit.text()
+            if parent == "":
+                parent = "None"
+        
+        # prepare the result variables
+        nameVariable = self.eNameVariable.text()
+        if not nameVariable:
+            if self.__typeButtonsGroup.checkedButton() in [
+                    self.rOpenFile, self.rfOpenFile,
+                    self.rSaveFile, self.rfSaveFile]:
+                nameVariable = "fileName"
+            elif self.__typeButtonsGroup.checkedButton() in [
+                    self.rOpenFiles, self.rfOpenFiles]:
+                nameVariable = "fileNames"
+            elif self.__typeButtonsGroup.checkedButton() == self.rDirectory:
+                nameVariable = "dirName"
+            else:
+                nameVariable = "res"
+        filterVariable = self.eFilterVariable.text()
+        if not filterVariable:
+            if (self.__pyqtVariant == 4 and
+                self.__typeButtonsGroup.checkedButton() in [
+                    self.rfOpenFile, self.rfOpenFiles, self.rfSaveFile]) or \
+                (self.__pyqtVariant == 5 and
+                self.__typeButtonsGroup.checkedButton() in [
+                    self.rOpenFile, self.rOpenFiles, self.rSaveFile]):
+                filterVariable = ", selectedFilter"
+            else:
+                filterVariable = ""
+        
+        code = '{0}{1} = QFileDialog.'.format(nameVariable, filterVariable)
         if self.rOpenFile.isChecked() or self.rfOpenFile.isChecked():
             if self.rOpenFile.isChecked():
                 code += 'getOpenFileName({0}{1}'.format(os.linesep, istring)
             else:
                 code += 'getOpenFileNameAndFilter({0}{1}'.format(
                     os.linesep, istring)
-            code += 'None,{0}{1}'.format(os.linesep, istring)
+            code += '{0},{1}{2}'.format(parent, os.linesep, istring)
             if not self.eCaption.text():
                 code += '"",{0}{1}'.format(os.linesep, istring)
             else:
@@ -190,8 +316,16 @@
                 else:
                     fmt = 'self.trUtf8("{0}")'
                 code += fmt.format(self.eFilters.text())
-            if self.rfOpenFile.isChecked():
-                code += ',{0}{1}None'.format(os.linesep, istring)
+            if self.rfOpenFile.isChecked() or self.__pyqtVariant == 5:
+                if self.eInitialFilter.text() == "":
+                    filter = "None"
+                else:
+                    if self.cInitialFilter.isChecked():
+                        fmt = '{0}'
+                    else:
+                        fmt = 'self.trUtf8("{0}")'
+                    filter = fmt.format(self.eInitialFilter.text())
+                code += ',{0}{1}{2}'.format(os.linesep, istring, filter)
             if not self.cSymlinks.isChecked():
                 code += \
                     ',{0}{1}QFileDialog.Options(' \
@@ -204,7 +338,7 @@
             else:
                 code += 'getOpenFileNamesAndFilter({0}{1}'.format(
                     os.linesep, istring)
-            code += 'None,{0}{1}'.format(os.linesep, istring)
+            code += '{0},{1}{2}'.format(parent, os.linesep, istring)
             if not self.eCaption.text():
                 code += '"",{0}{1}'.format(os.linesep, istring)
             else:
@@ -226,8 +360,16 @@
                 else:
                     fmt = 'self.trUtf8("{0}")'
                 code += fmt.format(self.eFilters.text())
-            if self.rfOpenFiles.isChecked():
-                code += ',{0}{1}None'.format(os.linesep, istring)
+            if self.rfOpenFiles.isChecked() or self.__pyqtVariant == 5:
+                if self.eInitialFilter.text() == "":
+                    filter = "None"
+                else:
+                    if self.cInitialFilter.isChecked():
+                        fmt = '{0}'
+                    else:
+                        fmt = 'self.trUtf8("{0}")'
+                    filter = fmt.format(self.eInitialFilter.text())
+                code += ',{0}{1}{2}'.format(os.linesep, istring, filter)
             if not self.cSymlinks.isChecked():
                 code += \
                     ',{0}{1}QFileDialog.Options(' \
@@ -240,7 +382,7 @@
             else:
                 code += 'getSaveFileNameAndFilter({0}{1}'.format(
                     os.linesep, istring)
-            code += 'None,{0}{1}'.format(os.linesep, istring)
+            code += '{0},{1}{2}'.format(parent, os.linesep, istring)
             if not self.eCaption.text():
                 code += '"",{0}{1}'.format(os.linesep, istring)
             else:
@@ -262,8 +404,16 @@
                 else:
                     fmt = 'self.trUtf8("{0}")'
                 code += fmt.format(self.eFilters.text())
-            if self.rfSaveFile.isChecked():
-                code += ',{0}{1}None'.format(os.linesep, istring)
+            if self.rfSaveFile.isChecked() or self.__pyqtVariant == 5:
+                if self.eInitialFilter.text() == "":
+                    filter = "None"
+                else:
+                    if self.cInitialFilter.isChecked():
+                        fmt = '{0}'
+                    else:
+                        fmt = 'self.trUtf8("{0}")'
+                    filter = fmt.format(self.eInitialFilter.text())
+                code += ',{0}{1}{2}'.format(os.linesep, istring, filter)
             if (not self.cSymlinks.isChecked()) or \
                (not self.cConfirmOverwrite.isChecked()):
                 code += ',{0}{1}QFileDialog.Options('.format(
@@ -279,7 +429,7 @@
             code += '){0}'.format(estring)
         elif self.rDirectory.isChecked():
             code += 'getExistingDirectory({0}{1}'.format(os.linesep, istring)
-            code += 'None,{0}{1}'.format(os.linesep, istring)
+            code += '{0},{1}{2}'.format(parent, os.linesep, istring)
             if not self.eCaption.text():
                 code += '"",{0}{1}'.format(os.linesep, istring)
             else:
@@ -303,13 +453,3 @@
             code += ')){0}'.format(estring)
             
         return code
-        
-    def getCode(self, indLevel, indString):
-        """
-        Public method to get the source code.
-        
-        @param indLevel indentation level (int)
-        @param indString string used for indentation (space or tab) (string)
-        @return generated code (string)
-        """
-        return self.__getCode4(indLevel, indString)

eric ide

mercurial