Development/Tutorials/Kross/Script-Actions: Difference between revisions

From KDE TechBase
mNo edit summary
(Mark for updating)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Review|Port to KF5}}
==Introduction==
==Introduction==


This tutorial uses the script plugin technique explained in [http://techbase.kde.org/index.php?title=Development/Tutorials/Kross/Introduction#The_Module_plugins The Module Plugins] and explains how to code your application in order to easily add new script actions into menus. Two typical scenarios are:
This tutorial uses the script plugin technique explained in [http://techbase.kde.org/index.php?title=Development/Tutorials/Kross/Introduction#The_Module_plugins The Module Plugins] and explains how to code your application in order to easily add new script actions into menus. Two typical scenarios are:


- scripts shipped by default by the application developer
- scripts shipped by default by the application developer<br>
- custom scripts added by the user
- custom scripts added by the user


Line 17: Line 19:
An example of such Kross rc file could be:
An example of such Kross rc file could be:


<code xml>
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8" ?>
<KrossScripting>
<KrossScripting>
     <collection name="tools" text="Tools">
     <collection name="tools" text="Tools">
         <script
         <script text="Dummy Script" comment="Dummy Script"
             name="dummy_script"
             name="dummy_script"
            text="Dummy Script"
            comment="Dummy Script"
             interpreter="python"
             interpreter="python"
             file="dummy_script.py" />
             file="dummy_script.py" />
     </collection>
     </collection>
</KrossScripting>
</KrossScripting>
</code>
</syntaxhighlight>


The effect of such description will be the creation of a new menu item called "Dummy Script" in the "Tools" menu. The action associated to the new menu item will execute the script defined in the "file" path.
The effect of such description will be the creation of a new menu item called "Dummy Script" in the "Tools" menu. The action associated to the new menu item will execute the script defined in the "file" path.
Placing text and comment attributes on the same line with tag name is important for extractattr script that extracts strings for i18n.


Kross rc files are searched in the appdata "scripts" subdirectory, e.g.:
Kross rc files are searched in the appdata "scripts" subdirectory, e.g.:


$KDEDIR/share/apps/myapplication/scripts/*.rc
$KDEDIR/share/apps/myapplication/scripts/*.rc<br>
$KDEHOME/share/apps/myapplication/scripts/*.rc
$KDEHOME/share/apps/myapplication/scripts/*.rc


Line 45: Line 48:
Header:
Header:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
#include <kross/ui/plugin.h>
#include <kross/ui/plugin.h>


Line 67: Line 70:
     Private* const d;
     Private* const d;
};
};
</code>
</syntaxhighlight>




Implementation:
Implementation:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
#include "scriptingpart.h"
#include "scriptingpart.h"


Line 98: Line 101:
{
{
     d->module = new ScriptingModule(parent);
     d->module = new ScriptingModule(parent);
    addObject(d->module, "MyApplication");


     setComponentData(ScriptingPart::componentData());
     setComponentData(ScriptingPart::componentData());
Line 122: Line 126:
     connect(action, SIGNAL(triggered(bool)), this, SLOT(slotResetScriptActions()));
     connect(action, SIGNAL(triggered(bool)), this, SLOT(slotResetScriptActions()));
}
}
</code>
</syntaxhighlight>
 




Kross::ScriptingPlugin have two additional slots that can be useful for applications to use:
Kross::ScriptingPlugin have two additional slots that can be useful for applications to use:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
     /**
     /**
     * This slot will open/create a scriptactions.rc file at $KDEHOME/share/apps/application/scripts/
     * This slot will open/create a scriptactions.rc file at $KDEHOME/share/apps/application/scripts/
Line 138: Line 143:
     */
     */
     virtual void slotResetScriptActions();
     virtual void slotResetScriptActions();
</code>
</syntaxhighlight>
 


===The KPart plugin rc file===
===The KPart plugin rc file===
Line 145: Line 149:
Suggestion on how to use the edit and reset script actions (respective slots provided by Kross::ScriptingPlugin class):
Suggestion on how to use the edit and reset script actions (respective slots provided by Kross::ScriptingPlugin class):


<code xml>
<syntaxhighlight lang="xml">
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="MyApplicationPluginScripting" library="krossmodulemyapplication" version="1">
<kpartgui name="MyApplicationPluginScripting" library="krossmodulemyapplication" version="1">
Line 156: Line 160:
     </Menu>
     </Menu>
</MenuBar>
</MenuBar>
</code>
</syntaxhighlight>

Latest revision as of 08:22, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

Port to KF5

Introduction

This tutorial uses the script plugin technique explained in The Module Plugins and explains how to code your application in order to easily add new script actions into menus. Two typical scenarios are:

- scripts shipped by default by the application developer
- custom scripts added by the user

This facility is targeted for both cases.

See also...

The code

The client Part Plugin class should extend Kross::ScriptingPlugin which will automatically load script actions stored in Kross rc files.

An example of such Kross rc file could be:

<?xml version="1.0" encoding="UTF-8" ?>
<KrossScripting>
    <collection name="tools" text="Tools">
        <script text="Dummy Script" comment="Dummy Script"
            name="dummy_script"
            interpreter="python"
            file="dummy_script.py" />
    </collection>
</KrossScripting>

The effect of such description will be the creation of a new menu item called "Dummy Script" in the "Tools" menu. The action associated to the new menu item will execute the script defined in the "file" path.

Placing text and comment attributes on the same line with tag name is important for extractattr script that extracts strings for i18n.

Kross rc files are searched in the appdata "scripts" subdirectory, e.g.:

$KDEDIR/share/apps/myapplication/scripts/*.rc
$KDEHOME/share/apps/myapplication/scripts/*.rc

It will be explained later how can a final user override the actions found in the default rc files.

The KParts Plugin class

In this example we show the scripting plugin class which inherits a Kross::ScriptingPlugin.

Header:

#include <kross/ui/plugin.h>

class ScriptingModule;

class ScriptingPart : public Kross::ScriptingPlugin
{
public:
    ScriptingPart(QObject* parent, const QStringList& list);
    ~ScriptingPart();

    ScriptingModule* module() const;

private:
    void initActions();

private:
    Q_DISABLE_COPY(ScriptingPart)

    class Private;
    Private* const d;
};


Implementation:

#include "scriptingpart.h"

#include <kgenericfactory.h>
#include <kstandarddirs.h>
#include <kactioncollection.h>
#include <kcmdlineargs.h>
#include <kurl.h>
#include <klocale.h>

#include "scriptingmodule.h"

typedef KGenericFactory<ScriptingPart> MyApplicationScriptingFactory;
K_EXPORT_COMPONENT_FACTORY(krossmodulemyapplication, MyApplicationScriptingFactory("krossmodulemyapplication"))

class ScriptingPart::Private
{
public:
    QPointer<ScriptingModule> module;
};

ScriptingPart::ScriptingPart(QObject* parent, const QStringList&)
    : Kross::ScriptingPlugin(parent)
    , d(new Private())
{
    d->module = new ScriptingModule(parent);
    addObject(d->module, "MyApplication");

    setComponentData(ScriptingPart::componentData());
    setXMLFile(KStandardDirs::locate("data", "myapplication/kpartplugins/scripting.rc"), true);
    kDebug(23100) <<"Scripting plugin. Class:" << metaObject()->className() 
        <<", Parent:" << parent->metaObject()->className();

    initActions();
}

ScriptingPart::~ScriptingPart()
{
    delete d;
}

void ScriptingPart::initActions()
{
    KAction* action  = new KAction(i18n("Edit Script Actions..."), this);
    actionCollection()->addAction("edit_script_actions", action);
    connect(action, SIGNAL(triggered(bool)), this, SLOT(slotEditScriptActions()));

    action  = new KAction(i18n("Reset Script Actions..."), this);
    actionCollection()->addAction("reset_script_actions", action);
    connect(action, SIGNAL(triggered(bool)), this, SLOT(slotResetScriptActions()));
}


Kross::ScriptingPlugin have two additional slots that can be useful for applications to use:

    /**
     * This slot will open/create a scriptactions.rc file at $KDEHOME/share/apps/application/scripts/
     * which will override other kross rc files. This allows a user to extend existing menus with new actions.
    */
    virtual void slotEditScriptActions();

    /**
     * Deletes the user rc file, which has the effect of falling back to the default script actions (if any).
    */
    virtual void slotResetScriptActions();

The KPart plugin rc file

Suggestion on how to use the edit and reset script actions (respective slots provided by Kross::ScriptingPlugin class):

<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="MyApplicationPluginScripting" library="krossmodulemyapplication" version="1">
<MenuBar>
    <Menu name="tools"><text>&amp;Tools</text>
        <Separator/>
        <Action name="edit_script_actions"/>
        <Action name="reset_script_actions"/>
        <Separator/>
    </Menu>
</MenuBar>