OSDN Git Service

Update license.
[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 (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 qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmljseditorfactory.h"
34 #include "qmljseditoreditable.h"
35 #include "qmljseditor.h"
36 #include "qmljseditoractionhandler.h"
37 #include "qmljseditorconstants.h"
38 #include "qmljseditorplugin.h"
39
40 #include <extensionsystem/pluginmanager.h>
41 #include <extensionsystem/pluginspec.h>
42
43 #include <coreplugin/icore.h>
44 #include <coreplugin/editormanager/editormanager.h>
45
46 #include <QtCore/QFileInfo>
47 #include <QtCore/QDebug>
48 #include <QtCore/QSettings>
49 #include <QtGui/QMessageBox>
50 #include <QtGui/QPushButton>
51 #include <QtGui/QMainWindow>
52
53 namespace {
54     const char * const QMLDESIGNER_INFO_BAR = "QmlJSEditor.QmlDesignerInfoBar";
55     const char * const KEY_QMLGROUP = "QML";
56     const char * const KEY_NAGABOUTDESIGNER = "AskAboutVisualDesigner";
57
58     bool isQmlDesignerExperimentallyDisabled()
59     {
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())
64                     return true;
65                 return false;
66             }
67         }
68         return false;
69     }
70
71     bool isNaggingAboutExperimentalDesignerEnabled()
72     {
73         if (!isQmlDesignerExperimentallyDisabled()) {
74             return false;
75         }
76         QSettings *settings = Core::ICore::instance()->settings();
77         settings->beginGroup(QLatin1String(KEY_QMLGROUP));
78         bool nag = settings->value(QLatin1String(KEY_NAGABOUTDESIGNER), true).toBool();
79         settings->endGroup();
80         return nag;
81     }
82 }
83
84 using namespace QmlJSEditor;
85 using namespace QmlJSEditor::Internal;
86 using namespace QmlJSEditor::Constants;
87
88 QmlJSEditorFactory::QmlJSEditorFactory(QObject *parent)
89   : Core::IEditorFactory(parent)
90 {
91     m_mimeTypes
92             << QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE)
93             << QLatin1String(QmlJSEditor::Constants::JS_MIMETYPE)
94             ;
95 }
96
97 QmlJSEditorFactory::~QmlJSEditorFactory()
98 {
99 }
100
101 QString QmlJSEditorFactory::id() const
102 {
103     return QLatin1String(C_QMLJSEDITOR_ID);
104 }
105
106 QString QmlJSEditorFactory::displayName() const
107 {
108     return tr(C_QMLJSEDITOR_DISPLAY_NAME);
109 }
110
111
112 Core::IFile *QmlJSEditorFactory::open(const QString &fileName)
113 {
114     Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
115     if (!iface) {
116         qWarning() << "QmlEditorFactory::open: openEditor failed for " << fileName;
117         return 0;
118     }
119     return iface->file();
120 }
121
122 Core::IEditor *QmlJSEditorFactory::createEditor(QWidget *parent)
123 {
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*)));
130         }
131     }
132     QmlJSEditor::QmlJSTextEditorWidget *rc = new QmlJSEditor::QmlJSTextEditorWidget(parent);
133     QmlJSEditorPlugin::instance()->initializeEditor(rc);
134     return rc->editor();
135 }
136
137 QStringList QmlJSEditorFactory::mimeTypes() const
138 {
139     return m_mimeTypes;
140 }
141
142 void QmlJSEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
143 {
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()));
150     } else {
151         Core::EditorManager::instance()->hideEditorInfoBar(QMLDESIGNER_INFO_BAR);
152     }
153 }
154
155 void QmlJSEditorFactory::activateQmlDesigner()
156 {
157     QString menu;
158 #ifdef Q_WS_MAC
159     menu = tr("Qt Creator -> About Plugins...");
160 #else
161     menu = tr("Help -> About Plugins...");
162 #endif
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);
172     message.exec();
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);
178                 pm->writeSettings();
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();
185                 return;
186             }
187         }
188     }
189 }
190
191 void QmlJSEditorFactory::neverAskAgainAboutQmlDesigner()
192 {
193     QSettings *settings = Core::ICore::instance()->settings();
194     settings->beginGroup(QLatin1String(KEY_QMLGROUP));
195     settings->setValue(QLatin1String(KEY_NAGABOUTDESIGNER), false);
196     settings->endGroup();
197     settings->sync();
198     disconnect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
199              this, SLOT(updateEditorInfoBar(Core::IEditor*)));
200 }