Files

190 lines
7.0 KiB
Python

# coding:utf-8
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QSize, QUrl, QPoint
from PyQt5.QtGui import QIcon, QDesktopServices, QColor
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QApplication, QFrame, QStackedWidget
from qfluentwidgets import (NavigationItemPosition, MessageBox, MSFluentTitleBar, MSFluentWindow,
TabBar, SubtitleLabel, setFont, TabCloseButtonDisplayMode, IconWidget,
TransparentDropDownToolButton, TransparentToolButton, setTheme, Theme, isDarkTheme)
from qfluentwidgets import FluentIcon as FIF
from qframelesswindow import AcrylicWindow
class Widget(QFrame):
def __init__(self, text: str, parent=None):
super().__init__(parent=parent)
self.label = SubtitleLabel(text, self)
self.hBoxLayout = QHBoxLayout(self)
setFont(self.label, 24)
self.label.setAlignment(Qt.AlignCenter)
self.hBoxLayout.addWidget(self.label, 1, Qt.AlignCenter)
self.setObjectName(text.replace(' ', '-'))
class TabInterface(QFrame):
""" Tab interface """
def __init__(self, text: str, icon, objectName, parent=None):
super().__init__(parent=parent)
self.iconWidget = IconWidget(icon, self)
self.label = SubtitleLabel(text, self)
self.iconWidget.setFixedSize(120, 120)
self.vBoxLayout = QVBoxLayout(self)
self.vBoxLayout.setAlignment(Qt.AlignCenter)
self.vBoxLayout.setSpacing(30)
self.vBoxLayout.addWidget(self.iconWidget, 0, Qt.AlignCenter)
self.vBoxLayout.addWidget(self.label, 0, Qt.AlignCenter)
setFont(self.label, 24)
self.setObjectName(objectName)
class CustomTitleBar(MSFluentTitleBar):
""" Title bar with icon and title """
def __init__(self, parent):
super().__init__(parent)
# add buttons
self.toolButtonLayout = QHBoxLayout()
color = QColor(206, 206, 206) if isDarkTheme() else QColor(96, 96, 96)
self.searchButton = TransparentToolButton(FIF.SEARCH_MIRROR.icon(color=color), self)
self.forwardButton = TransparentToolButton(FIF.RIGHT_ARROW.icon(color=color), self)
self.backButton = TransparentToolButton(FIF.LEFT_ARROW.icon(color=color), self)
self.forwardButton.setDisabled(True)
self.toolButtonLayout.setContentsMargins(20, 0, 20, 0)
self.toolButtonLayout.setSpacing(15)
self.toolButtonLayout.addWidget(self.searchButton)
self.toolButtonLayout.addWidget(self.backButton)
self.toolButtonLayout.addWidget(self.forwardButton)
self.hBoxLayout.insertLayout(4, self.toolButtonLayout)
# add tab bar
self.tabBar = TabBar(self)
self.tabBar.setMovable(True)
self.tabBar.setTabMaximumWidth(220)
self.tabBar.setTabShadowEnabled(False)
self.tabBar.setTabSelectedBackgroundColor(QColor(255, 255, 255, 125), QColor(255, 255, 255, 50))
# self.tabBar.setScrollable(True)
# self.tabBar.setCloseButtonDisplayMode(TabCloseButtonDisplayMode.ON_HOVER)
self.tabBar.tabCloseRequested.connect(self.tabBar.removeTab)
self.tabBar.currentChanged.connect(lambda i: print(self.tabBar.tabText(i)))
self.hBoxLayout.insertWidget(5, self.tabBar, 1)
self.hBoxLayout.setStretch(6, 0)
# add avatar
self.avatar = TransparentDropDownToolButton('resource/shoko.png', self)
self.avatar.setIconSize(QSize(26, 26))
self.avatar.setFixedHeight(30)
self.hBoxLayout.insertWidget(7, self.avatar, 0, Qt.AlignRight)
self.hBoxLayout.insertSpacing(8, 20)
def canDrag(self, pos: QPoint):
if not super().canDrag(pos):
return False
pos.setX(pos.x() - self.tabBar.x())
return not self.tabBar.tabRegion().contains(pos)
class Window(MSFluentWindow):
def __init__(self):
self.isMicaEnabled = False
super().__init__()
self.setTitleBar(CustomTitleBar(self))
self.tabBar = self.titleBar.tabBar # type: TabBar
# create sub interface
self.homeInterface = QStackedWidget(self, objectName='homeInterface')
self.appInterface = Widget('Application Interface', self)
self.videoInterface = Widget('Video Interface', self)
self.libraryInterface = Widget('library Interface', self)
self.initNavigation()
self.initWindow()
def initNavigation(self):
self.addSubInterface(self.homeInterface, FIF.HOME, '主页', FIF.HOME_FILL)
self.addSubInterface(self.appInterface, FIF.APPLICATION, '应用')
self.addSubInterface(self.videoInterface, FIF.VIDEO, '视频')
self.addSubInterface(self.libraryInterface, FIF.BOOK_SHELF,
'', FIF.LIBRARY_FILL, NavigationItemPosition.BOTTOM)
self.navigationInterface.addItem(
routeKey='Help',
icon=FIF.HELP,
text='帮助',
onClick=self.showMessageBox,
selectable=False,
position=NavigationItemPosition.BOTTOM,
)
self.navigationInterface.setCurrentItem(
self.homeInterface.objectName())
# add tab
self.addTab('Heart', 'As long as you love me', icon='resource/Heart.png')
self.tabBar.currentChanged.connect(self.onTabChanged)
self.tabBar.tabAddRequested.connect(self.onTabAddRequested)
def initWindow(self):
self.resize(1100, 750)
self.setWindowIcon(QIcon(':/qfluentwidgets/images/logo.png'))
self.setWindowTitle('PyQt-Fluent-Widgets')
desktop = QApplication.desktop().availableGeometry()
w, h = desktop.width(), desktop.height()
self.move(w//2 - self.width()//2, h//2 - self.height()//2)
def showMessageBox(self):
w = MessageBox(
'支持作者🥰',
'个人开发不易,如果这个项目帮助到了您,可以考虑请作者喝一瓶快乐水🥤。您的支持就是作者开发和维护项目的动力🚀',
self
)
w.yesButton.setText('来啦老弟')
w.cancelButton.setText('下次一定')
if w.exec():
QDesktopServices.openUrl(QUrl("https://afdian.net/a/zhiyiYo"))
def onTabChanged(self, index: int):
objectName = self.tabBar.currentTab().routeKey()
self.homeInterface.setCurrentWidget(self.findChild(TabInterface, objectName))
self.stackedWidget.setCurrentWidget(self.homeInterface)
def onTabAddRequested(self):
text = f'硝子酱一级棒卡哇伊×{self.tabBar.count()}'
self.addTab(text, text, 'resource/Smiling_with_heart.png')
def addTab(self, routeKey, text, icon):
self.tabBar.addTab(routeKey, text, icon)
self.homeInterface.addWidget(TabInterface(text, icon, routeKey, self))
if __name__ == '__main__':
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
# setTheme(Theme.DARK)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()