예제 #1
0
 def paintVerticalCell(self, painter: QPainter, hv: QHeaderView,
                       cellIndex: QModelIndex, leafIndex: QModelIndex,
                       logicalLeafIndex: int,
                       styleOptions: QStyleOptionHeader,
                       sectionRect: QRect, left: int):
     uniopt = QStyleOptionHeader(styleOptions)
     self.setForegroundBrush(uniopt, cellIndex)
     self.setBackgroundBrush(uniopt, cellIndex)
     width = self.cellSize(cellIndex, hv, uniopt).width()
     if cellIndex == leafIndex:
         width = sectionRect.width() - left
     top = self.currentCellLeft(cellIndex, leafIndex, logicalLeafIndex,
                                sectionRect.top(), hv)
     height = self.currentCellWidth(cellIndex, leafIndex,
                                    logicalLeafIndex, hv)
     r = QRect(left, top, width, height)
     uniopt.text = cellIndex.data(Qt.DisplayRole)
     painter.save()
     uniopt.rect = r
     if cellIndex.data(Qt.UserRole):
         hv.style().drawControl(QStyle.CE_HeaderSection, uniopt,
                                painter, hv)
         m = QMatrix()
         m.rotate(-90)
         painter.setWorldMatrix(m, True)
         new_r = QRect(0, 0, r.height(), r.width())
         new_r.moveCenter(QPoint(-r.center().y(), r.center().x()))
         uniopt.rect = new_r
         hv.style().drawControl(QStyle.CE_HeaderLabel, uniopt, painter,
                                hv)
     else:
         hv.style().drawControl(QStyle.CE_Header, uniopt, painter, hv)
     painter.restore()
     return left + width
예제 #2
0
파일: views.py 프로젝트: newnone/calibre
    def paintSection(self, painter, rect, logical_index):
        opt = QStyleOptionHeader()
        self.initStyleOption(opt)
        opt.rect = rect
        opt.section = logical_index
        opt.orientation = self.orientation()
        opt.textAlignment = Qt.AlignHCenter | Qt.AlignVCenter
        model = self.parent().model()
        opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '')
        if self.isSortIndicatorShown() and self.sortIndicatorSection() == logical_index:
            opt.sortIndicator = QStyleOptionHeader.SortDown if self.sortIndicatorOrder() == Qt.AscendingOrder else QStyleOptionHeader.SortUp
        opt.text = opt.fontMetrics.elidedText(opt.text, Qt.ElideRight, rect.width() - 4)
        if self.isEnabled():
            opt.state |= QStyle.State_Enabled
            if self.window().isActiveWindow():
                opt.state |= QStyle.State_Active
                if self.hover == logical_index:
                    opt.state |= QStyle.State_MouseOver
        sm = self.selectionModel()
        if opt.orientation == Qt.Vertical:
            try:
                opt.icon = model.headerData(logical_index, opt.orientation, Qt.DecorationRole)
                opt.iconAlignment = Qt.AlignVCenter
            except (IndexError, ValueError, TypeError):
                pass
            if sm.isRowSelected(logical_index, QModelIndex()):
                opt.state |= QStyle.State_Sunken

        painter.save()
        if (
                (opt.orientation == Qt.Horizontal and sm.currentIndex().column() == logical_index) or
                (opt.orientation == Qt.Vertical and sm.currentIndex().row() == logical_index)):
            painter.setFont(self.current_font)
        self.style().drawControl(QStyle.CE_Header, opt, painter, self)
        painter.restore()
예제 #3
0
 def paintHorizontalSection(self, painter: QPainter, sectionRect: QRect,
                            logicalLeafIndex: int, hv: QHeaderView,
                            styleOptions: QStyleOptionHeader,
                            leafIndex: QModelIndex):
     #            print(logicalLeafIndex)
     oldBO = painter.brushOrigin()
     top = sectionRect.y()
     indexes = QModelIndexList(self.parentIndexes(leafIndex))
     for i in range(indexes.size()):
         realStyleOptions = QStyleOptionHeader(styleOptions)
         if i < indexes.size() - 1 and (
                 realStyleOptions.state & QStyle.State_Sunken
                 or realStyleOptions.state & QStyle.State_On):
             t = QStyle.State(QStyle.State_Sunken | QStyle.State_On)
             realStyleOptions.state = realStyleOptions.state & ~t  # FIXME: parent items are not highlighted
         if i < indexes.size(
         ) - 1:  # Use sortIndicator for inner level only
             realStyleOptions.sortIndicator = False
         # if i==0:
         #                    print(self.leafs(indexes[i]), leafIndex)
         top = self.paintHorizontalCell(painter, hv, indexes[i],
                                        leafIndex, logicalLeafIndex,
                                        realStyleOptions, sectionRect,
                                        top)
     painter.setBrushOrigin(oldBO)
