view data/model/IntervalModel.h @ 282:d9319859a4cf tip

(none)
author benoitrigolleau
date Fri, 31 Oct 2008 11:00:24 +0000
parents 82d17c4d4b39
children
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sound Access
	EASAIER client application.
	Silogic 2007. Luc Barthélémy.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _INTERVAL_MODEL_H_
#define _INTERVAL_MODEL_H_
 
#include <QMutex>

#include "Model.h"
#include "base/RealTime.h"
#include "base/Command.h"
#include "base/CommandHistory.h"

/**
 * TimeInterval SparseValueModel
 */

class TimeInterval
{
public:
    
	TimeInterval(long start = 0, long end = 0, float value = 0.0f);
	TimeInterval(long start, long end, const QString& label, float value = 0.0f);
	virtual ~TimeInterval();
   
	QString toXmlString(QString indent = "", QString extraAttributes = "") const;
    QString toDelimitedDataString(const QString& delimiter, size_t sampleRate) const;

	long start() const {return m_start;}
	long end() const {return m_end;}
	float value() const {return m_value;}
	const QString& label() const { return m_label; }

	void start(long start) { m_start = start;}
	void end(long end) { m_end = end;}
	void label(const QString& label) { m_label = label;}
	void value(float val) { m_value = val;}

private:
    
	long	m_start;
    long	m_end;
	float	m_value;

	QString	m_label;
};

typedef TimeInterval* TimeIntervalPtr;

typedef std::list<TimeIntervalPtr> IntervalList;
typedef IntervalList::iterator IntervalListIterator;
typedef IntervalList::const_iterator IntervalListConstIterator;

/**
 * IntervalModel
 */

class IntervalModel : public Model
{
public:

    IntervalModel(size_t sampleRate, size_t resolution,
	      bool notifyOnAdd = true);
	~IntervalModel();


    virtual bool isOK() const { return true; }
    virtual size_t getStartFrame() const;
    virtual size_t getEndFrame() const;

	virtual size_t getSampleRate() const { return m_sampleRate; }
    virtual size_t getResolution() const { return m_resolution; }

    virtual Model *clone() const;
  
	virtual void toXml(QTextStream &out,
                       QString indent = "",
                       QString extraAttributes = "") const;
	virtual QString toXmlString(QString indent = "", QString extraAttributes = "") const;
    virtual QString toDelimitedDataString(QString) const;

	virtual void addInterval(long start, long end, const QString& label, float value);
	virtual void addInterval(TimeIntervalPtr ti);

	virtual void removeInterval(TimeIntervalPtr ti);

	IntervalList& intervals() { return m_intervals; }

	void changeInterval(TimeIntervalPtr ti, long start, long end, float value, const QString& label);

	inline float getMeanValue() {return m_meanValue;};
	inline void setMeanValue(float value) {m_meanValue = value;};
   /**
     * Command to change an interval.
     */
    class IntervalCommand : public Command
    {
	    public:
			
			enum Mode {
				Creation,
				Edition,
				Deletion,
			};

			IntervalCommand(IntervalModel* model,
				   const TimeIntervalPtr& inter,
				   Mode mode,
				   long newStart,
				   long newEnd,
				   float newValue,
				   const QString& newLabel) :
			m_model(model), m_interval(inter), m_mode(mode)
			{
				m_oldLabel = inter->label();
				m_oldStart = inter->start();
				m_oldEnd   = inter->end();
				m_oldValue = inter->value();
				m_newLabel = newLabel;
				m_newStart = newStart;
				m_newEnd   = newEnd;
				m_newValue = newValue;
			}

			virtual QString getName() const { return tr("Edit Interval"); }

			virtual void execute() 
			{ 
				if (m_mode == Creation)
				{
					m_model->addInterval(m_interval);
				} else if (m_mode == Edition)
				{
					m_model->changeInterval(m_interval, m_newStart, m_newEnd, m_newValue, m_newLabel);
				} else if (m_mode == Deletion)
				{
					m_model->removeInterval(m_interval);
				}
			}

			virtual void unexecute() 
			{ 
				if (m_mode == Creation)
				{
					m_model->removeInterval(m_interval);
				} else if (m_mode == Edition)
				{
					m_model->changeInterval(m_interval, m_oldStart, m_oldEnd, m_oldValue, m_oldLabel);
				} else if (m_mode == Deletion)
				{
					m_model->addInterval(m_interval);
				}
			}

			void newStart(long start) 
			{ 
				if (m_newStart != start)
				{	
					m_newStart  = start; 
					m_model->changeInterval(m_interval, m_newStart, m_newEnd, m_newValue, m_newLabel);
				}
			}

			void newEnd(long end)
			{
				if (m_newEnd != end)
				{	
					m_newEnd  = end; 
					m_model->changeInterval(m_interval, m_newStart, m_newEnd, m_newValue, m_newLabel);
				}
			}

			void newLabel(QString label)
			{
				if (m_newLabel != label)
				{	
					m_newLabel  = label; 
					m_model->changeInterval(m_interval, m_newStart, m_newEnd, m_newValue, m_newLabel);
				}
			}

	private:

			Mode			m_mode;

			IntervalModel*	m_model;
			TimeIntervalPtr	m_interval;
			QString			m_newLabel;
			QString			m_oldLabel;
			long			m_newStart;
			long			m_oldStart;
			long			m_newEnd;
			long			m_oldEnd;
			float			m_newValue;
			float			m_oldValue;
    };


private:

	size_t	m_sampleRate;
	size_t	m_resolution;
	bool	m_notifyOnAdd;

	IntervalList	m_intervals; 
	mutable QMutex	m_mutex;

	float			m_meanValue;

};

#endif // _INTERVAL_MODEL_H_