| 1 |
/***************************************************************************** |
| 2 |
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann |
| 3 |
* This file may be used under the terms of the 3-clause BSD License |
| 4 |
*****************************************************************************/ |
| 5 |
|
| 6 |
#include "Plot.h" |
| 7 |
|
| 8 |
#include <QwtPlotGrid> |
| 9 |
#include <QwtPlotItem> |
| 10 |
#include <QwtScaleMap> |
| 11 |
#include <QwtPlotLayout> |
| 12 |
#include <QwtInterval> |
| 13 |
#include <QwtPainter> |
| 14 |
#include <QwtMath> |
| 15 |
|
| 16 |
#include <QPainter> |
| 17 |
|
| 18 |
class TextItem : public QwtPlotItem |
| 19 |
{ |
| 20 |
public: |
| 21 |
void setText( const QString& text ) |
| 22 |
{ |
| 23 |
m_text = text; |
| 24 |
} |
| 25 |
|
| 26 |
virtual void draw( QPainter* painter, |
| 27 |
const QwtScaleMap&, const QwtScaleMap&, |
| 28 |
const QRectF& canvasRect ) const QWT_OVERRIDE |
| 29 |
{ |
| 30 |
const int margin = 5; |
| 31 |
const QRectF textRect = |
| 32 |
canvasRect.adjusted( margin, margin, -margin, -margin ); |
| 33 |
|
| 34 |
painter->setPen( Qt::white ); |
| 35 |
painter->drawText( textRect, |
| 36 |
Qt::AlignBottom | Qt::AlignRight, m_text ); |
| 37 |
} |
| 38 |
|
| 39 |
private: |
| 40 |
QString m_text; |
| 41 |
}; |
| 42 |
|
| 43 |
// RectItem shows how to implement a simple plot item, |
| 44 |
// what wouldn't be necessary as QwtPlotShapeItem |
| 45 |
// would do the same |
| 46 |
|
| 47 |
class RectItem : public QwtPlotItem |
| 48 |
{ |
| 49 |
public: |
| 50 |
enum Type |
| 51 |
{ |
| 52 |
Rect, |
| 53 |
Ellipse |
| 54 |
}; |
| 55 |
|
| 56 |
explicit RectItem( Type type ) |
| 57 |
: m_type( type ) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
void setPen( const QPen& pen ) |
| 62 |
{ |
| 63 |
if ( pen != m_pen ) |
| 64 |
{ |
| 65 |
m_pen = pen; |
| 66 |
itemChanged(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void setBrush( const QBrush& brush ) |
| 71 |
{ |
| 72 |
if ( brush != m_brush ) |
| 73 |
{ |
| 74 |
m_brush = brush; |
| 75 |
itemChanged(); |
| 76 |
} |
| 77 |
} |
| 78 |
void setRect( const QRectF& rect ) |
| 79 |
{ |
| 80 |
if ( m_rect != rect ) |
| 81 |
{ |
| 82 |
m_rect = rect; |
| 83 |
itemChanged(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
virtual QRectF boundingRect() const QWT_OVERRIDE |
| 88 |
{ |
| 89 |
return m_rect; |
| 90 |
} |
| 91 |
|
| 92 |
virtual void draw( QPainter* painter, |
| 93 |
const QwtScaleMap& xMap, const QwtScaleMap& yMap, |
| 94 |
const QRectF& ) const QWT_OVERRIDE |
| 95 |
{ |
| 96 |
if ( m_rect.isValid() ) |
| 97 |
{ |
| 98 |
const QRectF rect = QwtScaleMap::transform( |
| 99 |
xMap, yMap, m_rect ); |
| 100 |
|
| 101 |
painter->setPen( m_pen ); |
| 102 |
painter->setBrush( m_brush ); |
| 103 |
|
| 104 |
if ( m_type == Ellipse ) |
| 105 |
QwtPainter::drawEllipse( painter, rect ); |
| 106 |
else |
| 107 |
QwtPainter::drawRect( painter, rect ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private: |
| 112 |
QPen m_pen; |
| 113 |
QBrush m_brush; |
| 114 |
QRectF m_rect; |
| 115 |
Type m_type; |
| 116 |
}; |
| 117 |
|
| 118 |
Plot::Plot( QWidget* parent, const QwtInterval& interval ) |
| 119 |
: QwtPlot( parent ) |
| 120 |
{ |
| 121 |
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) |
| 122 |
setAxisScale( axisPos, interval.minValue(), interval.maxValue() ); |
| 123 |
|
| 124 |
setCanvasBackground( QColor( Qt::darkBlue ) ); |
| 125 |
plotLayout()->setAlignCanvasToScales( true ); |
| 126 |
|
| 127 |
// grid |
| 128 |
QwtPlotGrid* grid = new QwtPlotGrid; |
| 129 |
//grid->enableXMin(true); |
| 130 |
grid->setMajorPen( Qt::white, 0, Qt::DotLine ); |
| 131 |
grid->setMinorPen( Qt::gray, 0, Qt::DotLine ); |
| 132 |
grid->attach( this ); |
| 133 |
|
| 134 |
const int numEllipses = 10; |
| 135 |
|
| 136 |
for ( int i = 0; i < numEllipses; i++ ) |
| 137 |
{ |
| 138 |
const double x = interval.minValue() + |
| 139 |
qwtRand() % qRound( interval.width() ); |
| 140 |
const double y = interval.minValue() + |
| 141 |
qwtRand() % qRound( interval.width() ); |
| 142 |
const double r = interval.minValue() + |
| 143 |
qwtRand() % qRound( interval.width() / 6 ); |
| 144 |
|
| 145 |
const QRectF area( x - r, y - r, 2 * r, 2 * r ); |
| 146 |
|
| 147 |
RectItem* item = new RectItem( RectItem::Ellipse ); |
| 148 |
item->setRenderHint( QwtPlotItem::RenderAntialiased, true ); |
| 149 |
item->setRect( area ); |
| 150 |
item->setPen( QPen( Qt::yellow ) ); |
| 151 |
item->attach( this ); |
| 152 |
} |
| 153 |
|
| 154 |
TextItem* textItem = new TextItem(); |
| 155 |
textItem->setText( "Navigation Example" ); |
| 156 |
textItem->attach( this ); |
| 157 |
|
| 158 |
m_rectItem = new RectItem( RectItem::Rect ); |
| 159 |
m_rectItem->setPen( Qt::NoPen ); |
| 160 |
QColor c = Qt::gray; |
| 161 |
c.setAlpha( 100 ); |
| 162 |
m_rectItem->setBrush( QBrush( c ) ); |
| 163 |
m_rectItem->attach( this ); |
| 164 |
} |
| 165 |
|
| 166 |
void Plot::updateLayout() |
| 167 |
{ |
| 168 |
QwtPlot::updateLayout(); |
| 169 |
|
| 170 |
const QwtScaleMap xMap = canvasMap( QwtAxis::XBottom ); |
| 171 |
const QwtScaleMap yMap = canvasMap( QwtAxis::YLeft ); |
| 172 |
|
| 173 |
const QRect cr = canvas()->contentsRect(); |
| 174 |
const double x1 = xMap.invTransform( cr.left() ); |
| 175 |
const double x2 = xMap.invTransform( cr.right() ); |
| 176 |
const double y1 = yMap.invTransform( cr.bottom() ); |
| 177 |
const double y2 = yMap.invTransform( cr.top() ); |
| 178 |
|
| 179 |
const double xRatio = ( x2 - x1 ) / cr.width(); |
| 180 |
const double yRatio = ( y2 - y1 ) / cr.height(); |
| 181 |
|
| 182 |
Q_EMIT resized( xRatio, yRatio ); |
| 183 |
} |
| 184 |
|
| 185 |
void Plot::setRectOfInterest( const QRectF& rect ) |
| 186 |
{ |
| 187 |
m_rectItem->setRect( rect ); |
| 188 |
} |
| 189 |
|
| 190 |
#include "moc_Plot.cpp" |