aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/itemlibrary/assetimportupdatetreeitemdelegate.cpp
blob: 4de5a2e984f154e33401bf798374928a5cf6ee9a (plain)
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "assetimportupdatetreeitemdelegate.h"
#include "assetimportupdatetreemodel.h"

#include <QPainter>
#include <QModelIndex>

namespace QmlDesigner {
namespace Internal {

AssetImportUpdateTreeItemDelegate::AssetImportUpdateTreeItemDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}

AssetImportUpdateTreeItemDelegate::LayoutInfo AssetImportUpdateTreeItemDelegate::getLayoutInfo(
        const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    LayoutInfo info;
    info.option = setOptions(index, option);

    const bool checkable = (index.model()->flags(index) & Qt::ItemIsUserCheckable);
    info.checkState = Qt::Unchecked;
    if (checkable) {
        QVariant checkStateData = index.data(Qt::CheckStateRole);
        info.checkState = static_cast<Qt::CheckState>(checkStateData.toInt());
        info.checkRect = doCheck(info.option, info.option.rect, checkStateData);
    }

    info.textRect = info.option.rect.adjusted(0, 0, info.checkRect.width(), 0);

    doLayout(info.option, &info.checkRect, &info.iconRect, &info.textRect, false);

    return info;
}

void AssetImportUpdateTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                              const QModelIndex &index) const
{
    painter->save();

    const LayoutInfo info = getLayoutInfo(option, index);

    painter->setFont(info.option.font);

    drawBackground(painter, info.option, index);
    drawText(painter, info.option, info.textRect, index);
    drawCheck(painter, info.option, info.checkRect, info.checkState);

    painter->restore();
}

QSize AssetImportUpdateTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                                                  const QModelIndex &index) const
{
    const LayoutInfo info = getLayoutInfo(option, index);
    const int height = index.data(Qt::SizeHintRole).value<QSize>().height();
    // get text width, see QItemDelegatePrivate::displayRect
    const QString text = index.data(Qt::DisplayRole).toString();
    const QRect textMaxRect(0, 0, INT_MAX / 256, height);
    const QRect textLayoutRect = textRectangle(nullptr, textMaxRect, info.option.font, text);
    const QRect textRect(info.textRect.x(), info.textRect.y(), textLayoutRect.width(), height);
    const QRect layoutRect = info.checkRect | textRect;
    return QSize(layoutRect.x(), layoutRect.y()) + layoutRect.size();
}

void AssetImportUpdateTreeItemDelegate::drawText(QPainter *painter,
                                                 const QStyleOptionViewItem &option,
                                                 const QRect &rect,
                                                 const QModelIndex &index) const
{
    const QString text = index.data(Qt::DisplayRole).toString();
    drawDisplay(painter, option, rect, text);
}

} // namespace Internal
} // namespace QmlDesigner