1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "testtreeitemdelegate.h"
27
28 #include "testtreeitem.h"
29
30 #include <QPainter>
31
32 namespace Autotest {
33 namespace Internal {
34
TestTreeItemDelegate(QObject * parent)35 TestTreeItemDelegate::TestTreeItemDelegate(QObject *parent)
36 : QStyledItemDelegate(parent)
37 {
38 }
39
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const40 void TestTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
41 {
42 QStyleOptionViewItem opt = option;
43 initStyleOption(&opt, index);
44
45 bool italic = index.data(ItalicRole).toBool();
46 if (italic) {
47 QFont font(option.font);
48 font.setItalic(true);
49 opt.font = font;
50
51 // correct margin of items without a checkbox (except for root items)
52 QStyleOptionButton styleOpt;
53 styleOpt.initFrom(opt.widget);
54 const QSize sz; // no text, no icon - we just need the size of the check box
55 QSize cbSize = opt.widget->style()->sizeFromContents(QStyle::CT_CheckBox, &styleOpt, sz);
56 // the 6 results from hard coded margins of the checkbox itself (2x2) and the item (1x2)
57 opt.rect.setLeft(opt.rect.left() + cbSize.width() + 6);
58
59 // HACK make sure the pixels that have been moved right are painted for selections
60 if (opt.state & QStyle::State_Selected) {
61 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
62 ? QPalette::Normal : QPalette::Disabled;
63 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
64 cg = QPalette::Inactive;
65 painter->fillRect(option.rect, opt.palette.brush(cg, QPalette::Highlight));
66 }
67 }
68
69 // paint disabled items in gray
70 if (!index.data(EnabledRole).toBool())
71 opt.palette.setColor(QPalette::Text, QColor(0xa0, 0xa0, 0xa0));
72
73 QStyledItemDelegate::paint(painter, opt, index);
74 }
75
76 } // namespace Internal
77 } // namespace Autotest
78