class QtBookInfo(QtWidgets.QWidget, Ui_BookInfo):
    def __init__(self, owner):
        super(self.__class__, self).__init__()
        Ui_BookInfo.__init__(self)
        self.setupUi(self)
        self.owner = weakref.ref(owner)
        self.loadingForm = QtLoading(self)
        self.bookId = ""
        self.url = ""
        self.path = ""
        self.bookName = ""
        self.lastEpsId = -1

        self.msgForm = QtBubbleLabel(self)
        self.picture.installEventFilter(self)
        self.title.setGeometry(QRect(328, 240, 329, 27 * 4))
        self.title.setWordWrap(True)
        self.title.setAlignment(Qt.AlignTop)
        self.title.setContextMenuPolicy(Qt.CustomContextMenu)
        self.title.customContextMenuRequested.connect(self.CopyTitle)

        # self.autor.setContextMenuPolicy(Qt.CustomContextMenu)
        # self.autor.customContextMenuRequested.connect(self.OpenAutor)

        layouy = self.horizontalLayout_4
        self.autorList = QtCategoryList(self)
        layouy.addWidget(QLabel("作者:"))
        layouy.addWidget(self.autorList)
        self.autorList.itemClicked.connect(self.ClickTagsItem)

        self.description.setContextMenuPolicy(Qt.CustomContextMenu)
        self.description.customContextMenuRequested.connect(
            self.CopyDescription)

        self.description.setGeometry(QRect(328, 240, 329, 27 * 4))
        self.description.setWordWrap(True)
        self.description.setAlignment(Qt.AlignTop)

        # self.categories.setGeometry(QRect(328, 240, 329, 27 * 4))
        # self.categories.setWordWrap(True)
        # self.categories.setAlignment(Qt.AlignTop)

        layouy = self.horizontalLayout_6
        self.categoriesList = QtCategoryList(self)
        layouy.addWidget(QLabel("分类:"))
        layouy.addWidget(self.categoriesList)
        self.categoriesList.itemClicked.connect(self.ClickCategoriesItem)

        # self.tags.setGeometry(QRect(328, 240, 329, 27 * 4))
        # self.tags.setWordWrap(True)
        # self.tags.setAlignment(Qt.AlignTop)

        layouy = self.horizontalLayout_7
        self.tagsList = QtCategoryList(self)
        layouy.addWidget(QLabel("Tags:"))
        layouy.addWidget(self.tagsList)
        self.tagsList.itemClicked.connect(self.ClickTagsItem)

        self.epsListWidget = QListWidget(self)
        self.epsListWidget.setFlow(self.epsListWidget.LeftToRight)
        self.epsListWidget.setWrapping(True)
        self.epsListWidget.setFrameShape(self.epsListWidget.NoFrame)
        self.epsListWidget.setResizeMode(self.epsListWidget.Adjust)

        self.epsLayout.addWidget(self.epsListWidget)

        self.listWidget = QtBookList(self, self.__class__.__name__)
        self.listWidget.InitUser(self.LoadNextPage)
        self.listWidget.doubleClicked.connect(self.OpenCommentInfo)

        self.childrenListWidget = QtBookList(None, self.__class__.__name__)
        self.childrenListWidget.InitUser(self.LoadChildrenNextPage)

        self.childrenWidget = QtWidgets.QWidget()
        layout = QHBoxLayout(self.childrenWidget)

        label = QLabel()
        label.setMinimumWidth(100)
        layout.addWidget(label)
        layout3 = QVBoxLayout()

        layout2 = QHBoxLayout()
        self.commentLine2 = QLineEdit()
        self.commentButton2 = QPushButton("回复")
        self.commentButton2.clicked.connect(self.SendCommentChildren)
        layout2.addWidget(self.commentLine2)
        layout2.addWidget(self.commentButton2)
        layout3.addLayout(layout2)
        layout3.addWidget(self.childrenListWidget)
        layout.addLayout(layout3)

        self.commentLayout.addWidget(self.listWidget)
        layout = QHBoxLayout()
        self.commentLine = QLineEdit()
        layout.addWidget(self.commentLine)
        self.commentButton = QPushButton("发送评论")
        layout.addWidget(self.commentButton)
        self.commentLayout.addLayout(layout, 1, 0)
        self.commentButton.clicked.connect(self.SendComment)

        # self.stackedWidget.addWidget(self.qtReadImg)
        self.epsListWidget.clicked.connect(self.OpenReadImg)

        self.closeFlag = self.__class__.__name__ + "-close"  # 切换book时,取消加载

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        if self.stackedWidget.currentIndex() == 1:
            self.stackedWidget.setCurrentIndex(0)
            self.owner().qtReadImg.AddHistory()
            self.LoadHistory()
            a0.ignore()
        else:
            a0.accept()

    def CopyTitle(self):
        clipboard = QApplication.clipboard()
        clipboard.setText(self.title.text())
        self.msgForm.ShowMsg("复制标题")
        return

    # def OpenAutor(self):
    #     text = self.autor.text()
    #     self.owner().userForm.listWidget.setCurrentRow(0)
    #     self.owner().searchForm.searchEdit.setText(text)
    #     self.owner().searchForm.Search()
    #     return

    def Clear(self):
        self.stackedWidget.setCurrentIndex(0)
        self.owner().qtTask.CancelTasks(self.closeFlag)
        self.epsListWidget.clear()
        self.ClearCommnetList()

    def CopyDescription(self):
        clipboard = QApplication.clipboard()
        clipboard.setText(self.description.text())
        self.msgForm.ShowMsg("复制描述")
        return

    def OpenBook(self, bookId):
        self.bookId = bookId
        self.setWindowTitle(self.bookId)
        # if self.bookId in self.owner().downloadForm.downloadDict:
        #     self.download.setEnabled(False)
        # else:
        #     self.download.setEnabled(True)

        self.Clear()
        self.show()
        self.loadingForm.show()
        self.owner().qtTask.AddHttpTask(
            lambda x: BookMgr().AddBookById(bookId, x), self.OpenBookBack)

    def close(self):
        super(self.__class__, self).close()

    def OpenBookBack(self, msg):
        self.loadingForm.close()
        self.listWidget.UpdatePage(1, 1)
        self.childrenListWidget.UpdatePage(1, 1)
        self.childrenListWidget.UpdateState()
        self.listWidget.UpdateState()
        self.categoriesList.clear()
        self.tagsList.clear()
        self.autorList.clear()
        info = BookMgr().books.get(self.bookId)
        if msg == Status.Ok and info:
            # self.autor.setText(info.author)
            self.autorList.AddItem(info.author)
            self.title.setText(info.title)
            self.bookName = info.title
            self.description.setText(info.description)
            self.isFinished.setText("完本" if info.finished else "未完本")
            for name in info.categories:
                self.categoriesList.AddItem(name)
            # self.categories.setText(','.join(info.categories))
            # self.tags.setText(','.join(info.tags))
            for name in info.tags:
                self.tagsList.AddItem(name)
            self.likes.setText(str(info.totalLikes))
            self.views.setText(str(info.totalViews))

            if info.isFavourite:
                self.favorites.setEnabled(False)
            else:
                self.favorites.setEnabled(True)
            self.picture.setText("图片加载中...")
            fileServer = info.thumb.get("fileServer")
            path = info.thumb.get("path")
            name = info.thumb.get("originalName")
            self.url = fileServer
            self.path = path
            timeArray, day = ToolUtil.GetDateStr(info.updated_at)
            self.updateTick.setText(str(day) + "天前更新")
            if config.IsLoadingPicture:

                self.owner().qtTask.AddDownloadTask(
                    fileServer,
                    path,
                    completeCallBack=self.UpdatePicture,
                    cleanFlag=self.closeFlag)
            self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
                req.GetComments(self.bookId), bakParam=x),
                                            self.GetCommnetBack,
                                            cleanFlag=self.closeFlag)

            self.owner().qtTask.AddHttpTask(
                lambda x: BookMgr().AddBookEpsInfo(self.bookId, x),
                self.GetEpsBack,
                cleanFlag=self.closeFlag)
            self.startRead.setEnabled(False)
        else:
            # QtWidgets.QMessageBox.information(self, '加载失败', msg, QtWidgets.QMessageBox.Yes)
            self.msgForm.ShowError(msg)
            self.hide()
        return

    def UpdatePicture(self, data, status):
        if status == Status.Ok:
            pic = QtGui.QPixmap()
            pic.loadFromData(data)
            pic.scaled(self.picture.size(), QtCore.Qt.KeepAspectRatio)
            self.picture.setPixmap(pic)
            # self.picture.setScaledContents(True)
            self.update()
        else:
            self.picture.setText("图片加载失败")
        return

    # 加载评论
    def GetCommnetBack(self, data):
        try:
            self.loadingForm.close()
            self.listWidget.UpdateState()
            msg = json.loads(data)
            if msg.get("code") == 200:
                comments = msg.get("data", {}).get("comments", {})
                topComments = msg.get("data", {}).get("topComments", [])
                page = int(comments.get("page", 1))
                pages = int(comments.get("pages", 1))
                limit = int(comments.get("limit", 1))
                self.listWidget.UpdatePage(page, pages)
                total = comments.get("total", 0)
                self.tabWidget.setTabText(1, "评论({})".format(str(total)))
                if page == 1:
                    for index, info in enumerate(topComments):
                        floor = "置顶"
                        content = info.get("content")
                        name = info.get("_user", {}).get("name")
                        avatar = info.get("_user", {}).get("avatar", {})
                        createdTime = info.get("created_at")
                        commentsCount = info.get("commentsCount")
                        commnetId = info.get('_id')
                        likesCount = info.get("likesCount")
                        self.listWidget.AddUserItem(commnetId, commentsCount,
                                                    likesCount, content, name,
                                                    createdTime, floor,
                                                    avatar.get("fileServer"),
                                                    avatar.get("path"),
                                                    avatar.get("originalName"))

                for index, info in enumerate(comments.get("docs")):
                    floor = total - ((page - 1) * limit + index)
                    content = info.get("content")
                    name = info.get("_user", {}).get("name")
                    avatar = info.get("_user", {}).get("avatar", {})
                    createdTime = info.get("created_at")
                    commentsCount = info.get("commentsCount")
                    commnetId = info.get('_id')
                    likesCount = info.get("likesCount")
                    self.listWidget.AddUserItem(commnetId, commentsCount,
                                                likesCount, content, name,
                                                createdTime, floor,
                                                avatar.get("fileServer"),
                                                avatar.get("path"),
                                                avatar.get("originalName"))
            return
        except Exception as es:
            Log.Error(es)

    def GetEpsBack(self, st):
        if st == Status.Ok:
            self.UpdateEpsData()
            self.lastEpsId = -1
            self.LoadHistory()
            return
        return

    def UpdateEpsData(self):
        self.epsListWidget.clear()
        info = BookMgr().books.get(self.bookId)
        if not info:
            return
        self.startRead.setEnabled(True)
        downloadIds = self.owner().downloadForm.GetDownloadCompleteEpsId(
            self.bookId)
        for index, epsInfo in enumerate(info.eps):
            label = QLabel(epsInfo.title)
            label.setContentsMargins(20, 10, 20, 10)
            item = QListWidgetItem(self.epsListWidget)
            if index in downloadIds:
                item.setBackground(QColor(18, 161, 130))
            else:
                item.setBackground(QColor(0, 0, 0, 0))
            item.setSizeHint(label.sizeHint())
            self.epsListWidget.setItemWidget(item, label)
        self.tabWidget.setTabText(0, "章节({})".format(str(len(info.eps))))
        return

    def AddDownload(self):
        self.owner().epsInfoForm.OpenEpsInfo(self.bookId)
        # if self.owner().downloadForm.AddDownload(self.bookId):
        #     QtBubbleLabel.ShowMsgEx(self, "添加下载成功")
        # else:
        #     QtBubbleLabel.ShowMsgEx(self, "已在下载列表")
        # self.download.setEnabled(False)

    def AddFavority(self):
        User().AddAndDelFavorites(self.bookId)
        QtBubbleLabel.ShowMsgEx(self, "添加收藏成功")
        self.favorites.setEnabled(False)

    def LoadNextPage(self):
        self.loadingForm.show()
        self.owner().qtTask.AddHttpTask(lambda x: Server(
        ).Send(req.GetComments(self.bookId, self.listWidget.page + 1),
               bakParam=x),
                                        self.GetCommnetBack,
                                        cleanFlag=self.closeFlag)
        return

    def OpenReadImg(self, modelIndex):
        index = modelIndex.row()
        self.OpenReadIndex(index)

    def OpenReadIndex(self, index):
        item = self.epsListWidget.item(index)
        if not item:
            return
        widget = self.epsListWidget.itemWidget(item)
        if not widget:
            return
        name = widget.text()
        self.hide()
        self.owner().qtReadImg.OpenPage(self.bookId, index, name)
        # self.stackedWidget.setCurrentIndex(1)

    def StartRead(self):
        if self.lastEpsId >= 0:
            self.OpenReadIndex(self.lastEpsId)
        else:
            self.OpenReadIndex(0)
        return

    def LoadHistory(self):
        info = self.owner().historyForm.GetHistory(self.bookId)
        if not info:
            self.startRead.setText("观看第{}章".format(str(1)))
            return
        if self.lastEpsId == info.epsId:
            self.startRead.setText("观看第{}章".format(str(self.lastEpsId + 1)))
            return

        if self.lastEpsId >= 0:
            item = self.epsListWidget.item(self.lastEpsId)
            if item:
                downloadIds = self.owner(
                ).downloadForm.GetDownloadCompleteEpsId(self.bookId)
                if self.lastEpsId in downloadIds:
                    item.setBackground(QColor(18, 161, 130))
                else:
                    item.setBackground(QColor(0, 0, 0, 0))

        item = self.epsListWidget.item(info.epsId)
        if not item:
            return
        item.setBackground(QColor(238, 162, 164))
        self.epsListWidget.update()
        self.lastEpsId = info.epsId
        self.startRead.setText("观看第{}章".format(str(self.lastEpsId + 1)))

    def ClickCategoriesItem(self, item):
        text = item.text()
        self.owner().userForm.listWidget.setCurrentRow(1)
        self.owner().searchForm.searchEdit.setText("")
        self.owner().searchForm.OpenSearchCategories(text)
        return

    def ClickTagsItem(self, item):
        text = item.text()
        self.owner().userForm.listWidget.setCurrentRow(1)
        self.owner().searchForm.searchEdit.setText(text)
        self.owner().searchForm.Search()
        return

    def SendComment(self):
        data = self.commentLine.text()
        if not data:
            return
        self.commentLine.setText("")
        self.loadingForm.show()
        self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
            req.SendComment(self.bookId, data), bakParam=x),
                                        callBack=self.SendCommentBack)

    def SendCommentBack(self, msg):
        try:
            data = json.loads(msg)
            if data.get("code") == 200:
                self.ClearCommnetList()
                self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
                    req.GetComments(self.bookId), bakParam=x),
                                                self.GetCommnetBack,
                                                cleanFlag=self.closeFlag)
            else:
                self.loadingForm.close()
                QtBubbleLabel.ShowErrorEx(self, data.get("message", "错误"))
            self.commentLine.setText("")
        except Exception as es:
            self.loadingForm.close()
            Log.Error(es)

    def OpenCommentInfo(self, modelIndex):
        index = modelIndex.row()
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return

        self.childrenListWidget.clear()
        self.childrenListWidget.UpdatePage(1, 1)
        self.childrenListWidget.UpdateState()
        if self.childrenListWidget.parentId == index:
            # self.childrenWidget.hide()
            self.childrenWidget.setParent(None)
            widget.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item.setSizeHint(widget.sizeHint())
            return
        if self.childrenListWidget.parentId >= 0:
            item2 = self.listWidget.item(self.childrenListWidget.parentId)
            widget2 = self.listWidget.itemWidget(item2)
            self.childrenWidget.setParent(None)
            widget2.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item2.setSizeHint(widget2.sizeHint())

        self.loadingForm.show()
        self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
            req.GetCommentsChildrenReq(widget.id), bakParam=x),
                                        self.LoadCommentInfoBack,
                                        backParam=index,
                                        cleanFlag=self.closeFlag)

    def LoadCommentInfoBack(self, msg, index):
        try:
            self.loadingForm.close()
            item = self.listWidget.item(index)
            if not item:
                return
            widget = self.listWidget.itemWidget(item)
            if not widget:
                return
            self.childrenListWidget.UpdateState()
            data = json.loads(msg)
            self.childrenListWidget.parentId = index
            widget.gridLayout.addWidget(self.childrenWidget, 1, 0, 1, 1)
            if data.get("code") == 200:
                comments = data.get("data", {}).get("comments", {})
                page = int(comments.get("page", 1))
                total = int(comments.get("total", 1))
                pages = int(comments.get("pages", 1))
                limit = int(comments.get("limit", 1))
                self.childrenListWidget.UpdatePage(page, pages)
                for index, info in enumerate(comments.get("docs")):
                    floor = total - ((page - 1) * limit + index)
                    content = info.get("content")
                    name = info.get("_user", {}).get("name")
                    avatar = info.get("_user", {}).get("avatar", {})
                    createdTime = info.get("created_at")
                    commentsCount = info.get("commentsCount")
                    likesCount = info.get("likesCount")
                    commnetId = info.get('_id')
                    self.childrenListWidget.AddUserItem(
                        commnetId, commentsCount, likesCount, content, name,
                        createdTime, floor, avatar.get("fileServer"),
                        avatar.get("path"), avatar.get("originalName"))

                pass
            self.listWidget.scrollToItem(
                item, self.listWidget.ScrollHint.PositionAtTop)
            size = self.listWidget.size()
            item.setSizeHint(size)
        except Exception as es:
            Log.Error(es)

    def SendCommentChildren(self):
        data = self.commentLine2.text()
        if not data:
            return
        index = self.childrenListWidget.parentId
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return
        self.commentLine2.setText("")
        commentId = widget.id
        self.loadingForm.show()
        self.childrenListWidget.clear()
        self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
            req.SendCommentChildrenReq(commentId, data), bakParam=x),
                                        callBack=self.SendCommentChildrenBack,
                                        backParam=index)

    def SendCommentChildrenBack(self, msg, index):
        try:
            item = self.listWidget.item(index)
            if not item:
                self.loadingForm.close()
                return
            widget = self.listWidget.itemWidget(item)
            if not widget:
                self.loadingForm.close()
                return

            data = json.loads(msg)
            if data.get("code") == 200:
                self.owner().qtTask.AddHttpTask(lambda x: Server().Send(
                    req.GetCommentsChildrenReq(widget.id), bakParam=x),
                                                self.LoadCommentInfoBack,
                                                backParam=index,
                                                cleanFlag=self.closeFlag)
            else:
                self.loadingForm.close()
                QtBubbleLabel.ShowErrorEx(self, data.get("message", "错误"))
            self.commentLine.setText("")
        except Exception as es:
            self.loadingForm.close()
            Log.Error(es)

    def LoadChildrenNextPage(self):
        index = self.childrenListWidget.parentId
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return
        self.loadingForm.show()
        self.owner().qtTask.AddHttpTask(
            lambda x: Server().Send(req.GetCommentsChildrenReq(
                widget.id, self.childrenListWidget.page + 1),
                                    bakParam=x),
            self.LoadCommentInfoBack,
            backParam=index,
            cleanFlag=self.closeFlag)
        return

    def ClearCommnetList(self):
        if self.childrenListWidget.parentId >= 0:
            item2 = self.listWidget.item(self.childrenListWidget.parentId)
            widget2 = self.listWidget.itemWidget(item2)
            self.childrenWidget.setParent(None)
            widget2.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item2.setSizeHint(widget2.sizeHint())
        self.childrenListWidget.clear()
        self.listWidget.clear()

    def eventFilter(self, obj, event):
        if event.type() == QEvent.MouseButtonPress:
            if event.button() == Qt.LeftButton:
                if obj.pixmap() and not obj.text():
                    QtImgMgr().ShowImg(obj.pixmap())
                return True
            else:
                return False
        else:
            return super(self.__class__, self).eventFilter(obj, event)
