From a7227782db27c9b68cb1768d3f2a4ce4237c7ee5 Mon Sep 17 00:00:00 2001
From: dt <qtc-commiter@nokia.com>
Date: Thu, 11 Dec 2008 14:35:14 +0100
Subject: [PATCH] Fixes:    Saving and restoring of ProjectTreeWidget states

Task:     Reported on the ML multiple times.
Details:  Add a few methods to make it possible to save and restore
settings. Save those settings to the .ini file. (I'm not sure whether
seesion would be better, let's see what feedback we get.)
---
 .../coreplugin/inavigationwidgetfactory.cpp   | 10 ++++
 .../coreplugin/inavigationwidgetfactory.h     |  6 ++
 src/plugins/coreplugin/mainwindow.cpp         |  2 +-
 src/plugins/coreplugin/navigationwidget.cpp   | 20 ++++++-
 src/plugins/coreplugin/navigationwidget.h     |  5 +-
 src/plugins/projectexplorer/projectmodels.cpp | 10 ++++
 src/plugins/projectexplorer/projectmodels.h   |  3 +
 .../projectexplorer/projecttreewidget.cpp     | 55 +++++++++++++++----
 .../projectexplorer/projecttreewidget.h       |  6 ++
 9 files changed, 104 insertions(+), 13 deletions(-)

diff --git a/src/plugins/coreplugin/inavigationwidgetfactory.cpp b/src/plugins/coreplugin/inavigationwidgetfactory.cpp
index 7a043e4c9cc..2de8e72fbe6 100644
--- a/src/plugins/coreplugin/inavigationwidgetfactory.cpp
+++ b/src/plugins/coreplugin/inavigationwidgetfactory.cpp
@@ -48,3 +48,13 @@ QKeySequence INavigationWidgetFactory::activationSequence()
     return QKeySequence();
 }
 
+
+void INavigationWidgetFactory::saveSettings(int position, QWidget *widget)
+{
+
+}
+
+void INavigationWidgetFactory::restoreSettings(int position, QWidget *widget)
+{
+
+}
diff --git a/src/plugins/coreplugin/inavigationwidgetfactory.h b/src/plugins/coreplugin/inavigationwidgetfactory.h
index d4b3a84d3ad..e858d2024c1 100644
--- a/src/plugins/coreplugin/inavigationwidgetfactory.h
+++ b/src/plugins/coreplugin/inavigationwidgetfactory.h
@@ -65,6 +65,12 @@ public:
     // and the docktoolbar widgets
     // Similar to how IView
     virtual NavigationView createWidget() = 0;
+
+    // Read and store settings for the widget, created by this factory
+    // and beeing at position position. (The position is important since
+    // a certain type of widget could exist multiple times.)
+    virtual void saveSettings(int position, QWidget *widget);
+    virtual void restoreSettings(int position, QWidget *widget);
 };
 
 } // namespace Core
diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp
index 228c58a94ac..651e1d73830 100644
--- a/src/plugins/coreplugin/mainwindow.cpp
+++ b/src/plugins/coreplugin/mainwindow.cpp
@@ -997,7 +997,7 @@ void MainWindow::readSettings()
 
     m_settings->endGroup();
     m_editorManager->readSettings(m_settings);
-    m_navigationWidget->readSettings(m_settings);
+    m_navigationWidget->restoreSettings(m_settings);
     m_rightPaneWidget->readSettings(m_settings);
 }
 
diff --git a/src/plugins/coreplugin/navigationwidget.cpp b/src/plugins/coreplugin/navigationwidget.cpp
index 82ed13a29e8..bf78bf94f21 100644
--- a/src/plugins/coreplugin/navigationwidget.cpp
+++ b/src/plugins/coreplugin/navigationwidget.cpp
@@ -220,6 +220,8 @@ void NavigationWidget::close()
     }
 }
 
+
+
 void NavigationWidget::saveSettings(QSettings *settings)
 {
     QStringList views;
@@ -230,9 +232,12 @@ void NavigationWidget::saveSettings(QSettings *settings)
     settings->setValue("Navigation/Visible", isShown());
     settings->setValue("Navigation/VerticalPosition", saveState());
     settings->setValue("Navigation/Width", m_width);
+
+    for (int i=0; i<m_subWidgets.count(); ++i)
+        m_subWidgets.at(i)->saveSettings(i);
 }
 
