Skip to content

Commit 20682a7

Browse files
committedNov 17, 2014
[processing] update fixed table widget and dialog
1 parent 2a8fabf commit 20682a7

File tree

4 files changed

+166
-88
lines changed

4 files changed

+166
-88
lines changed
 

‎python/plugins/processing/gui/FixedTableDialog.py

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,87 +25,86 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from PyQt4 import QtCore, QtGui
2928
from PyQt4.QtCore import *
3029
from PyQt4.QtGui import *
3130

31+
from processing.ui.ui_DlgFixedTable import Ui_DlgFixedTable
3232

33-
class FixedTableDialog(QtGui.QDialog):
33+
34+
class FixedTableDialog(QDialog, Ui_DlgFixedTable):
3435

3536
def __init__(self, param, table):
36-
QtGui.QDialog.__init__(self)
37-
self.setModal(True)
37+
QDialog.__init__(self)
38+
self.setupUi(self)
39+
40+
self.tblView.setSelectionBehavior(QAbstractItemView.SelectRows)
41+
self.tblView.setSelectionMode(QAbstractItemView.ExtendedSelection)
42+
3843
self.param = param
39-
self.rettable = table
40-
self.setupUi()
4144
self.rettable = None
4245

43-
def setupUi(self):
44-
self.resize(600, 350)
45-
self.setWindowTitle(self.tr('Fixed Table'))
46-
self.horizontalLayout = QtGui.QHBoxLayout()
47-
self.horizontalLayout.setSpacing(2)
48-
self.horizontalLayout.setMargin(0)
49-
self.buttonBox = QtGui.QDialogButtonBox()
50-
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
51-
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel
52-
| QtGui.QDialogButtonBox.Ok)
53-
self.table = QtGui.QTableWidget()
54-
self.table.setColumnCount(len(self.param.cols))
55-
for i in range(len(self.param.cols)):
56-
self.table.setColumnWidth(i, 380 / len(self.param.cols))
57-
self.table.setHorizontalHeaderItem(i,
58-
QtGui.QTableWidgetItem(self.param.cols[i]))
59-
self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
60-
self.table.setRowCount(len(self.rettable))
61-
for i in range(len(self.rettable)):
62-
self.table.setRowHeight(i, 22)
63-
self.table.verticalHeader().setVisible(False)
64-
self.addRowButton = QtGui.QPushButton()
65-
self.addRowButton.setText(self.tr('Add row'))
66-
self.addRowButton.setEnabled(not self.param.fixedNumOfRows)
67-
self.removeRowButton = QtGui.QPushButton()
68-
self.removeRowButton.setText(self.tr('Remove row'))
69-
self.removeRowButton.setEnabled(not self.param.fixedNumOfRows)
70-
self.buttonBox.addButton(self.addRowButton,
71-
QtGui.QDialogButtonBox.ActionRole)
72-
self.buttonBox.addButton(self.removeRowButton,
73-
QtGui.QDialogButtonBox.ActionRole)
74-
self.setTableContent()
75-
self.horizontalLayout.addWidget(self.table)
76-
self.horizontalLayout.addWidget(self.buttonBox)
77-
self.setLayout(self.horizontalLayout)
78-
self.buttonBox.accepted.connect(self.okPressed)
79-
self.buttonBox.rejected.connect(self.cancelPressed)
80-
self.addRowButton.clicked.connect(self.addRow)
81-
self.removeRowButton.clicked.connect(self.removeRow)
82-
QtCore.QMetaObject.connectSlotsByName(self)
83-
84-
def setTableContent(self):
85-
for i in range(len(self.rettable)):
86-
for j in range(len(self.rettable[0])):
87-
self.table.setItem(i, j,
88-
QtGui.QTableWidgetItem(self.rettable[i][j]))
89-
90-
def okPressed(self):
46+
# Additional buttons
47+
self.btnAdd = QPushButton(self.tr('Add row'))
48+
self.buttonBox.addButton(self.btnAdd,
49+
QDialogButtonBox.ActionRole)
50+
self.btnRemove = QPushButton(self.tr('Remove row(s)'))
51+
self.buttonBox.addButton(self.btnRemove,
52+
QDialogButtonBox.ActionRole)
53+
self.btnRemoveAll = QPushButton(self.tr('Remove all'))
54+
self.buttonBox.addButton(self.btnRemoveAll,
55+
QDialogButtonBox.ActionRole)
56+
57+
self.btnAdd.clicked.connect(self.addRow)
58+
self.btnRemove.clicked.connect(lambda: self.removeRows())
59+
self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))
60+
61+
if self.param.fixedNumOfRows:
62+
self.btnAdd.setEnabled(False)
63+
self.btnRemove.setEnabled(False)
64+
self.btnRemoveAll.setEnabled(False)
65+
66+
self.populateTable(table)
67+
68+
def populateTable(self, table):
69+
cols = len(self.param.cols)
70+
rows = len(table)
71+
model = QStandardItemModel(rows, cols)
72+
73+
# Set headers
74+
model.setHorizontalHeaderLabels(self.param.cols)
75+
76+
# Populate table
77+
for i in xrange(rows):
78+
for j in xrange(cols):
79+
item = QStandardItem(table[i][j])
80+
model.setItem(i, j, item)
81+
self.tblView.setModel(model)
82+
83+
def accept(self):
84+
cols = self.tblView.model().columnCount()
85+
rows = self.tblView.model().rowCount()
9186
self.rettable = []
92-
for i in range(self.table.rowCount()):
87+
for i in xrange(rows):
9388
self.rettable.append(list())
94-
for j in range(self.table.columnCount()):
95-
self.rettable[i].append(unicode(self.table.item(i, j).text()))
96-
self.close()
89+
for j in xrange(cols):
90+
self.rettable[i].append(unicode(self.tblView.model().item(i, j).text()))
91+
QDialog.accept(self)
9792

