OSDN Git Service

Implement qml tab settings
authorJarek Kobus <jkobus@trolltech.com>
Wed, 25 May 2011 06:50:01 +0000 (08:50 +0200)
committerChristian Kamm <christian.d.kamm@nokia.com>
Wed, 25 May 2011 07:41:35 +0000 (09:41 +0200)
Change-Id: I0b7365b5b3d7538f2b4a8c5eaff3420f448dd5be
Reviewed-on: http://codereview.qt.nokia.com/99
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Christian Kamm <christian.d.kamm@nokia.com>
src/plugins/qmljstools/qmljscodestylesettingsfactory.cpp [new file with mode: 0644]
src/plugins/qmljstools/qmljscodestylesettingsfactory.h [new file with mode: 0644]
src/plugins/qmljstools/qmljscodestylesettingspage.cpp
src/plugins/qmljstools/qmljscodestylesettingspage.h
src/plugins/qmljstools/qmljstools-lib.pri
src/plugins/qmljstools/qmljstoolsplugin.cpp
src/plugins/qmljstools/qmljstoolsplugin.h
src/plugins/qmljstools/qmljstoolssettings.cpp [new file with mode: 0644]
src/plugins/qmljstools/qmljstoolssettings.h [new file with mode: 0644]

diff --git a/src/plugins/qmljstools/qmljscodestylesettingsfactory.cpp b/src/plugins/qmljstools/qmljscodestylesettingsfactory.cpp
new file mode 100644 (file)
index 0000000..ab6d556
--- /dev/null
@@ -0,0 +1,41 @@
+#include "qmljscodestylesettingsfactory.h"
+#include "qmljscodestylesettingspage.h"
+#include "qmljstoolsconstants.h"
+#include <texteditor/tabpreferences.h>
+#include <texteditor/tabsettings.h>
+#include <QtGui/QLayout>
+
+using namespace QmlJSTools;
+
+QmlJSCodeStylePreferencesFactory::QmlJSCodeStylePreferencesFactory()
+{
+}
+
+QString QmlJSCodeStylePreferencesFactory::languageId()
+{
+    return Constants::QML_JS_SETTINGS_ID;
+}
+
+QString QmlJSCodeStylePreferencesFactory::displayName()
+{
+    return Constants::QML_JS_SETTINGS_NAME;
+}
+
+TextEditor::IFallbackPreferences *QmlJSCodeStylePreferencesFactory::createPreferences(
+    const QList<TextEditor::IFallbackPreferences *> &fallbacks) const
+{
+    return 0;
+}
+
+QWidget *QmlJSCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPreferences *preferences,
+                                                           TextEditor::TabPreferences *tabPreferences,
+                                                           QWidget *parent) const
+{
+    Q_UNUSED(preferences)
+
+    Internal::QmlJSCodeStylePreferencesWidget *widget = new Internal::QmlJSCodeStylePreferencesWidget(parent);
+    widget->layout()->setMargin(0);
+    widget->setTabPreferences(tabPreferences);
+    return widget;
+}
+
diff --git a/src/plugins/qmljstools/qmljscodestylesettingsfactory.h b/src/plugins/qmljstools/qmljscodestylesettingsfactory.h
new file mode 100644 (file)
index 0000000..321f564
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef QMLJSCODESTYLESETTINGSFACTORY_H
+#define QMLJSCODESTYLESETTINGSFACTORY_H
+
+#include <texteditor/icodestylepreferencesfactory.h>
+
+namespace QmlJSTools {
+
+class QmlJSCodeStylePreferencesFactory : public TextEditor::ICodeStylePreferencesFactory
+{
+public:
+    QmlJSCodeStylePreferencesFactory();
+
+    virtual QString languageId();
+    virtual QString displayName();
+    virtual TextEditor::IFallbackPreferences *createPreferences(const QList<TextEditor::IFallbackPreferences *> &fallbacks) const;
+    virtual QWidget *createEditor(TextEditor::IFallbackPreferences *settings,
+                                          TextEditor::TabPreferences *tabSettings,
+                                          QWidget *parent) const;
+
+};
+
+} // namespace QmlJSTools
+
+#endif // QMLJSCODESTYLESETTINGSFACTORY_H
index 01968e4..31afdba 100644 (file)
@@ -1,6 +1,7 @@
 #include "qmljscodestylesettingspage.h"
 #include "ui_qmljscodestylesettingspage.h"
 #include "qmljstoolsconstants.h"
