From 2edcc7ef33f20e4e17f408821c67e90dada8237b Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Tue, 16 Mar 2010 16:51:45 +0100
Subject: [PATCH] Design mode: Give IEditor a 'preferred mode'.

... and switch to it in EditorManager::activateEditor.
There no longer is a need for special currentEditorChanged()
handling in the editor that use a special mode.

QmlDesigner receives an additional setting specifying the
desired edit mode. QmlJSEditor detects whether QmlDesigner
is present by checking the registered mimetypes of the
Design mode and retrieves the settings via keys.

Remove some obsolete code and clean up includes
on qmldesignerconstants.h

Reviewed-by: con
---
 src/plugins/coreplugin/basefilewizard.cpp     |  1 -
 src/plugins/coreplugin/designmode.cpp         |  8 +++++
 src/plugins/coreplugin/designmode.h           |  3 ++
 .../editormanager/editormanager.cpp           | 10 ++++--
 .../coreplugin/editormanager/ieditor.cpp      |  2 ++
 .../coreplugin/editormanager/ieditor.h        |  2 ++
 src/plugins/designer/formeditorw.cpp          |  2 --
 src/plugins/designer/formwindoweditor.cpp     |  6 ++--
 src/plugins/designer/formwindoweditor.h       |  2 ++
 src/plugins/qmldesigner/designersettings.cpp  | 20 +++++++----
 src/plugins/qmldesigner/designersettings.h    |  1 +
 .../qmldesigner/qmldesignerconstants.h        |  9 +++--
 src/plugins/qmldesigner/qmldesignerplugin.cpp | 17 ---------
 src/plugins/qmldesigner/qmldesignerplugin.h   |  1 -
 src/plugins/qmldesigner/settingspage.cpp      |  4 +++
 src/plugins/qmldesigner/settingspage.ui       | 29 ++++++++++++---
 src/plugins/qmljseditor/qmljseditor.cpp       | 35 ++++++++++++++++++-
 src/plugins/qmljseditor/qmljseditor.h         |  2 +-
 18 files changed, 113 insertions(+), 41 deletions(-)

diff --git a/src/plugins/coreplugin/basefilewizard.cpp b/src/plugins/coreplugin/basefilewizard.cpp
index 4b04d72ad72..ba40ced4b54 100644
--- a/src/plugins/coreplugin/basefilewizard.cpp
+++ b/src/plugins/coreplugin/basefilewizard.cpp
@@ -580,7 +580,6 @@ bool BaseFileWizard::postGenerateFiles(const QWizard *w, const GeneratedFiles &l
             return false;
         }
     }
-    em->ensureEditorManagerVisible();
     return true;
 }
 
diff --git a/src/plugins/coreplugin/designmode.cpp b/src/plugins/coreplugin/designmode.cpp
index ecf54a9be67..c4aff16b61c 100644
--- a/src/plugins/coreplugin/designmode.cpp
+++ b/src/plugins/coreplugin/designmode.cpp
@@ -127,6 +127,14 @@ QString DesignMode::id() const
     return QLatin1String(Constants::MODE_DESIGN);
 }
 
