-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt5_tabs_001.py
209 lines (152 loc) · 6.34 KB
/
qt5_tabs_001.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sys
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QPushButton,
QTabWidget,
QVBoxLayout,
QTableView,
QDialog,
QDialogButtonBox,
QLabel,
)
from PyQt5.Qt import QApplication, QClipboard
from PyQt5.QtGui import QIcon, QKeySequence
from PyQt5.QtCore import pyqtSlot, QAbstractTableModel, Qt
import pandas as pd
import io
import nmrProblem
from excelheaders import excel_orig_df_columns
class TableModel(QAbstractTableModel):
def __init__(self, dataframe: pd.DataFrame):
super(TableModel, self).__init__()
self._df = dataframe
def data(self, index, role):
if role == Qt.DisplayRole:
value = self._df.iloc[index.row(), index.column()]
return str(value)
def rowCount(self, index):
return self._df.shape[0]
def columnCount(self, index):
return self._df.shape[1]
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self._df.columns[section])
if orientation == Qt.Vertical:
return str(self._df.index[section])
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole or role == Qt.EditRole:
return str(self._df.iloc[index.row(), index.column()])
return None
def setData(self, index, value, role):
if role == Qt.EditRole:
self._df.iloc[index.row(), index.column()] = value
print(self._df)
return True
def flags(self, index):
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable
class tablePane(QTableView):
def __init__(self, parent, tab_title: str, pd_df: pd.DataFrame):
# super(QTableView, self).__init__(parent)
super().__init__(parent)
self.df = pd_df
self.model = TableModel(self.df)
self.setModel(self.model)
self.setAlternatingRowColors(True)
parent.addTab(self, tab_title)
def keyPressEvent(self, event):
# # clipboard = QApplication.clipboard()
# # if event.matches(QKeySequence.Copy):
# # print('Ctrl + C')
# # clipboard.setText("some text")
if event.matches(QKeySequence.Paste):
print(QApplication.clipboard().text())
print("Ctrl + V")
ctext = QApplication.clipboard().text()
if "\t" in ctext:
print("tabs in ctext")
if ctext.endswith("\t"):
print("remove extra tab")
ctext = ctext.rstrip("\t")
else:
print("No extra tab at end")
print(ctext)
iotext = io.StringIO(ctext)
# self.b.insertPlainText(text + '\n')
print("\nPandas dataframe\n")
self.df = pd.read_csv(iotext, sep="\t", index_col=0, dtype=str)
print(self.df)
self.model = TableModel(self.df)
self.setModel(self.model)
self.setAlternatingRowColors(True)
QTableView.keyPressEvent(self, event)
class MyTabWidget(QTabWidget):
def __init__(self, parent, tabTitles_dataframes):
super(QTabWidget, self).__init__(parent)
self.layout = QVBoxLayout()
self.resize(300, 200)
self.tables = {}
for title, table in tabTitles_dataframes.items():
self.tables[title] = tablePane(self, title, table)
# @pyqtSlot()
# def on_click(self):
# print("Button Clicked\n")
# for currentQTabWidgetItem in self.tableWidget.selectedItems():
# print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
class EditDataFrameDialog(QDialog):
def __init__(self, nmrproblem):
super().__init__()
self.nmrproblem = nmrproblem
# tabtitles_dataframes = {"Molecule": nmrproblem.molecule_df,
# "H1": nmrproblem.h1_df,
# "C13": nmrproblem.c13_df}
tabtitles_dataframes = {}
for t, v in excel_orig_df_columns.items():
tabtitles_dataframes[t] = pd.DataFrame(columns=v)
if t == "molecule":
tabtitles_dataframes[t].loc[1] = [""] * len(v)
self.table_widget = MyTabWidget(self, tabtitles_dataframes)
self.setWindowTitle("MestreNova - Edit DataFrame")
self.setGeometry(100, 100, 600, 400)
QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.on_accept)
self.buttonBox.rejected.connect(self.reject)
self.layout = QVBoxLayout()
message = QLabel("Something happened, is that OK?")
self.layout.addWidget(self.table_widget)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
def on_accept(self):
print(list(self.table_widget.tables.keys()))
# copy pandas df tables to nmrproblem
for k in excel_orig_df_columns.keys():
print(k)
nmrProblem.new_dataframes[k] = self.table_widget.tables[k].df.copy()
self.accept()
if __name__ == "__main__":
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
self.nmrproblem = nmrProblem.NMRproblem(nmrProblem.parse_argv())
button = QPushButton("Press me for a dialog!")
button.clicked.connect(self.button_clicked)
self.setCentralWidget(button)
def button_clicked(self, s):
print("click", s)
dlg = EditDataFrameDialog(self.nmrproblem)
# dlg.setWindowTitle("HELLO!")
# table_widget = MyTabWidget(dlg, self.nmrproblem)
if dlg.exec():
print("Success!")
# print(type(dlg.table_widget))
else:
print("Cancel!")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()