qmljseditorplugin.cpp 8.24 KB
Newer Older
1
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).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

30
#include "qmljseditorplugin.h"
31
#include "qmljshighlighter.h"
32
33
34
#include "qmljseditor.h"
#include "qmljseditorconstants.h"
#include "qmljseditorfactory.h"
Roberto Raggi's avatar
Roberto Raggi committed
35
#include "qmljscodecompletion.h"
Roberto Raggi's avatar
Roberto Raggi committed
36
#include "qmljshoverhandler.h"
Roberto Raggi's avatar
Roberto Raggi committed
37
#include "qmljsmodelmanager.h"
Erik Verbruggen's avatar
Erik Verbruggen committed
38
#include "qmlfilewizard.h"
39
40
41
#include "qmljspreviewrunner.h"

#include <qmldesigner/qmldesignerconstants.h>
42
43
44
45
46

#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/uniqueidmanager.h>
47
#include <coreplugin/fileiconprovider.h>
48
#include <coreplugin/actionmanager/actionmanager.h>
49
50
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
51
#include <coreplugin/editormanager/editormanager.h>
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <extensionsystem/pluginmanager.h>
#include <texteditor/fontsettings.h>
#include <texteditor/storagesettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/textfilewizard.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/completionsupport.h>
#include <utils/qtcassert.h>

#include <QtCore/QtPlugin>
#include <QtCore/QDebug>
#include <QtCore/QSettings>
65
66
#include <QtCore/QDir>
#include <QtCore/QCoreApplication>
67
#include <QtGui/QMenu>
68
69
#include <QtGui/QAction>

70
71
72
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
using namespace QmlJSEditor::Constants;
73

74
QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0;
75

76
QmlJSEditorPlugin::QmlJSEditorPlugin() :
77
        m_modelManager(0),
78
79
    m_wizard(0),
    m_editor(0),
80
    m_actionHandler(0)
81
82
83
84
{
    m_instance = this;
}

85
QmlJSEditorPlugin::~QmlJSEditorPlugin()
86
87
88
89
90
91
{
    removeObject(m_editor);
    delete m_actionHandler;
    m_instance = 0;
}

92
bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *error_message)
93
94
{
    Core::ICore *core = Core::ICore::instance();
95
    if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/qmljseditor/QmlJSEditor.mimetypes.xml"), error_message))
96
        return false;
97

Roberto Raggi's avatar
Roberto Raggi committed
98
    m_modelManager = new ModelManager(this);
99
100
    addAutoReleasedObject(m_modelManager);

mae's avatar
mae committed
101
    QList<int> context;
102
103
    context << core->uniqueIDManager()->uniqueIdentifier(QmlJSEditor::Constants::C_QMLJSEDITOR_ID)
            << core->uniqueIDManager()->uniqueIdentifier(QmlDesigner::Constants::C_QT_QUICK_TOOLS_MENU);
104

105
    m_editor = new QmlJSEditorFactory(this);
106
107
    addObject(m_editor);

108
#ifdef QTCREATOR_WITH_QML
109
    Core::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);
Friedemann Kleint's avatar
Friedemann Kleint committed
110
    wizardParameters.setCategory(QLatin1String(Core::Constants::WIZARD_CATEGORY_QT));
111
    wizardParameters.setDisplayCategory(QCoreApplication::translate("Core", Core::Constants::WIZARD_TR_CATEGORY_QT));
Erik Verbruggen's avatar
Erik Verbruggen committed
112
    wizardParameters.setDescription(tr("Creates a Qt QML file."));
