QXRD  0.11.16
qcepdatasetmodel.cpp
Go to the documentation of this file.
1 #include "qcepdatasetmodel.h"
2 #include "qcepdataobject.h"
3 #include "qcepdataobject-ptr.h"
4 #include "qcepdataset.h"
5 #include "qcepdebug.h"
6 #include <QMimeData>
7 #include <stdio.h>
8 #include <QFileInfo>
9 #include <QDir>
10 
12  m_Dataset(ds)
13 {
14  connect(ds.data(), SIGNAL(dataObjectChanged()), this, SLOT(onDataObjectChanged()));
15 }
16 
18 {
20 
22  printf("QcepDatasetModel::indexedObject(%s)\n", qPrintable(indexDescription(index)));
23  }
24 
25  if (index.isValid()) {
26  QcepDataObject *obj = static_cast<QcepDataObject*>(index.internalPointer());
27 
28  if (obj) {
29  res = obj->sharedFromThis();
30 
31  if (!res) {
32  printf("QcepDatasetModel::indexedObject returns NULL\n");
33  }
34  }
35  }
36 
38  printf("QcepDatasetModel::indexedObject(%s)", qPrintable(indexDescription(index)));
39  if (res) {
40  printf(" = %s\n", qPrintable(res->get_Name()));
41  } else {
42  printf(" = null\n");
43  }
44  }
45 
46  return res;
47 }
48 
49 QModelIndex QcepDatasetModel::index(int row, int column, const QModelIndex &parent) const
50 {
51  QModelIndex res = QModelIndex();
52 
54  printf("QcepDatasetModel::index(%d,%d,%s)\n",
55  row, column, qPrintable(indexDescription(parent)));
56  }
57 
58 // if (hasIndex(row, column, parent)) {
59  QcepDataObjectPtr parentItem = indexedObject(parent);
60 
61  if (!parentItem) {
62  parentItem = m_Dataset;
63  }
64 
65  if (parentItem) {
66  QcepDataObjectPtr childItem = parentItem->item(row);
67 
68  if (childItem) {
69  res = createIndex(row, column, childItem.data());
70  }
71  }
72  // }
73 
75  printf("QcepDatasetModel::index(%d,%d,%s) = %s\n", row, column,
76  qPrintable(indexDescription(parent)),
77  qPrintable(indexDescription(res)));
78  }
79 
80  return res;
81 }
82 
83 QModelIndex QcepDatasetModel::index(const QcepDataObjectPtr &obj) const
84 {
85  if (obj == NULL || obj == m_Dataset) {
86  return QModelIndex();
87  } else {
88  QcepDataGroupPtr parent = obj->parentItem();
89 
90  if (parent) {
91  int n = obj->indexInParent();
92 
93  if (n >= 0) {
94  return createIndex(n, 0, obj.data());
95  }
96  }
97 
98  return QModelIndex();
99  }
100 }
101 
102 QModelIndex QcepDatasetModel::parent(const QModelIndex &index) const
103 {
104  QModelIndex res = QModelIndex();
105 
107  printf("QcepDatasetModel::parent(%s)\n", qPrintable(indexDescription(index)));
108  }
109 
110  QcepDataObjectPtr childItem = indexedObject(index);
111 
112  if (childItem) {
113 // if (qcepDebug(DEBUG_DATABROWSER)) {
114 // printf("of object %s\n", qPrintable(childItem->get_Name()));
115 // }
116 
117  QcepDataObjectPtr parentItem = childItem->parentItem();
118 
119  if (parentItem) {
120  if (parentItem != m_Dataset.data()) {
121  res = createIndex(parentItem->indexInParent(), 0, parentItem.data());
122  }
123  }
124  }
125 
127  printf("QcepDatasetModel::parent(%s)", qPrintable(indexDescription(index)));
128  printf(" = (%s)\n", qPrintable(indexDescription(res)));
129  }
130 
131  return res;
132 }
133 
134 int QcepDatasetModel::rowCount(const QModelIndex &parent) const
135 {
136  int res = 0;
137 
139  printf("QcepDatasetModel::rowCount(%s)\n", qPrintable(indexDescription(parent)));
140  }
141 
142  if (parent.column() <= 0) {
143  QcepDataObjectPtr parentItem = indexedObject(parent);
144 
145  if (parentItem) {
146  int nrows = parentItem->childCount();
147 
148  res = nrows;
149  } else {
151 
152  if (ds) {
153  res = ds->childCount();
154  }
155  }
156  }
157 
159  printf("QcepDatasetModel::rowCount(%s)", qPrintable(indexDescription(parent)));
160  printf(" = %d\n", res);
161  }
162 
163  return res;
164 }
165 
166 int QcepDatasetModel::columnCount(const QModelIndex &parent) const
167 {
168  int res = 0;
169 
171  printf("QcepDatasetModel::columnCount(%s)\n", qPrintable(indexDescription(parent)));
172  }
173 
174  QcepDataObjectPtr parentItem = indexedObject(parent);
175 
176  if (parentItem) {
177  res = 3;
178  } else {
179  res = 3;
180  }
181 
183  printf("QcepDatasetModel::columnCount(%s)", qPrintable(indexDescription(parent)));
184  printf(" = %d\n", res);
185  }
186 
187  return res;
188 }
189 
190 QVariant QcepDatasetModel::headerData(int section, Qt::Orientation orientation, int role) const
191 {
192  if (orientation==Qt::Horizontal) {
193  if (role==Qt::DisplayRole) {
194  switch (section) {
195  case 0:
196  return "Name";
197 
198  case 1:
199  return "Type";
200 
201  case 2:
202  return "Description";
203  }
204  }
205  }
206 
207  return QVariant();
208 }
209 
210 QVariant QcepDatasetModel::data(const QModelIndex &index, int role) const
211 {
212  QVariant res = QVariant();
213 
215  printf("QcepDatasetModel::data(%s,%d)\n", qPrintable(indexDescription(index)), role);
216  }
217 
218  if (!index.isValid()) {
219  res = QVariant();
220  } else {
221 
222  QcepDataObjectPtr object = indexedObject(index);
223 
224  if (object) {
225  if (role == Qt::DisplayRole) {
226  res = object->columnData(index.column());
227  } else if (role == Qt::ToolTipRole) {
228  res = object->pathName() + "\n" +
229  object->columnData(1).toString() + "\n" +
230  object->columnData(2).toString();
231  }
232  }
233  }
234 
236  printf("QcepDatasetModel::data(%s,%d)", qPrintable(indexDescription(index)), role);
237  printf(" = %s\n", qPrintable(res.toString()));
238  }
239 
240  return res;
241 }
242 
243 Qt::ItemFlags QcepDatasetModel::flags(const QModelIndex &index) const
244 {
245  Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
246 
247  if (index.isValid()) {
248  return defaultFlags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
249  } else {
250  return defaultFlags | Qt::ItemIsDropEnabled;
251  }
252 }
253 
254 QStringList QcepDatasetModel::mimeTypes() const
255 {
256 // if (qcepDebug(DEBUG_DRAGDROP)) {
257  printf("QcepDatasetModel::mimeTypes\n");
258 // }
259 
260  QStringList types;
261  types << "application/vnd.text.list";
262 // types << "text/plain";
263  return types;
264 }
265 
266 QMimeData *QcepDatasetModel::mimeData(const QModelIndexList &indexes) const
267 {
268  QMimeData *mimeData = new QMimeData();
269  QString textData;
270 
271  foreach (const QModelIndex &index, indexes) {
272  if (index.isValid()) {
273  QString text = data(index, Qt::DisplayRole).toString();
274  textData += text;
275  }
276  }
277 
278  if (qcepDebug(DEBUG_DRAGDROP)) {
279  printf("QcepDatasetModel::mimeData = %s\n", qPrintable(textData));
280  }
281 
282  mimeData->setText(textData);
283 // mimeData->setData("text/plain", encodedData);
284  return mimeData;
285 }
286 
287 QString QcepDatasetModel::indexDescription(const QModelIndex &index) const
288 {
289  if (index.isValid()) {
290  QcepDataObject* obj = static_cast<QcepDataObject*>(index.internalPointer());
291 
292  if (obj) {
293  return tr("(%1,%2,\"%3\")")
294  .arg(index.row()).arg(index.column())
295  .arg(obj->get_Name());
296  } else {
298 
299  if (ds) {
300  return tr("(%1,%2,\"%3\")")
301  .arg(index.row()).arg(index.column())
302  .arg(ds->get_Name());
303  } else {
304  return "";
305  }
306  }
307  }
308 
309  return tr("(%1,%2,null)").arg(index.row()).arg(index.column());
310 }
311 
312 bool QcepDatasetModel::insertRows(int row, int count, const QModelIndex &parent)
313 {
314  printf("QcepDatasetModel::insertRows(row:%d, count:%d, parent:%s)\n", row, count, qPrintable(indexDescription(parent)));
315 
316  return QAbstractItemModel::insertRows(row, count, parent);
317 }
318 
320  (const QModelIndex &sourceParent, int sourceRow, int count,
321  const QModelIndex &destinationParent, int destinationChild)
322 {
323  printf("QAbstractItemModel::moveRows(sourceParent:%s, sourceRow:%d, count:%d,\n"
324  " destinationParent:%s, destinationChild:%d)\n",
325  qPrintable(indexDescription(sourceParent)), sourceRow, count,
326  qPrintable(indexDescription(destinationParent)), destinationChild);
327 
328  return QAbstractItemModel::moveRows(sourceParent, sourceRow, count,
329  destinationParent, destinationChild);
330 }
331 
332 bool QcepDatasetModel::removeRows(int row, int count, const QModelIndex &parent)
333 {
334  printf("QcepDatasetModel::removeRows(row:%d, count:%d, parent:%s)\n", row, count, qPrintable(indexDescription(parent)));
335 
336  return QAbstractItemModel::removeRows(row, count, parent);
337 }
338 
340 {
341 // beginResetModel();
342 // endResetModel();
343 
344  emit dataChanged(index(0,0), index(rowCount(), columnCount()));
345 }
346 
347 QString QcepDatasetModel::groupName(QString path)
348 {
349  QFileInfo info(path);
350 
351  if (info.isAbsolute()) {
352  return info.dir().absolutePath();
353  } else {
354  return info.dir().path();
355  }
356 }
357 
358 QString QcepDatasetModel::objectName(QString path)
359 {
360  QFileInfo info(path);
361 
362  return info.fileName();
363 }
364 
366 {
367  return indexedObject(index);
368 }
369 
371 {
373 
374  if (ds) {
375  return ds->item(path);
376  } else {
377  return QcepDataObjectPtr();
378  }
379 }
380 
382 {
384 
385  if (ds) {
386  return ds->item(n);
387  } else {
388  return QcepDataObjectPtr();
389  }
390 }
391 
393 {
394  return qSharedPointerDynamicCast<QcepDataGroup>(indexedObject(index));
395 }
396 
398 {
400 
401  if (ds) {
402  return ds->group(path);
403  } else {
404  return QcepDataGroupPtr();
405  }
406 }
407 
409 {
411 
412  if (ds) {
413  return ds->group(n);
414  } else {
415  return QcepDataGroupPtr();
416  }
417 }
418 
420 {
421  QcepDataGroupPtr gr = group(path);
422 
423  if (gr) {
424  return gr;
425  } else {
426  QcepDataObjectPtr obj = item(path);
428 
429  if (obj) {
430  if (ds) {
431  ds->printMessage(tr("Item %1 exists and is not a data group").arg(path));
432  }
433  } else {
434  QcepDataGroupPtr sgr = newGroup(groupName(path));
435 
436  if (sgr && ds) {
437  QcepDataGroupPtr par = sgr->parentItem();
438 
439  QcepDataGroupPtr ng =
440  QcepDataGroupPtr(new QcepDataGroup(ds->saver(), objectName(path), sgr.data()));
441 
442  if (ng) {
443  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
444 
445  sgr->append(ng);
446 
447  endInsertRows();
448 
449  return ng;
450  }
451  }
452  }
453  }
454 
455  return QcepDataGroupPtr();
456 }
457 
459 {
460  return qSharedPointerDynamicCast<QcepDataArray>(indexedObject(index));
461 }
462 
464 {
466 
467  if (ds) {
468  return ds->array(path);
469  } else {
470  return QcepDataArrayPtr();
471  }
472 }
473 
475 {
477 
478  if (ds) {
479  return ds->array(n);
480  } else {
481  return QcepDataArrayPtr();
482  }
483 }
484 
485 QcepDataArrayPtr QcepDatasetModel::newArray(QString path, QVector<int> dims)
486 {
487  QcepDataGroupPtr sgr = newGroup(groupName(path));
489 
490  if (sgr && ds) {
491  QcepDataObjectPtr ptr = item(path);
492  QcepDataArrayPtr arr = array(path);
493 
494  if (ptr && arr == NULL) {
495  ds->printMessage(tr("%1 exists but is not an array").arg(path));
496  } else if (arr) {
497  ds->printMessage(tr("Array %1 already exists").arg(path));
498  } else {
499  QcepDataArrayPtr arr(new QcepDataArray(ds->saver(), objectName(path), dims, sgr.data()));
500 
501  if (arr) {
502  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
503  sgr -> append(arr);
504  endInsertRows();
505 
506  return arr;
507  }
508  }
509  }
510 
511  return QcepDataArrayPtr();
512 }
513 
515 {
516  return qSharedPointerDynamicCast<QcepDataColumn>(indexedObject(index));
517 }
518 
520 {
522 
523  if (ds) {
524  return ds->column(path);
525  } else {
526  return QcepDataColumnPtr();
527  }
528 }
529 
531 {
533 
534  if (ds) {
535  return ds->column(n);
536  } else {
537  return QcepDataColumnPtr();
538  }
539 }
540 
542 {
543  QcepDataGroupPtr sgr = newGroup(groupName(path));
545 
546  if (sgr && ds) {
547  QcepDataObjectPtr ptr = item(path);
548  QcepDataColumnPtr col = column(path);
549 
550  if (ptr && col == NULL) {
551  ds->printMessage(tr("%1 exists but is not a data column").arg(path));
552  } else if (col) {
553  ds->printMessage(tr("Column %1 already exists").arg(path));
554  } else {
555  QcepDataColumnPtr col(new QcepDataColumn(ds->saver(), objectName(path), nRows, sgr.data()));
556 
557  if (col) {
558  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
559  sgr -> append(col);
560  endInsertRows();
561 
562  return col;
563  }
564  }
565  }
566 
567  return QcepDataColumnPtr();
568 }
569 
571 {
572  return qSharedPointerDynamicCast<QcepDataColumnScan>(indexedObject(index));
573 }
574 
576 {
578 
579  if (ds) {
580  return ds->columnScan(path);
581  } else {
582  return QcepDataColumnScanPtr();
583  }
584 }
585 
587 {
589 
590  if (ds) {
591  return ds->columnScan(n);
592  } else {
593  return QcepDataColumnScanPtr();
594  }
595 }
596 
597 QcepDataColumnScanPtr QcepDatasetModel::newColumnScan(QString path, int nRows, QStringList cols)
598 {
599  QcepDataGroupPtr sgr = newGroup(groupName(path));
601 
602  if (sgr && ds) {
603  QcepDataObjectPtr ptr = item(path);
604  QcepDataColumnScanPtr scan = columnScan(path);
605 
606  if (ptr && scan == NULL) {
607  ds->printMessage(tr("%1 exists but is not a data column scan").arg(path));
608  } else if (scan) {
609  ds->printMessage(tr("Column Scan %1 already exists").arg(path));
610  } else {
611  QcepDataColumnScanPtr scan(QcepDataColumnScan::newDataColumnScan(ds->saver(), objectName(path), cols, nRows, sgr.data()));
612 
613  if (scan) {
614  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
615  sgr -> append(scan);
616  endInsertRows();
617 
618  return scan;
619  }
620  }
621  }
622 
623  return QcepDataColumnScanPtr();
624 }
625 
627 {
628  return qSharedPointerDynamicCast<QcepDoubleImageData>(indexedObject(index));
629 }
630 
632 {
634 
635  if (ds) {
636  return ds->image(path);
637  } else {
638  return QcepDoubleImageDataPtr();
639  }
640 }
641 
643 {
645 
646  if (ds) {
647  return ds->image(n);
648  } else {
649  return QcepDoubleImageDataPtr();
650  }
651 }
652 
653 QcepDoubleImageDataPtr QcepDatasetModel::newImage(QString path, int width, int height)
654 {
655  QcepDataGroupPtr sgr = newGroup(groupName(path));
657 
658  if (sgr && ds) {
659  QcepDataObjectPtr ptr = item(path);
660  QcepDoubleImageDataPtr img = image(path);
661 
662  if (ptr && img == NULL) {
663  ds->printMessage(tr("%1 exists but is not an image").arg(path));
664  } else if (img) {
665  ds->printMessage(tr("Image %1 already exists").arg(path));
666  } else {
667  QcepDoubleImageDataPtr img(QcepDoubleImageData::newImage(ds->saver(), objectName(path), width, height, sgr.data()));
668 
669  if (img) {
670  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
671  sgr -> append(img);
672  endInsertRows();
673 
674  return img;
675  }
676  }
677  }
678 
679  return QcepDoubleImageDataPtr();
680 }
681 
683 {
684  return qSharedPointerDynamicCast<QcepIntegratedData>(indexedObject(index));
685 }
686 
688 {
690 
691  if (ds) {
692  return ds->integratedData(path);
693  } else {
694  return QcepIntegratedDataPtr();
695  }
696 }
697 
699 {
701 
702  if (ds) {
703  return ds->integratedData(n);
704  } else {
705  return QcepIntegratedDataPtr();
706  }
707 }
708 
710 {
711  QcepDataGroupPtr sgr = newGroup(groupName(path));
713 
714  if (sgr && ds) {
715  QcepDataObjectPtr ptr = item(path);
717 
718  if (ptr && dat == NULL) {
719  ds->printMessage(tr("%1 exists but is not an integrated dataset").arg(path));
720  } else if (dat) {
721  ds->printMessage(tr("Integrated dataset %1 already exists").arg(path));
722  } else {
723  QcepIntegratedDataPtr dat(QcepIntegratedData::newIntegratedData(ds->saver(), objectName(path), sz, sgr.data()));
724 
725  if (dat) {
726  beginInsertRows(index(sgr), sgr->rowCount(), sgr->rowCount()+1);
727  sgr -> append(dat);
728  endInsertRows();
729 
730  return dat;
731  }
732  }
733  }
734 
735  return QcepIntegratedDataPtr();
736 }
737 
738 void QcepDatasetModel::insertGroup(int atRow, QString name)
739 {
741 
742  if (ds) {
743  beginInsertRows(QModelIndex(), atRow, atRow+1);
744 
745  ds->insert(atRow, QcepDataGroupPtr(new QcepDataGroup(ds->saver(), name, ds.data())));
746 
747  endInsertRows();
748  }
749 }
750 
751 void QcepDatasetModel::append(const QModelIndex &index, QcepDataObjectPtr obj)
752 {
753 }
754 
756 {
757 }
758 
760 {
761  if (obj) {
762  QcepDataGroupPtr par = obj->parentItem();
763  int n = obj->indexInParent();
764 
765  if (par && n >= 0 && n < par->childCount()) {
766  beginRemoveRows(index(par), n, n);
767  par->remove(n);
768  endRemoveRows();
769  }
770  }
771 }
772 
773 void QcepDatasetModel::remove(const QModelIndex &index)
774 {
775  QcepDataObjectPtr obj = indexedObject(index);
776 
777  remove(obj);
778 }
779 
780 void QcepDatasetModel::remove(QString path)
781 {
782  QcepDataObjectPtr obj = item(path);
783 
784  remove(obj);
785 }
QcepDataColumnScanPtr newColumnScan(QString path, int nRows=0, QStringList cols=QStringList())
QSharedPointer< QcepDataArray > QcepDataArrayPtr
QcepDoubleImageDataPtr newImage(QString path, int width=0, int height=0)
QString groupName(QString path)
void append(const QModelIndex &index, QcepDataObjectPtr obj)
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
QcepDataArrayPtr array(const QModelIndex &index)
QSharedPointer< QcepDataGroup > QcepDataGroupPtr
static QSharedPointer< QcepImageData< T > > newImage(QcepSettingsSaverWPtr saver, QString name, int width, int height, QcepObject *parent)
QVariant headerData(int section, Qt::Orientation orientation, int role) const
QSharedPointer< QcepDataColumnScan > QcepDataColumnScanPtr
QcepDataObjectPtr indexedObject(const QModelIndex &index) const
QcepDataGroupPtr group(const QModelIndex &index)
QSharedPointer< QcepDataColumn > QcepDataColumnPtr
QcepIntegratedDataPtr newIntegratedData(QString path, int sz)
void insertGroup(int atRow, QString name)
QSharedPointer< QcepIntegratedData > QcepIntegratedDataPtr
QcepDataColumnPtr column(const QModelIndex &index)
int rowCount(const QModelIndex &parent=QModelIndex()) const
int columnCount(const QModelIndex &parent=QModelIndex()) const
QModelIndex parent(const QModelIndex &index) const
QcepDatasetModel(QcepDatasetWPtr ds)
QcepDataColumnPtr newColumn(QString path, int nRows=0)
QString get_Name() const
Definition: qcepobject.cpp:72
QStringList mimeTypes() const
QcepDatasetWPtr m_Dataset
QMimeData * mimeData(const QModelIndexList &indexes) const
QcepDataArrayPtr newArray(QString path, QVector< int > dims=QVector< int >())
QcepIntegratedDataPtr integratedData(const QModelIndex &index)
QcepDoubleImageDataPtr image(const QModelIndex &index)
QString indexDescription(const QModelIndex &index) const
QString objectName(QString path)
QcepDataColumnScanPtr columnScan(const QModelIndex &index)
QcepDataGroupPtr newGroup(QString path)
void remove(QcepDataObjectPtr obj)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
static QcepDataColumnScanPtr newDataColumnScan(QcepSettingsSaverWPtr sav, QString name, QStringList cols, int npts, QcepObject *parent)
static QcepIntegratedDataPtr newIntegratedData(QcepSettingsSaverWPtr saver, QString name, int sz, QcepObject *parent)
QcepDataObjectPtr item(const QModelIndex &index)
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
QSharedPointer< QcepDataObject > QcepDataObjectPtr
QWeakPointer< QcepDataset > QcepDatasetWPtr
bool insertRows(int row, int count, const QModelIndex &parent)
bool removeRows(int row, int count, const QModelIndex &parent)
QSharedPointer< QcepDataset > QcepDatasetPtr
Qt::ItemFlags flags(const QModelIndex &index) const
QSharedPointer< QcepDoubleImageData > QcepDoubleImageDataPtr