Beispiel #2
0
class QtFavorite(QtWidgets.QWidget, Ui_favorite):
    def __init__(self, owner):
        super(self.__class__, self).__init__(owner)
        Ui_favorite.__init__(self)
        self.setupUi(self)
        self.owner = weakref.ref(owner)

        self.dealCount = 0
        self.dirty = False

        self.bookList = QtBookList(self, self.__class__.__name__)
        self.bookList.InitBook(self.LoadNextPage)
        self.gridLayout_3.addWidget(self.bookList)
        self.bookList.doubleClicked.connect(self.OpenBookInfo)
        self.bookList.setContextMenuPolicy(Qt.CustomContextMenu)
        self.bookList.customContextMenuRequested.connect(self.SelectMenu)

        self.popMenu = QMenu(self.bookList)
        action = self.popMenu.addAction("打开")
        action.triggered.connect(self.OpenBookInfoHandler)
        action = self.popMenu.addAction("复制")
        action.triggered.connect(self.CopyHandler)
        action = self.popMenu.addAction("刪除")
        action.triggered.connect(self.DelHandler)
        action = self.popMenu.addAction("下载")
        action.triggered.connect(self.DownloadHandler)

        self.lineEdit.setValidator(QtIntLimit(1, 1, self))
        self.sortList = ["da", "dd"]

    def SwitchCurrent(self):
        self.RefreshDataFocus()

    def RefreshDataFocus(self):
        User().category.clear()
        self.bookList.UpdatePage(1, 1)
        self.bookList.UpdateState()
        self.bookList.clear()
        self.RefreshData()

    def RefreshData(self):
        if not User().category.get(self.bookList.page):
            self.LoadPage(self.bookList.page)
        else:
            self.UpdatePagesBack(Status.Ok)

    def UpdatePagesBack(self, st):
        self.owner().loadingForm.close()
        self.bookList.UpdateState()
        if st == Status.Ok:
            pageNums = User().pages
            page = User().page
            self.nums.setText("收藏数:{}".format(str(User().total)))
            self.pages.setText("页:{}/{}".format(str(page), str(pageNums)))
            self.bookList.UpdatePage(page, pageNums)
            self.lineEdit.setValidator(QtIntLimit(1, pageNums, self))
            for index, info in enumerate(User().category.get(
                    self.bookList.page)):
                url = info.thumb.get("fileServer")
                path = info.thumb.get("path")
                originalName = info.thumb.get("originalName")
                data = "完本," if info.finished else ""
                data += "{}E/{}P".format(str(info.epsCount),
                                         str(info.pagesCount))
                self.bookList.AddBookItem(info.id, info.title, data, url, path,
                                          originalName)

    def SelectMenu(self, pos):
        index = self.bookList.indexAt(pos)
        if index.isValid():
            self.popMenu.exec_(QCursor.pos())
        pass

    def CopyHandler(self):
        selected = self.bookList.selectedItems()
        if not selected:
            return

        data = ''
        for item in selected:
            widget = self.bookList.itemWidget(item)
            data += widget.GetTitle() + str("\r\n")
        clipboard = QApplication.clipboard()
        data = data.strip("\r\n")
        clipboard.setText(data)
        pass

    def DelHandler(self):
        bookIds = set()
        selected = self.bookList.selectedItems()
        for item in selected:
            widget = self.bookList.itemWidget(item)
            bookIds.add(widget.GetId())
        if not bookIds:
            return
        self.DelAndFavorites(bookIds)

    def DelAndFavorites(self, bookIds):
        self.owner().loadingForm.show()

        self.dealCount = len(bookIds)
        for bookId in bookIds:
            self.owner().qtTask.AddHttpTask(
                lambda x: User().AddAndDelFavorites(bookId, x),
                self.DelAndFavoritesBack)
        pass

    def DelAndFavoritesBack(self, msg):
        self.dealCount -= 1
        if self.dealCount <= 0:
            self.owner().loadingForm.close()
            self.RefreshDataFocus()

    def DownloadHandler(self):
        selected = self.bookList.selectedItems()
        for item in selected:
            widget = self.bookList.itemWidget(item)
            self.owner().epsInfoForm.OpenEpsInfo(widget.GetId())
        pass

    def OpenBookInfoHandler(self):
        selected = self.bookList.selectedItems()
        for item in selected:
            widget = self.bookList.itemWidget(item)
            self.owner().bookInfoForm.OpenBook(widget.GetId())
            return

    def OpenBookInfo(self, modelIndex):
        index = modelIndex.row()
        item = self.bookList.item(index)
        if not item:
            return
        widget = self.bookList.itemWidget(item)
        if not widget:
            return
        bookId = widget.id
        if not bookId:
            return
        self.owner().bookInfoForm.OpenBook(bookId)

    def LoadNextPage(self):
        self.LoadPage(self.bookList.page + 1)

    def LoadPage(self, page):
        sortKey = self.sortList[self.comboBox.currentIndex()]
        self.owner().qtTask.AddHttpTask(
            lambda x: User().UpdateFavorites(page, sortKey, x),
            self.UpdatePagesBack)
        self.owner().loadingForm.show()

    def JumpPage(self):
        page = int(self.lineEdit.text())
        if page > self.bookList.pages:
            return
        self.bookList.page = page
        self.bookList.clear()
        self.LoadPage(page)
