[SOLVED] RGuiAction.getByScriptFile

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

[SOLVED] RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 1:41 pm

Hi there,

I'm still trying to connect a QToolButton (inside a widget) to a script (see first question at viewtopic.php?f=30&t=3187 which was not solved because I changed the complete concept after understanding the answer to my former second question - now having the same problem again). The following code works:

Code: Select all

widgets["addTrack"]["clicked()"].connect(this, function() { /* do something somewhere else! */ });
but it is not what is wanted - I like to start a script as if the user started it from the menu bar or via short cut.

So I tried

Code: Select all

var button = formWidget.findChild("addTrack");
button.setDefaultAction(RGuiAction.getByScriptFile("scripts/QCTrack/TrackWidgetInsert/TrackWidgetInsert.js"));
which results in null-function (clicking on the button just does nothing, even with debugger enabled), because RGuiAction.getByScriptFile returns null.

Surprisingly

Code: Select all

var button = formWidget.findChild("addTrack");
button.setDefaultAction(RGuiAction.getByScriptFile("scripts/Block/InsertBlock/InsertBlock.js"));
gave me an icon on the button, clicking on it starts the InsertBlock-script.

I'm sorry for asking questions like that, but I can't see any difference. :oops: Can you help me out? Is there something that needs to be done inside the script that should be connected?

Thanks in advance,
Stefan
Last edited by smf on Thu Jan 08, 2015 8:22 pm, edited 1 time in total.

User avatar
andrew
Site Admin
Posts: 9062
Joined: Fri Mar 30, 2007 6:07 am

Re: RGuiAction.getByScriptFile

Post by andrew » Thu Jan 08, 2015 4:12 pm

If RGuiAction.getByScriptFile("scripts/QCTrack/TrackWidgetInsert/TrackWidgetInsert.js") returns null, there is no RGuiAction available for that script.

As soon as an action is added to the menu (or another tool button, etc) it 'lives' and can be queried by RGuiAction.getByScriptFile.

If you only need this action for this button, you can create it right there where you need it:
var action = new RGuiAction(qsTr("Text"), RMainWindowQt.getMainWindow());
action.setRequiresDocument(true or false);
action.setScriptFile("scripts/QCTrack/TrackWidgetInsert/TrackWidgetInsert.js");
action.setIcon("scripts/QCTrack/TrackWidgetInsert/TrackWidgetInsert.svg");
action.setStatusTip(...);
Note that RGuiAction objects should only be created once. Once created, the same action should be used behind all user interface components which trigger the action (menus, context menus, buttons, etc.).

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 4:34 pm

Thank you Andrew! :)

There is a menu item for the script, starting the script via short cut also works. At the moment, I see two differences: (1) On QCad start, the script is disabled because no document is existing. (2) I have no icon.

The init function of TrackWidgetInsert is:

Code: Select all

    var action = new RGuiAction(QCTrack.MenuItem.TrackWidgetInsert, RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/TrackWidgetInsert.js");
    action.setDefaultShortcut(new QKeySequence("q,n"));
    action.setGroupSortOrder(QCTrack.MenuOrder.GroupSortOrder);
    action.setSortOrder(QCTrack.MenuOrder.TrackWidgetInsert);
    EAction.addGuiActionTo(action, QCTrack, true, false, false);
    var appWin = EAction.getMainWindow();
    appWin.addSelectionListener(action);
Does this look that bad?

User avatar
andrew
Site Admin
Posts: 9062
Joined: Fri Mar 30, 2007 6:07 am

Re: RGuiAction.getByScriptFile

Post by andrew » Thu Jan 08, 2015 4:40 pm

Please check which code is executed first, the action creation or querying the action with RGuiAction.getByScriptFile.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 5:00 pm

The widget that uses the other function is called before (i.e. query before action). :(

User avatar
andrew
Site Admin
Posts: 9062
Joined: Fri Mar 30, 2007 6:07 am

Re: RGuiAction.getByScriptFile

Post by andrew » Thu Jan 08, 2015 5:09 pm

You can try to use postInit instead of init to create your widget.

I.e.
MyClass.postInit = function() {
    ...
};
Or if you have it in a separate file MyClassPostInit.js:
function postInit() {
    ...
}

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 5:20 pm

:) 8) :)

postInit did it. Thanks a lot! :) :) :)

One (little?) thing remaining: the text of the butten is replaced by the icon, but as I don't have one, the button becomes empty. Is there a "good" way to have a text on it?

Again: many thanks, Andrew! :)

User avatar
andrew
Site Admin
Posts: 9062
Joined: Fri Mar 30, 2007 6:07 am

Re: RGuiAction.getByScriptFile

Post by andrew » Thu Jan 08, 2015 5:38 pm

var action = new RGuiAction(...);
action.disableIcon();
By default, actions without an icon are assigned an empty icon. Without any icon, menu entries show check marks beside menu entries on some Linux systems.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 8:21 pm

Thanks again Andrew for the fast help. Disabling the icon will print the full menu item text, so my widget "explodes" in terms of size. :shock: But I think I found a "workaround": I will use a mini-function that uses something like

Code: Select all

var action = RGuiAction.getByScriptFile("scripts/Block/EditBlock/EditBlock.js");
if (!isNull(action)) { action.slotTrigger(); }
which I found in BlockList.js - now having RGuiAction working, I just call the other script and make the buttons completely independent. :D

Perfect support here in the forum, thanks again! :)

User avatar
andrew
Site Admin
Posts: 9062
Joined: Fri Mar 30, 2007 6:07 am

Re: RGuiAction.getByScriptFile

Post by andrew » Thu Jan 08, 2015 8:40 pm

smf wrote:which I found in BlockList.js - now having RGuiAction working, I just call the other script and make the buttons completely independent. :D
OK. Glad you could solve it.

Some things to keep in mind though: Your button will not be automatically disabled if no document is open. Also if the action requires a selection, the button will not be disabled if there is no selection. This is only the case if the action is the default action of the button.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: [SOLVED] RGuiAction.getByScriptFile

Post by smf » Thu Jan 08, 2015 9:56 pm

Thanks for the hint! :)

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”