113
    wizardParameters.setDisplayName(tr("Qt QML File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
114
    wizardParameters.setId(QLatin1String("Q.Qml"));
Erik Verbruggen's avatar
Erik Verbruggen committed
115
    addAutoReleasedObject(new QmlFileWizard(wizardParameters, core));
116
#endif
117

118
    m_actionHandler = new TextEditor::TextEditorActionHandler(QmlJSEditor::Constants::C_QMLJSEDITOR_ID,
119
120
121
          TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
        | TextEditor::TextEditorActionHandler::UnCollapseAll);
122
    m_actionHandler->initializeActions();
123

124
    Core::ActionManager *am =  core->actionManager();
125
126
    Core::ActionContainer *contextMenu = am->createMenu(QmlJSEditor::Constants::M_CONTEXT);

127
128
129
130
131
132
133
134
135
136
137
138
    Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
    Core::ActionContainer *menuQtQuick = am->createMenu(Constants::M_QTQUICK);
    menuQtQuick->menu()->setTitle(tr("Qt Quick"));
    mtools->addMenu(menuQtQuick);
    m_actionPreview = new QAction("&Preview", this);

    QList<int> toolsMenuContext = QList<int>()
                                  << core->uniqueIDManager()->uniqueIdentifier(QmlDesigner::Constants::C_QT_QUICK_TOOLS_MENU);
    Core::Command *cmd = addToolAction(m_actionPreview,  am, toolsMenuContext,
                   QLatin1String("QtQuick.Preview"), menuQtQuick, tr("Ctrl+Alt+R"));
    connect(cmd->action(), SIGNAL(triggered()), SLOT(openPreview()));
    m_previewRunner = new QmlJSPreviewRunner(this);
139
140
141
142
143

    QAction *followSymbolUnderCursorAction = new QAction(tr("Follow Symbol Under Cursor"), this);
    cmd = am->registerAction(followSymbolUnderCursorAction, Constants::FOLLOW_SYMBOL_UNDER_CURSOR, context);
    cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
    connect(followSymbolUnderCursorAction, SIGNAL(triggered()), this, SLOT(followSymbolUnderCursor()));
144
    contextMenu->addAction(cmd);
145
146
147
148

    cmd = am->command(TextEditor::Constants::AUTO_INDENT_SELECTION);
    contextMenu->addAction(cmd);

149
150
151
    cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
    contextMenu->addAction(cmd);

152
153
    CodeCompletion *completion = new CodeCompletion(m_modelManager);
    addAutoReleasedObject(completion);
154

155
    addAutoReleasedObject(new HoverHandler);
156

157
158
159
160
161
    // Set completion settings and keep them up to date
    TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
    completion->setCompletionSettings(textEditorSettings->completionSettings());
    connect(textEditorSettings, SIGNAL(completionSettingsChanged(TextEditor::CompletionSettings)),
            completion, SLOT(setCompletionSettings(TextEditor::CompletionSettings)));
162

163
164
    error_message->clear();

165
166
167
    Core::FileIconProvider *iconProvider = Core::FileIconProvider::instance();
    iconProvider->registerIconOverlayForSuffix(QIcon(":/qmljseditor/images/qmlfile.png"), "qml");

168
169
170
    return true;
}

171
void QmlJSEditorPlugin::extensionsInitialized()
172
173
174
{
}

175
176
177
178
179
180
181
182
183
void QmlJSEditorPlugin::openPreview()
{
    Core::EditorManager *em = Core::EditorManager::instance();

    if (em->currentEditor() && em->currentEditor()->id() == Constants::C_QMLJSEDITOR_ID)
        m_previewRunner->run(em->currentEditor()->file()->fileName());

}

184
void QmlJSEditorPlugin::initializeEditor(QmlJSEditor::Internal::QmlJSTextEditor *editor)
185
186
187
188
189
190
191
192
{
    QTC_ASSERT(m_instance, /**/);

    m_actionHandler->setupActions(editor);

    TextEditor::TextEditorSettings::instance()->initializeEditor(editor);

    // auto completion
193
194
    connect(editor, SIGNAL(requestAutoCompletion(TextEditor::ITextEditable*, bool)),
            TextEditor::Internal::CompletionSupport::instance(), SLOT(autoComplete(TextEditor::ITextEditable*, bool)));
195
196
}

197
198
199
200
201
202
203
204
void QmlJSEditorPlugin::followSymbolUnderCursor()
{
    Core::EditorManager *em = Core::EditorManager::instance();

    if (QmlJSTextEditor *editor = qobject_cast<QmlJSTextEditor*>(em->currentEditor()->widget()))
        editor->followSymbolUnderCursor();
}

205
206
207
208
209
210
211
212
213
214
215
216
Core::Command *QmlJSEditorPlugin::addToolAction(QAction *a, Core::ActionManager *am,
                                          const QList<int> &context, const QString &name,
                                          Core::ActionContainer *c1, const QString &keySequence)
{
    Core::Command *command = am->registerAction(a, name, context);
    if (!keySequence.isEmpty())
        command->setDefaultKeySequence(QKeySequence(keySequence));
    c1->addAction(command);
    return command;
}


217
Q_EXPORT_PLUGIN(QmlJSEditorPlugin)