- unvirtualize the methods of KPlotAxis, as imho it is not meant to be subclassed
- unvirtualize some KPlotWidget methods that doesn't need to be overloaded on subclassing
- moved some methods from inline to the relative cpp
- delete the KPlotObjects when emptying the list
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=491821
INCLUDES= $(all_includes)
SUBDIRS = .
+KDE_CXXFLAGS = -UQT3_SUPPORT
lib_LTLIBRARIES = libkdeeduplot.la
/**
* Destructor.
*/
- virtual ~KPlotAxis() {}
+ ~KPlotAxis() {}
/**
* Returns whether the axis is visible or not.
*/
- virtual bool isVisible() const { return m_visible; }
+ bool isVisible() const { return m_visible; }
/**
* Sets the "visible" property of the axis.
*/
- virtual void setVisible(bool visible) { m_visible = visible; }
+ void setVisible(bool visible) { m_visible = visible; }
/**
* Shows the axis (axis will be shown at next update of plot widget).
*/
- virtual void show() { m_visible = true; }
+ void show() { m_visible = true; }
/**
* Hides the axis (axis will be hidden at next update of plot widget).
*/
- virtual void hide() { m_visible = false; }
+ void hide() { m_visible = false; }
/**
* Sets the axis label.
* Set the label to an empty string to omit the axis label.
* @param label a string describing the data plotted on the axis.
*/
- virtual void setLabel( const QString& label ) { m_label = label; }
+ void setLabel( const QString& label ) { m_label = label; }
/**
* Returns the axis label.
*/
- virtual QString label() const { return m_label; }
+ QString label() const { return m_label; }
/**
* Set the number format for the tick labels, see QString::arg() for
* description of arguments.
*/
- virtual void setLabelFormat(int fieldWidth, char fmt = 'g', int prec=-1) {
+ void setLabelFormat(int fieldWidth, char fmt = 'g', int prec=-1) {
m_labelFieldWidth = fieldWidth; m_labelFmt = fmt; m_labelPrec = prec; }
/**
* Returns the field width of the tick labels.
*/
- virtual int labelFieldWidth() const { return m_labelFieldWidth; }
+ int labelFieldWidth() const { return m_labelFieldWidth; }
/**
* Returns the number format of the tick labels.
*/
- virtual char labelFmt() const { return m_labelFmt; }
+ char labelFmt() const { return m_labelFmt; }
/**
* short Returns the number precision of the tick labels.
*/
- virtual int labelPrec() const { return m_labelPrec; }
+ int labelPrec() const { return m_labelPrec; }
private:
bool m_visible; ///< Property "visible" defines if Axis is drawn or not.
#include <kdebug.h>
#include <qevent.h>
#include <qpainter.h>
+#include <qpalette.h>
#include <qpixmap.h>
#include <qpolygon.h>
+#include <QtAlgorithms>
#include "kplotwidget.h"
#include "kplotwidget.moc"
nmajX(0), nminX(0), nmajY(0), nminY(0),
ShowTickMarks( true ), ShowTickLabels( true ), ShowGrid( false )
{
- setBackgroundMode( Qt::NoBackground );
+ setAttribute( Qt::WA_NoBackground, true );
//set DataRect
setLimits( x1, x2, y1, y2 );
- setDefaultPadding();
+ setDefaultPaddings();
//Set PixRect (starts at (0,0) because we will translate by leftPadding(), topPadding() )
PixRect = QRect( 0, 0, width() - leftPadding() - rightPadding(),
buffer = new QPixmap();
//default colors:
- setBGColor( Qt::black );
- setFGColor( Qt::white );
+ setBackgroundColor( Qt::black );
+ setForegroundColor( Qt::white );
setGridColor( Qt::gray );
}
KPlotWidget::~KPlotWidget()
{
delete (buffer);
+ qDeleteAll( ObjectList );
ObjectList.clear();
}
} //end for iaxis
}
+void KPlotWidget::clearObjectList() {
+ qDeleteAll( ObjectList );
+ ObjectList.clear();
+ update();
+}
+
+KPlotObject *KPlotWidget::object( int i ) {
+ if ( i < 0 || i >= ObjectList.size() ) {
+ kdWarning() << "KPlotWidget::object(): index " << i << " out of range!" << endl;
+ return 0;
+ }
+ return ObjectList.at(i);
+}
+
+void KPlotWidget::setBackgroundColor( const QColor &bg ) {
+ cBackground = bg;
+ QPalette palette;
+ palette.setColor( backgroundRole(), bg );
+ setPalette( palette );
+}
+
void KPlotWidget::resizeEvent( QResizeEvent* /* e */ ) {
int newWidth = width() - leftPadding() - rightPadding();
int newHeight = height() - topPadding() - bottomPadding();
PixRect = QRect( 0, 0, newWidth, newHeight );
- buffer->resize( width(), height() );
+ QPixmap *tmp = new QPixmap( size() );
+ delete buffer;
+ buffer = tmp;
+ tmp = 0;
}
void KPlotWidget::paintEvent( QPaintEvent* /* e */ ) {
QPainter p;
p.begin( buffer );
- p.fillRect( 0, 0, width(), height(), bgColor() );
+ p.fillRect( 0, 0, width(), height(), backgroundColor() );
p.translate( leftPadding(), topPadding() );
drawObjects( &p );
drawBox( &p );
p.end();
- bitBlt( this, 0, 0, buffer );
+ p.begin( this );
+ p.drawPixmap( 0, 0, *buffer );
+ p.end();
}
void KPlotWidget::drawObjects( QPainter *p ) {
QPolygon a( po->count() );
- unsigned int i=0;
for ( QList<QPointF*>::ConstIterator dpit = po->points()->begin(); dpit != po->points()->constEnd(); ++dpit )
- a.setPoint( i++, mapToPoint( **dpit ) );
+ a << mapToPoint( **dpit );
p->drawPolygon( a );
break;
void KPlotWidget::drawBox( QPainter *p ) {
//First, fill in padding region with bgColor() to mask out-of-bounds plot data
- p->setPen( bgColor() );
- p->setBrush( bgColor() );
+ p->setPen( backgroundColor() );
+ p->setBrush( backgroundColor() );
//left padding ( don't forget: we have translated by XPADDING, YPADDING )
p->drawRect( -leftPadding(), -topPadding(), leftPadding(), height() );
}
}
- p->setPen( fgColor() );
+ p->setPen( foregroundColor() );
p->setBrush( Qt::NoBrush );
if (BottomAxis.isVisible() || LeftAxis.isVisible()) p->drawRect( PixRect ); //box outline
/**
* @class KPlotWidget
*
- * @short Genric data plotting widget.
+ * @short Generic data plotting widget.
*
* Widget for drawing plots. Includes adjustable axes (KPlotAxis) with
* tickmarks and labels and a list of KPlotObjects to be drawn.
*
+ * @note KPlotWidget will take care of the objects added to it, so when
+ * clearing the objects list (eg with clearObjectList()) any previous reference
+ * to a KPlotObject already added to a KPlotWidget will be invalid
+ *
* @author Jason Harris
*
* @version 1.1
virtual void addObject( KPlotObject *o ) { ObjectList.append( o ); }
/**
- * Remove all items from the list of KPlotObjects
+ * Remove and delete all items from the list of KPlotObjects
*/
- virtual void clearObjectList() { ObjectList.clear(); update(); }
+ void clearObjectList();
/**
* Replace an item in the KPlotObject list.
* @param i the index of th item to be replaced
* @param o pointer to the replacement KPlotObject
*/
- virtual void replaceObject( int i, KPlotObject *o ) { ObjectList.replace( i, o ); }
+ void replaceObject( int i, KPlotObject *o ) { ObjectList.replace( i, o ); }
/**
* @return the number of KPlotObjects in the list
*/
- virtual int objectCount() const { return ObjectList.count(); }
+ int objectCount() const { return ObjectList.count(); }
/**
* @return a pointer to a specific KPlotObject in the list
* @param i the index of the desired KPlotObject
*/
- virtual KPlotObject *object( int i ) { return ObjectList.at(i); }
+ KPlotObject *object( int i );
/**
* @return the background color
*/
- virtual QColor bgColor() const { return cBackground; }
+ QColor backgroundColor() const { return cBackground; }
/**
* @return the foreground color
*/
- virtual QColor fgColor() const { return cForeground; }
+ QColor foregroundColor() const { return cForeground; }
/**
* @return the grid color
*/
- virtual QColor gridColor() const { return cGrid; }
+ QColor gridColor() const { return cGrid; }
/**
* Set the background color
* @param bg the new background color
*/
- virtual void setBGColor( const QColor &bg ) { cBackground = bg; setBackgroundColor( bg ); }
+ void setBackgroundColor( const QColor &bg );
/**
* Set the foreground color
* @param fg the new foreground color
*/
- virtual void setFGColor( const QColor &fg ) { cForeground = fg; }
+ void setForegroundColor( const QColor &fg ) { cForeground = fg; }
/**
* Set the grid color
* @param gc the new grid color
*/
- virtual void setGridColor( const QColor &gc ) { cGrid = gc; }
+ void setGridColor( const QColor &gc ) { cGrid = gc; }
/**
* Toggle whether plot axes are drawn.
/**
* Revert all four padding values to be automatically determined.
*/
- virtual void setDefaultPadding() { LeftPadding = -1; RightPadding = -1; TopPadding = -1; BottomPadding = -1; }
+ void setDefaultPaddings() { LeftPadding = -1; RightPadding = -1; TopPadding = -1; BottomPadding = -1; }
QPoint mapToPoint( const QPointF& p ) {
int px = PixRect.left() + int( PixRect.width()*( p.x() - DataRect.x() )/DataRect.width() );
//The number of major and minor tickmarks to be plotted in X and Y
int nmajX, nminX, nmajY, nminY;
- //Limits of the plot area in pixel units
+ /**
+ * Limits of the plot area in pixel units
+ */
QRect PixRect;
- //Limits of the plot area in data units
+ /**
+ * Limits of the plot area in data units
+ */
QRectF DataRect;
/**
* List of KPlotObjects