changing icon

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
bakoun
Registered Member
Posts: 2
Joined: Wed Jul 06, 2022 8:24 am

changing icon

Post by bakoun » Wed Jul 06, 2022 8:45 am

Hi !

I wrote a small script that shows/hides a specific layer.
I would like the button icon to change depending on the state of the layer. For example: ToggleSpitLayer.svg when the layer is visible and ToggleSpitLayer-off.svg when it is not.

Code: Select all

include("scripts/Layer/Layer.js");

function ToggleSpitLayer(guiAction) {
    Layer.call(this, guiAction);
}

ToggleSpitLayer.prototype = new Layer();

ToggleSpitLayer.init = function(basePath) {
	var action = new RGuiAction(qsTr("Show/Hide Spit layer"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/ToggleSpitLayer.js");
	action.setIcon(basePath + "/ToggleSpitLayer.svg");
	action.setStatusTip(qsTr("Show/Hide Spit layer"));
    action.setDefaultShortcut(new QKeySequence("s,p,i"));
    action.setDefaultCommands(["spi"]);
    action.setGroupSortOrder(80100);
    action.setSortOrder(300);
    action.setWidgetNames(["MenuSpit", "ToolBarSpit"]);
};

ToggleSpitLayer.prototype.togglevue = function(layerName, operation) {  
	try {
		var layer = this.getDocument().queryLayer(layerName);
		layer.setFrozen(!layer.isOff());
		layer.setOff(!layer.isOff());    
		operation.addObject(layer);
	}catch(error){}
}

ToggleSpitLayer.prototype.beginEvent = function() {
    Layer.prototype.beginEvent.call(this);
	var operation = new RModifyObjectsOperation();
	this.togglevue("SPIT", operation);
    var di = this.getDocumentInterface();
    di.applyOperation(operation);
	this.terminate();
};
Any ideas?
Thanks in advance!

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

Re: changing icon

Post by andrew » Wed Jul 06, 2022 12:31 pm

You can retrieve your action (RGuiAction, inherited from QAction) from anywhere with:

Code: Select all

var action = RGuiAction.getByScriptFile("scripts/......./ToggleSpitLayer/ToggleSpitLayer.js");
You can then change the icon with:

Code: Select all

action.setIcon(...);

bakoun
Registered Member
Posts: 2
Joined: Wed Jul 06, 2022 8:24 am

Re: changing icon

Post by bakoun » Wed Jul 06, 2022 2:04 pm

Many thanks !!!

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: changing icon

Post by CVH » Thu Jul 07, 2022 4:26 am

Hi,

Note that the KeySequence ("s,p,i") would conflict with Draw Spline (Control Points) with KeySequence("s,p"). :wink:

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”