예제 #4
0
 def paintVerticalSection(self, painter: QPainter, sectionRect: QRect,
                          logicalLeafIndex: int, hv: QHeaderView,
                          styleOptions: QStyleOptionHeader,
                          leafIndex: QModelIndex):
     oldBO = painter.brushOrigin()
     left = sectionRect.x()
     indexes = QModelIndexList(self.parentIndexes(leafIndex))
     for i in range(indexes.size()):
         realStyleOptions = QStyleOptionHeader(styleOptions)
         if i < indexes.size() - 1 and (
                 realStyleOptions.state & QStyle.State_Sunken
                 or realStyleOptions.state & QStyle.State_On):
             t = QStyle.State(QStyle.State_Sunken | QStyle.State_On)
             realStyleOptions.state = realStyleOptions.state & ~t  # FIXME: parent items are not highlighted
         left = self.paintVerticalCell(painter, hv, indexes[i],
                                       leafIndex, logicalLeafIndex,
                                       realStyleOptions, sectionRect,
                                       left)
     painter.setBrushOrigin(oldBO)
예제 #5
0
 def sectionSizeFromContents(self, logical_index):
     self.ensurePolished()
     opt = QStyleOptionHeader()
     self.initStyleOption(opt)
     opt.section = logical_index
     opt.orientation = self.orientation()
     opt.fontMetrics = self.fm
     model = self.parent().model()
     opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '')
     if opt.orientation == Qt.Vertical:
         try:
             val = model.headerData(logical_index, opt.orientation, Qt.DecorationRole)
             if val is not None:
                 opt.icon = val
             opt.iconAlignment = Qt.AlignVCenter
         except (IndexError, ValueError, TypeError):
             pass
     if self.isSortIndicatorShown():
         opt.sortIndicator = QStyleOptionHeader.SortDown
     return self.style().sizeFromContents(QStyle.CT_HeaderSection, opt, QSize(), self)
예제 #6
0
파일: views.py 프로젝트: Qasbi/calibre
 def sectionSizeFromContents(self, logical_index):
     self.ensurePolished()
     opt = QStyleOptionHeader()
     self.initStyleOption(opt)
     opt.section = logical_index
     opt.orientation = self.orientation()
     opt.fontMetrics = self.fm
     model = self.parent().model()
     opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '')
     if opt.orientation == Qt.Vertical:
         try:
             opt.icon = model.headerData(logical_index, opt.orientation, Qt.DecorationRole)
             opt.iconAlignment = Qt.AlignVCenter
         except (IndexError, ValueError, TypeError):
             pass
     if self.isSortIndicatorShown():
         opt.sortIndicator = QStyleOptionHeader.SortDown
     return self.style().sizeFromContents(QStyle.CT_HeaderSection, opt, QSize(), self)
예제 #7
0
 def sectionSizeFromContents(self, logicalIndex: int) -> QSize:
     if self._pd.headerModel:
         curLeafIndex = QModelIndex(self._pd.leafIndex(logicalIndex))
         if curLeafIndex.isValid():
             styleOption = QStyleOptionHeader(
                 self.styleOptionForCell(logicalIndex))
             s = QSize(self._pd.cellSize(curLeafIndex, self, styleOption))
             curLeafIndex = curLeafIndex.parent()
             while curLeafIndex.isValid():
                 if self.orientation() == Qt.Horizontal:
                     s.setHeight(s.height() + self._pd.cellSize(
                         curLeafIndex, self, styleOption).height())
                 else:
                     s.setWidth(s.width() + self._pd.cellSize(
                         curLeafIndex, self, styleOption).width())
                 curLeafIndex = curLeafIndex.parent()
             return s
     return super().sectionSizeFromContents(logicalIndex)
