summaryrefslogtreecommitdiff
path: root/src/plugins/qmljseditor/qmljsoutlinetreeview.cpp
blob: 1e47392f70da1364e1164e79de674f2215cb4f50 (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
80
81
82
83
84
85
86
87
88
#include "qmljsoutlinetreeview.h"
#include "qmloutlinemodel.h"

#include <QtCore/QDebug>
#include <QtGui/QApplication>
#include <QtGui/QPainter>

namespace QmlJSEditor {
namespace Internal {

QmlJSOutlineItemDelegate::QmlJSOutlineItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QSize QmlJSOutlineItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);

    const QString annotation = index.data(QmlOutlineModel::AnnotationRole).toString();
    if (!annotation.isEmpty())
        opt.text += "     " + annotation;

    QStyle *style = QApplication::style();
    return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), 0);
}

void QmlJSOutlineItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
           const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);

    if (opt.state & QStyle::State_Selected)
        painter->fillRect(opt.rect, option.palette.highlight());

    const QString typeString = index.data(Qt::DisplayRole).toString();
    const QString annotationString = index.data(QmlOutlineModel::AnnotationRole).toString();

    QStyle *style = QApplication::style();

    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);

    if (!annotationString.isEmpty()) {
        QPalette::ColorRole textColorRole = QPalette::Text;
        if (option.state & QStyle::State_Selected) {
            textColorRole = QPalette::HighlightedText;
        }

        // calculate sizes of icon, type.
        QPixmap iconPixmap = opt.icon.pixmap(opt.rect.size());
        QRect iconRect = style->itemPixmapRect(opt.rect, Qt::AlignLeft, iconPixmap);
        QRect typeRect = style->itemTextRect(opt.fontMetrics, opt.rect, Qt::AlignLeft, true, typeString);
        QRect annotationRect = style->itemTextRect(opt.fontMetrics, opt.rect, Qt::AlignLeft | Qt::AlignBottom, true,
                                                   annotationString);

        static int space = opt.fontMetrics.width("     ");
        annotationRect.adjust(iconRect.width() + typeRect.width() + space, 0,
                              iconRect.width() + typeRect.width() + space, 0);

        QPalette disabledPalette(opt.palette);
        disabledPalette.setCurrentColorGroup(QPalette::Disabled);
        style->drawItemText(painter, annotationRect, Qt::AlignLeft, disabledPalette, true,
                            annotationString, textColorRole);
    }
}

QmlJSOutlineTreeView::QmlJSOutlineTreeView(QWidget *parent) :
    Utils::NavigationTreeView(parent)
{
    // see also CppOutlineTreeView
    setFocusPolicy(Qt::NoFocus);
    setExpandsOnDoubleClick(false);

    setDragEnabled(true);
    viewport()->setAcceptDrops(true);
    setDropIndicatorShown(true);
    setDragDropMode(InternalMove);

    setRootIsDecorated(false);

    QmlJSOutlineItemDelegate *itemDelegate = new QmlJSOutlineItemDelegate(this);
    setItemDelegateForColumn(0, itemDelegate);
}

} // namespace Internal
} // namespace QmlJSEditor