-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_ui.py
244 lines (221 loc) · 9.1 KB
/
tag_ui.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
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
import os
from aqt import QComboBox, QFileDialog, QLabel, QMainWindow, QVBoxLayout, mw
try:
from PyQt6.QtWidgets import (QPushButton, QLineEdit)
except ImportError:
from PyQt5.QtWidgets import (QPushButton, QLineEdit)
from . import tag_function
class SelectTagWindow(QMainWindow):
def __init__(self, parent=None):
# select window
super(SelectTagWindow, self).__init__(parent)
# window title
self.setWindowTitle('Tags')
self.setGeometry(750, 350, 290, 190)
# message
self.label_info = QLabel('Select a tag', self)
self.label_info.resize(300,30)
self.label_info.setStyleSheet("QLabel{font-size : 20px; font: bold}");
self.label_info.move(40,20)
# pull-down option
self.combo = QComboBox(self)
self.combo.resize(220,30)
self.combo.move(40, 60)
# fill options
taglist = mw.col.tags.all()
for tag in sorted(taglist):
self.combo.addItem(tag)
# set layout
layout = QVBoxLayout()
layout.addWidget(self.label_info)
layout.addWidget(self.combo)
# confirmation button
btn = QPushButton('Ok', self)
btn.move(90, 120)
btn.clicked.connect(self.ok_clicked)
layout.addWidget(btn)
def ok_clicked(self):
# passed value
selected_item = self.combo.currentText()
self.parent().select_tag_dialog.setText(selected_item)
# mw.col.close()
self.close()
class SelectFieldWindow(QMainWindow):
def __init__(self, parent=None):
# select window
super(SelectFieldWindow, self).__init__(parent)
# window title
self.setWindowTitle('Fields')
self.setGeometry(750, 350, 290, 190)
# message
self.label_info = QLabel('Select a field', self)
self.label_info.resize(300,30)
self.label_info.setStyleSheet("QLabel{font-size : 20px; font: bold}");
self.label_info.move(40,20)
# pull-down option
self.combo = QComboBox(self)
self.combo.resize(220,30)
self.combo.move(40, 60)
# fill options
if self.parent().select_tag_dialog.text() == "":
fields_list = []
for tag in set(mw.col.tags.all()):
notes_list = mw.col.find_notes("tag:" + tag)
# fields = mw.col.get_note(notes_list[0]).keys()
fields = mw.col.field_names_for_note_ids([notes_list[0]])
for field in fields:
fields_list.append(field)
for field_name in sorted(set(fields_list)):
self.combo.addItem(field_name)
else:
notes_list = mw.col.find_notes("tag:" + self.parent().select_tag_dialog.text())
if notes_list:
fields_list = set(mw.col.field_names_for_note_ids([notes_list[0]]))
for field_name in sorted(fields_list):
self.combo.addItem(field_name)
# set layout
layout = QVBoxLayout()
layout.addWidget(self.label_info)
layout.addWidget(self.combo)
# confirmation button
btn = QPushButton('Ok', self)
btn.move(90, 120)
btn.clicked.connect(self.ok_clicked)
layout.addWidget(btn)
def ok_clicked(self):
# passed value
selected_item = self.combo.currentText()
self.parent().field_name_dialog.setText(selected_item)
# mw.col.close()
self.close()
class MainWindow(QMainWindow):
def __init__(self, browser):
# main window
super(MainWindow, self).__init__()
# set window title
self.setWindowTitle("Batch Add Tag")
# field name
self.field_name_value = ""
# add tag name
self.add_tag_value = ""
# file path
self.file_path_value = ""
# select tag name
self.select_tag_value = ""
# add tag layout
# select a tag
self.search_tag_title = QLabel('Select Tag: ', self)
self.search_tag_title.move(20, 20)
# input select tag name
self.select_tag_dialog = QLineEdit(self)
self.select_tag_dialog.setPlaceholderText(' Tags To Look For(Selectable)')
self.select_tag_dialog.resize(210, 30)
self.select_tag_dialog.move(130, 15)
self.select_tag_dialog.setMaxLength(30)
# select tag button
self.select_tag_btn = QPushButton("SelectTag", self)
self.select_tag_btn.resize(150, 30)
self.select_tag_btn.move(350, 15)
# select field name
self.fields_name_title = QLabel('Field Name: ', self)
self.fields_name_title.move(20, 60)
# input field name
self.field_name_dialog = QLineEdit(self)
self.field_name_dialog.setPlaceholderText(" Card's Fields Name")
self.field_name_dialog.resize(210, 30)
self.field_name_dialog.move(130, 55)
self.field_name_dialog.setMaxLength(30)
# select field name button
self.fields_name_btn = QPushButton("SelectField", self)
self.fields_name_btn.resize(150, 30)
self.fields_name_btn.move(350, 55)
# show file name
self.file_name_title = QLabel('File Name: ', self)
self.file_name_title.move(20, 100)
self.file_name = QLabel('Only .txt .xls .xlsx', self)
self.file_name.setGeometry(130,95,200,30)
self.file_name.setStyleSheet("QLabel{color : red;}");
# select file button
self.select_file_btn = QPushButton("SelectFile", self)
self.select_file_btn.resize(150, 30)
self.select_file_btn.move(350, 95)
self.select_file_btn.clicked.connect(self.SelectFile)
# add tag name
self.add_tag_title = QLabel('TagWannaAdd: ', self)
self.add_tag_title.move(20, 140)
# input tag name
self.add_tag_dialog = QLineEdit(self)
self.add_tag_dialog.setPlaceholderText(' Tag You Want To Add')
self.add_tag_dialog.resize(370, 30)
self.add_tag_dialog.move(130, 135)
self.add_tag_dialog.setMaxLength(30)
# start add tag button
self.batch_add_tag_btn = QPushButton("Batch Add Tags", self)
self.batch_add_tag_btn.resize(210, 30)
self.batch_add_tag_btn.move(130, 170)
# help doc link
helpDoc = QLabel(self)
helpDoc.resize(600, 30)
helpDoc.move(95, 210)
helpDoc.setText(
"If you have any question, Click here: <a href='https://github.com/RhettTien/Batch-Editor-Tools'> Github</a> <a href='https://ankiweb.net/shared/info/1609139780'>AnkiWeb</a>")
# The setOpenExternalLinks (True) method of the tag is used to control the connection with the external environment
helpDoc.setOpenExternalLinks(True)
layout = QVBoxLayout()
layout.addWidget(self.select_tag_dialog)
layout.addWidget(self.select_tag_btn)
layout.addWidget(self.field_name_dialog)
layout.addWidget(self.fields_name_btn)
# set window size
self.setFixedSize(520, 240)
self.show()
# start button
self.batch_add_tag_btn.clicked.connect(self.batch_add_tags)
# button function
self.select_tag_btn.clicked.connect(self.show_select_tag_window)
self.fields_name_btn.clicked.connect(self.show_select_field_window)
# add tags
def batch_add_tags(self):
# select tag , add tag, field name, file path
self.select_tag_value = self.select_tag_dialog.text()
self.field_name_value = self.field_name_dialog.text()
self.add_tag_value = self.add_tag_dialog.text()
tag_function.add_tag(
self.select_tag_value,
self.add_tag_value,
self.field_name_value,
self.file_path_value
)
# get file path
def SelectFile(self):
# user folder
folderpath = "C:/Users/" + str(os.getlogin())
filePath = QFileDialog.getOpenFileName(self, 'Select a list file', folderpath)
# empty file path
if(filePath[0] == ""):
return
# split file name
self.file_path_value = str(filePath[0])
filepath = str(self.file_path_value)
self.file_name.setText(filepath[filepath.rfind("/")+1:])
# wrong file type
if not ("." in filepath):
return
# determine file type
if(filepath[filepath.rfind(".")+1:] == "xls" or
filepath[filepath.rfind(".")+1:] == "xlsx" or
filepath[filepath.rfind(".")+1:] == "txt"):
self.file_name.setStyleSheet("QLabel{color : green;}");
else:
self.file_name.setStyleSheet("QLabel{color : red;}");
def show_select_tag_window(self):
# select tag window
select_window = SelectTagWindow(self)
select_window.show()
def show_select_field_window(self):
# select field window
select_window = SelectFieldWindow(self)
select_window.show()
def tag_start():
mw.tools_dialog = MainWindow(mw.app.activeWindow())
mw.tools_dialog.show()