Commit f79187ca authored by Leandro Melo's avatar Leandro Melo

Create type hierarchy widget; Refactor code from C++ hover into a simple reusable model.

Hierarchy information removed from tooltips now.
parent 761a9694
......@@ -1755,6 +1755,9 @@ void CPPEditor::unCommentSelection()
CPPEditor::Link CPPEditor::linkToSymbol(CPlusPlus::Symbol *symbol)
{
if (!symbol)
return Link();
const QString fileName = QString::fromUtf8(symbol->fileName(),
symbol->fileNameLength());
unsigned line = symbol->line();
......
......@@ -180,6 +180,10 @@ public:
void setObjCEnabled(bool onoff);
bool isObjCEnabled() const;
bool openLink(const Link &link) { return openCppEditorAt(link); }
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
Q_SIGNALS:
void outlineModelIndexChanged(const QModelIndex &index);
......@@ -267,13 +271,10 @@ private:
void abortRename();
Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
bool openLink(const Link &link) { return openCppEditorAt(link); }
bool openCppEditorAt(const Link &);
QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex()) const;
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
CppTools::CppModelManagerInterface *m_modelManager;
QComboBox *m_outlineCombo;
......
......@@ -20,8 +20,9 @@ HEADERS += cppplugin.h \
cppsemanticinfo.h \
cppoutline.h \
cppdeclfromdef.h \
cpplocalsymbols.h
cpplocalsymbols.h \
cpptypehierarchy.h \
cppelementevaluator.h
SOURCES += cppplugin.cpp \
cppeditor.cpp \
cpphighlighter.cpp \
......@@ -35,8 +36,9 @@ SOURCES += cppplugin.cpp \
cppsemanticinfo.cpp \
cppoutline.cpp \
cppdeclfromdef.cpp \
cpplocalsymbols.cpp
cpplocalsymbols.cpp \
cpptypehierarchy.cpp \
cppelementevaluator.cpp
RESOURCES += cppeditor.qrc
OTHER_FILES += CppEditor.pluginspec CppEditor.mimetypes.xml
OTHER_FILES += CppEditor.pluginspec \
CppEditor.mimetypes.xml
......@@ -48,6 +48,9 @@ const char * const FIND_REFERENCES = "CppEditor.FindReferences";
const char * const JUMP_TO_DEFINITION = "CppEditor.JumpToDefinition";
const char * const UPDATE_CODEMODEL = "CppEditor.UpdateCodeModel";
const char * const TYPE_HIERARCHY_ID = "CppEditor.TypeHierarchy";
const char * const OPEN_TYPE_HIERARCHY = "CppEditor.OpenTypeHierarchy";
const char * const HEADER_FILE_TYPE = "CppHeaderFiles";
const char * const SOURCE_FILE_TYPE = "CppSourceFiles";
......
This diff is collapsed.
/**************************************************************************
**
** 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.
**
**************************************************************************/
#ifndef CPPHIGHLEVELMODEL_H
#define CPPHIGHLEVELMODEL_H
#include "cppeditor.h"
#include "cpphoverhandler.h"
#include <cplusplus/CppDocument.h>
#include <cplusplus/Overview.h>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QSharedPointer>
#include <QtGui/QTextCursor>
#include <QtGui/QIcon>
namespace CPlusPlus {
class LookupItem;
class LookupContext;
}
namespace CppTools {
class CppModelManagerInterface;
}
namespace CppEditor {
namespace Internal {
class CPPEditor;
class CppElement;
class CppElementEvaluator
{
public:
CppElementEvaluator(CPPEditor *editor);
void setTextCursor(const QTextCursor &tc);
void setLookupBaseClasses(const bool lookup);
QSharedPointer<CppElement> identifyCppElement();
private:
void evaluate();
bool matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchMacroInUse(const CPlusPlus::Document::Ptr &document, unsigned pos);
void handleLookupItemMatch(const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::LookupItem &lookupItem,
const CPlusPlus::LookupContext &lookupContext);
CPPEditor *m_editor;
CppTools::CppModelManagerInterface *m_modelManager;
QTextCursor m_tc;
bool m_lookupBaseClasses;
QSharedPointer<CppElement> m_element;
};
class CppElement
{
public:
virtual ~CppElement();
const CppHoverHandler::HelpCandidate::Category &helpCategory() const;
const QStringList &helpIdCandidates() const;
const QString &helpMark() const;
const CPPEditor::Link &link() const;
const QString &tooltip() const;
protected:
CppElement();
void setHelpCategory(const CppHoverHandler::HelpCandidate::Category &category);
void setLink(const CPPEditor::Link &link);
void setTooltip(const QString &tooltip);
void setHelpIdCandidates(const QStringList &candidates);
void addHelpIdCandidate(const QString &candidate);
void setHelpMark(const QString &mark);
private:
CppHoverHandler::HelpCandidate::Category m_helpCategory;
QStringList m_helpIdCandidates;
QString m_helpMark;
CPPEditor::Link m_link;
QString m_tooltip;
};
class Unknown : public CppElement
{
public:
Unknown(const QString &type);
virtual ~Unknown();
const QString &type() const;
private:
QString m_type;
};
class CppDiagnosis : public CppElement
{
public:
CppDiagnosis(const CPlusPlus::Document::DiagnosticMessage &message);
virtual ~CppDiagnosis();
const QString &text() const;
private:
QString m_text;
};
class CppInclude : public CppElement
{
public:
CppInclude(const CPlusPlus::Document::Include &includeFile);
virtual ~CppInclude();
const QString &path() const;
const QString &fileName() const;
private:
QString m_path;
QString m_fileName;
};
class CppMacro : public CppElement
{
public:
CppMacro(const CPlusPlus::Macro &macro);
virtual ~CppMacro();
};
class CppDeclarableElement : public CppElement
{
public:
CppDeclarableElement(CPlusPlus::Symbol *declaration);
virtual ~CppDeclarableElement();
const QString &name() const;
const QString &qualifiedName() const;
const QString &type() const;
const QIcon &icon() const;
protected:
void setName(const QString &name);
void setQualifiedName(const QString &name);
void setType(const QString &type);
void setIcon(const QIcon &icon);
private:
QString m_name;
QString m_qualifiedName;
QString m_type;
QIcon m_icon;
};
class CppNamespace : public CppDeclarableElement
{
public:
CppNamespace(CPlusPlus::Symbol *declaration);
virtual ~CppNamespace();
};
class CppClass : public CppDeclarableElement
{
public:
CppClass(CPlusPlus::Symbol *declaration);
virtual ~CppClass();
void lookupBases(CPlusPlus::Symbol *declaration, const CPlusPlus::LookupContext &context);
const QList<CppClass> &bases() const;
private:
QList<CppClass> m_bases;
};
class CppFunction : public CppDeclarableElement
{
public:
CppFunction(CPlusPlus::Symbol *declaration);
virtual ~CppFunction();
};
class CppEnum : public CppDeclarableElement
{
public:
CppEnum(CPlusPlus::Symbol *declaration);
virtual ~CppEnum();
};
class CppTypedef : public CppDeclarableElement
{
public:
CppTypedef(CPlusPlus::Symbol *declaration);
virtual ~CppTypedef();
};
class CppVariable : public CppDeclarableElement
{
public:
CppVariable(CPlusPlus::Symbol *declaration,
const CPlusPlus::LookupContext &context,
CPlusPlus::Scope *scope);
virtual ~CppVariable();
};
} // namespace Internal
} // namespace CppEditor
#endif // CPPHIGHLEVELMODEL_H
This diff is collapsed.
......@@ -30,26 +30,14 @@
#ifndef CPPHOVERHANDLER_H
#define CPPHOVERHANDLER_H
#include <cplusplus/CppDocument.h>
#include <texteditor/basehoverhandler.h>
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QStringList>
namespace CPlusPlus {
class LookupItem;
class LookupContext;
}
namespace Core {
class IEditor;
}
namespace CppTools {
class CppModelManagerInterface;
}
namespace TextEditor {
class ITextEditor;
}
......@@ -62,26 +50,13 @@ class CppHoverHandler : public TextEditor::BaseHoverHandler
Q_OBJECT
public:
CppHoverHandler(QObject *parent = 0);
virtual ~CppHoverHandler();
private:
virtual bool acceptEditor(Core::IEditor *editor);
virtual void identifyMatch(TextEditor::ITextEditor *editor, int pos);
virtual void resetExtras();
virtual void evaluateHelpCandidates();
virtual void decorateToolTip(TextEditor::ITextEditor *editor);
bool matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchMacroInUse(const CPlusPlus::Document::Ptr &document, unsigned pos);
void handleLookupItemMatch(const CPlusPlus::LookupItem &lookupItem,
const CPlusPlus::LookupContext &lookupContext,
const bool assignTooltip);
void generateDiagramTooltip(const bool extendTooltips);
void generateNormalTooltip(const bool extendTooltips);
CppTools::CppModelManagerInterface *m_modelManager;
QList<QStringList> m_classHierarchy;
};
} // namespace Internal
......
......@@ -36,6 +36,7 @@
#include "cpphoverhandler.h"
#include "cppquickfix.h"
#include "cppoutline.h"
#include "cpptypehierarchy.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
......@@ -46,6 +47,7 @@
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/navigationwidget.h>
#include <texteditor/completionsupport.h>
#include <texteditor/fontsettings.h>
#include <texteditor/storagesettings.h>
......@@ -60,6 +62,7 @@
#include <QtCore/QSettings>
#include <QtCore/QTimer>
#include <QtCore/QCoreApplication>
#include <QtCore/QStringList>
#include <QtGui/QMenu>
......@@ -139,8 +142,8 @@ CppPlugin::CppPlugin() :
m_sortedOutline(false),
m_renameSymbolUnderCursorAction(0),
m_findUsagesAction(0),
m_updateCodeModelAction(0)
m_updateCodeModelAction(0),
m_openTypeHierarchyAction(0)
{
m_instance = this;
......@@ -205,7 +208,7 @@ bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMess
addAutoReleasedObject(new CppEditorFactory(this));
addAutoReleasedObject(new CppHoverHandler);
addAutoReleasedObject(new CppOutlineWidgetFactory);
addAutoReleasedObject(new CppTypeHierarchyFactory);
m_quickFixCollector = new CppQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
......@@ -274,6 +277,13 @@ bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMess
contextMenu->addAction(cmd);
cppToolsMenu->addAction(cmd);
m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
cmd = am->registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+T")));
connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
contextMenu->addAction(cmd);
cppToolsMenu->addAction(cmd);
// Update context in global context
Core::Context globalContext(Core::Constants::C_GLOBAL);
cppToolsMenu->addAction(createSeparator(am, this, globalContext, CppEditor::Constants::SEPARATOR2));
......@@ -391,6 +401,7 @@ void CppPlugin::onTaskStarted(const QString &type)
m_renameSymbolUnderCursorAction->setEnabled(false);
m_findUsagesAction->setEnabled(false);
m_updateCodeModelAction->setEnabled(false);
m_openTypeHierarchyAction->setEnabled(false);
}
}
......@@ -400,6 +411,7 @@ void CppPlugin::onAllTasksFinished(const QString &type)
m_renameSymbolUnderCursorAction->setEnabled(true);
m_findUsagesAction->setEnabled(true);
m_updateCodeModelAction->setEnabled(true);
m_openTypeHierarchyAction->setEnabled(true);
}
}
......@@ -413,4 +425,15 @@ void CppPlugin::currentEditorChanged(Core::IEditor *editor)
}
}
void CppPlugin::openTypeHierarchy()
{
Core::EditorManager *em = Core::EditorManager::instance();
CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
if (editor) {
Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
navigation->activateSubWidget(QLatin1String(Constants::TYPE_HIERARCHY_ID));
emit typeHierarchyRequested();
}
}
Q_EXPORT_PLUGIN(CppPlugin)
......@@ -71,6 +71,7 @@ public:
signals:
void outlineSortingChanged(bool sort);
void typeHierarchyRequested();
public slots:
void setSortedOutline(bool sorted);
......@@ -85,6 +86,7 @@ private slots:
void quickFix(TextEditor::ITextEditable *editable);
void quickFixNow();
void currentEditorChanged(Core::IEditor *editor);
void openTypeHierarchy();
private:
Core::IEditor *createEditor(QWidget *parent);
......@@ -98,6 +100,7 @@ private:
QAction *m_renameSymbolUnderCursorAction;
QAction *m_findUsagesAction;
QAction *m_updateCodeModelAction;
QAction *m_openTypeHierarchyAction;
CppQuickFixCollector *m_quickFixCollector;
......
/**************************************************************************
**
** 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