File indexing completed on 2024-12-22 04:17:44

0001 /***************************************************************************
0002 *                                                                         *
0003 *   copyright : (C) 2011 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 "primitivemodel.h"
0014 
0015 #include <assert.h>
0016 #include <objectstore.h>
0017 #include <dataobject.h>
0018 #include <datavector.h>
0019 #include <generatedvector.h>
0020 #include <datamatrix.h>
0021 #include <generatedmatrix.h>
0022 #include <datasource.h>
0023 #include <QFileInfo>
0024 
0025 namespace Kst {
0026 
0027 PrimitiveTreeItem::PrimitiveTreeItem(const QList<QVariant> &data, PrimitiveTreeItem *parent) {
0028   parentItem = parent;
0029   itemData = data; 
0030   if (parent) {
0031     parent->addChild(this);
0032   }
0033 }
0034 
0035 
0036 PrimitiveTreeItem::~PrimitiveTreeItem() {
0037   qDeleteAll(childItems);
0038 }
0039 
0040 
0041 void PrimitiveTreeItem::addChild(PrimitiveTreeItem *item) {
0042   childItems.append(item);
0043 }
0044 
0045 
0046 PrimitiveTreeItem *PrimitiveTreeItem::child(int row) {
0047   return childItems.value(row);
0048 }
0049 
0050 
0051 int PrimitiveTreeItem::childCount() const {
0052   return childItems.count();
0053 }
0054 
0055 
0056 int PrimitiveTreeItem::row() const {
0057   if (parentItem)
0058     return parentItem->childItems.indexOf(const_cast<PrimitiveTreeItem*>(this));
0059 
0060   return 0;
0061 }
0062 
0063 
0064 int PrimitiveTreeItem::columnCount() const {
0065   return itemData.count();
0066 }
0067 
0068 
0069 QVariant PrimitiveTreeItem::data(int column) const {
0070   return itemData.value(column);
0071 }
0072 
0073 
0074 PrimitiveTreeItem *PrimitiveTreeItem::parent() {
0075   return parentItem;
0076 }
0077 
0078 
0079 PrimitiveModel::PrimitiveModel(ObjectStore *store)
0080   : QAbstractItemModel(), _store(store) {
0081     QList<QVariant> rootData;
0082     rootData << "Scalars";
0083     _rootItem = new PrimitiveTreeItem(rootData);
0084 }
0085 
0086 
0087 PrimitiveModel::~PrimitiveModel() {
0088 }
0089 
0090 
0091 int PrimitiveModel::columnCount(const QModelIndex& parent) const {
0092   Q_UNUSED(parent)
0093     return 2;
0094 }
0095 
0096 
0097 PrimitiveTreeItem* PrimitiveModel::addPrimitiveTreeItem(const QList<QVariant>& data, PrimitiveTreeItem* parent) {
0098   PrimitiveTreeItem* parentItem;
0099   if (parent) {
0100     parentItem = parent;
0101   } else {
0102     parentItem = _rootItem;
0103   }
0104   return new PrimitiveTreeItem(data, parentItem);
0105 }
0106 
0107 
0108 int PrimitiveModel::rowCount(const QModelIndex& parent) const {
0109   PrimitiveTreeItem *parentItem;
0110   if (parent.column() > 0)
0111     return 0;
0112 
0113   if (!parent.isValid())
0114     parentItem = _rootItem;
0115   else
0116     parentItem = static_cast<PrimitiveTreeItem*>(parent.internalPointer());
0117 
0118   return parentItem->childCount();
0119 }
0120 
0121 
0122 QVariant PrimitiveModel::data(const QModelIndex& index, int role) const {
0123   if (!index.isValid()) {
0124     return QVariant();
0125   }
0126 
0127   if (role != Qt::DisplayRole) {
0128     return QVariant();
0129   }
0130 
0131   PrimitiveTreeItem *item = static_cast<PrimitiveTreeItem*>(index.internalPointer());
0132 
0133   return item->data(index.column());
0134 }
0135 
0136 
0137 QModelIndex PrimitiveModel::index(int row, int col, const QModelIndex& parent) const {
0138   if (row < 0 || col < 0 || col > 1) {
0139     return QModelIndex();
0140   }
0141 
0142   if (!hasIndex(row, col, parent))
0143     return QModelIndex();
0144 
0145   PrimitiveTreeItem *parentItem;
0146 
0147   if (!parent.isValid())
0148     parentItem = _rootItem;
0149   else
0150     parentItem = static_cast<PrimitiveTreeItem*>(parent.internalPointer());
0151 
0152   PrimitiveTreeItem *childItem = parentItem->child(row);
0153   if (childItem)
0154     return createIndex(row, col, childItem);
0155   else
0156     return QModelIndex();
0157 }
0158 
0159 
0160 QModelIndex PrimitiveModel::parent(const QModelIndex& index) const {
0161   Q_ASSERT(_store);
0162 
0163   if (!index.isValid())
0164     return QModelIndex();
0165 
0166   PrimitiveTreeItem *childItem = static_cast<PrimitiveTreeItem*>(index.internalPointer());
0167   PrimitiveTreeItem *parentItem = childItem->parent();
0168 
0169   if (parentItem == _rootItem)
0170     return QModelIndex();
0171 
0172   return createIndex(parentItem->row(), 0, parentItem);
0173 }
0174 
0175 
0176 QVariant PrimitiveModel::headerData(int section, Qt::Orientation orientation, int role) const {
0177   if (role != Qt::DisplayRole) {
0178     return QAbstractItemModel::headerData(section, orientation, role);
0179   }
0180   switch (section) {
0181   case Name:
0182     return tr("Name");
0183   case Value:
0184     return tr("Value");
0185   default:
0186     break;
0187   }
0188   return QVariant();
0189 }
0190 
0191 }
0192 
0193 // vim: ts=2 sw=2 et