Skip to content

Commit

Permalink
[processing] modeler GUI for matrix parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy authored and nyalldawson committed May 11, 2018
1 parent ffa4b04 commit 038da11
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
135 changes: 135 additions & 0 deletions python/plugins/processing/gui/matrixmodelerwidget.py
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
MatrixModelerWidget.py
---------------------
Date : May 2018
Copyright : (C) 2018 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Alexander Bruy'
__date__ = 'May 2018'
__copyright__ = '(C) 2018, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os

from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
from qgis.PyQt.QtWidgets import QInputDialog

from qgis.core import QgsApplication

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'matrixmodelerwidgetbase.ui'))


class MatrixModelerWidget(BASE, WIDGET):

def __init__(self, parent=None):
super(MatrixModelerWidget, self).__init__(parent)
self.setupUi(self)

self.btnAddColumn.setIcon(QgsApplication.getThemeIcon('/mActionNewAttribute.svg'))
self.btnRemoveColumn.setIcon(QgsApplication.getThemeIcon('/mActionDeleteAttribute.svg'))
self.btnAddRow.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
self.btnRemoveRow.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))
self.btnClear.setIcon(QgsApplication.getThemeIcon('/mIconClearText.svg'))

self.btnAddColumn.clicked.connect(self.addColumn)
self.btnRemoveColumn.clicked.connect(self.removeColumns)
self.btnAddRow.clicked.connect(self.addRow)
self.btnRemoveRow.clicked.connect(self.removeRows)
self.btnClear.clicked.connect(self.clearTable)

self.tblView.setModel(QStandardItemModel())

self.tblView.horizontalHeader().sectionDoubleClicked.connect(self.changeHeader)

def addColumn(self):
model = self.tblView.model()
items = [QStandardItem('0') for i in range(model.rowCount())]
model.appendColumn(items)

def removeColumns(self):
indexes = sorted(self.tblView.selectionModel().selectedColumns())
self.tblView.setUpdatesEnabled(False)
for i in reversed(indexes):
self.tblView.model().removeColumns(i.column(), 1)
self.tblView.setUpdatesEnabled(True)

def addRow(self):
model = self.tblView.model()
items = [QStandardItem('0') for i in range(model.columnCount())]
model.appendRow(items)

def removeRows(self):
indexes = sorted(self.tblView.selectionModel().selectedRows())
self.tblView.setUpdatesEnabled(False)
for i in reversed(indexes):
self.tblView.model().removeRows(i.row(), 1)
self.tblView.setUpdatesEnabled(True)

def clearTable(self, removeAll=False):
self.tblView.model().clear()

def changeHeader(self, index):
txt, ok = QInputDialog.getText(self, self.tr("Enter column name"), self.tr("Column name"))
if ok:
self.tblView.model().setHeaderData(index, Qt.Horizontal, txt)

def value(self):
items = []
model = self.tblView.model()
for i in range(model.rowCount()):
row = []
for j in range(model.columnCount()):
item = model.item(i, j)
row.append(item.text())
items.append(row)

return items

def setValue(self, table):
cols = len(table[0])
rows = len(table)
model = QStandardItemModel(rows, cols)

for i in range(rows):
for j in range(cols):
item = QStandardItem(str(table[i][j]))
model.setItem(i, j, item)
self.tblView.setModel(model)

def headers(self):
headers = []
model = self.tblView.model()
for i in range(model.columnCount()):
headers.append(model.headerData(i, Qt.Horizontal))

return headers

def setHeaders(self, headers):
model = self.tblView.model()
model.setHorizontalHeaderLabels(headers)

def fixedRows(self):
return self.chkFixedRows.isChecked()

def setFixedRows(self):
self.chkFixedRows.setChecked(True)
Expand Up @@ -67,6 +67,7 @@
)

from processing.gui.enummodelerwidget import EnumModelerWidget
from processing.gui.matrixmodelerwidget import MatrixModelerWidget
from processing.core import parameters
from processing.modeler.exceptions import UndefinedParameterException

Expand Down Expand Up @@ -283,6 +284,14 @@ def setupUi(self):
self.widget.setDefault(int(self.param.defaultValue()))
self.widget.setAllowMultiple(bool(self.param.allowMultiple()))
self.verticalLayout.addWidget(self.widget)
elif self.paramType == parameters.PARAMETER_MATRIX or \
isinstance(self.param, QgsProcessingParameterMatrix):
self.widget = MatrixModelerWidget(self)
if self.param is not None:
self.widget.setValue(self.param.defaultValue())
self.widget.setHeaders(self.param.headers())
self.widget.setFixedRows(self.param.hasFixedNumberRows())
self.verticalLayout.addWidget(self.widget)

self.verticalLayout.addSpacing(20)
self.requiredCheck = QCheckBox()
Expand Down
110 changes: 110 additions & 0 deletions python/plugins/processing/ui/matrixmodelerwidgetbase.ui
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0" rowspan="8">
<widget class="QTableView" name="tblView">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
<item row="4" column="1">
<widget class="QToolButton" name="btnRemoveRow">
<property name="toolTip">
<string>Remove row</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QToolButton" name="btnClear">
<property name="toolTip">
<string>Clear all</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="7" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>172</height>
</size>
</property>
</spacer>
</item>
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="chkFixedRows">
<property name="text">
<string>Fixed number of rows</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QToolButton" name="btnAddRow">
<property name="toolTip">
<string>Add row</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QToolButton" name="btnAddColumn">
<property name="toolTip">
<string>Add column</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="btnRemoveColumn">
<property name="toolTip">
<string>Remove column</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit 038da11

Please sign in to comment.