Last active
March 1, 2018 13:00
-
-
Save usernaamee/48600a5067cb2811dfaae81c6fc66be0 to your computer and use it in GitHub Desktop.
Dock widget based plugin builder for QGIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Replace following four appropriately first: | |
yourname yourcompany http://yourcompany.com http://yourcompany.com/logo.png | |
""" | |
import os | |
import sys | |
import urllib | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('plugin_name') | |
args = parser.parse_args() | |
base_dir = args.plugin_name | |
if os.path.exists(base_dir): | |
print '\n +++ FATAL ERROR: Plugin dir already exists\n' | |
sys.exit() | |
os.mkdir(base_dir) | |
metadata_txt = """ | |
; the next section is mandatory | |
[general] | |
name="""+args.plugin_name+""" | |
email=yourname@company | |
author=yourname | |
qgisMinimumVersion=2.0 | |
description=Plugin creates a dock | |
about=Plugin creates a dock | |
version=version 2.0 | |
tracker= | |
repository= | |
; end of mandatory metadata | |
; start of optional metadata | |
changelog=First version? | |
; Tags are in comma separated value format, spaces are allowed within the | |
; tag name. | |
; Tags should be in English language. Please also check for existing tags and | |
; synonyms before creating a new one. | |
tags= | |
; these metadata can be empty, they will eventually become mandatory. | |
homepage=http://yourcompany.com | |
icon=logo.png | |
; experimental flag (applies to the single version) | |
experimental=False | |
; deprecated flag (applies to the whole plugin and not only to the uploaded version) | |
deprecated=False | |
; if empty, it will be automatically set to major version + .99 | |
qgisMaximumVersion= | |
""" | |
open(os.path.join(base_dir, 'metadata.txt'), 'w').write(metadata_txt) | |
urllib.urlretrieve('http://yourcompany.com/logo.png', os.path.join(base_dir, 'logo.png')) | |
init_txt=""" | |
def classFactory(iface): | |
from """+args.plugin_name+""" import """+args.plugin_name+""" | |
return """+args.plugin_name+"""(iface) | |
""" | |
open(os.path.join(base_dir, '__init__.py'), 'w').write(init_txt) | |
resources_txt="""<RCC> | |
<qresource prefix=\"/plugins/"""+args.plugin_name+"""\" > | |
<file>logo.png</file> | |
</qresource> | |
</RCC> | |
""" | |
open(os.path.join(base_dir, 'resources.qrc'), 'w').write(resources_txt) | |
ui_txt="""<?xml version=\"1.0\" encoding=\"UTF-8\"?> | |
<ui version=\"4.0\"> | |
<class>"""+args.plugin_name+"""</class> | |
<widget class=\"QDockWidget\" name=\""""+args.plugin_name+"""\"> | |
<property name=\"geometry\"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>240</width> | |
<height>320</height> | |
</rect> | |
</property> | |
<property name=\"windowTitle\"> | |
<string>"""+args.plugin_name+"""</string> | |
</property> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> | |
""" | |
open(os.path.join(base_dir, 'Ui_'+args.plugin_name+'.ui'), 'w').write(ui_txt) | |
dock_txt=""" | |
from PyQt4 import QtCore, QtGui | |
from Ui_"""+args.plugin_name+""" import Ui_"""+args.plugin_name+""" | |
class """+args.plugin_name+"""Dock(QtGui.QDockWidget): | |
def __init__(self): | |
QtGui.QDockWidget.__init__(self) | |
self.ui = Ui_"""+args.plugin_name+"""() | |
self.ui.setupUi(self) | |
""" | |
open(os.path.join(base_dir, args.plugin_name+'Dock.py'), 'w').write(dock_txt) | |
main_txt=""" | |
import os | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from qgis.core import * | |
import resources | |
from """+args.plugin_name+"""Dock import """+args.plugin_name+"""Dock | |
class """+args.plugin_name+""": | |
def __init__(self, iface): | |
self.iface = iface | |
self.canvas = iface.mapCanvas() | |
self.mapRenderer = self.canvas.mapRenderer() | |
self.plugin_dir = os.path.dirname(__file__) | |
self.dock = """+args.plugin_name+"""Dock() | |
def initGui(self): | |
self.action = QAction(QIcon(\":/plugins/"""+args.plugin_name+"""/logo.png\"), \""""+args.plugin_name+"""\", self.iface.mainWindow()) | |
self.action.triggered.connect(self.run) | |
self.iface.addToolBarIcon(self.action) | |
self.iface.addDockWidget( Qt.RightDockWidgetArea, self.dock ) | |
def unload(self): | |
self.iface.removeDockWidget(self.dock) | |
self.iface.removeToolBarIcon(self.action) | |
def run(self): | |
self.dock.show() | |
""" | |
open(os.path.join(base_dir, args.plugin_name+'.py'), 'w').write(main_txt) | |
make_txt=""" | |
UI_FILES = Ui_"""+args.plugin_name+""".py | |
RESOURCE_FILES = resources.py | |
default: compile | |
compile: $(UI_FILES) $(RESOURCE_FILES) | |
%.py : %.qrc | |
pyrcc4 -o $@ $< | |
%.py : %.ui | |
pyuic4 -o $@ $< | |
""" | |
open(os.path.join(base_dir, 'Makefile'), 'w').write(make_txt) | |
os.system('cd '+args.plugin_name+'; make;') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment