]> Git trees. - libqmvoc.git/commitdiff
API change in the plot objects handling:
authorPino Toscano <pino@kde.org>
Fri, 16 Feb 2007 20:12:31 +0000 (20:12 +0000)
committerPino Toscano <pino@kde.org>
Fri, 16 Feb 2007 20:12:31 +0000 (20:12 +0000)
- 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
kdeeduplot/kplotwidget.h
kdeeduplot/tests/testplot_widget.cpp

index e4a1e4d890a906ee27f07e3eb631feb4ac956da1..ea722fa75f306a9a5e2a7e41962a6c451096a3b2 100644 (file)
@@ -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<Axis, KPlotAxis*> axes;
+        // List of KPlotObjects
+        QList<KPlotObject*> 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<KPlotPoint*> KPlotWidget::pointsUnderPoint( const QPoint& p ) const {
        QList<KPlotPoint*> 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
index 42494fc0ae71a3d3a4acd38d508f347f05b330ce..2a67ffaa9d0f7bfd10db1a113a3ccfddbfc75a8a 100644 (file)
@@ -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<KPlotObject*> ObjectList;
 
        //Grid of bools to mask "used" regions of the plot
        float PlotMask[100][100];
index 7de958e0513b2862f6da3869b8364205ad65a09e..96907d15caf82c47cf5a4e4cbc9dd84a3aff4ce7 100644 (file)
@@ -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;