It looks like you're new here. If you want to get involved, click one of these buttons!
I would find it useful to see the 'state' of certain settings at a glance without opening any additional dialog/window.
For example, see if 'select top level objects' is selected.
So I wrote this code. It mostly works (displays correctly, works correctly when you click the checkbox). But the last bit where I try to link that to a shortcut key doesn't work. (Scroll to "DOESN'T WORK")
I think that the a=Action.new... functionality was probably originally intended to create Action items to place inside menus, and I have successfully used it in that way before.. but am I correct in concluding that this .shortcut method doesn't work if the Action is not inside a menu?
include RBA
inst = Application.instance
# Set up the panel
dock = QDockWidget::new("Settings", Application.instance.main_window)
dock.setAllowedAreas(Qt_DockWidgetArea::AllDockWidgetAreas) # Prior to 0.24 you had to use: dock.setAllowedAreas(0xF)
lw = QListWidget.new(dock)
dock.setWidget(lw)
sel_top_t = "Select top level (T)"
sel_top = QListWidgetItem.new(sel_top_t,lw)
snap_obj_t = "Snap to objects (J)"
snap_obj = QListWidgetItem.new(snap_obj_t,lw)
lw.addItem(sel_top)
lw.addItem(snap_obj)
c = Qt_CheckState::Checked
nc = Qt_CheckState::Unchecked
# Figure out if it should start out checked or not
s = "edit-top-level-selection"
inst.get_config(s)=="true" ? sel_top.setCheckState(c) : sel_top.setCheckState(nc)
s = "edit-snap-to-objects"
inst.get_config(s)=="true" ? snap_obj.setCheckState(c) : snap_obj.setCheckState(nc)
# Whenever it's clicked with the mouse, toggle the actual setting state
lw.itemChanged() { |qlwi|
case qlwi.text
when sel_top_t
s = "edit-top-level-selection"
when snap_obj_t
s = "edit-snap-to-objects"
end
inst.get_config(s)=="true" ? inst.set_config(s, "false") : inst.set_config(s, "true") # Toggle it
}
# Or, whenever you press the relevant shortcut key, it toggles the setting state and the checked state
# DOESN'T WORK
a = Action.new
a.shortcut = "T"
a.on_triggered {
s = "edit-top-level-selection"
if inst.get_config(s)=="true"
sel_top.setCheckState(c)
inst.set_config(s, "false")
else
sel_top.setCheckState(nc)
inst.set_config(s, "true")
end
}
a = Action.new
a.shortcut = "J"
a.on_triggered {
s = "edit-snap-to-objects"
if inst.get_config(s)=="true"
snap_obj.setCheckState(c)
inst.set_config(s, "false")
else
snap_obj.setCheckState(nc)
inst.set_config(s, "true")
end
}
inst.main_window.addDockWidget(Qt_DockWidgetArea::RightDockWidgetArea, dock)
Thanks,
David
Comments
I realized a second problem. If you change the setting in the usual manner (eg. F3 > Select top level objects) then the toggling function of the above code gets out of sync.
Basically it lacks a way to respond to the user setting the "true" or "false" setting by another method (F3).
Is this not possible with the existing API?
Hi David,
well, you'd need a way to watch the configuration settings. That's more or less the database holding the configuration parameters where various sources (including scripts) send their configuration requests to. Later these requests will be distributed to the modules.
Right now there is no direct way to receive configuration events, but there is an indirect one: By registering a plugin you basically create another module which will receive configuration events. This solution exploits an undocumented feature: a plugin factory will receive configuration events even for keys not registered for that specific plugin.
Please don't tell anybody :-)
Matthias
Thanks Matthias,
Yes your secret is safe with me.
Well this is a nice feature! But it seems to be messing with either other plugins or configuration settings. When I run it, various things get disabled, for example
Hopefully the above symptoms might give you some idea what is going on? I took a look at PluginFactory class but can't see any hints..
I understand this is not standard territory into which we are venturing, and KL is still 'young', so please don't view my comments as any kind of disrespect for what is a GREAT tool and for the thought you have put in to it.
David
P.S. For future readers: A second minor point is that it installs a toolbar entry "My plugin test" which wouldn't be required for such 'stealth' (run-in-background) plugins. I find I can delete it with RBA::Application.instance.main_window.menu.delete_item("@toolbar.my_plugin_test"); deleting it doesn't appear to affect the functioning of the plugin.
Huh, never mind, it seems to be solved by just making it "Run early on startup" -- i.e. run before any layout views are created.
If I get a working final "settings display" script, I'll post it here.
Thanks Matthias
David
Hi David,
The symptoms you describe look like the return value of the "configure" method is not "false" - if it's true, the configuration events will basically be blocked with similar effects than you describe.
To prevent the toolbar entry, you can set the "has_tool_entry" attribute to false in the initialization. Here is my complete code which works for me even if started without "run early":
Best regards,
Matthias
Thanks Matthias for the tip about has_tool_entry.
Thanks also for your other comments. I do still see the problems I mention if I run the plugin after a layoutview already exists. If I run it before a layoutview exists, it is fine. In that sense then it doesn't need to "run on startup" necessarily, it just needs to run before a layoutview exists. But it's fine, the fix for me is just to run on startup. ("early" is not required, contrary to my previous post.)
Here is the final code, which works quite nicely I think! Thanks so much for your help.
David
Hi David,
thanks for providing the script.
I can confirm the issue with the missing background after running the script directly. I'd like to understand the reason, but on the other hand there seems to be a simple solution.
Thanks,
Matthias