1 | #include "FileListDelegate.h" |
2 | |
3 | #include <GitQlientStyles.h> |
4 | |
5 | #include <QPainter> |
6 | |
7 | constexpr auto Offset = 10; |
8 | constexpr auto DefaultHeight = 30.0; |
9 | constexpr auto HeightIncrement = 15.0; |
10 | |
11 | FileListDelegate::FileListDelegate(QObject *parent) |
12 | : QItemDelegate(parent) |
13 | { |
14 | } |
15 | |
16 | void FileListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
17 | { |
18 | painter->save(); |
19 | |
20 | if (option.state & QStyle::State_Selected) |
21 | painter->fillRect(option.rect, GitQlientStyles::getGraphSelectionColor()); |
22 | else if (option.state & QStyle::State_MouseOver) |
23 | painter->fillRect(option.rect, GitQlientStyles::getGraphHoverColor()); |
24 | |
25 | painter->setPen(qvariant_cast<QColor>(index.data(Qt::ForegroundRole))); |
26 | |
27 | auto newOpt = option; |
28 | newOpt.rect.setX(newOpt.rect.x() + Offset); |
29 | |
30 | painter->drawText(newOpt.rect, index.data().toString(), QTextOption(Qt::AlignLeft | Qt::AlignVCenter)); |
31 | |
32 | painter->restore(); |
33 | } |
34 | |
35 | QSize FileListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
36 | { |
37 | auto rect = option.rect; |
38 | rect.setWidth(rect.width() - Offset); |
39 | |
40 | const auto text = index.model()->data(index, Qt::DisplayRole).toString(); |
41 | QFontMetrics fm(option.font); |
42 | QRect neededsize = fm.boundingRect(rect, Qt::TextWordWrap, text); |
43 | |
44 | if (neededsize.height() < DefaultHeight) |
45 | neededsize.setHeight(DefaultHeight); |
46 | else |
47 | neededsize.setHeight((neededsize.height() / DefaultHeight) * HeightIncrement + DefaultHeight); |
48 | |
49 | return QSize(option.rect.width(), neededsize.height()); |
50 | } |
51 | |