cpptypehierarchy.cpp 9.51 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 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 "cpptypehierarchy.h"
#include "cppeditorconstants.h"
#include "cppeditor.h"
#include "cppelementevaluator.h"
#include "cppplugin.h"

#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/coreconstants.h>
#include <utils/navigationtreeview.h>

#include <QtCore/QLatin1Char>
#include <QtCore/QLatin1String>
#include <QtCore/QModelIndex>
#include <QtCore/QSettings>
#include <QtGui/QVBoxLayout>
#include <QtGui/QStandardItemModel>
#include <QtGui/QFontMetrics>
#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QLabel>

using namespace CppEditor;
using namespace Internal;

// CppTypeHierarchyItem
CppTypeHierarchyItem::CppTypeHierarchyItem(const CppClass &cppClass) :
    QStandardItem(), m_cppClass(cppClass)
{}

CppTypeHierarchyItem::~CppTypeHierarchyItem()
{}

int CppTypeHierarchyItem::type() const
{ return UserType; }

const CppClass &CppTypeHierarchyItem::cppClass() const
{ return m_cppClass; }

// CppTypeHierarchyDelegate
CppTypeHierarchyDelegate::CppTypeHierarchyDelegate(QObject *parent) : QStyledItemDelegate(parent)
{}

CppTypeHierarchyDelegate::~CppTypeHierarchyDelegate()
{}

void CppTypeHierarchyDelegate::paint(QPainter *painter,
                                     const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const
{
    if (const QStyleOptionViewItemV3 *v3 =
            qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option)) {
        QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, v3, painter, v3->widget);

        QStyleOptionViewItemV4 opt = option;
        initStyleOption(&opt, index);

        const QStandardItemModel *model = static_cast<const QStandardItemModel *>(index.model());
        CppTypeHierarchyItem *item =
            static_cast<CppTypeHierarchyItem *>(model->itemFromIndex(index));

        painter->save();
        const QIcon &icon = item->cppClass().icon();
        const QSize &iconSize = icon.actualSize(opt.decorationSize);
        QRect workingRect(opt.rect);
        QRect decorationRect(workingRect.topLeft(), iconSize);
        icon.paint(painter, decorationRect, opt.decorationAlignment);
        workingRect.setX(workingRect.x() + iconSize.width() + 4);

        QRect boundingRect;
        const QString &name = item->cppClass().name() + QLatin1Char(' ');
        painter->drawText(workingRect, Qt::AlignLeft, name, &boundingRect);
        if (item->cppClass().name() != item->cppClass().qualifiedName()) {
            QFont font(painter->font());
            if (font.pointSize() > 2)
                font.setPointSize(font.pointSize() - 2);
            else if (font.pointSize() > 1)
                font.setPointSize(font.pointSize() - 1);
            font.setItalic(true);
            painter->setFont(font);

            QFontMetrics metrics(font);
            workingRect.setX(boundingRect.x() + boundingRect.width());
            workingRect.setY(boundingRect.y() + boundingRect.height() - metrics.height());
            painter->drawText(workingRect, Qt::AlignLeft, item->cppClass().qualifiedName());
        }
        painter->restore();
    } else {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

QSize CppTypeHierarchyDelegate::sizeHint(const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
{
    QSize size = QStyledItemDelegate::sizeHint(option, index);
    size.rwidth() += 5; // Extend a bit because of the font processing.
    return size;
}

// CppTypeHierarchyWidget
CppTypeHierarchyWidget::CppTypeHierarchyWidget(Core::IEditor *editor) :
    QWidget(0),
    m_cppEditor(0),
    m_treeView(0),
    m_model(0),
    m_delegate(0)
{
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);

    if (CPPEditorEditable *cppEditable = qobject_cast<CPPEditorEditable *>(editor)) {
        m_cppEditor = static_cast<CPPEditor *>(cppEditable->widget());

        m_model = new QStandardItemModel;
        m_treeView = new Utils::NavigationTreeView;
        m_delegate = new CppTypeHierarchyDelegate;
        m_treeView->setModel(m_model);
        m_treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        m_treeView->setItemDelegate(m_delegate);
        layout->addWidget(m_treeView);

        connect(m_treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(onItemClicked(QModelIndex)));
        connect(CppPlugin::instance(), SIGNAL(typeHierarchyRequested()), this, SLOT(perform()));
    } else {
        QLabel *label = new QLabel(tr("No type hierarchy available"), this);
        label->setAlignment(Qt::AlignCenter);
        label->setAutoFillBackground(true);
        label->setBackgroundRole(QPalette::Base);
        layout->addWidget(label);
    }
    setLayout(layout);
}

CppTypeHierarchyWidget::~CppTypeHierarchyWidget()
{
    delete m_model;
    delete m_delegate;
}

171
172
173
174
175
176
177
178
179
180
181
182
183
bool CppTypeHierarchyWidget::handleReplacement(Core::IEditor *editor)
{
    if (CPPEditorEditable *cppEditable = qobject_cast<CPPEditorEditable *>(editor)) {
        if (m_cppEditor) {
            m_cppEditor = static_cast<CPPEditor *>(cppEditable->widget());
            return true;
        }
    } else if (!m_cppEditor) {
        return true;
    }
    return false;
}

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
void CppTypeHierarchyWidget::perform()
{
    if (!m_cppEditor)
        return;

    m_model->clear();

    CppElementEvaluator evaluator(m_cppEditor);
    evaluator.setLookupBaseClasses(true);
    QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement();
    if (!cppElement.isNull()) {
        CppElement *element = cppElement.data();
        if (CppClass *cppClass = dynamic_cast<CppClass *>(element))
            buildModel(*cppClass, m_model->invisibleRootItem());
    }
}

void CppTypeHierarchyWidget::buildModel(const CppClass &cppClass, QStandardItem *parent)
{
    CppTypeHierarchyItem *item = new CppTypeHierarchyItem(cppClass);
    parent->appendRow(item);

    // The delegate retrieves data from the item directly. This is to help size hint.
    const QString &display = cppClass.name() + cppClass.qualifiedName();
    m_model->setData(m_model->indexFromItem(item), display, Qt::DisplayRole);
    m_model->setData(m_model->indexFromItem(item), cppClass.icon(), Qt::DecorationRole);

    foreach (const CppClass &cppBase, cppClass.bases())
        buildModel(cppBase, item);

    m_treeView->expand(m_model->indexFromItem(item));
}

void CppTypeHierarchyWidget::onItemClicked(const QModelIndex &index)
{
    if (QStandardItem *item = m_model->itemFromIndex(index)) {
        CppTypeHierarchyItem *cppItem = static_cast<CppTypeHierarchyItem *>(item);
        m_cppEditor->openLink(cppItem->cppClass().link());
    }
}

// CppTypeHierarchyStackedWidget
CppTypeHierarchyStackedWidget::CppTypeHierarchyStackedWidget(QWidget *parent) :
    QStackedWidget(parent),
    m_typeHiearchyWidgetInstance(
        new CppTypeHierarchyWidget(Core::EditorManager::instance()->currentEditor()))
{
    addWidget(m_typeHiearchyWidgetInstance);

    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(editorChanged(Core::IEditor*)));
}