Beispiel #3
0
class QtLeaveMsg(QtWidgets.QWidget, Ui_LeaveMsg):
    def __init__(self, owner):
        super(self.__class__, self).__init__(owner)
        Ui_LeaveMsg.__init__(self)
        self.setupUi(self)
        self.owner = weakref.ref(owner)
        self.closeFlag = self.__class__.__name__
        self.listWidget = QtBookList(None, self.__class__.__name__)
        # self.gridLayout_3.addWidget(self.listWidget, 0, 0, 1, 1)

        self.listWidget.InitUser(self.LoadNextPage)
        self.listWidget.doubleClicked.connect(self.OpenCommentInfo)

        self.childrenListWidget = QtBookList(None, self.__class__.__name__)
        self.childrenListWidget.InitUser(self.LoadChildrenNextPage)

        self.childrenWidget = QtWidgets.QWidget()
        layout = QHBoxLayout(self.childrenWidget)

        label = QLabel()
        label.setMinimumWidth(100)
        layout.addWidget(label)
        layout3 = QVBoxLayout()

        layout2 = QHBoxLayout()
        self.commentLine2 = QLineEdit()
        self.commentButton2 = QPushButton("回复")
        self.commentButton2.clicked.connect(self.SendCommentChildren)
        layout2.addWidget(self.commentLine2)
        layout2.addWidget(self.commentButton2)
        layout3.addLayout(layout2)
        layout3.addWidget(self.childrenListWidget)
        layout.addLayout(layout3)

        self.gridLayout_3.addWidget(self.listWidget)
        layout = QHBoxLayout()
        self.commentLine = QLineEdit()
        layout.addWidget(self.commentLine)
        self.commentButton = QPushButton("发送评论")
        layout.addWidget(self.commentButton)
        self.gridLayout_3.addLayout(layout, 1, 0)
        self.commentButton.clicked.connect(self.SendComment)
        self.bookId = "5822a6e3ad7ede654696e482"

    @property
    def loadingForm(self):
        return self.owner().loadingForm

    def SwitchCurrent(self):
        self.loadingForm.show()
        self.listWidget.UpdatePage(1, 1)
        self.nums.setText("分页:{}/{}".format(str(1), str(1)))
        self.childrenListWidget.UpdatePage(1, 1)
        self.childrenListWidget.UpdateState()
        self.listWidget.UpdateState()
        QtTask().AddHttpTask(
            lambda x: Server().Send(req.GetComments(self.bookId, self.listWidget.page), bakParam=x),
            self.GetCommnetBack, cleanFlag=self.closeFlag)
        return

    def JumpPage(self):
        try:
            page = int(self.lineEdit.text())
            if page > self.listWidget.pages:
                return
            self.listWidget.page = page
            self.listWidget.clear()
            self.loadingForm.show()
            QtTask().AddHttpTask(
                lambda x: Server().Send(req.GetComments(self.bookId, self.listWidget.page), bakParam=x),
                self.GetCommnetBack, cleanFlag=self.closeFlag)
        except Exception as es:
            Log.Error(es)

    def OpenCommentInfo(self, modelIndex):
        index = modelIndex.row()
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return

        self.childrenListWidget.clear()
        self.childrenListWidget.UpdatePage(1, 1)
        self.childrenListWidget.UpdateState()
        if self.childrenListWidget.parentId == index:
            # self.childrenWidget.hide()
            self.childrenWidget.setParent(None)
            widget.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item.setSizeHint(widget.sizeHint())
            return
        if self.childrenListWidget.parentId >= 0:
            item2 = self.listWidget.item(self.childrenListWidget.parentId)
            widget2 = self.listWidget.itemWidget(item2)
            self.childrenWidget.setParent(None)
            widget2.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item2.setSizeHint(widget2.sizeHint())

        self.loadingForm.show()
        QtTask().AddHttpTask(lambda x: Server().Send(req.GetCommentsChildrenReq(widget.id), bakParam=x),
                                        self.LoadCommentInfoBack, backParam=index, cleanFlag=self.closeFlag)

    def LoadNextPage(self):
        self.loadingForm.show()
        QtTask().AddHttpTask(
            lambda x: Server().Send(req.GetComments(self.bookId, self.listWidget.page + 1), bakParam=x),
            self.GetCommnetBack, cleanFlag=self.closeFlag)
        return

    def LoadChildrenNextPage(self):
        index = self.childrenListWidget.parentId
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return
        self.loadingForm.show()
        QtTask().AddHttpTask(lambda x: Server().Send(req.GetCommentsChildrenReq(widget.id, self.childrenListWidget.page + 1), bakParam=x),
                                        self.LoadCommentInfoBack, backParam=index, cleanFlag=self.closeFlag)
        return

    def LoadCommentInfoBack(self, msg, index):
        try:
            self.loadingForm.close()
            item = self.listWidget.item(index)
            if not item:
                return
            widget = self.listWidget.itemWidget(item)
            if not widget:
                return
            self.childrenListWidget.UpdateState()
            data = json.loads(msg)
            self.childrenListWidget.parentId = index
            widget.gridLayout.addWidget(self.childrenWidget, 1, 0, 1, 1)
            if data.get("code") == 200:
                comments = data.get("data", {}).get("comments", {})
                page = int(comments.get("page", 1))
                total = int(comments.get("total", 1))
                pages = int(comments.get("pages", 1))
                limit = int(comments.get("limit", 1))
                self.childrenListWidget.UpdatePage(page, pages)
                for index, info in enumerate(comments.get("docs")):
                    floor = total - ((page - 1) * limit + index)
                    content = info.get("content")
                    name = info.get("_user", {}).get("name")
                    avatar = info.get("_user", {}).get("avatar", {})
                    createdTime = info.get("created_at")
                    commentsCount = info.get("commentsCount")
                    likesCount = info.get("likesCount")
                    commnetId = info.get('_id')
                    self.childrenListWidget.AddUserItem(commnetId, commentsCount, likesCount, content, name, createdTime, floor,
                                                avatar.get("fileServer"),
                                                avatar.get("path"), avatar.get("originalName"))

                pass
            self.listWidget.scrollToItem(item, self.listWidget.ScrollHint.PositionAtTop)
            size = self.listWidget.size()
            item.setSizeHint(size)
        except Exception as es:
            Log.Error(es)

    # 加载评论
    def GetCommnetBack(self, data):
        try:
            self.loadingForm.close()
            self.listWidget.UpdateState()
            msg = json.loads(data)
            if msg.get("code") == 200:
                comments = msg.get("data", {}).get("comments", {})
                topComments = msg.get("data", {}).get("topComments", [])
                page = int(comments.get("page", 1))
                pages = int(comments.get("pages", 1))
                limit = int(comments.get("limit", 1))
                self.listWidget.UpdatePage(page, pages)
                self.nums.setText("分页:{}/{}".format(str(page), str(pages)))
                total = comments.get("total", 0)
                # self.tabWidget.setTabText(1, "评论({})".format(str(total)))
                if page == 1:
                    for index, info in enumerate(topComments):
                        floor = "置顶"
                        content = info.get("content")
                        name = info.get("_user", {}).get("name")
                        avatar = info.get("_user", {}).get("avatar", {})
                        createdTime = info.get("created_at")
                        commentsCount = info.get("commentsCount")
                        commnetId = info.get('_id')
                        likesCount = info.get("likesCount")
                        self.listWidget.AddUserItem(commnetId, commentsCount, likesCount, content, name, createdTime, floor, avatar.get("fileServer"),
                                                    avatar.get("path"), avatar.get("originalName"))

                for index, info in enumerate(comments.get("docs")):
                    floor = total - ((page - 1) * limit + index)
                    content = info.get("content")
                    name = info.get("_user", {}).get("name")
                    avatar = info.get("_user", {}).get("avatar", {})
                    createdTime = info.get("created_at")
                    commentsCount = info.get("commentsCount")
                    commnetId = info.get('_id')
                    likesCount = info.get("likesCount")
                    self.listWidget.AddUserItem(commnetId, commentsCount, likesCount, content, name, createdTime, floor, avatar.get("fileServer"),
                                 avatar.get("path"), avatar.get("originalName"))
            return
        except Exception as es:
            Log.Error(es)

    def SendComment(self):
        data = self.commentLine.text()
        if not data:
            return
        self.commentLine.setText("")
        self.loadingForm.show()
        QtTask().AddHttpTask(lambda x: Server().Send(req.SendComment(self.bookId, data), bakParam=x), callBack=self.SendCommentBack)

    def SendCommentBack(self, msg):
        try:
            data = json.loads(msg)
            if data.get("code") == 200:
                self.ClearCommnetList()
                QtTask().AddHttpTask(lambda x: Server().Send(req.GetComments(self.bookId), bakParam=x),
                                                self.GetCommnetBack, cleanFlag=self.closeFlag)
            else:
                self.loadingForm.close()
                QtBubbleLabel.ShowErrorEx(self, data.get("message", "错误"))
            self.commentLine.setText("")
        except Exception as es:
            self.loadingForm.close()
            Log.Error(es)

    def SendCommentChildren(self):
        data = self.commentLine2.text()
        if not data:
            return
        index = self.childrenListWidget.parentId
        item = self.listWidget.item(index)
        if not item:
            return
        widget = self.listWidget.itemWidget(item)
        if not widget:
            return
        self.commentLine2.setText("")
        commentId = widget.id
        self.loadingForm.show()
        self.childrenListWidget.clear()
        QtTask().AddHttpTask(lambda x: Server().Send(req.SendCommentChildrenReq(commentId, data), bakParam=x), callBack=self.SendCommentChildrenBack, backParam=index)

    def SendCommentChildrenBack(self, msg, index):
        try:
            item = self.listWidget.item(index)
            if not item:
                self.loadingForm.close()
                return
            widget = self.listWidget.itemWidget(item)
            if not widget:
                self.loadingForm.close()
                return

            data = json.loads(msg)
            if data.get("code") == 200:
                QtTask().AddHttpTask(
                    lambda x: Server().Send(req.GetCommentsChildrenReq(widget.id), bakParam=x),
                    self.LoadCommentInfoBack, backParam=index, cleanFlag=self.closeFlag)
            else:
                self.loadingForm.close()
                QtBubbleLabel.ShowErrorEx(self, data.get("message", "错误"))
            self.commentLine.setText("")
        except Exception as es:
            self.loadingForm.close()
            Log.Error(es)

    def ClearCommnetList(self):
        if self.childrenListWidget.parentId >= 0:
            item2 = self.listWidget.item(self.childrenListWidget.parentId)
            widget2 = self.listWidget.itemWidget(item2)
            self.childrenWidget.setParent(None)
            widget2.gridLayout.removeWidget(self.childrenWidget)
            self.childrenListWidget.parentId = -1
            item2.setSizeHint(widget2.sizeHint())
        self.childrenListWidget.clear()
        self.listWidget.clear()
