Last active
July 29, 2016 08:53
-
-
Save minorua/86c22f5298b00c6c7a61e9b2e9b7f46b to your computer and use it in GitHub Desktop.
[QGIS] 地物フォームのデフォルト値設定とフィールド値の自動更新のためのPython初期化コード
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# 地物フォームのデフォルト値設定とフィールド値の自動更新のためのPython初期化コード | |
# 留意点 | |
# - 自動設定フィールドを非表示や編集不可にするとフォームで値を設定できなかったり | |
# 地物に値が反映されなくなったりしますので自動設定の対象フィールドは編集可能な | |
# テキスト編集のままにします。 | |
# - ドラッグアンドドロップデザイナを使えばフォーム内のフィールドの順番を変えたり、 | |
# 自動入力を行うフィールドを別のタブに入れるといったことも可能です。 | |
# - プロパティダイアログに関数名(my_form_open)を入力するのを忘れないで下さい。 | |
# - QGIS 2.14および2.16で動作確認しています。 | |
from PyQt4.QtCore import QObject, qDebug | |
from PyQt4.QtGui import QLineEdit | |
#from qgis.gui import QgsAttributeForm | |
import qgis | |
if not hasattr(qgis, "myLinkedWidgets"): | |
qgis.myLinkedWidgets = [] | |
my_debug_message = qDebug | |
class LinkedWidgets(QObject): | |
def __init__(self, parent, win1, win2, wout): | |
QObject.__init__(self, parent) | |
self.win1 = win1 | |
self.win2 = win2 | |
self.wout = wout | |
win1.textChanged.connect(self.valueChanged) | |
win2.textChanged.connect(self.valueChanged) | |
self.destroyed.connect(self._destroyed) | |
self.valueChanged() | |
def _destroyed(self, obj=None): | |
qgis.myLinkedWidgets.remove(self) | |
my_debug_message("destroyed: {0} remains".format(len(qgis.myLinkedWidgets))) | |
def valueChanged(self, text=None): | |
try: | |
self.wout.setText("{0:04d}{1:04d}".format(int(self.win1.text()), int(self.win2.text()))) | |
# TODO: 任意のフォーマットに変更。04dは数値を0埋めで4桁の文字列にするという意味。 | |
# http://docs.python.jp/2/library/string.html#format-specification-mini-language を参照のこと。 | |
except: | |
pass | |
def my_form_open(dialog, layer, feature): | |
# if dialog.mode() == QgsAttributeForm.AddFeatureMode: | |
attrset = set(feature.attributes()) | |
attrset.discard(None) | |
if not attrset: | |
# TODO: フィールド名とデフォルト値の組の追加/削除 | |
dialog.findChild(QLineEdit, u"nendo").setText(u"2016") | |
dialog.findChild(QLineEdit, u"chiku").setText(u"100") | |
w = LinkedWidgets(dialog, | |
dialog.findChild(QLineEdit, u"nendo"), # TODO: 入力フィールド1名の設定 | |
dialog.findChild(QLineEdit, u"chiku"), # TODO: 入力フィールド2名の設定 | |
dialog.findChild(QLineEdit, u"ID")) # TODO: 出力フィールド名の設定 | |
qgis.myLinkedWidgets.append(w) | |
my_debug_message("my_form_open: {0} forms".format(len(qgis.myLinkedWidgets))) | |
my_debug_message("script loaded") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment