1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "sheet_delegate_p.h"
5
6#include <QtCore/qabstractitemmodel.h>
7#include <QtWidgets/qtreeview.h>
8#include <QtWidgets/qstyle.h>
9#include <QtGui/qpainter.h>
10
11QT_BEGIN_NAMESPACE
12
13namespace qdesigner_internal {
14
15SheetDelegate::SheetDelegate(QTreeView *view, QWidget *parent)
16 : QStyledItemDelegate(parent),
17 m_view(view)
18{
19}
20
21void SheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
22{
23 const QAbstractItemModel *model = index.model();
24 Q_ASSERT(model);
25
26 if (!model->parent(child: index).isValid()) {
27 // this is a top-level item.
28 QStyleOptionButton buttonOption;
29
30 buttonOption.state = option.state;
31#ifdef Q_OS_MACOS
32 buttonOption.state |= QStyle::State_Raised;
33#endif
34 buttonOption.state &= ~QStyle::State_HasFocus;
35
36 buttonOption.rect = option.rect;
37 buttonOption.palette = option.palette;
38 buttonOption.features = QStyleOptionButton::None;
39
40 painter->save();
41 QColor buttonColor(230, 230, 230);
42 QBrush buttonBrush = option.palette.button();
43 if (!buttonBrush.gradient() && buttonBrush.texture().isNull())
44 buttonColor = buttonBrush.color();
45 QColor outlineColor = buttonColor.darker(f: 150);
46 QColor highlightColor = buttonColor.lighter(f: 130);
47
48 // Only draw topline if the previous item is expanded
49 QModelIndex previousIndex = model->index(row: index.row() - 1, column: index.column());
50 bool drawTopline = (index.row() > 0 && m_view->isExpanded(index: previousIndex));
51 int highlightOffset = drawTopline ? 1 : 0;
52
53 QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomLeft());
54 gradient.setColorAt(pos: 0, color: buttonColor.lighter(f: 102));
55 gradient.setColorAt(pos: 1, color: buttonColor.darker(f: 106));
56
57 painter->setPen(Qt::NoPen);
58 painter->setBrush(gradient);
59 painter->drawRect(r: option.rect);
60 painter->setPen(highlightColor);
61 painter->drawLine(p1: option.rect.topLeft() + QPoint(0, highlightOffset),
62 p2: option.rect.topRight() + QPoint(0, highlightOffset));
63 painter->setPen(outlineColor);
64 if (drawTopline)
65 painter->drawLine(p1: option.rect.topLeft(), p2: option.rect.topRight());
66 painter->drawLine(p1: option.rect.bottomLeft(), p2: option.rect.bottomRight());
67 painter->restore();
68
69 QStyleOption branchOption;
70 static const int i = 9; // ### hardcoded in qcommonstyle.cpp
71 QRect r = option.rect;
72 branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
73 branchOption.palette = option.palette;
74 branchOption.state = QStyle::State_Children;
75
76 if (m_view->isExpanded(index))
77 branchOption.state |= QStyle::State_Open;
78
79 m_view->style()->drawPrimitive(pe: QStyle::PE_IndicatorBranch, opt: &branchOption, p: painter, w: m_view);
80
81 // draw text
82 QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
83 QString text = option.fontMetrics.elidedText(text: model->data(index, role: Qt::DisplayRole).toString(),
84 mode: Qt::ElideMiddle,
85 width: textrect.width());
86 m_view->style()->drawItemText(painter, rect: textrect, flags: Qt::AlignCenter,
87 pal: option.palette, enabled: m_view->isEnabled(), text);
88
89 } else {
90 QStyledItemDelegate::paint(painter, option, index);
91 }
92}
93
94QSize SheetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
95{
96 QSize sz = QStyledItemDelegate::sizeHint(option: opt, index) + QSize(2, 2);
97 return sz;
98}
99
100} // namespace qdesigner_internal
101
102QT_END_NAMESPACE
103

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qttools/src/designer/src/lib/shared/sheet_delegate.cpp