Developer’s Corner: Integrate your plugin into the Workspace Editor

March 25th, 2022

Did you know that it is easy to integrate your custom plugin into the Workspace editor itself?

The custom plugin “My Project” has its own top-level menu and configuration form inside the Workspace editor

What sort of tools could you add to a custom menu? How could your configure your plugin?

Look at the Rendering form for inspiration.

Example of a configuration form/widget. It’s easy to create one customised for your own plugin,

The Workspace editor gives you lots of “Wizards” that make it easy to jump-start your development code. In the following blog we’re going to look at how two of these can help you to integrate your own plugins into the Workspace editor: with their help, you can easily add your own custom menu and configuration form.

Integrating your own plugin into the Workspace editor

Workspace wizards

The Workspace editor’s Development menu gives access to a range of “wizards” that automatically generate stub code to help you develop your plugins. You run the wizard, and subsequently you customise the stubs, recompile the code and are ready to use the new features. Moreover, there is a range of Workspace tutorials available to help you use these: for more examples, see Workspace Developer Tutorials.

Finally, two of these wizards are designed to help you link your own plugins directly into the Workspace editor. They do, however, assume you have already created your own Workspace plugin to work on.

If you don’t have a plugin yet, you can create one with the Create Plugin wizard (see Writing a Simple Workspace Plugin))

How to add your own menu to the Workspace editor

What you need to do:

These steps are described in more detail below. Click on a link to jump ahead.

Run the code wizard to generate stub code

The Workspace editor’s Add plugin menu wizard makes it easy to add the code you need. To use it you need to do the following,

  1. Open the Workspace editor
  2. Select Create custom plugin menu… from the Development menu. This will bring up the Add plugin menu wizard
  3. Then the wizard will prompt you to enter your plugin name and location before generating some new files and modifying some existing ones

If you were to open up your source file directory, you would see that the wizard has added two new files. For example, look at the figure below, generated after running the wizard. I have added a menu to a workspace plugin called “MyProjectPlugin”. The new files are highlighted in the figure below:

Customise your plugin code using Visual Studio

Firstly, decide where you want the menu to be located. Perhaps it should be a top-level menu, or maybe it would make more sense as a submenu under “Plugins”. Secondly, decide if you want use the default menu name or set your own. Thirdly, for each item you want in your customised menu, you’ll need to edit the code in three places.

  • If you want the menu to be a drop-down member of a “Plugins” menu , edit the function isTopLevelMenu() to return “False”
  • If you want to change the default menu name, edit getMenuName()
  • For each item you want in your menu, you should
    • Create and implement a new slot that defines the action you want
      • Declare the slot in the pluginmenu.h file
      • Define the slot in the pluginmenu.cpp file and then
    • Define an action that links the slot to the menu in the createMenu() function of the pluginmenu.cpp file

For example, in the figure below, firstly, a single menu item is added to the new menu. It brings up a link to the online Workspace documentation. Secondly, the function isTopLevelMenu() is edited to return false. Consequently, the menu will appear under “Plugins”

Firstly, here is what the Workspace editor looks like with the new menu. The “My Project” plugin now has it’s own menu entry. It is a submenu of the top-level menu “Plugins” because isTopLevelMenu() was edited to return false.

And secondly, here is what it would have looked like if we had left isTopLevelMenu() returning true. It’s up to you.

Finally, there is more detail about this in the Workspace tutorial Workspace: Integrating a custom plugin menu into the Workspace Editor

How to add your own configuration widget to the Workspace editor

Generate stub code

The figure below shows an example of a settings widget for the “My Project” plugin that has been added to the Workspace editor. It isn’t difficult to add your own plugin-specific settings to the editor. Here’s how to do it:

What you need to do:

  1. Generate stub code by running the Add plugin configuration widget wizard from the Workspace editor
  2. Design your form using Qt Designer
  3. Link your form widgets to your plugin code
  4. Compile your code and restart the Workspace editor

The Workspace editor’s Add plugin configuration widget wizard makes it easy to add the code you need. To use it,

  1. Open the Workspace editor
  2. Then select Create custom plugin settings widget… from the Development menu. This will bring up the Add plugin configuration widget wizard
  3. The wizard will prompt you to enter your plugin name and code location before modifying your plugin code. The wizard creates a stub .ui form and matching C++ files to link it to the plugin. You can see the new files that are created highlighted in the figure below.

Don’t forget, if you don’t have a plugin yet, you can create one with the Create Plugin wizard (see Writing a Simple Workspace Plugin) .

Design your form using Qt Designer

Once you have generated your stub code, you’ll want to design your widget. You can do this by opening up the application Qt Designer from the Workspace editor’s Developer menu (see figure below).

To link the widgets that you added when you designed your form to your plugin, you need to edit the new configwidget.cpp file. Here’s how:

  • First connect widget signals to the class’s modified signal so that the program knows when any of the form widget values have changed
  • Then implement the configuration widget’s save() and reload() functions. As an example, the following code shows how you could use the plugin’s setting file to save the entered settings.

Recompile the plugin, restart the Workspace editor and see the result

By default, your new settings can be used by selecting Plugins menu, and selecting your plugin name from the drop-down Plugin list.

However, if your plugin has a lot of settings that you think the user will access frequently, you’ll probably want an entry in the main list. To do this, you’ll need edit the isTopLevelConfig() entry in the plugin configuration file so that it returns true. To clarify, in this example, you would edit the file myprojectpluginconfig.cpp.

Finally, there is more detail about this and about how to connect your settings to your plugin operations in the Workspace tutorial Workspace: Integrating a custom plugin settings widget into the Workspace Editor

Workspace

For more information about the Workspace see User Documentation
As always, we would be really glad to hear from you if you have any feedback.