{
public:
virtual ~RectangleOverlay();
const QRectF &canvasRect
) const override
{
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
// Fill color
blue.setAlpha(125);
// Rectangle
pen.setColor(pen.color().lighter());
pen.setWidth(m_edgeWidth);
painter->setPen(pen);
// Add penWidth's pixels to the top and bottom of the rectangle
// so only the side lines are visible
QRect overlayRect
(x1, canvasRect.
top()-m_edgeWidth, x2
- x1, canvasRect.
height()+m_edgeWidth
);
painter->drawRect(overlayRect);
painter->fillRect(overlayRect, brush);
}
virtual QRectF boundingRect
() const override
{
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
QRectF overlayRect
(x1
- m_edgeWidth,
m_plot->canvas()->contentsRect().top(),
x2 - x1 + (2*m_edgeWidth),
m_plot->canvas()->contentsRect().height());
return overlayRect;
}
void setInterval(QwtInterval &interval) {m_interval = interval;}
QwtInterval getInterval() const {return m_interval;}
protected:
private:
QwtInterval m_interval;
int m_edgeWidth;
};
class Overlay: public QwtWidgetOverlay
{
public:
: QwtWidgetOverlay(parent)
, m_plot
(dynamic_cast<const
QwtPlot*>
(parent
)) {
}
virtual ~Overlay() {};
void attachPlotItem
(QwtPlotItem *plotItem
) {m_plotItem
= plotItem;
}
protected:
virtual void drawOverlay
( QPainter *painter
) const {
if (!m_plot)
return;
const QwtScaleMap xMap
= m_plot
->canvasMap
(m_plotItem
->xAxis
());
const QwtScaleMap yMap
= m_plot
->canvasMap
(m_plotItem
->yAxis
());
QRectF rect
(m_plot
->canvas
()->contentsRect
());
if (m_plotItem)
m_plotItem->draw(painter, xMap, yMap, m_plot->canvas()->contentsRect());
}
{
if (!m_plot)
QRectF boundingRect
= m_plotItem
->boundingRect
();
return QRegion(boundingRect.
x(), boundingRect.
y(), boundingRect.
width(), boundingRect.
height());
}
private:
};
class RectangleOverlay: public QwtPlotItem
{
public:
RectangleOverlay(QwtPlot *plot, const QwtText &title = QwtText());
virtual ~RectangleOverlay();
virtual void draw (QPainter *painter,
const QwtScaleMap &xMap,
const QwtScaleMap &yMap,
const QRectF &canvasRect) const override
{
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
// Fill color
QColor blue("RoyalBlue");
blue.setAlpha(125);
QBrush brush(blue);
// Rectangle
QPen pen("RoyalBlue");
pen.setColor(pen.color().lighter());
pen.setWidth(m_edgeWidth);
painter->setPen(pen);
// Add penWidth's pixels to the top and bottom of the rectangle
// so only the side lines are visible
QRect overlayRect(x1, canvasRect.top()-m_edgeWidth, x2 - x1, canvasRect.height()+m_edgeWidth);
painter->drawRect(overlayRect);
painter->fillRect(overlayRect, brush);
}
virtual QRectF boundingRect() const override
{
const QwtScaleMap xMap = m_plot->canvasMap(xAxis());
int x1 = qRound(xMap.transform(m_interval.minValue()));
int x2 = qRound(xMap.transform(m_interval.maxValue()));
QRectF overlayRect(x1 - m_edgeWidth,
m_plot->canvas()->contentsRect().top(),
x2 - x1 + (2*m_edgeWidth),
m_plot->canvas()->contentsRect().height());
return overlayRect;
}
void setInterval(QwtInterval &interval) {m_interval = interval;}
QwtInterval getInterval() const {return m_interval;}
protected:
private:
QwtPlot *m_plot;
QwtInterval m_interval;
int m_edgeWidth;
};
class Overlay: public QwtWidgetOverlay
{
public:
Overlay(QWidget *parent)
: QwtWidgetOverlay(parent)
, m_plot(dynamic_cast<const QwtPlot*>(parent))
{
}
virtual ~Overlay() {};
void attachPlotItem(QwtPlotItem *plotItem) {m_plotItem = plotItem;}
protected:
virtual void drawOverlay( QPainter *painter ) const
{
if (!m_plot)
return;
const QwtScaleMap xMap = m_plot->canvasMap(m_plotItem->xAxis());
const QwtScaleMap yMap = m_plot->canvasMap(m_plotItem->yAxis());
QRectF rect(m_plot->canvas()->contentsRect());
if (m_plotItem)
m_plotItem->draw(painter, xMap, yMap, m_plot->canvas()->contentsRect());
}
virtual QRegion maskHint() const
{
if (!m_plot)
return QRegion();
QRectF boundingRect = m_plotItem->boundingRect();
return QRegion(boundingRect.x(), boundingRect.y(), boundingRect.width(), boundingRect.height());
}
private:
const QwtPlot *m_plot;
QwtPlotItem *m_plotItem;
};
To copy to clipboard, switch view to plain text mode
Bookmarks