waigani's diary

QGISを中心にFOSS4Gをいじくる

QGISでプラグインを作成する その4

もう少しプラグインらしく

プラグインのダイアログからQGIS側の情報を取ってみます
まずは簡単にレイヤー数だけ

ソース一式を貼っておきます

この記事で作成したソース一式です
plugintest.zip 直

ダイアログの変更

前回のダイアログにボタンを1つ追加しておきます

追加したボタンが押された際に、レイヤー数を表示するようにします

作成したdialogをui_dialog.uiという名前で保存しておき、pythonコンパイルします

 pyuic4 -o ui_dialog.py ui_dialog.ui

中身はこんな感じになります

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_dialog.ui'
#
# Created: Fri Jun 03 22:25:35 2011
#      by: PyQt4 UI code generator 4.8.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(300, 100)
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(10, 10, 281, 51))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.button1 = QtGui.QPushButton(Dialog)
        self.button1.setGeometry(QtCore.QRect(174, 70, 111, 23))
        self.button1.setObjectName(_fromUtf8("button1"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.button1.setText(QtGui.QApplication.translate("Dialog", "CountLayerNum", None, QtGui.QApplication.UnicodeUTF8))

ダイアログインスタンス作成時の動作変更

ダイアログを作成する際に、Qgis.ui.iface(QgisInterface Class)を渡すようにplugin.pyを変更しておきます
下から3行目のところです

# -*- coding: utf-8 -*-
#Import the PyOt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
#Initialize Qt resources from file resources.py
import resources_rc

class pluginTest:

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

  def initGui(self):
    self.action = QAction(QIcon(":/icon/qgis-icon.png"), "test", self.iface.mainWindow())
    QObject.connect(self.action, SIGNAL("triggered()"), self.run)
  
    self.iface.addToolBarIcon(self.action)
    self.iface.addPluginToMenu("test", self.action)
  
  def unload(self):
    self.iface.removePluginMenu("test",self.action)
    self.iface.removeToolBarIcon(self.action)
    
  def run(self):
    from dialog import Dialog 

    dlg = Dialog(self.iface)
    dlg.show()
    dlg.exec_() 

レイヤー数表示動作の追加

dialog.pyにボタンを押した際の動作を追加します

# -*- coding: utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import * 
from ui_dialog import Ui_Dialog 
from qgis.core import *

class Dialog(QDialog): 
  def __init__(self, iface): 
    QDialog.__init__(self) 
    
    self.iface = iface
    
    self.ui = Ui_Dialog() 
    self.ui.setupUi(self)
    
    QObject.connect(self.ui.button1, SIGNAL("clicked()"), self.pushButton)

  def pushButton(self):
    layerNum = str(self.iface.mapCanvas().layerCount())
    self.ui.textEdit.setText(layerNum)

その他のソース

__init__.pyとresources_rc.pyはそのまま変更なしです

動作させると

プラグインを起動してボタンを押すと、レイヤー数を表示します

この時のレイヤー数は表示されているレイヤー数になります
非表示のレイヤーはカウントされません