navigatortreeview.cpp 4.57 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "navigatortreeview.h"

#include "navigatorview.h"
#include "navigatortreemodel.h"
#include "navigatorwidget.h"

#include <nodeproperty.h>
37
#include "metainfo.h"
38

39
#define _separator_line_color_ "#757575"
40

41
42
43
44
45
namespace QmlDesigner {

QSize IconCheckboxItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                                         const QModelIndex &index) const
{
46
47
    Q_UNUSED(option);
    Q_UNUSED(index);
48
    return QSize(15,21);
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
}

void IconCheckboxItemDelegate::paint(QPainter *painter,
                                     const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());
    bool isChecked= (m_TreeModel->itemFromIndex(index)->checkState() == Qt::Checked);

    if (m_TreeModel->isNodeInvisible( index ))
        painter->setOpacity(0.5);

    if (isChecked)
        painter->drawPixmap(option.rect.x()+2,option.rect.y()+1,onPix);
    else
        painter->drawPixmap(option.rect.x()+2,option.rect.y()+1,offPix);

    painter->restore();
}


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

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

    ModelNode node = m_TreeModel->nodeForIndex(index);

81
    QIcon icon=node.metaInfo().icon();
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    if (icon.isNull())
    {
        // if node has no own icon, search for it in the itemlibrary
        QList <ItemLibraryInfo> InfoList = node.metaInfo().metaInfo().itemLibraryRepresentations(node.metaInfo());
        foreach (const ItemLibraryInfo &entry, InfoList)
        {
            if (entry.typeName()==node.metaInfo().typeName()) {
                icon = entry.icon();
                break;
            }
        }

        // if the library was also empty, use the default icon
        if (icon.isNull()) icon = QIcon(":/ItemLibrary/images/default-icon.png");
    }

98
99
    QPixmap pixmap = icon.pixmap(option.rect.width(),option.rect.height()-4);
    painter->drawPixmap(option.rect.x()+5,option.rect.y()+2,pixmap);
100
101
102
103
104

    QString myString = node.id();
    if (myString.isEmpty())
        myString = node.simplifiedTypeName();

105
106
107
108
109
110
111
112
113
114
115
116
117
118
    // Check text length does not exceed available space
    int extraSpace=12+pixmap.width();
    QFontMetrics metric(painter->fontMetrics());
    if (painter->fontMetrics().boundingRect(myString).width() > option.rect.width()-extraSpace)
    {
        QString origString(myString);
        int cutpoint=origString.length()/2;
        while (painter->fontMetrics().boundingRect(myString).width() > option.rect.width()-extraSpace)
        {
            cutpoint--;
            myString = origString.left(cutpoint)+QLatin1String("...")+origString.right(cutpoint);
        }
    }

119
120
    if (m_TreeModel->isNodeInvisible( index ))
        painter->setOpacity(0.5);
121
    painter->drawText(option.rect.bottomLeft()+QPoint(8+pixmap.width(),-4),myString);
122
123
124
125
126
127
128
129
130

    painter->restore();
}

void NavigatorTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
    painter->save();

    QTreeView::drawRow(painter,options,index);
131
    painter->setPen(QColor(_separator_line_color_));
132
133
134
135
136
137
    painter->drawLine(options.rect.bottomLeft(),options.rect.bottomRight());

    painter->restore();
}

}