From: Jason Harris Date: Mon, 11 Dec 2006 02:06:47 +0000 (+0000) Subject: Several fixes: X-Git-Tag: v3.80.3~98 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=7bae05473b62a861baabfe892fa1832e7c906ec2;p=libqmvoc.git Several fixes: + implement KPlotObject::label(int) and KPlotObject::setLabel(int, QString) + Add KPlotWidget::pixRect() accessor, returning the QRect of the plot area in pixel coordinates. + Don't draw plot objects that lie outside the PixRect. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=612364 --- diff --git a/kdeeduplot/kplotobject.cpp b/kdeeduplot/kplotobject.cpp index 157ecd7..fbbee9d 100644 --- a/kdeeduplot/kplotobject.cpp +++ b/kdeeduplot/kplotobject.cpp @@ -136,92 +136,94 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) { foreach( KPlotPoint *pp, pList ) { //q is the position of the point in screen pixel coordinates QPointF q = pw->toScreen( pp->position() ); - double x1 = q.x() - size(); - double y1 = q.y() - size(); - QRectF qr = QRectF( x1, y1, 2*size(), 2*size() ); - - //Mask out this rect in the plot for label avoidance - pw->maskRect( qr, 2.0 ); - - painter->setPen( pen() ); - painter->setBrush( brush() ); - - switch ( pointStyle() ) { - case CIRCLE: - painter->drawEllipse( qr ); - break; - - case LETTER: - painter->drawText( qr, Qt::AlignCenter, pp->label().left(1) ); - break; - - case TRIANGLE: - { - QPolygonF tri; - tri << QPointF( q.x() - size(), q.y() + size() ) - << QPointF( q.x(), q.y() - size() ) - << QPointF( q.x() + size(), q.y() + size() ); - painter->drawPolygon( tri ); + if ( pw->pixRect().contains( q.toPoint(), false ) ) { + double x1 = q.x() - size(); + double y1 = q.y() - size(); + QRectF qr = QRectF( x1, y1, 2*size(), 2*size() ); + + //Mask out this rect in the plot for label avoidance + pw->maskRect( qr, 2.0 ); + + painter->setPen( pen() ); + painter->setBrush( brush() ); + + switch ( pointStyle() ) { + case CIRCLE: + painter->drawEllipse( qr ); break; - } - - case SQUARE: - painter->drawRect( qr ); - break; - - case PENTAGON: - { - QPolygonF pent; - pent << QPointF( q.x(), q.y() - size() ) - << QPointF( q.x() + size(), q.y() - 0.309*size() ) - << QPointF( q.x() + 0.588*size(), q.y() + size() ) - << QPointF( q.x() - 0.588*size(), q.y() + size() ) - << QPointF( q.x() - size(), q.y() - 0.309*size() ); - painter->drawPolygon( pent ); + + case LETTER: + painter->drawText( qr, Qt::AlignCenter, pp->label().left(1) ); break; - } - - case HEXAGON: - { - QPolygonF hex; - hex << QPointF( q.x(), q.y() + size() ) - << QPointF( q.x() + size(), q.y() + 0.5*size() ) - << QPointF( q.x() + size(), q.y() - 0.5*size() ) - << QPointF( q.x(), q.y() - size() ) - << QPointF( q.x() - size(), q.y() + 0.5*size() ) - << QPointF( q.x() - size(), q.y() - 0.5*size() ); - painter->drawPolygon( hex ); + + case TRIANGLE: + { + QPolygonF tri; + tri << QPointF( q.x() - size(), q.y() + size() ) + << QPointF( q.x(), q.y() - size() ) + << QPointF( q.x() + size(), q.y() + size() ); + painter->drawPolygon( tri ); + break; + } + + case SQUARE: + painter->drawRect( qr ); break; - } - - case ASTERISK: - painter->drawLine( q, QPointF( q.x(), q.y() + size() ) ); - painter->drawLine( q, QPointF( q.x() + size(), q.y() + 0.5*size() ) ); - painter->drawLine( q, QPointF( q.x() + size(), q.y() - 0.5*size() ) ); - painter->drawLine( q, QPointF( q.x(), q.y() - size() ) ); - painter->drawLine( q, QPointF( q.x() - size(), q.y() + 0.5*size() ) ); - painter->drawLine( q, QPointF( q.x() - size(), q.y() - 0.5*size() ) ); - break; - - case STAR: - { - QPolygonF star; - star << QPointF( q.x(), q.y() - size() ) - << QPointF( q.x() + 0.2245*size(), q.y() - 0.309*size() ) - << QPointF( q.x() + size(), q.y() - 0.309*size() ) - << QPointF( q.x() + 0.363*size(), q.y() + 0.118*size() ) - << QPointF( q.x() + 0.588*size(), q.y() + size() ) - << QPointF( q.x(), q.y() + 0.382*size() ) - << QPointF( q.x() - 0.588*size(), q.y() + size() ) - << QPointF( q.x() - 0.363*size(), q.y() + 0.118*size() ) - << QPointF( q.x() - size(), q.y() - 0.309*size() ) - << QPointF( q.x() - 0.2245*size(), q.y() - 0.309*size() ); - painter->drawPolygon( star ); + + case PENTAGON: + { + QPolygonF pent; + pent << QPointF( q.x(), q.y() - size() ) + << QPointF( q.x() + size(), q.y() - 0.309*size() ) + << QPointF( q.x() + 0.588*size(), q.y() + size() ) + << QPointF( q.x() - 0.588*size(), q.y() + size() ) + << QPointF( q.x() - size(), q.y() - 0.309*size() ); + painter->drawPolygon( pent ); + break; + } + + case HEXAGON: + { + QPolygonF hex; + hex << QPointF( q.x(), q.y() + size() ) + << QPointF( q.x() + size(), q.y() + 0.5*size() ) + << QPointF( q.x() + size(), q.y() - 0.5*size() ) + << QPointF( q.x(), q.y() - size() ) + << QPointF( q.x() - size(), q.y() + 0.5*size() ) + << QPointF( q.x() - size(), q.y() - 0.5*size() ); + painter->drawPolygon( hex ); + break; + } + + case ASTERISK: + painter->drawLine( q, QPointF( q.x(), q.y() + size() ) ); + painter->drawLine( q, QPointF( q.x() + size(), q.y() + 0.5*size() ) ); + painter->drawLine( q, QPointF( q.x() + size(), q.y() - 0.5*size() ) ); + painter->drawLine( q, QPointF( q.x(), q.y() - size() ) ); + painter->drawLine( q, QPointF( q.x() - size(), q.y() + 0.5*size() ) ); + painter->drawLine( q, QPointF( q.x() - size(), q.y() - 0.5*size() ) ); + break; + + case STAR: + { + QPolygonF star; + star << QPointF( q.x(), q.y() - size() ) + << QPointF( q.x() + 0.2245*size(), q.y() - 0.309*size() ) + << QPointF( q.x() + size(), q.y() - 0.309*size() ) + << QPointF( q.x() + 0.363*size(), q.y() + 0.118*size() ) + << QPointF( q.x() + 0.588*size(), q.y() + size() ) + << QPointF( q.x(), q.y() + 0.382*size() ) + << QPointF( q.x() - 0.588*size(), q.y() + size() ) + << QPointF( q.x() - 0.363*size(), q.y() + 0.118*size() ) + << QPointF( q.x() - size(), q.y() - 0.309*size() ) + << QPointF( q.x() - 0.2245*size(), q.y() - 0.309*size() ); + painter->drawPolygon( star ); + break; + } + + default: break; } - - default: - break; } } } @@ -230,7 +232,8 @@ void KPlotObject::draw( QPainter *painter, KPlotWidget *pw ) { painter->setPen( labelPen() ); foreach ( KPlotPoint *pp, pList ) { - if ( ! pp->label().isEmpty() ) { + QPoint q = pw->toScreen( pp->position() ).toPoint(); + if ( pw->pixRect().contains(q, false) && ! pp->label().isEmpty() ) { pw->placeLabel( painter, pp ); } } diff --git a/kdeeduplot/kplotobject.h b/kdeeduplot/kplotobject.h index 566087e..51d3543 100644 --- a/kdeeduplot/kplotobject.h +++ b/kdeeduplot/kplotobject.h @@ -192,14 +192,14 @@ public: * @return the label of point i * @param i the index of the point */ - QString label( int i ) const; + inline QString label( int i ) const { if (pList.size() > i) return pList.at(i)->label(); } /** * Set the label text for point i * @param i the index of the point * @param n the new name */ - void setLabel( int i, const QString &n ); + inline void setLabel( int i, const QString &n ) { if (pList.size() > i) pList.at(i)->setLabel(n); } /** * @return true if points will be drawn for this object diff --git a/kdeeduplot/kplotwidget.cpp b/kdeeduplot/kplotwidget.cpp index 77474af..fb6f5e7 100644 --- a/kdeeduplot/kplotwidget.cpp +++ b/kdeeduplot/kplotwidget.cpp @@ -330,39 +330,38 @@ void KPlotWidget::placeLabel( QPainter *painter, KPlotPoint *pp ) { } } - painter->drawText( bestRect, textFlags, pp->label() ); - - //Is a line needed to connect the label to the point? - if ( rbest > 2.0 ) { - //Draw a rectangle around the label - painter->setBrush( QBrush() ); - //QPen pen = painter->pen(); - //pen.setStyle( Qt::DotLine ); - //painter->setPen( pen ); - painter->drawRoundRect( bestRect ); - - //Now connect the label to the point with a line. - //The line is drawn from the center of the near edge of the rectangle - float xline = bestRect.center().x(); - if ( bestRect.left() > pos.x() ) - xline = bestRect.left(); - if ( bestRect.right() < pos.x() ) - xline = bestRect.right(); - - float yline = bestRect.center().y(); - if ( bestRect.top() > pos.y() ) - yline = bestRect.top(); - if ( bestRect.bottom() < pos.y() ) - yline = bestRect.bottom(); - - //DEBUG - kDebug() << pp->label() << ": ( " << xline << ", " << yline << " ) " << bestRect << endl; - - painter->drawLine( QPointF( xline, yline ), pos ); + if ( ! bestRect.isNull() ) { + painter->drawText( bestRect, textFlags, pp->label() ); + + //Is a line needed to connect the label to the point? + if ( rbest > 2.0 ) { + //Draw a rectangle around the label + painter->setBrush( QBrush() ); + //QPen pen = painter->pen(); + //pen.setStyle( Qt::DotLine ); + //painter->setPen( pen ); + painter->drawRoundRect( bestRect ); + + //Now connect the label to the point with a line. + //The line is drawn from the center of the near edge of the rectangle + float xline = bestRect.center().x(); + if ( bestRect.left() > pos.x() ) + xline = bestRect.left(); + if ( bestRect.right() < pos.x() ) + xline = bestRect.right(); + + float yline = bestRect.center().y(); + if ( bestRect.top() > pos.y() ) + yline = bestRect.top(); + if ( bestRect.bottom() < pos.y() ) + yline = bestRect.bottom(); + + painter->drawLine( QPointF( xline, yline ), pos ); + } + + //Mask the label's rectangle so other labels won't overlap it. + maskRect( bestRect ); } - - //Mask the label's rectangle so other labels won't overlap it. - maskRect( bestRect ); } float KPlotWidget::rectCost ( const QRectF &r ) { diff --git a/kdeeduplot/kplotwidget.h b/kdeeduplot/kplotwidget.h index 2e9ecd1..216889a 100644 --- a/kdeeduplot/kplotwidget.h +++ b/kdeeduplot/kplotwidget.h @@ -362,6 +362,8 @@ public: */ KPlotAxis* axis( Axis a ); + inline QRect& pixRect() { return PixRect; } + public slots: /** * Toggle whether grid lines are drawn at major tickmarks.