Development/Tutorials/Kross/ActionCollections

    From KDE TechBase
    Revision as of 13:39, 2 March 2010 by Xxtjaxx (talk | contribs) (A small how to on Kross::ActionCollection's)
    (diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

    Basics

    Kross::ActionCollections are like normal ActionCollections a group of Actions. In this case those are 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: actionCollection = new Kross::ActionCollection("actioncollection",Kross::Manager::self().actionCollection()); 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.

    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. 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 declared these Actions we gave it as first Argument the Kross::ActionCollection this Action is a child of and a path to a file. 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(QUrl("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.