Exemple #1
0
def hyphenate(text, mainwindow):
    """
    Ask the user which language to use.
    Returns None if the user cancels the dialog or no hyphenation pattern files
    could be found.
    """
    if not hyphdicts:
        KMessageBox.sorry(mainwindow, i18n(
            "Could not find any hyphenation dictionaries.\n\n"
            "Please install a package containing some and/or or configure the "
            "search path to find them in the Frescobaldi settings under "
            "\"Paths.\""))
        return
    
    conf = config("hyphenation")
    lang = conf.readEntry("lastused", "")
    langs = list(sorted(hyphdicts.keys()))
    index = lang in langs and langs.index(lang) or 0
    
    d = KDialog(mainwindow)
    d.setButtons(KDialog.ButtonCode(KDialog.Ok | KDialog.Cancel | KDialog.Help))
    d.setCaption(i18n("Hyphenate Lyrics Text"))
    d.setHelp("lyrics")
    layout = QVBoxLayout()
    d.mainWidget().setLayout(layout)
    layout.addWidget(QLabel(i18n("Please select a language:")))
    listbox = QListWidget()
    layout.addWidget(listbox)
    listbox.addItems(langs)
    listbox.setCurrentRow(index)
    listbox.setFocus()
    if d.exec_():
        lang = langs[listbox.currentRow()]
        conf.writeEntry("lastused", lang)
        conf.sync()
        # get hyphenator
        h = Hyphenator(hyphdicts[lang])
        return ly.rx.lyric_word.sub(lambda m: h.inserted(m.group(), ' -- '), text)
Exemple #2
0
class RunLilyPondDialog(KDialog):
    """
    A dialog where a DocumentJob can be configured before it's started.
    """
    def __init__(self, mainwin):
        self.mainwin = mainwin
        KDialog.__init__(self, mainwin)
        self.setCaption(i18n("Run LilyPond"))
        self.setButtons(KDialog.ButtonCode(
            KDialog.Help | KDialog.Ok | KDialog.Cancel ))
        self.setButtonText(KDialog.Ok, i18n("Run LilyPond"))
        self.setButtonIcon(KDialog.Ok, KIcon("run-lilypond"))
        self.setHelp("running")
        
        layout = QVBoxLayout(self.mainWidget())
        
        layout.addWidget(QLabel(i18n(
            "Select which LilyPond version you want to run:")))
            
        self.lilypond = QListWidget()
        self.lilypond.setIconSize(QSize(22, 22))
        self.lilypond.setSpacing(4)
        layout.addWidget(self.lilypond)
        
        self.preview = QCheckBox(i18n(
            "Run LilyPond in preview mode (with Point and Click)"))
        layout.addWidget(self.preview)
        
        self.verbose = QCheckBox(i18n("Run LilyPond with verbose output"))
        layout.addWidget(self.verbose)
        
    def configureJob(self, job, doc=None):
        """Configure a job, belonging to document.
        
        If the document is not given, it is expected to live in the document
        attribute of the job. If there is already a job running, we just display,
        but disable the Run button, until the old job finishes.
        """
        doc = doc or job.document
        
        # populate the dialog based on remembered settings for this document
        self.lilypond.clear()
        
        # find the configured lilypond versions
        conf = config("lilypond")
        paths = conf.readEntry("paths", ["lilypond"]) or ["lilypond"]
        default = conf.readEntry("default", "lilypond")
        
        import ly.version
        
        # get all versions
        ver = dict((path, lilyPondVersion(path)) for path in paths)
        
        # default
        if default not in paths:
            default = paths[0]
            
        # Sort on version
        paths.sort(key=ver.get)
        versions = [format(ver.get(p)) for p in paths]
        
        # Determine automatic version (lowest possible)
        autopath = None
        docVersion = doc.lilyPondVersion()
        if docVersion:
            autopath = automaticLilyPondCommand(docVersion)
        
        def addItem(version, path, icon, title, tooltip):
            item = QListWidgetItem(self.lilypond)
            item.setIcon(KIcon(icon))
            item.setText("{0}\n{1}: {2}".format(title, i18n("Command"), path))
            item.setToolTip(tooltip)
            version or item.setFlags(Qt.NoItemFlags)
        
        # Add all available LilyPond versions:
        for path in paths:
            if ver[path]:
                title = i18n("LilyPond %1", format(ver[path]))
                tooltip = i18n("Use LilyPond version %1", format(ver[path]))
                addenda, tips = [], []
                if path == default:
                    addenda.append(i18n("default"))
                    tips.append(i18n("Default LilyPond Version."))
                if path == autopath:
                    addenda.append(i18n("automatic"))
                    tips.append(i18n("Automatic LilyPond Version (determined from document)."))
                if addenda:
                    title += " [{0}]".format(", ".join(addenda))
                    tooltip += "\n{0}".format("\n".join(tips))
                addItem(format(ver[path]), path, "run-lilypond", title,
                    tooltip + "\n" + i18n("Path: %1",
                        ly.version.LilyPondInstance(path).command() or path))
            else:
                addItem("", path, "dialog-error",
                    i18n("LilyPond (version unknown)"),
                    i18n("Use LilyPond (version unknown)\nPath: %1",
                        ly.version.LilyPondInstance(path).command() or path))
        
        # Copy the settings from the document:
        self.preview.setChecked(doc.metainfo["custom preview"])
        self.verbose.setChecked(doc.metainfo["custom verbose"])
        
        try:
            self.lilypond.setCurrentRow(versions.index(
                doc.metainfo["custom lilypond version"]))
        except ValueError:
            cmd = autopath if autopath and conf.readEntry("automatic version",
                False) else default
            self.lilypond.setCurrentRow(paths.index(cmd))
            
        # Focus our listbox:
        self.lilypond.setFocus()
        
        # Disable the Run button if a job is running for this document
        oldjob = self.mainwin.jobManager().job(doc)
        self.enableButtonOk(not oldjob)
        if oldjob:
            enable = lambda: self.enableButtonOk(True)
            oldjob.done.connect(enable)
        
        # Wait for user interaction:
        result = self.exec_()
        
        # If a job was running, don't listen to it anymore
        if oldjob:
            oldjob.done.disconnect(enable)
        
        if not result:
            return False # cancelled
        
        # Save the settings in the document's metainfo and configure job:
        doc.metainfo["custom preview"] = job.preview = self.preview.isChecked()
        doc.metainfo["custom verbose"] = job.verbose = self.verbose.isChecked()
        index = self.lilypond.currentRow()
        doc.metainfo["custom lilypond version"] = versions[index]
        job.command = paths[index]
        return True