forked from dragondjf/QMarkdowner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQMain.py
416 lines (359 loc) · 16 KB
/
QMain.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import logging
from logging.handlers import RotatingFileHandler
from qframer.qt import QtGui
from qframer.qt import QtCore
from qframer import FMainWindow
import json
import time
#主日志保存在log/QSoftkeyer.log
logging.root.setLevel(logging.INFO)
logging.root.propagate = 0
loghandler = RotatingFileHandler(os.path.join("log", "QMain.log"), maxBytes=10 * 1024 * 1024, backupCount=100)
loghandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)8s [%(filename)16s:%(lineno)04s] %(message)s'))
loghandler.level = logging.INFO
logging.root.addHandler(loghandler)
logger = logging.root
logger.propagate = 0
from config import windowsoptions
import config
from effects import *
from childpages import *
from guiutil.utils import set_skin, set_bg
import utildialog
class MetroWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MetroWindow, self).__init__(parent)
self.pagetags = windowsoptions['mainwindow']['centralwindow']['pagetags']
self.pagetags_zh = windowsoptions['mainwindow']['centralwindow']['pagetags_zh']
self.initUI()
def initUI(self):
self.pagecount = len(self.pagetags_zh) # 页面个数
# self.createNavigation()
self.pages = QtGui.QStackedWidget() # 创建堆控件
# self.pages.addWidget(self.navigationPage)
self.createChildPages() # 创建子页面
# self.createConnections()
mainLayout = QtGui.QHBoxLayout()
mainLayout.addWidget(self.pages)
self.setLayout(mainLayout)
self.layout().setContentsMargins(0, 0, 0, 0)
self.faderWidget = None
self.pages.currentChanged.connect(self.fadeInWidget) # 页面切换时淡入淡出效果
def createNavigation(self):
'''
创建导航页面
'''
self.navigationPage = NavigationPage()
def createChildPages(self):
'''
创建子页面
'''
for buttons in self.pagetags:
for item in buttons:
page = item + 'Page'
childpage = 'child' + page
if hasattr(sys.modules[__name__], page):
setattr(self, page, getattr(sys.modules[__name__], page)(self))
else:
setattr(self, page, getattr(sys.modules[__name__], 'BasePage')(self))
setattr(self, childpage, ChildPage(self, getattr(self, page)))
self.pages.addWidget(getattr(self, childpage))
def createConnections(self):
'''
创建按钮与页面的链接
'''
for buttons in self.pagetags:
for item in buttons:
button = item + 'Button'
getattr(self.navigationPage, button).clicked.connect(self.childpageChange)
def childpageChange(self):
'''
页面切换响应函数
'''
currentpage = getattr(self, unicode('child' + self.sender().objectName()[:-6]) + 'Page')
if hasattr(self, 'navigationPage'):
if currentpage is self.navigationPage:
currentpage.parent.parent().statusBar().hide()
self.pages.setCurrentWidget(currentpage)
self.sender().setFocus()
#切换QChrome页面时进行实时刷新显示预览
if isinstance(currentpage.child, QChromePage):
currentpage.child.refreshcontent()
@QtCore.pyqtSlot()
def backnavigationPage(self):
self.parent().statusBar().hide()
self.pages.setCurrentWidget(self.navigationPage)
@QtCore.pyqtSlot()
def backPage(self):
index = self.pages.currentIndex()
if index == 1:
self.parent().statusBar().hide()
self.pages.setCurrentWidget(self.navigationPage)
else:
self.pages.setCurrentIndex(index - 1)
@QtCore.pyqtSlot()
def forwardnextPage(self):
index = self.pages.currentIndex()
if index < self.pagecount:
self.pages.setCurrentIndex(index + 1)
else:
self.parent().statusBar().hide()
self.pages.setCurrentWidget(self.navigationPage)
def fadeInWidget(self, index):
'''
页面切换时槽函数实现淡入淡出效果
'''
self.faderWidget = FaderWidget(self.pages.widget(0.5))
self.faderWidget.start()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.initFrame()
self.centeralwindow = MetroWindow(self)
self.setCentralWidget(self.centeralwindow)
self.createMenus()
self.createToolbars()
self.createStatusbar()
self.setskin()
currentpage = self.centralWidget().pages.currentWidget()
currentpage.navigation.setVisible(windowsoptions['mainwindow']['navigationvisual'])
def initFrame(self):
title = windowsoptions['mainwindow']['title']
postion = windowsoptions['mainwindow']['postion']
minsize = windowsoptions['mainwindow']['minsize']
size = windowsoptions['mainwindow']['size']
windowicon = windowsoptions['mainwindow']['windowicon']
fullscreenflag = windowsoptions['mainwindow']['fullscreenflag']
navigationvisual = windowsoptions['mainwindow']['navigationvisual']
self.setWindowTitle(title)
self.setWindowIcon(QtGui.QIcon(windowicon)) # 设置程序图标
self.setMinimumSize(minsize[0], minsize[1])
width = QtGui.QDesktopWidget().availableGeometry().width() * 5 / 6
height = QtGui.QDesktopWidget().availableGeometry().height() * 7 / 8
self.setGeometry(postion[0], postion[1], width, height) # 初始化窗口位置和大小
self.center() # 将窗口固定在屏幕中间
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.fullscreenflag = fullscreenflag # 初始化时非窗口最大话标志
if self.fullscreenflag:
self.showFullScreen()
else:
self.showNormal()
self.navigationvisual = navigationvisual # 导航标志,初始化时显示导航
self.layout().setContentsMargins(0, 0, 0, 0)
# self.setWindowFlags(QtCore.Qt.CustomizeWindowHint) # 隐藏标题栏, 可以拖动边框改变大小
# self.setWindowFlags(QtCore.Qt.FramelessWindowHint) # 隐藏标题栏, 无法改变大小
self.setWindowFlags(QtCore.Qt.FramelessWindowHint) # 无边框, 带系统菜单, 可以最小化
def setskin(self):
for buttons in windowsoptions['mainwindow']['centralwindow']['pagetags']:
for item in buttons:
childpage = getattr(self.centeralwindow, 'child' + item + 'Page')
set_skin(childpage, os.sep.join(['skin', 'qss', 'MetroNavigationBar.qss'])) # 设置导航工具条的样式
set_skin(self, os.sep.join(['skin', 'qss', 'MetroMainwindow.qss'])) # 设置主窗口样式
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def createMenus(self):
menusettings = windowsoptions['mainwindow']['menusettings']
menubar = self.menuBar()
menubar.setVisible(menusettings['visual'])
for menu in menusettings['menus']:
setattr(
self,
'%smenu' % menu['name'],
menubar.addMenu(u'%s%s' % (menu['name'], menu['name_zh']))
)
submenu = getattr(self, '%smenu' % menu['name'])
for menuaction in menu['actions']:
setattr(
self,
'%sAction' % menuaction['trigger'],
QtGui.QAction(
QtGui.QIcon(QtGui.QPixmap(menuaction['icon'])),
'%s%s' % (menuaction['name'], menuaction['name_zh']),
self
)
)
if hasattr(self, 'action%s' % menuaction['trigger']):
action = getattr(self, '%sAction' % menuaction['trigger'])
action.setShortcut(QtGui.QKeySequence(menuaction['shortcut']))
submenu.addAction(action)
action.triggered.connect(
getattr(self, 'action%s' % menuaction['trigger'])
)
else:
action = getattr(self, '%sAction' % menuaction['trigger'])
action.setShortcut(QtGui.QKeySequence(menuaction['shortcut']))
submenu.addAction(action)
action.triggered.connect(
getattr(self, 'actionNotImplement')
)
def createToolbars(self):
toolbarsettings = windowsoptions['mainwindow']['toolbarsettings']
self.toolbar = QtGui.QToolBar(self)
self.toolbar.setMovable(toolbarsettings['movable'])
self.toolbar.setVisible(toolbarsettings['visual'])
self.addToolBar(toolbarsettings['dockArea'], self.toolbar)
for toolbar in toolbarsettings['toolbars']:
setattr(
self,
'%sAction' % toolbar['trigger'],
QtGui.QAction(
QtGui.QIcon(QtGui.QPixmap(toolbar['icon'])),
'%s%s' % (toolbar['name'], toolbar['name_zh']),
self
)
)
if hasattr(self, 'action%s' % toolbar['trigger']):
action = getattr(self, '%sAction' % toolbar['trigger'])
action.setShortcut(QtGui.QKeySequence(toolbar['shortcut']))
action.setToolTip(toolbar['tooltip'])
self.toolbar.addAction(action)
action.triggered.connect(
getattr(self, 'action%s' % toolbar['trigger'])
)
self.toolbar.widgetForAction(action).setObjectName(toolbar['id'])
else:
action = getattr(self, '%sAction' % toolbar['trigger'])
action.setShortcut(QtGui.QKeySequence(toolbar['shortcut']))
action.setToolTip(toolbar['tooltip'])
self.toolbar.addAction(action)
action.triggered.connect(
getattr(self, 'actionNotImplement')
)
self.toolbar.widgetForAction(action).setObjectName(toolbar['id'])
def createStatusbar(self):
statusbarsettings = windowsoptions['mainwindow']['statusbarsettings']
self.statusbar = QtGui.QStatusBar()
self.setStatusBar(self.statusbar)
self.statusbar.showMessage(statusbarsettings['initmessage'])
self.statusbar.setMinimumHeight(statusbarsettings['minimumHeight'])
self.statusbar.setVisible(statusbarsettings['visual'])
def actionAbout(self):
pass
def actionNotImplement(self):
utildialog.msg(u'This action is not Implemented', windowsoptions['msgdialog'])
@QtCore.pyqtSlot()
def windowMaxNormal(self):
if self.isFullScreen():
self.showNormal()
self.sender().setObjectName("MaxButton")
set_skin(self, os.sep.join(['skin', 'qss', 'MetroMainwindow.qss'])) # 设置主窗口样式
else:
self.showFullScreen()
self.sender().setObjectName("MaxNormalButton")
set_skin(self, os.sep.join(['skin', 'qss', 'MetroMainwindow.qss'])) # 设置主窗口样式
def closeEvent(self, evt):
flag, exitflag = utildialog.exit(windowsoptions['exitdialog'])
if flag:
for item in exitflag:
if item == 'minRadio' and exitflag[item]:
self.showMinimized()
evt.ignore()
elif item == 'exitRadio' and exitflag[item]:
evt.accept()
elif item == 'exitsaveRadio' and exitflag[item]:
evt.accept()
self.saveoptions()
with open(os.sep.join([os.getcwd(), 'options', 'windowsoptions.json']), 'wb') as f:
json.dump(windowsoptions, f, indent=4)
else:
evt.ignore()
def saveoptions(self):
windowsoptions['mainwindow']['fullscreenflag'] = self.fullscreenflag
windowsoptions['mainwindow']['navigationvisual'] = \
self.centeralwindow.pages.currentWidget().navigation.isVisible()
windowsoptions['mainwindow']['menusettings']['visual'] = \
self.menuBar().isVisible()
windowsoptions['mainwindow']['statusbarsettings']['visual'] = \
self.statusBar().isVisible()
def keyPressEvent(self, evt):
if evt.key() == QtCore.Qt.Key_Escape:
self.close()
elif evt.key() == QtCore.Qt.Key_F5:
if not self.fullscreenflag:
self.showFullScreen()
self.fullscreenflag = True
else:
self.showNormal()
self.fullscreenflag = False
elif evt.key() == QtCore.Qt.Key_F10:
currentpage = self.centralWidget().pages.currentWidget()
if hasattr(currentpage, 'navigation'):
if self.navigationvisual:
currentpage.navigation.setVisible(False)
self.navigationvisual = False
else:
currentpage.navigation.setVisible(True)
self.navigationvisual = True
elif evt.key() == QtCore.Qt.Key_F9:
if self.menuBar().isVisible():
self.menuBar().hide()
else:
self.menuBar().show()
elif evt.key() == QtCore.Qt.Key_F8:
if self.statusbar.isVisible():
self.statusbar.hide()
else:
self.statusbar.show()
def mousePressEvent(self,event):
# 鼠标点击事件
if event.button() == QtCore.Qt.LeftButton:
self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self,event):
# 鼠标移动事件
if hasattr(self, "dragPosition"):
if event.buttons() == QtCore.Qt.LeftButton:
self.move(event.globalPos() - self.dragPosition)
event.accept()
class SplashScreen(QtGui.QSplashScreen):
def __init__(self, splash_image):
super(SplashScreen, self).__init__(splash_image) # 启动程序的图片
self.setWindowModality(QtCore.Qt.ApplicationModal)
def fadeTicker(self, keep_t):
self.setWindowOpacity(0)
t = 0
while t <= 50:
newOpacity = self.windowOpacity() + 0.02 # 设置淡入
if newOpacity > 1:
break
self.setWindowOpacity(newOpacity)
self.show()
t -= 1
time.sleep(0.04)
self.show()
time.sleep(keep_t)
t = 0
while t <= 50:
newOpacity = self.windowOpacity() - 0.02 # 设置淡出
if newOpacity < 0:
self.close()
break
self.setWindowOpacity(newOpacity)
self.show()
t += 1
time.sleep(0.04)
if __name__ == '__main__':
import sys
if sys.platform == "linux2":
import platform
if platform.architecture()[0] == "32bit":
QtGui.QApplication.addLibraryPath(
'/usr/lib/%s-linux-gnu/qt5/plugins/' % 'i386')
else:
QtGui.QApplication.addLibraryPath(
'/usr/lib/%s-linux-gnu/qt5/plugins/' % platform.machine())
app = QtGui.QApplication(sys.argv)
splash = SplashScreen(QtGui.QPixmap(windowsoptions['splashimg']))
splash.fadeTicker(0)
app.processEvents()
main = MainWindow()
main.show()
splash.finish(main)
sys.exit(app.exec_())