Vespucci  1.0.0
datasettreemodel.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
20 #include "datasettreemodel.h"
21 
23  : QAbstractItemModel(parent)
24 {
25  QStringList root_keys;
26  QList<QVariant> root_data = {QVariant("Title"), QVariant("Description")};
27  root_item_ = new TreeItem(TreeItem::ItemType::Base, root_keys, root_data, 0);
28 
29 }
30 
32  : QAbstractItemModel(parent)
33 {
34  QStringList root_keys;
35  QList<QVariant> root_data = {QVariant("Title"), QVariant("Description")};
36  root_item_ = new TreeItem(TreeItem::ItemType::Base, root_keys, root_data, 0);
37  QStringList dataset_keys = data_model->DatasetNames();
38 
39  for (auto dataset_key: dataset_keys){
40  QStringList dataset_item_keys = {dataset_key};
41  QList<QVariant> dataset_item_data = {QVariant(dataset_key),
42  QVariant("Dataset")};
43  TreeItem *dataset_tree_item = new TreeItem(TreeItem::ItemType::Dataset,
44  dataset_item_keys, dataset_item_data,
45  root_item_);
46  QSharedPointer<VespucciDataset> dataset = data_model->GetDataset(dataset_key);
47  QList<QStringList> corekeys = {
48  {dataset_key, "Spectra"},
49  {dataset_key, "Spectral Abscissa"},
50  {dataset_key, "x"},
51  {dataset_key, "y"}
52  };
53 
54  QList<QList<QVariant> > coredata = {
55  {"Spectra", DescribeSpectra(dataset->spectra_ref())},
56  {"Spectral Abscissa", DescribeAbscissa(dataset->abscissa_ref())},
57  {"x", DescribeMatrix(dataset->x_ref())},
58  {"y", DescribeMatrix(dataset->y_ref())}
59  };
60 
61  for (int i = 0; i < coredata.size(); ++i){
62  TreeItem *coreitem = new TreeItem(TreeItem::Matrix,
63  corekeys[i],
64  coredata[i],
65  dataset_tree_item);
66  dataset_tree_item->appendChild(coreitem);
67  }
68 
69  for (auto matrix_key: dataset->AuxiliaryMatrixKeys()){
70  QStringList item_keys = {dataset_key, matrix_key};
71  QList<QVariant> item_data =
72  {QVariant(matrix_key),
73  QVariant(DescribeMatrix(dataset->GetAuxiliaryMatrix(matrix_key)))
74  };
75  TreeItem *matrix_item = new TreeItem(TreeItem::ItemType::Matrix,
76  item_keys, item_data,
77  dataset_tree_item);
78  dataset_tree_item->appendChild(matrix_item);
79  }
80 
81  for (auto results_key: dataset->AnalysisResultsKeys()){
82  QSharedPointer<AnalysisResults> results =
83  dataset->GetAnalysisResult(results_key);
84  QStringList results_item_keys = {dataset_key, results_key};
85  QList<QVariant> results_item_data = {QVariant(results_key),
86  QVariant(results->type())};
87  TreeItem *results_tree_item =
88  new TreeItem(TreeItem::ItemType::AnalysisResult,
89  results_item_keys,
90  results_item_data,
91  dataset_tree_item);
92 
93  for (auto matrix_key: results->KeyList()){
94  QStringList matrix_item_keys = {dataset_key, results_key, matrix_key};
95  QList<QVariant> matrix_item_data = {QVariant(matrix_key),
96  QVariant(DescribeMatrix(results->GetMatrix(matrix_key)))
97  };
98  TreeItem *matrix_tree_item =
100  matrix_item_keys,
101  matrix_item_data,
102  results_tree_item);
103  results_tree_item->appendChild(matrix_tree_item);
104  }
105  dataset_tree_item->appendChild(results_tree_item);
106  }
107 
108  for (auto map_key: dataset->MapKeys()){
109  QSharedPointer<MapData> mapdata = dataset->GetMapData(map_key);
110  QStringList map_item_keys = {dataset_key, map_key};
111  QList<QVariant> map_item_data = {QVariant(map_key),
112  QVariant(mapdata->type())};
113  TreeItem *map_tree_item =
115  map_item_keys,
116  map_item_data,
117  dataset_tree_item);
118  dataset_tree_item->appendChild(map_tree_item);
119  }
120  root_item_->appendChild(dataset_tree_item);
121  }
122 }
123 
129 {
130  QStringList dataset_keys = data_model->DatasetNames();
131  for (auto dataset_key: dataset_keys){
132  QSharedPointer<VespucciDataset> dataset = data_model->GetDataset(dataset_key);
133  TreeItem *dataset_tree_item = SetupDatasetTreeItem(dataset);
134  root_item_->appendChild(dataset_tree_item);
135  }
136 }
137 
138 
140 {
141  delete root_item_;
142 }
143 
144 QVariant DatasetTreeModel::data(const QModelIndex &index, int role) const
145 {
146  if (!index.isValid())
147  return QVariant();
148  if(role != Qt::DisplayRole)
149  return QVariant();
150  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
151  return item->data(index.column());
152 }
153 
154 Qt::ItemFlags DatasetTreeModel::flags(const QModelIndex &index) const
155 {
156  if (!index.isValid())
157  return 0;
158  return QAbstractItemModel::flags(index);
159 }
160 
161 QVariant DatasetTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
162 {
163  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
164  return root_item_->data(section);
165  return QVariant();
166 }
167 
168 QModelIndex DatasetTreeModel::index(int row, int column, const QModelIndex &parent) const
169 {
170  if(!hasIndex(row, column, parent))
171  return QModelIndex();
172  TreeItem *parent_item;
173  if (!parent.isValid())
174  parent_item = root_item_;
175  else
176  parent_item = static_cast<TreeItem*>(parent.internalPointer());
177 
178  TreeItem *child_item = parent_item->child(row);
179  if (child_item)
180  return createIndex(row, column, child_item);
181  else
182  return QModelIndex();
183 }
184 
185 QModelIndex DatasetTreeModel::parent(const QModelIndex &index) const
186 {
187  if (!index.isValid())
188  return QModelIndex();
189 
190  TreeItem *child_item = static_cast<TreeItem*>(index.internalPointer());
191  TreeItem *parent_item = child_item->parentItem();
192 
193  if (parent_item == root_item_)
194  return QModelIndex();
195  return createIndex(parent_item->row(), 0, parent_item);
196 }
197 
198 int DatasetTreeModel::rowCount(const QModelIndex &parent) const
199 {
200  TreeItem *parent_item = getItem(parent);
201  return parent_item->childCount();
202 }
203 
204 int DatasetTreeModel::columnCount(const QModelIndex &parent) const
205 {
206  return root_item_->columnCount();
207 }
208 
209 bool DatasetTreeModel::removeRows(int row, int count, const QModelIndex &parent)
210 {
211  //TreeItem *parent_item = getItem(parent);
212  bool success = true;
213  beginRemoveRows(parent, row, count + row - 1);
214  endRemoveRows();
215  return success;
216 }
217 
219 {
220  QList<TreeItem*> items = root_item_->child_items();
221  //From top level (dataset level), make sure that any items in items
222  //that are not in the data model are removed
223  for (auto item: items)
224  if (!data_model->DatasetNames().contains(item->data(0).toString()))
225  root_item_->removeChild(item);
226 
227  //Check if a dataset has been added and set it up if it has been
228  QStringList dataset_names = root_item_->ChildNames();
229  for (auto dataset_key: data_model->DatasetNames()){
230  if (!dataset_names.contains(dataset_key)){
231  TreeItem *dataset_tree_item = SetupDatasetTreeItem(data_model->GetDataset(dataset_key));
232  root_item_->appendChild(dataset_tree_item);
233  }
234  }
235 
236  //check all the datasets to see if anything changed in them:
237  for (auto item: root_item_->child_items()){
238  QString dataset_name = item->data(0).toString();
239  QStringList analysis_results_names = data_model->AnalysisResultsNames(dataset_name);
240  QStringList core_matrix_names = {"Spectra", "Spectral Abscissa", "x", "y"};
241 
242  //check for changed analysis results
243  QStringList child_names = item->ChildNames();
244  //add new analysis results
245  for (auto results_name: analysis_results_names){
246  if (!child_names.contains(results_name)
247  && data_model->GetResults(dataset_name, results_name)->KeyList().size()){
248  TreeItem *analysis_results_item =
249  SetupAnalysisResultTreeItem(data_model->GetDataset(dataset_name),
250  data_model->GetResults(dataset_name, results_name),
251  item);
252  item->appendChild(analysis_results_item);
253  }//if
254  }//for (AnalysisResultsNames)
255 
256  //Remove old subobjects
257  for (auto child_item: item->child_items()){
258  QString child_name = child_item->data(0).toString();
259  if (!analysis_results_names.contains(child_name)
260  && !core_matrix_names.contains(child_name)){
261  item->removeChild(child_item);
262  }
263  }//for (item->child_items)
264 
265  }//for (root_item->child_items)
266 
267 
268 }
269 
270 void DatasetTreeModel::AddDataset(QSharedPointer<VespucciDataset> dataset)
271 {
272  TreeItem *dataset_tree_item = SetupDatasetTreeItem(dataset);
273  root_item_->appendChild(dataset_tree_item);
274 }
275 
276 
278 {
279  int row_count = rowCount(index(0,0));
280  beginRemoveRows(index(0,0), 0, row_count - 1);
281  root_item_->ClearChildren();
282  endRemoveRows();
283 }
284 
285 void DatasetTreeModel::removeRow(const QModelIndex &index)
286 {
287  beginRemoveRows(index, index.row(), index.row());
288  removeRows(index.row(), 1, index);
289  endRemoveRows();
290 }
291 
292 TreeItem *DatasetTreeModel::getItem(const QModelIndex &index) const
293 {
294  if (index.isValid()){
295  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
296  if (item){return item;}
297  }
298  return root_item_;
299 }
300 
302 {
303  return root_item_;
304 }
305 
311 TreeItem *DatasetTreeModel::SetupDatasetTreeItem(QSharedPointer<VespucciDataset> dataset)
312 {
313  QList<QVariant> dataset_data = {QVariant(dataset->name()), QVariant("Dataset")};
314  QStringList dataset_key_list = {dataset->name()};
315  TreeItem *dataset_tree_item = new TreeItem(TreeItem::Dataset,
316  dataset_key_list,
317  dataset_data,
318  root_item_);
319  QList<QStringList> corekeys = {
320  {dataset->name(), "Spectra"},
321  {dataset->name(), "Spectral Abscissa"},
322  {dataset->name(), "x"},
323  {dataset->name(), "y"}
324  };
325 
326  QList<QList<QVariant> > coredata = {
327  {"Spectra", DescribeSpectra(dataset->spectra_ref())},
328  {"Spectral Abscissa", DescribeAbscissa(dataset->abscissa_ref())},
329  {"x", DescribeMatrix(dataset->x_ref())},
330  {"y", DescribeMatrix(dataset->y_ref())}
331  };
332 
333  for (int i = 0; i < coredata.size(); ++i){
334  TreeItem *coreitem = new TreeItem(TreeItem::Matrix,
335  corekeys[i],
336  coredata[i],
337  dataset_tree_item);
338  dataset_tree_item->appendChild(coreitem);
339  }
340 
341  for (auto results_key: dataset->AnalysisResultsKeys()){
342  QSharedPointer<AnalysisResults> results = dataset->GetAnalysisResult(results_key);
343  TreeItem *analysis_results_item =
344  SetupAnalysisResultTreeItem(dataset,
345  results,
346  dataset_tree_item);
347  dataset_tree_item->appendChild(analysis_results_item);
348  }
349 
350  for (auto matrix_key: dataset->AuxiliaryMatrixKeys()){
351  const mat& matrix = dataset->GetAuxiliaryMatrix(matrix_key);
352  TreeItem *matrix_item = SetupMatrixTreeItem(dataset->name(),
353  matrix_key,
354  matrix,
355  dataset_tree_item);
356  dataset_tree_item->appendChild(matrix_item);
357  }
358 
359  for (auto map_key: dataset->MapKeys()){
360  QSharedPointer<MapData> map = dataset->GetMapData(map_key);
361  TreeItem *map_item = SetupMapTreeItem(dataset->name(),
362  map_key,
363  map,
364  dataset_tree_item);
365  dataset_tree_item->appendChild(map_item);
366  }
367 
368  return dataset_tree_item;
369 }
370 
377 TreeItem *DatasetTreeModel::SetupAnalysisResultTreeItem(QSharedPointer<VespucciDataset> dataset,
378  QSharedPointer<AnalysisResults> results,
379  TreeItem *parent)
380 {
381  QStringList analysis_results_key_list = {dataset->name(), results->name()};
382 
383  QList<QVariant> analysis_results_item_data =
384  {results->name(), results->type()};
385 
386  TreeItem *results_tree_item =
388  analysis_results_key_list,
389  analysis_results_item_data,
390  parent);
391 
392  for (auto matrix_key: results->KeyList()){
393  TreeItem *matrix_tree_item =
394  SetupMatrixTreeItem(dataset->name(),
395  results->name(),
396  results->GetMatrix(matrix_key),
397  results_tree_item);
398  results_tree_item->appendChild(matrix_tree_item);
399  }
400 
401  return results_tree_item;
402 }
403 
413 TreeItem *DatasetTreeModel::SetupMatrixTreeItem(const QString &dataset_key,
414  const QString &results_key,
415  const QString &matrix_key,
416  const mat & matrix,
417  TreeItem *parent)
418 {
419  QStringList matrix_key_list = {dataset_key, results_key, matrix_key};
420  QList<QVariant> matrix_data = {matrix_key, DescribeMatrix(matrix)};
421  return new TreeItem(TreeItem::Matrix, matrix_key_list, matrix_data, parent);
422 }
423 
432 TreeItem *DatasetTreeModel::SetupMatrixTreeItem(const QString &dataset_key,
433  const QString &matrix_key,
434  const mat & matrix,
435  TreeItem *parent)
436 {
437  QStringList matrix_key_list = {dataset_key, matrix_key};
438  QList<QVariant> matrix_data = {matrix_key, DescribeMatrix(matrix)};
439  return new TreeItem(TreeItem::Matrix, matrix_key_list, matrix_data, parent);
440 }
441 
450 TreeItem *DatasetTreeModel::SetupMapTreeItem(const QString &dataset_key,
451  const QString &map_key,
452  QSharedPointer<MapData> data,
453  TreeItem *parent)
454 {
455  QStringList map_key_list = {dataset_key, map_key};
456  QList<QVariant> map_data = {map_key, data->type()};
457  return new TreeItem(TreeItem::Map, map_key_list, map_data, parent);
458 }
459 
460 
461 QString DatasetTreeModel::DescribeMatrix(const mat & matrix) const
462 {
463  return QString::number(matrix.n_rows)
464  + "×"
465  + QString::number(matrix.n_cols)
466  + " matrix";
467 }
468 
469 QString DatasetTreeModel::DescribeSpectra(const mat & spectra_matrix) const
470 {
471  return QString::number(spectra_matrix.n_cols) + " spectra";
472 }
473 
474 QString DatasetTreeModel::DescribeAbscissa(const vec &abscissa) const
475 {
476  return QString::number(abscissa.n_rows)
477  + " points (" + QString::number(abscissa.min())
478  + "–" + QString::number(abscissa.max()) + ")";
479 }
void ClearChildren()
Definition: treeitem.cpp:88
TreeItem * child(int row)
Definition: treeitem.cpp:45
void AddDataset(QSharedPointer< VespucciDataset > dataset)
QStringList DatasetNames() const
DataModel::DatasetNames.
Definition: datamodel.cpp:266
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE
void removeChild(TreeItem *child)
Definition: treeitem.cpp:40
int row() const
Definition: treeitem.cpp:65
TreeItem::ItemType type() const
Definition: treeitem.cpp:93
void UpdateData(const DataModel *data_model)
int childCount() const
Definition: treeitem.cpp:50
TreeItem * parentItem()
Definition: treeitem.cpp:72
QSharedPointer< AnalysisResults > GetResults(const QString &dataset_key, const QString &results_key) const
DataModel::GetResults Get the AnalysisResults object named results_key from the dataset named dataset...
Definition: datamodel.cpp:49
void appendChild(TreeItem *child)
Definition: treeitem.cpp:35
QSharedPointer< VespucciDataset > GetDataset(const QString &key) const
DataModel::GetDataset Get the VespucciDataset named key.
Definition: datamodel.cpp:32
QStringList ChildNames() const
Definition: treeitem.cpp:108
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
TreeItem * getItem(const QModelIndex &index) const
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE
int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE
DatasetTreeModel(QObject *parent)
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE
void removeRow(const QModelIndex &index)
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) Q_DECL_OVERRIDE
int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE
QVariant data(int column) const
Definition: treeitem.cpp:60
void SetupModelData(const DataModel *data_model)
DatasetTreeModel::SetupModelData.
TreeItem * root_item()
QList< TreeItem * > child_items()
Definition: treeitem.cpp:77
QStringList AnalysisResultsNames(const QString &dataset_key) const
DataModel::AnalysisResultsNames.
Definition: datamodel.cpp:279
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
int columnCount() const
Definition: treeitem.cpp:55