Exemplo n.º 1
0
def kaction(name, actionCollection=None, callable=None):
    # Create the toolbar action and insert it into the toolbar_actions dict
    global parent
    global action_definitions
    global action_groups

    config = action_definitions.get(name, None)

    if config is None:
        print "ERR: No config for action:", name
        return None #Without the config no action can be created

    action = KAction(parent)

    if "group" in config:
        group_name = config["group"]
        if group_name in action_groups:
            group = action_groups[group_name]
        else:
            group = QtGui.QActionGroup(parent)
            action_groups[group_name] = group

        action.setActionGroup(group)

    if "icon" in config:
        action.setIcon(KIcon(config["icon"]))

    if "checkable" in config:
        action.setCheckable(True)

    # Same exception is wanted here. As an alternative one can specify a callable as a parameter.
    if callable is not None:
        action.triggered.connect(callable)
    else:
        action.triggered.connect(config["action"])

    if "text" in config:
        action.setText(config["text"])

    if "tooltip" in config:
        action.setToolTip(config["tooltip"])

    if "whatsthis" in config:
        action.setWhatsThis(config["whatsthis"])

    if actionCollection is not None:
        actionCollection.addAction(name, action)

    return action
Exemplo n.º 2
0
    def setupActions(self):
        """Sets up all actions (signal/slot combinations)."""
        # standard action
        KStandardAction.quit(g_app.quit, self.actionCollection())

        # dictionary actions
        self.dictionaryPage.registerGlobalActions(self.actionCollection())
        # update dictionaries
        if self.updateDialog:
            self.updateAction = self.updateDialog.updateAction(
                self.actionCollection())
            # optimise database
            self.updateDialog.optimiseAction(self.actionCollection())
        else:
            self.updateAction = None

        # search bar
        self.characterCombo = KHistoryComboBox()
        self.characterCombo.setSizePolicy(QSizePolicy.Expanding,
            QSizePolicy.Fixed)
        font = QFont()
        font.setPointSize(13)
        self.characterCombo.setFont(font)
        self.characterCombo.setObjectName("characterCombo")
        self.connect(self.characterCombo, SIGNAL("activated(const QString &)"),
            self.slotCharacterComboActivated)

        comboAction = KAction(self)
        comboAction.setText(i18n("Search bar"))
        comboAction.setShortcut(Qt.Key_F6)
        self.connect(comboAction, SIGNAL("triggered()"),
            self.slotSearchComboActivated)
        comboAction.setDefaultWidget(self.characterCombo)
        comboAction.setWhatsThis(
            i18n("<html>Search bar<br/><br/>Enter character of search string</html>"))
        self.actionCollection().addAction("searchbar", comboAction)

        goUrl = self.actionCollection().addAction("go_search")
        goUrl.setIcon(KIcon("go-jump-locationbar"))
        goUrl.setText(i18n("Go"))
        self.connect(goUrl, SIGNAL("triggered()"),
            lambda: self.slotCharacterComboActivated(
                self.characterCombo.currentText()))
        goUrl.setWhatsThis(
            i18n("<html>Go<br /><br />Searches for the string given in the search bar.</html>"))

        # clear search bar action
        clearLocationAction = KAction(KIcon("edit-clear-locationbar-ltr"),
            i18n("Clear &Location Bar"), self)
        clearLocationAction.setShortcut(Qt.CTRL + Qt.Key_L)
        clearLocationAction.setWhatsThis(
            i18n("Clears the location bar and places the cursor inside"))
        self.actionCollection().addAction("clearlocationbar",
            clearLocationAction)
        self.connect(clearLocationAction, SIGNAL("triggered(bool)"),
            self.characterCombo.clearEditText)
        self.connect(clearLocationAction, SIGNAL("triggered(bool)"),
            self.characterCombo.setFocus)

        # show/hide character page
        self.toggleToolboxAction = KToggleAction(KIcon("view-sidetree"),
            i18n("Show Character Toolbox"), self)
        self.toggleToolboxAction.setShortcut(Qt.Key_F9)
        self.toggleToolboxAction.setWhatsThis(
            i18n("Shows and Hides the character choosing toolbox"))
        self.actionCollection().addAction("showtoolbox",
            self.toggleToolboxAction)
        self.connect(self.toggleToolboxAction, SIGNAL("triggered(bool)"),
            self.slotToggleToolbox)

        # auto-lookup clipboard
        self.autoLookupAction = KToggleAction(i18n("&Auto-Lookup"), self)
        self.autoLookupAction.setToolTip(
            i18n("Automatically look up text selected by the mouse cursor."))
        self.autoLookupAction.setWhatsThis(
            i18n("Automatically look up text selected by the mouse cursor."))
        self.actionCollection().addAction("autolookup", self.autoLookupAction)
        self.connect(self.autoLookupAction, SIGNAL("triggered(bool)"),
            self.setAutoLookup)
        self.autoLookupAction.setIcon(
            QIcon(util.getIcon('auto-lookup-selection.png')))

        self.connect(QApplication.clipboard(), SIGNAL("selectionChanged()"),
            self.slotSelectionChanged)
