Difference between revisions of "Development/Tutorials/Kross/ActionCollections"

Jump to: navigation, search
(A small how to on Kross::ActionCollection's)
 
m (Text replace - "</code>" to "</syntaxhighlight>")
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== Basics ==
 
== Basics ==
Kross::ActionCollections are like normal ActionCollections a group of Actions. In this case those are
+
Kross::ActionCollections, much like KActionCollections, represent a group of actions. A Kross::ActionCollection is used to group Kross::Actions together and collectively give them an icon, description, name and display text.  
Kross::ActionCollections and can be used to group Actions and give the Collection Icon, a Description a Name and a Display Text.  
+
  
 
You can create a new Kross::ActionCollection like this:
 
You can create a new Kross::ActionCollection like this:
<code cpp>
+
<syntaxhighlight lang="cpp">
 
actionCollection = new Kross::ActionCollection("actioncollection",Kross::Manager::self().actionCollection());
 
actionCollection = new Kross::ActionCollection("actioncollection",Kross::Manager::self().actionCollection());
</code>
+
</syntaxhighlight>
The name "actioncollection can be replaced by any name you want and should be usefull and reasonably chosen. The function actionCollection() from Kross::Manager::self() is used to define that this is a toplevel collection. This is usefull if you want to have sub collections for different types of Kross::Actions.  
+
The first parameter, "actioncollection" in the example above, can be replaced by any name you want and should be useful and reasonably chosen. Passing in the collection returned by Kross::Manager::self()->actionCollection() defines that this is a toplevel collection. This is usefull if you want to have sub collections for different types of Kross::Actions.
  
 
== Using it ==
 
== Using it ==
Now that we created a Kross::ActionCollection its time to populate it with Kross::Actions. Therefore we create some actions which can all have different code and interpreters for them selfs.
+
Now that we have created a Kross::ActionCollection it is time to populate it with Kross::Actions. Therefore we next create some actions, each of which can have different code and interpreters.
<code cpp>
+
<syntaxhighlight lang="cpp">
 
Kross::Action *action1 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.py"));
 
Kross::Action *action1 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.py"));
 
Kross::Action *action2 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.js"));
 
Kross::Action *action2 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.js"));
 
Kross::Action *action3 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.rb"));
 
Kross::Action *action3 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.rb"));
</code>
+
</syntaxhighlight>
Each time we declared these Actions we gave it as first Argument the Kross::ActionCollection this Action is a child of and a path to a file.  
+
Each time we declare an Action, we gave it the Kross::ActionCollection the Action should be a child of as the first parameter and a path to a file as the second.  
'''NOTE:''' It doesn't need to be a valid file since you can set the code content later on any way.
+
'
 +
{{note|It doesn't need to be a valid file since you can set the code content later on any way.}}
  
 
Once we have declared the Kross::Actions we can either access them by their fully qualified name (the second Argument in the constructor) :
 
Once we have declared the Kross::Actions we can either access them by their fully qualified name (the second Argument in the constructor) :
<code cpp>
+
<syntaxhighlight lang="cpp">
actioncollection->action(QUrl("path/to/some/snippet.js"));
+
actioncollection->action("path/to/some/snippet.js");
</code>
+
</syntaxhighlight>
 
or iterate through all Kross::Actions and trigger those that match a pattern for example:
 
or iterate through all Kross::Actions and trigger those that match a pattern for example:
<code cpp>
+
<syntaxhighlight lang="cpp">
 
foreach(Kross::Action*  myAction, actioncollection->actions()) {
 
foreach(Kross::Action*  myAction, actioncollection->actions()) {
 
   if(myAction->name().contains("py", Qt::CaseInsensitive)) {
 
   if(myAction->name().contains("py", Qt::CaseInsensitive)) {
Line 32: Line 32:
 
   }
 
   }
 
}
 
}
</code>
+
</syntaxhighlight>
 
Notice that we connected the SIGNAL finished(Kross::Action*) before triggering the script. Otherwise the SLOT finished(Kross::Action*) won't run.
 
Notice that we connected the SIGNAL finished(Kross::Action*) before triggering the script. Otherwise the SLOT finished(Kross::Action*) won't run.
 +
 +
== What now? ==
 +
With the ActionCollection and  the metadata interfaces such as name() icon() and description() you can create a small MVC so users can enable/disable some of the actions you loaded from your files.
 +
 +
Happy hacking!

Latest revision as of 20:42, 29 June 2011

[edit] Basics

Kross::ActionCollections, much like KActionCollections, represent a group of actions. A Kross::ActionCollection is used to group Kross::Actions together and collectively give them an icon, description, name and display text.

You can create a new Kross::ActionCollection like this:

actionCollection = new Kross::ActionCollection("actioncollection",Kross::Manager::self().actionCollection());

The first parameter, "actioncollection" in the example above, can be replaced by any name you want and should be useful and reasonably chosen. Passing in the collection returned by Kross::Manager::self()->actionCollection() defines that this is a toplevel collection. This is usefull if you want to have sub collections for different types of Kross::Actions.

[edit] Using it

Now that we have created a Kross::ActionCollection it is time to populate it with Kross::Actions. Therefore we next create some actions, each of which can have different code and interpreters.

Kross::Action *action1 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.py"));
Kross::Action *action2 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.js"));
Kross::Action *action3 = new Kross::Action(actionCollection,QUrl("path/to/some/snippet.rb"));

Each time we declare an Action, we gave it the Kross::ActionCollection the Action should be a child of as the first parameter and a path to a file as the second. '

noframe
 
Note
It doesn't need to be a valid file since you can set the code content later on any way.

Once we have declared the Kross::Actions we can either access them by their fully qualified name (the second Argument in the constructor) :

actioncollection->action("path/to/some/snippet.js");

or iterate through all Kross::Actions and trigger those that match a pattern for example:

foreach(Kross::Action*  myAction, actioncollection->actions()) {
   if(myAction->name().contains("py", Qt::CaseInsensitive)) {
       myAction->setInterpreter("python");
       connect(myAction,SIGNAL(finished(Kross::Action* )),this ,SLOT(finished(Kross::Action*)));
       myAction->trigger();
   }
}

Notice that we connected the SIGNAL finished(Kross::Action*) before triggering the script. Otherwise the SLOT finished(Kross::Action*) won't run.

[edit] What now?

With the ActionCollection and the metadata interfaces such as name() icon() and description() you can create a small MVC so users can enable/disable some of the actions you loaded from your files.

Happy hacking!


This page was last modified on 29 June 2011, at 20:42. This page has been accessed 4,772 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal