From bff4fcb257295b5b032eed25153b3eb510d14a7f Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Fri, 16 Feb 2007 20:12:31 +0000 Subject: [PATCH] API change in the plot objects handling: - add plotObjects() to get the list of the plot objects (thus removing objectCount() ) - add addPlotObjects() to add more objects with a single update - add 'Plot' in the name of the other functions that work with the plot objects - finally hide the real object list in the Private class svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=634293 --- kdeeduplot/kplotwidget.cpp | 62 +++++++++++++++++----------- kdeeduplot/kplotwidget.h | 32 +++++++------- kdeeduplot/tests/testplot_widget.cpp | 16 +++---- 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/kdeeduplot/kplotwidget.cpp b/kdeeduplot/kplotwidget.cpp index e4a1e4d..ea722fa 100644 --- a/kdeeduplot/kplotwidget.cpp +++ b/kdeeduplot/kplotwidget.cpp @@ -62,6 +62,7 @@ class KPlotWidget::Private Private() { + qDeleteAll( objectList ); qDeleteAll( axes ); } @@ -75,6 +76,8 @@ class KPlotWidget::Private int leftPadding, rightPadding, topPadding, bottomPadding; // hashmap with the axes we have QHash axes; + // List of KPlotObjects + QList objectList; }; KPlotWidget::KPlotWidget( QWidget *parent, double x1, double x2, double y1, double y2 ) @@ -109,9 +112,6 @@ KPlotWidget::KPlotWidget( QWidget * parent ) KPlotWidget::~KPlotWidget() { - qDeleteAll( ObjectList ); - ObjectList.clear(); - delete d; } @@ -183,17 +183,41 @@ void KPlotWidget::clearSecondaryLimits() { update(); } -void KPlotWidget::addObject( KPlotObject *o ) { +void KPlotWidget::addPlotObject( KPlotObject *o ) { // skip null pointers if ( !o ) return; - ObjectList.append( o ); + d->objectList.append( o ); update(); } -void KPlotWidget::clearObjectList() { - qDeleteAll( ObjectList ); - ObjectList.clear(); - update(); +void KPlotWidget::addPlotObjects( const QList< KPlotObject* >& objects ) +{ + bool addedsome = false; + foreach ( KPlotObject *o, objects ) + { + if ( !o ) + continue; + + d->objectList.append( o ); + addedsome = true; + } + if ( addedsome ) + update(); +} + +QList< KPlotObject* > KPlotWidget::plotObjects() const +{ + return d->objectList; +} + +void KPlotWidget::removeAllPlotObjects() +{ + if ( d->objectList.isEmpty() ) + return; + + qDeleteAll( d->objectList ); + d->objectList.clear(); + update(); } void KPlotWidget::resetPlotMask() { @@ -203,7 +227,8 @@ void KPlotWidget::resetPlotMask() { } void KPlotWidget::resetPlot() { - clearObjectList(); + qDeleteAll( d->objectList ); + d->objectList.clear(); clearSecondaryLimits(); setLimits(0.0, 1.0, 0.0, 1.0); axis(KPlotWidget::RightAxis)->setShowTickLabels( false ); @@ -215,22 +240,13 @@ void KPlotWidget::resetPlot() { resetPlotMask(); } -void KPlotWidget::replaceObject( int i, KPlotObject *o ) { +void KPlotWidget::replacePlotObject( int i, KPlotObject *o ) { // skip null pointers if ( !o ) return; - ObjectList.replace( i, o ); + d->objectList.replace( i, o ); update(); } - -KPlotObject *KPlotWidget::object( int i ) { - if ( i < 0 || i >= ObjectList.count() ) { - kWarning() << "KPlotWidget::object(): index " << i << " out of range!" << endl; - return 0; - } - return ObjectList.at(i); -} - QColor KPlotWidget::backgroundColor() const { return d->cBackground; @@ -308,7 +324,7 @@ const KPlotAxis* KPlotWidget::axis( Axis a ) const QList KPlotWidget::pointsUnderPoint( const QPoint& p ) const { QList pts; - foreach ( KPlotObject *po, ObjectList ) { + foreach ( KPlotObject *po, d->objectList ) { foreach ( KPlotPoint *pp, po->points() ) { if ( ( p - toScreen( pp->position() ).toPoint() ).manhattanLength() <= 4 ) pts << pp; @@ -496,7 +512,7 @@ void KPlotWidget::paintEvent( QPaintEvent *e ) { resetPlotMask(); - foreach( KPlotObject *po, ObjectList ) + foreach( KPlotObject *po, d->objectList ) po->draw( &p, this ); //DEBUG_MASK diff --git a/kdeeduplot/kplotwidget.h b/kdeeduplot/kplotwidget.h index 42494fc..2a67ffa 100644 --- a/kdeeduplot/kplotwidget.h +++ b/kdeeduplot/kplotwidget.h @@ -175,12 +175,23 @@ public: * Add an item to the list of KPlotObjects to be plotted. * @param o pointer to the KPlotObject to be added */ - void addObject( KPlotObject *o ); + void addPlotObject( KPlotObject *o ); + + /** + * Add more than one KPlotObject at one time. + * @param objects the KPlotObject's to be added + */ + void addPlotObjects( const QList< KPlotObject* >& objects ); + + /** + * @return the list of the current objects + */ + QList< KPlotObject* > plotObjects() const; /** * Remove and delete all items from the list of KPlotObjects */ - void clearObjectList(); + void removeAllPlotObjects(); /** * Reset the PlotMask so that all regions are empty @@ -197,18 +208,7 @@ public: * @param i the index of th item to be replaced * @param o pointer to the replacement KPlotObject */ - void replaceObject( int i, KPlotObject *o ); - - /** - * @return the number of KPlotObjects in the list - */ - int objectCount() const { return ObjectList.size(); } - - /** - * @return a pointer to a specific KPlotObject in the list - * @param i the index of the desired KPlotObject - */ - KPlotObject *object( int i ); + void replacePlotObject( int i, KPlotObject *o ); /** * @return the background color of the plot @@ -439,10 +439,6 @@ protected: * Limits of the plot area in data units */ QRectF DataRect, SecondDataRect; - /** - * List of KPlotObjects - */ - QList ObjectList; //Grid of bools to mask "used" regions of the plot float PlotMask[100][100]; diff --git a/kdeeduplot/tests/testplot_widget.cpp b/kdeeduplot/tests/testplot_widget.cpp index 7de958e..96907d1 100644 --- a/kdeeduplot/tests/testplot_widget.cpp +++ b/kdeeduplot/tests/testplot_widget.cpp @@ -66,8 +66,8 @@ void TestPlot::slotSelectPlot( int n ) { po2->addPoint( x, 50.0 - 5.0*x ); } - plot->addObject( po1 ); - plot->addObject( po2 ); + plot->addPlotObject( po1 ); + plot->addPlotObject( po2 ); plot->update(); break; @@ -89,8 +89,8 @@ void TestPlot::slotSelectPlot( int n ) { po2->addPoint( t, cos(t) ); } - plot->addObject( po1 ); - plot->addObject( po2 ); + plot->addPlotObject( po1 ); + plot->addPlotObject( po2 ); plot->update(); break; @@ -107,7 +107,7 @@ void TestPlot::slotSelectPlot( int n ) { po1->addPoint( x, 100*exp( -0.5*x*x ), "", 0.5 ); } - plot->addObject( po1 ); + plot->addPlotObject( po1 ); plot->update(); break; @@ -129,7 +129,7 @@ void TestPlot::slotSelectPlot( int n ) { po1->addPoint( -0.8, 0.0, i18n("West") ); po1->addPoint( -0.57, 0.57, i18n("Northwest") ); - plot->addObject( po1 ); + plot->addPlotObject( po1 ); plot->update(); break; @@ -156,7 +156,7 @@ void TestPlot::slotSelectPlot( int n ) { po1->addPoint( 1.25, 1.5 ); po1->addPoint( 1.75, 1.75 ); - plot->addObject( po1 ); + plot->addPlotObject( po1 ); update(); break; @@ -183,7 +183,7 @@ void TestPlot::slotSelectPlot( int n ) { po1->addPoint( 1.25, 1.5, "G" ); po1->addPoint( 1.75, 1.75, "H" ); - plot->addObject( po1 ); + plot->addPlotObject( po1 ); update(); break; -- 2.47.3