texteditorplugin.cpp 7.18 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
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
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
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
    Q_UNUSED(arguments)
89

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)."));
95
    wizardParameters.setDisplayName(tr("Text File"));
con's avatar
con committed
96
    wizardParameters.setCategory(QLatin1String("U.General"));
97
    wizardParameters.setDisplayCategory(tr("General"));
con's avatar
con committed
98
    m_wizard = new TextFileWizard(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
99
                                  QLatin1String(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID),
con's avatar
con committed
100
                                  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
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Space")));
#else
130
    command->setDefaultKeySequence(QKeySequence(tr("Alt+Space")));
con's avatar
con committed
131
132
133
#endif
    connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion()));

134
    // Add shortcut for invoking quick fix options
Roberto Raggi's avatar
Roberto Raggi committed
135
136
137
138
139
    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
    connect(m_settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
        this, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)));

con's avatar
con committed
146
147
148
149
150
151
    return true;
}

void TextEditorPlugin::extensionsInitialized()
{
    m_editorFactory->actionHandler()->initializeActions();
152
153
154
155
156
157
158

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

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

    updateSearchResultsFont(m_settings->fontSettings());
159
160
161

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

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

171
    TextEditorSettings::instance()->initializeEditor(editor);
con's avatar
con committed
172
173
174
175
}

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

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

190
191
192
void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
{
    if (m_searchResultWindow)
193
194
        m_searchResultWindow->setTextEditorFont(QFont(settings.family(),
                                                      settings.fontSize() * settings.fontZoom() / 100));
195
}
con's avatar
con committed
196
197

Q_EXPORT_PLUGIN(TextEditorPlugin)