+#include "qmljstoolssettings.h"
 #include "qmljsindenter.h"
 #include "qmljsqtstylecodeformatter.h"
 
@@ -24,7 +25,7 @@ namespace Internal {
 
 // ------------------ CppCodeStyleSettingsWidget
 
-QmlJSCodeStyleSettingsWidget::QmlJSCodeStyleSettingsWidget(QWidget *parent) :
+QmlJSCodeStylePreferencesWidget::QmlJSCodeStylePreferencesWidget(QWidget *parent) :
     QWidget(parent),
     m_tabPreferences(0),
     m_ui(new ::Ui::QmlJSCodeStyleSettingsPage)
@@ -49,22 +50,22 @@ QmlJSCodeStyleSettingsWidget::QmlJSCodeStyleSettingsWidget(QWidget *parent) :
     updatePreview();
 }
 
-QmlJSCodeStyleSettingsWidget::~QmlJSCodeStyleSettingsWidget()
+QmlJSCodeStylePreferencesWidget::~QmlJSCodeStylePreferencesWidget()
 {
     delete m_ui;
 }
 
-void QmlJSCodeStyleSettingsWidget::setTabPreferences(TextEditor::TabPreferences *tabPreferences)
+void QmlJSCodeStylePreferencesWidget::setTabPreferences(TextEditor::TabPreferences *tabPreferences)
 {
     m_tabPreferences = tabPreferences;
     m_ui->tabPreferencesWidget->setTabPreferences(tabPreferences);
     connect(m_tabPreferences, SIGNAL(currentSettingsChanged(TextEditor::TabSettings)),
-            this, SLOT(slotTabSettingsChanged()));
+            this, SLOT(slotSettingsChanged()));
     updatePreview();
 }
 
 
-QString QmlJSCodeStyleSettingsWidget::searchKeywords() const
+QString QmlJSCodeStylePreferencesWidget::searchKeywords() const
 {
     QString rc;
     QLatin1Char sep(' ');
@@ -75,29 +76,30 @@ QString QmlJSCodeStyleSettingsWidget::searchKeywords() const
     return rc;
 }
 
-void QmlJSCodeStyleSettingsWidget::setFontSettings(const TextEditor::FontSettings &fontSettings)
+void QmlJSCodeStylePreferencesWidget::setFontSettings(const TextEditor::FontSettings &fontSettings)
 {
     m_ui->previewTextEdit->setFont(fontSettings.font());
 }
 
-void QmlJSCodeStyleSettingsWidget::setVisualizeWhitespace(bool on)
+void QmlJSCodeStylePreferencesWidget::setVisualizeWhitespace(bool on)
 {
     DisplaySettings displaySettings = m_ui->previewTextEdit->displaySettings();
     displaySettings.m_visualizeWhitespace = on;
     m_ui->previewTextEdit->setDisplaySettings(displaySettings);
 }
 
-void QmlJSCodeStyleSettingsWidget::slotTabSettingsChanged()
+void QmlJSCodeStylePreferencesWidget::slotSettingsChanged()
 {
     updatePreview();
 }
 
