Skip to content

Commit 038da11

Browse files
alexbruynyalldawson
authored andcommitted
[processing] modeler GUI for matrix parameter
1 parent ffa4b04 commit 038da11

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
MatrixModelerWidget.py
6+
---------------------
7+
Date : May 2018
8+
Copyright : (C) 2018 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'May 2018'
22+
__copyright__ = '(C) 2018, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.PyQt import uic
31+
from qgis.PyQt.QtCore import Qt
32+
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
33+
from qgis.PyQt.QtWidgets import QInputDialog
34+
35+
from qgis.core import QgsApplication
36+
37+
pluginPath = os.path.split(os.path.dirname(__file__))[0]
38+
WIDGET, BASE = uic.loadUiType(
39+
os.path.join(pluginPath, 'ui', 'matrixmodelerwidgetbase.ui'))
40+
41+
42+
class MatrixModelerWidget(BASE, WIDGET):
43+
44+
def __init__(self, parent=None):
45+
super(MatrixModelerWidget, self).__init__(parent)
46+
self.setupUi(self)
47+
48+
self.btnAddColumn.setIcon(QgsApplication.getThemeIcon('/mActionNewAttribute.svg'))
49+
self.btnRemoveColumn.setIcon(QgsApplication.getThemeIcon('/mActionDeleteAttribute.svg'))
50+
self.btnAddRow.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
51+
self.btnRemoveRow.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))
52+
self.btnClear.setIcon(QgsApplication.getThemeIcon('/mIconClearText.svg'))
53+
54+
self.btnAddColumn.clicked.connect(self.addColumn)
55+
self.btnRemoveColumn.clicked.connect(self.removeColumns)
56+
self.btnAddRow.clicked.connect(self.addRow)
57+
self.btnRemoveRow.clicked.connect(self.removeRows)
58+
self.btnClear.clicked.connect(self.clearTable)
59+
60+
self.tblView.setModel(QStandardItemModel())
61+
62+
self.tblView.horizontalHeader().sectionDoubleClicked.connect(self.changeHeader)
63+
64+
def addColumn(self):
65+
model = self.tblView.model()
66+
items = [QStandardItem('0') for i in range(model.rowCount())]
67+
model.appendColumn(items)
68+
69+
def removeColumns(self):
70+
indexes = sorted(self.tblView.selectionModel().selectedColumns())
71+
self.tblView.setUpdatesEnabled(False)
72+
for i in reversed(indexes):
73+
self.tblView.model().removeColumns(i.column(), 1)
74+
self.tblView.setUpdatesEnabled(True)
75+
76+
def addRow(self):
77+
model = self.tblView.model()
78+
items = [QStandardItem('0') for i in range(model.columnCount())]
79+
model.appendRow(items)
80+
81+
def removeRows(self):
82+
indexes = sorted(self.tblView.selectionModel().selectedRows())
83+
self.tblView.setUpdatesEnabled(False)
84+
for i in reversed(indexes):
85+
self.tblView.model().removeRows(i.row(), 1)
86+
self.tblView.setUpdatesEnabled(True)
87+
88+
def clearTable(self, removeAll=False):
89+
self.tblView.model().clear()
90+
91+
def changeHeader(self, index):
92+
txt, ok = QInputDialog.getText(self, self.tr("Enter column name"), self.tr("Column name"))
93+
if ok:
94+
self.tblView.model().setHeaderData(index, Qt.Horizontal, txt)
95+
96+
def value(self):
97+
items = []
98+
model = self.tblView.model()
99+
for i in range(model.rowCount()):
100+
row = []
101+
for j in range(model.columnCount()):
102+
item = model.item(i, j)
103+
row.append(item.text())
104+
items.append(row)
105+
106+
return items
107+
108+
def setValue(self, table):
109+
cols = len(table[0])
110+
rows = len(table)
111+
model = QStandardItemModel(rows, cols)
112+
113+
for i in range(rows):
114+
for j in range(cols):
115+
item = QStandardItem(str(table[i][j]))
116+
model.setItem(i, j, item)
117+
self.tblView.setModel(model)
118+
119+
def headers(self):
120+
headers = []
121+
model = self.tblView.model()
122+
for i in range(model.columnCount()):
123+
headers.append(model.headerData(i, Qt.Horizontal))
124+
125+
return headers
126+
127+
def setHeaders(self, headers):
128+
model = self.tblView.model()
129+
model.setHorizontalHeaderLabels(headers)
130+
131+
def fixedRows(self):
132+
return self.chkFixedRows.isChecked()
133+
134+
def setFixedRows(self):
135+
self.chkFixedRows.setChecked(True)

