Chris@420: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@420: 
Chris@420: /*
Chris@420:     Sonic Visualiser
Chris@420:     An audio file viewer and annotation editor.
Chris@420:     Centre for Digital Music, Queen Mary, University of London.
Chris@420:     This file copyright 2008 QMUL.
Chris@420:     
Chris@420:     This program is free software; you can redistribute it and/or
Chris@420:     modify it under the terms of the GNU General Public License as
Chris@420:     published by the Free Software Foundation; either version 2 of the
Chris@420:     License, or (at your option) any later version.  See the file
Chris@420:     COPYING included with this distribution for more information.
Chris@420: */
Chris@420: 
Chris@1581: #ifndef SV_TABULAR_MODEL_H
Chris@1581: #define SV_TABULAR_MODEL_H
Chris@420: 
Chris@420: #include <QVariant>
Chris@420: #include <QString>
Chris@420: 
Chris@1643: #include "base/RealTime.h"
Chris@1643: 
Chris@420: class Command;
Chris@420: 
Chris@420: /**
Chris@420:  * TabularModel is an abstract base class for models that support
Chris@420:  * direct access to data in a tabular form.  A model that implements
Chris@420:  * TabularModel may be displayed and, perhaps, edited in a data
Chris@420:  * spreadsheet window.
Chris@420:  *
Chris@420:  * This is very like a cut-down QAbstractItemModel.  It assumes a
Chris@420:  * relationship between row number and frame time.
Chris@420:  */
Chris@420: 
Chris@420: class TabularModel
Chris@420: {
Chris@420: public:
Chris@462:     virtual ~TabularModel() { }
Chris@462: 
Chris@420:     virtual int getRowCount() const = 0;
Chris@420:     virtual int getColumnCount() const = 0;
Chris@420: 
Chris@420:     virtual QString getHeading(int column) const = 0;
Chris@422: 
Chris@422:     enum { SortRole = Qt::UserRole };
Chris@422:     enum SortType { SortNumeric, SortAlphabetical };
Chris@422: 
Chris@420:     virtual QVariant getData(int row, int column, int role) const = 0;
Chris@422:     virtual bool isColumnTimeValue(int col) const = 0;
Chris@422:     virtual SortType getSortType(int col) const = 0;
Chris@420: 
Chris@1055:     virtual sv_frame_t getFrameForRow(int row) const = 0;
Chris@1055:     virtual int getRowForFrame(sv_frame_t frame) const = 0;
Chris@420: 
Chris@420:     virtual bool isEditable() const { return false; }
Chris@425:     virtual Command *getSetDataCommand(int /* row */, int /* column */, const QVariant &, int /* role */) { return 0; }
Chris@427:     virtual Command *getInsertRowCommand(int /* beforeRow */) { return 0; }
Chris@427:     virtual Command *getRemoveRowCommand(int /* row */) { return 0; }
Chris@1643: 
Chris@1643:     QVariant adaptFrameForRole(sv_frame_t frame,
Chris@1643:                                sv_samplerate_t rate,
Chris@1643:                                int role) const {
Chris@1643:         if (role == SortRole) return int(frame);
Chris@1643:         RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@1643:         if (role == Qt::EditRole) return rt.toString().c_str();
Chris@1643:         else return rt.toText().c_str();
Chris@1643:     }
Chris@1643: 
Chris@1643:     QVariant adaptValueForRole(float value,
Chris@1643:                                QString unit,
Chris@1643:                                int role) const {
Chris@1643:         if (role == SortRole || role == Qt::EditRole) return value;
Chris@1643:         else return QString("%1 %2").arg(value).arg(unit);
Chris@1643:     }
Chris@420: };
Chris@420: 
Chris@420: #endif