annotate data/model/TabularModel.h @ 1859:4c5736a517e0

Avoid more recent Qt api, for older platform package builds
author Chris Cannam
date Mon, 18 May 2020 14:06:30 +0100
parents 343ef2a866a4
children
rev   line source
Chris@420 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@420 2
Chris@420 3 /*
Chris@420 4 Sonic Visualiser
Chris@420 5 An audio file viewer and annotation editor.
Chris@420 6 Centre for Digital Music, Queen Mary, University of London.
Chris@420 7 This file copyright 2008 QMUL.
Chris@420 8
Chris@420 9 This program is free software; you can redistribute it and/or
Chris@420 10 modify it under the terms of the GNU General Public License as
Chris@420 11 published by the Free Software Foundation; either version 2 of the
Chris@420 12 License, or (at your option) any later version. See the file
Chris@420 13 COPYING included with this distribution for more information.
Chris@420 14 */
Chris@420 15
Chris@1581 16 #ifndef SV_TABULAR_MODEL_H
Chris@1581 17 #define SV_TABULAR_MODEL_H
Chris@420 18
Chris@420 19 #include <QVariant>
Chris@420 20 #include <QString>
Chris@420 21
Chris@1643 22 #include "base/RealTime.h"
Chris@1643 23
Chris@420 24 class Command;
Chris@420 25
Chris@420 26 /**
Chris@420 27 * TabularModel is an abstract base class for models that support
Chris@420 28 * direct access to data in a tabular form. A model that implements
Chris@420 29 * TabularModel may be displayed and, perhaps, edited in a data
Chris@420 30 * spreadsheet window.
Chris@420 31 *
Chris@420 32 * This is very like a cut-down QAbstractItemModel. It assumes a
Chris@420 33 * relationship between row number and frame time.
Chris@420 34 */
Chris@420 35 class TabularModel
Chris@420 36 {
Chris@420 37 public:
Chris@462 38 virtual ~TabularModel() { }
Chris@462 39
Chris@1804 40 /**
Chris@1804 41 * Return the number of rows (items) in the model.
Chris@1804 42 */
Chris@420 43 virtual int getRowCount() const = 0;
Chris@1804 44
Chris@1804 45 /**
Chris@1804 46 * Return the number of columns (values/labels/etc per item).
Chris@1804 47 */
Chris@420 48 virtual int getColumnCount() const = 0;
Chris@420 49
Chris@1804 50 /**
Chris@1804 51 * Return the heading for a given column, e.g. "Time" or "Value".
Chris@1804 52 * These are shown directly to the user, so must be translated
Chris@1804 53 * already.
Chris@1804 54 */
Chris@420 55 virtual QString getHeading(int column) const = 0;
Chris@422 56
Chris@422 57 enum { SortRole = Qt::UserRole };
Chris@422 58 enum SortType { SortNumeric, SortAlphabetical };
Chris@422 59
Chris@1804 60 /**
Chris@1804 61 * Get the value in the given cell, for the given role. The role
Chris@1804 62 * is actually a Qt::ItemDataRole.
Chris@1804 63 */
Chris@420 64 virtual QVariant getData(int row, int column, int role) const = 0;
Chris@1804 65
Chris@1804 66 /**
Chris@1804 67 * Return true if the column is the frame time of the item, or an
Chris@1804 68 * alternative representation of it (i.e. anything that has the
Chris@1804 69 * same sort order). Duration is not a time value by this meaning.
Chris@1804 70 */
Chris@422 71 virtual bool isColumnTimeValue(int col) const = 0;
Chris@1804 72
Chris@1804 73 /**
Chris@1804 74 * Return the sort type (numeric or alphabetical) for the column.
Chris@1804 75 */
Chris@422 76 virtual SortType getSortType(int col) const = 0;
Chris@420 77
Chris@1804 78 /**
Chris@1804 79 * Return the frame time for the given row.
Chris@1804 80 */
Chris@1055 81 virtual sv_frame_t getFrameForRow(int row) const = 0;
Chris@1804 82
Chris@1804 83 /**
Chris@1804 84 * Return the number of the first row whose frame time is not less
Chris@1804 85 * than the given one. If there is none, return getRowCount().
Chris@1804 86 */
Chris@1055 87 virtual int getRowForFrame(sv_frame_t frame) const = 0;
Chris@420 88
Chris@1804 89 /**
Chris@1804 90 * Return true if the model is user-editable, false otherwise.
Chris@1804 91 */
Chris@1804 92 virtual bool isEditable() const = 0;
Chris@1643 93
Chris@1804 94 /**
Chris@1804 95 * Return a command to set the value in the given cell, for the
Chris@1804 96 * given role, to the contents of the supplied variant.
Chris@1804 97 *
Chris@1804 98 * If the model is not editable or the cell or value is out of
Chris@1804 99 * range, return nullptr.
Chris@1804 100 */
Chris@1804 101 virtual Command *getSetDataCommand(int row, int column,
Chris@1804 102 const QVariant &, int role) = 0;
Chris@1804 103
Chris@1804 104 /**
Chris@1804 105 * Return a command to insert a new row before the row with the
Chris@1804 106 * given index.
Chris@1804 107 *
Chris@1804 108 * If the model is not editable or the index is out of range,
Chris@1804 109 * return nullptr.
Chris@1804 110 */
Chris@1804 111 virtual Command *getInsertRowCommand(int beforeRow) = 0;
Chris@1804 112
Chris@1804 113 /**
Chris@1804 114 * Return a command to delete the row with the given index.
Chris@1804 115 *
Chris@1804 116 * If the model is not editable or the index is out of range,
Chris@1804 117 * return nullptr.
Chris@1804 118 */
Chris@1804 119 virtual Command *getRemoveRowCommand(int row) = 0;
Chris@1804 120
Chris@1804 121 protected:
Chris@1804 122 // Helpers
Chris@1804 123
Chris@1804 124 static QVariant adaptFrameForRole(sv_frame_t frame,
Chris@1804 125 sv_samplerate_t rate,
Chris@1804 126 int role) {
Chris@1643 127 if (role == SortRole) return int(frame);
Chris@1643 128 RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@1643 129 if (role == Qt::EditRole) return rt.toString().c_str();
Chris@1643 130 else return rt.toText().c_str();
Chris@1643 131 }
Chris@1643 132
Chris@1804 133 static QVariant adaptValueForRole(float value,
Chris@1804 134 QString unit,
Chris@1804 135 int role) {
Chris@1643 136 if (role == SortRole || role == Qt::EditRole) return value;
Chris@1643 137 else return QString("%1 %2").arg(value).arg(unit);
Chris@1643 138 }
Chris@420 139 };
Chris@420 140
Chris@420 141 #endif