+QStringList DesignMode::registeredMimeTypes() const
+{
+    QStringList rc;
+    foreach(const DesignEditorInfo *i, m_editors)
+        rc += i->mimeTypes;
+    return rc;
+}
+
 void DesignMode::registerDesignWidget(QWidget *widget, const QStringList &mimeTypes, bool preferDesignMode)
 {
     int index = m_stackWidget->addWidget(widget);
diff --git a/src/plugins/coreplugin/designmode.h b/src/plugins/coreplugin/designmode.h
index c52b264a08f..4b28b6da13b 100644
--- a/src/plugins/coreplugin/designmode.h
+++ b/src/plugins/coreplugin/designmode.h
@@ -78,6 +78,9 @@ public:
     void registerDesignWidget(QWidget *widget, const QStringList &mimeTypes,
                               bool preferDesignMode = false);
     void unregisterDesignWidget(QWidget *widget);
+
+    QStringList registeredMimeTypes() const;
+
     // IContext
     QList<int> context() const;
     QWidget *widget();
diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index 11e397bf4bf..1f4089f3a13 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -928,8 +928,14 @@ Core::IEditor *EditorManager::activateEditor(Core::Internal::EditorView *view, C
 
     if (!(flags & NoActivate)) {
         setCurrentEditor(editor, (flags & IgnoreNavigationHistory));
-        if (!(flags & NoModeSwitch))
-            ensureEditorManagerVisible();
+        if (!(flags & NoModeSwitch)) {
+            const QString preferredMode = editor->preferredMode();
+            if (preferredMode.isEmpty() || preferredMode == Core::Constants::MODE_EDIT) {
+                ensureEditorManagerVisible();
+            } else {
+                ModeManager::instance()->activateMode(preferredMode);
+            }
+        }
         if (isVisible())
             editor->widget()->setFocus();
     }
diff --git a/src/plugins/coreplugin/editormanager/ieditor.cpp b/src/plugins/coreplugin/editormanager/ieditor.cpp
index 6c3ecf904d0..7ec81c4ae97 100644
--- a/src/plugins/coreplugin/editormanager/ieditor.cpp
+++ b/src/plugins/coreplugin/editormanager/ieditor.cpp
@@ -48,6 +48,8 @@
      (so /bold{not} every time the document changes, but /bold{only once}).
   \o If duplication is supported, you need to ensure that all duplicates
         return the same file().
+  \o QString preferredMode() const is the mode the editor manager should activate.
+     Some editors use a special mode (such as Design mode).
   \endlist
 
   \sa Core::EditorFactoryInterface Core::IContext
diff --git a/src/plugins/coreplugin/editormanager/ieditor.h b/src/plugins/coreplugin/editormanager/ieditor.h
index e639f400e0d..58dd9060d8f 100644
--- a/src/plugins/coreplugin/editormanager/ieditor.h
+++ b/src/plugins/coreplugin/editormanager/ieditor.h
@@ -65,6 +65,8 @@ public:
 
     virtual QWidget *toolBar() = 0;
 
+    virtual QString preferredMode() const { return QString(); }
+
 signals:
     void changed();
 };
diff --git a/src/plugins/designer/formeditorw.cpp b/src/plugins/designer/formeditorw.cpp
index 657e7b30596..5f3a39ecc0c 100644
--- a/src/plugins/designer/formeditorw.cpp
+++ b/src/plugins/designer/formeditorw.cpp
@@ -715,8 +715,6 @@ void FormEditorW::currentEditorChanged(Core::IEditor *editor)
         m_fwm->setActiveFormWindow(fw->formWindow());
         m_actionGroupEditMode->setVisible(true);
         m_modeActionSeparator->setVisible(true);
-        // Now switch to design mode.
-        m_core->modeManager()->activateMode(QLatin1String(Core::Constants::MODE_DESIGN));
     } else {
         // Deactivate Designer if a non-form is being edited
         m_actionGroupEditMode->setVisible(false);
diff --git a/src/plugins/designer/formwindoweditor.cpp b/src/plugins/designer/formwindoweditor.cpp
index 9fe8e3916d7..760c3c491e4 100644
--- a/src/plugins/designer/formwindoweditor.cpp
+++ b/src/plugins/designer/formwindoweditor.cpp
@@ -248,8 +248,10 @@ TextEditor::PlainTextEditorEditable *FormWindowEditor::textEditable()
     return &d->m_textEditable;
 }
 
-
-
+QString FormWindowEditor::preferredMode() const
+{
+    return QLatin1String(Core::Constants::MODE_DESIGN);
+}
 
 } // namespace Designer
 
diff --git a/src/plugins/designer/formwindoweditor.h b/src/plugins/designer/formwindoweditor.h
index b99317e9ce9..d85845e7d2d 100644
--- a/src/plugins/designer/formwindoweditor.h
+++ b/src/plugins/designer/formwindoweditor.h
@@ -88,6 +88,8 @@ public:
 
     virtual QWidget *toolBar();
 
