| 1 | #include "BranchesViewDelegate.h" |
| 2 | |
| 3 | #include <GitQlientBranchItemRole.h> |
| 4 | #include <GitQlientStyles.h> |
| 5 | |
| 6 | #include <QPainter> |
| 7 | |
| 8 | using namespace GitQlient; |
| 9 | |
| 10 | BranchesViewDelegate::BranchesViewDelegate(bool isTag, QObject *parent) |
| 11 | : QStyledItemDelegate(parent) |
| 12 | , mIsTag(isTag) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void BranchesViewDelegate::paint(QPainter *p, const QStyleOptionViewItem &o, const QModelIndex &i) const |
| 17 | { |
| 18 | p->setRenderHints(QPainter::Antialiasing); |
| 19 | |
| 20 | QStyleOptionViewItem newOpt(o); |
| 21 | |
| 22 | if (newOpt.state & QStyle::State_Selected) |
| 23 | { |
| 24 | p->fillRect(newOpt.rect, GitQlientStyles::getGraphSelectionColor()); |
| 25 | |
| 26 | if (i.column() == 0) |
| 27 | { |
| 28 | QRect rect(0, newOpt.rect.y(), newOpt.rect.x(), newOpt.rect.height()); |
| 29 | p->fillRect(rect, GitQlientStyles::getGraphSelectionColor()); |
| 30 | } |
| 31 | } |
| 32 | else if (newOpt.state & QStyle::State_MouseOver) |
| 33 | { |
| 34 | p->fillRect(newOpt.rect, GitQlientStyles::getGraphHoverColor()); |
| 35 | |
| 36 | if (i.column() == 0) |
| 37 | { |
| 38 | QRect rect(0, newOpt.rect.y(), newOpt.rect.x(), newOpt.rect.height()); |
| 39 | p->fillRect(rect, GitQlientStyles::getGraphHoverColor()); |
| 40 | } |
| 41 | } |
| 42 | else |
| 43 | p->fillRect(newOpt.rect, GitQlientStyles::getBackgroundColor()); |
| 44 | |
| 45 | static const auto iconSize = 20; |
| 46 | static const auto offset = 5; |
| 47 | |
| 48 | if (i.column() == 0) |
| 49 | { |
| 50 | if (i.data(IsLeaf).toBool()) |
| 51 | { |
| 52 | const auto width = newOpt.rect.x(); |
| 53 | QRect rectIcon(width - offset, newOpt.rect.y(), iconSize, newOpt.rect.height()); |
| 54 | QIcon icon(QString::fromUtf8(mIsTag ? ":/icons/tag_indicator" : ":/icons/repo_indicator" )); |
| 55 | icon.paint(p, rectIcon); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | const auto width = newOpt.rect.x(); |
| 60 | QRect rectIcon(width - offset, newOpt.rect.y(), iconSize, newOpt.rect.height()); |
| 61 | QIcon icon(QString::fromUtf8(":/icons/folder_indicator" )); |
| 62 | icon.paint(p, rectIcon); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | p->setPen(GitQlientStyles::getTextColor()); |
| 67 | |
| 68 | QFontMetrics fm(newOpt.font); |
| 69 | |
| 70 | newOpt.font.setBold(i.data(Qt::UserRole).toBool()); |
| 71 | |
| 72 | if (i.data().toString() == "detached" ) |
| 73 | newOpt.font.setItalic(true); |
| 74 | |
| 75 | p->setFont(newOpt.font); |
| 76 | |
| 77 | const auto elidedText = fm.elidedText(i.data().toString(), Qt::ElideRight, newOpt.rect.width()); |
| 78 | |
| 79 | if (i.column() == 0) |
| 80 | newOpt.rect.setX(newOpt.rect.x() + iconSize + offset); |
| 81 | else |
| 82 | newOpt.rect.setX(newOpt.rect.x() + iconSize - offset); |
| 83 | |
| 84 | p->drawText(newOpt.rect, elidedText, QTextOption(Qt::AlignLeft | Qt::AlignVCenter)); |
| 85 | } |
| 86 | |
| 87 | QSize BranchesViewDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const |
| 88 | { |
| 89 | return QSize(0, 25); |
| 90 | } |
| 91 | |