]> Git trees. - libqmvoc.git/commitdiff
API change:
authorPino Toscano <pino@kde.org>
Sat, 17 Feb 2007 12:21:41 +0000 (12:21 +0000)
committerPino Toscano <pino@kde.org>
Sat, 17 Feb 2007 12:21:41 +0000 (12:21 +0000)
- move all the private members of KPlotObject into a Private class
- make the class non-copiable
- merge the two constructors in one, made explicit

svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=634474

kdeeduplot/kplotobject.cpp
kdeeduplot/kplotobject.h

index fbbee9d06f0541fdb8eb073576c53b8ed3e335ab..54d73ec06d5f10bbd8a856c4c80e5e222c8d905e 100644 (file)
 #include "kplotobject.h"
 #include "kplotwidget.h"
 
+class KPlotObject::Private
+{
+    public:
+        Private( KPlotObject * qq )
+            : q( qq )
+        {
+        }
+
+        ~Private()
+        {
+            qDeleteAll( pList );
+        }
+
+        KPlotObject *q;
+
+        QList<KPlotPoint*> pList;
+        int type;
+        PStyle pointStyle;
+        double size;
+        QPen pen, linePen, barPen, labelPen;
+        QBrush brush, barBrush;
+};
+
+
 KPlotPoint::KPlotPoint()
  : X(0), Y(0), Label(QString()), BarWidth(0.0)
 {
@@ -42,11 +66,9 @@ KPlotPoint::~KPlotPoint()
 {
 }
 
-KPlotObject::KPlotObject() {
-       KPlotObject( Qt::white, POINTS );
-}
-
-KPlotObject::KPlotObject( const QColor &c, PlotType t, double size, PStyle ps ) {
+KPlotObject::KPlotObject( const QColor &c, PlotType t, double size, PStyle ps )
+    : d( new Private( this ) )
+{
        //By default, all pens and brushes are set to the given color
        setBrush( c );
        setBarBrush( c );
@@ -55,29 +77,210 @@ KPlotObject::KPlotObject( const QColor &c, PlotType t, double size, PStyle ps )
        setBarPen( pen() );
        setLabelPen( pen() );
 
-       Type = t;
+       d->type = t;
        setSize( size );
        setPointStyle( ps );
 }
 
 KPlotObject::~KPlotObject()
 {
-       qDeleteAll( pList );
-       pList.clear();
+    delete d;
+}
+
+QString KPlotObject::label( int i ) const
+{
+    if ( i < 0 || i >= d->pList.count() )
+        return QString();
+
+    return d->pList.at( i )->label();
+}
+
+void KPlotObject::setLabel( int i, const QString &n )
+{
+    if ( i < 0 || i >= d->pList.count() )
+        return;
+
+    d->pList.at(i)->setLabel( n );
+}
+
+bool KPlotObject::showPoints() const
+{
+    return d->type & KPlotObject::POINTS;
+}
+
+bool KPlotObject::showLines() const
+{
+    return d->type & KPlotObject::LINES;
+}
+
+bool KPlotObject::showBars() const
+{
+    return d->type & KPlotObject::BARS;
+}
+
+void KPlotObject::setShowPoints( bool b )
+{
+    if ( b )
+    {
+        d->type = d->type | KPlotObject::POINTS;
+    }
+    else
+    {
+        d->type = d->type & ~KPlotObject::POINTS;
+    }
+}
+
+void KPlotObject::setShowLines( bool b )
+{
+    if ( b )
+    {
+        d->type = d->type | KPlotObject::LINES;
+    }
+    else
+    {
+        d->type = d->type & ~KPlotObject::LINES;
+    }
+}
+
+void KPlotObject::setShowBars( bool b )
+{
+    if ( b )
+    {
+        d->type = d->type | KPlotObject::BARS;
+    }
+    else
+    {
+        d->type = d->type & ~KPlotObject::BARS;
+    }
+}
+
+double KPlotObject::size() const
+{
+    return d->size;
+}
+
+void KPlotObject::setSize( double s )
+{
+    d->size = s;
+}
+
+unsigned int KPlotObject::pointStyle() const
+{
+    return d->pointStyle;
+}
+
+void KPlotObject::setPointStyle( PStyle p )
+{
+    d->pointStyle = p;
+}
+
+const QPen& KPlotObject::pen() const
+{
+    return d->pen;
+}
+
+void KPlotObject::setPen( const QPen &p )
+{
+    d->pen = p;
+}
+
+const QPen& KPlotObject::linePen() const
+{
+    return d->linePen;
+}
+
+void KPlotObject::setLinePen( const QPen &p )
+{
+    d->linePen = p;
+}
+
+const QPen& KPlotObject::barPen() const
+{
+    return d->barPen;
+}
+
+void KPlotObject::setBarPen( const QPen &p )
+{
+    d->barPen = p;
+}
+
+const QPen& KPlotObject::labelPen() const
+{
+    return d->labelPen;
+}
+
+void KPlotObject::setLabelPen( const QPen &p )
+{
+    d->labelPen = p;
+}
+
+const QBrush KPlotObject::brush() const
+{
+    return d->brush;
+}
+
+void KPlotObject::setBrush( const QBrush &b )
+{
+    d->brush = b;
+}
+
+const QBrush KPlotObject::barBrush() const
+{
+    return d->barBrush;
+}
+
+void KPlotObject::setBarBrush( const QBrush &b )
+{
+    d->barBrush = b;
+}
+
+QList< KPlotPoint* > KPlotObject::points() const
+{
+    return d->pList;
+}
+
+void KPlotObject::addPoint( const QPointF &p, const QString &label, double barWidth )
+{
+    Q_UNUSED( barWidth )
+    addPoint( new KPlotPoint( p.x(), p.y(), label ) );
+}
+
+void KPlotObject::addPoint( KPlotPoint *p )
+{
+    if ( !p )
+        return;
+    d->pList.append( p );
+}
+
+void KPlotObject::addPoint( double x, double y, const QString &label, double barWidth )
+{
+    Q_UNUSED( barWidth )
+    addPoint( new KPlotPoint( x, y, label ) );
 }
 
 void KPlotObject::removePoint( int index ) {
-       if ( ( index < 0 ) || ( index >= pList.count() ) ) {
+       if ( ( index < 0 ) || ( index >= d->pList.count() ) ) {
                kWarning() << "KPlotObject::removePoint(): index " << index << " out of range!" << endl;
                return;
        }
 
-       pList.removeAt( index );
+       d->pList.removeAt( index );
+}
+
+KPlotPoint* KPlotObject::point( int index )
+{
+    return d->pList[index];
+}
+
+int KPlotObject::count() const
+{
+    return d->pList.count();
 }
 
-void KPlotObject::clearPoints() {
-       qDeleteAll( pList );
-       pList.clear();
+void KPlotObject::clearPoints()
+{
+    qDeleteAll( d->pList );
+    d->pList.clear();
 }
 
 void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
@@ -88,18 +291,18 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
                painter->setPen( barPen() );
                painter->setBrush( barBrush() );
 
-               for ( int i=0; i<pList.size(); ++i ) {
+               for ( int i=0; i<d->pList.size(); ++i ) {
                        double w = 0;
-                       if ( pList[i]->barWidth() == 0.0 ) {
-                               if ( i<pList.size()-1 ) 
-                                       w = pList[i+1]->x() - pList[i]->x();
+                       if ( d->pList[i]->barWidth() == 0.0 ) {
+                               if ( i<d->pList.size()-1 ) 
+                                       w = d->pList[i+1]->x() - d->pList[i]->x();
                                //For the last bin, we'll just keep the previous width
 
                        } else {
-                               w = pList[i]->barWidth();
+                               w = d->pList[i]->barWidth();
                        }
 
-                       QPointF pp = pList[i]->position();
+                       QPointF pp = d->pList[i]->position();
                        QPointF p1( pp.x() - 0.5*w, 0.0 );
                        QPointF p2( pp.x() + 0.5*w, pp.y() );
                        QPointF sp1 = pw->toScreen( p1 );
@@ -117,7 +320,7 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
 
                QPointF Previous = QPointF();  //Initialize to null
 
-               foreach ( KPlotPoint *pp, pList ) {
+               foreach ( KPlotPoint *pp, d->pList ) {
                        //q is the position of the point in screen pixel coordinates
                        QPointF q = pw->toScreen( pp->position() );
 
@@ -133,7 +336,7 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
        //Draw points:
        if ( showPoints() ) {
 
-               foreach( KPlotPoint *pp, pList ) {
+               foreach( KPlotPoint *pp, d->pList ) {
                        //q is the position of the point in screen pixel coordinates
                        QPointF q = pw->toScreen( pp->position() );
                        if ( pw->pixRect().contains( q.toPoint(), false ) ) {
@@ -231,7 +434,7 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) {
        //Draw labels
        painter->setPen( labelPen() );
 
-       foreach ( KPlotPoint *pp, pList ) {
+       foreach ( KPlotPoint *pp, d->pList ) {
                QPoint q = pw->toScreen( pp->position() ).toPoint();
                if ( pw->pixRect().contains(q, false) && ! pp->label().isEmpty() ) {
                        pw->placeLabel( painter, pp );
index aed1e6d55321d463a0f63f6d75aab78fcb2ecfd9..728bcff7f4968d645182dfe7ff94d1bf7db87dc3 100644 (file)
@@ -166,12 +166,6 @@ public:
         */
        enum PStyle { NOPOINTS=0, CIRCLE=1, LETTER=2, TRIANGLE=3, SQUARE=4, PENTAGON=5, HEXAGON=6, ASTERISK=7, STAR=8, UNKNOWN_POINT };
 
-       /**
-        * Default constructor.  Create a POINTS-type object with an 
-        * empty list of points.
-        */
-       KPlotObject();
-
        /**
         * Constructor. Create a KPlotObject according to the arguments.
         * @param color The color for plotting this object.  By default this sets 
@@ -181,7 +175,7 @@ public:
         * @param size the size to use for the drawn points, in pixels
         * @param ps The PStyle describing the shape for the drawn points
         */
-       KPlotObject( const QColor &color, PlotType otype, double size=2, PStyle ps=CIRCLE );
+        explicit KPlotObject( const QColor &color = Qt::white, PlotType otype = POINTS, double size = 2, PStyle ps = CIRCLE );
 
        /**
         * Destructor.
@@ -192,143 +186,134 @@ public:
         * @return the label of point i
         * @param i the index of the point
         */
-       inline QString label( int i ) const { if (pList.size() > i) return pList.at(i)->label(); }
+        QString label( int i ) const;
 
        /**
         * Set the label text for point i
         * @param i the index of the point
         * @param n the new name
         */
-       inline void setLabel( int i, const QString &n ) { if (pList.size() > i) pList.at(i)->setLabel(n); }
+        void setLabel( int i, const QString &n );
 
        /**
         * @return true if points will be drawn for this object
         */
-       bool showPoints() const { return Type & KPlotObject::POINTS; }
+        bool showPoints() const;
        /**
         * @return true if lines will be drawn for this object
         */
-       bool showLines() const { return Type & KPlotObject::LINES; }
+        bool showLines() const;
        /**
         * @return true if bars will be drawn for this object
         */
-       bool showBars() const { return Type & KPlotObject::BARS; }
+        bool showBars() const;
 
        /**
         * Set whether points will be drawn for this object
         * @param b if true, points will be drawn
         */
-       void setShowPoints( bool b ) { 
-               if ( b ) { Type = Type | KPlotObject::POINTS; }
-               else     { Type = Type & ~KPlotObject::POINTS; }
-       }
+        void setShowPoints( bool b );
 
        /**
         * Set whether lines will be drawn for this object
         * @param b if true, lines will be drawn
         */
-       void setShowLines( bool b ) { 
-               if ( b ) { Type = Type | KPlotObject::LINES; }
-               else     { Type = Type & ~KPlotObject::LINES; }
-       }
+        void setShowLines( bool b );
 
        /**
         * Set whether bars will be drawn for this object
         * @param b if true, bars will be drawn
         */
-       void setShowBars( bool b ) { 
-               if ( b ) { Type = Type | KPlotObject::BARS; }
-               else     { Type = Type & ~KPlotObject::BARS; }
-       }
+        void setShowBars( bool b );
 
        /**
         * @return the KPlotObject's Size
        */
-       double size() const { return Size; }
+        double size() const;
 
        /**
         * Set the KPlotObject's Size
         * @param s the new size
         */
-       void setSize( double s ) { Size = s; }
+        void setSize( double s );
 
        /**
         * @return the KPlotObject's PointStyle value
         */
-       unsigned int pointStyle() const { return PointStyle; }
+        unsigned int pointStyle() const;
 
        /**
         * Set the KPlotObject's type-specific Parameter
         * @param p the new parameter
         */
-       void setPointStyle( PStyle p ) { PointStyle = p; }
+        void setPointStyle( PStyle p );
 
        /**
         * @return the default pen for this Object.
         * If no other pens are set, this pen will be used for 
         * points, lines, bars and labels (this pen is always used for points).
         */
-       const QPen& pen() const { return Pen; }
+        const QPen& pen() const;
        /**
         * Set the default pen for this object
         * @p The pen to use
         */
-       void setPen( const QPen &p ) { Pen = p; }
+        void setPen( const QPen &p );
        
        /**
         * @return the pen to use for drawing lines for this Object.
         */
-       const QPen& linePen() const { return LinePen; }
+        const QPen& linePen() const;
        /**
         * Set the pen to use for drawing lines for this object
         * @p The pen to use
         */
-       void setLinePen( const QPen &p ) { LinePen = p; }
+        void setLinePen( const QPen &p );
        
        /**
         * @return the pen to use for drawing bars for this Object.
         */
-       const QPen& barPen() const { return BarPen; }
+        const QPen& barPen() const;
        /**
         * Set the pen to use for drawing bars for this object
         * @p The pen to use
         */
-       void setBarPen( const QPen &p ) { BarPen = p; }
+        void setBarPen( const QPen &p );
        
        /**
         * @return the pen to use for drawing labels for this Object.
         */
-       const QPen& labelPen() const { return LabelPen; }
+        const QPen& labelPen() const;
        /**
         * Set the pen to use for labels for this object
         * @p The pen to use
         */
-       void setLabelPen( const QPen &p ) { LabelPen = p; }
+        void setLabelPen( const QPen &p );
        
        /**
         * @return the default Brush to use for this Object.
         */
-       const QBrush brush() const { return Brush; }
+        const QBrush brush() const;
        /**
         * Set the default brush for this object
         * @b The brush to use
         */
-       void setBrush( const QBrush &b ) { Brush = b; }
+        void setBrush( const QBrush &b );
 
        /**
         * @return the brush to use for filling bars for this Object.
         */
-       const QBrush barBrush() const { return BarBrush; }
+        const QBrush barBrush() const;
        /**
         * Set the brush to use for drawing bars for this object
         * @b The brush to use
         */
-       void setBarBrush( const QBrush &b ) { BarBrush = b; }
+        void setBarBrush( const QBrush &b );
 
        /**
         * @return the list of KPlotPoints that make up this object
         */
-       QList<KPlotPoint*> points() { return pList; }
+        QList< KPlotPoint* > points() const;
 
        /**
         * Add a point to the object's list.
@@ -336,19 +321,14 @@ public:
         * @param label the optional text label
         * @param barWidth the width of the bar, if this object is drawn as bars
         */
-       void addPoint( const QPointF &p, const QString &label="", double barWidth=0.0 ) { 
-               Q_UNUSED( barWidth )
-               addPoint( new KPlotPoint( p.x(), p.y(), label ) ); 
-       }
+        void addPoint( const QPointF &p, const QString &label = QString(), double barWidth = 0.0 );
 
        /**
         * Add a point to the object's list.
         * @overload
         * @param p pointer to the KPlotPoint to add.
         */
-       void addPoint( KPlotPoint *p ) {
-               pList.append( p );
-       }
+        void addPoint( KPlotPoint *p );
 
        /**
         * Add a point to the object's list.
@@ -358,10 +338,7 @@ public:
         * @param label the optional text label
         * @param barWidth the width of the bar, if this object is drawn as bars
         */
-       void addPoint( double x, double y, const QString &label="", double barWidth=0.0 ) {
-               Q_UNUSED( barWidth )
-               addPoint( new KPlotPoint( x, y, label ) ); 
-       }
+        void addPoint( double x, double y, const QString &label = QString(), double barWidth = 0.0 );
 
        /**
         * Remove the QPointF at position index from the list of points
@@ -373,12 +350,12 @@ public:
         * @return a pointer to the KPlotPoint at the given position in the list
         * @param index the index of the point in the list
         */
-       KPlotPoint* point( int index ) { return pList[index]; }
+        KPlotPoint* point( int index );
 
        /**
         * @return the number of QPoints currently in the list
         */
-       int count() const { return pList.count(); }
+        int count() const;
 
        /**
         * Clear the Object's points list
@@ -394,12 +371,10 @@ public:
        void draw( QPainter *p, KPlotWidget *pw );
 
 private:
-       QList<KPlotPoint*> pList;
-       int Type;
-       PStyle PointStyle;
-       double Size;
-       QPen Pen, LinePen, BarPen, LabelPen;
-       QBrush Brush, BarBrush;
+        class Private;
+        Private * const d;
+
+        Q_DISABLE_COPY( KPlotObject );
 };
 
 #endif