-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfmainwindow.py
276 lines (230 loc) · 8.86 KB
/
fmainwindow.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from .ftitlebar import FTitleBar
from .fstatusbar import FStatusBar
class FMainWindow(QMainWindow):
sizeChanged = Signal()
def __init__(self):
super(FMainWindow, self).__init__()
self._initFlags()
self._initWindowFlags()
self._initMainWindow()
self._initTitleBar()
self._initTitlebarConnect()
self._initToolbars()
self._initStatusbar()
self._initSystemTray()
self.setAttribute(Qt.WA_Hover)
self.installEventFilter(self)
self.isSideClicked = False
self.isCusorLeftSide = False
self.isCusorRightSide = False
self.isCusorDownSide = False
def _initFlags(self):
self._framelessflag = True # 无系统边框标志
self._customTitlebarFlag = True # 自定义标题栏标志
self._menubarFlag = False
self._maximizedflag = False # 初始化时窗口最大化标志
self._lockflag = False # 锁定标志
self._pinflag = False # 置顶标志
self._modeflag = False # 手机(True)或桌面模式(False)
desktopWidth = QDesktopWidget().availableGeometry().width()
desktopHeight = QDesktopWidget().availableGeometry().height()
self.default_size = QSize(desktopWidth * 0.8, desktopHeight * 0.8)
self.phone_size = QSize(400, desktopHeight * 0.8)
self.oldPosition = QPoint(0, 0)
def _initWindowFlags(self, flag=True):
framelessflag = flag
if framelessflag:
# 无边框, 带系统菜单, 可以最小化
self.setWindowFlags(
Qt.FramelessWindowHint)
self._framelessflag = framelessflag
def _initMainWindow(self):
self.showNormal()
self.moveCenter()
self.oldPosition = self.pos()
self.setAttribute(Qt.WA_DeleteOnClose)
self.layout().setContentsMargins(0, 0, 0, 0)
def _initSystemTray(self):
self.systemTray = QSystemTrayIcon(self)
self.systemTray.activated.connect(self.onSystemTrayIconClicked)
def onSystemTrayIconClicked(self, reason):
if reason == QSystemTrayIcon.Unknown:
pass
elif reason == QSystemTrayIcon.Context:
pass
elif reason == QSystemTrayIcon.DoubleClick:
pass
elif reason == QSystemTrayIcon.Trigger:
self.setVisible(not self.isVisible())
elif reason == QSystemTrayIcon.MiddleClick:
pass
else:
pass
def setSystemTrayMenu(self, menu):
if isinstance(menu, QMenu) and \
hasattr(self, 'systemTray') and self.systemTray:
self.systemTray.setContextMenu(menu)
def setCentralWidget(self, widget):
centralWidget = QFrame(self)
mainlayout = QVBoxLayout()
mainlayout.addWidget(self.titleBar())
mainlayout.addWidget(widget)
mainlayout.setContentsMargins(0, 0, 0, 0)
mainlayout.setSpacing(0)
centralWidget.setLayout(mainlayout)
super(FMainWindow, self).setCentralWidget(centralWidget)
def _initTitleBar(self, flag=True):
self._customTitlebarFlag = flag
if self._customTitlebarFlag:
self._titleBar = FTitleBar()
def _initTitlebarConnect(self):
if self.isFtitleBarExisted():
self._titleBar.locked.connect(self.setLocked)
self._titleBar.modeed.connect(self.setDesktopMode)
self._titleBar.pined.connect(self.setPined)
self._titleBar.minimized.connect(self.showMinimized)
self._titleBar.maximized.connect(self.swithMaxNormal)
def isFtitleBarExisted(self):
if self._customTitlebarFlag:
if isinstance(self._titleBar, FTitleBar):
return True
return False
def titleBar(self):
if self.isFtitleBarExisted():
return self._titleBar
def _initToolbars(self):
pass
def _initStatusbar(self):
statusbar = FStatusBar(self)
self.setStatusBar(statusbar)
def setWindowIcon(self, icon):
if not isinstance(icon, QIcon):
qicon = QIcon(icon)
else:
qicon = icon
super(FMainWindow, self).setWindowIcon(qicon)
if self.isFtitleBarExisted():
self._titleBar.setLogo(qicon)
self.systemTray.setIcon(qicon)
self.systemTray.show()
def setWindowTitle(self, title):
super(FMainWindow, self).setWindowTitle(title)
if self.isFtitleBarExisted():
self._titleBar.setTitle(title)
def setDesktopMode(self, flag):
self._modeflag = flag
if flag:
self.resize(self.phone_size.width(), self.height())
else:
self.resize(self.default_size)
def isDesktopMode(self):
return self._modeflag
def setPined(self, flag):
self._pinflag = flag
if flag:
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
else:
self.setWindowFlags(
self.windowFlags() & (~Qt.WindowStaysOnTopHint))
self.show()
def isPined(self):
return self._pinflag
def setLocked(self, flag):
self._lockflag = flag
def isLocked(self):
return self._lockflag
def moveCenter(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def swithMaxNormal(self, flag):
self._maximizedflag = flag
if self._maximizedflag:
self.showMaximized()
else:
self.showNormal()
def showMaximized(self):
self.resize(QDesktopWidget().availableGeometry().size())
self.moveCenter()
def showNormal(self):
self.resize(self.default_size)
self.move(self.oldPosition)
def isMaximized(self):
return self._maximizedflag
def resizeEvent(self, event):
super(FMainWindow, self).resizeEvent(event)
self.default_size = event.oldSize()
self.sizeChanged.emit()
def mousePressEvent(self, event):
self.setFocus()
# 鼠标点击事件
if event.button() == Qt.LeftButton:
self.dragPosition = event.globalPos() - \
self.frameGeometry().topLeft()
event.accept()
self.xPos = self.x()
self.yPos = self.y()
self.rdragx = event.x()
self.rdragy = event.y()
self.currentWidth = self.width()
self.currentHeight = self.height()
self.isSideClicked = True
def mouseReleaseEvent(self, event):
# 鼠标释放事件
if hasattr(self, "dragPosition"):
del self.dragPosition
self.isSideClicked = False
self.setCursor(Qt.ArrowCursor)
def mouseMoveEvent(self, event):
self.oldPosition = self.pos()
if self.isSideClicked and self.isCusorRightSide:
w = max(self.minimumWidth(),
self.currentWidth + event.x() - self.rdragx)
h = self.currentHeight
self.resize(w, h)
elif self.isSideClicked and self.isCusorDownSide:
w = self.currentWidth
h = max(self.minimumHeight(),
self.currentHeight + event.y() - self.rdragy)
self.resize(w, h)
# elif self.isSideClicked and self.isCusorLeftSide:
# x = event.x() + self.xPos - self.rdragx
# w = max(self.minimumWidth(),
# self.xPos + self.currentWidth - x)
# self.move(x , self.yPos)
# self.resize(w, self.currentHeight)
else:
# 鼠标移动事件
if self.isMaximized():
event.ignore()
else:
if not self.isLocked():
if hasattr(self, "dragPosition"):
if event.buttons() == Qt.LeftButton:
self.move(
event.globalPos() - self.dragPosition)
event.accept()
def eventFilter(self, obj, event):
if event.type() == QEvent.HoverMove:
if self.width() - event.pos().x() < 5:
self.setCursor(Qt.SizeHorCursor)
self.isCusorRightSide = True
elif self.height() - event.pos().y() < 5:
self.setCursor(Qt.SizeVerCursor)
self.isCusorDownSide = True
# elif event.pos().x() < 5:
# self.setCursor(Qt.SizeHorCursor)
# self.isCusorLeftSide = True
elif not self.isSideClicked:
self.setCursor(Qt.ArrowCursor)
self.isCusorLeftSide = False
self.isCusorRightSide = False
self.isCusorDownSide = False
return True
return super(FMainWindow, self).eventFilter(obj, event)