PDA

View Full Version : need design suggestion



ModeZt
19th January 2008, 02:58
I whant a plot that will show a nuber of curves with the ability to add/remove curves.
The first idea i got was to use a QHash to store information about attached curves. So this is an overview:

class Plotter: public QwtPlot
{
public:
Plotter( QObject parent = 0 );
~Plotter();
bool addCurve( QString curveId, QwtData *data );
bool removeCurve( QString curveId );
const QwtPlotCurve* curve( QString curveId );
const QwtData* data( QString curveId );
private:
QMultiHash<QString, QwtPlotCurve*, QwtData* > curveHash;
};

bool Plotter::addCurve( QString curveId, QwtData *data ) {
if( !curveHash.contains( curveId ) ) return false;
QwtPlotCurve * curve = new QwtPlotCurve;
curve->setData(*data);
curve->attach(this);
curveHash.insert( curveId, curve, data );
return true;
}

bool Plotter::removeCurve( QString curveId ) {
if( !curveHash.contains( curveId ) ) return false;
curveHash.value( curveId ).curve->detach(this);
delete curveHash.value( curveId ).curve;
delete curveHash.value( curveId ).data;
curveHash.remove( curveId );
return true;
}
I like this code, but maybe there's a better design.