|
25 | 25 |
|
26 | 26 | __revision__ = '$Format:%H$'
|
27 | 27 |
|
28 |
| -from PyQt4 import QtCore, QtGui |
29 | 28 | from PyQt4.QtCore import *
|
30 | 29 | from PyQt4.QtGui import *
|
31 | 30 |
|
| 31 | +from processing.ui.ui_DlgFixedTable import Ui_DlgFixedTable |
32 | 32 |
|
33 |
| -class FixedTableDialog(QtGui.QDialog): |
| 33 | + |
| 34 | +class FixedTableDialog(QDialog, Ui_DlgFixedTable): |
34 | 35 |
|
35 | 36 | 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 | + |
38 | 43 | self.param = param
|
39 |
| - self.rettable = table |
40 |
| - self.setupUi() |
41 | 44 | self.rettable = None
|
42 | 45 |
|
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() |
91 | 86 | self.rettable = []
|
92 |
| - for i in range(self.table.rowCount()): |
| 87 | + for i in xrange(rows): |
93 | 88 | 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) |
97 | 92 |
|
98 |
| - def cancelPressed(self): |
99 |
| - self.rettable = None |
100 |
| - self.close() |
| 93 | + def reject(self): |
| 94 | + QDialog.reject(self) |
101 | 95 |
|
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) |
105 | 107 |
|
106 | 108 | 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) |
0 commit comments