diff libgui/src/workspace-view.cc @ 16445:3f8d3fc907af

store workspace model in main_window, not in workspace view * main-window.h, main-window.cc (main_window::_workspace_model): New data member. (main_window::construct): Create _workspace_model and make it the model for _workspace_view. connect _workspace_model::model_changed to _workspace_view::model_changed. (main_window::~main_window): Delete _workspace_model. * workspace-view.h, workspace-view.cc (workspace_view::_workspace_model): Delete. Use model() method to get model where needed. (workspace_view::setModel): New function. (workspace_view::view): Rename from _workspace_tree_view. Change all uses. (workspace_view::workspace_view): Don't connect _workspace_model::model_changed signal to workspace_view::model_changed here.
author John W. Eaton <jwe@octave.org>
date Sat, 06 Apr 2013 14:23:52 -0400
parents 6cd66a5a76e8
children cbc39a3d0c42
line wrap: on
line diff
--- a/libgui/src/workspace-view.cc	Sat Apr 06 14:01:58 2013 -0400
+++ b/libgui/src/workspace-view.cc	Sat Apr 06 14:23:52 2013 -0400
@@ -38,23 +38,19 @@
   setWindowIcon (QIcon(":/actions/icons/logo.png"));
   setWindowTitle (tr ("Workspace"));
 
-  // Create a new workspace model.
-  _workspace_model = new workspace_model ();
-
-  _workspace_tree_view = new QTreeView (this);            // Create a new tree view.
-  _workspace_tree_view->setHeaderHidden (false);          // Do not show header columns.
-  _workspace_tree_view->setAlternatingRowColors (true);   // Activate alternating row colors.
-  _workspace_tree_view->setAnimated (false);              // Deactivate animations because of strange glitches.
-  _workspace_tree_view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells.
-  _workspace_tree_view->setWordWrap (false);              // No wordwrapping in cells.
-  _workspace_tree_view->setModel (_workspace_model);      // Assign model.
+  view = new QTreeView (this);            // Create a new tree view.
+  view->setHeaderHidden (false);          // Do not show header columns.
+  view->setAlternatingRowColors (true);   // Activate alternating row colors.
+  view->setAnimated (false);              // Deactivate animations because of strange glitches.
+  view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells.
+  view->setWordWrap (false);              // No wordwrapping in cells.
 
   // Set an empty widget, so we can assign a layout to it.
   setWidget (new QWidget (this));
 
   // Create a new layout and add widgets to it.
   QVBoxLayout *vbox_layout = new QVBoxLayout ();
-  vbox_layout->addWidget (_workspace_tree_view);
+  vbox_layout->addWidget (view);
   vbox_layout->setMargin (2);
 
   // Set the empty widget to have our layout.
@@ -72,21 +68,18 @@
 
   // Initialize column order and width of the workspace
   
-  _workspace_tree_view->header ()->restoreState (settings->value("workspaceview/column_state").toByteArray ());
+  view->header ()->restoreState (settings->value("workspaceview/column_state").toByteArray ());
 
   // Connect signals and slots.
   connect (this, SIGNAL (visibilityChanged (bool)),
            this, SLOT(handle_visibility_changed (bool)));
 
-  connect (_workspace_model, SIGNAL (model_changed ()),
-           this, SLOT (model_changed ()));
-
-  connect (_workspace_tree_view, SIGNAL (collapsed (QModelIndex)),
+  connect (view, SIGNAL (collapsed (QModelIndex)),
            this, SLOT (collapse_requested (QModelIndex)));
-  connect (_workspace_tree_view, SIGNAL (expanded (QModelIndex)),
+  connect (view, SIGNAL (expanded (QModelIndex)),
            this, SLOT (expand_requested (QModelIndex)));
 
-  connect (_workspace_tree_view, SIGNAL (doubleClicked (QModelIndex)),
+  connect (view, SIGNAL (doubleClicked (QModelIndex)),
            this, SLOT (item_double_clicked (QModelIndex)));
 
   // topLevelChanged is emitted when floating property changes (floating = true)
@@ -100,7 +93,7 @@
   settings->setValue("workspaceview/local_collapsed", _explicit_collapse.local);
   settings->setValue("workspaceview/global_collapsed", _explicit_collapse.global);
   settings->setValue("workspaceview/persistent_collapsed", _explicit_collapse.persistent);
-  settings->setValue("workspaceview/column_state", _workspace_tree_view->header ()->saveState ());
+  settings->setValue("workspaceview/column_state", view->header ()->saveState ());
   settings->sync ();
 }
 
@@ -114,7 +107,9 @@
 void
 workspace_view::model_changed ()
 {
-  _workspace_model->update_workspace_callback ();
+  QAbstractItemModel *m = view->model ();
+
+  dynamic_cast<workspace_model *> (m)->update_workspace_callback ();
 
   // This code is very quirky and requires some explanation.
   // Usually, we should not deal with collapsing or expanding ourselves,
@@ -130,26 +125,26 @@
   // In order to make collapsing/expanding work again, we need to set
   // flags ourselves here.
 
-  QModelIndex local_model_index = _workspace_model->index (0, 0);
-  QModelIndex global_model_index = _workspace_model->index (1, 0);
-  QModelIndex persistent_model_index = _workspace_model->index (2, 0);
+  QModelIndex local_model_index = m->index (0, 0);
+  QModelIndex global_model_index = m->index (1, 0);
+  QModelIndex persistent_model_index = m->index (2, 0);
 
   if (_explicit_collapse.local) {
-    _workspace_tree_view->collapse (local_model_index);
+    view->collapse (local_model_index);
   } else {
-    _workspace_tree_view->expand (local_model_index);
+    view->expand (local_model_index);
   }
 
   if (_explicit_collapse.global) {
-    _workspace_tree_view->collapse (global_model_index);
+    view->collapse (global_model_index);
   } else {
-    _workspace_tree_view->expand (global_model_index);
+    view->expand (global_model_index);
   }
 
   if (_explicit_collapse.persistent) {
-    _workspace_tree_view->collapse (persistent_model_index);
+    view->collapse (persistent_model_index);
   } else {
-    _workspace_tree_view->expand (persistent_model_index);
+    view->expand (persistent_model_index);
   }
 }
 
@@ -169,8 +164,9 @@
   //
   // In order to make collapsing/expanding work again, we need to set
   // flags ourselves here.
-  QMap<int, QVariant> item_data
-    = _workspace_model->itemData (index);
+  QAbstractItemModel *m = view->model ();
+
+  QMap<int, QVariant> item_data = m->itemData (index);
 
   if (item_data[0] == "Local")
     _explicit_collapse.local = true;
@@ -196,8 +192,9 @@
   //
   // In order to make collapsing/expanding work again, we need to do set
   // flags ourselves here.
-  QMap<int, QVariant> item_data
-    = _workspace_model->itemData (index);
+  QAbstractItemModel *m = view->model ();
+
+  QMap<int, QVariant> item_data = m->itemData (index);
 
   if (item_data[0] == "Local")
     _explicit_collapse.local = false;