Widget to Plugin?

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
Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 9:33 am

Greetings.

So I have made a widget that performs a set of functions when called on the toolbar.

I would like to make this widget stand permanently on the right side of the screen (Like the Editor Properties, Layout and Block, etc). I believe, but am not sure, that what I intend to is called a Plugin.
One of the reasons is that, as it is, I cannot alter the drawing while the widget is activated and one of my final objectives for this script will demand that possibility.

I have found no specific examples in the Qcad introduction and tutorial page, though this page seems to fit the bill, It seems it is imcomplete and so, I cannot understand how to procede.

I hope this process is simple.

Thank you very much.

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

Re: Widget to Plugin?

Post by andrew » Wed Apr 20, 2016 9:44 am

My best guess (without further information) is that your widget is implemented as a dialog. You can create a dock widget instead (QDockWidget) to make the widget dockable at the right and be able to work on your drawing.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 10:09 am

I'm assuming this is what you mean by dialog?

Code: Select all

FirstWidget.prototype.beginEvent = function() {
	
	IuSYS.prototype.beginEvent.call(this);
	// Create the dialog from the .ui file using the helper
	var dialog = WidgetFactory.createWidget(FirstWidget.includeBasePath, "FirstWidget.ui");

	var appWin = EAction.getMainWindow();
	var widgets = getWidgets(dialog);
Tried to peruse the Porperty Editor code to try to glean any indication on how to use the QDockWidget class, but found nothing.
What information must I provide to make my question and objectives more clear?

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

Re: Widget to Plugin?

Post by andrew » Wed Apr 20, 2016 10:15 am

In your example, the file FirstWidget.ui contains all the information about what type of widget you are creating and what controls (line edits, combo boxes, etc.) are available in your widget.
You can open / edit that file with the user interface designer of Qt creator or edit it as text to change QDialog into QDockWidget.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 10:42 am

Interesting.
I changed the

Code: Select all

<widget class="QDockDialog" name="MyPositionDialog">
to

Code: Select all

<widget class="QDockWidget" name="MyPositionDialog">
I did, indeed, dock the widget to the side. However, the widget itself held none of the objects, like buttons of tables, that I had in the previous UI.

I'm going to try to make a new UI as a Qdockwidget and reproduce the objects as they were and I'll get back to you.

Thank you andrew, as always, a swift reply.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 11:20 am

So I have successfully made it into a dockwidget. I am frankly embarassed over how simple it was.

Not to be dismayed by that embarassment, I will endeavour to embarass myself even further.

2 Questions:
I have noticed that the docked widget needs to be called back every time I restart the program. Is there a way I can make it be called as soon as the program starts?
(I'm assuming calling var dialog = WidgetFactory.createWidget(FirstWidget.includeBasePath, "FirstWidget.ui"); in the init function FirstWidget.init = function(basePath) { is not the way to go.)

I have noticed that when I call the widget for the first time, I cannot select anything on my drawing. I've come to understand that it happens because I have an active tool selected with the name of my widget. It is solved by deselecting the tool (right click). This is not a major issue, more a curiosity and desire to eliminate a possible annoyance.
I'm assuming it has something to do with one of EActions but, in your experience, what causes this and how to eliminate it?

Thank you very much.

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

Re: Widget to Plugin?

Post by andrew » Wed Apr 20, 2016 11:36 am

Creating the widget in init is indeed the way how things can be initialized or created on start up.

Typically, init would add a menu to show / hide the dock widget as well as create the dock widget in its default state (visible or invisible). beginEvent on the other hand would only toggle the visibility of the widget. Below the essential bits of the code needed. For a full example, please refer to LayerList.js.
MyWidget.prototype.beginEvent = function() {
    EAction.prototype.beginEvent.call(this);

    // running the action toggles the dock visibility:
    var appWin = RMainWindowQt.getMainWindow();
    var dock = appWin.findChild("MyWidgetDock");
    dock.visible = !dock.visible;
};


MyWidget.init = function(basePath) {
    var appWin = RMainWindowQt.getMainWindow();

    // set up action to toggle widget visibility:
    var action = new RGuiAction(qsTr("&My Widget"), appWin);
    action.setRequiresDocument(false);
    action.setScriptFile(basePath + "/MyWidget.js");
    action.setIcon(basePath + "/MyWidget.svg");
    action.setGroupSortOrder(3600);
    action.setSortOrder(500);
    action.setWidgetNames(["ViewMenu", "WidgetsToolBar", "ViewToolsPanel", "WidgetMatrixPanel"]);

    // MyWidget.ui contains a QWidget in this example (not a dock widget):
    var widget = WidgetFactory.createWidget(basePath, "MyWidget.ui");

    // the dock widget is created here with the widget from the ui file as contents:
    // we use RDockWidget instead of QDockWidget here to receive the signals 'shown' and 'hidden' which we use below:
    var dock = new RDockWidget(qsTr("My Widget"), appWin);
    dock.objectName = "MyWidgetDock";
    dock.setWidget(widget);
    appWin.addDockWidget(Qt.RightDockWidgetArea, dock);

    // keep action status up to date when dock widget is shown (tool button pressed, menu checked) / hidden (tool button not pressed, menu unchecked):
    dock.shown.connect(function() { action.setChecked(true); });
    dock.hidden.connect(function() { action.setChecked(false); });
};

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 3:35 pm

The widget does start when Qcad initializes, however the buttons stopped working.

I presume the problem lies here:
FirstWidget.prototype.beginEvent = function() {
	
	IuSYS.prototype.beginEvent.call(this);
	
	var appWin = RMainWindowQt.getMainWindow();
    var dock = appWin.findChild("MyWidgetDock");
    dock.visible = !dock.visible;
	
	//var dialog = WidgetFactory.createWidget(FirstWidget.includeBasePath, "FirstWidget.ui");
	
	var widgets = getWidgets(dialog); // var dialog is now called at init.
	
//This called a button named cmdExit
	widgets["cmdExit"].clicked.connect(function () {
		appWin.handleUserMessage("Exit Successful");
		dialog.destroy();
		EAction.activateMainWindow();
		this.terminate();
	});
since var dialog = WidgetFactory.createWidget(FirstWidget.includeBasePath, "FirstWidget.ui"); is now at the init, var widgets = getWidgets(dialog); gets no source data, I suppose.
I have tried to switch "dialog" for "block", to no avail. Again, this likely requires a simple correction, but I'm still unsure of how the classes operate.
I speculate that either there is a specific way to get the widget through block or a awy to migrate the data like it was done through var dock = appWin.findChild("MyWidgetDock");

Thank you very much.

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

Re: Widget to Plugin?

Post by andrew » Wed Apr 20, 2016 4:02 pm

Variables from init are not available in beginEvent (different scope).

The widget containing the button is now simply the widget that was used inside the dock widget:

var dialog = dock.widget();

You can also get any uniquely named widget anytime from anywhere using appWin.findChild("WidgetName");

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Widget to Plugin?

Post by Ghost_of_Magellan » Wed Apr 20, 2016 4:39 pm

eeer, so:

Code: Select all

var appWin = RMainWindowQt.getMainWindow();
    var dock = appWin.findChild("MyWidgetDock");
    dock.visible = !dock.visible;
	
	var dialog = dock.widget();
	
	var widgets = getWidgets(dialog);
	
	widgets["cmdExit"].clicked.connect(function () {
		appWin.handleUserMessage("Exit Successful");
		dialog.destroy();
		EAction.activateMainWindow();
		this.terminate();
	});
?

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

Re: Widget to Plugin?

Post by andrew » Wed Apr 20, 2016 6:31 pm

Looks about right, yes. A dock widget usually does not have a button to close and destroy it though. Dock widgets are typically only shown / hidden, never destroyed.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”