コード例 #1
0
void RepoItemDelegate::paintRepoCategoryItem(QPainter *painter,
                                             const QStyleOptionViewItem& option,
                                             const RepoCategoryItem *item) const
{
    QBrush backBrush;
    bool hover = false;
    bool selected = false;

    if (option.state & (QStyle::State_HasFocus | QStyle::State_Selected)) {
        backBrush = QColor(kRepoCategoryBackgroundColorHighlighted);
        selected = true;

    } else {
        backBrush = QColor(kRepoCategoryBackgroundColor);
    }

    painter->save();
    painter->fillRect(option.rect, backBrush);
    painter->restore();

    // Paint the expand/collapse indicator
    RepoTreeModel *model = (RepoTreeModel *)item->model();
    RepoTreeView *view = model->treeView();
    bool expanded = view->isExpanded(model->indexFromItem(item));

    QRect indicator_rect(option.rect.topLeft(),
                         option.rect.bottomLeft() + QPoint(option.rect.height(), 0));
    painter->save();
    painter->setPen(QColor(selected ? kRepoCategoryColorHighlighted : kRepoCategoryColor));
    painter->setFont(awesome->font(16));
    painter->drawText(indicator_rect,
                      Qt::AlignCenter,
                      QChar(expanded ? icon_caret_down : icon_caret_right),
                      &indicator_rect);
    painter->restore();

    // Paint category name
    painter->save();
    QPoint category_name_pos = indicator_rect.topRight() + QPoint(kMarginBetweenIndicatorAndName, 0);
    QRect category_name_rect(category_name_pos,
                             option.rect.bottomRight() - QPoint(kPadding, 0));
    painter->setPen(QColor(selected ? kRepoCategoryColorHighlighted : kRepoCategoryColor));
    painter->drawText(category_name_rect,
                      Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
                      fitTextToWidth(item->name() + QString().sprintf(" [%d]", item->rowCount()),
                                     option.font, category_name_rect.width()));
    painter->restore();
}
コード例 #2
0
void RepoItemDelegate::paintRepoCategoryItem(QPainter *painter,
                                             const QStyleOptionViewItem& option,
                                             const RepoCategoryItem *item) const
{
    QBrush backBrush;
    //bool hover = false;
    bool selected = false;

    backBrush = QColor(kRepoCategoryBackgroundColor);
    if (option.state & (QStyle::State_HasFocus | QStyle::State_Selected)) {
        selected = true;
    }

    painter->save();
    painter->fillRect(option.rect, backBrush);
    painter->restore();

    // Paint the expand/collapse indicator
    RepoTreeModel *model = (RepoTreeModel *)item->model();
    RepoTreeView *view = model->treeView();
    bool expanded = view->isExpanded(model->indexFromItem(item));

    QRect indicator_rect(option.rect.topLeft() + QPoint(kMarginLeft, 0),
                         QSize(kRepoCategoryIndicatorWidth, kRepoCategoryIndicatorHeight));
    painter->save();
    QString icon_path = QString(":/images/caret-%1.png").arg(expanded ? "down" : "right");
    QString icon_2x_path = QString(":/images/caret-%[email protected]").arg(expanded ? "down" : "right");
    QIcon icon = QIcon();
    icon.addFile(icon_path, QSize(kRepoCategoryIndicatorWidth, kRepoCategoryIndicatorHeight));
    icon.addFile(icon_2x_path, QSize(kRepoCategoryIndicatorWidth * 2, kRepoCategoryIndicatorHeight * 2));

    painter->drawPixmap(indicator_rect,
                       icon.pixmap(QSize(kRepoCategoryIndicatorWidth, kRepoCategoryIndicatorHeight)));
    painter->restore();

    // Paint category name
    painter->save();
    QPoint category_name_pos = indicator_rect.topRight() + QPoint(kMarginBetweenIndicatorAndName, 0);
    QRect category_name_rect(category_name_pos,
                             option.rect.bottomRight() - QPoint(kPadding, 0));
    // painter->setPen(QColor(selected ? kRepoCategoryColorHighlighted : kRepoCategoryColor));
    painter->setPen(QColor(kRepoCategoryColor));
    painter->drawText(category_name_rect,
                      Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
                      fitTextToWidth(item->name() + QString().sprintf(" [%d]", item->rowCount()),
                                     option.font, category_name_rect.width()));
    painter->restore();
}