2

In ArcView 3 all controls (menu choices, buttons, tools) had scripts associated with them that could be changed simply by supplying a script with the same name through an extension (plugin). That way you spared designing a new control and could keep your panels tidy while improving and extending functionality of the program.

Now I'd like to do the same thing with QGIS: E.g. I'd prefer to preselect the default directory of the "add ogr layer" action by the path of the first active layer in my map.
So if it's possible I'd like to replace or modify the mActionAddOgrLayer.triggered Script, as long as my plugin is loaded.

Another way to achieve this would be to modify the built-in system response to a change of the active layer(s) by changing the variable that by default saves the directory of the last file added. I'm sure there is a signaling but I don't know if and how responses can be changed or added without adding additional gui-controls (actions).

  • By active layer you mean visible layer or selected layer? – Germán Carrillo Mar 12 '15 at 14:10
  • 1
    Thank you for asking. I'm new to QGIS and still on the way translating ArcView features and terms to QGIS. In ArcView you have visible layers and active (selected) layers. In QGIS you have visible, underlined (which is the one called activeLayer in PyQGIS), and highlighted layers (can be more than one). I couldn't figure out if there is a functionality connected to the highlighted state; at least it wouldn't make a difference with the identify-tool. So to answer your question: selected (underlined) layer – Rudi Uhl Mar 12 '15 at 14:45
  • To be more precise about the highlighted state: you can delete those layers all at the same time, and you can set their crs at the same time, but you cannot open their attribute tables at the same time, so I couldn't tell what to expect beforehand and if one would call them selected. – Rudi Uhl Mar 12 '15 at 14:57
2

You can set the base directory of the "Add Vector Layer" dialog, reflecting the latest active layer's path, this way:

import os
from PyQt4.QtCore import QSettings

def setOpenDir( layer ):
  # Check if layer exists, has a proper path, and is of type vector
  if layer and os.path.exists( layer.source() ) and layer.type() == 0:
    layerPath = os.path.dirname( layer.source() )
    QSettings().setValue( "/UI/lastVectorFileFilterDir", layerPath )
    print "Path changed to", QSettings().value( "/UI/lastVectorFileFilterDir", "", type=str )

ltv = iface.layerTreeView()
ltv.currentLayerChanged.connect( setOpenDir )

Explanation:

You need to create a function to set the base directory of the "Add Vector Layer" dialog. Such function (setOpenDir) will get the current layer, check if it's a layer (could be None if you click on a group layer), check if it has a proper path (e.g., WFS layers don't), and check if it's a vector layer. Then it sets the layer's source path to the appropriate QGIS setting (/UI/lastVectorFileFilterDir).

Finally, you need to establish a connection to run the function each time the user changes the active layer.

I used QGIS 2.8 for testing the code in the QGIS Python console. Of course, you could include it in your plugin (with minor adjustments) and disconnect the function once the plugin is unloaded.

Hope it helps.

  • Thank you very much, that certainly helps. But just to gain a better understanding (and also get an answer to the first of my two questions, the one that gave the title): would there also be a possibility to access the script triggered by mActionAddOgrLayer directly or replace it by an own version? To give another example: I had many controls supplied with additional functionality by using the Shiftkey or Controlkey along with triggering click. That would require changes to those scripts (or replacements). If I'd learn how to change them I sure could learn a lot from them about QGIS as well... – Rudi Uhl Mar 12 '15 at 16:51
  • Generally, the code that is executed by QGIS buttons is written in C++ and methods like trigger() are offered via the PyQGIS API. You cannot overwrite their source code unless you change the C++ code and compile QGIS. However, it really depends on your use case. You can alter some GUI behavior, such as in my answer. On the other hand, you could also set short cuts to open dialogs and trigger actions. I would suggest you to ask a question per case, until you get the point. It's a bit confusing what you are attempting yo get. – Germán Carrillo Mar 12 '15 at 17:06
  • Thanks for your patience! Your answer is very valuable to me. Regarding the title it's plainly "no", so I don't have to look for a way to achieve it, right? Why I was asking: in ArcView every control had scripts associated calling low-level routines. So between gui and low-level routines there was a layer of scripts that could be manipulated. I'm too much a novice to QGIS to be able to judge if such a layer (in this case python written) wouldn't be a great thing to have, to extend the existing gui functionality more individually. But there are other ways of course, as you had shown before... – Rudi Uhl Mar 12 '15 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.