def __init__(self, parent=None): """包括博客、用户申请""" super(Navigation, self).__init__(parent) self.parent = parent self.frame = QFrame() self.setMaximumWidth(200) self.setWidget(self.frame) self.setWidgetResizable(True) self.frame.setMinimumWidth(200) # 定义3个事件函数,方便扩展。 self.navigationListFunction = self.none self.nativeListFunction = self.none self.singsFunction = self.none with open('QSS/navigation.qss', 'r') as f: style = f.read() self.setStyleSheet(style) self.frame.setStyleSheet(style) # 包括显示信息: 推荐 我的音乐 歌单。 self.setLabels() # 包括详细的内容:发现音乐,FM,MV等。 self.setListViews() self.setLayouts() self.bindConnect()
def setLines(self): """设置装饰用小细线。""" self.line1 = QFrame(self) self.line1.setObjectName("line1") self.line1.setFrameShape(QFrame.VLine) self.line1.setFrameShadow(QFrame.Plain) self.line1.setMaximumSize(1, 25)
def drawPanel(self): self.spaceLine = QFrame(self) self.spaceLine.setObjectName("spaceLine") self.spaceLine.setFrameShape(QFrame.HLine) self.spaceLine.setFrameShadow(QFrame.Plain) self.spaceLine.setLineWidth(2) self.userTable = QTableWidget() self.userTable.setObjectName('userTable') self.userTable.setMinimumWidth(self.width()) self.userTable.setColumnCount(6) self.userTable.setHorizontalHeaderLabels( ['用户名', '昵称', '邮箱', '联系方式', '签名', '添加好友']) self.userTable.setColumnWidth(0, self.width() / 6 * 0.9) self.userTable.setColumnWidth(1, self.width() / 6 * 0.9) self.userTable.setColumnWidth(2, self.width() / 6 * 1.1) self.userTable.setColumnWidth(3, self.width() / 6 * 1.1) self.userTable.setColumnWidth(4, self.width() / 6 * 1.5) self.userTable.setColumnWidth(5, self.width() / 6 * 0.5) self.userTable.horizontalHeader().setStretchLastSection(True) self.userTable.verticalHeader().setVisible(False) self.userTable.setShowGrid(False) self.userTable.setAlternatingRowColors(True) self.userTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.userTable.setSelectionBehavior(QAbstractItemView.SelectRows)
def setLines(self): """设置布局小细线。""" self.line1 = QFrame(self) self.line1.setObjectName("line1") self.line1.setFrameShape(QFrame.HLine) self.line1.setFrameShadow(QFrame.Plain) self.line1.setLineWidth(2)
class ItemWidget(ScrollArea): def __init__(self, parent=None, blog=None): super(ItemWidget, self).__init__() self.parent = parent self.blog = blog self.init() self.setLayouts() self.setData() with open('qss/common.qss', 'r', encoding="utf-8") as f: self.setStyleSheet(f.read()) def init(self): self.headerText = InputLine(self.frame, 600, 32, self.blog.header) self.headerText.setAlignment(Qt.AlignCenter) self.contentText = QTextEdit(self.frame) self.contentText.setMaximumWidth(600) self.contentText.setMaximumHeight(100) self.contentText.setMinimumHeight(100) self.contentText.setText(self.blog.content) self.imageFrame = QFrame() self.imageLayout = QGridLayout() self.imageFrame.setLayout(self.imageLayout) def setLayouts(self): self.mainLayout = VBoxLayout() self.frameLayout = HBoxLayout() self.contentLayout = VBoxLayout() self.contentLayout.addSpacing(30) self.contentLayout.addWidget(self.headerText) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.contentText) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.imageFrame) self.frameLayout.addSpacing(10) self.frameLayout.addLayout(self.contentLayout) self.frameLayout.addSpacing(10) self.mainLayout.addLayout(self.frameLayout) self.mainLayout.addStretch() self.setLayout(self.mainLayout) def setData(self): if self.blog.image is None or self.blog.image == "": return images = self.blog.image.split(":") count = 0 for image in images: row = int(count / 3) col = int(count % 3) pic_label = PicLabel(src=IMAGE_BASE + image, width=180, height=120) self.imageFrame.setFixedHeight(120 * (row + 1) + 10) self.imageLayout.addWidget(pic_label, row, col) count = count + 1 def sizeHint(self): # 每个item控件的大小 return QSize(self.width(), self.height())
def init(self): self.headerText = InputLine(self.frame, 600, 32, self.blog.header) self.headerText.setAlignment(Qt.AlignCenter) self.contentText = QTextEdit(self.frame) self.contentText.setMaximumWidth(600) self.contentText.setMaximumHeight(100) self.contentText.setMinimumHeight(100) self.contentText.setText(self.blog.content) self.imageFrame = QFrame() self.imageLayout = QGridLayout() self.imageFrame.setLayout(self.imageLayout)
def draw(self): self.headerText = InputLine(self.frame, 600, 32, "» 标题 «") self.headerText.setAlignment(Qt.AlignCenter) self.contentText = QTextEdit(self.frame) self.contentText.setMaximumWidth(600) self.contentText.setMaximumHeight(100) self.contentText.setMinimumHeight(100) self.imageFrame = QFrame() self.imageFrame.setToolTip("添加图片") self.imageFrame.setFixedHeight(20) self.addImageButton = QPushButton('☞ ❒') self.addImageButton.clicked.connect(self.addImage) self.addButton = QPushButton("发布") self.addButton.clicked.connect(self.publish)
class Navigation(QScrollArea): def __init__(self, parent=None): """包括博客、用户申请""" super(Navigation, self).__init__(parent) self.parent = parent self.frame = QFrame() self.setMaximumWidth(200) self.setWidget(self.frame) self.setWidgetResizable(True) self.frame.setMinimumWidth(200) # 定义3个事件函数,方便扩展。 self.navigationListFunction = self.none self.nativeListFunction = self.none self.singsFunction = self.none with open('QSS/navigation.qss', 'r') as f: style = f.read() self.setStyleSheet(style) self.frame.setStyleSheet(style) # 包括显示信息: 推荐 我的音乐 歌单。 self.setLabels() # 包括详细的内容:发现音乐,FM,MV等。 self.setListViews() self.setLayouts() self.bindConnect() # 布局。 def setLabels(self): """定义所有的标签。""" self.userLabel = QLabel(" 用户管理") self.userLabel.setObjectName("userManagement") self.userLabel.setMaximumHeight(27) self.blogLabel = QLabel(" 博客管理") self.blogLabel.setObjectName("blogManagement") self.blogLabel.setMaximumHeight(27) # self.myMusic.setMaximumHeight(54) self.otherLabel = QLabel(" 其他...") self.otherLabel.setObjectName("otherLabel") self.otherLabel.setMaximumHeight(27) def setListViews(self): """定义承载功能的ListView""" self.userItemList = QListWidget() self.userItemList.setMaximumHeight(110) self.userItemList.setObjectName("userItemList") self.userItemList.addItem(QListWidgetItem("♠ 个人信息")) self.userItemList.addItem(QListWidgetItem("❧ 新增用户")) self.userItemList.addItem(QListWidgetItem("ღ 用户查找")) self.userItemList.setCurrentRow(0) self.blogItemList = QListWidget() self.blogItemList.setObjectName("blogItemList") self.blogItemList.setMaximumHeight(100) # self.blogItemList.addItem(QListWidgetItem(QIcon('resource/notes.png'), "☯ 博客浏览")) self.blogItemList.addItem(QListWidgetItem("☯ 博客浏览")) self.blogItemList.addItem(QListWidgetItem("☪ 我的博客")) self.blogItemList.addItem(QListWidgetItem("✯ 博客发表")) def setLayouts(self): """定义布局。""" self.mainLayout = VBoxLayout(self.frame) self.mainLayout.addSpacing(10) self.mainLayout.addWidget(self.blogLabel) self.mainLayout.addSpacing(10) self.mainLayout.addWidget(self.blogItemList) self.mainLayout.addSpacing(10) self.mainLayout.addWidget(self.userLabel) self.mainLayout.addSpacing(10) self.mainLayout.addWidget(self.userItemList) self.mainLayout.addSpacing(1) self.mainLayout.addWidget(self.otherLabel) self.mainLayout.addSpacing(1) self.mainLayout.addStretch(1) self.setContentsMargins(0, 0, 0, 0) def bindConnect(self): self.blogItemList.itemPressed.connect(self.blogItemListClickEvent) self.userItemList.itemPressed.connect(self.userItemListClickEvent) def userItemListClickEvent(self): print(self.userItemList.currentRow()) self.blogItemList.setCurrentRow(-1) row = self.userItemList.currentRow() if 0 <= row < 3: self.parent.mainContents.setCurrentIndex(row) def blogItemListClickEvent(self): self.userItemList.setCurrentRow(-1) if self.blogItemList.currentRow() == 0: self.parent.mainContents.setCurrentIndex(3) elif self.blogItemList.currentRow() == 1: self.parent.mainContents.setCurrentIndex(4) elif self.blogItemList.currentRow() == 2: self.parent.mainContents.setCurrentIndex(5) # just a test. def setSingsList(self): pass # 功能。 def none(self): # 没有用的空函数。 pass
class UserList(ScrollArea): def __init__(self, parent=None): super().__init__() self.parent = parent self.setObjectName('userList') self.userService = self.parent.parent.userService self.drawPanel() self.setLayouts() with open('qss/userList.qss', 'r', encoding="utf-8") as f: self.setStyleSheet(f.read()) self.echoUserList() def drawPanel(self): self.spaceLine = QFrame(self) self.spaceLine.setObjectName("spaceLine") self.spaceLine.setFrameShape(QFrame.HLine) self.spaceLine.setFrameShadow(QFrame.Plain) self.spaceLine.setLineWidth(2) self.userTable = QTableWidget() self.userTable.setObjectName('userTable') self.userTable.setMinimumWidth(self.width()) self.userTable.setColumnCount(6) self.userTable.setHorizontalHeaderLabels( ['用户名', '昵称', '邮箱', '联系方式', '签名', '添加好友']) self.userTable.setColumnWidth(0, self.width() / 6 * 0.9) self.userTable.setColumnWidth(1, self.width() / 6 * 0.9) self.userTable.setColumnWidth(2, self.width() / 6 * 1.1) self.userTable.setColumnWidth(3, self.width() / 6 * 1.1) self.userTable.setColumnWidth(4, self.width() / 6 * 1.5) self.userTable.setColumnWidth(5, self.width() / 6 * 0.5) self.userTable.horizontalHeader().setStretchLastSection(True) self.userTable.verticalHeader().setVisible(False) self.userTable.setShowGrid(False) self.userTable.setAlternatingRowColors(True) self.userTable.setEditTriggers(QAbstractItemView.NoEditTriggers) self.userTable.setSelectionBehavior(QAbstractItemView.SelectRows) def setLayouts(self): self.mainLayout = VBoxLayout() self.mainLayout.addSpacing(10) self.contentLayout = HBoxLayout() self.mainLayout.addWidget(self.spaceLine) self.mainLayout.addSpacing(10) self.contentLayout.addSpacing(30) self.contentLayout.addWidget(self.userTable) self.contentLayout.addSpacing(30) self.mainLayout.addLayout(self.contentLayout) self.mainLayout.addSpacing(30) self.setLayout(self.mainLayout) def echoUserList(self): allUser = self.userService.selectAll() for user in allUser: rowCount = self.userTable.rowCount() self.userTable.setRowCount(rowCount + 1) userInfo = [ user.user_name, user.nick_name, user.email, user.phone, user.sign ] for i in range(5): self.userTable.setItem(rowCount, i, QTableWidgetItem(userInfo[i]))
class Header(QFrame): def __init__(self, parent=None): """头部区域,包括图标/搜索/设置/登陆/最大/小化/关闭。""" super(Header, self).__init__() self.setObjectName('Header') self.parent = parent # 用于确定是否最大化. self.isMax = False with open('qss/header.qss', 'r', encoding='utf-8') as f: self.setStyleSheet(f.read()) # 加载按钮设置。 self.setButtons() # 加载小细线装饰。 self.setLines() # 加载布局设置。 self.setLayouts() self.bindConnect() # 布局。 def setButtons(self): """创建所有的按钮。""" self.logoLabel = PicLabel(r'resource/header.png', 32, 32) self.descriptionLabel = QLabel(self) self.descriptionLabel.setText("<b>酌酒援北斗</b>") self.userName = QLabel(self) self.userName.setText("<b>" + self.parent.parent.user.user_name + "</b>") self.userPix = PicLabel(IMAGE_BASE + self.parent.parent.user.avatar, 32, 32) self.userPix.setMinimumSize(22, 22) self.userPix.setObjectName("userPix") self.closeButton = QPushButton('×', self) self.closeButton.setObjectName("closeButton") self.closeButton.setMinimumSize(21, 17) self.showminButton = QPushButton('-', self) self.showminButton.setObjectName("minButton") self.showminButton.setMinimumSize(21, 17) self.showmaxButton = QPushButton('□') self.showmaxButton.setObjectName("maxButton") self.showmaxButton.setMaximumSize(16, 16) self.loginButton = QPushButton("未登录 ▼", self) self.loginButton.setObjectName("loginButton") self.prevButton = QPushButton("<") self.prevButton.setObjectName("prevButton") self.prevButton.setMaximumSize(28, 22) self.prevButton.setMinimumSize(28, 22) self.nextButton = QPushButton(">") self.nextButton.setObjectName("nextButton") self.nextButton.setMaximumSize(28, 22) self.nextButton.setMinimumSize(28, 22) def setLines(self): """设置装饰用小细线。""" self.line1 = QFrame(self) self.line1.setObjectName("line1") self.line1.setFrameShape(QFrame.VLine) self.line1.setFrameShadow(QFrame.Plain) self.line1.setMaximumSize(1, 25) def setLayouts(self): """设置布局。""" self.mainLayout = QHBoxLayout() self.mainLayout.setSpacing(0) self.mainLayout.addWidget(self.logoLabel) self.mainLayout.addWidget(self.descriptionLabel) self.mainLayout.addSpacing(30) self.mainLayout.addWidget(self.prevButton) self.mainLayout.addWidget(self.nextButton) self.mainLayout.addStretch(1) self.mainLayout.addWidget(self.userPix) self.mainLayout.addSpacing(10) self.mainLayout.addWidget(self.userName) self.mainLayout.addSpacing(18) self.mainLayout.addWidget(self.loginButton) self.mainLayout.addSpacing(7) self.mainLayout.addWidget(self.line1) self.mainLayout.addSpacing(30) self.mainLayout.addWidget(self.showminButton) self.mainLayout.addSpacing(8) self.mainLayout.addWidget(self.showmaxButton) self.mainLayout.addSpacing(8) self.mainLayout.addWidget(self.closeButton) self.setLayout(self.mainLayout) # 事件。 """重写鼠标事件,实现窗口拖动。""" def mousePressEvent(self, event): if event.buttons() == Qt.LeftButton: self.parent.m_drag = True self.parent.m_DragPosition = event.globalPos() - self.parent.pos() event.accept() def mouseMoveEvent(self, event): try: if event.buttons() and Qt.LeftButton: self.parent.move(event.globalPos() - self.parent.m_DragPosition) event.accept() except AttributeError: pass def mouseReleaseEvent(self, event): if event.buttons() == Qt.LeftButton: self.m_drag = False def bindConnect(self): self.closeButton.clicked.connect(self.parent.close) self.showminButton.clicked.connect(self.parent.showMinimized) self.showmaxButton.clicked.connect(self.showMaxiOrRevert) def showMaxiOrRevert(self): if self.isMax: self.parent.showNormal() self.isMax = False else: self.parent.showMaximized() self.isMax = True
class MainWindow(QWidget): def __init__(self, parent=None): super(MainWindow, self).__init__() self.parent = parent self.user = self.parent.user self.setObjectName("MainWindow") self.setWindowFlags(Qt.FramelessWindowHint) self.setWindowIcon(QIcon('resource/header.PNG')) self.setWindowTitle("酌酒援北斗") self.resize(1022, 670) self.header = Header(self) self.navigation = Navigation(self) self.personalContent = Personal(self) self.userAdd = UserAdd(self) self.userList = UserList(self) self.blogAdd = BlogAdd(self) self.ownBlog = BlogList(self, self.user.id) self.blogList = BlogList(self) self.mainContents = QStackedWidget() self.mainContents.addWidget(self.personalContent) self.mainContents.addWidget(self.userAdd) self.mainContents.addWidget(self.userList) self.mainContents.addWidget(self.blogList) self.mainContents.addWidget(self.ownBlog) self.mainContents.addWidget(self.blogAdd) self.mainContents.setCurrentIndex(0) # 设置布局小细线。 self.setLines() # 设置布局。 self.setLayouts() with open('qss/window.qss', 'r') as f: self.setStyleSheet(f.read()) def setLines(self): """设置布局小细线。""" self.line1 = QFrame(self) self.line1.setObjectName("line1") self.line1.setFrameShape(QFrame.HLine) self.line1.setFrameShadow(QFrame.Plain) self.line1.setLineWidth(2) def setLayouts(self): self.mainLayout = QVBoxLayout() self.mainLayout.addWidget(self.header) self.mainLayout.addWidget(self.line1) self.contentLayout = QHBoxLayout() self.contentLayout.setStretch(0, 70) self.contentLayout.setStretch(1, 570) self.contentLayout.addWidget(self.navigation) self.contentLayout.addWidget(self.mainContents) self.contentLayout.setSpacing(0) self.contentLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.addLayout(self.contentLayout) # self.mainLayout.addWidget(self.playWidgets) self.mainLayout.setStretch(0, 43) self.mainLayout.setStretch(1, 0) self.mainLayout.setStretch(2, 576) self.mainLayout.setStretch(3, 50) self.mainLayout.setSpacing(0) self.mainLayout.setContentsMargins(0, 0, 0, 0) self.setLayout(self.mainLayout)
class BlogAdd(ScrollArea): def __init__(self, parent=None): super(BlogAdd, self).__init__(self) self.parent = parent self.user = self.parent.user self.blogService = self.parent.parent.blogService self.imageService = self.parent.parent.imageService self.title = None self.contents = None self.images = [] self.image_pic = [] self.draw() self.setLayouts() with open('qss/common.qss', 'r', encoding="utf-8") as f: self.setStyleSheet(f.read()) def draw(self): self.headerText = InputLine(self.frame, 600, 32, "» 标题 «") self.headerText.setAlignment(Qt.AlignCenter) self.contentText = QTextEdit(self.frame) self.contentText.setMaximumWidth(600) self.contentText.setMaximumHeight(100) self.contentText.setMinimumHeight(100) self.imageFrame = QFrame() self.imageFrame.setToolTip("添加图片") self.imageFrame.setFixedHeight(20) self.addImageButton = QPushButton('☞ ❒') self.addImageButton.clicked.connect(self.addImage) self.addButton = QPushButton("发布") self.addButton.clicked.connect(self.publish) def setLayouts(self): self.mainLayout = VBoxLayout() self.frameLayout = HBoxLayout() self.contentLayout = VBoxLayout() self.imageLayout = QGridLayout() self.imageFrame.setLayout(self.imageLayout) self.contentLayout.addSpacing(30) self.contentLayout.addWidget(self.headerText) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.contentText) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.addImageButton) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.imageFrame) self.contentLayout.addSpacing(20) self.contentLayout.addWidget(self.addButton) self.frameLayout.addSpacing(10) self.frameLayout.addLayout(self.contentLayout) self.frameLayout.addSpacing(10) self.mainLayout.addLayout(self.frameLayout) self.mainLayout.addStretch() self.setLayout(self.mainLayout) def addImage(self): paths, _ = QFileDialog.getOpenFileNames(self, "选择图片", '.', '图像文件(*.jpg *.png)') print(paths) for path in paths: image = self.imageService.saveImage(path) if image is not None: count = len(self.images) row = count / 3 col = count % 3 pic_label = PicLabel(src=IMAGE_BASE + image, width=180, height=120) self.imageFrame.setFixedHeight(120 * (row + 1) + 10) self.imageLayout.addWidget(pic_label, row, col) self.images.append(image) self.image_pic.append(pic_label) def publish(self): header = self.headerText.text() if self.isEmpty(header): QMessageBox.information(self, "提示", "标题不能为空!", QMessageBox.Yes) return blog = BlogPro(user_id=self.user.id) blog.header = header blog.content = self.contentText.toPlainText() if len(self.images) > 0: blog.image = ":".join(self.images) result = self.blogService.save(blog) if result is None: QMessageBox.information(self, "提示", "添加成功!", QMessageBox.Yes) else: QMessageBox.information(self, "提示", "添加成功!", QMessageBox.Yes) self.headerText.setText("") self.contentText.setText("") for pic in self.image_pic: self.imageLayout.removeWidget(pic) sip.delete(pic) self.images = [] self.image_pic = [] self.imageFrame.setFixedHeight(20) @staticmethod def isEmpty(text): return text is None or text.strip() == ""