+    virtual  QString preferredMode() const;
+
     // IContext
     virtual QList<int> context() const;
     virtual QWidget *widget();
diff --git a/src/plugins/qmldesigner/designersettings.cpp b/src/plugins/qmldesigner/designersettings.cpp
index b5eee4e051e..acfa55cd829 100644
--- a/src/plugins/qmldesigner/designersettings.cpp
+++ b/src/plugins/qmldesigner/designersettings.cpp
@@ -28,13 +28,12 @@
 **************************************************************************/
 
 #include "designersettings.h"
+#include "qmldesignerconstants.h"
 
 #include <QtCore/QSettings>
 
 using namespace QmlDesigner;
 
-static const char *qmlGroup = "Qml";
-static const char *qmlDesignerGroup = "Designer";
 static const char *snapToGridKey = "SnapToGrid";
 static const char *showBoundingRectanglesKey = "ShowBoundingRectangles";
 static const char *onlyShowItemsWithContentsKey = "OnlyShowItemsWithContents";
@@ -43,29 +42,34 @@ DesignerSettings::DesignerSettings()
     : snapToGrid(false)
     , showBoundingRectangles(false)
     , onlyShowItemsWithContents(false)
+    , openDesignMode(QmlDesigner::Constants::QML_OPENDESIGNMODE_DEFAULT)
 {}
 void DesignerSettings::fromSettings(QSettings *settings)
 {
-    settings->beginGroup(QLatin1String(qmlGroup));
-    settings->beginGroup(QLatin1String(qmlDesignerGroup));
+    settings->beginGroup(QLatin1String(QmlDesigner::Constants::QML_SETTINGS_GROUP));
+    settings->beginGroup(QLatin1String(QmlDesigner::Constants::QML_DESIGNER_SETTINGS_GROUP));
     snapToGrid = settings->value(QLatin1String(snapToGridKey), false).toBool();
     showBoundingRectangles = settings->value(
             QLatin1String(showBoundingRectanglesKey), false).toBool();
     onlyShowItemsWithContents = settings->value(
             QLatin1String(onlyShowItemsWithContentsKey), false).toBool();
+    openDesignMode = settings->value(
+            QLatin1String(QmlDesigner::Constants::QML_OPENDESIGNMODE_SETTINGS_KEY),
+            bool(QmlDesigner::Constants::QML_OPENDESIGNMODE_DEFAULT)).toBool();
     settings->endGroup();
     settings->endGroup();
 }
 
 void DesignerSettings::toSettings(QSettings *settings) const
 {
-    settings->beginGroup(QLatin1String(qmlGroup));
-    settings->beginGroup(QLatin1String(qmlDesignerGroup));
+    settings->beginGroup(QLatin1String(QmlDesigner::Constants::QML_SETTINGS_GROUP));
+    settings->beginGroup(QLatin1String(QmlDesigner::Constants::QML_DESIGNER_SETTINGS_GROUP));
     settings->setValue(QLatin1String(snapToGridKey), snapToGrid);
     settings->setValue(QLatin1String(showBoundingRectanglesKey),
                        showBoundingRectangles);
     settings->setValue(QLatin1String(onlyShowItemsWithContentsKey),
                        onlyShowItemsWithContents);
+    settings->setValue(QLatin1String(QmlDesigner::Constants::QML_OPENDESIGNMODE_SETTINGS_KEY), openDesignMode);
     settings->endGroup();
     settings->endGroup();
 }
@@ -73,5 +77,7 @@ void DesignerSettings::toSettings(QSettings *settings) const
 bool DesignerSettings::equals(const DesignerSettings &other) const
 {
     return snapToGrid == other.snapToGrid
-            && showBoundingRectangles == other.showBoundingRectangles;
+            && showBoundingRectangles == other.showBoundingRectangles
+            && onlyShowItemsWithContents == other.onlyShowItemsWithContents
+            && openDesignMode == other.openDesignMode;
 }
