-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableEditorWidget.py
More file actions
171 lines (151 loc) · 5.72 KB
/
TableEditorWidget.py
File metadata and controls
171 lines (151 loc) · 5.72 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
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
import contextlib
import sqlite3
from PyQt5.QtWidgets import QTableWidget, QDialog, QGridLayout, QLabel, QLineEdit, QPushButton
from EditorWidget import EditorWidget
class InsertDialog(QDialog):
def __init__(self, columns: list, cb):
QDialog.__init__(self)
self.main_layout = None
self.columns = columns
self.cols = dict()
self.add_button = None
self.cb = cb
self.initUI()
def initUI(self):
self.setWindowTitle("Insert Row")
self.main_layout = QGridLayout()
self.setLayout(self.main_layout)
for i, col in enumerate(self.columns):
self.main_layout.addWidget(QLabel(str(col)), i, 0)
self.cols[col] = QLineEdit()
self.main_layout.addWidget(self.cols[col], i, 1)
self.add_button = QPushButton("Insert Row")
self.add_button.clicked.connect(self.insert_row)
self.main_layout.addWidget(self.add_button, len(self.columns), 1)
def insert_row(self):
res = dict()
for col in self.cols.keys():
text = self.cols[col].text()
if text:
res[col] = text
self.cb(res)
self.close()
class TableEditorWidget(EditorWidget):
def __init__(self, render_method):
EditorWidget.__init__(self, QTableWidget())
self.table_names = None
self.con = None
self.path = None
self._active_table = None
self._is_reading_db = False
self._columns = dict()
self.render_method = render_method
self.structure = dict()
self.initUI()
def initUI(self):
"""
Method setts UI of QTableWidget (editor)
:return: None
"""
self._editor: QTableWidget
self._editor.itemChanged.connect(self.changed)
def changed(self, item):
"""
Methods handles itemChanged event
:param item: cell in QTableWidget that changed
:return: None
"""
if not self._is_reading_db:
row = self._editor.row(item)
cols = list(range(self.get_columns_len(self._active_table)))
cols.remove(item.column())
cols_values = [self._editor.item(row, i).text() for i in cols]
columns = [self._columns[self._active_table][i] for i in cols]
column = self._columns[self._active_table][item.column()]
self.con.execute(f"""
UPDATE {self._active_table}
SET {column} = ?
WHERE {' AND '.join([f"({col} = ?)" for col in columns])}
""", [item.text(), *cols_values])
def apply_changes(self):
"""
Method applies changes after working with db
:return: None
"""
if self.con is not None:
self.con.commit()
def active_table(self):
"""
Getter for TableEditorWidget._active_table
:return: str
"""
return self._active_table
def __enter__(self):
self._is_reading_db = True
return self.editor()
def __exit__(self, exc_type, exc_val, exc_tb):
self._is_reading_db = False
self._editor.resizeColumnsToContents()
return isinstance(exc_type, TypeError)
def get_columns_len(self, table_name):
"""
Getter for length of TableEditorWidget._columns
:param table_name: key in TableEditorWidget._columns
:return: int
"""
return len(self._columns[table_name])
def add_row(self):
"""
Add row in db
:return: None
"""
if self._active_table is not None:
res = []
InsertDialog(self._columns[self._active_table], lambda x: res.append(x)).exec_()
res = res[0] if len(res) > 0 else None
columns = []
values = []
if res:
for col in res.keys():
columns.append(col)
values.append(res[col])
with contextlib.closing(self.con.cursor()) as cur:
try:
cur.execute(f"""INSERT INTO {self._active_table}
({', '.join(columns)}) VALUES
({', '.join(['?' for _ in range(len(values))])})""",
values)
except sqlite3.Error:
pass
else:
self.render_method(self)
def remove_row(self):
"""
Remove selected rows
:return: None
"""
if self._active_table is not None and isinstance(self.con, sqlite3.Connection):
removed_rows = []
for index in self._editor.selectedIndexes():
row = index.row()
if row not in removed_rows:
where = {col: self._editor.item(row, i).text()
for i, col in enumerate(self._columns[self._active_table])}
with contextlib.closing(self.con.cursor()) as cur:
try:
cur.execute(f"""
DELETE FROM {self._active_table} WHERE
{' AND '.join([f'({key} = ?)' for key in where.keys()])}
""", [value for value in where.values()])
except sqlite3.Error:
pass
else:
removed_rows.append(row)
self.render_method(self)
def close_con(self):
"""
Method closes connection with db
:return: None
"""
if self.con is not None:
self.con.close()