File indexing completed on 2025-07-06 04:09:47

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "sessionmodel.h"
0014 #include "updateserver.h"
0015 
0016 #include <assert.h>
0017 #include <objectstore.h>
0018 #include <dataobject.h>
0019 #include <relation.h>
0020 #include <datavector.h>
0021 #include <datascalar.h>
0022 #include <vscalar.h>
0023 #include <generatedvector.h>
0024 #include <datamatrix.h>
0025 #include <generatedmatrix.h>
0026 #include <datasource.h>
0027 
0028 namespace Kst {
0029 
0030 SessionModel::SessionModel(ObjectStore *store)
0031 : QAbstractItemModel(), _store(store) {
0032   generateObjectList();
0033 
0034   connect(UpdateServer::self(), SIGNAL(objectListsChanged()), this, SLOT(triggerReset()));
0035 }
0036 
0037 
0038 SessionModel::~SessionModel() {
0039 }
0040 
0041 
0042 int SessionModel::columnCount(const QModelIndex& parent) const {
0043   Q_UNUSED(parent)
0044   return 4;
0045 }
0046 
0047 
0048 void SessionModel::triggerReset() {
0049   generateObjectList();
0050   reset();
0051 }
0052 
0053 
0054 void SessionModel::generateObjectList() {
0055   ObjectList<Primitive> pol = _store->getObjects<Primitive>();
0056   ObjectList<Relation> rol = _store->getObjects<Relation>();
0057   ObjectList<DataObject> dol = _store->getObjects<DataObject>();
0058   DataSourceList dsl = _store->dataSourceList();
0059   _objectList.clear();
0060   foreach(PrimitivePtr P, pol) {
0061     if ((!P->provider()) && (!P->hidden())) {
0062       _objectList.append(P);
0063     }
0064   }
0065 
0066   foreach(RelationPtr relation, rol) {
0067     _objectList.append(relation);
0068   }
0069 
0070   foreach(DataObjectPtr dataObject, dol) {
0071     _objectList.append(dataObject);
0072   }
0073 
0074   foreach(DataSourcePtr dataSource, dsl) {
0075     _objectList.append(dataSource);
0076   }
0077 }
0078 
0079 
0080 int SessionModel::rowCount(const QModelIndex& parent) const {
0081   Q_ASSERT(_store);
0082   //ObjectList<Object> dol = generateObjectList();
0083   int rc = 0;
0084   if (!parent.isValid()) {
0085     rc = _objectList.count();  /* + generated primitives */
0086     return rc;
0087   }
0088 
0089   if (parent.parent().isValid()) {
0090     return rc;
0091   }
0092 
0093   DataObject *pdo = kst_cast<DataObject>(_objectList.at(parent.row()));
0094   if (pdo) {
0095     pdo->readLock();
0096     rc = pdo->outputVectors().count();
0097     rc += pdo->outputMatrices().count();
0098     pdo->unlock();
0099   }
0100   return rc;
0101 }
0102 
0103 
0104 QVariant SessionModel::data(const QModelIndex& index, int role) const {
0105   if (!index.isValid()) {
0106     return QVariant();
0107   }
0108   if (role == Qt::UserRole) {
0109     if (index.parent().isValid()) {
0110       Q_ASSERT(!index.parent().parent().isValid());
0111       QVariant p = data(index.parent(), role);
0112       DataObjectPtr parent = p.value<DataObject*>();
0113       const int vectorCount = parent->outputVectors().count();
0114       if (index.row() < vectorCount) {
0115         if (VectorPtr v = parent->outputVectors().values()[index.row()]) {
0116           return qVariantFromValue(v.data());
0117         }
0118       } else if (MatrixPtr m = parent->outputMatrices().values()[index.row() - vectorCount]) {
0119         return qVariantFromValue(m.data());
0120       }
0121     } else {
0122       Q_ASSERT(_store);
0123       DataObjectPtr p = kst_cast<DataObject>(_objectList.at(index.row()));
0124       return qVariantFromValue(p.data());
0125     }
0126   }
0127 
0128   if (role != Qt::DisplayRole) {
0129     return QVariant();
0130   }
0131   if (index.internalPointer()) { //parent().isValid()) {
0132     Q_ASSERT(!index.parent().parent().isValid());
0133     DataObject *parent = static_cast<DataObject*>(index.internalPointer());
0134     return dataObjectOutputData(parent, index);
0135   }
0136 
0137   Q_ASSERT(_store);
0138   //ObjectList<Object> objectList = generateObjectList();
0139 
0140   const int row = index.row();
0141   if (row >= _objectList.count()) {
0142     return QVariant();
0143   }
0144 
0145   if (DataObjectPtr p = kst_cast<DataObject>(_objectList.at(row))) {
0146     return dataObjectData(p, index);
0147   } else if (RelationPtr p = kst_cast<Relation>(_objectList.at(row))) {
0148     return relationData(p, index);
0149   } else if (PrimitivePtr p=kst_cast<Primitive>(_objectList.at(row))) {
0150     return primitiveData(p, index);
0151   } else if (DataSourcePtr p=kst_cast<DataSource>(_objectList.at(row))) {
0152     return dataSourceData(p, index);
0153   } else {
0154     return QVariant();
0155   }
0156   return QVariant();
0157 }
0158 
0159 
0160 QVariant SessionModel::dataObjectOutputData(DataObjectPtr parent, const QModelIndex& index) const {
0161   QVariant rc;
0162 
0163   if (!parent) {
0164     return rc;
0165   }
0166 
0167   const int row = index.row();
0168   const int vectorCount = parent->outputVectors().count();
0169   if (parent->outputVectors().count() > row) {
0170     if (VectorPtr v = parent->outputVectors().values()[row]) {
0171       return primitiveData(v, index);
0172     }
0173   } else if (MatrixPtr m = parent->outputMatrices().values()[row - vectorCount]) {
0174     return primitiveData(m, index);
0175   }
0176   return rc;
0177 }
0178 
0179 
0180 QVariant SessionModel::primitiveData(PrimitivePtr p, const QModelIndex& index) const {
0181   QVariant rc;
0182 
0183   if (!p) {
0184     return rc;
0185   }
0186 
0187   p->readLock();
0188   switch (index.column()) {
0189     case 0:
0190       rc.setValue(p->Name());
0191       break;
0192     case 1:
0193       rc = p->typeString();
0194       break;
0195     case 2:
0196       rc = p->sizeString();
0197       break;
0198     case 3:
0199       rc = p->propertyString();
0200       break;
0201     default:
0202       break;
0203   }
0204   p->unlock();
0205   return rc;
0206 }
0207 
0208 QVariant SessionModel::dataSourceData(DataSourcePtr p, const QModelIndex& index) const {
0209   QVariant rc;
0210 
0211   if (!p) {
0212     return rc;
0213   }
0214 
0215   p->readLock();
0216   switch (index.column()) {
0217   case 0:
0218     rc.setValue(p->Name());
0219     break;
0220   case 1:
0221     rc = p->typeString();
0222     break;
0223     //case 2:
0224     //  rc = p->sizeString();
0225     //  break;
0226   case 3:
0227     if (p->updateType() == DataSource::Timer) {
0228       rc = "Timer update. " + p->fileName();
0229     } else if (p->updateType() == DataSource::File) {
0230       rc = "FileChange update. " + p->fileName();
0231     } else if (p->updateType() == DataSource::None) {
0232       rc = "No update. " + p->fileName();
0233     }
0234     break;
0235   default:
0236     break;
0237   }
0238   p->unlock();
0239   return rc;
0240 }
0241 
0242 QVariant SessionModel::dataObjectData(DataObjectPtr p, const QModelIndex& index) const {
0243   QVariant rc;
0244   if (!p) {
0245     return rc;
0246   }
0247 
0248   switch (index.column()) {
0249     case 0:
0250       p->readLock();
0251       rc.setValue(p->Name());
0252       p->unlock();
0253       break;
0254     case 1:
0255       p->readLock();
0256       rc = p->typeString();
0257       p->unlock();
0258       break;
0259     case 2:
0260       p->readLock();
0261       rc = p->sampleCount();
0262       p->unlock();
0263       break;
0264     case 3:
0265       p->readLock();
0266       rc = p->propertyString();
0267       p->unlock();
0268       break;
0269     default:
0270       break;
0271   }
0272   return rc;
0273 }
0274 
0275 
0276 QVariant SessionModel::relationData(RelationPtr p, const QModelIndex& index) const {
0277   QVariant rc;
0278   if (!p) {
0279     return rc;
0280   }
0281 
0282   switch (index.column()) {
0283     case 0:
0284       p->readLock();
0285       rc.setValue(p->Name());
0286       p->unlock();
0287       break;
0288     case 1:
0289       p->readLock();
0290       rc = p->typeString();
0291       p->unlock();
0292       break;
0293     case 2:
0294       p->readLock();
0295       rc = p->sampleCount();
0296       p->unlock();
0297       break;
0298     case 3:
0299       p->readLock();
0300       rc = p->propertyString();
0301       p->unlock();
0302       break;
0303     default:
0304       break;
0305   }
0306   return rc;
0307 }
0308 
0309 
0310 QModelIndex SessionModel::index(int row, int col, const QModelIndex& parent) const {
0311   if (row < 0 || col < 0 || col > 4) {
0312     return QModelIndex();
0313   }
0314 
0315   Q_ASSERT(_store);
0316   //ObjectList<Object> dol = generateObjectList();
0317 
0318   if (!parent.isValid()) {
0319     const int cnt = _objectList.count();
0320     if (row >= cnt) {
0321       return QModelIndex();
0322     }
0323     return createIndex(row, col);
0324   }
0325 
0326   const int cnt = _objectList.count();
0327   DataObject *p = 0;
0328   if (parent.row() >= 0 && parent.row() < cnt) { 
0329     p = kst_cast<DataObject>(_objectList.at(parent.row()));
0330   }
0331   if (!p) {
0332     return QModelIndex();
0333   }
0334 
0335   p->readLock();
0336   if (row >= (p->outputVectors().count() + p->outputMatrices().count())) {
0337     p->unlock();
0338     return QModelIndex();
0339   }
0340   p->unlock();
0341   return createIndex(row, col, p);
0342 }
0343 
0344 
0345 QModelIndex SessionModel::parent(const QModelIndex& index) const {
0346   // If it is a primitive and has a provider, return the index of that provider
0347 
0348   DataObject *dop = static_cast<DataObject*>(index.internalPointer());
0349   if (!dop) {
0350     return QModelIndex();
0351   }
0352 
0353   Q_ASSERT(_store);
0354   const int cnt = _objectList.indexOf(dop);
0355   if (cnt < 0) {
0356     return QModelIndex();
0357   }
0358   return createIndex(cnt, index.column());
0359 }
0360 
0361 
0362 QVariant SessionModel::headerData(int section, Qt::Orientation orientation, int role) const {
0363   if (role != Qt::DisplayRole) {
0364     return QAbstractItemModel::headerData(section, orientation, role);
0365   }
0366   switch (section) {
0367     case 0:
0368       return tr("Name");
0369     case 1:
0370       return tr("Type");
0371     case 2:
0372       return tr("Samples");
0373     case 3:
0374       return tr("Properties");
0375     default:
0376       break;
0377   }
0378   return QVariant();
0379 }
0380 
0381 }
0382 
0383 // vim: ts=2 sw=2 et