I am trying to execute a code on Left Click and Right Click should stop code execution and reopen the plugin dialog.
My code: This class is created for mouse click on the map. Here I do not really understand the way to stop plugin, instead I am trying to relaunch plugin dialog window to be able to add some variables in it later.
class PointTool(QgsMapTool):
def __init__(self, canvas):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.iface = iface
def canvasReleaseEvent(self, event):
#Get the click
if event.button() == QtCore.Qt.RightButton:
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
x1=point[0]
y1=point[1]
self.iface.messageBar().pushMessage("RIGHT BUTTON COORDINATES: "+str(round(point[0],1))+","+str(round(point[1],1))+"</a>", duration=7)
##### HERE I AM TRYING TO RELAUNCH THE PLUGIN DIALOG
rightaction = myplag(iface)
rightaction.run()
elif event.button() == QtCore.Qt.LeftButton:
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
x1=point[0]
y1=point[1]
self.iface.messageBar().pushMessage("LEFT BUTTON COORDINATES: "+str(round(point[0],1))+","+str(round(point[1],1))+"</a>", duration=7)
and below is a standard plugin class that I edited in def run(self):
class myplag:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'myplag_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&myplag')
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
# noinspection PyMethodMayBeStatic
def tr(self, message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('myplag', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
# Adds plugin icon to Plugins toolbar
self.iface.addToolBarIcon(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/myplag/icon.png'
self.add_action(
icon_path,
text=self.tr(u''),
callback=self.run,
parent=self.iface.mainWindow())
# will be set False in run()
self.first_start = True
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&myplag'),
action)
self.iface.removeToolBarIcon(action)
# def display_point(pointTool):
# try:
# print(pointTool.x(), pointTool.y())
# except AttributeError:
# pass
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.dlg = myplagDialog()
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# a reference to our map canvas
tool = PointTool(iface.mapCanvas())
iface.mapCanvas().setMapTool(tool)
When I Right click, it gives me error:
'myplag' object has no attribute 'dlg'