2

I want to execute the merge(self,aLayer) method after clicking with the tool I've created, i.e. after the mouse click I want the control to come to the main.py module how can I do that using Python programming?

I have two files one is mouseevent.py and the other one main.py.

class MouseClick(QgsMapTool):
    canvasDoubleClicked = QtCore.pyqtSignal(object, object)

    def __init__(self, canvas,layer,Vertex_list,mergeDlg):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas
        self.layer=layer
        self.Vertex_list=Vertex_list
        self.mergeDlg=mergeDlg
        self.rb = QgsRubberBand(self.canvas, QGis.Line)
        self.rb.setColor(QColor(255, 0, 0))
        self.rb.setWidth(2)
        self.rb.reset()
    def canvasPressEvent(self, event):

        if event.button() == 1:
            print "helo"
            x = event.pos().x()
            y = event.pos().y()
            Lpoint = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
            print 'Then Left points are',Lpoint


    def canvasMoveEvent(self, event):
        pass

    def canvasReleaseEvent(self, event):
        #Get the click
        if event.button() == 2:
            print "hello right"
            x = event.pos().x()
            y = event.pos().y()
            Rpoint = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
            print 'Then Right points are',Rpoint
            self.Vertex_list.append(Rpoint)
            print 'The list after right click is',self.Vertex_list

    def canvasSelectionChanged(self,layer):

        pass
    def activate(self):
        pass

    def deactivate(self):
        pass

    def isZoomTool(self):
        return False

    def isTransient(self):
        return False

    def isEditTool(self):
        return True

Main.py:

def merge(self,aLayer):
    canvas=qgis.utils.iface.mapCanvas()
    aLayer=canvas.currentLayer()
    aLayer.removeSelection()
    selection=aLayer.selectedFeatures()
    print 'length is',len(selection)


def run(self):
    """Run method that performs all the real work"""
    mergeDlg = MergeDialog()
    Vertex_list = []
    global Vertex_list
    MergeByFreeHand.readConfigfile(self)
    AreaList=[]
    Value=99
    canvas=qgis.utils.iface.mapCanvas()
    aLayer=canvas.currentLayer()
    aLayer.removeSelection()
    qgis.utils.iface.setActiveLayer(aLayer)
    count=0
    global count          
    tool = MouseClick(canvas,aLayer,Vertex_list,mergeDlg,count)
    qgis.utils.iface.mapCanvas().setMapTool(tool)
Midavalo
  • 29,696
  • 10
  • 48
  • 104
user99
  • 979
  • 2
  • 15
  • 36
  • You should look into signals and slots in PyQt. – BritishSteel Feb 04 '15 at 13:54
  • I have tried (QObject.connect(tool, SIGNAL("triggered()"),self.merge)) that but not wrking for me can you suggest me any code? – user99 Feb 04 '15 at 14:02
  • I am just teaching myself all this, so it is hard for me to fix this code. But there are some other users here who are really good at PyQGIS. I think that within an hour you should have an answer :-) – BritishSteel Feb 04 '15 at 14:03

1 Answers1

3

You must use PyQt4 SIGNALS/SLOTS for that. The reference documentation is this one.

SIGNALS/SLOTS allow you to keep components isolated in your application while providing you with a way to communicate among them, which is your case.

In your example, do the following steps:

  1. First of all, you don't need to pass the parameter aLayer to your merge() method, do you? So, just leave def merge(self): while defining the method.

  2. You need to define a custom SIGNAL to inform other components when a click has been done using your tool. This way:

    a. Set a new pyqtSignal() object in your MouseClick class:

    class MouseClick(QgsMapTool):
        afterClick = pyqtSignal()
        ...
    

    b. Emit the afterClick signal after your code in the release event (or place it wherever you want, it depends on your needs):

    def canvasReleaseEvent(self, event):
        #Get the click
        if event.button() == 2:
           # Your "if" code goes here
        self.afterClick.emit()
    
  3. In your run() function, after creating the tool object, connect its afterClick signal to your merge() method, this way:

    tool = MouseClick(canvas,aLayer,Vertex_list,mergeDlg,count)
    tool.afterClick.connect(self.merge)
    qgis.utils.iface.mapCanvas().setMapTool(tool)
    
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • It is working perfectly fine.Thank you.. But i want this process to move from Mouseevent.py to Main.py and main.py to Mouseevent.py until i break the loop how can i do that? Bec if i click on the plugin the process is happening only once if i want o execute again i have to click on the plugin to avoid that how can i do? – user99 Feb 05 '15 at 07:55
  • Isn't the tool still active on the map after merge() runs? – Germán Carrillo Feb 05 '15 at 13:04
  • Yes it is not active so i am calling the tool in the if condition again so the tool is active now.That problem is solved. But when i am pressing the ESCAPE button it has to make the tool deactivate.How can i do that using python programming? I have used event.ignore() but it is nt working so is there any way to close the event. – user99 Feb 05 '15 at 13:25