Exemple #1
0
class XViewProfileToolBar(XToolBar):
    profileCreated = qt.Signal(qt.PyObject)
    profileChanged = qt.Signal(qt.PyObject)
    profileRemoved = qt.Signal(qt.PyObject)
    currentProfileChanged = qt.Signal(qt.PyObject)

    def __init__(self, parent):
        super(XViewProfileToolBar, self).__init__(parent)

        # create custom properties
        self._editingEnabled = True
        self._viewWidget = None
        self._profileGroup = QActionGroup(self)

        # set the default options
        self.setIconSize(QSize(48, 48))
        self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.setContextMenuPolicy(Qt.CustomContextMenu)

        # create connections
        self.actionTriggered.connect(self.handleActionTrigger)
        self.customContextMenuRequested.connect(self.showProfileMenu)

    def addProfile(self, profile):
        """
        Adds the inputed profile as an action to the toolbar.
        
        :param      profile | <projexui.widgets.xviewwidget.XViewProfile>
        """
        act = XViewProfileAction(profile, self)
        self._profileGroup.addAction(act)
        self.addAction(act)
        return act

    def currentProfile(self):
        """
        Returns the current profile for this toolbar.
        
        :return     <projexui.widgets.xviewwidget.XViewProfile> || None
        """
        act = self._profileGroup.checkedAction()
        if (act):
            return act.profile()
        return None

    def createProfile(self, profile=None, clearLayout=True):
        """
        Prompts the user to create a new profile.
        """
        if (profile):
            prof = profile
        elif (not self.viewWidget() or clearLayout):
            prof = XViewProfile()
        else:
            prof = self.viewWidget().saveProfile()

        blocked = self.signalsBlocked()
        self.blockSignals(False)
        changed = self.editProfile(prof)
        self.blockSignals(blocked)

        if (not changed):
            return

        act = self.addProfile(prof)
        act.setChecked(True)

        # update the interface
        if (self.viewWidget() and (profile or clearLayout)):
            self.viewWidget().restoreProfile(prof)

        if (not self.signalsBlocked()):
            self.profileCreated.emit(prof)

    @qt.Slot(qt.PyObject)
    def editProfile(self, profile):
        """
        Prompts the user to edit the given profile.
        
        :param      profile | <projexui.widgets.xviewwidget.XViewProfile>
        """
        mod = XViewProfileDialog.edit(self, profile)
        if (not mod):
            return False

        # update the action interface
        for act in self._profileGroup.actions():
            if (act.profile() == profile):
                act.setProfile(profile)
                break

        # signal the change
        if (not self.signalsBlocked()):
            self.profileChanged.emit(profile)

        return True

    def exportProfiles(self, filename=None):
        """
        Exports this toolbar to the given filename.
        
        :param      filename | <str> || None
        """
        if (not filename):
            filename = QFileDialog.getSaveFileName(self, 'Export Toolbar', '',
                                                   'Toolbar Files (*.xtool)')

        if (not filename):
            return False

        profile_xml = self.toXml()

        projex.text.xmlindent(profile_xml)
        profile_string = ElementTree.tostring(profile_xml)

        f = open(str(filename), 'w')
        f.write(profile_string)
        f.close()

        return True

    def handleActionTrigger(self, action):
        """
        Handles when an action has been triggered.  If the inputed action is a 
        XViewProfileAction, then the currentProfileChanged signal will emit.
        
        :param      action | <QAction>
        """
        # trigger a particular profile
        if (isinstance(action, XViewProfileAction)):
            if (not self.signalsBlocked()):
                self.currentProfileChanged.emit(action.profile())

            if (self._viewWidget):
                self._viewWidget.restoreProfile(action.profile())

    def importProfiles(self, filename=None):
        """
        Imports the profiles from the given filename.
        
        :param      filename | <str> || None
        """
        if (not filename):
            filename = QFileDialog.getOpenFileName(self, 'Import Toolbar', '',
                                                   'Toolbar Files (*.xtool)')

            if type(filename) == tuple:
                filename = str(filename[0])

        if (not (filename and os.path.exists(filename))):
            return False

        f = open(str(filename), 'r')
        profile_string = f.read()
        f.close()

        self.loadString(profile_string)

        # load the default toolbar
        action = self._profileGroup.checkedAction()
        if (action):
            self.handleActionTrigger(action)

    def isEditingEnabled(self):
        """
        Sets whether or not the create is enabled for this toolbar.
        
        :return     <bool>
        """
        return self._editingEnabled

    def isEmpty(self):
        """
        Returns whether or not this toolbar is empty.
        
        :return     <bool>
        """
        return len(self._profileGroup.actions()) == 0

    def loadString(self, profilestr):
        """
        Loads the information for this toolbar from the inputed string.
        
        :param      profilestr | <str>
        """
        try:
            xtoolbar = ElementTree.fromstring(str(profilestr))
        except ExpatError, e:
            return

        self.clear()
        curr = xtoolbar.get('current')

        for xprofile in xtoolbar:
            prof = XViewProfile.fromXml(xprofile)
            act = self.addProfile(prof)
            if (prof.name() == curr):
                act.setChecked(True)
