forked from SarthakJariwala/Python_GUI_apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable_widget.py
More file actions
145 lines (109 loc) · 4.56 KB
/
Table_widget.py
File metadata and controls
145 lines (109 loc) · 4.56 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 2 17:04:49 2019
@author: Sarthak
"""
from pathlib import Path
import pyqtgraph as pg
from pyqtgraph.python2_3 import asUnicode
from pyqtgraph.Qt import QtCore, QtGui
pg.mkQApp()
pg.setConfigOption('background', 'w')
base_path = Path(__file__).parent
file_path = (base_path / "Table_widget_gui.ui").resolve()
uiFile = file_path
WindowTemplate, TemplateBaseClass = pg.Qt.loadUiType(uiFile)
class MainWindow(TemplateBaseClass):
def __init__(self):
super(TemplateBaseClass, self).__init__()
# Create the main window
self.ui = WindowTemplate()
self.ui.setupUi(self)
self.clear()
self.ui.clear_pushButton.clicked.connect(self.clear)
self.ui.add_row_pushButton.clicked.connect(self.add_row)
self.ui.add_column_pushButton.clicked.connect(self.add_column)
"""Saving and Copying --- implemented from pyqtgraph TableWidget"""
self.contextMenu = QtGui.QMenu()
self.contextMenu.addAction('Copy Selection').triggered.connect(self.copySel)
self.contextMenu.addAction('Copy All').triggered.connect(self.copyAll)
self.contextMenu.addAction('Save Selection').triggered.connect(self.saveSel)
self.contextMenu.addAction('Save All').triggered.connect(self.saveAll)
self.show()
def clear(self):
self.ui.tableWidget.clear()
self.verticalHeadersSet = False
self.horizontalHeadersSet = False
def add_row(self):
row_position = self.ui.tableWidget.rowCount()
self.ui.tableWidget.insertRow(row_position)
def add_column(self):
column_position = self.ui.tableWidget.columnCount()
self.ui.tableWidget.insertColumn(column_position)
def save_table(self):# Needs to be implemented
print(self.ui.tableWidget.currentItem().text())
def serialize(self, useSelection=False):
"""Convert entire table (or just selected area) into tab-separated text values"""
if useSelection:
selection = self.ui.tableWidget.selectedRanges()[0]
rows = list(range(selection.topRow(),
selection.bottomRow() + 1))
columns = list(range(selection.leftColumn(),
selection.rightColumn() + 1))
else:
rows = list(range(self.ui.tableWidget.rowCount()))
columns = list(range(self.ui.tableWidget.columnCount()))
data = []
if self.horizontalHeadersSet:
row = []
if self.verticalHeadersSet:
row.append(asUnicode(''))
for c in columns:
row.append(asUnicode(self.ui.tableWidget.horizontalHeaderItem(c).text()))
data.append(row)
for r in rows:
row = []
if self.verticalHeadersSet:
row.append(asUnicode(self.ui.tableWidget.verticalHeaderItem(r).text()))
for c in columns:
item = self.ui.tableWidget.item(r, c)
if item is not None:
row.append(asUnicode(item.text()))
else:
row.append(asUnicode(''))
data.append(row)
s = ''
for row in data:
s += ('\t'.join(row) + '\n')
return s
def copySel(self):
"""Copy selected data to clipboard."""
QtGui.QApplication.clipboard().setText(self.serialize(useSelection=True))
def copyAll(self):
"""Copy all data to clipboard."""
QtGui.QApplication.clipboard().setText(self.serialize(useSelection=False))
def saveSel(self):
"""Save selected data to file."""
self.save(self.serialize(useSelection=True))
def saveAll(self):
"""Save all data to file."""
self.save(self.serialize(useSelection=False))
def save(self, data):
fileName = QtGui.QFileDialog.getSaveFileName(self, "Save As..", "", "Tab-separated values (*.tsv)")
if fileName == '':
return
open(fileName[0], 'w').write(data)
def contextMenuEvent(self, ev):
self.contextMenu.popup(ev.globalPos())
def keyPressEvent(self, ev):
if ev.key() == QtCore.Qt.Key_C and ev.modifiers() == QtCore.Qt.ControlModifier:
ev.accept()
self.copySel()
# else:
# QtGui.QTableWidget.keyPressEvent(self, ev)
"""Run the Main Window"""
def run():
win = MainWindow()
QtGui.QApplication.instance().exec_()
return win
#run()