TabularModel.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2008 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_TABULAR_MODEL_H
17 #define SV_TABULAR_MODEL_H
18 
19 #include <QVariant>
20 #include <QString>
21 
22 #include "base/RealTime.h"
23 
24 class Command;
25 
36 {
37 public:
38  virtual ~TabularModel() { }
39 
43  virtual int getRowCount() const = 0;
44 
48  virtual int getColumnCount() const = 0;
49 
55  virtual QString getHeading(int column) const = 0;
56 
57  enum { SortRole = Qt::UserRole };
59 
64  virtual QVariant getData(int row, int column, int role) const = 0;
65 
71  virtual bool isColumnTimeValue(int col) const = 0;
72 
76  virtual SortType getSortType(int col) const = 0;
77 
81  virtual sv_frame_t getFrameForRow(int row) const = 0;
82 
87  virtual int getRowForFrame(sv_frame_t frame) const = 0;
88 
92  virtual bool isEditable() const = 0;
93 
101  virtual Command *getSetDataCommand(int row, int column,
102  const QVariant &, int role) = 0;
103 
111  virtual Command *getInsertRowCommand(int beforeRow) = 0;
112 
119  virtual Command *getRemoveRowCommand(int row) = 0;
120 
121 protected:
122  // Helpers
123 
124  static QVariant adaptFrameForRole(sv_frame_t frame,
125  sv_samplerate_t rate,
126  int role) {
127  if (role == SortRole) return int(frame);
128  RealTime rt = RealTime::frame2RealTime(frame, rate);
129  if (role == Qt::EditRole) return rt.toString().c_str();
130  else return rt.toText().c_str();
131  }
132 
133  static QVariant adaptValueForRole(float value,
134  QString unit,
135  int role) {
136  if (role == SortRole || role == Qt::EditRole) return value;
137  else return QString("%1 %2").arg(value).arg(unit);
138  }
139 };
140 
141 #endif
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
virtual bool isEditable() const =0
Return true if the model is user-editable, false otherwise.
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTimeSV.cpp:498
virtual int getRowCount() const =0
Return the number of rows (items) in the model.
virtual sv_frame_t getFrameForRow(int row) const =0
Return the frame time for the given row.
static QVariant adaptFrameForRole(sv_frame_t frame, sv_samplerate_t rate, int role)
Definition: TabularModel.h:124
virtual Command * getInsertRowCommand(int beforeRow)=0
Return a command to insert a new row before the row with the given index.
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTimeSV.cpp:213
virtual Command * getRemoveRowCommand(int row)=0
Return a command to delete the row with the given index.
static QVariant adaptValueForRole(float value, QString unit, int role)
Definition: TabularModel.h:133
virtual SortType getSortType(int col) const =0
Return the sort type (numeric or alphabetical) for the column.
virtual int getRowForFrame(sv_frame_t frame) const =0
Return the number of the first row whose frame time is not less than the given one.
virtual Command * getSetDataCommand(int row, int column, const QVariant &, int role)=0
Return a command to set the value in the given cell, for the given role, to the contents of the suppl...
TabularModel is an abstract base class for models that support direct access to data in a tabular for...
Definition: TabularModel.h:35
virtual QVariant getData(int row, int column, int role) const =0
Get the value in the given cell, for the given role.
virtual QString getHeading(int column) const =0
Return the heading for a given column, e.g.
virtual ~TabularModel()
Definition: TabularModel.h:38
virtual int getColumnCount() const =0
Return the number of columns (values/labels/etc per item).
std::string toText(bool fixedDp=false) const
Return a user-readable string to the nearest millisecond, typically in a form like HH:MM:SS...
Definition: RealTimeSV.cpp:274
virtual bool isColumnTimeValue(int col) const =0
Return true if the column is the frame time of the item, or an alternative representation of it (i...
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42