-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1OK_sample_UI_with_SQLite.py
More file actions
344 lines (292 loc) · 13.4 KB
/
1OK_sample_UI_with_SQLite.py
File metadata and controls
344 lines (292 loc) · 13.4 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Sample code dùng PyQt5 UI tạo giao diện ứng dụng xử lý lưu trữ dữ liệu với SQLite.
import sys
import json
import sqlite3
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QLabel, QLineEdit, QListWidget, QListWidgetItem, QComboBox, QMessageBox, QSizePolicy
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
class DatabaseManager:
def __init__(self, db_name='actions.db'):
self.conn = sqlite3.connect(db_name)
self.create_table()
self.check_and_add_columns()
def create_table(self):
query = '''
CREATE TABLE IF NOT EXISTS templates (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
actions TEXT NOT NULL
)
'''
self.conn.execute(query)
self.conn.commit()
def check_and_add_columns(self):
cursor = self.conn.cursor()
cursor.execute("PRAGMA table_info(templates)")
columns = [info[1] for info in cursor.fetchall()]
if 'name' not in columns:
cursor.execute("ALTER TABLE templates ADD COLUMN name TEXT")
self.conn.commit()
def save_template(self, name, actions):
actions_json = json.dumps(actions)
query = 'INSERT INTO templates (name, actions) VALUES (?, ?)'
self.conn.execute(query, (name, actions_json))
self.conn.commit()
def update_template(self, template_id, name, actions):
actions_json = json.dumps(actions)
query = 'UPDATE templates SET name = ?, actions = ? WHERE id = ?'
self.conn.execute(query, (name, actions_json, template_id))
self.conn.commit()
def load_templates(self):
query = 'SELECT id, name, actions FROM templates'
cursor = self.conn.execute(query)
templates = [{'id': row[0], 'name': row[1], 'actions': self.ensure_action_names(json.loads(row[2]))} for row in cursor]
return templates
def ensure_action_names(self, actions):
for action in actions:
if 'Name' not in action:
action['Name'] = 'Unnamed Action'
return actions
def delete_template(self, template_id):
query = 'DELETE FROM templates WHERE id = ?'
self.conn.execute(query, (template_id,))
self.conn.commit()
class DraggableListWidget(QListWidget):
def __init__(self):
super().__init__()
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setDefaultDropAction(Qt.MoveAction)
self.setSelectionMode(QListWidget.SingleSelection)
def startDrag(self, supportedActions):
item = self.currentItem()
if item:
drag = QDrag(self)
mime_data = QMimeData()
mime_data.setText(item.text())
drag.setMimeData(mime_data)
drag.exec_(Qt.MoveAction)
def dropEvent(self, event):
source_row = self.currentRow()
target_row = self.indexAt(event.pos()).row()
if target_row == -1:
target_row = self.count() - 1
if source_row != target_row:
item = self.takeItem(source_row)
self.insertItem(target_row, item)
self.reorder_steps()
event.accept()
def reorder_steps(self):
for i in range(self.count()):
item = self.item(i)
item_text = item.text()
item.setText(f"{i + 1}: {item_text.split(': ')[1]}")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Action Template Manager')
self.setGeometry(100, 100, 1000, 600)
self.db_manager = DatabaseManager()
main_layout = QHBoxLayout()
left_layout = QVBoxLayout()
right_layout = QVBoxLayout()
# Template List
self.template_list = QListWidget()
self.template_list.itemClicked.connect(self.load_template)
left_layout.addWidget(QLabel('Templates:'))
left_layout.addWidget(self.template_list)
# Action Form
form_layout = QVBoxLayout()
self.action_name_input = QLineEdit(self)
form_layout.addWidget(QLabel('Action Name:'))
form_layout.addWidget(self.action_name_input)
self.type_input = QComboBox(self)
self.type_input.addItems(['image', 'path', 'xpath', 'element id', 'class', 'app'])
form_layout.addWidget(QLabel('Type:'))
form_layout.addWidget(self.type_input)
self.type_target_input = QLineEdit(self)
form_layout.addWidget(QLabel('Target by type: application name if type is app. Otherwise, it is a processing link.'))
form_layout.addWidget(self.type_target_input)
self.media_input = QLineEdit(self)
form_layout.addWidget(QLabel('Media (if image):'))
form_layout.addWidget(self.media_input)
self.content_input = QLineEdit(self)
form_layout.addWidget(QLabel('Content:'))
form_layout.addWidget(self.content_input)
button_layout = QHBoxLayout()
self.add_button = QPushButton('Add Action', self)
self.add_button.clicked.connect(self.add_action)
self.add_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
button_layout.addWidget(self.add_button)
self.update_button = QPushButton('Update Action', self)
self.update_button.clicked.connect(self.update_action)
self.update_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
button_layout.addWidget(self.update_button)
form_layout.addLayout(button_layout)
right_layout.addLayout(form_layout)
# Action List
self.action_list = DraggableListWidget()
self.action_list.itemDoubleClicked.connect(self.edit_action)
right_layout.addWidget(QLabel('Actions:'))
right_layout.addWidget(self.action_list)
self.template_name_input = QLineEdit(self)
right_layout.addWidget(QLabel('Template File Name:'))
right_layout.addWidget(self.template_name_input)
template_button_layout = QHBoxLayout()
self.save_button = QPushButton('Save Template', self)
self.save_button.clicked.connect(self.save_template)
self.save_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
template_button_layout.addWidget(self.save_button)
self.update_template_button = QPushButton('Update Template', self)
self.update_template_button.clicked.connect(self.update_template)
self.update_template_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
template_button_layout.addWidget(self.update_template_button)
right_layout.addLayout(template_button_layout)
main_layout.addLayout(left_layout, 1)
main_layout.addLayout(right_layout, 2)
container = QWidget()
container.setLayout(main_layout)
self.setCentralWidget(container)
self.actions = []
self.templates = []
self.current_action_index = None
self.current_template_id = None
self.load_templates()
def add_action(self):
action_name = self.action_name_input.text()
action_type = self.type_input.currentText()
type_target = self.type_target_input.text()
media = self.media_input.text()
content = self.content_input.text()
step = len(self.actions) + 1
action = {
'Name': action_name,
'Type': action_type,
'Type_target': type_target,
'Media': media,
'Step': step,
'Content': content
}
self.actions.append(action)
self.update_action_list()
self.clear_form()
def update_action_list(self):
self.actions.sort(key=lambda x: x['Step'])
self.action_list.clear()
for index, action in enumerate(self.actions):
action['Step'] = index + 1
item_text = f"{action['Step']}: {action['Name']}"
list_item = QListWidgetItem(item_text)
item_widget = QWidget()
item_layout = QHBoxLayout()
item_label = QLabel(item_text)
delete_button = QPushButton('Delete')
delete_button.setFixedWidth(50)
delete_button.clicked.connect(lambda _, idx=index: self.confirm_delete_action(idx))
item_layout.addWidget(item_label)
item_layout.addWidget(delete_button)
item_layout.setStretch(0, 1)
item_widget.setLayout(item_layout)
list_item.setSizeHint(item_widget.sizeHint())
self.action_list.addItem(list_item)
self.action_list.setItemWidget(list_item, item_widget)
def edit_action(self, item):
index = self.action_list.row(item)
action = self.actions[index]
self.action_name_input.setText(action['Name'])
self.type_input.setCurrentText(action['Type'])
self.type_target_input.setText(action['Type_target'])
self.media_input.setText(action['Media'])
self.content_input.setText(action['Content'])
self.current_action_index = index
self.add_button.setText("Add as New Action")
self.update_button.setVisible(True)
def update_action(self):
if self.current_action_index is not None:
action = self.actions[self.current_action_index]
action['Name'] = self.action_name_input.text()
action['Type'] = self.type_input.currentText()
action['Type_target'] = self.type_target_input.text()
action['Media'] = self.media_input.text()
action['Content'] = self.content_input.text()
self.update_action_list()
if self.current_template_id:
self.db_manager.update_template(self.current_template_id, self.template_name_input.text(), self.actions)
def clear_form(self):
self.action_name_input.clear()
self.type_input.setCurrentIndex(0)
self.type_target_input.clear()
self.media_input.clear()
self.content_input.clear()
self.add_button.setText("Add Action")
self.update_button.setVisible(False)
self.current_action_index = None
def confirm_delete_action(self, index):
reply = QMessageBox.question(self, 'Confirm Delete', 'Are you sure you want to delete this action?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.delete_action(index)
def delete_action(self, index):
self.actions.pop(index)
self.update_action_list()
if self.current_template_id:
self.db_manager.update_template(self.current_template_id, self.template_name_input.text(), self.actions)
def save_template(self):
file_name = self.template_name_input.text()
if not file_name:
QMessageBox.warning(self, "Error", "Please enter a template file name.")
return
try:
self.db_manager.save_template(file_name, self.actions)
QMessageBox.information(self, "Success", f"Template '{file_name}' saved to database.")
self.load_templates()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not save template: {e}")
def update_template(self):
if self.current_template_id is not None:
try:
self.db_manager.update_template(self.current_template_id, self.template_name_input.text(), self.actions)
QMessageBox.information(self, "Success", f"Template updated successfully.")
self.load_templates()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not update template: {e}")
def load_templates(self):
self.templates = self.db_manager.load_templates()
self.template_list.clear()
for template in self.templates:
item_widget = QWidget()
item_layout = QHBoxLayout()
item_label = QLabel(template['name'])
delete_button = QPushButton('Delete')
delete_button.setFixedWidth(50)
delete_button.clicked.connect(lambda _, t=template: self.delete_template(t['id']))
item_layout.addWidget(item_label)
item_layout.addWidget(delete_button)
item_layout.setStretch(0, 1)
item_widget.setLayout(item_layout)
list_item = QListWidgetItem()
list_item.setSizeHint(item_widget.sizeHint())
self.template_list.addItem(list_item)
self.template_list.setItemWidget(list_item, item_widget)
def delete_template(self, template_id):
try:
self.db_manager.delete_template(template_id)
QMessageBox.information(self, "Success", "Template deleted from database.")
self.load_templates()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not delete template: {e}")
def load_template(self, item):
item_widget = self.template_list.itemWidget(item)
template_name = item_widget.findChild(QLabel).text()
for template in self.templates:
if template['name'] == template_name:
self.actions = template['actions']
self.current_template_id = template['id']
self.template_name_input.setText(template['name'])
self.update_action_list()
break
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())