diff options
author | Ken Moore <moorekou@gmail.com> | 2016-06-29 17:28:54 -0400 |
---|---|---|
committer | Ken Moore <moorekou@gmail.com> | 2016-06-29 17:28:54 -0400 |
commit | 1fc459836734c564aec5e92f9f4adaa29aa8178f (patch) | |
tree | 4846db7898613a425872ba49cd152585731be3b1 /src-qt5/core/lumina-desktop/JsonMenu.h | |
parent | A minor tweak to the "Glass" theme. (diff) | |
download | lumina-1fc459836734c564aec5e92f9f4adaa29aa8178f.tar.gz lumina-1fc459836734c564aec5e92f9f4adaa29aa8178f.tar.bz2 lumina-1fc459836734c564aec5e92f9f4adaa29aa8178f.zip |
Add a new type of menu plugin: jsonmenu
This is a recursive, auto-generating menu which runs an external utility (a script of some kind usually), which generates a JSON document/object which is used to populate the menu.
Syntax:
(Per object)
{
"type" : "item",
"icon" : "icon name (optional)",
"action" : "something lumina-open can run (optional)"
}
Or for a recursive menu generation
{
"type" : "jsonmenu",
"exec" : "some command to run to populate menu",
"icon" : "icon name (optional)"
}
Example for a full return:
{
"Item1" : {
"type" : "item",
"icon" : "folder",
"action" : "~/item1.jpg"
},
"Menu1" : {
"type" : "jsonmenu",
"exec" : "some script",
"icon" : "system-run"
}
}
Item1 will open ~/item1.jpg with lumina-open when clicked, while Menu1 will call "some script" to generate a new menu with additional options.
}
Diffstat (limited to 'src-qt5/core/lumina-desktop/JsonMenu.h')
-rw-r--r-- | src-qt5/core/lumina-desktop/JsonMenu.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src-qt5/core/lumina-desktop/JsonMenu.h b/src-qt5/core/lumina-desktop/JsonMenu.h new file mode 100644 index 00000000..fbb80d28 --- /dev/null +++ b/src-qt5/core/lumina-desktop/JsonMenu.h @@ -0,0 +1,68 @@ +//=========================================== +// Lumina Desktop source code +// Copyright (c) 2016, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +// This menu is used to automatically generate menu contents +// based on the JSON output of an external script/utility +//=========================================== +#ifndef _LUMINA_DESKTOP_JSON_MENU_H +#define _LUMINA_DESKTOP_JSON_MENU_H + +#include <QMenu> +#include <QString> +#include <QJsonDocument> +#include <QJsonObject> +#include <QJsonArray> + +#include <LuminaUtils.h> +#include <LuminaXDG.h> + +class JsonMenu : public QMenu{ + Q_OBJECT +private: + QString exec; + +public: + JsonMenu(QString execpath, QWidget *parent = 0) : QMenu(parent){ + exec = execpath; + connect(this, SIGNAL(aboutToShow()), this, SLOT(updateMenu()) ); + } + +private slots: + void parseObject(QString label, QJsonObject obj){ + if( label.isEmpty() || !obj.contains("type") ){ return; } + QString type = obj.value("type").toString(); + if(type.toLower()=="item"){ + QAction *act = this->addAction(label); + if(obj.contains("icon")){ act->setIcon( LXDG::findIcon(obj.value("icon").toString(),"") ); } + if(obj.contains("action")){ act->setWhatsThis( obj.value("action").toString() ); } + else{ act->setEnabled(false); } //not interactive + }else if(type.toLower()=="menu"){ + + }else if(type.toLower()=="jsonmenu"){ + //This is a recursive JSON menu object + if(!obj.contains("exec")){ return; } + JsonMenu *menu = new JsonMenu(obj.value("exec").toString(), this); + menu->setTitle(label); + if(obj.contains("icon")){ menu->setIcon(LXDG::findIcon(obj.value("icon").toString(),"") ); } + this->addMenu(menu); + } + } + void updateMenu(){ + this->clear(); + QJsonDocument doc = QJsonDocument::fromJson( LUtils::getCmdOutput(exec).join(" ").toLocal8Bit() ); + if(doc.isNull() || !doc.isObject()){ + this->addAction( QString(tr("Error parsing script output: %1")).arg("\n"+exec) )->setEnabled(false); + }else{ + QStringList keys = doc.object().keys(); + for(int i=0; i<keys.length(); i++){ + if(doc.object().value(keys[i]).isObject()){ + parseObject(keys[i], doc.object().value(keys[i]).toObject()); + } + } + } + } +}; +#endif |