import sys from PySide6.QtCore import Signal from PySide6.QtGui import Qt from PySide6.QtWidgets import QLabel, QFrame, QPushButton, QVBoxLayout, QHBoxLayout, QApplication from app.common.Manager import UserManager from qfluentwidgets import FluentStyleSheet, PrimaryPushButton, TextWrap, LineEdit, InfoBar, InfoBarPosition from qfluentwidgets.components.dialog_box.dialog import Ui_MessageBox from qframelesswindow import FramelessDialog class AddBookInstance: yesSignal = Signal() cancelSignal = Signal() def __init__(self, *args, **kwargs): pass def _setUpUi(self, title, parent): self.titleLabel = QLabel(title, parent) self.ISBN = LineEdit() self.ISBNBox = QHBoxLayout() self.ISBN_text = QLabel('ISBN', parent) self.ISBNBox.addWidget(self.ISBN_text) self.ISBNBox.addWidget(self.ISBN) self.location = LineEdit() self.locationBox = QHBoxLayout() self.location_text = QLabel('藏书位置', parent) self.locationBox.addWidget(self.location_text) self.locationBox.addWidget(self.location) self.buttonGroup = QFrame(parent) self.yesButton = PrimaryPushButton(self.tr('OK'), self.buttonGroup) self.cancelButton = QPushButton(self.tr('Cancel'), self.buttonGroup) self.vBoxLayout = QVBoxLayout(parent) self.textLayout = QVBoxLayout() self.buttonLayout = QHBoxLayout(self.buttonGroup) self.__initWidget() def __initWidget(self): self.__setQss() self.__initLayout() # fixes https://github.com/zhiyiYo/PyQt-Fluent-Widgets/issues/19 self.yesButton.setAttribute(Qt.WA_LayoutUsesWidgetRect) self.cancelButton.setAttribute(Qt.WA_LayoutUsesWidgetRect) self.yesButton.setFocus() self.buttonGroup.setFixedHeight(81) self._adjustText() #self.yesButton.clicked.connect(self.__onYesButtonClicked) self.cancelButton.clicked.connect(self.__onCancelButtonClicked) def _adjustText(self): if self.isWindow(): if self.parent(): w = max(self.titleLabel.width(), self.parent().width()) chars = max(min(w / 9, 140), 30) else: chars = 100 else: w = max(self.titleLabel.width(), self.window().width()) chars = max(min(w / 9, 100), 30) #self.contentLabel.setText(TextWrap.wrap(self.content, chars, False)[0]) def __initLayout(self): self.vBoxLayout.setSpacing(0) self.vBoxLayout.setContentsMargins(0, 0, 0, 0) self.vBoxLayout.addLayout(self.textLayout, 1) self.vBoxLayout.addLayout(self.ISBNBox) self.vBoxLayout.addLayout(self.locationBox) self.vBoxLayout.addWidget(self.buttonGroup, 0, Qt.AlignBottom) self.vBoxLayout.setSizeConstraint(QVBoxLayout.SetMinimumSize) self.textLayout.setSpacing(12) self.textLayout.setContentsMargins(24, 24, 24, 24) self.textLayout.addWidget(self.titleLabel, 0, Qt.AlignTop) self.ISBNBox.setContentsMargins(24, 24, 24, 24) self.locationBox.setContentsMargins(24, 24, 24, 24) self.buttonLayout.setSpacing(12) self.buttonLayout.setContentsMargins(24, 24, 24, 24) self.buttonLayout.addWidget(self.yesButton, 1, Qt.AlignVCenter) self.buttonLayout.addWidget(self.cancelButton, 1, Qt.AlignVCenter) def __onCancelButtonClicked(self): self.reject() self.cancelSignal.emit() def __onYesButtonClicked(self): self.accept() self.yesSignal.emit() def __setQss(self): self.titleLabel.setObjectName("titleLabel") self.buttonGroup.setObjectName('buttonGroup') self.cancelButton.setObjectName('cancelButton') FluentStyleSheet.DIALOG.apply(self) self.yesButton.adjustSize() self.cancelButton.adjustSize() class AddBookInstanceDialog(FramelessDialog, AddBookInstance): yesSignal = Signal() cancelSignal = Signal() def __init__(self, parent=None): super().__init__(parent=parent) self._setUpUi("图书添加", self) self.windowTitleLabel = QLabel("图书添加", self) self.setResizeEnabled(False) self.resize(540, 192) self.titleBar.hide() self.vBoxLayout.insertWidget(0, self.windowTitleLabel, 0, Qt.AlignTop) self.windowTitleLabel.setObjectName('windowTitleLabel') FluentStyleSheet.DIALOG.apply(self) self.setFixedSize(self.size()) self.yesButton.clicked.connect(self.addBook) def setTitleBarVisible(self, isVisible: bool): self.windowTitleLabel.setVisible(isVisible) def addBook(self): isbn = self.ISBN.text() location = self.location.text() if isbn == '' or location == '': InfoBar.warning( title='操作失败', content="你填写的内容不够完全!", orient=Qt.Horizontal, isClosable=False, # disable close button position=InfoBarPosition.TOP_LEFT, duration=2000, parent=self ) else: true = UserManager.addBookInstance(isbn, location) if true: self.ISBN.clear() InfoBar.success( title='添加书籍信息成功', content="添加书籍信息成功,你可以继续添加书籍或退出此界面.", orient=Qt.Horizontal, isClosable=True, position=InfoBarPosition.TOP, duration=2000, parent=self ) else: InfoBar.error( title='操作失败', content="也许你还未录入该书籍信息", orient=Qt.Horizontal, isClosable=True, position=InfoBarPosition.BOTTOM_RIGHT, duration=-1, # won't disappear automatically parent=self )