98-
def cancelPressed(self):
99-
self.rettable = None
100-
self.close()
93+
def reject(self):
94+
QDialog.reject(self)
10195

102-
def removeRow(self):
103-
if self.table.rowCount() > 1:
104-
self.table.setRowCount(self.table.rowCount() - 1)
96+
def removeRows(self, removeAll=False):
97+
if removeAll:
98+
self.tblView.model().clear()
99+
self.tblView.model().setHorizontalHeaderLabels(self.param.cols)
100+
else:
101+
indexes = self.tblView.selectionModel().selectedRows()
102+
indexes.sort()
103+
self.tblView.setUpdatesEnabled(False)
104+
for i in reversed(indexes):
105+
self.tblView.model().removeRows(i.row(), 1)
106+
self.tblView.setUpdatesEnabled(True)
105107

106108
def addRow(self):
107-
self.table.setRowCount(self.table.rowCount() + 1)
108-
self.table.setRowHeight(self.table.rowCount() - 1, 22)
109-
for i in range(self.table.columnCount()):
110-
self.table.setItem(self.table.rowCount() - 1, i,
111-
QtGui.QTableWidgetItem('0'))
109+
items = [QStandardItem('') for i in xrange(self.tblView.model().columnCount())]
110+
self.tblView.model().appendRow(items)

‎python/plugins/processing/gui/FixedTablePanel.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,38 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from PyQt4 import QtGui
28+
from PyQt4.QtGui import *
29+
2930
from processing.gui.FixedTableDialog import FixedTableDialog
3031

32+
from processing.ui.ui_widgetBaseSelector import Ui_Form
33+
3134

32-
class FixedTablePanel(QtGui.QWidget):
35+
class FixedTablePanel(QWidget, Ui_Form):
3336

