This forum has been archived. All content is frozen. Please use KDE Discuss instead.

QML/Plasmoid How I can add global shortcuts?

Tags: plasmoid, shortcuts plasmoid, shortcuts plasmoid, shortcuts
(comma "," separated)
User avatar
smithar
Registered Member
Posts
3
Karma
0
OS
Greetings, this is my first post.

My problem is that I want to add various global shortcuts to my plasmoid written in QML, and also a configuration UI KShorcutsEditor in order to configure my shortcuts to my liking.

Try it with KCM module written in C++ and export the plasmoid as a [X-Plasma-ConfigPlugins] but did not work as wanted. Because global shortcuts only work if open UI settings, but when I close it no longer work. :(

My question is how it's done for global shortcuts to work even KCM module is closed?.

Thanks for your help, and sorry for my ugly English.

Code: Select all
#include "kcm_playbarshortcuts.h"

//KDE
#include <KPluginFactory>
#include <KPluginLoader>
#include <KAboutData>
#include <KAction>
#include <KIcon>

//Qt
#include <QBoxLayout>

K_PLUGIN_FACTORY(PlayBarShortcutsFactory, registerPlugin<KCM_PlayBarShortcuts>();)

K_EXPORT_PLUGIN(PlayBarShortcutsFactory(
                    "kcm_playbarshortcuts" /* kcm name */,
                    "kcm_playbarshortcuts" /* catalog name */)
               )

KCM_PlayBarShortcuts::KCM_PlayBarShortcuts(QWidget*& parent, const QVariantList& args)
    : KCModule(PlayBarShortcutsFactory::componentData(), parent, args)
{
    KAboutData about("simple",
                     0,
                     ki18n("KCM_PlayBarShortcuts"), "0.6",
                     ki18n("Configure PlayBar global shortcuts"),
                     KAboutData::License_GPL,
                     ki18n("(C) 2014 Smith AR"),
                     KLocalizedString(),
                     0,
                     "audoban@openmailbox.org");

    about.addAuthor(ki18n("Smith AR"), KLocalizedString(), "mail@example.com");

    //Layout
    QVBoxLayout* vBox = new QVBoxLayout();

    shortcutsWidget = new KShortcutsEditor(0, KShortcutsEditor::GlobalAction);
    shortcutsWidget->setMinimumSize(200, 200);

    this->setLayoutDirection(Qt::LayoutDirectionAuto);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->setLayout(vBox);

    //create action collection
    createMediaActions(shortcutsWidget);
    shortcutsWidget->addCollection(actions, "PlayBar");

    vBox->addWidget(shortcutsWidget);
}

KCM_PlayBarShortcuts::~KCM_PlayBarShortcuts()
{

}

void KCM_PlayBarShortcuts::createMediaActions(QWidget* parent)
{
    actions = new KActionCollection(parent);
    actions->setConfigGlobal(true);

    KAction* previous = createAction("media-skip-backward", "Previous", Qt::Key_MediaPrevious, actions);

    KAction* pause = createAction("media-playback-pause", "Pause", Qt::Key_MediaPause, actions);

    KAction* play = createAction("media-playback-start", "Play", Qt::Key_MediaPlay, actions);

    KAction* stop = createAction("media-playback-stop", "Stop", Qt::Key_MediaStop, actions);

    KAction* play_pause = createAction("media-playback-start", "PlayPause", Qt::Key_MediaTogglePlayPause, actions);

    KAction* next = createAction("media-skip-forward", "Next", Qt::Key_MediaNext, actions);

    actions->addAction("Play", play);
    actions->addAction("Pause", pause);
    actions->addAction("PlayPause", play_pause);
    actions->addAction("Stop", stop);
    actions->addAction("Previous", previous);
    actions->addAction("Next", next);
    actions->setConfigGlobal(true);

    service = new MprisService(parent);

    connect(actions, SIGNAL(actionTriggered(QAction*)), this, SLOT(actionTriggered(QAction*)));

}

KAction* KCM_PlayBarShortcuts::createAction(const char* icon, const char* name, Qt::Key key, QObject* parent)
{
    KAction* action = new KAction(KIcon(icon), ki18n(name).toString(), parent);
    action->setObjectName(name);
    action->setGlobalShortcut(KShortcut(key));

    return action;
}

//SLOT
void KCM_PlayBarShortcuts::actionTriggered(QAction* action)
{
    service = new MprisService(0);
    service->startOperation(action->objectName());
}
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
While i'm not familiar with how shortcut configuration is handled in other applications, my guess would be you need to have the KAction and KActionCollection instances exist for the lifetime of your applet. Also make sure you handle the import and export configuration actions - otherwise the changes won't be preserved I suspect.

See http://lxr.kde.org/source/kde/applicati ... temain.cpp for instance. actionCollection() is a function provided by the KParts class KWrite derives from - if you create your action collection at the applet level and then make it accessible to the control module you should be fine.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
smithar
Registered Member
Posts
3
Karma
0
OS
bcooksley wrote: if you create your action collection at the applet level and then make it accessible to the control module you should be fine.


It was what I suspected from the beginning, but then now I have a question more, as do a global action in QML?. I have read many times the documentation for QML and Plasma Runtime, although I create these shortcuts are not global. See it http://qt-project.org/doc/qt-4.8/qdecla ... l#qmlfocus

In Python for example is easy because I have access to almost all KDE API (action collection). But it's not the same with QML, for example I can not use the KAction Class.

Not if I'm right, but I can not create global shortcuts for a Plasmoid written in QML 1.1.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
To my knowledge, QAction has no support for interacting with the KDE infrastructure necessary to support global shortcuts. I'd suggest verifying this through plasma-devel@kde.org - however I suspect it is intended that applet's using the Plasma JS / QML interfaces are not allowed to create global shortcuts.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
smithar
Registered Member
Posts
3
Karma
0
OS
Thanks a lot. Already wrote to the mailing list plasma-devel, but still do not give response. For now I have solved my problem through a Plasma Engine, it may not be the most elegant solution but it works. :/ (Of course with limitations).


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]