forked from Class-Widgets/Class-Widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexact_menu.py
165 lines (140 loc) · 6.68 KB
/
exact_menu.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
import sys
import datetime as dt
import sys
from shutil import copy
from PyQt6 import uic
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication
from loguru import logger
from qfluentwidgets import FluentWindow, setTheme, Theme, FluentIcon as fIcon, ComboBox, \
PrimaryPushButton, Flyout, FlyoutAnimationType, InfoBarIcon, ListWidget, LineEdit, ToolButton, HyperlinkButton
from win32 import win32api
import conf
import list
import menu
filename = conf.read_conf('General', 'schedule')
current_week = dt.datetime.today().weekday()
temp_schedule = {'schedule': {}, 'schedule_even': {}}
class ExactMenu(FluentWindow):
def __init__(self):
super().__init__()
self.menu = None
self.interface = uic.loadUi('exact_menu.ui')
self.initUI()
self.init_interface()
def init_interface(self):
select_temp_week = self.findChild(ComboBox, 'select_temp_week') # 选择替换日期
select_temp_week.addItems(list.week)
select_temp_week.setCurrentIndex(current_week)
select_temp_week.currentIndexChanged.connect(self.refresh_schedule_list) # 日期选择变化
tmp_schedule_list = self.findChild(ListWidget, 'schedule_list') # 换课列表
tmp_schedule_list.addItems(self.load_schedule())
tmp_schedule_list.itemChanged.connect(self.upload_item)
class_kind_combo = self.findChild(ComboBox, 'class_combo') # 课程类型
class_kind_combo.addItems(list.class_kind)
set_button = self.findChild(ToolButton, 'set_button')
set_button.setIcon(fIcon.EDIT)
set_button.clicked.connect(self.edit_item)
save_temp_conf = self.findChild(PrimaryPushButton, 'save_temp_conf') # 保存设置
save_temp_conf.clicked.connect(self.save_temp_conf)
redirect_to_settings = self.findChild(HyperlinkButton, 'redirect_to_settings')
redirect_to_settings.clicked.connect(self.open_settings)
def open_settings(self):
if self.menu is None or not self.menu.isVisible(): # 防多开
self.menu = menu.desktop_widget()
self.menu.show()
else:
self.menu.raise_()
self.menu.activateWindow()
def load_schedule(self):
global filename
if conf.read_conf('Temp', 'temp_schedule') == '':
filename = conf.read_conf('General', 'schedule')
else:
filename = 'backup.json'
if conf.get_week_type():
return conf.load_from_json(filename)['schedule_even'][str(current_week)]
else:
return conf.load_from_json(filename)['schedule'][str(current_week)]
def save_temp_conf(self):
try:
temp_week = self.findChild(ComboBox, 'select_temp_week')
if temp_schedule != {'schedule': {}, 'schedule_even': {}}:
if conf.read_conf('Temp', 'temp_schedule') == '': # 备份检测
copy(f'config/schedule/{filename}', f'config/schedule/backup.json') # 备份课表配置
logger.info(f'备份课表配置成功:已将 {filename} -备份至-> backup.json')
conf.write_conf('Temp', 'temp_schedule', filename)
conf.save_data_to_json(temp_schedule, filename)
conf.write_conf('Temp', 'set_week', str(temp_week.currentIndex()))
Flyout.create(
icon=InfoBarIcon.SUCCESS,
title='保存成功',
content=f"已保存至 ./config.ini \n重启后失效。",
target=self.findChild(PrimaryPushButton, 'save_temp_conf'),
parent=self,
isClosable=True,
aniType=FlyoutAnimationType.PULL_UP
)
except Exception as e:
Flyout.create(
icon=InfoBarIcon.ERROR,
title='保存失败',
content=f"错误信息:{e}",
target=self.findChild(PrimaryPushButton, 'save_temp_conf'),
parent=self,
isClosable=True,
aniType=FlyoutAnimationType.PULL_UP
)
def refresh_schedule_list(self):
global current_week
current_week = self.findChild(ComboBox, 'select_temp_week').currentIndex()
tmp_schedule_list = self.findChild(ListWidget, 'schedule_list') # 换课列表
tmp_schedule_list.clear()
if conf.get_week_type():
tmp_schedule_list.addItems(conf.load_from_json(filename)['schedule_even'][str(current_week)])
else:
tmp_schedule_list.addItems(conf.load_from_json(filename)['schedule'][str(current_week)])
def upload_item(self):
global temp_schedule
se_schedule_list = self.findChild(ListWidget, 'schedule_list')
cache_list = []
for i in range(se_schedule_list.count()): # 缓存ListWidget数据至列表
item_text = se_schedule_list.item(i).text()
cache_list.append(item_text)
if conf.get_week_type():
temp_schedule['schedule_even'][str(current_week)] = cache_list
else:
temp_schedule['schedule'][str(current_week)] = cache_list
def edit_item(self):
tmp_schedule_list = self.findChild(ListWidget, 'schedule_list')
class_combo = self.findChild(ComboBox, 'class_combo')
custom_class = self.findChild(LineEdit, 'custom_class')
selected_items = tmp_schedule_list.selectedItems()
if selected_items:
selected_item = selected_items[0]
if class_combo.currentIndex() != 0:
selected_item.setText(class_combo.currentText())
else:
if custom_class.text() != '':
selected_item.setText(custom_class.text())
def initUI(self):
screen_geometry = QApplication.primaryScreen().geometry()
screen_width = screen_geometry.width()
self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
setTheme(Theme.AUTO)
self.resize(1000, 700)
self.setWindowTitle('Class Widgets - 更多功能')
self.setWindowIcon(QIcon('img/favicon-exmenu.ico'))
self.move(int(screen_width/2-500), 150) # 窗体居中,但不完全居中
self.addSubInterface(self.interface, fIcon.INFO, '更多设置')
def closeEvent(self, event):
event.ignore()
self.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
if sys.platform == 'win32' and sys.getwindowsversion().build >= 22000: # 修改在win11高版本阴影异常
app.setStyle("fusion")
ex = ExactMenu()
ex.show()
sys.exit(app.exec())