3437
def __init__(self, param, parent=None):
35-
super(FixedTablePanel, self).__init__(parent)
38+
QWidget.__init__(self)
39+
self.setupUi(self)
40+
41+
self.leText.setEnabled(False)
42+
3643
self.param = param
3744
self.table = []
3845
for i in range(param.numRows):
3946
self.table.append(list())
4047
for j in range(len(param.cols)):
4148
self.table[i].append('0')
42-
self.horizontalLayout = QtGui.QHBoxLayout(self)
43-
self.horizontalLayout.setSpacing(2)
44-
self.horizontalLayout.setMargin(0)
45-
self.label = QtGui.QLabel()
46-
self.label.setText(self.tr('Fixed table %dx%d' % (len(param.cols), param.numRows)))
47-
self.label.setSizePolicy(QtGui.QSizePolicy.Expanding,
48-
QtGui.QSizePolicy.Expanding)
49-
self.horizontalLayout.addWidget(self.label)
50-
self.pushButton = QtGui.QPushButton()
51-
self.pushButton.setText(self.tr('...'))
52-
self.pushButton.clicked.connect(self.showFixedTableDialog)
53-
self.horizontalLayout.addWidget(self.pushButton)
54-
self.setLayout(self.horizontalLayout)
49+
50+
self.leText.setText(
51+
self.tr('Fixed table %dx%d' % (len(param.cols), param.numRows)))
52+
53+
self.btnSelect.clicked.connect(self.showFixedTableDialog)
5554

5655
def showFixedTableDialog(self):
5756
dlg = FixedTableDialog(self.param, self.table)
5857
dlg.exec_()
5958
if dlg.rettable is not None:
6059
self.table = dlg.rettable
60+
61+
self.leText.setText(self.tr('Fixed table %dx%d' % (
62+
len(self.table), len(self.table[0]))))

‎python/plugins/processing/gui/MultipleFileInputDialog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def __init__(self, options):
5757
QDialogButtonBox.ActionRole)
5858

5959
self.btnAdd.clicked.connect(self.addFile)
60-
self.btnRemove.clicked.connect(lambda: self.removeAll())
61-
self.btnRemoveAll.clicked.connect(lambda: self.removeAll(True))
60+
self.btnRemove.clicked.connect(lambda: self.removeRows())
61+
self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))
6262

6363
self.populateList()
6464

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

102-
def removeAll(self, removeAll=False):
102+
def removeRows(self, removeAll=False):
103103
if removeAll:
104104
self.lstLayers.model().clear()
105105
else:
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>DlgFixedTable</class>
4+
<widget class="QDialog" name="DlgFixedTable">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>380</width>
10+
<height>320</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Fixed table</string>
15+
</property>
16+
<layout class="QHBoxLayout" name="horizontalLayout">
17+
<property name="spacing">
18+
<number>6</number>
19+
</property>
20+
<property name="margin">
21+
<number>9</number>
22+
</property>
23+
<item>
24+
<widget class="QTableView" name="tblView">
25+
<attribute name="horizontalHeaderStretchLastSection">
26+
<bool>true</bool>
27+
</attribute>
28+
</widget>
29+
</item>
30+
<item>
31+
<widget class="QDialogButtonBox" name="buttonBox">
32+
<property name="orientation">
33+
<enum>Qt::Vertical</enum>
34+
</property>
35+
<property name="standardButtons">
36+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
37+
</property>
38+
</widget>
39+
</item>
40+
</layout>
41+
</widget>
42+
<resources/>
43+
<connections>
44+
<connection>
45+
<sender>buttonBox</sender>
46+
<signal>accepted()</signal>
47+
<receiver>DlgFixedTable</receiver>
48+
<slot>accept()</slot>
49+
<hints>
50+
<hint type="sourcelabel">
51+
<x>248</x>
52+
<y>254</y>
53+
</hint>
54+
<hint type="destinationlabel">
55+
<x>157</x>
56+
<y>274</y>
57+
</hint>
58+
</hints>
59+
</connection>
60+
<connection>
61+
<sender>buttonBox</sender>
62+
<signal>rejected()</signal>
63+
<receiver>DlgFixedTable</receiver>
64+
<slot>reject()</slot>
65+
<hints>
66+
<hint type="sourcelabel">
67+
<x>316</x>
68+
<y>260</y>
69+
</hint>
70+
<hint type="destinationlabel">
71+
<x>286</x>
72+
<y>274</y>
73+
</hint>
74+
</hints>
75+
</connection>
76+
</connections>
77+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.