Beispiel #4
0
class QtSearch(QtWidgets.QWidget, Ui_search):
    def __init__(self, owner):
        super(self.__class__, self).__init__(owner)
        Ui_search.__init__(self)
        self.setupUi(self)
        self.owner = weakref.ref(owner)
        self.index = 1
        self.data = ""
        self.bookList = QtBookList(self, self.__class__.__name__, owner)
        self.bookList.InitBook(self.LoadNextPage)

        self.bookLayout.addWidget(self.bookList)
        self.bookList.doubleClicked.connect(self.OpenSearch)
        self.categories = ""
        self.jumpLine.setValidator(QtIntLimit(1, 1, self))

        self.categoryList = QtCategoryList(self)
        layouy = QtWidgets.QHBoxLayout()
        layouy.addWidget(QLabel("屏蔽:"))
        layouy.addWidget(self.categoryList)
        self.bookLayout.addLayout(layouy, 1, 0)
        for name in ["耽美", "偽娘", "禁書", "扶她", "重口", "生肉", "純愛", "WEBTOON"]:
            self.categoryList.AddItem(name)
        self.categoryList.itemClicked.connect(self.ClickCategoryListItem)

        self.keywordList = QtCategoryList(self)
        layouy = QtWidgets.QHBoxLayout()
        layouy.addWidget(QLabel("大家都在搜:"))
        layouy.addWidget(self.keywordList)
        self.bookLayout.addLayout(layouy, 2, 0)
        self.keywordList.itemClicked.connect(self.ClickKeywordListItem)

    def SwitchCurrent(self):
        pass

    def InitCheckBox(self):
        # TODO 分类标签有点问题 暂时不显示
        return
        size = len(CateGoryMgr().idToCateGoryBase)
        hBoxLayout = QtWidgets.QHBoxLayout(self)
        a = QCheckBox("全部分类", self.groupBox)
        hBoxLayout.addWidget(a)

        for index, info in enumerate(CateGoryMgr().idToCateGoryBase, 2):
            if index % 9 == 0:
                self.comboBoxLayout.addLayout(hBoxLayout)
                hBoxLayout = QtWidgets.QHBoxLayout(self)
            a = QCheckBox(info.title, self.groupBox)
            hBoxLayout.addWidget(a)
        self.comboBoxLayout.addLayout(hBoxLayout)
        return

    def Search(self, categories=None):
        data = self.searchEdit.text()
        self.data = data
        if len(data) == len("5822a6e3ad7ede654696e482"):
            self.owner().bookInfoForm.OpenBook(data)
            return
        if not data:
            return
        if not categories:
            self.categories = []
        else:
            pass
        self.categories = ""
        self.bookList.clear()
        self.bookList.UpdatePage(1, 1)
        self.bookList.UpdateState()
        self.searchEdit.setPlaceholderText("")
        self.SendSearch(self.data, 1)

    def SendSearch(self, data, page):
        self.owner().loadingForm.show()
        self.index = 1
        sort = ["dd", "da", "ld", "vd"]
        sortId = sort[self.comboBox.currentIndex()]
        self.owner().qtTask.AddHttpTask(lambda x: Server().Send(req.AdvancedSearchReq(page, [], data, sortId), bakParam=x), self.SendSearchBack)

    def OpenSearchCategories(self, categories):
        self.bookList.clear()
        self.categories = categories
        self.searchEdit.setPlaceholderText(categories)
        self.bookList.UpdatePage(1, 1)
        self.bookList.UpdateState()
        self.SendSearchCategories(1)

    def SendSearchCategories(self, page):
        self.owner().loadingForm.show()
        # TODO 搜索和分类检索不太一样,切页时会有点问题
        sort = ["dd", "da", "ld", "vd"]
        sortId = sort[self.comboBox.currentIndex()]
        self.owner().qtTask.AddHttpTask(
            lambda x: Server().Send(req.CategoriesSearchReq(page, self.categories, sortId), bakParam=x),
            self.SendSearchBack)

    def InitKeyWord(self):
        self.owner().qtTask.AddHttpTask(
            lambda x: Server().Send(req.GetKeywords(), bakParam=x), self.SendKeywordBack)

    def SendSearchBack(self, raw):
        self.owner().loadingForm.close()
        try:
            self.bookList.UpdateState()
            data = json.loads(raw)
            if data.get("code") == 200:
                info = data.get("data").get("comics")
                page = int(info.get("page"))
                pages = int(info.get("pages"))
                self.bookList.UpdatePage(page, pages)
                self.jumpLine.setValidator(QtIntLimit(1, pages, self))
                pageText = "页:" + str(self.bookList.page) + "/" + str(self.bookList.pages)
                self.label.setText(pageText)
                for v in info.get("docs", []):
                    title = v.get("title", "")
                    _id = v.get("_id")
                    url = v.get("thumb", {}).get("fileServer")
                    path = v.get("thumb", {}).get("path")
                    originalName = v.get("thumb", {}).get("originalName")
                    info2 = "完本," if v.get("finished") else ""
                    info2 += "{}E/{}P".format(str(v.get("epsCount")), str(v.get("pagesCount")))
                    param = ",".join(v.get("categories"))
                    self.bookList.AddBookItem(_id, title, info2, url, path, param)
                self.CheckCategoryShowItem()
            else:
                # QtWidgets.QMessageBox.information(self, '未搜索到结果', "未搜索到结果", QtWidgets.QMessageBox.Yes)
                self.owner().msgForm.ShowError("未搜索到结果")
        except Exception as es:
            Log.Error(es)
        pass

    def SendKeywordBack(self, raw):
        try:
            data = json.loads(raw)
            if data.get("code") == 200:
                self.keywordList.clear()
                for keyword in data.get('data', {}).get("keywords", []):
                    self.keywordList.AddItem(keyword)
                pass
            else:
                pass
        except Exception as es:
            Log.Error(es)
        pass

    def OpenSearch(self, modelIndex):
        index = modelIndex.row()
        item = self.bookList.item(index)
        if not item:
            return
        widget = self.bookList.itemWidget(item)
        if not widget:
            return
        bookId = widget.id
        if not bookId:
            return
        self.owner().bookInfoForm.OpenBook(bookId)

    def JumpPage(self):
        page = int(self.jumpLine.text())
        if page > self.bookList.pages:
            return
        self.bookList.page = page
        self.bookList.clear()
        if not self.categories:
            self.SendSearch(self.data, page)
        else:
            self.SendSearchCategories(page)
        return

    def LoadNextPage(self):
        if not self.categories:
            self.SendSearch(self.data, self.bookList.page + 1)
        else:
            self.SendSearchCategories(self.bookList.page + 1)
        return

    def ChangeSort(self, pos):
        self.bookList.page = 1
        self.bookList.clear()
        if not self.categories:
            self.SendSearch(self.data, 1)
        else:
            self.SendSearchCategories(1)

    def ClickCategoryListItem(self, item):
        isClick = self.categoryList.ClickItem(item)
        data = item.text()
        if isClick:
            self.owner().msgForm.ShowMsg("屏蔽" + data)
        else:
            self.owner().msgForm.ShowMsg("取消屏蔽" + data)
        self.CheckCategoryShowItem()

    def CheckCategoryShowItem(self):
        data = self.categoryList.GetAllSelectItem()
        for i in range(self.bookList.count()):
            item = self.bookList.item(i)
            widget = self.bookList.itemWidget(item)
            isHidden = False
            for name in data:
                if name in widget.param:
                    item.setHidden(True)
                    isHidden = True
                    break
            if not isHidden:
                item.setHidden(False)

    def ClickKeywordListItem(self, item):
        data = item.text()
        self.searchEdit.setText(data)
        self.Search()
