1+ # -*- coding: utf-8 -*-
2+ """
3+ Created on Mon Sep 2 17:04:49 2019
4+
5+ @author: Sarthak
6+ """
7+
8+ from pathlib import Path
9+ import pyqtgraph as pg
10+ from pyqtgraph .python2_3 import asUnicode
11+ from pyqtgraph .Qt import QtCore , QtGui
12+
13+
14+ pg .mkQApp ()
15+ pg .setConfigOption ('background' , 'w' )
16+
17+
18+ base_path = Path (__file__ ).parent
19+ file_path = (base_path / "Table_widget_gui.ui" ).resolve ()
20+
21+ uiFile = file_path
22+
23+ WindowTemplate , TemplateBaseClass = pg .Qt .loadUiType (uiFile )
24+
25+ class MainWindow (TemplateBaseClass ):
26+
27+ def __init__ (self ):
28+ super (TemplateBaseClass , self ).__init__ ()
29+
30+ # Create the main window
31+ self .ui = WindowTemplate ()
32+ self .ui .setupUi (self )
33+
34+ self .clear ()
35+
36+ self .ui .clear_pushButton .clicked .connect (self .clear )
37+ self .ui .add_row_pushButton .clicked .connect (self .add_row )
38+ self .ui .add_column_pushButton .clicked .connect (self .add_column )
39+
40+ """Saving and Copying --- implemented from pyqtgraph TableWidget"""
41+ self .contextMenu = QtGui .QMenu ()
42+ self .contextMenu .addAction ('Copy Selection' ).triggered .connect (self .copySel )
43+ self .contextMenu .addAction ('Copy All' ).triggered .connect (self .copyAll )
44+ self .contextMenu .addAction ('Save Selection' ).triggered .connect (self .saveSel )
45+ self .contextMenu .addAction ('Save All' ).triggered .connect (self .saveAll )
46+
47+ self .show ()
48+
49+ def clear (self ):
50+ self .ui .tableWidget .clear ()
51+ self .verticalHeadersSet = False
52+ self .horizontalHeadersSet = False
53+
54+ def add_row (self ):
55+ row_position = self .ui .tableWidget .rowCount ()
56+ self .ui .tableWidget .insertRow (row_position )
57+
58+ def add_column (self ):
59+ column_position = self .ui .tableWidget .columnCount ()
60+ self .ui .tableWidget .insertColumn (column_position )
61+
62+ def save_table (self ):# Needs to be implemented
63+ print (self .ui .tableWidget .currentItem ().text ())
64+
65+ def serialize (self , useSelection = False ):
66+ """Convert entire table (or just selected area) into tab-separated text values"""
67+ if useSelection :
68+ selection = self .ui .tableWidget .selectedRanges ()[0 ]
69+ rows = list (range (selection .topRow (),
70+ selection .bottomRow () + 1 ))
71+ columns = list (range (selection .leftColumn (),
72+ selection .rightColumn () + 1 ))
73+ else :
74+ rows = list (range (self .ui .tableWidget .rowCount ()))
75+ columns = list (range (self .ui .tableWidget .columnCount ()))
76+
77+ data = []
78+ if self .horizontalHeadersSet :
79+ row = []
80+ if self .verticalHeadersSet :
81+ row .append (asUnicode ('' ))
82+
83+ for c in columns :
84+ row .append (asUnicode (self .ui .tableWidget .horizontalHeaderItem (c ).text ()))
85+ data .append (row )
86+
87+ for r in rows :
88+ row = []
89+ if self .verticalHeadersSet :
90+ row .append (asUnicode (self .ui .tableWidget .verticalHeaderItem (r ).text ()))
91+ for c in columns :
92+ item = self .ui .tableWidget .item (r , c )
93+ if item is not None :
94+ row .append (asUnicode (item .text ()))
95+ else :
96+ row .append (asUnicode ('' ))
97+ data .append (row )
98+
99+ s = ''
100+ for row in data :
101+ s += ('\t ' .join (row ) + '\n ' )
102+ return s
103+
104+
105+ def copySel (self ):
106+ """Copy selected data to clipboard."""
107+ QtGui .QApplication .clipboard ().setText (self .serialize (useSelection = True ))
108+
109+ def copyAll (self ):
110+ """Copy all data to clipboard."""
111+ QtGui .QApplication .clipboard ().setText (self .serialize (useSelection = False ))
112+
113+
114+ def saveSel (self ):
115+ """Save selected data to file."""
116+ self .save (self .serialize (useSelection = True ))
117+
118+
119+ def saveAll (self ):
120+ """Save all data to file."""
121+ self .save (self .serialize (useSelection = False ))
122+
123+
124+ def save (self , data ):
125+ fileName = QtGui .QFileDialog .getSaveFileName (self , "Save As.." , "" , "Tab-separated values (*.tsv)" )
126+ if fileName == '' :
127+ return
128+ open (fileName [0 ], 'w' ).write (data )
129+
130+ def contextMenuEvent (self , ev ):
131+ self .contextMenu .popup (ev .globalPos ())
132+
133+ def keyPressEvent (self , ev ):
134+ if ev .key () == QtCore .Qt .Key_C and ev .modifiers () == QtCore .Qt .ControlModifier :
135+ ev .accept ()
136+ self .copySel ()
137+ # else:
138+ # QtGui.QTableWidget.keyPressEvent(self, ev)
139+ """Run the Main Window"""
140+ def run ():
141+ win = MainWindow ()
142+ QtGui .QApplication .instance ().exec_ ()
143+ return win
144+
145+ run ()
0 commit comments