Skip to content

Commit

Permalink
[processing] update fixed table widget and dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Nov 17, 2014
1 parent 2a8fabf commit 20682a7
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 88 deletions.
137 changes: 68 additions & 69 deletions python/plugins/processing/gui/FixedTableDialog.py
Expand Up @@ -25,87 +25,86 @@

__revision__ = '$Format:%H$'

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from processing.ui.ui_DlgFixedTable import Ui_DlgFixedTable

class FixedTableDialog(QtGui.QDialog):

class FixedTableDialog(QDialog, Ui_DlgFixedTable):

def __init__(self, param, table):
QtGui.QDialog.__init__(self)
self.setModal(True)
QDialog.__init__(self)
self.setupUi(self)

self.tblView.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tblView.setSelectionMode(QAbstractItemView.ExtendedSelection)

self.param = param
self.rettable = table
self.setupUi()
self.rettable = None

def setupUi(self):
self.resize(600, 350)
self.setWindowTitle(self.tr('Fixed Table'))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel
| QtGui.QDialogButtonBox.Ok)
self.table = QtGui.QTableWidget()
self.table.setColumnCount(len(self.param.cols))
for i in range(len(self.param.cols)):
self.table.setColumnWidth(i, 380 / len(self.param.cols))
self.table.setHorizontalHeaderItem(i,
QtGui.QTableWidgetItem(self.param.cols[i]))
self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
self.table.setRowCount(len(self.rettable))
for i in range(len(self.rettable)):
self.table.setRowHeight(i, 22)
self.table.verticalHeader().setVisible(False)
self.addRowButton = QtGui.QPushButton()
self.addRowButton.setText(self.tr('Add row'))
self.addRowButton.setEnabled(not self.param.fixedNumOfRows)
self.removeRowButton = QtGui.QPushButton()
self.removeRowButton.setText(self.tr('Remove row'))
self.removeRowButton.setEnabled(not self.param.fixedNumOfRows)
self.buttonBox.addButton(self.addRowButton,
QtGui.QDialogButtonBox.ActionRole)
self.buttonBox.addButton(self.removeRowButton,
QtGui.QDialogButtonBox.ActionRole)
self.setTableContent()
self.horizontalLayout.addWidget(self.table)
self.horizontalLayout.addWidget(self.buttonBox)
self.setLayout(self.horizontalLayout)
self.buttonBox.accepted.connect(self.okPressed)
self.buttonBox.rejected.connect(self.cancelPressed)
self.addRowButton.clicked.connect(self.addRow)
self.removeRowButton.clicked.connect(self.removeRow)
QtCore.QMetaObject.connectSlotsByName(self)

def setTableContent(self):
for i in range(len(self.rettable)):
for j in range(len(self.rettable[0])):
self.table.setItem(i, j,
QtGui.QTableWidgetItem(self.rettable[i][j]))

def okPressed(self):
# Additional buttons
self.btnAdd = QPushButton(self.tr('Add row'))
self.buttonBox.addButton(self.btnAdd,
QDialogButtonBox.ActionRole)
self.btnRemove = QPushButton(self.tr('Remove row(s)'))
self.buttonBox.addButton(self.btnRemove,
QDialogButtonBox.ActionRole)
self.btnRemoveAll = QPushButton(self.tr('Remove all'))
self.buttonBox.addButton(self.btnRemoveAll,
QDialogButtonBox.ActionRole)

self.btnAdd.clicked.connect(self.addRow)
self.btnRemove.clicked.connect(lambda: self.removeRows())
self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))

if self.param.fixedNumOfRows:
self.btnAdd.setEnabled(False)
self.btnRemove.setEnabled(False)
self.btnRemoveAll.setEnabled(False)

self.populateTable(table)

def populateTable(self, table):
cols = len(self.param.cols)
rows = len(table)
model = QStandardItemModel(rows, cols)

# Set headers
model.setHorizontalHeaderLabels(self.param.cols)

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

def accept(self):
cols = self.tblView.model().columnCount()
rows = self.tblView.model().rowCount()
self.rettable = []
for i in range(self.table.rowCount()):
for i in xrange(rows):
self.rettable.append(list())
for j in range(self.table.columnCount()):
self.rettable[i].append(unicode(self.table.item(i, j).text()))
self.close()
for j in xrange(cols):
self.rettable[i].append(unicode(self.tblView.model().item(i, j).text()))
QDialog.accept(self)