CppTypeHierarchyStackedWidget::~CppTypeHierarchyStackedWidget()
{
    delete m_typeHiearchyWidgetInstance;
}

void CppTypeHierarchyStackedWidget::editorChanged(Core::IEditor *editor)
{
244
245
246
247
248
249
250
    if (!m_typeHiearchyWidgetInstance->handleReplacement(editor)) {
        CppTypeHierarchyWidget *replacement = new CppTypeHierarchyWidget(editor);
        removeWidget(m_typeHiearchyWidgetInstance);
        m_typeHiearchyWidgetInstance->deleteLater();
        m_typeHiearchyWidgetInstance = replacement;
        addWidget(m_typeHiearchyWidgetInstance);
    }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}

// CppTypeHierarchyFactory
CppTypeHierarchyFactory::CppTypeHierarchyFactory()
{}

CppTypeHierarchyFactory::~CppTypeHierarchyFactory()
{}

QString CppTypeHierarchyFactory::displayName() const
{
    return tr("Type Hierarchy");
}

QString CppTypeHierarchyFactory::id() const
{
    return QLatin1String(Constants::TYPE_HIERARCHY_ID);
}

QKeySequence CppTypeHierarchyFactory::activationSequence() const
{
    return QKeySequence();
}

Core::NavigationView CppTypeHierarchyFactory::createWidget()
{
    CppTypeHierarchyStackedWidget *w = new CppTypeHierarchyStackedWidget;
    static_cast<CppTypeHierarchyWidget *>(w->currentWidget())->perform();
    Core::NavigationView navigationView;
    navigationView.widget = w;
    return navigationView;
}