1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmljseditorfactory.h"
34 #include "qmljseditoreditable.h"
35 #include "qmljseditor.h"
36 #include "qmljseditoractionhandler.h"
37 #include "qmljseditorconstants.h"
38 #include "qmljseditorplugin.h"
40 #include <extensionsystem/pluginmanager.h>
41 #include <extensionsystem/pluginspec.h>
43 #include <coreplugin/icore.h>
44 #include <coreplugin/editormanager/editormanager.h>
46 #include <QtCore/QFileInfo>
47 #include <QtCore/QDebug>
48 #include <QtCore/QSettings>
49 #include <QtGui/QMessageBox>
50 #include <QtGui/QPushButton>
51 #include <QtGui/QMainWindow>
54 const char * const QMLDESIGNER_INFO_BAR = "QmlJSEditor.QmlDesignerInfoBar";
55 const char * const KEY_QMLGROUP = "QML";
56 const char * const KEY_NAGABOUTDESIGNER = "AskAboutVisualDesigner";
58 bool isQmlDesignerExperimentallyDisabled()
60 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
61 foreach (const ExtensionSystem::PluginSpec *spec, pm->plugins()) {
62 if (spec->name() == QLatin1String("QmlDesigner")) {
63 if (spec->isExperimental() && !spec->isEnabled())
71 bool isNaggingAboutExperimentalDesignerEnabled()
73 if (!isQmlDesignerExperimentallyDisabled()) {
76 QSettings *settings = Core::ICore::instance()->settings();
77 settings->beginGroup(QLatin1String(KEY_QMLGROUP));
78 bool nag = settings->value(QLatin1String(KEY_NAGABOUTDESIGNER), true).toBool();
84 using namespace QmlJSEditor;
85 using namespace QmlJSEditor::Internal;
86 using namespace QmlJSEditor::Constants;
88 QmlJSEditorFactory::QmlJSEditorFactory(QObject *parent)
89 : Core::IEditorFactory(parent)
92 << QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE)
93 << QLatin1String(QmlJSEditor::Constants::JS_MIMETYPE)
97 QmlJSEditorFactory::~QmlJSEditorFactory()
101 QString QmlJSEditorFactory::id() const
103 return QLatin1String(C_QMLJSEDITOR_ID);
106 QString QmlJSEditorFactory::displayName() const
108 return tr(C_QMLJSEDITOR_DISPLAY_NAME);
112 Core::IFile *QmlJSEditorFactory::open(const QString &fileName)
114 Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
116 qWarning() << "QmlEditorFactory::open: openEditor failed for " << fileName;
119 return iface->file();
122 Core::IEditor *QmlJSEditorFactory::createEditor(QWidget *parent)
124 static bool listenerInitialized = false;
125 if (!listenerInitialized) {
126 listenerInitialized = true;
127 if (isNaggingAboutExperimentalDesignerEnabled()) {
128 connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
129 SLOT(updateEditorInfoBar(Core::IEditor*)));
132 QmlJSEditor::QmlJSTextEditorWidget *rc = new QmlJSEditor::QmlJSTextEditorWidget(parent);
133 QmlJSEditorPlugin::instance()->initializeEditor(rc);
137 QStringList QmlJSEditorFactory::mimeTypes() const
142 void QmlJSEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
144 if (qobject_cast<QmlJSEditorEditable *>(editor)) {
145 Core::EditorManager::instance()->showEditorInfoBar(QMLDESIGNER_INFO_BAR,
146 tr("Do you want to enable the experimental Qt Quick Designer?"),
147 tr("Enable Qt Quick Designer"), this,
148 SLOT(activateQmlDesigner()),
149 SLOT(neverAskAgainAboutQmlDesigner()));
151 Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
155 void QmlJSEditorFactory::activateQmlDesigner()
159 menu = tr("Qt Creator -> About Plugins...");
161 menu = tr("Help -> About Plugins...");
163 QMessageBox message(Core::ICore::instance()->mainWindow());
164 message.setWindowTitle(tr("Enable experimental Qt Quick Designer?"));
165 message.setText(tr("Do you want to enable the experimental Qt Quick Designer? "
166 "After enabling it, you can access the visual design capabilities by switching to Design Mode. "
167 "This can affect the overall stability of Qt Creator. "
168 "To disable Qt Quick Designer again, visit the menu '%1' and disable 'QmlDesigner'.").arg(menu));
169 message.setIcon(QMessageBox::Question);
170 QPushButton *enable = message.addButton(tr("Enable Qt Quick Designer"), QMessageBox::AcceptRole);
171 message.addButton(tr("Cancel"), QMessageBox::RejectRole);
173 if (message.clickedButton() == enable) {
174 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
175 foreach (ExtensionSystem::PluginSpec *spec, pm->plugins()) {
176 if (spec->name() == QLatin1String("QmlDesigner")) {
177 spec->setEnabled(true);
179 QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Please restart Qt Creator"),
180 tr("Please restart Qt Creator to make the change effective."));
181 disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
182 this, SLOT(updateEditorInfoBar(Core::IEditor*)));
183 Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
184 neverAskAgainAboutQmlDesigner();
191 void QmlJSEditorFactory::neverAskAgainAboutQmlDesigner()
193 QSettings *settings = Core::ICore::instance()->settings();
194 settings->beginGroup(QLatin1String(KEY_QMLGROUP));
195 settings->setValue(QLatin1String(KEY_NAGABOUTDESIGNER), false);
196 settings->endGroup();
198 disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
199 this, SLOT(updateEditorInfoBar(Core::IEditor*)));