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

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 "matrixmodel.h"
0014 
0015 #include <assert.h>
0016 
0017 #include <QFont>
0018 
0019 namespace Kst {
0020 
0021 MatrixModel::MatrixModel(MatrixPtr m)
0022 : QAbstractItemModel(), _m(m) {
0023   assert(m.data());
0024 }
0025 
0026 
0027 MatrixModel::~MatrixModel() {
0028 }
0029 
0030 
0031 int MatrixModel::columnCount(const QModelIndex& parent) const {
0032   Q_UNUSED(parent)
0033   return _m->xNumSteps();
0034 }
0035 
0036 
0037 int MatrixModel::rowCount(const QModelIndex& parent) const {
0038   Q_UNUSED(parent)
0039   return _m->yNumSteps();
0040 }
0041 
0042 
0043 QVariant MatrixModel::data(const QModelIndex& index, int role) const {
0044   Q_UNUSED(role)
0045   QVariant rc;
0046   if (index.isValid()) {
0047     switch (role) {
0048       case Qt::DisplayRole:
0049         rc = QVariant(_m->value(index.column(), index.row()));
0050         break;
0051       case Qt::FontRole:
0052         {
0053           if (_m->editable()) {
0054             QFont f;
0055             f.setBold(true);
0056             rc = f;
0057           }
0058         }
0059         break;
0060       default:
0061         break;
0062     }
0063   }
0064   return rc;
0065 }
0066 
0067 
0068 QModelIndex MatrixModel::index(int row, int col, const QModelIndex& parent) const {
0069   Q_UNUSED(parent)
0070   if (row >= 0 && row < _m->yNumSteps() && col >= 0 && col < _m->xNumSteps()) {
0071     return createIndex(row, col);
0072   }
0073   return QModelIndex();
0074 }
0075 
0076 
0077 QModelIndex MatrixModel::parent(const QModelIndex& index) const {
0078   Q_UNUSED(index)
0079   return QModelIndex();
0080 }
0081 
0082 
0083 QVariant MatrixModel::headerData(int section, Qt::Orientation orientation, int role) const {
0084   return QAbstractItemModel::headerData(section, orientation, role);
0085 }
0086 
0087 
0088 Qt::ItemFlags MatrixModel::flags(const QModelIndex& index) const {
0089   Qt::ItemFlags f = QAbstractItemModel::flags(index);
0090   if (!index.isValid()) {
0091     return f;
0092   }
0093 
0094   if (_m->editable() && index.row() >= 0 && index.row() < _m->yNumSteps() && index.column() >= 0 && index.column() < _m->xNumSteps()) {
0095     f |= Qt::ItemIsEditable;
0096   }
0097 
0098   return f;
0099 }
0100 
0101 
0102 bool MatrixModel::setData(const QModelIndex& index, const QVariant& value, int role) {
0103   if (role != Qt::EditRole) {
0104     return QAbstractItemModel::setData(index, value, role);
0105   }
0106 
0107   if (!index.isValid() || !_m->editable() || index.row() < 0 || index.row() >= _m->yNumSteps() || index.column() < 0 || index.column() >= _m->xNumSteps()) {
0108     return false;
0109   }
0110 
0111   bool ok = false;
0112   double v = value.toDouble(&ok);
0113   if (!ok) {
0114     return false;
0115   }
0116 
0117   return _m->setValue(index.column(), index.row(), v);
0118 }
0119 
0120 
0121 }
0122 
0123 // vim: ts=2 sw=2 et