Ejemplo n.º 1
0
class Preferences():
    """
    Store preferences locally for easy programmatic access.

    Persistant copy of preferences will be stored in a file and loaded upon startup.
    User can modify preferences by opening UI, editing preferences, and saving them.
    By saving the preferences, the object will be updated to reflect the UI, and the
    file will also be updated.
    """

    # These are the defaults for each preference. They will be overwritten
    # by the user's preferences at startup.
    prefs = {
            'projectDirectory': Property.String('~/'),   # Dir for where to store project files
            'defaultDirectory': Property.String('~/'),   # Dir for other purposes
            'textOptions':      Property.TextOptions(),  # Default text options
            }

    def __init__(self, fileName):
        self._app = QApplication.instance().window
        self.preferencesFile = os.path.expanduser(fileName)

        self._widget = QWidget()
        self._ui = Ui_Preferences()
        self._ui.setupUi(self._widget)

        # Load preferences from file
        self.loadPreferencesFile()
        self.resetUi()
        
        # Connect button signals
        self._ui.buttons.button(QDialogButtonBox.Reset).clicked.connect(self.resetUi)
        self._ui.buttons.button(QDialogButtonBox.Save).clicked.connect(self.savePreferences)
        self._ui.buttons.button(QDialogButtonBox.Cancel).clicked.connect(self.hideDialog)
        self._ui.defaultDirectoryButton.clicked.connect(self.defaultDirectorySelector)
        self._ui.projectDirectoryButton.clicked.connect(self.projectDirectorySelector)
        #self._ui.textOptions.clicked.connect(self._ui.textOptions.showTextOptionsDialog)

        self._window = SubWindow(self._app.ui.workspace)
        self._window.setWidget(self._widget)
        self._widget.setParent(self._window)

        self._window.hide()


    def getInternal(self, variable):
        try:
            return self.prefs[variable].get()
        except:
            return None

    def getUi(self, variable):
        return Util.getWidgetValue(vars(self._ui)[variable])

    def setInternal(self, variable, value):
        return self.prefs[variable].set(value)
    
    def setUi(self, variable, value):
        return Util.setWidgetValue(vars(self._ui)[variable], value)

    def showDialog(self):
        self.resetUi()
        self._window.show()

    def hideDialog(self):
        self.resetUi()
        self._window.hide()

    def loadPreferencesFile(self):
        if os.path.isfile(self.preferencesFile):
            with open(self.preferencesFile, 'r') as fileHandle:
                self.prefs = pickle.load(fileHandle)
            
    def uiToInternal(self):
        """
        Copy the UI pref values to the internal prefs dict.
        """
        for pref in self.prefs.keys():
            self.setInternal(pref, self.getUi(pref))

    def resetUi(self):
        """
        Update UI to the current internal cache of preferences.
        """

        for pref in self.prefs.keys():
            self.setUi(pref, self.getInternal(pref))

    def savePreferences(self):
        """
        Save preferences to file and then read file to internal cache.
        Close dialog afterwards.
        """
        
        self.uiToInternal()
        self.writePreferences()
        self.loadPreferencesFile()
        self.hideDialog()

    def writePreferences(self):
        if os.path.isfile(self.preferencesFile) or not os.path.exists(self.preferencesFile):
            with open(self.preferencesFile, 'wb') as fileHandle:
                pickle.dump(self.prefs, fileHandle)



    # Methods for specific preferences
    def defaultDirectorySelector(self):
        directory = QFileDialog.getExistingDirectory(self._app.ui.workspace, "Select Default Directory", Util.getWidgetValue(self._ui.defaultDirectory))
        if os.path.isdir(directory):
            return self.setUi('defaultDirectory', directory)
        return False

    def projectDirectorySelector(self):
        directory = QFileDialog.getExistingDirectory(self._app.ui.workspace, "Select Project Directory", Util.getWidgetValue(self._ui.projectDirectory))
        if os.path.isdir(directory):
            return self.setUi('projectDirectory', directory)
        return False
Ejemplo n.º 2
0
class QTextOptionsButton(QPushButton):
    """A push button that is used to select a font and text options."""

    def __init__(self, *args):
        QPushButton.__init__(self, *args)
        self._app = QApplication.instance().window

        self.textOptions = Property.TextOptions()

        # Set initial textOptions values based on application preferences
        if 'preferences' in self._app.__dict__.keys():
            preferenceTextOptions = self._app.preferences.getInternal('textOptions')
            if preferenceTextOptions is not None:
                self.setTextOptions(preferenceTextOptions)

        # Create dialog
        self._dialog = QTextOptionsWidget(self)
        self._ui = Ui_TextOptionsWidget()
        self._ui.setupUi(self._dialog)
        self._dialog.setUiObject(self._ui)

        self._window = SubWindow(self._app.ui.workspace)
        self._window.setWidget(self._dialog)
        self._dialog.setParent(self._window)
        
        self.hideTextOptionsWidget()
        
        # Create font list from matplotlib fonts
        fontPropList = fm.createFontList(fm.findSystemFonts())
        
        # Get just the names
        fontList = map(lambda x:x.name, fontPropList)
        
        # Unique and sort the names
        fontList = list(set(fontList))
        fontList.sort()
        
        # Enter fonts into the ui widget
        self._ui.name.addItems(fontList)
        
    def getOption(self, option):
        return self.textOptions.get()[option]

    def getTextOptions(self):
        return Property.TextOptions(self.textOptions)

    def setTextOptions(self, textOptions={}):
        """
        Set text options.
        """
        self.textOptions.set(textOptions)

    def showTextOptionsWidget(self):
        """
        Create a dialog box to select a font and text options.
        """
        
        self._dialog.resetUi()
        self._window.show()
    
    def hideTextOptionsWidget(self):
        self._window.hide()