|
| 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) |
0 commit comments