-void QmlJSCodeStyleSettingsWidget::updatePreview()
+void QmlJSCodeStylePreferencesWidget::updatePreview()
 {
     QTextDocument *doc = m_ui->previewTextEdit->document();
 
     const TextEditor::TabSettings &ts = m_tabPreferences
-            ? m_tabPreferences->currentSettings() : TextEditorSettings::instance()->tabPreferences()->settings();
+            ? m_tabPreferences->currentSettings()
+            : TextEditorSettings::instance()->tabPreferences()->settings();
     m_ui->previewTextEdit->setTabSettings(ts);
     QtStyleCodeFormatter formatter(ts);
     formatter.invalidateCache(doc);
@@ -119,17 +121,9 @@ void QmlJSCodeStyleSettingsWidget::updatePreview()
 
 QmlJSCodeStyleSettingsPage::QmlJSCodeStyleSettingsPage(/*QSharedPointer<CppFileSettings> &settings,*/
                      QWidget *parent) :
-    Core::IOptionsPage(parent)/*,
-    m_settings(settings)*/
-{
-    if (const QSettings *s = Core::ICore::instance()->settings()) {
-        TextEditor::TabSettings tabSettings;
-        // read it from old global settings for the first time?
-        tabSettings.fromSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID, s);
-//        TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
-//        textEditorSettings->setTabSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID,
-//                                           tabSettings);
-    }
+    Core::IOptionsPage(parent),
+    m_pageTabPreferences(0)
+{
 }
 
 QmlJSCodeStyleSettingsPage::~QmlJSCodeStyleSettingsPage()
@@ -163,10 +157,14 @@ QIcon QmlJSCodeStyleSettingsPage::categoryIcon() const
 
 QWidget *QmlJSCodeStyleSettingsPage::createPage(QWidget *parent)
 {
-    m_widget = new QmlJSCodeStyleSettingsWidget(parent);
-    m_widget->setTabPreferences(
-                TextEditorSettings::instance()->tabPreferences(
-                    QmlJSTools::Constants::QML_JS_SETTINGS_ID));
+    m_widget = new QmlJSCodeStylePreferencesWidget(parent);
+
+    TextEditor::TabPreferences *originalTabPreferences
+            = QmlJSToolsSettings::instance()->tabPreferences();
+    m_pageTabPreferences = new TextEditor::TabPreferences(originalTabPreferences->fallbacks(), m_widget);
+    m_pageTabPreferences->setSettings(originalTabPreferences->settings());
+    m_pageTabPreferences->setCurrentFallback(originalTabPreferences->currentFallback());
+    m_widget->setTabPreferences(m_pageTabPreferences);
 
     if (m_searchKeywords.isEmpty())
         m_searchKeywords = m_widget->searchKeywords();
@@ -175,17 +173,21 @@ QWidget *QmlJSCodeStyleSettingsPage::createPage(QWidget *parent)
 
 void QmlJSCodeStyleSettingsPage::apply()
 {
-//    if (m_widget) {
-//        const TabSettings newTabSettings = m_widget->tabSettings();
-//        TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
-//        if (newTabSettings != textEditorSettings->tabSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID)) {
-//            textEditorSettings->setTabSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID,
-//                                               newTabSettings);
-//            if (QSettings *s = Core::ICore::instance()->settings()) {
-//                newTabSettings.toSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID, s);
-//            }
-//        }
-//    }
+    if (m_widget) {
+        QSettings *s = Core::ICore::instance()->settings();
+
+        TextEditor::TabPreferences *originalTabPreferences = QmlJSToolsSettings::instance()->tabPreferences();
+        if (originalTabPreferences->settings() != m_pageTabPreferences->settings()) {
+            originalTabPreferences->setSettings(m_pageTabPreferences->settings());
+            if (s)
+                originalTabPreferences->toSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID, s);
+        }
+        if (originalTabPreferences->currentFallback() != m_pageTabPreferences->currentFallback()) {
+            originalTabPreferences->setCurrentFallback(m_pageTabPreferences->currentFallback());
+            if (s)
+                originalTabPreferences->toSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID, s);
+        }
+    }
 }
 
 bool QmlJSCodeStyleSettingsPage::matches(const QString &s) const
@@ -194,4 +196,4 @@ bool QmlJSCodeStyleSettingsPage::matches(const QString &s) const
 }
 
 } // namespace Internal
