OSDN Git Service

Apply code style settings to templates
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljstools / qmljstoolsplugin.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmljstoolsplugin.h"
34 #include "qmljsmodelmanager.h"
35 #include "qmljsfunctionfilter.h"
36 #include "qmljslocatordata.h"
37 #include "qmljscodestylesettingspage.h"
38 #include "qmljstoolsconstants.h"
39 #include "qmljstoolssettings.h"
40 #include "qmljscodestylesettingsfactory.h"
41
42 #include <texteditor/texteditorsettings.h>
43 #include <texteditor/tabsettings.h>
44 #include <texteditor/codestylepreferencesmanager.h>
45
46 #include <extensionsystem/pluginmanager.h>
47
48 #include <coreplugin/icore.h>
49 #include <coreplugin/coreconstants.h>
50 #include <coreplugin/actionmanager/actionmanager.h>
51 #include <coreplugin/actionmanager/actioncontainer.h>
52 #include <coreplugin/actionmanager/command.h>
53 #include <coreplugin/progressmanager/progressmanager.h>
54
55 #include <QtCore/QtPlugin>
56 #include <QtCore/QFileInfo>
57 #include <QtCore/QDir>
58 #include <QtCore/QDebug>
59 #include <QtCore/QSettings>
60 #include <QtGui/QMenu>
61
62 using namespace QmlJSTools::Internal;
63
64 enum { debug = 0 };
65
66 QmlJSToolsPlugin *QmlJSToolsPlugin::m_instance = 0;
67
68 QmlJSToolsPlugin::QmlJSToolsPlugin()
69     : m_modelManager(0)
70 {
71     m_instance = this;
72 }
73
74 QmlJSToolsPlugin::~QmlJSToolsPlugin()
75 {
76     m_instance = 0;
77     m_modelManager = 0; // deleted automatically
78 }
79
80 bool QmlJSToolsPlugin::initialize(const QStringList &arguments, QString *error)
81 {
82     Q_UNUSED(arguments)
83     Q_UNUSED(error)
84
85     Core::ICore *core = Core::ICore::instance();
86     Core::ActionManager *am = core->actionManager();
87
88     m_settings = new QmlJSToolsSettings(this); // force registration of qmljstools settings
89
90     // Objects
91     m_modelManager = new ModelManager(this);
92 //    Core::VCSManager *vcsManager = core->vcsManager();
93 //    Core::FileManager *fileManager = core->fileManager();
94 //    connect(vcsManager, SIGNAL(repositoryChanged(QString)),
95 //            m_modelManager, SLOT(updateModifiedSourceFiles()));
96 //    connect(fileManager, SIGNAL(filesChangedInternally(QStringList)),
97 //            m_modelManager, SLOT(updateSourceFiles(QStringList)));
98     addAutoReleasedObject(m_modelManager);
99
100     LocatorData *locatorData = new LocatorData;
101     addAutoReleasedObject(locatorData);
102     addAutoReleasedObject(new FunctionFilter(locatorData));
103     addAutoReleasedObject(new QmlJSCodeStyleSettingsPage);
104
105     TextEditor::CodeStylePreferencesManager::instance()->registerFactory(
106                 new QmlJSTools::QmlJSCodeStylePreferencesFactory());
107
108     // Menus
109     Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
110     Core::ActionContainer *mqmljstools = am->createMenu(Constants::M_TOOLS_QMLJS);
111     QMenu *menu = mqmljstools->menu();
112     menu->setTitle(tr("&QML/JS"));
113     menu->setEnabled(true);
114     mtools->addMenu(mqmljstools);
115
116     // Update context in global context
117     m_resetCodeModelAction = new QAction(tr("Reset Code Model"), this);
118     Core::Context globalContext(Core::Constants::C_GLOBAL);
119     Core::Command *cmd = am->registerAction(m_resetCodeModelAction, Core::Id(Constants::RESET_CODEMODEL), globalContext);
120     connect(m_resetCodeModelAction, SIGNAL(triggered()), m_modelManager, SLOT(resetCodeModel()));
121     mqmljstools->addAction(cmd);
122
123     // watch task progress
124     connect(core->progressManager(), SIGNAL(taskStarted(QString)),
125             this, SLOT(onTaskStarted(QString)));
126     connect(core->progressManager(), SIGNAL(allTasksFinished(QString)),
127             this, SLOT(onAllTasksFinished(QString)));
128
129     TextEditor::TextEditorSettings *ts = TextEditor::TextEditorSettings::instance();
130     ts->registerMimeTypeForLanguageId(QLatin1String(Constants::QML_MIMETYPE), Constants::QML_JS_SETTINGS_ID);
131     ts->registerMimeTypeForLanguageId(QLatin1String(Constants::JS_MIMETYPE), Constants::QML_JS_SETTINGS_ID);
132
133     return true;
134 }
135
136 void QmlJSToolsPlugin::extensionsInitialized()
137 {
138     m_modelManager->delayedInitialization();
139 }
140
141 ExtensionSystem::IPlugin::ShutdownFlag QmlJSToolsPlugin::aboutToShutdown()
142 {
143     return SynchronousShutdown;
144 }
145
146 void QmlJSToolsPlugin::onTaskStarted(const QString &type)
147 {
148     if (type == QmlJSTools::Constants::TASK_INDEX) {
149         m_resetCodeModelAction->setEnabled(false);
150     }
151 }
152
153 void QmlJSToolsPlugin::onAllTasksFinished(const QString &type)
154 {
155     if (type == QmlJSTools::Constants::TASK_INDEX) {
156         m_resetCodeModelAction->setEnabled(true);
157     }
158 }
159
160 Q_EXPORT_PLUGIN(QmlJSToolsPlugin)