예제 #8
0
    def paintSection(self, painter, rect, logical_index):
        opt = QStyleOptionHeader()
        self.initStyleOption(opt)
        opt.rect = rect
        opt.section = logical_index
        opt.orientation = self.orientation()
        opt.textAlignment = Qt.AlignHCenter | Qt.AlignVCenter
        model = self.parent().model()
        opt.text = unicode(model.headerData(logical_index, opt.orientation, Qt.DisplayRole) or '')
        if self.isSortIndicatorShown() and self.sortIndicatorSection() == logical_index:
            opt.sortIndicator = QStyleOptionHeader.SortDown if self.sortIndicatorOrder() == Qt.AscendingOrder else QStyleOptionHeader.SortUp
        opt.text = opt.fontMetrics.elidedText(opt.text, Qt.ElideRight, rect.width() - 4)
        if self.isEnabled():
            opt.state |= QStyle.State_Enabled
            if self.window().isActiveWindow():
                opt.state |= QStyle.State_Active
                if self.hover == logical_index:
                    opt.state |= QStyle.State_MouseOver
        sm = self.selectionModel()
        if opt.orientation == Qt.Vertical:
            try:
                opt.icon = model.headerData(logical_index, opt.orientation, Qt.DecorationRole)
                opt.iconAlignment = Qt.AlignVCenter
            except (IndexError, ValueError, TypeError):
                pass
            if sm.isRowSelected(logical_index, QModelIndex()):
                opt.state |= QStyle.State_Sunken

        painter.save()
        if (
                (opt.orientation == Qt.Horizontal and sm.currentIndex().column() == logical_index) or
                (opt.orientation == Qt.Vertical and sm.currentIndex().row() == logical_index)):
            painter.setFont(self.current_font)
        self.style().drawControl(QStyle.CE_Header, opt, painter, self)
        painter.restore()
예제 #9
0
 def styleOptionForCell(self, logicalInd: int) -> QStyleOptionHeader:
     opt = QStyleOptionHeader()
     self.initStyleOption(opt)
     if self.isSortIndicatorShown() and self.sortIndicatorSection(
     ) == logicalInd:
         opt.sortIndicator = (
             QStyleOptionHeader.SortUp, QStyleOptionHeader.SortDown
         )[self.sortIndicatorOrder() == Qt.AscendingOrder]
     if self.window().isActiveWindow():
         opt.state = opt.state | QStyle.State_Active
     opt.textAlignment = Qt.AlignCenter
     opt.iconAlignment = Qt.AlignVCenter
     opt.section = logicalInd
     visual = self.visualIndex(logicalInd)
     if self.count() == 1:
         opt.position = QStyleOptionHeader.OnlyOneSection
     else:
         if visual == 0:
             opt.position = QStyleOptionHeader.Beginning
         else:
             opt.position = QStyleOptionHeader.End if visual == self.count(
             ) - 1 else QStyleOptionHeader.Middle
     if self.isClickable():
         #            if logicalIndex == d.hover:
         #            ...
         if self.highlightSections() and self.selectionModel():
             if self.orientation() == Qt.Horizontal:
                 if self.selectionModel().columnIntersectsSelection(
                         logicalInd, self.rootIndex()):
                     opt.state = opt.state | QStyle.State_On
                 if self.selectionModel().isColumnSelected(
                         logicalInd, self.rootIndex()):
                     opt.state = opt.state | QStyle.State_Sunken
             else:
                 if self.selectionModel().rowIntersectsSelection(
                         logicalInd, self.rootIndex()):
                     opt.state = opt.state | QStyle.State_On
                 if self.selectionModel().isRowSelected(
                         logicalInd, self.rootIndex()):
                     opt.state = opt.state | QStyle.State_Sunken
     if self.selectionModel():
         previousSelected = False
         if self.orientation() == Qt.Horizontal:
             previousSelected = self.selectionModel().isColumnSelected(
                 self.logicalIndex(visual - 1), self.rootIndex())
         else:
             previousSelected = self.selectionModel().isRowSelected(
                 self.logicalIndex(visual - 1), self.rootIndex())
         nextSelected = False
         if self.orientation() == Qt.Horizontal:
             nextSelected = self.selectionModel().isColumnSelected(
                 self.logicalIndex(visual + 1), self.rootIndex())
         else:
             nextSelected = self.selectionModel().isRowSelected(
                 self.logicalIndex(visual + 1), self.rootIndex())
         if previousSelected and nextSelected:
             opt.selectedPosition = QStyleOptionHeader.NextAndPreviousAreSelected
         else:
             if previousSelected:
                 opt.selectedPosition = QStyleOptionHeader.PreviousIsSelected
             else:
                 if nextSelected:
                     opt.selectedPosition = QStyleOptionHeader.NextIsSelected
                 else:
                     opt.selectedPosition = QStyleOptionHeader.NotAdjacent
     return opt