def cancelPressed(self):
self.rettable = None
self.close()
def reject(self):
QDialog.reject(self)

def removeRow(self):
if self.table.rowCount() > 1:
self.table.setRowCount(self.table.rowCount() - 1)
def removeRows(self, removeAll=False):
if removeAll:
self.tblView.model().clear()
self.tblView.model().setHorizontalHeaderLabels(self.param.cols)
else:
indexes = self.tblView.selectionModel().selectedRows()
indexes.sort()
self.tblView.setUpdatesEnabled(False)
for i in reversed(indexes):
self.tblView.model().removeRows(i.row(), 1)
self.tblView.setUpdatesEnabled(True)

def addRow(self):
self.table.setRowCount(self.table.rowCount() + 1)
self.table.setRowHeight(self.table.rowCount() - 1, 22)
for i in range(self.table.columnCount()):
self.table.setItem(self.table.rowCount() - 1, i,
QtGui.QTableWidgetItem('0'))
items = [QStandardItem('') for i in xrange(self.tblView.model().columnCount())]
self.tblView.model().appendRow(items)
34 changes: 18 additions & 16 deletions python/plugins/processing/gui/FixedTablePanel.py
Expand Up @@ -25,36 +25,38 @@

__revision__ = '$Format:%H$'

from PyQt4 import QtGui
from PyQt4.QtGui import *

from processing.gui.FixedTableDialog import FixedTableDialog

from processing.ui.ui_widgetBaseSelector import Ui_Form


class FixedTablePanel(QtGui.QWidget):
class FixedTablePanel(QWidget, Ui_Form):

def __init__(self, param, parent=None):
super(FixedTablePanel, self).__init__(parent)
QWidget.__init__(self)
self.setupUi(self)

self.leText.setEnabled(False)

self.param = param
self.table = []
for i in range(param.numRows):
self.table.append(list())
for j in range(len(param.cols)):
self.table[i].append('0')
self.horizontalLayout = QtGui.QHBoxLayout(self)
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.label = QtGui.QLabel()
self.label.setText(self.tr('Fixed table %dx%d' % (len(param.cols), param.numRows)))
self.label.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
self.horizontalLayout.addWidget(self.label)
self.pushButton = QtGui.QPushButton()
self.pushButton.setText(self.tr('...'))
self.pushButton.clicked.connect(self.showFixedTableDialog)
self.horizontalLayout.addWidget(self.pushButton)
self.setLayout(self.horizontalLayout)

self.leText.setText(
self.tr('Fixed table %dx%d' % (len(param.cols), param.numRows)))

self.btnSelect.clicked.connect(self.showFixedTableDialog)

def showFixedTableDialog(self):
dlg = FixedTableDialog(self.param, self.table)
dlg.exec_()
if dlg.rettable is not None:
self.table = dlg.rettable

self.leText.setText(self.tr('Fixed table %dx%d' % (
len(self.table), len(self.table[0]))))
6 changes: 3 additions & 3 deletions python/plugins/processing/gui/MultipleFileInputDialog.py
Expand Up @@ -57,8 +57,8 @@ def __init__(self, options):
QDialogButtonBox.ActionRole)

self.btnAdd.clicked.connect(self.addFile)
self.btnRemove.clicked.connect(lambda: self.removeAll())
self.btnRemoveAll.clicked.connect(lambda: self.removeAll(True))
self.btnRemove.clicked.connect(lambda: self.removeRows())
self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))

self.populateList()

Expand Down Expand Up @@ -99,7 +99,7 @@ def addFile(self):
settings.setValue('/Processing/LastInputPath',
os.path.dirname(files[0]))

def removeAll(self, removeAll=False):
def removeRows(self, removeAll=False):
if removeAll:
self.lstLayers.model().clear()
else:
Expand Down
77 changes: 77 additions & 0 deletions python/plugins/processing/ui/DlgFixedTable.ui
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DlgFixedTable</class>
<widget class="QDialog" name="DlgFixedTable">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>380</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
<string>Fixed table</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="margin">
<number>9</number>
</property>
<item>
<widget class="QTableView" name="tblView">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DlgFixedTable</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DlgFixedTable</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 20682a7

Please sign in to comment.