OSDN Git Service

6689cf827f592d7262f93722563fef10cec49c68
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljseditorfactory.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljseditorfactory.h"
35 #include "qmljseditoreditable.h"
36 #include "qmljseditor.h"
37 #include "qmljseditoractionhandler.h"
38 #include "qmljseditorconstants.h"
39 #include "qmljseditorplugin.h"
40
41 #include <extensionsystem/pluginmanager.h>
42 #include <extensionsystem/pluginspec.h>
43
44 #include <coreplugin/icore.h>
45 #include <coreplugin/editormanager/editormanager.h>
46
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDebug>
49 #include <QtCore/QSettings>
50 #include <QtGui/QMessageBox>
51 #include <QtGui/QPushButton>
52 #include <QtGui/QMainWindow>
53
54 namespace {
55     const char * const QMLDESIGNER_INFO_BAR = "QmlJSEditor.QmlDesignerInfoBar";
56     const char * const KEY_QMLGROUP = "QML";
57     const char * const KEY_NAGABOUTDESIGNER = "AskAboutVisualDesigner";
58
59     bool isQmlDesignerExperimentallyDisabled()
60     {
61         ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
62         foreach (const ExtensionSystem::PluginSpec *spec, pm->plugins()) {
63             if (spec->name() == QLatin1String("QmlDesigner")) {
64                 if (spec->isExperimental() && !spec->isEnabled())
65                     return true;
66                 return false;
67             }
68         }
69         return false;
70     }
71
72     bool isNaggingAboutExperimentalDesignerEnabled()
73     {
74         if (!isQmlDesignerExperimentallyDisabled()) {
75             return false;
76         }
77         QSettings *settings = Core::ICore::instance()->settings();
78         settings->beginGroup(QLatin1String(KEY_QMLGROUP));
79         bool nag = settings->value(QLatin1String(KEY_NAGABOUTDESIGNER), true).toBool();
80         settings->endGroup();
81         return nag;
82     }
83 }
84
85 using namespace QmlJSEditor;
86 using namespace QmlJSEditor::Internal;
87 using namespace QmlJSEditor::Constants;
88
89 QmlJSEditorFactory::QmlJSEditorFactory(QObject *parent)
90   : Core::IEditorFactory(parent)
91 {
92     m_mimeTypes
93             << QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE)
94             << QLatin1String(QmlJSEditor::Constants::JS_MIMETYPE)
95             ;
96 }
97
98 QmlJSEditorFactory::~QmlJSEditorFactory()
99 {
100 }
101
102 QString QmlJSEditorFactory::id() const
103 {
104     return QLatin1String(C_QMLJSEDITOR_ID);
105 }
106
107 QString QmlJSEditorFactory::displayName() const
108 {
109     return tr(C_QMLJSEDITOR_DISPLAY_NAME);
110 }
111
112
113 Core::IFile *QmlJSEditorFactory::open(const QString &fileName)
114 {
115     Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
116     if (!iface) {
117         qWarning() << "QmlEditorFactory::open: openEditor failed for " << fileName;
118         return 0;
119     }
120     return iface->file();
121 }
122
123 Core::IEditor *QmlJSEditorFactory::createEditor(QWidget *parent)
124 {
125     static bool listenerInitialized = false;
126     if (!listenerInitialized) {
127         listenerInitialized = true;
128         if (isNaggingAboutExperimentalDesignerEnabled()) {
129             connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
130                      SLOT(updateEditorInfoBar(Core::IEditor*)));
131         }
132     }
133     QmlJSEditor::QmlJSTextEditorWidget *rc = new QmlJSEditor::QmlJSTextEditorWidget(parent);
134     QmlJSEditorPlugin::instance()->initializeEditor(rc);
135     return rc->editor();
136 }
137
138 QStringList QmlJSEditorFactory::mimeTypes() const
139 {
140     return m_mimeTypes;
141 }
142
143 void QmlJSEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
144 {
145     if (qobject_cast<QmlJSEditorEditable *>(editor)) {
146         Core::EditorManager::instance()->showEditorInfoBar(QMLDESIGNER_INFO_BAR,
147             tr("Do you want to enable the experimental Qt Quick Designer?"),
148             tr("Enable Qt Quick Designer"), this,
149             SLOT(activateQmlDesigner()),
150             SLOT(neverAskAgainAboutQmlDesigner()));
151     } else {
152         Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
153     }
154 }
155
156 void QmlJSEditorFactory::activateQmlDesigner()
157 {
158     QString menu;
159 #ifdef Q_WS_MAC
160     menu = tr("Qt Creator -> About Plugins...");
161 #else
162     menu = tr("Help -> About Plugins...");
163 #endif
164     QMessageBox message(Core::ICore::instance()->mainWindow());
165     message.setWindowTitle(tr("Enable experimental Qt Quick Designer?"));
166     message.setText(tr("Do you want to enable the experimental Qt Quick Designer? "
167                        "After enabling it, you can access the visual design capabilities by switching to Design Mode. "
168                        "This can affect the overall stability of Qt Creator. "
169                        "To disable Qt Quick Designer again, visit the menu '%1' and disable 'QmlDesigner'.").arg(menu));
170     message.setIcon(QMessageBox::Question);
171     QPushButton *enable = message.addButton(tr("Enable Qt Quick Designer"), QMessageBox::AcceptRole);
172     message.addButton(tr("Cancel"), QMessageBox::RejectRole);
173     message.exec();
174     if (message.clickedButton() == enable) {
175         ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
176         foreach (ExtensionSystem::PluginSpec *spec, pm->plugins()) {
177             if (spec->name() == QLatin1String("QmlDesigner")) {
178                 spec->setEnabled(true);
179                 pm->writeSettings();
180                 QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Please restart Qt Creator"),
181                                          tr("Please restart Qt Creator to make the change effective."));
182                 disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
183                          this, SLOT(updateEditorInfoBar(Core::IEditor*)));
184                 Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
185                 neverAskAgainAboutQmlDesigner();
186                 return;
187             }
188         }
189     }
190 }
191
192 void QmlJSEditorFactory::neverAskAgainAboutQmlDesigner()
193 {
194     QSettings *settings = Core::ICore::instance()->settings();
195     settings->beginGroup(QLatin1String(KEY_QMLGROUP));
196     settings->setValue(QLatin1String(KEY_NAGABOUTDESIGNER), false);
197     settings->endGroup();
198     settings->sync();
199     disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
200              this, SLOT(updateEditorInfoBar(Core::IEditor*)));
201 }