diff --git a/src/plugins/qmldesigner/designersettings.h b/src/plugins/qmldesigner/designersettings.h
index 50d88015782..af23234a2fc 100644
--- a/src/plugins/qmldesigner/designersettings.h
+++ b/src/plugins/qmldesigner/designersettings.h
@@ -50,6 +50,7 @@ struct DesignerSettings {
     bool snapToGrid;
     bool showBoundingRectangles;
     bool onlyShowItemsWithContents;
+    bool openDesignMode;
 };
 
 inline bool operator==(const DesignerSettings &s1, const DesignerSettings &s2)
diff --git a/src/plugins/qmldesigner/qmldesignerconstants.h b/src/plugins/qmldesigner/qmldesignerconstants.h
index 1e75f5625c9..13ce485e027 100644
--- a/src/plugins/qmldesigner/qmldesignerconstants.h
+++ b/src/plugins/qmldesigner/qmldesignerconstants.h
@@ -30,8 +30,6 @@
 #ifndef QMLDESIGNERPLUGIN_CONSTANTS_H
 #define QMLDESIGNERPLUGIN_CONSTANTS_H
 
-#include <coreplugin/coreconstants.h>
-
 namespace QmlDesigner {
 namespace Constants {
 
@@ -46,11 +44,16 @@ const char * const SWITCH_TEXT_DESIGN   = "QmlDesigner.SwitchTextDesign";
 
 // mode
 const char * const DESIGN_MODE_NAME     = "Design";
-const int DESIGN_MODE_PRIORITY          = Core::Constants::P_MODE_EDIT - 1;
 
 // Wizard type
 const char * const FORM_MIMETYPE        = "application/x-qmldesigner";
 
+// This setting is also accessed by the QMlJsEditor.
+const char * const QML_SETTINGS_GROUP = "Qml";
+const char * const QML_DESIGNER_SETTINGS_GROUP = "Designer";
+const char * const QML_OPENDESIGNMODE_SETTINGS_KEY = "OpenDesignMode";
+enum { QML_OPENDESIGNMODE_DEFAULT = 1 };
+
 namespace Internal {
     enum { debug = 0 };
 }
diff --git a/src/plugins/qmldesigner/qmldesignerplugin.cpp b/src/plugins/qmldesigner/qmldesignerplugin.cpp
index 07aff5d0e37..8c639bd5778 100644
--- a/src/plugins/qmldesigner/qmldesignerplugin.cpp
+++ b/src/plugins/qmldesigner/qmldesignerplugin.cpp
@@ -204,7 +204,6 @@ void BauhausPlugin::createDesignModeWidget()
                                             Core::Constants::PASTE, m_context->context());
     command->setDefaultKeySequence(QKeySequence::Paste);
     editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);
-    Core::ModeManager *modeManager = creatorCore->modeManager();
 
     command = actionManager->registerAction(m_mainWidget->selectAllAction(),
                                             Core::Constants::SELECTALL, m_context->context());
@@ -222,9 +221,6 @@ void BauhausPlugin::createDesignModeWidget()
     connect(m_editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)),
             this, SLOT(updateEditor(Core::IEditor*)));
 
-    connect(modeManager, SIGNAL(currentModeChanged(Core::IMode*)),
-            this, SLOT(modeChanged(Core::IMode*)));
-
     connect(m_editorManager, SIGNAL(editorsClosed(QList<Core::IEditor*>)),
             this, SLOT(textEditorsClosed(QList<Core::IEditor*>)));
 
@@ -240,19 +236,6 @@ void BauhausPlugin::updateEditor(Core::IEditor *editor)
     }
 }
 
