comparison 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
comparison
equal deleted inserted replaced
16444:6cd66a5a76e8 16445:3f8d3fc907af
36 { 36 {
37 setObjectName ("WorkspaceView"); 37 setObjectName ("WorkspaceView");
38 setWindowIcon (QIcon(":/actions/icons/logo.png")); 38 setWindowIcon (QIcon(":/actions/icons/logo.png"));
39 setWindowTitle (tr ("Workspace")); 39 setWindowTitle (tr ("Workspace"));
40 40
41 // Create a new workspace model. 41 view = new QTreeView (this); // Create a new tree view.
42 _workspace_model = new workspace_model (); 42 view->setHeaderHidden (false); // Do not show header columns.
43 43 view->setAlternatingRowColors (true); // Activate alternating row colors.
44 _workspace_tree_view = new QTreeView (this); // Create a new tree view. 44 view->setAnimated (false); // Deactivate animations because of strange glitches.
45 _workspace_tree_view->setHeaderHidden (false); // Do not show header columns. 45 view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells.
46 _workspace_tree_view->setAlternatingRowColors (true); // Activate alternating row colors. 46 view->setWordWrap (false); // No wordwrapping in cells.
47 _workspace_tree_view->setAnimated (false); // Deactivate animations because of strange glitches.
48 _workspace_tree_view->setTextElideMode (Qt::ElideRight);// Elide text to the right side of the cells.
49 _workspace_tree_view->setWordWrap (false); // No wordwrapping in cells.
50 _workspace_tree_view->setModel (_workspace_model); // Assign model.
51 47
52 // Set an empty widget, so we can assign a layout to it. 48 // Set an empty widget, so we can assign a layout to it.
53 setWidget (new QWidget (this)); 49 setWidget (new QWidget (this));
54 50
55 // Create a new layout and add widgets to it. 51 // Create a new layout and add widgets to it.
56 QVBoxLayout *vbox_layout = new QVBoxLayout (); 52 QVBoxLayout *vbox_layout = new QVBoxLayout ();
57 vbox_layout->addWidget (_workspace_tree_view); 53 vbox_layout->addWidget (view);
58 vbox_layout->setMargin (2); 54 vbox_layout->setMargin (2);
59 55
60 // Set the empty widget to have our layout. 56 // Set the empty widget to have our layout.
61 widget ()->setLayout (vbox_layout); 57 widget ()->setLayout (vbox_layout);
62 58
70 _explicit_collapse.global = settings->value ("workspaceview/global_collapsed", false).toBool ();; 66 _explicit_collapse.global = settings->value ("workspaceview/global_collapsed", false).toBool ();;
71 _explicit_collapse.persistent = settings->value ("workspaceview/persistent_collapsed", false).toBool ();; 67 _explicit_collapse.persistent = settings->value ("workspaceview/persistent_collapsed", false).toBool ();;
72 68
73 // Initialize column order and width of the workspace 69 // Initialize column order and width of the workspace
74 70
75 _workspace_tree_view->header ()->restoreState (settings->value("workspaceview/column_state").toByteArray ()); 71 view->header ()->restoreState (settings->value("workspaceview/column_state").toByteArray ());
76 72
77 // Connect signals and slots. 73 // Connect signals and slots.
78 connect (this, SIGNAL (visibilityChanged (bool)), 74 connect (this, SIGNAL (visibilityChanged (bool)),
79 this, SLOT(handle_visibility_changed (bool))); 75 this, SLOT(handle_visibility_changed (bool)));
80 76
81 connect (_workspace_model, SIGNAL (model_changed ()), 77 connect (view, SIGNAL (collapsed (QModelIndex)),
82 this, SLOT (model_changed ()));
83
84 connect (_workspace_tree_view, SIGNAL (collapsed (QModelIndex)),
85 this, SLOT (collapse_requested (QModelIndex))); 78 this, SLOT (collapse_requested (QModelIndex)));
86 connect (_workspace_tree_view, SIGNAL (expanded (QModelIndex)), 79 connect (view, SIGNAL (expanded (QModelIndex)),
87 this, SLOT (expand_requested (QModelIndex))); 80 this, SLOT (expand_requested (QModelIndex)));
88 81
89 connect (_workspace_tree_view, SIGNAL (doubleClicked (QModelIndex)), 82 connect (view, SIGNAL (doubleClicked (QModelIndex)),
90 this, SLOT (item_double_clicked (QModelIndex))); 83 this, SLOT (item_double_clicked (QModelIndex)));
91 84
92 // topLevelChanged is emitted when floating property changes (floating = true) 85 // topLevelChanged is emitted when floating property changes (floating = true)
93 connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool))); 86 connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool)));
94 87
98 { 91 {
99 QSettings *settings = resource_manager::get_settings (); 92 QSettings *settings = resource_manager::get_settings ();
100 settings->setValue("workspaceview/local_collapsed", _explicit_collapse.local); 93 settings->setValue("workspaceview/local_collapsed", _explicit_collapse.local);
101 settings->setValue("workspaceview/global_collapsed", _explicit_collapse.global); 94 settings->setValue("workspaceview/global_collapsed", _explicit_collapse.global);
102 settings->setValue("workspaceview/persistent_collapsed", _explicit_collapse.persistent); 95 settings->setValue("workspaceview/persistent_collapsed", _explicit_collapse.persistent);
103 settings->setValue("workspaceview/column_state", _workspace_tree_view->header ()->saveState ()); 96 settings->setValue("workspaceview/column_state", view->header ()->saveState ());
104 settings->sync (); 97 settings->sync ();
105 } 98 }
106 99
107 void 100 void
108 workspace_view::handle_visibility_changed (bool visible) 101 workspace_view::handle_visibility_changed (bool visible)
112 } 105 }
113 106
114 void 107 void
115 workspace_view::model_changed () 108 workspace_view::model_changed ()
116 { 109 {
117 _workspace_model->update_workspace_callback (); 110 QAbstractItemModel *m = view->model ();
111
112 dynamic_cast<workspace_model *> (m)->update_workspace_callback ();
118 113
119 // This code is very quirky and requires some explanation. 114 // This code is very quirky and requires some explanation.
120 // Usually, we should not deal with collapsing or expanding ourselves, 115 // Usually, we should not deal with collapsing or expanding ourselves,
121 // because the view itself determines (based on the model) whether it 116 // because the view itself determines (based on the model) whether it
122 // is appropriate to collapse or expand items. 117 // is appropriate to collapse or expand items.
128 // displayed data as invalid. 123 // displayed data as invalid.
129 // 124 //
130 // In order to make collapsing/expanding work again, we need to set 125 // In order to make collapsing/expanding work again, we need to set
131 // flags ourselves here. 126 // flags ourselves here.
132 127
133 QModelIndex local_model_index = _workspace_model->index (0, 0); 128 QModelIndex local_model_index = m->index (0, 0);
134 QModelIndex global_model_index = _workspace_model->index (1, 0); 129 QModelIndex global_model_index = m->index (1, 0);
135 QModelIndex persistent_model_index = _workspace_model->index (2, 0); 130 QModelIndex persistent_model_index = m->index (2, 0);
136 131
137 if (_explicit_collapse.local) { 132 if (_explicit_collapse.local) {
138 _workspace_tree_view->collapse (local_model_index); 133 view->collapse (local_model_index);
139 } else { 134 } else {
140 _workspace_tree_view->expand (local_model_index); 135 view->expand (local_model_index);
141 } 136 }
142 137
143 if (_explicit_collapse.global) { 138 if (_explicit_collapse.global) {
144 _workspace_tree_view->collapse (global_model_index); 139 view->collapse (global_model_index);
145 } else { 140 } else {
146 _workspace_tree_view->expand (global_model_index); 141 view->expand (global_model_index);
147 } 142 }
148 143
149 if (_explicit_collapse.persistent) { 144 if (_explicit_collapse.persistent) {
150 _workspace_tree_view->collapse (persistent_model_index); 145 view->collapse (persistent_model_index);
151 } else { 146 } else {
152 _workspace_tree_view->expand (persistent_model_index); 147 view->expand (persistent_model_index);
153 } 148 }
154 } 149 }
155 150
156 void 151 void
157 workspace_view::collapse_requested (QModelIndex index) 152 workspace_view::collapse_requested (QModelIndex index)
167 // completely from scratch (O(n)), which is why the view renders all 162 // completely from scratch (O(n)), which is why the view renders all
168 // displayed data as invalid. 163 // displayed data as invalid.
169 // 164 //
170 // In order to make collapsing/expanding work again, we need to set 165 // In order to make collapsing/expanding work again, we need to set
171 // flags ourselves here. 166 // flags ourselves here.
172 QMap<int, QVariant> item_data 167 QAbstractItemModel *m = view->model ();
173 = _workspace_model->itemData (index); 168
169 QMap<int, QVariant> item_data = m->itemData (index);
174 170
175 if (item_data[0] == "Local") 171 if (item_data[0] == "Local")
176 _explicit_collapse.local = true; 172 _explicit_collapse.local = true;
177 if (item_data[0] == "Global") 173 if (item_data[0] == "Global")
178 _explicit_collapse.global = true; 174 _explicit_collapse.global = true;
194 // completely from scratch (O(n)), which is why the view renders all 190 // completely from scratch (O(n)), which is why the view renders all
195 // displayed data as invalid. 191 // displayed data as invalid.
196 // 192 //
197 // In order to make collapsing/expanding work again, we need to do set 193 // In order to make collapsing/expanding work again, we need to do set
198 // flags ourselves here. 194 // flags ourselves here.
199 QMap<int, QVariant> item_data 195 QAbstractItemModel *m = view->model ();
200 = _workspace_model->itemData (index); 196
197 QMap<int, QVariant> item_data = m->itemData (index);
201 198
202 if (item_data[0] == "Local") 199 if (item_data[0] == "Local")
203 _explicit_collapse.local = false; 200 _explicit_collapse.local = false;
204 if (item_data[0] == "Global") 201 if (item_data[0] == "Global")
205 _explicit_collapse.global = false; 202 _explicit_collapse.global = false;