QgsLayerTreeViewMenuProvider and default values

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

QgsLayerTreeViewMenuProvider and default values

Richard Duivenvoorde

Hi,

trying to implement a 'refresh data' menu item for a layer, I followed
this blog post to create one:

http://www.lutraconsulting.co.uk/blog/2015/01/30/qgis-layer-tree-api-part-3/

but using it removes all default options, AND it is created for ALL
layers.

Q1: is there an option to only add this menu item to certain (1) layers?

Q2: is it possible to add the new 'refresh' option on top of all normal
default menu items?

Q3: looking at the possibility to add a 'Transparency slider' in the
layertree (see layer props bottom item): how difficult is it to add a
'refresh data' button (with custom refresh data code) in the layer tree

Regards,

Richard Duivenvoorde
_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsLayerTreeViewMenuProvider and default values

Martin Dobias
Hi Richard


On Fri, Nov 18, 2016 at 5:02 PM, Richard Duivenvoorde
<[hidden email]> wrote:
>
> trying to implement a 'refresh data' menu item for a layer, I followed this
> blog post to create one:
>
> http://www.lutraconsulting.co.uk/blog/2015/01/30/qgis-layer-tree-api-part-3/
>
> but using it removes all default options, AND it is created for ALL layers.

Right - if you want to modify the default menu, there is
QgsLegendInterface that you can obtain through
iface.legendInterface().

Note: most of the methods are obsolete now thanks to layer tree API,
but there are few methods for menu handling that are not available
elsewhere. Note to myself: remove QgsLegendInterface in 3.0 and move
the few useful methods to QgisInterface.


> Q1: is there an option to only add this menu item to certain (1) layers?

Yes, QgsLegendInterface::addLegendLayerActionForLayer(...)


> Q2: is it possible to add the new 'refresh' option on top of all normal
> default menu items?

Yes, QgsLegendInterface::addLegendLayerAction(...)

(but not for group nodes)


> Q3: looking at the possibility to add a 'Transparency slider' in the
> layertree (see layer props bottom item): how difficult is it to add a
> 'refresh data' button (with custom refresh data code) in the layer tree

See the original pull request for some example code:

https://github.com/qgis/QGIS/pull/3170

Cheers
Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsLayerTreeViewMenuProvider and default values

Richard Duivenvoorde
On 2016-11-21 03:02, Martin Dobias wrote:

Ah, thanks Martin for your anwers.

>> Q3: looking at the possibility to add a 'Transparency slider' in the
>> layertree (see layer props bottom item): how difficult is it to add a
>> 'refresh data' button (with custom refresh data code) in the layer
>> tree
>
> See the original pull request for some example code:
>
> https://github.com/qgis/QGIS/pull/3170

I tried this, and indeed can create such a widget (QToolButton or
QPushBtuuon) with a handle to a layer to do things with it.
But I can only add it to a layer in the legend via the properties
dialog?

If I try to add the widget via the addLegendLayerActionForLayer

btn = self.refresh_button_provider.createWidget(self.measurements_layer,
0)
self.iface.legendInterface().addLegendLayerActionForLayer(btn.defaultAction(),
self.action_layer)

it does not show up in the legend. (the same button DOES show up (and
works) if I do via Layer Properties/Legend dialog).

Should I be able to add such a button/action to just that layer via
addLegendLayerActionForLayer?

Or do I miss something here?

Regards,

Richard Duivenvoorde

_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsLayerTreeViewMenuProvider and default values

Martin Dobias
In reply to this post by Martin Dobias
On Mon, Nov 21, 2016 at 6:09 PM, Richard Duivenvoorde <[hidden email]> wrote:

> On 2016-11-21 03:02, Martin Dobias wrote:
>
> Ah, thanks Martin for your anwers.
>
>>> Q3: looking at the possibility to add a 'Transparency slider' in the
>>> layertree (see layer props bottom item): how difficult is it to add a
>>> 'refresh data' button (with custom refresh data code) in the layer tree
>>
>>
>> See the original pull request for some example code:
>>
>> https://github.com/qgis/QGIS/pull/3170
>
>
> I tried this, and indeed can create such a widget (QToolButton or
> QPushBtuuon) with a handle to a layer to do things with it.
> But I can only add it to a layer in the legend via the properties dialog?

Here is how to configure an embedded widget without properties dialog:

my_layer.setCustomProperty("embeddedWidgets/count", 1)
my_layer.setCustomProperty("embeddedWidgets/0/id", "transparency")

Finally one needs to call refreshLayerLegend(), e.g.:

view = iface.layerTreeView()
view.model().refreshLayerLegend(view.currentNode())

The methods from QgsLegendInterface only affect the actions in context
menu for the layer tree, they are not related to embedded widgets
functionality.

Cheers
Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: QgsLayerTreeViewMenuProvider and default values

Richard Duivenvoorde
On 21-11-16 16:40, Martin Dobias wrote:

> On Mon, Nov 21, 2016 at 6:09 PM, Richard Duivenvoorde <[hidden email]> wrote:
>> On 2016-11-21 03:02, Martin Dobias wrote:
>>
>> Ah, thanks Martin for your anwers.
>>
>>>> Q3: looking at the possibility to add a 'Transparency slider' in the
>>>> layertree (see layer props bottom item): how difficult is it to add a
>>>> 'refresh data' button (with custom refresh data code) in the layer tree
>>>
>>>
>>> See the original pull request for some example code:
>>>
>>> https://github.com/qgis/QGIS/pull/3170
>>
>>
>> I tried this, and indeed can create such a widget (QToolButton or
>> QPushBtuuon) with a handle to a layer to do things with it.
>> But I can only add it to a layer in the legend via the properties dialog?
>
> Here is how to configure an embedded widget without properties dialog:
>
> my_layer.setCustomProperty("embeddedWidgets/count", 1)
> my_layer.setCustomProperty("embeddedWidgets/0/id", "transparency")
>
> Finally one needs to call refreshLayerLegend(), e.g.:
>
> view = iface.layerTreeView()
> view.model().refreshLayerLegend(view.currentNode())
>
> The methods from QgsLegendInterface only affect the actions in context
> menu for the layer tree, they are not related to embedded widgets
> functionality.

Hi Martin,

a cool thanks. Will try.

I think having this kind of functionality has a lot of potential
specially for plugin writers (examples taken from Martin's blogs or PR's):
- change transparency with a slider widget
- refresh data for this layer via a button widget
- let user choose between x predefined styles with a dropdown widget
- create a timebased subquery via a datetime-widget
- do 'something' on this layer
- layer based timeslider?
etc etc

This kind of 'components' could easily be exchanged via plugins isn't
it? Or do you envision some other exchange mechanism?

The api for using a widget via python/code could have some love I think.

Also do you think it is usefull to have the same type of widget 2 times
for a layer in the legend?

I'll try to add this new knowledge in the python cookbook too.

Thanks & Regards,

Richard Duivenvoorde



_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer