1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmljseditorfactory.h"
35 #include "qmljseditoreditable.h"
36 #include "qmljseditor.h"
37 #include "qmljseditoractionhandler.h"
38 #include "qmljseditorconstants.h"
39 #include "qmljseditorplugin.h"
41 #include <extensionsystem/pluginmanager.h>
42 #include <extensionsystem/pluginspec.h>
44 #include <coreplugin/icore.h>
45 #include <coreplugin/editormanager/editormanager.h>
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDebug>
49 #include <QtCore/QSettings>
50 #include <QtGui/QMessageBox>
51 #include <QtGui/QPushButton>
52 #include <QtGui/QMainWindow>
55 const char * const QMLDESIGNER_INFO_BAR = "QmlJSEditor.QmlDesignerInfoBar";
56 const char * const KEY_QMLGROUP = "QML";
57 const char * const KEY_NAGABOUTDESIGNER = "AskAboutVisualDesigner";
59 bool isQmlDesignerExperimentallyDisabled()
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())
72 bool isNaggingAboutExperimentalDesignerEnabled()
74 if (!isQmlDesignerExperimentallyDisabled()) {
77 QSettings *settings = Core::ICore::instance()->settings();
78 settings->beginGroup(QLatin1String(KEY_QMLGROUP));
79 bool nag = settings->value(QLatin1String(KEY_NAGABOUTDESIGNER), true).toBool();
85 using namespace QmlJSEditor;
86 using namespace QmlJSEditor::Internal;
87 using namespace QmlJSEditor::Constants;
89 QmlJSEditorFactory::QmlJSEditorFactory(QObject *parent)
90 : Core::IEditorFactory(parent)
93 << QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE)
94 << QLatin1String(QmlJSEditor::Constants::JS_MIMETYPE)
98 QmlJSEditorFactory::~QmlJSEditorFactory()
102 QString QmlJSEditorFactory::id() const
104 return QLatin1String(C_QMLJSEDITOR_ID);
107 QString QmlJSEditorFactory::displayName() const
109 return tr(C_QMLJSEDITOR_DISPLAY_NAME);
113 Core::IFile *QmlJSEditorFactory::open(const QString &fileName)
115 Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
117 qWarning() << "QmlEditorFactory::open: openEditor failed for " << fileName;
120 return iface->file();
123 Core::IEditor *QmlJSEditorFactory::createEditor(QWidget *parent)
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*)));
133 QmlJSEditor::QmlJSTextEditorWidget *rc = new QmlJSEditor::QmlJSTextEditorWidget(parent);
134 QmlJSEditorPlugin::instance()->initializeEditor(rc);
138 QStringList QmlJSEditorFactory::mimeTypes() const
143 void QmlJSEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
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()));
152 Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
156 void QmlJSEditorFactory::activateQmlDesigner()
160 menu = tr("Qt Creator -> About Plugins...");
162 menu = tr("Help -> About Plugins...");
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);
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);
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();
192 void QmlJSEditorFactory::neverAskAgainAboutQmlDesigner()
194 QSettings *settings = Core::ICore::instance()->settings();
195 settings->beginGroup(QLatin1String(KEY_QMLGROUP));
196 settings->setValue(QLatin1String(KEY_NAGABOUTDESIGNER), false);
197 settings->endGroup();
199 disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
200 this, SLOT(updateEditorInfoBar(Core::IEditor*)));