Skip to content

Commit

Permalink
Move plugin to his self toolbar
Browse files Browse the repository at this point in the history
    0.9 (09.11.2020)
        - Move plugin to his self toolbar
  • Loading branch information
kikislater authored Nov 9, 2020
1 parent 6bce7fc commit cf4c775
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.9 (09.11.2020)
- Move plugin to his self toolbar

0.8 (23.05.2020)
- fix import resources

Expand Down
161 changes: 149 additions & 12 deletions generalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,165 @@
from .resources import *
# Import the code for the dialog
from .generalizerdialog import generalizerDialog, getLayersNames
import os.path

class generalizer(object):

class generalizer:

def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface

self.canvas = iface.mapCanvas()

# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)

# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Generalizer')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'Generalizer')
self.toolbar.setObjectName(u'Generalizer')

#print "** INITIALIZING Generalizer"

self.pluginIsActive = False
self.dockwidget = None

self.output_layer = None

# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('Generalizer', 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):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""

icon = QIcon(icon_path)
text = "Generalizer"
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:
self.toolbar.addAction(action)

if add_to_menu:
self.iface.addPluginToVectorMenu(
self.menu,
action)

self.actions.append(action)

return action


def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(QIcon(":/plugins/generalizer/icon.png"), \
"Generalizer", self.iface.mainWindow())
# connect the action to the run method
self.action.triggered.connect( self.run )
"""Create the menu entries and toolbar icons inside the QGIS GUI."""

icon_path = ':/plugins/Generalizer3/icon.png'
self.add_action(
icon_path,
text=self.tr(u''),
callback=self.run,
parent=self.iface.mainWindow())

#--------------------------------------------------------------------------

def onClosePlugin(self):
"""Cleanup necessary items here when plugin dockwidget is closed"""

#print "** CLOSING Generalizer"

# disconnects
self.dockwidget.closingPlugin.disconnect(self.onClosePlugin)

# remove this statement if dockwidget is to remain
# for reuse if plugin is reopened
# Commented next statement since it causes QGIS crashe
# when closing the docked window:
# self.dockwidget = None

self.pluginIsActive = False

# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&Generalizer", self.action)

def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu("&Generalizer",self.action)
self.iface.removeToolBarIcon(self.action)
"""Removes the plugin menu item and icon from QGIS GUI."""

#print "** UNLOAD Generalizer"

for action in self.actions:
self.iface.removePluginVectorMenu(
self.tr(u'&Generalizer'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar

# run method that performs all the real work
def run(self):
Expand Down
4 changes: 3 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ icon=icon.png
tags=line,vector,smooth,simplify

# experimental flag
experimental=True
experimental=False

# deprecated flag (applies to the whole plugin, not just a single version
deprecated=False

changelog=
0.9 (09.11.2020)
- Move plugin to his self toolbar
0.8 (23.05.2020)
- fix import resources
0.7 (17.05.2020)
Expand Down
16 changes: 8 additions & 8 deletions resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Resource object code
#
# Created by: The Resource Compiler for PyQt5 (Qt v5.13.1)
# Created by: The Resource Compiler for PyQt5 (Qt v5.15.1)
#
# WARNING! All changes made in this file will be lost!

Expand Down Expand Up @@ -399,10 +399,10 @@
\x07\x3b\xe0\xb3\
\x00\x70\
\x00\x6c\x00\x75\x00\x67\x00\x69\x00\x6e\x00\x73\
\x00\x0b\
\x08\xd8\x99\x42\
\x00\x67\
\x00\x65\x00\x6e\x00\x65\x00\x72\x00\x61\x00\x6c\x00\x69\x00\x7a\x00\x65\x00\x72\
\x00\x0c\
\x01\x89\x95\x73\
\x00\x47\
\x00\x65\x00\x6e\x00\x65\x00\x72\x00\x61\x00\x6c\x00\x69\x00\x7a\x00\x65\x00\x72\x00\x33\
\x00\x08\
\x0a\x61\x5a\xa7\
\x00\x69\
Expand All @@ -413,7 +413,7 @@
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x14\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\
\x00\x00\x00\x30\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x00\x32\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
"

qt_resource_struct_v2 = b"\
Expand All @@ -423,8 +423,8 @@
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x14\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x30\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x6e\x54\x90\x86\x20\
\x00\x00\x00\x32\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x73\x87\x86\xe3\x8a\
"

qt_version = [int(v) for v in QtCore.qVersion().split('.')]
Expand Down
2 changes: 1 addition & 1 deletion resources.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<RCC>
<qresource prefix="/plugins/generalizer" >
<qresource prefix="/plugins/Generalizer3" >
<file>icon.png</file>
</qresource>
</RCC>

0 comments on commit cf4c775

Please sign in to comment.