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

Jump to: navigation, search
m (moved User:Xxtjaxx/HowTo-use-ActionCollections to Development/Tutorials/Kross/ActionCollections: Proof read by Aaron Seigo. Allowed to move by Aaron Seigo.)
m (Text replace - "<code cpp>" to "<syntaxhighlight lang="cpp">")
Line 3: Line 3:
  
 
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>
 
</code>
Line 10: Line 10:
 
== Using it ==
 
== 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.
 
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"));
Line 20: Line 20:
  
 
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("path/to/some/snippet.js");
 
actioncollection->action("path/to/some/snippet.js");
 
</code>
 
</code>
 
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)) {

Revision as of 20:38, 29 June 2011

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());
</code>
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 ==
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.
<syntaxhighlight lang="cpp">
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"));
</code>
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.}}
 
Once we have declared the Kross::Actions we can either access them by their fully qualified name (the second Argument in the constructor) :
<syntaxhighlight lang="cpp">
actioncollection->action("path/to/some/snippet.js");
</code>
or iterate through all Kross::Actions and trigger those that match a pattern for example:
<syntaxhighlight lang="cpp">
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();
   }
}
</code>
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!

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal