Skip to content

Commit a30161d

Browse files
authored
Merge pull request #11 from dayjournal/dev
Dev
2 parents 720a5b5 + e696192 commit a30161d

7 files changed

+309
-113
lines changed

AttributeAssignment.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
def _fromUtf8(s):
3636
return s
3737

38+
3839
class AttributeAssignment:
3940
def __init__(self, iface):
4041
self.iface = iface
4142
self.canvas = self.iface.mapCanvas()
4243
self.dlg = AttributeAssignmentDialog()
43-
self.dlg.hide()
44+
self.dlg.hide()
4445
self.plugin_dir = os.path.dirname(__file__)
4546
locale = QSettings().value('locale/userLocale')[0:2]
4647
locale_path = os.path.join(
@@ -56,19 +57,21 @@ def __init__(self, iface):
5657
self.menu = self.tr(u'&AttributeAssignment')
5758
self.toolbar = self.iface.addToolBar(u'AttributeAssignment')
5859
self.toolbar.setObjectName(u'AttributeAssignment')
60+
5961
def tr(self, message):
6062
return QCoreApplication.translate('AttributeAssignment', message)
63+
6164
def add_action(
62-
self,
63-
icon_path,
64-
text,
65-
callback,
66-
enabled_flag=True,
67-
add_to_menu=True,
68-
add_to_toolbar=True,
69-
status_tip=None,
70-
whats_this=None,
71-
parent=None):
65+
self,
66+
icon_path,
67+
text,
68+
callback,
69+
enabled_flag=True,
70+
add_to_menu=True,
71+
add_to_toolbar=True,
72+
status_tip=None,
73+
whats_this=None,
74+
parent=None):
7275
icon = QIcon(icon_path)
7376
action = QAction(icon, text, parent)
7477
action.triggered.connect(callback)
@@ -85,45 +88,49 @@ def add_action(
8588
action)
8689
self.actions.append(action)
8790
return action
91+
8892
def initGui(self):
8993
icon_path = ':/plugins/AttributeAssignment/icon.png'
9094
self.add_action(
9195
icon_path,
9296
text=self.tr(u'AttributeAssignment'),
9397
callback=self.run,
9498
parent=self.iface.mainWindow())
99+
95100
def unload(self):
96101
for action in self.actions:
97102
self.iface.removePluginMenu(
98103
self.tr(u'&AttributeAssignment'),
99104
action)
100105
self.iface.removeToolBarIcon(action)
101106
del self.toolbar
107+
102108
def run(self):
103109
self.dlg.show()
104-
self.dlg.mMapLayerComboBox.setLayer(self.iface.layerTreeView().currentLayer())
110+
self.dlg.mMapLayerComboBox.setLayer(
111+
self.iface.layerTreeView().currentLayer())
105112
self.toolClick = QgsMapToolClick(self.iface, self.canvas, self.dlg)
106113
self.canvas.setMapTool(self.toolClick)
107114

115+
108116
class QgsMapToolClick(QgsMapTool):
109117
def __init__(self, iface, canvas, dlg):
110118
QgsMapTool.__init__(self, canvas)
111119
self.iface = iface
112120
self.canvas = canvas
113121
self.dlg = dlg
122+
114123
def canvasPressEvent(self, mouseEvent):
115-
layer = self.dlg.mMapLayerComboBox.currentText()
124+
layer = self.dlg.mMapLayerComboBox.currentLayer()
116125
fieldname = self.dlg.mFieldComboBox.currentText()
117-
textvalue = self.dlg.lineEdit_text.text()
118-
layers = QgsProject.instance().mapLayers()
119-
for k,v in layers.items():
120-
if v.name() == layer:
121-
layer = v
122-
else:
123-
pass
126+
if self.dlg.wrapper is not None:
127+
value = self.dlg.wrapper.value()
128+
else:
129+
value = None
124130
if not layer or layer.type() != QgsMapLayer.VectorLayer:
125131
QMessageBox.warning(None, u"Error", u"This is not a vector layer.")
126132
return
133+
127134
mPosBefore = mouseEvent.mapPoint()
128135
layerCRS = layer.crs()
129136
destcrs = self.iface.mapCanvas().mapSettings().destinationCrs()
@@ -136,13 +143,13 @@ def canvasPressEvent(self, mouseEvent):
136143
mPos.y() + width)
137144
layer.startEditing()
138145
rectadd = layer.getFeatures(QgsFeatureRequest().setFilterRect(rect))
139-
featureid = "";
146+
featureid = None
140147
for f in rectadd:
141148
attrs = f.attributes()
142149
findex = f.fieldNameIndex(fieldname)
143150
featureid = f.id()
144-
if featureid != "":
145-
layer.changeAttributeValue(featureid, findex, textvalue)
151+
if featureid is not None:
152+
layer.changeAttributeValue(featureid, findex, value)
146153
else:
147154
QMessageBox.warning(None, u"Error", u"This is not a feature.")
148155
layer.triggerRepaint()

AttributeAssignment_base.py

