Skip to content
Snippets Groups Projects
cppplugin.cpp 9.42 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** 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://www.qtsoftware.com/contact.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

con's avatar
con committed
#include "cppplugin.h"
#include "cppclasswizard.h"
con's avatar
con committed
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include "cppeditorenums.h"
#include "cppfilewizard.h"
#include "cpphoverhandler.h"
con's avatar
con committed

con's avatar
con committed
#include <coreplugin/coreconstants.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/completionsupport.h>
#include <texteditor/fontsettings.h>
#include <texteditor/storagesettings.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorplugin.h>
con's avatar
con committed
#include <texteditor/texteditorsettings.h>
#include <texteditor/texteditorconstants.h>
con's avatar
con committed
#include <cpptools/cpptoolsconstants.h>

#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtGui/QMenu>
#include <QtGui/QAction>

using namespace CppEditor::Internal;

//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed

CppEditorFactory::CppEditorFactory(CppPlugin *owner) :
con's avatar
con committed
    m_kind(QLatin1String(CppEditor::Constants::CPPEDITOR_KIND)),
    m_owner(owner)
{
    m_mimeTypes << QLatin1String(CppEditor::Constants::C_SOURCE_MIMETYPE)
            << QLatin1String(CppEditor::Constants::C_HEADER_MIMETYPE)
            << QLatin1String(CppEditor::Constants::CPP_SOURCE_MIMETYPE)
            << QLatin1String(CppEditor::Constants::CPP_HEADER_MIMETYPE);

    Core::FileIconProvider *iconProvider = Core::FileIconProvider::instance();
    Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_cpp.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::CPP_SOURCE_MIMETYPE)));
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_c.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::C_SOURCE_MIMETYPE)));
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_h.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::CPP_HEADER_MIMETYPE)));
con's avatar
con committed
}

QString CppEditorFactory::kind() const
con's avatar
con committed
{
    return m_kind;
}

Core::IFile *CppEditorFactory::open(const QString &fileName)
con's avatar
con committed
{
    Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, kind());
con's avatar
con committed
    return iface ? iface->file() : 0;
}

Core::IEditor *CppEditorFactory::createEditor(QWidget *parent)
con's avatar
con committed
{
    CPPEditor *editor = new CPPEditor(parent);
    editor->setRevisionsVisible(true);
    editor->setMimeType(CppEditor::Constants::CPP_SOURCE_MIMETYPE);
    m_owner->initializeEditor(editor);
    return editor->editableInterface();
}

QStringList CppEditorFactory::mimeTypes() const
con's avatar
con committed
{
    return m_mimeTypes;
}

///////////////////////////////// CppPlugin //////////////////////////////////

CppPlugin *CppPlugin::m_instance = 0;

CppPlugin::CppPlugin() :
    m_actionHandler(0),
con's avatar
con committed
{
    m_instance = this;
}

CppPlugin::~CppPlugin()
{
    delete m_actionHandler;
    m_instance = 0;
}

CppPlugin *CppPlugin::instance()
{
    return m_instance;
}

void CppPlugin::initializeEditor(CPPEditor *editor)
{
    m_actionHandler->setupActions(editor);

    TextEditor::TextEditorSettings::instance()->initializeEditor(editor);
con's avatar
con committed

    // auto completion
    connect(editor, SIGNAL(requestAutoCompletion(ITextEditable*, bool)),
            TextEditor::Internal::CompletionSupport::instance(), SLOT(autoComplete(ITextEditable*, bool)));
Roberto Raggi's avatar
Roberto Raggi committed

    // quick fix
    connect(editor, SIGNAL(requestQuickFix(ITextEditable*)),
            TextEditor::Internal::CompletionSupport::instance(), SLOT(quickFix(ITextEditable*)));

    // method combo box sorting
    connect(this, SIGNAL(methodOverviewSortingChanged(bool)),
            editor, SLOT(setSortedMethodOverview(bool)));
}

void CppPlugin::setSortedMethodOverview(bool sorted)
{
    m_sortedMethodOverview = sorted;
    emit methodOverviewSortingChanged(sorted);
}

