changeset 427:72ec275e458b

* Basic implementation of add and remove point in data editor * Improve resilience of frame - real-time - frame round-trip conversions
author Chris Cannam
date Mon, 16 Jun 2008 14:48:42 +0000
parents 2386582f67cd
children 3e1d190048f4
files base/RealTime.cpp data/model/ModelDataTableModel.cpp data/model/ModelDataTableModel.h data/model/SparseModel.h data/model/SparseOneDimensionalModel.h data/model/TabularModel.h
diffstat 6 files changed, 68 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/base/RealTime.cpp	Mon Jun 16 07:55:35 2008 +0000
+++ b/base/RealTime.cpp	Mon Jun 16 14:48:42 2008 +0000
@@ -304,17 +304,8 @@
 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
 {
     if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
-
-    // We like integers.  The last term is always zero unless the
-    // sample rate is greater than 1MHz, but hell, you never know...
-
-    long frame =
-	time.sec * sampleRate +
-	(time.msec() * sampleRate) / 1000 +
-	((time.usec() - 1000 * time.msec()) * sampleRate) / 1000000 +
-	((time.nsec - 1000 * time.usec()) * sampleRate) / 1000000000;
-
-    return frame;
+    double s = time.sec + double(time.nsec + 1) / 1000000000.0;
+    return long(s * sampleRate);
 }
 
 RealTime
@@ -325,7 +316,7 @@
     RealTime rt;
     rt.sec = frame / long(sampleRate);
     frame -= rt.sec * long(sampleRate);
-    rt.nsec = (int)(((float(frame) * 1000000) / long(sampleRate)) * 1000);
+    rt.nsec = (int)(((double(frame) * 1000000.0) / long(sampleRate)) * 1000.0);
     return rt;
 }
 
--- a/data/model/ModelDataTableModel.cpp	Mon Jun 16 07:55:35 2008 +0000
+++ b/data/model/ModelDataTableModel.cpp	Mon Jun 16 14:48:42 2008 +0000
@@ -50,19 +50,53 @@
 ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
 {
     if (!index.isValid()) return false;
-    std::cerr << "ModelDataTableModel::setData(" << index.row() << ", " << index.column() << ", " << value.toString().toStdString() << ", " << role << ")" << std::endl;
     Command *command = m_model->getSetDataCommand(getUnsorted(index.row()),
                                                   index.column(),
                                                   value, role);
     if (command) {
-        std::cerr << "emitting executeCommand" << std::endl;
-        emit executeCommand(command);
+        emit addCommand(command);
         return true;
     } else {
         return false;
     }
 }
 
+bool
+ModelDataTableModel::insertRow(int row, const QModelIndex &parent)
+{
+    if (parent.isValid()) return false;
+
+    emit beginInsertRows(parent, row, row);
+
+    Command *command = m_model->getInsertRowCommand(getUnsorted(row));
+
+    if (command) {
+        emit addCommand(command);
+    }
+
+    emit endInsertRows();
+
+    return (command ? true : false);
+}
+
+bool
+ModelDataTableModel::removeRow(int row, const QModelIndex &parent)
+{
+    if (parent.isValid()) return false;
+
+    emit beginRemoveRows(parent, row, row);
+
+    Command *command = m_model->getRemoveRowCommand(getUnsorted(row));
+
+    if (command) {
+        emit addCommand(command);
+    }
+
+    emit endRemoveRows();
+
+    return (command ? true : false);
+}
+
 Qt::ItemFlags
 ModelDataTableModel::flags(const QModelIndex &index) const
 {
--- a/data/model/ModelDataTableModel.h	Mon Jun 16 07:55:35 2008 +0000
+++ b/data/model/ModelDataTableModel.h	Mon Jun 16 14:48:42 2008 +0000
@@ -35,6 +35,9 @@
 
     bool setData(const QModelIndex &index, const QVariant &value, int role);
 
+    bool insertRow(int row, const QModelIndex &parent = QModelIndex());
+    bool removeRow(int row, const QModelIndex &parent = QModelIndex());
+
     Qt::ItemFlags flags(const QModelIndex &index) const;
 
     QVariant headerData(int section, Qt::Orientation orientation,
@@ -55,7 +58,7 @@
 
 signals:
     void frameSelected(size_t);
-    void executeCommand(Command *);
+    void addCommand(Command *);
 
 protected slots:
     void modelChanged();
--- a/data/model/SparseModel.h	Mon Jun 16 07:55:35 2008 +0000
+++ b/data/model/SparseModel.h	Mon Jun 16 14:48:42 2008 +0000
@@ -307,6 +307,7 @@
 
         return QVariant();
     }
+
     virtual Command *getSetDataCommand(int row, int column,
                                        const QVariant &value, int role)
     {
@@ -327,6 +328,26 @@
         return command->finish();
     }
 
+    virtual Command *getInsertRowCommand(int row)
+    {
+        EditCommand *command = new EditCommand(this, tr("Insert Data Point"));
+        Point point(0);
+        PointListIterator i = getPointListIteratorForRow(row);
+        if (i == m_points.end() && i != m_points.begin()) --i;
+        if (i != m_points.end()) point = *i;
+        command->addPoint(point);
+        return command->finish();
+    }
+            
+    virtual Command *getRemoveRowCommand(int row)
+    {
+        EditCommand *command = new EditCommand(this, tr("Delete Data Point"));
+        PointListIterator i = getPointListIteratorForRow(row);
+        if (i == m_points.end()) return 0;
+        command->deletePoint(*i);
+        return command->finish();
+    }
+            
 protected:
     size_t m_sampleRate;
     size_t m_resolution;
--- a/data/model/SparseOneDimensionalModel.h	Mon Jun 16 07:55:35 2008 +0000
+++ b/data/model/SparseOneDimensionalModel.h	Mon Jun 16 14:48:42 2008 +0000
@@ -169,6 +169,7 @@
         return command->finish();
     }
 
+
     virtual bool isColumnTimeValue(int column) const
     {
         return (column < 2); 
--- a/data/model/TabularModel.h	Mon Jun 16 07:55:35 2008 +0000
+++ b/data/model/TabularModel.h	Mon Jun 16 14:48:42 2008 +0000
@@ -51,6 +51,8 @@
 
     virtual bool isEditable() const { return false; }
     virtual Command *getSetDataCommand(int /* row */, int /* column */, const QVariant &, int /* role */) { return 0; }
+    virtual Command *getInsertRowCommand(int /* beforeRow */) { return 0; }
+    virtual Command *getRemoveRowCommand(int /* row */) { return 0; }
 };
 
 #endif