texteditorplugin.cpp 6.94 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6 7 8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25 26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32
#include "texteditorplugin.h"

#include "findinfiles.h"
con's avatar
con committed
33
#include "findincurrentfile.h"
con's avatar
con committed
34 35 36 37 38 39 40 41 42
#include "fontsettings.h"
#include "linenumberfilter.h"
#include "texteditorconstants.h"
#include "texteditorsettings.h"
#include "textfilewizard.h"
#include "plaintexteditorfactory.h"
#include "plaintexteditor.h"
#include "storagesettings.h"

43
#include <coreplugin/icore.h>
con's avatar
con committed
44 45 46
#include <coreplugin/coreconstants.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/uniqueidmanager.h>
47
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
48
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
49
#include <coreplugin/editormanager/editormanager.h>
50
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
51
#include <texteditor/texteditoractionhandler.h>
52
#include <find/searchresultwindow.h>
hjk's avatar
hjk committed
53
#include <utils/qtcassert.h>
con's avatar
con committed
54

55
#include <QtCore/QtPlugin>
con's avatar
con committed
56
#include <QtGui/QMainWindow>
57
#include <QtGui/QShortcut>
con's avatar
con committed
58 59 60 61 62 63

using namespace TextEditor;
using namespace TextEditor::Internal;

TextEditorPlugin *TextEditorPlugin::m_instance = 0;

64 65
TextEditorPlugin::TextEditorPlugin()
  : m_settings(0),
con's avatar
con committed
66 67
    m_wizard(0),
    m_editorFactory(0),
68 69
    m_lineNumberFilter(0),
    m_searchResultWindow(0)
con's avatar
con committed
70
{
hjk's avatar
hjk committed
71
    QTC_ASSERT(!m_instance, return);
con's avatar
con committed
72 73 74 75 76 77 78 79 80 81 82 83 84
    m_instance = this;
}

TextEditorPlugin::~TextEditorPlugin()
{
    m_instance = 0;
}

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

85 86
// ExtensionSystem::PluginInterface
bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
87
{
88 89
    Q_UNUSED(arguments);

90
    if (!Core::ICore::instance()->mimeDatabase()->addMimeTypes(QLatin1String(":/texteditor/TextEditor.mimetypes.xml"), errorMessage))
con's avatar
con committed
91 92 93
        return false;

    Core::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);
94
    wizardParameters.setDescription(tr("Creates a text file (.txt)."));
con's avatar
con committed
95 96 97 98 99 100
    wizardParameters.setName(tr("Text File"));
    wizardParameters.setCategory(QLatin1String("General"));
    wizardParameters.setTrCategory(tr("General"));
    m_wizard = new TextFileWizard(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
                                  QLatin1String(Core::Constants::K_DEFAULT_TEXT_EDITOR),
                                  QLatin1String("text$"),
101
                                  wizardParameters);
con's avatar
con committed
102 103 104 105
    // Add text file wizard
    addAutoReleasedObject(m_wizard);


106
    m_settings = new TextEditorSettings(this);
con's avatar
con committed
107 108 109 110 111 112

    // Add plain text editor factory
    m_editorFactory = new PlainTextEditorFactory;
    addAutoReleasedObject(m_editorFactory);

    // Goto line functionality for quick open
113
    Core::ICore *core = Core::ICore::instance();
114
    m_lineNumberFilter = new LineNumberFilter;
con's avatar
con committed
115 116
    addAutoReleasedObject(m_lineNumberFilter);

117
    int contextId = core->uniqueIDManager()->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
con's avatar
con committed
118
    QList<int> context = QList<int>() << contextId;
119
    Core::ActionManager *am = core->actionManager();
con's avatar
con committed
120 121

    // Add shortcut for invoking automatic completion
122
    QShortcut *completionShortcut = new QShortcut(core->mainWindow());
con's avatar
con committed
123 124 125
    completionShortcut->setWhatsThis(tr("Triggers a completion in this scope"));
    // Make sure the shortcut still works when the completion widget is active
    completionShortcut->setContext(Qt::ApplicationShortcut);
con's avatar
con committed
126
    Core::Command *command = am->registerShortcut(completionShortcut, Constants::COMPLETE_THIS, context);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
127
#ifndef Q_WS_MAC
con's avatar
con committed
128 129 130 131 132 133
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Space")));
#else
    command->setDefaultKeySequence(QKeySequence(tr("Meta+Space")));
#endif
    connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion()));

Roberto Raggi's avatar
Roberto Raggi committed
134 135 136 137 138 139
    // Add shortcut for invoking automatic completion
    QShortcut *quickFixShortcut = new QShortcut(core->mainWindow());
    quickFixShortcut->setWhatsThis(tr("Triggers a quick fix in this scope"));
    // Make sure the shortcut still works when the quick fix widget is active
    quickFixShortcut->setContext(Qt::ApplicationShortcut);
    Core::Command *quickFixCommand = am->registerShortcut(quickFixShortcut, Constants::QUICKFIX_THIS, context);
Roberto Raggi's avatar
Roberto Raggi committed
140
    quickFixCommand->setDefaultKeySequence(QKeySequence(tr("Alt+Return")));
Roberto Raggi's avatar
Roberto Raggi committed
141
    connect(quickFixShortcut, SIGNAL(activated()), this, SLOT(invokeQuickFix()));
con's avatar
con committed
142 143 144 145 146 147 148

    return true;
}

void TextEditorPlugin::extensionsInitialized()
{
    m_editorFactory->actionHandler()->initializeActions();
149 150 151 152 153 154 155

    m_searchResultWindow = ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>();

    connect(m_settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
            this, SLOT(updateSearchResultsFont(TextEditor::FontSettings)));

    updateSearchResultsFont(m_settings->fontSettings());
156 157 158

    addAutoReleasedObject(new FindInFiles(
        ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>()));
con's avatar
con committed
159 160
    addAutoReleasedObject(new FindInCurrentFile(
        ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>()));
con's avatar
con committed
161 162
}

163
void TextEditorPlugin::initializeEditor(PlainTextEditor *editor)
con's avatar
con committed
164 165 166 167
{
    // common actions
    m_editorFactory->actionHandler()->setupActions(editor);

168
    TextEditorSettings::instance()->initializeEditor(editor);
con's avatar
con committed
169 170 171 172
}

void TextEditorPlugin::invokeCompletion()
{
173
    Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
con's avatar
con committed
174 175 176 177 178
    ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
    if (editor)
        editor->triggerCompletions();
}

Roberto Raggi's avatar
Roberto Raggi committed
179 180 181 182 183 184 185 186
void TextEditorPlugin::invokeQuickFix()
{
    Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
    ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
    if (editor)
        editor->triggerQuickFix();
}

187 188 189 190 191
void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
{
    if (m_searchResultWindow)
        m_searchResultWindow->setTextEditorFont(QFont(settings.family(), settings.fontSize()));
}
con's avatar
con committed
192 193

Q_EXPORT_PLUGIN(TextEditorPlugin)