Python programlama dilinde eklenti oluşturmak mümkündür. C++ ile yazılan klasik eklentilerle karşılaştırıldığında, Python dilinin dinamik doğası gereği Python da eklenti yazmak, anlamak, bakımı ve dağıtımı daha kolaydır.
QGIS eklenti yönetiminde Python eklentileri C++ eklentileri ile birlikte listelenir. Bu yoldan araştırılırlar.
Since the introduction of Python plugins in QGIS, a number of plugins have
appeared - on Plugin Repositories wiki page
you can find some of them, you can use their source to learn more about
programming with PyQGIS or find out whether you are not duplicating development
effort. The QGIS team also maintains an Resimi python eklenti kaynağı.
Ready to create a plugin but no idea what to do? Python Plugin Ideas wiki
page lists wishes from the
community!
Örnek eklentimizin dizin yapısı şu şekilde
PYTHON_PLUGINS_PATH/
MyPlugin/
__init__.py --> *required*
mainPlugin.py --> *required*
metadata.txt --> *required*
resources.qrc --> *likely useful*
resources.py --> *compiled version, likely useful*
form.ui --> *likely useful*
form.py --> *compiled version, likely useful*
Dosyaların anlamı nedir:
- __init__.py = The starting point of the plugin. It has to have the
classFactory() method and may have any other initialisation code.
- mainPlugin.py = The main working code of the plugin. Contains all
the information about the actions of the plugin and the main code.
- resources.qrc = The .xml document created by Qt Designer. Contains
relative paths to resources of the forms.
:dosya:`kaynaklar.py` = .qrc dosyasının çevirisi yukarıda Python da anlatılmıştır.
:dosya:`form.ui`= KGA Qt Tasarımcı tarafından oluşturuldu.
:dosya:`form.py` = form.ui nin çevirisi yukarıda Python da anlatılmıştır.
- metadata.txt = Required for QGIS >= 1.8.0. Contains general info,
version, name and some other metadata used by plugins website and plugin
infrastructure. Since QGIS 2.0 the metadata from __init__.py are not
accepted anymore and the metadata.txt is required.
Here
is an online automated way of creating the basic files (skeleton) of a typical
QGIS Python plugin.
Also there is a QGIS plugin called Plugin Builder
that creates plugin template from QGIS and doesn’t require internet connection.
This is the recommended option, as it produces 2.0 compatible sources.
Burada dosya yapısı yukarıda anlatılan her bir dosyaya ne eklemek istediğinizle ilgili bilgi ve örnekler bulabilirsiniz.
This file is required by Python’s import system. Also, QGIS requires that this
file contains a classFactory() function, which is called when the
plugin gets loaded to QGIS. It receives reference to instance of
QgisInterface and must return instance of your plugin’s class from the
mainplugin.py — in our case it’s called TestPlugin (see below).
This is how __init__.py should look like
def classFactory(iface):
from mainPlugin import TestPlugin
return TestPlugin(iface)
## any other initialisation needed
Büyünün olduğu ve nasıl göründüğü yer burasıdır:(:dosya:`anaEklenti.py`)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# initialize Qt resources from file resources.py
import resources
class TestPlugin:
def __init__(self, iface):
# save reference to the QGIS interface
self.iface = iface
def initGui(self):
# create action that will start plugin configuration
self.action = QAction(QIcon(":/plugins/testplug/icon.png"), "Test plugin", self.iface.mainWindow())
self.action.setObjectName("testAction")
self.action.setWhatsThis("Configuration for test plugin")
self.action.setStatusTip("This is status tip")
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
# add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&Test plugins", self.action)
# connect to signal renderComplete which is emitted when canvas
# rendering is done
QObject.connect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
def unload(self):
# remove the plugin menu item and icon
self.iface.removePluginMenu("&Test plugins", self.action)
self.iface.removeToolBarIcon(self.action)
# disconnect form signal of the canvas
QObject.disconnect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
def run(self):
# create and show a configuration dialog or something similar
print "TestPlugin: run called!"
def renderTest(self, painter):
# use painter for drawing to map canvas
print "TestPlugin: renderTest called!"
ana eklenti kaynak dosyasında olması gerekli olan tek eklenti fonksiyonları(:dosya:`anaEklenti.py gibi`)
You can see that in the above example, the addPluginToMenu() is used.
This will add the corresponding menu action to the
menu. Alternative methods exist to add the action to a different menu. Here is
a list of those methods:
RasterMenüyeEklentiekle()
VektörMenüyeEklentiekle()
VeriTabanıMenüyeEklentiekle()
WebMenüyeEklentiekle()
Onların hepsi MenüyeEklentiekle() metodu ile aynı sentaksa sahiptir.
Adding your plugin menu to one of those predefined method is recommended to
keep consistency in how plugin entries are organized. However, you can add your
custom menu group directly to the menu bar, as the next example demonstrates:
def initGui(self):
self.menu = QMenu(self.iface.mainWindow())
self.menu.setObjectName("testMenu")
self.menu.setTitle("MyMenu")
self.action = QAction(QIcon(":/plugins/testplug/icon.png"), "Test plugin", self.iface.mainWindow())
self.action.setObjectName("testAction")
self.action.setWhatsThis("Configuration for test plugin")
self.action.setStatusTip("This is status tip")
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
self.menu.addAction(self.action)
menuBar = self.iface.mainWindow().menuBar()
menuBar.insertMenu(self.iface.firstRightStandardMenu().menuAction(), self.menu)
def unload(self):
self.menu.deleteLater()
Don’t forget to set QAction and QMenu objectName to a name
specific to your plugin so that it can be customized.
Kaynak dosyadan ikon(:dosya:’kaynaklar.qrc’ olarak adlandırılır) kullanıyoruz, bunu:func:initGui() de görebilirsiniz.
<RCC>
<qresource prefix="/plugins/testplug" >
<file>icon.png</file>
</qresource>
</RCC>
It is good to use a prefix that will not collide with other plugins or any
parts of QGIS, otherwise you might get resources you did not want. Now you
just need to generate a Python file that will contain the resources. It’s
done with pyrcc4 command:
pyrcc4 -o resources.py resources.qrc
Not
In Windows environments, attempting to run the pyrcc4 from
Command Prompt or Powershell will probably result in the error “Windows
cannot access the specified device, path, or file [...]”. The easiest
solution is probably to use the OSGeo4W Shell but if you are comfortable
modifying the PATH environment variable or specifiying the path to the
executable explicitly you should be able to find it at
<Your QGIS Install Directory>\bin\pyrcc4.exe.
Hepsi bu... zor birşey yok :)
If you’ve done everything correctly you should be able to find and load
your plugin in the plugin manager and see a message in console when toolbar
icon or appropriate menu item is selected.
Gerçek bir eklenti üzerinde çalışırken çalışan başka bir (çalışan) adres defterinde eklenti yazmanın yolu ve UI + kaynak dosya oluşturan ve QGIS kurulumunuzun eklentisini kuran bir makefile oluşturmaktır.
Eklenti dokümantasyonu HTML yardım dosyası şeklinde yazılabilir.:mod:qgis.utils modülü yardım dosyası tarayıcısı showPluginHelp() fonksiyonunu sağlar, diğer QGIS yardımlarıda aynı şekildedir.
The showPluginHelp() function looks for help files in the same
directory as the calling module. It will look for, in turn,
index-ll_cc.html, index-ll.html, index-en.html,
index-en_us.html and index.html, displaying whichever it finds
first. Here ll_cc is the QGIS locale. This allows multiple translations of
the documentation to be included with the plugin.
The showPluginHelp() function can also take parameters packageName,
which identifies a specific plugin for which the help will be displayed,
filename, which can replace “index” in the names of files being searched,
and section, which is the name of an html anchor tag in the document
on which the browser will be positioned.
With a few steps you can set up the environment for the plugin localization so
that depending on the locale settings of your computer the plugin will be loaded
in different languages.
The easiest way to create and manage all the translation files is to install
Qt Linguist.
In a Debian-based GNU/Linux environment you can install it typing:
sudo apt-get install qt4-dev-tools
When you create the plugin you will find the i18n folder within the main
plugin directory.
All the translation files have to be within this directory.
First you should create a .pro file, that is a project file that can be
managed by Qt Linguist.
In this .pro file you have to specify all the files and forms you want to
translate. This file is used to set up the localization files and variables.
A possible project file, matching the structure of our
example plugin:
FORMS = ../form.ui
SOURCES = ../your_plugin.py
TRANSLATIONS = your_plugin_it.ts
Your plugin might follow a more complex structure, and it might be distributed
across several files. If this is the case, keep in mind that pylupdate4,
the program we use to read the .pro file and update the translatable string,
does not expand wild card characters, so you need to place every file explicitly
in the .pro file.
Your project file might then look like something like this:
FORMS = ../ui/about.ui ../ui/feedback.ui \
../ui/main_dialog.ui
SOURCES = ../your_plugin.py ../computation.py \
../utils.py
Furthermore, the your_plugin.py file is the file that calls all the menu
and sub-menus of your plugin in the QGIS toolbar and you want to translate them
all.
Finally with the TRANSLATIONS variable you can specify the translation
languages you want.
Uyarı
Be sure to name the ts file like your_plugin_ + language + .ts
otherwise the language loading will fail! Use 2 letters shortcut for the
language (it for Italian, de for German, etc...)
Once you have created the .pro you are ready to generate the .ts file(s)
of the language(s) of your plugin.
Open a terminal, go to your_plugin/i18n directory and type:
pylupdate4 your_plugin.pro
you should see the your_plugin_language.ts file(s).
Open the .ts file with Qt Linguist and start to translate.
When you finish to translate your plugin (if some strings are not completed the
source language for those strings will be used) you have to create the .qm
file (the compiled .ts file that will be used by QGIS).
Just open a terminal cd in your_plugin/i18n directory and type:
now, in the i18n directory you will see the your_plugin.qm file(s).
Alternatively you can use the makefile to extract messages from python code and
Qt dialogs, if you created your plugin with Plugin Builder.
At the beginning of the Makefile there is a LOCALES variable:
Add the abbreviation of the language to this variable, for example for
Hungarian language:
Now you can generate or update the hu.ts file (and the en.ts too)
from the sources by:
After this, you have updated .ts file for all languages set in the LOCALES
variable.
Use Qt4 Linguist to translate the program messages.
Finishing the translation the .qm files can be created by the transcompile:
You have to distribute .ts files with your plugin.
In order to see the translation of your plugin just open QGIS, change the
language () and restart QGIS.
You should see your plugin in the correct language.
Uyarı
If you change something in your plugin (new UIs, new menu, etc..) you have to
generate again the update version of both .ts and .qm file, so run
again the command of above.