-void NavigationWidget::readSettings(QSettings *settings)
+void NavigationWidget::restoreSettings(QSettings *settings)
 {
     if (settings->contains("Navigation/Views")) {
         QStringList views = settings->value("Navigation/Views").toStringList();
@@ -266,6 +271,9 @@ void NavigationWidget::readSettings(QSettings *settings)
     if (NavigationWidgetPlaceHolder::m_current) {
         NavigationWidgetPlaceHolder::m_current->applyStoredSize(m_width);
     }
+
+    for (int i=0; i<m_subWidgets.count(); ++i)
+        m_subWidgets.at(i)->restoreSettings(i);
 }
 
 void NavigationWidget::setShown(bool b)
@@ -465,6 +473,16 @@ INavigationWidgetFactory *NavigationSubWidget::factory()
     return m_navigationComboBox->itemData(index).value<INavigationWidgetFactory *>();
 }
 
+void NavigationSubWidget::saveSettings(int position)
+{
+    factory()->saveSettings(position, m_navigationWidget);
+}
+
+void NavigationSubWidget::restoreSettings(int position)
+{
+    factory()->restoreSettings(position, m_navigationWidget);
+}
+
 Core::ICommand *NavigationSubWidget::command(const QString &title) const
 {
     const QHash<QString, Core::ICommand*> commandMap = m_parentWidget->commandMap();
diff --git a/src/plugins/coreplugin/navigationwidget.h b/src/plugins/coreplugin/navigationwidget.h
index 96a64f4ac3a..7ab27e28ddd 100644
--- a/src/plugins/coreplugin/navigationwidget.h
+++ b/src/plugins/coreplugin/navigationwidget.h
@@ -84,7 +84,7 @@ public:
     ~NavigationWidget();
 
     void saveSettings(QSettings *settings);
-    void readSettings(QSettings *settings);
+    void restoreSettings(QSettings *settings);
 
     bool isShown() const;
     void setShown(bool b);
@@ -133,6 +133,9 @@ public:
     void setFactory(const QString &name);
     void setFocusWidget();
 
+    void saveSettings(int position);
+    void restoreSettings(int position);
+
     Core::ICommand *command(const QString &title) const;
 
 signals:
diff --git a/src/plugins/projectexplorer/projectmodels.cpp b/src/plugins/projectexplorer/projectmodels.cpp
index de4f851e5ce..823244578ed 100644
--- a/src/plugins/projectexplorer/projectmodels.cpp
+++ b/src/plugins/projectexplorer/projectmodels.cpp
@@ -926,6 +926,16 @@ void FlatModel::setGeneratedFilesFilterEnabled(bool filter)
     reset();
 }
 
+bool FlatModel::projectFilterEnabled()
+{
+    return m_filterProjects;
+}
+
+bool FlatModel::generatedFilesFilterEnabled()
+{
+    return m_filterGeneratedFiles;
+}
+
 Node *FlatModel::nodeForIndex(const QModelIndex &index) const
 {
     if (index.isValid())
diff --git a/src/plugins/projectexplorer/projectmodels.h b/src/plugins/projectexplorer/projectmodels.h
index cb7376f932c..35c575fe94b 100644
--- a/src/plugins/projectexplorer/projectmodels.h
+++ b/src/plugins/projectexplorer/projectmodels.h
@@ -136,6 +136,9 @@ public:
     ProjectExplorer::Node *nodeForIndex(const QModelIndex &index) const;
     QModelIndex indexForNode(const Node *node);
 
+    bool projectFilterEnabled();
+    bool generatedFilesFilterEnabled();
+
 public slots:
     void setProjectFilterEnabled(bool filter);
     void setGeneratedFilesFilterEnabled(bool filter);
diff --git a/src/plugins/projectexplorer/projecttreewidget.cpp b/src/plugins/projectexplorer/projecttreewidget.cpp
index c2197a2dc7c..724ab9d5796 100644
--- a/src/plugins/projectexplorer/projecttreewidget.cpp
+++ b/src/plugins/projectexplorer/projecttreewidget.cpp
@@ -42,6 +42,7 @@
 #include <utils/qtcassert.h>
 
 #include <QtCore/QDebug>
+#include <QtCore/QSettings>
 
 #include <QtGui/QHeaderView>
 #include <QtGui/QVBoxLayout>
@@ -156,7 +157,20 @@ ProjectTreeWidget::ProjectTreeWidget(Core::ICore *core, QWidget *parent)
     connect(m_explorer->session(), SIGNAL(startupProjectChanged(ProjectExplorer::Project *)),
             this, SLOT(startupProjectChanged(ProjectExplorer::Project *)));
 
-    setAutoSynchronization(true);
+    m_toggleSync = new QToolButton;
+    m_toggleSync->setProperty("type", "dockbutton");
+    m_toggleSync->setIcon(QIcon(":/qworkbench/images/linkicon.png"));
+    m_toggleSync->setCheckable(true);
+    m_toggleSync->setChecked(autoSynchronization());
+    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
+    connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
+
+    //setAutoSynchronization(true);
+}
+
+QToolButton *ProjectTreeWidget::toggleSync()
+{
+    return m_toggleSync;
 }
 
 void ProjectTreeWidget::toggleAutoSynchronization()
