-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_widget.py
155 lines (133 loc) · 5.81 KB
/
table_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) Grigoriy A. Armeev, 2015
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as·
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License v2 for more details.
# Cheers, Satary.
#
from PyQt4 import QtGui,QtCore
import sys, csv
import numpy as np
class TableWidget(QtGui.QTableWidget):
def __init__(self,parent=None):
super(TableWidget, self).__init__(parent)
# if you want to use parent's methods and be ugly as I do, use parent =)
self.parent=parent
self.clip = QtGui.QApplication.clipboard()
self.horizontalHeader().setDefaultSectionSize(60)
self.setMinimumWidth(250)
self.setMinimumHeight(250)
self.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.rowOrder=[]
self.columnOrder=[]
def buildFromList(self,inList,addHeaders=True):
'''
This methods builds table from rectangular 2D list or np.array
where 1 column and 1 line contain names of lines and columns
'''
self.setRowCount(0)
self.setColumnCount(0)
if addHeaders:
for row in inList[1:,0]:
self.insertRow(self.rowCount())
self.setVerticalHeaderItem(self.rowCount()-1, QtGui.QTableWidgetItem(row))
for col in inList[0,1:]:
self.insertColumn(self.columnCount())
self.setHorizontalHeaderItem(self.columnCount()-1,QtGui.QTableWidgetItem(col))
inList=inList[1:,1:]
#asidning values
else:
for row in inList[:,0]:
self.insertRow(self.rowCount())
for col in inList[0,:]:
self.insertColumn(self.columnCount())
it = np.nditer(inList, flags=['multi_index'])
while not it.finished:
self.setItem(it.multi_index[0],it.multi_index[1],QtGui.QTableWidgetItem(str(it[0])))
it.iternext()
self.verticalHeader().setDefaultSectionSize(self.verticalHeader().minimumSectionSize())
def addFromList(self,inList,addHeaders=True):
numCols=self.columnCount()
for col in inList[:,0]:
self.insertColumn(numCols)
it = np.nditer(inList, flags=['multi_index'])
while not it.finished:
self.setItem(it.multi_index[1],it.multi_index[0]+numCols,QtGui.QTableWidgetItem(str(it[0])))
it.iternext()
def keyPressEvent(self, e):
if (e.modifiers() & QtCore.Qt.ControlModifier):
if e.key() == QtCore.Qt.Key_C:
self.copySelectionToClipboard()
elif e.key() == QtCore.Qt.Key_A:
self.selectAll()
QtGui.QTableWidget.keyPressEvent(self, e)
self.parent.keyPressEvent(e)
def contextMenuEvent(self, pos):
menu = QtGui.QMenu()
copyAction = menu.addAction("Copy")
copyAllAction = menu.addAction("Copy All")
selectAll = menu.addAction("Select All")
action = menu.exec_(QtGui.QCursor.pos())
if action == copyAction:
self.copySelectionToClipboard()
elif action == copyAllAction:
self.copySelectionToClipboard(True)
elif action == selectAll:
self.selectAll()
def handleSave(self,path):
rowLog = range(self.rowCount())
rowIndx = [self.visualRow(i) for i in rowLog]
rowVis = [x for (y,x) in sorted(zip(rowIndx,rowLog))]
colLog = range(self.columnCount())
colIndx = [self.visualColumn(i) for i in colLog]
colVis = [x for (y,x) in sorted(zip(colIndx,colLog))]
with open(unicode(path), 'wb') as stream:
writer = csv.writer(stream)
rowdata = []
rowdata.append("")
for column in colVis:
rowdata.append(unicode(self.horizontalHeaderItem(column).text()).encode('utf8'))
writer.writerow(rowdata)
for row in rowVis:
rowdata = []
rowdata.append(unicode(self.verticalHeaderItem(row).text()).encode('utf8'))
for column in colVis:
item = self.item(row, column)
if item is not None:
rowdata.append(
unicode(item.text()).encode('utf8'))
else:
rowdata.append('')
writer.writerow(rowdata)
def selectAll(self):
self.setRangeSelected(QtGui.QTableWidgetSelectionRange( 0,0, self.rowCount()-1, self.columnCount()-1)
, True)
def copySelectionToClipboard(self,all=False):
if all:
selected = [QtGui.QTableWidgetSelectionRange( 0,0, self.rowCount()-1, self.columnCount()-1)]
else:
selected = self.selectedRanges()
s = ""
for r in xrange(selected[0].topRow(),selected[0].bottomRow()+1):
for c in xrange(selected[0].leftColumn(),selected[0].rightColumn()+1):
try:
s += str(self.item(r,c).text()) + "\t"
except AttributeError:
s += "\t"
s = s[:-1] + "\n" #eliminate last '\t'
self.clip.setText(s)
def main():
app = QtGui.QApplication(sys.argv)
ex = TableWidget()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()