Beispiel #5
0
class QtFavorite(QtWidgets.QWidget, Ui_favorite, QtBookListMenu):
    def __init__(self, owner):
        super(self.__class__, self).__init__(owner)
        Ui_favorite.__init__(self)

        self.setupUi(self)
        self.owner = weakref.ref(owner)

        self.dealCount = 0
        self.dirty = False

        self.bookList = QtBookList(self, self.__class__.__name__)
        QtBookListMenu.__init__(self)
        self.bookList.InitBook(self.LoadNextPage)
        self.gridLayout_3.addWidget(self.bookList)

        self.lineEdit.setValidator(QtIntLimit(1, 1, self))
        self.sortList = ["dd", "da"]

    def SwitchCurrent(self):
        self.RefreshDataFocus()

    def RefreshDataFocus(self):
        User().category.clear()
        self.bookList.UpdatePage(1, 1)
        self.bookList.UpdateState()
        self.bookList.clear()
        self.RefreshData()

    def RefreshData(self):
        if not User().category.get(self.bookList.page):
            self.LoadPage(self.bookList.page)
        else:
            self.UpdatePagesBack(Status.Ok)

    def UpdatePagesBack(self, st):
        self.owner().loadingForm.close()
        self.bookList.UpdateState()
        if st == Status.Ok:
            pageNums = User().pages
            page = User().page
            self.nums.setText("收藏数:{}".format(str(User().total)))
            self.pages.setText("页:{}/{}".format(str(page), str(pageNums)))
            self.bookList.UpdatePage(page, pageNums)
            self.lineEdit.setValidator(QtIntLimit(1, pageNums, self))
            for index, info in enumerate(User().category.get(
                    self.bookList.page)):
                url = info.thumb.get("fileServer")
                path = info.thumb.get("path")
                originalName = info.thumb.get("originalName")
                data = "完本," if info.finished else ""
                data += "{}E/{}P".format(str(info.epsCount),
                                         str(info.pagesCount))
                self.bookList.AddBookItem(info.id, info.title, data, url, path,
                                          originalName)

    def DelCallBack(self, bookIds):
        self.owner().loadingForm.show()

        self.dealCount = len(bookIds)
        for bookId in bookIds:
            self.owner().qtTask.AddHttpTask(
                lambda x: User().AddAndDelFavorites(bookId, x),
                self.DelAndFavoritesBack)
        pass

    def DelAndFavoritesBack(self, msg):
        self.dealCount -= 1
        if self.dealCount <= 0:
            self.owner().loadingForm.close()
            self.RefreshDataFocus()

    def LoadNextPage(self):
        self.LoadPage(self.bookList.page + 1)

    def LoadPage(self, page):
        sortKey = self.sortList[self.comboBox.currentIndex()]
        self.owner().qtTask.AddHttpTask(
            lambda x: User().UpdateFavorites(page, sortKey, x),
            self.UpdatePagesBack)
        self.owner().loadingForm.show()

    def JumpPage(self):
        page = int(self.lineEdit.text())
        if page > self.bookList.pages:
            return
        self.bookList.page = page
        self.bookList.clear()
        self.LoadPage(page)