2018-05-06 13:07:47 +03:00
|
|
|
# -*- 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'
|
|
|
|
|
|
|
|
import os
|
2018-05-21 14:23:26 +10:00
|
|
|
import warnings
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
from qgis.PyQt import uic
|
|
|
|
from qgis.PyQt.QtCore import Qt
|
|
|
|
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
|
2018-05-07 09:43:37 +03:00
|
|
|
from qgis.PyQt.QtWidgets import QInputDialog, QMessageBox
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
from qgis.core import QgsApplication
|
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
2018-05-21 14:23:26 +10:00
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
WIDGET, BASE = uic.loadUiType(
|
|
|
|
os.path.join(pluginPath, 'ui', 'matrixmodelerwidgetbase.ui'))
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
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'))
|
2018-05-07 09:43:37 +03:00
|
|
|
self.btnClear.setIcon(QgsApplication.getThemeIcon('console/iconClearConsole.svg'))
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-05-08 07:43:09 +03:00
|
|
|
items = [QStandardItem('0')]
|
|
|
|
model = QStandardItemModel()
|
|
|
|
model.appendColumn(items)
|
|
|
|
self.tblView.setModel(model)
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
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):
|
2018-05-07 09:43:37 +03:00
|
|
|
res = QMessageBox.question(self, self.tr('Clear?'), self.tr('Are you sure you want to clear table?'))
|
|
|
|
if res == QMessageBox.Yes:
|
|
|
|
self.tblView.model().clear()
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
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):
|
2018-06-13 20:08:59 +03:00
|
|
|
cols = self.tblView.model().columnCount()
|
|
|
|
rows = self.tblView.model().rowCount()
|
|
|
|
|
2018-05-06 13:07:47 +03:00
|
|
|
items = []
|
2018-06-13 20:08:59 +03:00
|
|
|
for row in range(rows):
|
|
|
|
for col in range(cols):
|
|
|
|
items.append(str(self.tblView.model().item(row, col).text()))
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
return items
|
|
|
|
|
2018-06-13 20:08:59 +03:00
|
|
|
def setValue(self, headers, table):
|
|
|
|
model = self.tblView.model()
|
|
|
|
model.setHorizontalHeaderLabels(headers)
|
|
|
|
|
|
|
|
cols = len(headers)
|
|
|
|
rows = len(table) // cols
|
2018-05-06 13:07:47 +03:00
|
|
|
model = QStandardItemModel(rows, cols)
|
|
|
|
|
2018-06-13 20:08:59 +03:00
|
|
|
for row in range(rows):
|
|
|
|
for col in range(cols):
|
|
|
|
item = QStandardItem(str(table[row * cols + col]))
|
|
|
|
model.setItem(row, col, item)
|
2018-05-06 13:07:47 +03:00
|
|
|
self.tblView.setModel(model)
|
|
|
|
|
|
|
|
def headers(self):
|
|
|
|
headers = []
|
|
|
|
model = self.tblView.model()
|
|
|
|
for i in range(model.columnCount()):
|
2018-05-07 09:55:19 +03:00
|
|
|
headers.append(str(model.headerData(i, Qt.Horizontal)))
|
2018-05-06 13:07:47 +03:00
|
|
|
|
|
|
|
return headers
|
|
|
|
|
|
|
|
def fixedRows(self):
|
|
|
|
return self.chkFixedRows.isChecked()
|
|
|
|
|
2018-05-07 09:55:19 +03:00
|
|
|
def setFixedRows(self, fixedRows):
|
|
|
|
self.chkFixedRows.setChecked(fixedRows)
|