Lines changed: 105 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,105 @@
1-
# -*- coding: utf-8 -*-
2-
"""
3-
/***************************************************************************
4-
AttributeAssignment
5-
A QGIS plugin
6-
Easy to assign an attribute on QGIS
7-
-------------------
8-
begin : 2018-03-14
9-
git sha : $Format:%H$
10-
copyright : (C) 2018 by Yasunori Kirimoto
11-
12-
license : GNU General Public License v2.0
13-
***************************************************************************/
14-
"""
15-
16-
from PyQt5 import QtCore, QtGui, QtWidgets
17-
18-
try:
19-
_fromUtf8 = QtCore.QString.fromUtf8
20-
except AttributeError:
21-
def _fromUtf8(s):
22-
return s
23-
24-
try:
25-
_encoding = QtWidgets.QApplication.UnicodeUTF8
26-
def _translate(context, text, disambig):
27-
return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
28-
except AttributeError:
29-
def _translate(context, text, disambig):
30-
return QtWidgets.QApplication.translate(context, text, disambig)
31-
32-
class Ui_AttributeAssignment(object):
33-
def setupUi(self, AttributeAssignment):
34-
AttributeAssignment.setObjectName(_fromUtf8("AttributeAssignment"))
35-
AttributeAssignment.resize(240, 220)
36-
AttributeAssignment.move(50, 125)
37-
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
38-
self.label_layer = QtWidgets.QLabel(AttributeAssignment)
39-
self.label_layer.setGeometry(QtCore.QRect(20, 15, 201, 21))
40-
font = QtGui.QFont()
41-
font.setPointSize(10)
42-
self.label_layer.setFont(font)
43-
self.label_layer.setObjectName(_fromUtf8("label_layer"))
44-
self.mMapLayerComboBox = QgsMapLayerComboBox(AttributeAssignment)
45-
self.mMapLayerComboBox.setGeometry(QtCore.QRect(20, 35, 200, 25))
46-
font = QtGui.QFont()
47-
font.setPointSize(10)
48-
self.mMapLayerComboBox.setFont(font)
49-
self.mMapLayerComboBox.setObjectName(_fromUtf8("mMapLayerComboBox"))
50-
self.label_field = QtWidgets.QLabel(AttributeAssignment)
51-
self.label_field.setGeometry(QtCore.QRect(20, 75, 201, 21))
52-
font = QtGui.QFont()
53-
font.setPointSize(10)
54-
self.label_field.setFont(font)
55-
self.label_field.setObjectName(_fromUtf8("label_field"))
56-
self.mFieldComboBox = QgsFieldComboBox(AttributeAssignment)
57-
self.mFieldComboBox.setGeometry(QtCore.QRect(20, 95, 200, 25))
58-
font = QtGui.QFont()
59-
font.setPointSize(10)
60-
self.mFieldComboBox.setFont(font)
61-
self.mFieldComboBox.setObjectName(_fromUtf8("mFieldComboBox"))
62-
self.label_text = QtWidgets.QLabel(AttributeAssignment)
63-
self.label_text.setGeometry(QtCore.QRect(20, 140, 201, 21))
64-
font = QtGui.QFont()
65-
font.setPointSize(10)
66-
self.label_text.setFont(font)
67-
self.label_text.setObjectName(_fromUtf8("label_text"))
68-
self.lineEdit_text = QtWidgets.QLineEdit(AttributeAssignment)
69-
self.lineEdit_text.setGeometry(QtCore.QRect(20, 160, 200, 25))
70-
font = QtGui.QFont()
71-
font.setPointSize(10)
72-
self.lineEdit_text.setFont(font)
73-
self.lineEdit_text.setObjectName(_fromUtf8("lineEdit_text"))
74-
self.retranslateUi(AttributeAssignment)
75-
# QtCore.QObject.connect(self.mMapLayerComboBox, QtCore.SIGNAL(_fromUtf8("layerChanged(QgsMapLayer*)")), self.mFieldComboBox.setLayer)
76-
self.mMapLayerComboBox.layerChanged.connect(self.mFieldComboBox.setLayer)
77-
QtCore.QMetaObject.connectSlotsByName(AttributeAssignment)
78-
79-
def retranslateUi(self, AttributeAssignment):
80-
AttributeAssignment.setWindowTitle(_translate("AttributeAssignment", u"AttributeAssignment", None))
81-
self.label_layer.setText(_translate("AttributeAssignment", u"Layer", None))
82-
self.label_field.setText(_translate("AttributeAssignment", u"Field", None))
83-
self.label_text.setText(_translate("AttributeAssignment", u"Value", None))
84-
85-
from qgis.gui import QgsFieldComboBox, QgsMapLayerComboBox
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
AttributeAssignment
5+
A QGIS plugin
6+
Easy to assign an attribute on QGIS
7+
-------------------
8+
begin : 2018-03-14
9+
git sha : $Format:%H$
10+
copyright : (C) 2018 by Yasunori Kirimoto
11+
12+
license : GNU General Public License v2.0
13+
***************************************************************************/
14+
"""
15+
16+
from qgsfieldcombobox import QgsFieldComboBox
17+
from qgsmaplayercombobox import QgsMapLayerComboBox
18+
from PyQt5 import QtCore, QtGui, QtWidgets
19+
20+
21+
class Ui_AttributeAssignment(object):
22+
def setupUi(self, AttributeAssignment):
23+
AttributeAssignment.setObjectName("AttributeAssignment")
24+
AttributeAssignment.setEnabled(True)
25+
AttributeAssignment.resize(240, 220)
26+
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
27+
self.formLayout = QtWidgets.QFormLayout(AttributeAssignment)
28+
self.formLayout.setFieldGrowthPolicy(
29+
QtWidgets.QFormLayout.AllNonFixedFieldsGrow)
30+
self.formLayout.setRowWrapPolicy(QtWidgets.QFormLayout.DontWrapRows)
31+
self.formLayout.setContentsMargins(20, 20, 20, 20)
32+
self.formLayout.setSpacing(10)
33+
self.formLayout.setObjectName("formLayout")
34+
self.label_layer = QtWidgets.QLabel(AttributeAssignment)
35+
font = QtGui.QFont()
36+
font.setPointSize(10)
37+
self.label_layer.setFont(font)
38+
self.label_layer.setObjectName("label_layer")
39+
self.formLayout.setWidget(
40+
0, QtWidgets.QFormLayout.LabelRole, self.label_layer)
41+
self.mMapLayerComboBox = QgsMapLayerComboBox(AttributeAssignment)
42+
sizePolicy = QtWidgets.QSizePolicy(
43+
QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
44+
sizePolicy.setHorizontalStretch(0)
45+
sizePolicy.setVerticalStretch(25)
46+
sizePolicy.setHeightForWidth(
47+
self.mMapLayerComboBox.sizePolicy().hasHeightForWidth())
48+
self.mMapLayerComboBox.setSizePolicy(sizePolicy)
49+
self.mMapLayerComboBox.setAllowEmptyLayer(False)
50+
self.mMapLayerComboBox.setShowCrs(False)
51+
self.mMapLayerComboBox.setObjectName("mMapLayerComboBox")
52+
self.formLayout.setWidget(
53+
1, QtWidgets.QFormLayout.SpanningRole, self.mMapLayerComboBox)
54+
self.label_field = QtWidgets.QLabel(AttributeAssignment)
55+
font = QtGui.QFont()
56+
font.setPointSize(10)
57+
self.label_field.setFont(font)
58+
self.label_field.setObjectName("label_field")
59+
self.formLayout.setWidget(
60+
3, QtWidgets.QFormLayout.LabelRole, self.label_field)
61+
self.mFieldComboBox = QgsFieldComboBox(AttributeAssignment)
62+
sizePolicy = QtWidgets.QSizePolicy(
63+
QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
64+
sizePolicy.setHorizontalStretch(0)
65+
sizePolicy.setVerticalStretch(25)
66+
sizePolicy.setHeightForWidth(
67+
self.mFieldComboBox.sizePolicy().hasHeightForWidth())
68+
self.mFieldComboBox.setSizePolicy(sizePolicy)
69+
self.mFieldComboBox.setObjectName("mFieldComboBox")
70+
self.formLayout.setWidget(
71+
4, QtWidgets.QFormLayout.SpanningRole, self.mFieldComboBox)
72+
self.label_value = QtWidgets.QLabel(AttributeAssignment)
73+
font = QtGui.QFont()
74+
font.setPointSize(10)
75+
self.label_value.setFont(font)
76+
self.label_value.setObjectName("label_value")
77+
self.formLayout.setWidget(
78+
6, QtWidgets.QFormLayout.LabelRole, self.label_value)
79+
self.mValuePlaceholder = QtWidgets.QWidget(AttributeAssignment)
80+
sizePolicy = QtWidgets.QSizePolicy(
81+
QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
82+
sizePolicy.setHorizontalStretch(0)
83+
sizePolicy.setVerticalStretch(25)
84+
sizePolicy.setHeightForWidth(
85+
self.mValuePlaceholder.sizePolicy().hasHeightForWidth())
86+
self.mValuePlaceholder.setSizePolicy(sizePolicy)
87+
self.mValuePlaceholder.setObjectName("mValuePlaceholder")
88+
self.formLayout.setWidget(
89+
7, QtWidgets.QFormLayout.SpanningRole, self.mValuePlaceholder)
90+
self.label_layer.raise_()
91+
self.mValuePlaceholder.raise_()
92+
self.label_field.raise_()
93+
self.mMapLayerComboBox.raise_()
94+
self.mFieldComboBox.raise_()
95+
self.label_value.raise_()
96+
self.retranslateUi(AttributeAssignment)
97+
QtCore.QMetaObject.connectSlotsByName(AttributeAssignment)
98+
99+
def retranslateUi(self, AttributeAssignment):
100+
_translate = QtCore.QCoreApplication.translate
101+
AttributeAssignment.setWindowTitle(_translate(
102+
"AttributeAssignment", "AttributeAssignment"))
103+
self.label_layer.setText(_translate("AttributeAssignment", "Layer"))
104+
self.label_field.setText(_translate("AttributeAssignment", "Field"))
105+
self.label_value.setText(_translate("AttributeAssignment", "Value"))

0 commit comments

Comments
 (0)