refactor: folder card in settings

This commit is contained in:
RedDeadDepresso
2024-09-24 17:59:51 +01:00
parent 0e32b48abe
commit 1047da9aa0
2 changed files with 171 additions and 94 deletions

View File

@@ -0,0 +1,101 @@
import os
import pathlib
import subprocess
from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QFileDialog
from qfluentwidgets import SettingCard, FluentIconBase, FluentIcon, CommandBar, Action, LineEdit, LineEditButton
from typing import Union
from ..common.config import cfg
class FolderLineEdit(LineEdit):
""" Search line edit """
def __init__(self, parent=None):
super().__init__(parent)
self.validButton = LineEditButton(FluentIcon.ACCEPT, self)
self.hBoxLayout.addWidget(self.validButton, 0, Qt.AlignRight)
self.setTextMargins(0, 0, 59, 0)
def setValid(self, value: bool):
if value:
self.validButton._icon = FluentIcon.ACCEPT
else:
self.validButton._icon = FluentIcon.CLOSE
class FolderSettingCard(SettingCard):
def __init__(self, configItem, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
configItem: TimeConfigItem
configuration item operated by the card
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
the title of card
content: str
the content of card
parent: QWidget
parent widget
"""
super().__init__(icon, title, content, parent)
self.configItem = configItem
self.lineEdit = FolderLineEdit()
self.lineEdit.setText(configItem.value)
self.__initCommandBar()
self.__initLayout()
self.__connectSignalToSlot()
def __initCommandBar(self):
self.commandBar = CommandBar()
explorerAction = Action(FluentIcon.FOLDER, 'Show in Explorer')
explorerAction.triggered.connect(self.openExplorer)
self.commandBar.addHiddenAction(explorerAction)
browseAction = Action(FluentIcon.EDIT, 'Browse')
browseAction.triggered.connect(self.browse)
self.commandBar.addHiddenAction(browseAction)
def __initLayout(self):
self.setFixedHeight(70)
self.vBoxLayout.addWidget(self.lineEdit)
self.hBoxLayout.setStretch(2, 10)
self.hBoxLayout.addSpacing(8)
self.hBoxLayout.addWidget(self.commandBar, 0, Qt.AlignmentFlag.AlignRight)
self.hBoxLayout.addSpacing(16)
def __connectSignalToSlot(self):
self.lineEdit.textChanged.connect(self.pathValid)
def pathValid(self, text):
if not text:
cfg.set(self.configItem, "")
self.lineEdit.setValid(True)
return
path = pathlib.Path(text)
if path.is_absolute() and path.is_dir() and path.exists():
cfg.set(self.configItem, text)
self.lineEdit.setValid(True)
else:
self.lineEdit.setValid(False)
def openExplorer(self):
if self.configItem.value:
file_path = os.path.normpath(self.configItem.value)
subprocess.Popen(f'explorer /select,"{file_path}"')
def browse(self):
""" download folder card clicked slot """
folder = QFileDialog.getExistingDirectory(
self, self.tr("Choose folder"), "./")
if not folder or cfg.get(self.configItem) == folder:
return

View File

@@ -14,6 +14,7 @@ from ..common.config import cfg, HELP_URL, FEEDBACK_URL, AUTHOR, VERSION, YEAR,
from ..components.line_edit_card import LineEditSettingCard
from ..common.signal_bus import signalBus
from ..common.style_sheet import StyleSheet
from ..components.folder_setting_card import FolderSettingCard
class SettingInterface(ScrollArea):
@@ -30,23 +31,21 @@ class SettingInterface(ScrollArea):
# core
self.coreGroup = SettingCardGroup(
self.tr("Core"), self.scrollWidget)
self.gamePathCard = PushSettingCard(
self.tr('Choose folder'),
self.gamePathCard = FolderSettingCard(
cfg.gamePath,
FIF.GAME,
self.tr("Koikatsu directory"),
cfg.get(cfg.gamePath),
self.coreGroup
parent=self.coreGroup
)
# createBackup
self.backupGroup = SettingCardGroup(
self.tr("Create Backup"), self.scrollWidget)
self.backupPathCard = PushSettingCard(
self.tr('Choose folder'),
self.backupPathCard = FolderSettingCard(
cfg.backupPath,
FIF.ZIP_FOLDER,
self.tr("Backup directory"),
cfg.get(cfg.backupPath),
self.backupGroup
parent=self.backupGroup
)
self.filenameCard = LineEditSettingCard(
cfg.filename,
@@ -80,12 +79,11 @@ class SettingInterface(ScrollArea):
# fckks
self.fckksGroup = SettingCardGroup(
self.tr("Filter & Convert"), self.scrollWidget)
self.fckksPathCard = PushSettingCard(
self.tr('Choose folder'),
self.fckksPathCard = FolderSettingCard(
cfg.fccksPath,
FIF.DOWNLOAD,
self.tr("Input directory"),
cfg.get(cfg.fccksPath),
self.fckksGroup
self.tr("Backup directory"),
parent=self.fckksGroup
)
self.convertCard = SwitchSettingCard(
FIF.UPDATE,
@@ -98,12 +96,11 @@ class SettingInterface(ScrollArea):
# installChara
self.installGroup = SettingCardGroup(
self.tr("Install Chara"), self.scrollWidget)
self.installPathCard = PushSettingCard(
self.tr('Choose folder'),
self.installPathCard = FolderSettingCard(
cfg.installPath,
FIF.DOWNLOAD,
self.tr("Input directory"),
cfg.get(cfg.installPath),
self.installGroup,
parent=self.installGroup
)
self.fileConflictsCard = ComboBoxSettingCard(
cfg.fileConflicts,
@@ -124,12 +121,11 @@ class SettingInterface(ScrollArea):
# removeChara
self.removeGroup = SettingCardGroup(
self.tr("Remove Chara"), self.scrollWidget)
self.removePathCard = PushSettingCard(
self.tr('Choose folder'),
self.removePathCard = FolderSettingCard(
cfg.removePath,
FIF.DOWNLOAD,
self.tr("Input directory"),
cfg.get(cfg.removePath),
self.removeGroup,
parent=self.removeGroup
)
# personalization
@@ -181,53 +177,53 @@ class SettingInterface(ScrollArea):
)
# material
self.materialGroup = SettingCardGroup(
self.tr('Material'), self.scrollWidget)
self.blurRadiusCard = RangeSettingCard(
cfg.blurRadius,
FIF.ALBUM,
self.tr('Acrylic blur radius'),
self.tr('The greater the radius, the more blurred the image'),
self.materialGroup
)
# self.materialGroup = SettingCardGroup(
# self.tr('Material'), self.scrollWidget)
# self.blurRadiusCard = RangeSettingCard(
# cfg.blurRadius,
# FIF.ALBUM,
# self.tr('Acrylic blur radius'),
# self.tr('The greater the radius, the more blurred the image'),
# self.materialGroup
# )
# update software
self.updateSoftwareGroup = SettingCardGroup(
self.tr("Software update"), self.scrollWidget)
self.updateOnStartUpCard = SwitchSettingCard(
FIF.UPDATE,
self.tr('Check for updates when the application starts'),
self.tr('The new version will be more stable and have more features'),
configItem=cfg.checkUpdateAtStartUp,
parent=self.updateSoftwareGroup
)
# self.updateSoftwareGroup = SettingCardGroup(
# self.tr("Software update"), self.scrollWidget)
# self.updateOnStartUpCard = SwitchSettingCard(
# FIF.UPDATE,
# self.tr('Check for updates when the application starts'),
# self.tr('The new version will be more stable and have more features'),
# configItem=cfg.checkUpdateAtStartUp,
# parent=self.updateSoftwareGroup
# )
# application
self.aboutGroup = SettingCardGroup(self.tr('About'), self.scrollWidget)
self.helpCard = HyperlinkCard(
HELP_URL,
self.tr('Open help page'),
FIF.HELP,
self.tr('Help'),
self.tr(
'Discover new features and learn useful tips about PyQt-Fluent-Widgets'),
self.aboutGroup
)
self.feedbackCard = PrimaryPushSettingCard(
self.tr('Provide feedback'),
FIF.FEEDBACK,
self.tr('Provide feedback'),
self.tr('Help us improve PyQt-Fluent-Widgets by providing feedback'),
self.aboutGroup
)
self.aboutCard = PrimaryPushSettingCard(
self.tr('Check update'),
FIF.INFO,
self.tr('About'),
'© ' + self.tr('Copyright') + f" {YEAR}, {AUTHOR}. " +
self.tr('Version') + " " + VERSION,
self.aboutGroup
)
# self.aboutGroup = SettingCardGroup(self.tr('About'), self.scrollWidget)
# self.helpCard = HyperlinkCard(
# HELP_URL,
# self.tr('Open help page'),
# FIF.HELP,
# self.tr('Help'),
# self.tr(
# 'Discover new features and learn useful tips about PyQt-Fluent-Widgets'),
# self.aboutGroup
# )
# self.feedbackCard = PrimaryPushSettingCard(
# self.tr('Provide feedback'),
# FIF.FEEDBACK,
# self.tr('Provide feedback'),
# self.tr('Help us improve PyQt-Fluent-Widgets by providing feedback'),
# self.aboutGroup
# )
# self.aboutCard = PrimaryPushSettingCard(
# self.tr('Check update'),
# FIF.INFO,
# self.tr('About'),
# '© ' + self.tr('Copyright') + f" {YEAR}, {AUTHOR}. " +
# self.tr('Version') + " " + VERSION,
# self.aboutGroup
# )
self.__initWidget()
@@ -277,13 +273,13 @@ class SettingInterface(ScrollArea):
self.personalGroup.addSettingCard(self.zoomCard)
self.personalGroup.addSettingCard(self.languageCard)
self.materialGroup.addSettingCard(self.blurRadiusCard)
# self.materialGroup.addSettingCard(self.blurRadiusCard)
self.updateSoftwareGroup.addSettingCard(self.updateOnStartUpCard)
# self.updateSoftwareGroup.addSettingCard(self.updateOnStartUpCard)
self.aboutGroup.addSettingCard(self.helpCard)
self.aboutGroup.addSettingCard(self.feedbackCard)
self.aboutGroup.addSettingCard(self.aboutCard)
# self.aboutGroup.addSettingCard(self.helpCard)
# self.aboutGroup.addSettingCard(self.feedbackCard)
# self.aboutGroup.addSettingCard(self.aboutCard)
# add setting card group to layout
self.expandLayout.setSpacing(28)
@@ -294,9 +290,9 @@ class SettingInterface(ScrollArea):
self.expandLayout.addWidget(self.installGroup)
self.expandLayout.addWidget(self.removeGroup)
self.expandLayout.addWidget(self.personalGroup)
self.expandLayout.addWidget(self.materialGroup)
self.expandLayout.addWidget(self.updateSoftwareGroup)
self.expandLayout.addWidget(self.aboutGroup)
# self.expandLayout.addWidget(self.materialGroup)
# self.expandLayout.addWidget(self.updateSoftwareGroup)
# self.expandLayout.addWidget(self.aboutGroup)
def __showRestartTooltip(self):
""" show restart tooltip """
@@ -307,29 +303,9 @@ class SettingInterface(ScrollArea):
parent=self
)
def __onFolderCardClicked(self, item, card):
""" download folder card clicked slot """
folder = QFileDialog.getExistingDirectory(
self, self.tr("Choose folder"), "./")
if not folder or cfg.get(item) == folder:
return
cfg.set(item, folder)
card.setContent(folder)
def __connectSignalToSlot(self):
""" connect signal to slot """
cfg.appRestartSig.connect(self.__showRestartTooltip)
# gamePath
self.gamePathCard.clicked.connect(lambda: self.__onFolderCardClicked(cfg.gamePath, self.gamePathCard))
# backup
self.backupPathCard.clicked.connect(lambda: self.__onFolderCardClicked(cfg.backupPath, self.backupPathCard))
# fckks
self.fckksPathCard.clicked.connect(lambda: self.__onFolderCardClicked(cfg.fccksPath, self.fckksPathCard))
# install
self.installPathCard.clicked.connect(lambda: self.__onFolderCardClicked(cfg.installPath, self.installPathCard))
# remove
self.removePathCard.clicked.connect(lambda: self.__onFolderCardClicked(cfg.removePath, self.removePathCard))
# personalization
self.themeCard.optionChanged.connect(lambda ci: setTheme(cfg.get(ci)))
@@ -337,8 +313,8 @@ class SettingInterface(ScrollArea):
self.micaCard.checkedChanged.connect(signalBus.micaEnableChanged)
# about
self.feedbackCard.clicked.connect(
lambda: QDesktopServices.openUrl(QUrl(FEEDBACK_URL)))
# self.feedbackCard.clicked.connect(
# lambda: QDesktopServices.openUrl(QUrl(FEEDBACK_URL)))
def scrollToGroup(self, group):
self.verticalScrollBar().setValue(group.y())