-} // namespace CppTools
+} // namespace QmlJSTools
index c7574e3..e246191 100644 (file)
@@ -21,13 +21,13 @@ namespace TextEditor {
 namespace QmlJSTools {
 namespace Internal {
 
-class QmlJSCodeStyleSettingsWidget : public QWidget
+class QmlJSCodeStylePreferencesWidget : public QWidget
 {
     Q_OBJECT
 
 public:
-    explicit QmlJSCodeStyleSettingsWidget(QWidget *parent = 0);
-    virtual ~QmlJSCodeStyleSettingsWidget();
+    explicit QmlJSCodeStylePreferencesWidget(QWidget *parent = 0);
+    virtual ~QmlJSCodeStylePreferencesWidget();
 
     void setTabPreferences(TextEditor::TabPreferences *tabPreferences);
 
@@ -36,7 +36,7 @@ public:
 private slots:
     void setFontSettings(const TextEditor::FontSettings &fontSettings);
     void setVisualizeWhitespace(bool on);
-    void slotTabSettingsChanged();
+    void slotSettingsChanged();
     void updatePreview();
 
 private:
@@ -68,7 +68,8 @@ public:
 
 private:
     QString m_searchKeywords;
-    QPointer<QmlJSCodeStyleSettingsWidget> m_widget;
+    TextEditor::TabPreferences *m_pageTabPreferences;
+    QPointer<QmlJSCodeStylePreferencesWidget> m_widget;
 };
 
 } // namespace Internal
index 607830d..2d5cad5 100644 (file)
@@ -8,6 +8,8 @@ HEADERS += \
     $$PWD/qmljstools_global.h \
     $$PWD/qmljstoolsplugin.h \
     $$PWD/qmljstoolsconstants.h \
+    $$PWD/qmljstoolssettings.h \
+    $$PWD/qmljscodestylesettingsfactory.h \
     $$PWD/qmljsmodelmanager.h \
     $$PWD/qmljsqtstylecodeformatter.h \
     $$PWD/qmljsrefactoringchanges.h \
@@ -19,6 +21,8 @@ HEADERS += \
 
 SOURCES += \
     $$PWD/qmljstoolsplugin.cpp \
+    $$PWD/qmljstoolssettings.cpp \
+    $$PWD/qmljscodestylesettingsfactory.cpp \
     $$PWD/qmljsmodelmanager.cpp \
     $$PWD/qmljsqtstylecodeformatter.cpp \
     $$PWD/qmljsrefactoringchanges.cpp \
index 322070c..5e750bc 100644 (file)
 #include "qmljslocatordata.h"
 #include "qmljscodestylesettingspage.h"
 #include "qmljstoolsconstants.h"
+#include "qmljstoolssettings.h"
+#include "qmljscodestylesettingsfactory.h"
 
 #include <texteditor/texteditorsettings.h>
 #include <texteditor/tabsettings.h>
+#include <texteditor/codestylepreferencesmanager.h>
 
 #include <extensionsystem/pluginmanager.h>
 
@@ -74,6 +77,8 @@ bool QmlJSToolsPlugin::initialize(const QStringList &arguments, QString *error)
     Q_UNUSED(error)
 //    Core::ICore *core = Core::ICore::instance();
 
+    m_settings = new QmlJSToolsSettings(this); // force registration of qmljstools settings
+
     // Objects
     m_modelManager = new ModelManager(this);
 //    Core::VCSManager *vcsManager = core->vcsManager();
@@ -87,7 +92,10 @@ bool QmlJSToolsPlugin::initialize(const QStringList &arguments, QString *error)
     LocatorData *locatorData = new LocatorData;
     addAutoReleasedObject(locatorData);
     addAutoReleasedObject(new FunctionFilter(locatorData));
-//    addAutoReleasedObject(new QmlJSCodeStyleSettingsPage);
+    addAutoReleasedObject(new QmlJSCodeStyleSettingsPage);
+
+    TextEditor::CodeStylePreferencesManager::instance()->registerFactory(
+                new QmlJSTools::QmlJSCodeStylePreferencesFactory());
 
     return true;
 }
index 3c4e879..c6019e4 100644 (file)
@@ -45,6 +45,9 @@ class QDir;
 QT_END_NAMESPACE
 
 namespace QmlJSTools {
+
+class QmlJSToolsSettings;
+
 namespace Internal {
 
 class ModelManager;
@@ -66,6 +69,7 @@ public:
 
 private:
     ModelManager *m_modelManager;
+    QmlJSToolsSettings *m_settings;
 
     static QmlJSToolsPlugin *m_instance;
 };
diff --git a/src/plugins/qmljstools/qmljstoolssettings.cpp b/src/plugins/qmljstools/qmljstoolssettings.cpp
new file mode 100644 (file)
index 0000000..503f8f0
--- /dev/null
@@ -0,0 +1,66 @@
+#include "qmljstoolssettings.h"
+#include "qmljstoolsconstants.h"
+
+#include <texteditor/texteditorsettings.h>
+#include <texteditor/tabpreferences.h>
+
+#include <utils/qtcassert.h>
+#include <coreplugin/icore.h>
+#include <QtCore/QSettings>
+
+static const char *idKey = "QmlJSGlobal";
+
+using namespace QmlJSTools;
+
+namespace QmlJSTools {
+namespace Internal {
+
+class QmlJSToolsSettingsPrivate
+{
+public:
+    TextEditor::TabPreferences *m_tabPreferences;
+};
+
+} // namespace Internal
+} // namespace QmlJSTools
+
+QmlJSToolsSettings *QmlJSToolsSettings::m_instance = 0;
+
+QmlJSToolsSettings::QmlJSToolsSettings(QObject *parent)
+    : QObject(parent)
+    , m_d(new Internal::QmlJSToolsSettingsPrivate)
+{
+    QTC_ASSERT(!m_instance, return);
+    m_instance = this;
+
+    if (const QSettings *s = Core::ICore::instance()->settings()) {
+        TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
+        m_d->m_tabPreferences
+                = new TextEditor::TabPreferences(QList<TextEditor::IFallbackPreferences *>()
+                                                 << textEditorSettings->tabPreferences(), this);
+        m_d->m_tabPreferences->setCurrentFallback(textEditorSettings->tabPreferences());
+        m_d->m_tabPreferences->fromSettings(QmlJSTools::Constants::QML_JS_SETTINGS_ID, s);
+        m_d->m_tabPreferences->setDisplayName(tr("global QML"));
+        m_d->m_tabPreferences->setId(idKey);
+        textEditorSettings->registerLanguageTabPreferences(QmlJSTools::Constants::QML_JS_SETTINGS_ID, m_d->m_tabPreferences);
+    }
+}
+
+QmlJSToolsSettings::~QmlJSToolsSettings()
+{
+    delete m_d;
+
+    m_instance = 0;
+}
+
+QmlJSToolsSettings *QmlJSToolsSettings::instance()
+{
+    return m_instance;
+}
+
+TextEditor::TabPreferences *QmlJSToolsSettings::tabPreferences() const
+{
+    return m_d->m_tabPreferences;
+}
+
+
diff --git a/src/plugins/qmljstools/qmljstoolssettings.h b/src/plugins/qmljstools/qmljstoolssettings.h
new file mode 100644 (file)
index 0000000..9eb8b65
--- /dev/null
@@ -0,0 +1,77 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** No Commercial Usage
+**
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights.  These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**************************************************************************/
+
+#ifndef QMLJSTOOLSSETTINGS_H
+#define QMLJSTOOLSSETTINGS_H
+
+#include "qmljstools_global.h"
+
+#include <QtCore/QObject>
+
+namespace TextEditor
+{
+class TabPreferences;
+}
+
+namespace QmlJSTools
+{
+
+namespace Internal
+{
+class QmlJSToolsSettingsPrivate;
+}
+
+/**
+ * This class provides a central place for cpp tools settings.
+ */
+class QMLJSTOOLS_EXPORT QmlJSToolsSettings : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit QmlJSToolsSettings(QObject *parent);
+    ~QmlJSToolsSettings();
+
+    static QmlJSToolsSettings *instance();
+
+    TextEditor::TabPreferences *tabPreferences() const;
+
+private:
+    Internal::QmlJSToolsSettingsPrivate *m_d;
+
+    static QmlJSToolsSettings *m_instance;
+};
+
+} // namespace QmlJSTools
+
+#endif // QMLJSTOOLSSETTINGS_H