1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 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 "assetimportupdatetreeitemdelegate.h"
27 #include "assetimportupdatetreemodel.h"
28
29 #include <QPainter>
30 #include <QModelIndex>
31
32 namespace QmlDesigner {
33 namespace Internal {
34
AssetImportUpdateTreeItemDelegate(QObject * parent)35 AssetImportUpdateTreeItemDelegate::AssetImportUpdateTreeItemDelegate(QObject *parent)
36 : QItemDelegate(parent)
37 {
38 }
39
getLayoutInfo(const QStyleOptionViewItem & option,const QModelIndex & index) const40 AssetImportUpdateTreeItemDelegate::LayoutInfo AssetImportUpdateTreeItemDelegate::getLayoutInfo(
41 const QStyleOptionViewItem &option, const QModelIndex &index) const
42 {
43 LayoutInfo info;
44 info.option = setOptions(index, option);
45
46 const bool checkable = (index.model()->flags(index) & Qt::ItemIsUserCheckable);
47 info.checkState = Qt::Unchecked;
48 if (checkable) {
49 QVariant checkStateData = index.data(Qt::CheckStateRole);
50 info.checkState = static_cast<Qt::CheckState>(checkStateData.toInt());
51 info.checkRect = doCheck(info.option, info.option.rect, checkStateData);
52 }
53
54 info.textRect = info.option.rect.adjusted(0, 0, info.checkRect.width(), 0);
55
56 doLayout(info.option, &info.checkRect, &info.iconRect, &info.textRect, false);
57
58 return info;
59 }
60
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const61 void AssetImportUpdateTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
62 const QModelIndex &index) const
63 {
64 painter->save();
65
66 const LayoutInfo info = getLayoutInfo(option, index);
67
68 painter->setFont(info.option.font);
69
70 drawBackground(painter, info.option, index);
71 drawText(painter, info.option, info.textRect, index);
72 drawCheck(painter, info.option, info.checkRect, info.checkState);
73
74 painter->restore();
75 }
76
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const77 QSize AssetImportUpdateTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option,
78 const QModelIndex &index) const
79 {
80 const LayoutInfo info = getLayoutInfo(option, index);
81 const int height = index.data(Qt::SizeHintRole).value<QSize>().height();
82 // get text width, see QItemDelegatePrivate::displayRect
83 const QString text = index.data(Qt::DisplayRole).toString();
84 const QRect textMaxRect(0, 0, INT_MAX / 256, height);
85 const QRect textLayoutRect = textRectangle(nullptr, textMaxRect, info.option.font, text);
86 const QRect textRect(info.textRect.x(), info.textRect.y(), textLayoutRect.width(), height);
87 const QRect layoutRect = info.checkRect | textRect;
88 return QSize(layoutRect.x(), layoutRect.y()) + layoutRect.size();
89 }
90
drawText(QPainter * painter,const QStyleOptionViewItem & option,const QRect & rect,const QModelIndex & index) const91 void AssetImportUpdateTreeItemDelegate::drawText(QPainter *painter,
92 const QStyleOptionViewItem &option,
93 const QRect &rect,
94 const QModelIndex &index) const
95 {
96 const QString text = index.data(Qt::DisplayRole).toString();
97 drawDisplay(painter, option, rect, text);
98 }
99
100 } // namespace Internal
101 } // namespace QmlDesigner
102