python/plugins/processing/modeler/ModelerParameterDefinitionDialog.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
)
6868

6969
from processing.gui.enummodelerwidget import EnumModelerWidget
70+
from processing.gui.matrixmodelerwidget import MatrixModelerWidget
7071
from processing.core import parameters
7172
from processing.modeler.exceptions import UndefinedParameterException
7273

@@ -283,6 +284,14 @@ def setupUi(self):
283284
self.widget.setDefault(int(self.param.defaultValue()))
284285
self.widget.setAllowMultiple(bool(self.param.allowMultiple()))
285286
self.verticalLayout.addWidget(self.widget)
287+
elif self.paramType == parameters.PARAMETER_MATRIX or \
288+
isinstance(self.param, QgsProcessingParameterMatrix):
289+
self.widget = MatrixModelerWidget(self)
290+
if self.param is not None:
291+
self.widget.setValue(self.param.defaultValue())
292+
self.widget.setHeaders(self.param.headers())
293+
self.widget.setFixedRows(self.param.hasFixedNumberRows())
294+
self.verticalLayout.addWidget(self.widget)
286295

287296
self.verticalLayout.addSpacing(20)
288297
self.requiredCheck = QCheckBox()
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Form</class>
4+
<widget class="QWidget" name="Form">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Form</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout">
17+
<property name="leftMargin">
18+
<number>0</number>
19+
</property>
20+
<property name="topMargin">
21+
<number>0</number>
22+
</property>
23+
<property name="rightMargin">
24+
<number>0</number>
25+
</property>
26+
<property name="bottomMargin">
27+
<number>0</number>
28+
</property>
29+
<item row="0" column="0" rowspan="8">
30+
<widget class="QTableView" name="tblView">
31+
<attribute name="horizontalHeaderStretchLastSection">
32+
<bool>true</bool>
33+
</attribute>
34+
</widget>
35+
</item>
36+
<item row="4" column="1">
37+
<widget class="QToolButton" name="btnRemoveRow">
38+
<property name="toolTip">
39+
<string>Remove row</string>
40+
</property>
41+
<property name="text">
42+
<string>...</string>
43+
</property>
44+
</widget>
45+
</item>
46+
<item row="6" column="1">
47+
<widget class="QToolButton" name="btnClear">
48+
<property name="toolTip">
49+
<string>Clear all</string>
50+
</property>
51+
<property name="text">
52+
<string>...</string>
53+
</property>
54+
</widget>
55+
</item>
56+
<item row="7" column="1">
57+
<spacer name="verticalSpacer">
58+
<property name="orientation">
59+
<enum>Qt::Vertical</enum>
60+
</property>
61+
<property name="sizeHint" stdset="0">
62+
<size>
63+
<width>20</width>
64+
<height>172</height>
65+
</size>
66+
</property>
67+
</spacer>
68+
</item>
69+
<item row="8" column="0" colspan="2">
70+
<widget class="QCheckBox" name="chkFixedRows">
71+
<property name="text">
72+
<string>Fixed number of rows</string>
73+
</property>
74+
</widget>
75+
</item>
76+
<item row="3" column="1">
77+
<widget class="QToolButton" name="btnAddRow">
78+
<property name="toolTip">
79+
<string>Add row</string>
80+
</property>
81+
<property name="text">
82+
<string>...</string>
83+
</property>
84+
</widget>
85+
</item>
86+
<item row="1" column="1">
87+
<widget class="QToolButton" name="btnAddColumn">
88+
<property name="toolTip">
89+
<string>Add column</string>
90+
</property>
91+
<property name="text">
92+
<string>...</string>
93+
</property>
94+
</widget>
95+
</item>
96+
<item row="2" column="1">
97+
<widget class="QToolButton" name="btnRemoveColumn">
98+
<property name="toolTip">
99+
<string>Remove column</string>
100+
</property>
101+
<property name="text">
102+
<string>...</string>
103+
</property>
104+
</widget>
105+
</item>
106+
</layout>
107+
</widget>
108+
<resources/>
109+
<connections/>
110+
</ui>

0 commit comments

Comments
 (0)