bool CppPlugin::sortedMethodOverview() const
{
    return m_sortedMethodOverview;
con's avatar
con committed
}

bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
{
    Core::ICore *core = Core::ICore::instance();
    if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/cppeditor/CppEditor.mimetypes.xml"), errorMessage))
con's avatar
con committed
        return false;

    addAutoReleasedObject(new CppEditorFactory(this));
    addAutoReleasedObject(new CppHoverHandler);

con's avatar
con committed
    CppFileWizard::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);

    wizardParameters.setCategory(QLatin1String("C++"));
    wizardParameters.setTrCategory(tr("C++"));
    wizardParameters.setDescription(tr("Creates a C++ header file."));
con's avatar
con committed
    wizardParameters.setName(tr("C++ Header File"));
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Header, core));
con's avatar
con committed

    wizardParameters.setDescription(tr("Creates a C++ source file."));
con's avatar
con committed
    wizardParameters.setName(tr("C++ Source File"));
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Source, core));
con's avatar
con committed

    wizardParameters.setKind(Core::IWizard::ClassWizard);
    wizardParameters.setName(tr("C++ Class"));
    wizardParameters.setDescription(tr("Creates a header and a source file for a new class."));
    addAutoReleasedObject(new CppClassWizard(wizardParameters, core));
con's avatar
con committed

    QList<int> context;
    context << core->uniqueIDManager()->uniqueIdentifier(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed

    Core::ActionManager *am = core->actionManager();
con's avatar
con committed
    am->createMenu(CppEditor::Constants::M_CONTEXT);

con's avatar
con committed
    Core::Command *cmd;
con's avatar
con committed

    QAction *jumpToDefinition = new QAction(tr("Follow Symbol under Cursor"), this);
    cmd = am->registerAction(jumpToDefinition,
        Constants::JUMP_TO_DEFINITION, context);
    cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
    connect(jumpToDefinition, SIGNAL(triggered()),
            this, SLOT(jumpToDefinition()));
    am->actionContainer(CppEditor::Constants::M_CONTEXT)->addAction(cmd);
    am->actionContainer(CppTools::Constants::M_TOOLS_CPP)->addAction(cmd);

    QAction *switchDeclarationDefinition = new QAction(tr("Switch between Method Declaration/Definition"), this);
    cmd = am->registerAction(switchDeclarationDefinition,
        Constants::SWITCH_DECLARATION_DEFINITION, context);
    cmd->setDefaultKeySequence(QKeySequence("Shift+F2"));
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
    am->actionContainer(CppEditor::Constants::M_CONTEXT)->addAction(cmd);
    am->actionContainer(CppTools::Constants::M_TOOLS_CPP)->addAction(cmd);

    m_actionHandler = new TextEditor::TextEditorActionHandler(CppEditor::Constants::C_CPPEDITOR,
con's avatar
con committed
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
        | TextEditor::TextEditorActionHandler::UnCollapseAll);

    m_actionHandler->initializeActions();

    cmd = am->command(TextEditor::Constants::AUTO_INDENT_SELECTION);
    am->actionContainer(CppEditor::Constants::M_CONTEXT)->addAction(cmd);

    cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
    am->actionContainer(CppEditor::Constants::M_CONTEXT)->addAction(cmd);


con's avatar
con committed
    return true;
}

void CppPlugin::readSettings()
{
    m_sortedMethodOverview = Core::ICore::instance()->settings()->value("CppTools/SortedMethodOverview", false).toBool();
}

void CppPlugin::writeSettings()
{
    Core::ICore::instance()->settings()->setValue("CppTools/SortedMethodOverview", m_sortedMethodOverview);
}

con's avatar
con committed
void CppPlugin::extensionsInitialized()
{
}

con's avatar
con committed
void CppPlugin::switchDeclarationDefinition()
{
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
con's avatar
con committed
        editor->switchDeclarationDefinition();
}

void CppPlugin::jumpToDefinition()
{
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
con's avatar
con committed
        editor->jumpToDefinition();
}

Q_EXPORT_PLUGIN(CppPlugin)