Exemplo n.º 3
0
class UpdateDialog(KDialog):
    def __init__(self, mainWindow, renderThread, pluginConfig=None):
        KDialog.__init__(self, mainWindow)
        self.renderThread = renderThread

        self.databaseUrl = None
        if pluginConfig:
            self.databaseUrl = util.readConfigString(self.pluginConfig,
                "Update database url", None)

        if not self.databaseUrl:
            self.databaseUrl = unicode('sqlite:///'
                + util.getLocalData('dictionaries.db'))

        self.renderThread.setObject(DictionaryInfo,
            databaseUrl=self.databaseUrl)

        self.setCaption(i18n("Install/Update Dictionaries"))
        self.setButtons(KDialog.ButtonCode(KDialog.Close))
        self.enableButton(KDialog.Cancel, False)

        # TODO can we defer the creation of the update widget until the dialog is shown?
        self.updateWidget = UpdateWidget(mainWindow, renderThread, pluginConfig)
        self.connect(self.updateWidget, SIGNAL("working(bool)"),
            self.slotUpdateWorking)
        self.setMainWidget(self.updateWidget)

        self.connect(self, SIGNAL("finished()"), self.slotFinish)

        self.initialised = False

        self.connect(self.renderThread, SIGNAL("jobFinished"),
            self.contentRendered)
        self.connect(self.renderThread, SIGNAL("jobErrorneous"),
            self.renderingFailed)

        self.actionCollection = KActionCollection(self)
        self.setupActions()

    def showEvent(self, event):
        if not self.initialised:
            self.initialised = True
            self.loadDatabaseBuilder()
            self.updateWidget.setup()

    def setupActions(self):
        # update dictionaries
        self._updateAction = KAction(KIcon('system-software-update'),
            i18n("&Update..."), self)
        self.actionCollection.addAction("updatedictionaries",
            self._updateAction)
        self._updateAction.setWhatsThis(
            i18n("Download and update dictionaries."))
        self.connect(self._updateAction, SIGNAL("triggered(bool)"),
            self.exec_)
        # optimise database
        self._optimiseAction = KAction(KIcon('system-run'),
            i18n("&Optimise database"), self)
        self.actionCollection.addAction("optimisedatabase",
            self._optimiseAction)
        self._optimiseAction.setWhatsThis(
            i18n("Rearranges and optimises the database."))
        self._optimiseAction.setEnabled(True) # TODO
        self.connect(self._optimiseAction, SIGNAL("triggered(bool)"),
            self.slotOptimiseDatabase)

    def updateAction(self, actionCollection):
        actionCollection.addAction(self._updateAction.objectName(),
            self._updateAction)
        return self._updateAction

    def optimiseAction(self, actionCollection):
        actionCollection.addAction(self._optimiseAction.objectName(),
            self._optimiseAction)
        return self._optimiseAction

    def slotUpdateWorking(self, working):
        if working:
            self.setButtons(KDialog.ButtonCode(KDialog.Cancel))
        else:
            self.setButtons(KDialog.ButtonCode(KDialog.Close))

    def slotFinish(self):
        if self.updateWidget.isWorking():
            self.updateWidget.cancel()

    def slotOptimiseDatabase(self):
        self.loadDatabaseBuilder()
        dbBuild = self.renderThread.getObjectInstance(build.DatabaseBuilder)
        if dbBuild.isOptimizable():
            if KMessageBox.warningContinueCancel(self,
                i18n("This operation might take some time."),
                i18n("Optimise Database"), KStandardGuiItem.cont(),
                KStandardGuiItem.cancel(), 'database_optimise') \
                    == KMessageBox.Continue:
                QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
                self.currentJob = self.renderThread.enqueue(
                    build.DatabaseBuilder, 'optimize')

    def loadDatabaseBuilder(self):
        if not self.renderThread.hasObject(build.DatabaseBuilder):
            options = EclectusCommandLineBuilder.getDefaultOptions()

            db = getDBConnector(getDatabaseConfiguration(self.databaseUrl))

            self.renderThread.setObject(build.DatabaseBuilder, dbConnectInst=db,
                **options)

    def contentRendered(self, id, classObject, method, args, param, content):
        if classObject == build.DatabaseBuilder and method == 'optimize':
            QApplication.restoreOverrideCursor()

    def renderingFailed(self, id, classObject, method, args, param, e,
            stacktrace):
        if classObject == build.DatabaseBuilder and method == 'optimize':
            print >>sys.stderr, stacktrace
            QApplication.restoreOverrideCursor()