-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_def.py
267 lines (206 loc) · 8.34 KB
/
action_def.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
from PySide2 import QtCore, QtGui, QtWidgets
import global_env
import widgets_creat
from xml.dom import minidom
import os
import logging
def test():
table_row_updata()
def sc_load_file(sc_file):
load_file(sc_file)
def window_load_file():
fdir = global_env.get_file_dir()
fileName_choose, filetype = QtWidgets.QFileDialog.getOpenFileName(global_env.myWin,
"选取弹幕文件",
fdir, # 起始路径
"XML Files (*.xml)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
logging.debug("取消选择")
return
load_file(fileName_choose)
def load_file(fileName_choose):
logging.debug(fileName_choose)
if not global_env.data_saved:
reply = QtWidgets.QMessageBox.question(global_env.myWin,
'将打开其他文件',
"是否保存进度?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No |
QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.Cancel)
if reply == QtWidgets.QMessageBox.Yes:
global_env.keep_data_store()
pass
elif reply == QtWidgets.QMessageBox.No:
pass
else:
return
else:
pass
if not fileName_choose.lower().endswith(".xml"):
global_env.statusbar.showMessage("打开文件错误,不是xml文件")
return
dirname, filename = os.path.split(fileName_choose)
domTree = minidom.parse(fileName_choose)
rootNode = domTree.documentElement
danmu = rootNode.getElementsByTagName("d")
logging.debug(len(danmu))
if len(danmu) < 1:
global_env.statusbar.showMessage("打开文件错误,一条弹幕都没有哦")
return
global_env.recent_file = fileName_choose
global_env.recent_file_dir = dirname
global_env.recent_file_name = filename
global_env.recent_file_rootnode = rootNode
global_env.danmu_data = danmu
if not global_env.keep_data_read():
global_env.statusbar.showMessage("缓存未找到,新的弹幕文件或缓存文件丢失》》》新创建缓存")
global_env.keep_data_init()
else:
global_env.statusbar.showMessage("打开成功,并载入缓存")
global_env.keep_data_read()
global_env.data_saved = True
table_updata(global_env.recent_row)
def table_init():
row_count = global_env.row_count
for i in range(0, row_count):
item = widgets_creat.danmu_tableWidget()
global_env.tableWidget.setItem(i, 0, item)
item = QtWidgets.QTableWidgetItem()
global_env.tableWidget.setVerticalHeaderItem(i, item)
def table_updata(start_num=None):
row_count = global_env.row_count
if start_num is None:
start_num = global_env.recent_row
if start_num < 0:
global_env.statusbar.showMessage("页码已经最小")
return
if not global_env.has_danmu_text(start_num):
global_env.statusbar.showMessage("页码已经最大")
return
recent_page = int(start_num / row_count) + 1
for i in range(0, row_count):
index = start_num + i
str1 = global_env.get_danmu_text(index)
if str1 is not None:
global_env.set_tableWidget_text_and_index(i, str1, index)
else:
table_row_clean(i)
global_env.edit_recent_page_and_row(recent_page, start_num)
def table_updata_by_page(start_page=None):
if start_page is None:
table_updata()
return
row_count = global_env.row_count
start_num = int((start_page - 1) * row_count)
table_updata(start_num)
def table_row_clean(row_num):
# noinspection PyBroadException
try:
global_env.set_tableWidget_text_and_index(row_num, '', '')
except:
pass
def table_clean():
for i in range(0, global_env.row_count):
# noinspection PyBroadException
try:
global_env.set_tableWidget_text_and_index(i, '', '')
except:
pass
# def table_del():
# for i in range(0, global_env.row_count):
# # noinspection PyBroadException
# try:
# global_env.tableWidget.setItem(i, 0, None)
# except:
# pass
def table_row_updata(num=None):
if num is None:
row_count = global_env.row_count
global_env.tableWidget.setRowCount(row_count)
else:
global_env.row_count = num
global_env.tableWidget.setRowCount(num)
def edit_rowcount():
num, ok = QtWidgets.QInputDialog.getInt(global_env.myWin, '修改显示弹幕行数', '输入数字',
value=global_env.row_count, minValue=10, maxValue=30)
if ok and num:
table_row_updata(num)
else:
return
table_init()
if global_env.recent_file is not None:
table_updata()
def tableWidgetItemClick(item):
item.getclick()
def jump_page():
if global_env.recent_file is None:
global_env.statusbar.showMessage("跳页错误,还没有打开弹幕文件")
return
maxpage = int(len(global_env.danmu_data) / global_env.row_count) + 1
num, ok = QtWidgets.QInputDialog.getInt(global_env.myWin, '跳页', '输入数字',
value=global_env.recent_row, minValue=1, maxValue=maxpage)
if ok and num:
table_updata_by_page(num)
else:
return
def previous_page():
page, row = global_env.get_recent_page_and_row()
if page is None:
global_env.statusbar.showMessage("跳页错误,还没有打开弹幕文件")
return
num = page - 1
table_updata_by_page(num)
def next_page():
page, row = global_env.get_recent_page_and_row()
if page is None:
global_env.statusbar.showMessage("跳页错误,还没有打开弹幕文件")
return
num = page + 1
table_updata_by_page(num)
def save_data():
if global_env.recent_file is None:
global_env.statusbar.showMessage("保存错误,还没有打开弹幕文件")
return
global_env.keep_data_store()
QtWidgets.QMessageBox.information(global_env.myWin, '提示', "已保存", QtWidgets.QMessageBox.Ok)
def output_file():
if global_env.recent_file is None:
global_env.statusbar.showMessage("输出错误,还没有打开弹幕文件")
return
file_url = os.path.join(global_env.recent_file_dir, global_env.recent_file_name[0:-4]) + '_dmke.xml'
fileName_choose, filetype = QtWidgets.QFileDialog.getSaveFileName(global_env.myWin,
"弹幕文件输出",
file_url, # 起始路径
"Text Files (*.xml)") # 设置文件扩展名过滤,用双分号间隔
logging.debug(fileName_choose)
if fileName_choose == "":
logging.debug("取消选择")
return
danmu = global_env.danmu_data
dom = minidom.Document()
root_node = dom.createElement('i')
dom.appendChild(root_node)
for i in ("chatid", "chatserver", "mission", "maxlimit", "state", "real_name", "source"):
# noinspection PyBroadException
try:
nnode = global_env.recent_file_rootnode.getElementsByTagName(i)[0]
root_node.appendChild(nnode)
except:
pass
for i in range(0, len(danmu)):
if not global_env.get_danmu_whether_del(i):
root_node.appendChild(danmu[i])
with open(fileName_choose, 'w', encoding='UTF-8') as fh:
dom.writexml(fh, indent='', addindent='\t', newl='\n', encoding='UTF-8')
QtWidgets.QMessageBox.information(global_env.myWin, '提示', "输出弹幕文件成功", QtWidgets.QMessageBox.Ok)
def reverse_election():
row_count = global_env.row_count
for i in range(0, row_count):
global_env.tableWidget.item(i, 0).getclick()
def about_text():
about_str = '''
作者:ikarosf
v0.1
项目地址: https://github.com/ikarosf/bilidanmakuedit
'''
QtWidgets.QMessageBox.about(global_env.myWin, "关于", about_str)