-void BauhausPlugin::modeChanged(Core::IMode *mode)
-{
-    if (mode == m_designMode) {
-        m_isActive = true;
-        m_mainWidget->showEditor(m_editorManager->currentEditor());
-    } else {
-        if (m_isActive) {
-            m_isActive = false;
-            m_mainWidget->showEditor(0);
-        }
-    }
-}
-
 void BauhausPlugin::textEditorsClosed(QList<Core::IEditor*> editors)
 {
     m_mainWidget->closeEditors(editors);
diff --git a/src/plugins/qmldesigner/qmldesignerplugin.h b/src/plugins/qmldesigner/qmldesignerplugin.h
index e01df161e8f..7a170875b35 100644
--- a/src/plugins/qmldesigner/qmldesignerplugin.h
+++ b/src/plugins/qmldesigner/qmldesignerplugin.h
@@ -81,7 +81,6 @@ public:
 private slots:
 
     void switchTextDesign();
-    void modeChanged(Core::IMode *mode);
     void textEditorsClosed(QList<Core::IEditor *> editors);
     void updateActions(Core::IEditor* editor);
     void updateEditor(Core::IEditor *editor);
diff --git a/src/plugins/qmldesigner/settingspage.cpp b/src/plugins/qmldesigner/settingspage.cpp
index 8f074dd7acc..778848e3de0 100644
--- a/src/plugins/qmldesigner/settingspage.cpp
+++ b/src/plugins/qmldesigner/settingspage.cpp
@@ -38,6 +38,8 @@
 using namespace QmlDesigner;
 using namespace QmlDesigner::Internal;
 
+enum EditModeCombo { EditModeDesign, EditModeEdit };
+
 SettingsPageWidget::SettingsPageWidget(QWidget *parent) :
     QWidget(parent)
 {
@@ -51,6 +53,7 @@ DesignerSettings SettingsPageWidget::settings() const
     ds.showBoundingRectangles = m_ui.showBoundingRectanglesCheckbox->isChecked();
     ds.onlyShowItemsWithContents =
             m_ui.onlyShowItemsWithContentsCheckBox->isChecked();
+    ds.openDesignMode = m_ui.editorModeComboBox->currentIndex() == EditModeDesign;
     return ds;
 }
 
@@ -59,6 +62,7 @@ void SettingsPageWidget::setSettings(const DesignerSettings &s)
     m_ui.snapToGridCheckbox->setChecked(s.snapToGrid);
     m_ui.showBoundingRectanglesCheckbox->setChecked(s.showBoundingRectangles);
     m_ui.onlyShowItemsWithContentsCheckBox->setChecked(s.onlyShowItemsWithContents);
+    m_ui.editorModeComboBox->setCurrentIndex(s.openDesignMode ? EditModeDesign : EditModeEdit);
 }
 
 QString SettingsPageWidget::searchKeywords() const
diff --git a/src/plugins/qmldesigner/settingspage.ui b/src/plugins/qmldesigner/settingspage.ui
index 09244658e4d..5afaa800563 100644
--- a/src/plugins/qmldesigner/settingspage.ui
+++ b/src/plugins/qmldesigner/settingspage.ui
@@ -19,28 +19,49 @@
      <property name="title">
       <string>GroupBox</string>
      </property>
-     <layout class="QVBoxLayout" name="verticalLayout">
-      <item>
+     <layout class="QFormLayout" name="formLayout">
+      <item row="0" column="0" colspan="2">
        <widget class="QCheckBox" name="snapToGridCheckbox">
         <property name="text">
          <string>&amp;Snap to Grid</string>
         </property>
        </widget>
       </item>
-      <item>
+      <item row="1" column="0" colspan="2">
        <widget class="QCheckBox" name="showBoundingRectanglesCheckbox">
         <property name="text">
          <string>Show Bounding Rectangles</string>
         </property>
        </widget>
       </item>
-      <item>
+      <item row="2" column="0" colspan="2">
        <widget class="QCheckBox" name="onlyShowItemsWithContentsCheckBox">
         <property name="text">
          <string>Only Show Items with Contents</string>
         </property>
        </widget>
       </item>