Exemple #2
0
class XViewProfileToolBar(XToolBar):
    profileCreated = Signal(PyObject)
    profileChanged = Signal(PyObject)
    profileRemoved = Signal(PyObject)
    profilesChanged = Signal()
    currentProfileChanged = Signal(PyObject)
    loadProfileFinished = Signal(PyObject)
    newWindowProfileRequested = Signal(PyObject)

    def __init__(self, parent):
        super(XViewProfileToolBar, self).__init__(parent)

        # create custom properties
        self._editingEnabled = True
        self._viewWidget = None
        self._profileText = 'Profile'
        self._profileGroup = QActionGroup(self)
        self._currentProfile = None

        # set the default options
        self.setIconSize(QSize(48, 48))
        self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.setContextMenuPolicy(Qt.CustomContextMenu)

        # create connections
        self.actionTriggered.connect(self.handleActionTrigger)
        self.customContextMenuRequested.connect(self.showProfileMenu)

    def addProfile(self, profile):
        """
        Adds the inputed profile as an action to the toolbar.
        
        :param      profile | <projexui.widgets.xviewwidget.XViewProfile>
        """
        # use the existing version
        for exist in self.profiles():
            if exist.name() == profile.name():
                if exist.version() < profile.version():
                    exist.setProfile(profile)

                return

        # otherwise create a new profile
        act = XViewProfileAction(profile, self)
        self._profileGroup.addAction(act)
        self.addAction(act)
        return act

    def clearActive(self):
        # clear the GUI
        self.blockSignals(True)
        for act in self.actions():
            act.blockSignals(True)
            act.setChecked(False)
            act.blockSignals(False)
        self.blockSignals(False)

        self._currentProfile = None

        # reset the layout
        widget = self.viewWidget()
        if self.sender() != widget:
            widget.reset(force=True)
            widget.setLocked(False)

        self.currentProfileChanged.emit(None)

    def currentProfile(self):
        """
        Returns the current profile for this toolbar.
        
        :return     <projexui.widgets.xviewwidget.XViewProfile> || None
        """
        return self._currentProfile

    def createProfile(self, profile=None, clearLayout=True):
        """
        Prompts the user to create a new profile.
        """
        if profile:
            prof = profile
        elif not self.viewWidget() or clearLayout:
            prof = XViewProfile()
        else:
            prof = self.viewWidget().saveProfile()

        blocked = self.signalsBlocked()
        self.blockSignals(False)
        changed = self.editProfile(prof)
        self.blockSignals(blocked)

        if not changed:
            return

        act = self.addProfile(prof)
        act.setChecked(True)

        # update the interface
        if self.viewWidget() and (profile or clearLayout):
            self.viewWidget().restoreProfile(prof)

        if not self.signalsBlocked():
            self.profileCreated.emit(prof)
            self.profilesChanged.emit()

    @Slot(PyObject)
    def editProfile(self, profile):
        """
        Prompts the user to edit the given profile.
        
        :param      profile | <projexui.widgets.xviewwidget.XViewProfile>
        """
        mod = XViewProfileDialog.edit(self.window(), profile)
        if not mod:
            return False

        # update the action interface
        for act in self._profileGroup.actions():
            if act.profile() == profile:
                act.setProfile(profile)
                break

        # signal the change
        if not self.signalsBlocked():
            self.profileChanged.emit(profile)
            self.profilesChanged.emit()

        return True

    def exportProfile(self, profile, filename=None):
        """
        Exports this toolbar to the given filename.
        
        :param      profile  | <XViewProfile>
                    filename | <str> || None
        """
        if not filename:
            filename = QFileDialog.getSaveFileName(self, 'Export Profile', '',
                                                   'XML Files (*.xml)')
            if type(filename) == tuple:
                filename = nativestring(filename[0])

        if not filename:
            return False

        profile.save(filename)
        return True

    def handleActionTrigger(self, action):
        """
        Handles when an action has been triggered.  If the inputed action is a 
        XViewProfileAction, then the currentProfileChanged signal will emit.
        
        :param      action | <QAction>
        """
        # trigger a particular profile
        if isinstance(action, XViewProfileAction):
            # if the user CTRL+Clicks on the action, then attempt
            # to load it in a new window
            if QApplication.keyboardModifiers() == Qt.ControlModifier:
                self.newWindowProfileRequested.emit(action.profile())
                self.setCurrentProfile(self.currentProfile())
                return
            else:
                self.setCurrentProfile(action.profile())

    def importProfile(self, filename=None):
        """
        Imports the profiles from the given filename.
        
        :param      filename | <str> || None
        """
        if not filename:
            filename = QFileDialog.getOpenFileName(self, 'Import Perspective',
                                                   '', 'XML Files (*.xml)')

            if type(filename) == tuple:
                filename = nativestring(filename[0])

        if not (filename and os.path.exists(filename)):
            return False

        prof = XViewProfile.load(filename)
        if prof:
            self.addProfile(prof)

    def isEditingEnabled(self):
        """
        Sets whether or not the create is enabled for this toolbar.
        
        :return     <bool>
        """
        return self._editingEnabled

    def isEmpty(self):
        """
        Returns whether or not this toolbar is empty.
        
        :return     <bool>
        """
        return len(self._profileGroup.actions()) == 0

    def loadString(self, profilestr, merge=False, loadProfile=True):
        """
        Loads the information for this toolbar from the inputed string.
        
        :param      profilestr | <str>
        """
        try:
            xtoolbar = ElementTree.fromstring(nativestring(profilestr))
        except ExpatError, e:
            return

        if not merge:
            self.clear()

        self.blockSignals(True)
        for xprofile in xtoolbar:
            prof = XViewProfile.fromXml(xprofile)

            if merge:
                self.removeProfile(prof.name(), silent=True)

            self.addProfile(prof)

        self.setCurrentProfile(xtoolbar.get('current'))

        self.blockSignals(False)
        self.profilesChanged.emit()