@@ -171,6 +185,7 @@ bool ProjectTreeWidget::autoSynchronization() const
 
 void ProjectTreeWidget::setAutoSynchronization(bool sync, bool syncNow)
 {
+    m_toggleSync->setChecked(sync);
     if (sync == m_autoSync)
         return;
 
@@ -296,6 +311,17 @@ void ProjectTreeWidget::setGeneratedFilesFilter(bool filter)
     m_filterGeneratedFilesAction->setChecked(filter);
 }
 
+bool ProjectTreeWidget::generatedFilesFilter()
+{
+    return m_model->generatedFilesFilterEnabled();
+}
+
+bool ProjectTreeWidget::projectFilter()
+{
+    return m_model->projectFilterEnabled();
+}
+
+
 ProjectTreeWidgetFactory::ProjectTreeWidgetFactory(Core::ICore *core)
     : m_core(core)
 {
@@ -331,15 +357,24 @@ Core::NavigationView ProjectTreeWidgetFactory::createWidget()
     filterMenu->addAction(ptw->m_filterGeneratedFilesAction);
     filter->setMenu(filterMenu);
 
-    QToolButton *toggleSync = new QToolButton;
-    toggleSync->setProperty("type", "dockbutton");
-    toggleSync->setIcon(QIcon(":/qworkbench/images/linkicon.png"));
-    toggleSync->setCheckable(true);
-    toggleSync->setChecked(ptw->autoSynchronization());
-    toggleSync->setToolTip(tr("Synchronize with Editor"));
-    connect(toggleSync, SIGNAL(clicked(bool)), ptw, SLOT(toggleAutoSynchronization()));
-
-    n.doockToolBarWidgets << filter << toggleSync;
+    n.doockToolBarWidgets << filter << ptw->toggleSync();
     return n;
 }
 
+void ProjectTreeWidgetFactory::saveSettings(int position, QWidget *widget)
+{
+    ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
+    Q_ASSERT(ptw);
+    m_core->settings()->setValue("ProjectTreeWidget."+QString::number(position)+".ProjectFilter", ptw->projectFilter());
+    m_core->settings()->setValue("ProjectTreeWidget."+QString::number(position)+".GeneratedFilter", ptw->generatedFilesFilter());
+    m_core->settings()->setValue("ProjectTreeWidget."+QString::number(position)+".SyncWithEditor", ptw->autoSynchronization());
+}
+
+void ProjectTreeWidgetFactory::restoreSettings(int position, QWidget *widget)
+{
+    ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
+    Q_ASSERT(ptw);
+    ptw->setProjectFilter(m_core->settings()->value("ProjectTreeWidget."+QString::number(position)+".ProjectFilter", false).toBool());
+    ptw->setGeneratedFilesFilter(m_core->settings()->value("ProjectTreeWidget."+QString::number(position)+".GeneratedFilter", true).toBool());
+    ptw->setAutoSynchronization(m_core->settings()->value("ProjectTreeWidget."+QString::number(position)+".SyncWithEditor", true).toBool());
+}
diff --git a/src/plugins/projectexplorer/projecttreewidget.h b/src/plugins/projectexplorer/projecttreewidget.h
index dcb26c2d9aa..85dc9801abd 100644
--- a/src/plugins/projectexplorer/projecttreewidget.h
+++ b/src/plugins/projectexplorer/projecttreewidget.h
@@ -60,6 +60,9 @@ public:
 
     bool autoSynchronization() const;
     void setAutoSynchronization(bool sync, bool syncNow = true);
+    bool projectFilter();
+    bool generatedFilesFilter();
+    QToolButton *toggleSync();
 
 public slots:
     void toggleAutoSynchronization();
@@ -84,6 +87,7 @@ private:
     FlatModel *m_model;
     QAction *m_filterProjectsAction;
     QAction *m_filterGeneratedFilesAction;
+    QToolButton *m_toggleSync;
 
     QModelIndex m_subIndex;
     QString m_modelId;
@@ -100,6 +104,8 @@ public:
     virtual QString displayName();
     virtual QKeySequence activationSequence();
     virtual Core::NavigationView createWidget();
+    void restoreSettings(int position, QWidget *widget);
+    void saveSettings(int position, QWidget *widget);
 private:
     Core::ICore *m_core;
 };
-- 
GitLab