+      <item row="3" column="0">
+       <widget class="QLabel" name="editorModeLabel">
+        <property name="text">
+         <string>Open editor in:</string>
+        </property>
+       </widget>
+      </item>
+      <item row="3" column="1">
+       <widget class="QComboBox" name="editorModeComboBox">
+        <item>
+         <property name="text">
+          <string>Design mode</string>
+         </property>
+        </item>
+        <item>
+         <property name="text">
+          <string>Edit mode</string>
+         </property>
+        </item>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp
index 6bcd17c66c0..0822dd39ebf 100644
--- a/src/plugins/qmljseditor/qmljseditor.cpp
+++ b/src/plugins/qmljseditor/qmljseditor.cpp
@@ -45,6 +45,9 @@
 
 #include <coreplugin/actionmanager/actionmanager.h>
 #include <coreplugin/icore.h>
+#include <coreplugin/coreconstants.h>
+#include <coreplugin/modemanager.h>
+#include <coreplugin/designmode.h>
 #include <coreplugin/mimedatabase.h>
 #include <coreplugin/uniqueidmanager.h>
 #include <extensionsystem/pluginmanager.h>
@@ -53,7 +56,7 @@
 #include <texteditor/textblockiterator.h>
 #include <texteditor/texteditorconstants.h>
 #include <texteditor/texteditorsettings.h>
-
+#include <qmldesigner/qmldesignerconstants.h>
 #include <utils/changeset.h>
 #include <utils/uncommentselection.h>
 
@@ -574,6 +577,36 @@ QmlJSEditorEditable::QmlJSEditorEditable(QmlJSTextEditor *editor)
     m_context << uidm->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
 }
 
+// Use preferred mode from Bauhaus settings
+static bool openInDesignMode()
+{
+    static bool bauhausDetected = false;
+    static bool bauhausPresent = false;
+    // Check if Bauhaus is loaded, that is, a Design mode widget is
+    // registered for the QML mime type.
+    if (!bauhausDetected) {
+        if (const Core::IMode *dm = Core::ModeManager::instance()->mode(QLatin1String(Core::Constants::MODE_DESIGN)))
+            if (const Core::DesignMode *designMode = qobject_cast<const Core::DesignMode *>(dm))
+                bauhausPresent = designMode->registeredMimeTypes().contains(QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE));
+        bauhausDetected =  true;
+    }
+    if (!bauhausPresent)
+        return false;
+    // Query the bauhaus setting if it wants to be opened in Design mode.
+    const QString settingsKey = QLatin1String(QmlDesigner::Constants::QML_SETTINGS_GROUP)
+                                + QLatin1Char('/') + QLatin1String(QmlDesigner::Constants::QML_DESIGNER_SETTINGS_GROUP)
+                                + QLatin1Char('/') + QLatin1String(QmlDesigner::Constants::QML_OPENDESIGNMODE_SETTINGS_KEY);
+    const QVariant openDesignMode = Core::ICore::instance()->settings()->value(settingsKey);
+    return openDesignMode.isValid() ? openDesignMode.toBool() : bool(QmlDesigner::Constants::QML_OPENDESIGNMODE_DEFAULT);
+}
+
+QString QmlJSEditorEditable::preferredMode() const
+{
+    if (openInDesignMode())
+        return QLatin1String(Core::Constants::MODE_DESIGN);
+    return QString();
+}
+
 QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
     TextEditor::BaseTextEditor(parent),
     m_methodCombo(0),
diff --git a/src/plugins/qmljseditor/qmljseditor.h b/src/plugins/qmljseditor/qmljseditor.h
index 7e361a079d7..46019116d12 100644
--- a/src/plugins/qmljseditor/qmljseditor.h
+++ b/src/plugins/qmljseditor/qmljseditor.h
@@ -69,12 +69,12 @@ public:
     QString id() const;
     bool isTemporary() const { return false; }
     virtual bool open(const QString & fileName);
+    virtual QString preferredMode() const;
 
 private:
     QList<int> m_context;
 };
 
-
 struct Declaration
 {
     QString text;
-- 
GitLab