1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "sheet_delegate_p.h"
30
31#include <QtCore/qabstractitemmodel.h>
32#include <QtWidgets/qtreeview.h>
33#include <QtWidgets/qstyle.h>
34#include <QtGui/qpainter.h>
35
36QT_BEGIN_NAMESPACE
37
38namespace qdesigner_internal {
39
40SheetDelegate::SheetDelegate(QTreeView *view, QWidget *parent)
41 : QItemDelegate(parent),
42 m_view(view)
43{
44}
45
46void SheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
47{
48 const QAbstractItemModel *model = index.model();
49 Q_ASSERT(model);
50
51 if (!model->parent(index).isValid()) {
52 // this is a top-level item.
53 QStyleOptionButton buttonOption;
54
55 buttonOption.state = option.state;
56#ifdef Q_OS_MACOS
57 buttonOption.state |= QStyle::State_Raised;
58#endif
59 buttonOption.state &= ~QStyle::State_HasFocus;
60
61 buttonOption.rect = option.rect;
62 buttonOption.palette = option.palette;
63 buttonOption.features = QStyleOptionButton::None;
64
65 painter->save();
66 QColor buttonColor(230, 230, 230);
67 QBrush buttonBrush = option.palette.button();
68 if (!buttonBrush.gradient() && buttonBrush.texture().isNull())
69 buttonColor = buttonBrush.color();
70 QColor outlineColor = buttonColor.darker(150);
71 QColor highlightColor = buttonColor.lighter(130);
72
73 // Only draw topline if the previous item is expanded
74 QModelIndex previousIndex = model->index(index.row() - 1, index.column());
75 bool drawTopline = (index.row() > 0 && m_view->isExpanded(previousIndex));
76 int highlightOffset = drawTopline ? 1 : 0;
77
78 QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomLeft());
79 gradient.setColorAt(0, buttonColor.lighter(102));
80 gradient.setColorAt(1, buttonColor.darker(106));
81
82 painter->setPen(Qt::NoPen);
83 painter->setBrush(gradient);
84 painter->drawRect(option.rect);
85 painter->setPen(highlightColor);
86 painter->drawLine(option.rect.topLeft() + QPoint(0, highlightOffset),
87 option.rect.topRight() + QPoint(0, highlightOffset));
88 painter->setPen(outlineColor);
89 if (drawTopline)
90 painter->drawLine(option.rect.topLeft(), option.rect.topRight());
91 painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
92 painter->restore();
93
94 QStyleOption branchOption;
95 static const int i = 9; // ### hardcoded in qcommonstyle.cpp
96 QRect r = option.rect;
97 branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
98 branchOption.palette = option.palette;
99 branchOption.state = QStyle::State_Children;
100
101 if (m_view->isExpanded(index))
102 branchOption.state |= QStyle::State_Open;
103
104 m_view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, m_view);
105
106 // draw text
107 QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
108 QString text = option.fontMetrics.elidedText(model->data(index, Qt::DisplayRole).toString(),
109 Qt::ElideMiddle,
110 textrect.width());
111 m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
112 option.palette, m_view->isEnabled(), text);
113
114 } else {
115 QItemDelegate::paint(painter, option, index);
116 }
117}
118
119QSize SheetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
120{
121 QSize sz = QItemDelegate::sizeHint(opt, index) + QSize(2, 2);
122 return sz;
123}
124
125} // namespace qdesigner_internal
126
127QT_END_NAMESPACE
128