Ejemplo n.º 1
0
class LilyDocBrowser(SettingsGroup):
    def __init__(self, page):
        super(LilyDocBrowser, self).__init__(i18n("LilyPond Documentation"), page)

        layout = QVBoxLayout(self)
        layout.setSpacing(0)

        h = KHBox()
        l = QLabel(i18n("Url:"), h)
        self.lilydoc = KUrlRequester(h)
        self.lilydoc.textChanged.connect(page.changed)
        l.setBuddy(self.lilydoc)
        tooltip = i18n("Url or path to the LilyPond documentation.")
        l.setToolTip(tooltip)
        self.lilydoc.setToolTip(tooltip)
        self.lilydoc.fileDialog().setCaption(i18n("LilyPond Documentation"))
        layout.addWidget(h)
        self.lilydoc.setMode(KFile.Mode(KFile.File | KFile.Directory | KFile.ExistingOnly))

    def defaults(self):
        self.lilydoc.setUrl(KUrl())

    def loadSettings(self):
        self.lilydoc.setUrl(KUrl(config("preferences").readEntry("lilypond documentation", "")))

    def saveSettings(self):
        config("preferences").writeEntry("lilypond documentation", self.lilydoc.url().url())

    def applySettings(self):
        lilydoc = self.page.dialog.mainwin.tools.get("lilydoc")
        if lilydoc:
            lilydoc.newDocFinder()
Ejemplo n.º 2
0
class EditorDialog(KPageDialog):
    """A dialog to edit properties of a session.
    
    You can subclass this to add more settings to a session config dialog.
    Add more pages in the __init__() method, and inherit the validate() method
    to check the input if necessary. Implement the load and save methods to
    load and save the settings.
    
    """
    def __init__(self, manager):
        super(EditorDialog, self).__init__(manager.mainwin)
        self.mainwin = manager.mainwin
        self.sm = manager
        self.setButtons(KDialog.ButtonCode(
            KDialog.Help | KDialog.Ok | KDialog.Cancel))
        self.setFaceType(KPageDialog.List)
        self.setHelp("sessions")
        
        # First page with name and auto-save option
        page = QWidget(self)
        item = self.firstPage = self.addPage(page, i18n("Session"))
        item.setHeader(i18n("Properties of this session"))
        item.setIcon(KIcon("configure"))
        
        layout = QGridLayout(page)
        
        l = QLabel(i18n("Name:"))
        self.name = QLineEdit()
        l.setBuddy(self.name)
        layout.addWidget(l, 0, 0)
        layout.addWidget(self.name, 0, 1)
        
        self.autosave = QCheckBox(i18n(
            "Always save the list of documents in this session"))
        layout.addWidget(self.autosave, 1, 1)
        
        l = QLabel(i18n("Base directory:"))
        self.basedir = KUrlRequester()
        self.basedir.setMode(KFile.Mode(
            KFile.Directory | KFile.ExistingOnly | KFile.LocalOnly))
        l.setBuddy(self.basedir)
        layout.addWidget(l, 2, 0)
        layout.addWidget(self.basedir, 2, 1)
        
    def edit(self, name=None):
        """Edit the named or new (if not given) session."""
        # load the session
        self._originalName = name
        if name:
            self.setCaption(i18n("Edit session: %1", name))
            self.name.setText(name)
            conf = self.sm.config(name)
            self.autosave.setChecked(conf.readEntry("autosave", True))
            self.basedir.setUrl(KUrl(conf.readPathEntry("basedir", "")))
            self.loadSessionConfig(conf)
        else:
            self.setCaption(i18n("Edit new session"))
            self.name.clear()
            self.name.setFocus()
            self.autosave.setChecked(True)
            self.basedir.setUrl(KUrl())
            self.loadSessionDefaults()
        self.setCurrentPage(self.firstPage)
        if self.exec_():
            # save
            name = self.name.text()
            if self._originalName and name != self._originalName:
                self.sm.renameSession(self._originalName, name)
            conf = self.sm.config(name)
            conf.writeEntry("autosave", self.autosave.isChecked())
            conf.writePathEntry("basedir", self.basedir.url().path())
            self.saveSessionConfig(conf)
            return name

    def done(self, result):
        if not result or self.validate():
            super(EditorDialog, self).done(result)
        
    def validate(self):
        """Checks if the input is acceptable.
        
        If this method returns True, the dialog is accepted when OK is clicked.
        Otherwise a messagebox could be displayed, and the dialog will remain
        visible.
        """
        # strip off whitespace
        name = self.name.text().strip()
        self.name.setText(name)
        
        if not name:
            self.setCurrentPage(self.firstPage)
            self.name.setFocus()
            KMessageBox.error(self, i18n("Please enter a session name."))
            if self._originalName:
                self.name.setText(self._originalName)
            return False
        
        if name == 'none':
            self.setCurrentPage(self.firstPage)
            self.name.setFocus()
            KMessageBox.error(self, i18n(
                "Please do not use the name '%1'.", "none"))
            return False
        
        if '&' in name:
            self.setCurrentPage(self.firstPage)
            self.name.setFocus()
            KMessageBox.error(self, i18n(
                "Please do not use the ampersand (&) character "
                "in a session name."))
            return False
            
        if self._originalName != name and name in self.sm.names():
            self.setCurrentPage(self.firstPage)
            self.name.setFocus()
            if KMessageBox.warningContinueCancel(self, i18n(
                "Another session with the name %1 exists already.\n\n"
                "Do you want to overwrite it?", name), None,
                KStandardGuiItem.overwrite(), KStandardGuiItem.cancel(),
                "session_overwrite") == KMessageBox.Cancel:
                return False
            
        return True
    
    def loadSessionDefaults(self):
        """Implement to set defaults for your new session."""
        pass
    
    def loadSessionConfig(self, conf):
        """Implement to load settings from the config group for this session."""
        pass
    
    def saveSessionConfig(self, conf):
        """Implement to save settings to the config group for this session."""
        pass