-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtableitemdelegate.cpp
82 lines (71 loc) · 2.52 KB
/
tableitemdelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "library/tableitemdelegate.h"
#include <QPainter>
#include <QTableView>
#include "moc_tableitemdelegate.cpp"
#include "util/painterscope.h"
#include "widget/wtracktableview.h"
TableItemDelegate::TableItemDelegate(QTableView* pTableView)
: QStyledItemDelegate(pTableView),
m_pTableView(pTableView) {
DEBUG_ASSERT(m_pTableView);
auto* pTrackTableView = qobject_cast<WTrackTableView*>(m_pTableView);
if (pTrackTableView) {
m_pFocusBorderColor = pTrackTableView->getFocusBorderColor();
}
}
void TableItemDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
PainterScope painterScope(painter);
painter->setClipRect(option.rect);
// Set the palette appropriately based on whether the row is selected or
// not. We also have to check if it is inactive or not and use the
// appropriate ColorGroup.
QPalette::ColorGroup cg = QPalette::Disabled;
if ((option.state & QStyle::State_Enabled) &&
(option.state & QStyle::State_Active)) {
cg = QPalette::Normal;
}
if (option.state & QStyle::State_Selected) {
painter->setBrush(option.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setBrush(option.palette.color(cg, QPalette::Text));
}
QStyle* style = m_pTableView->style();
if (style) {
style->drawControl(
QStyle::CE_ItemViewItem,
&option,
painter,
m_pTableView);
}
paintItem(painter, option, index);
}
int TableItemDelegate::columnWidth(const QModelIndex &index) const {
return m_pTableView->columnWidth(index.column());
}
void TableItemDelegate::paintItemBackground(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) {
// If the row is not selected, paint the desired background color before
// painting the delegate item
if (option.showDecorationSelected &&
(option.state & QStyle::State_Selected)) {
return;
}
QVariant bgValue = index.data(Qt::BackgroundRole);
if (!bgValue.isValid()) {
return;
}
DEBUG_ASSERT(bgValue.canConvert<QBrush>());
const auto bgBrush = qvariant_cast<QBrush>(bgValue);
painter->fillRect(option.rect, bgBrush);
}
void TableItemDelegate::paintItem(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
QStyledItemDelegate::paint(painter, option, index);
}