23 #include <QMouseEvent> 26 #include <QResizeEvent> 28 #include <qwt/qwt_plot.h> 29 #include <qwt/qwt_plot_canvas.h> 30 #include <qwt/qwt_plot_curve.h> 31 #include <qwt/qwt_scale_widget.h> 45 QwtPlotPicker(canvas),
47 mouseControl_(false) {
48 setTrackerMode(QwtPicker::AlwaysOn);
51 setRubberBand(QwtPicker::CrossRubberBand);
52 setRubberBandPen(Qt::DashLine);
54 connect(plot()->axisWidget(xAxis()), SIGNAL(scaleDivChanged()),
56 connect(plot()->axisWidget(yAxis()), SIGNAL(scaleDivChanged()),
71 if (active && !isActive()) {
72 setTrackerMode(QwtPicker::AlwaysOff);
75 append(transform(position));
81 else if (!active && isActive()) {
85 setTrackerMode(QwtPicker::AlwaysOn);
97 move(transform(position));
129 QSizeF textSize = text.textSize(font);
130 QRect textRect(0, 0, qCeil(textSize.width()), qCeil(textSize.height()));
131 QPoint position = transform(point);
133 int alignment = Qt::AlignTop | Qt::AlignRight;
135 int x = position.x();
136 int y = position.y();
138 if (alignment & Qt::AlignLeft)
139 x -= textRect.width() + margin;
140 else if (alignment & Qt::AlignRight)
143 if (alignment & Qt::AlignBottom)
145 else if (alignment & Qt::AlignTop)
146 y -= textRect.height() + margin;
148 textRect.moveTopLeft(QPoint(x, y));
150 #if QWT_VERSION >= 0x060100 151 int left = qMax(textRect.left(), trackerRect(font).left()+margin);
152 int right = qMin(textRect.right(), trackerRect(font).right()-margin);
153 int top = qMax(textRect.top(), trackerRect(font).top()+margin);
154 int bottom = qMin( textRect.bottom(), trackerRect(font).bottom()-margin);
156 int left = qMax(textRect.left(), pickRect().left()+margin);
157 int right = qMin(textRect.right(), pickRect().right()-margin);
158 int top = qMax(textRect.top(), pickRect().top()+margin);
159 int bottom = qMin( textRect.bottom(), pickRect().bottom()-margin);
162 textRect.moveBottomRight(QPoint(right, bottom));
163 textRect.moveTopLeft(QPoint(left, top));
169 QwtScaleMap xMap = plot()->canvasMap(xAxis());
170 QwtScaleMap yMap = plot()->canvasMap(yAxis());
172 double xPrecision = log10(fabs(xMap.invTransform(1.0)-
173 xMap.invTransform(0.0)));
174 double yPrecision = log10(fabs(yMap.invTransform(1.0)-
175 yMap.invTransform(0.0)));
179 if ((xPrecision < 0.0) && (fabs(point.x()) >= 1.0))
180 x.sprintf(
"%.*f", (
int)ceil(fabs(xPrecision)), point.x());
182 x.sprintf(
"%g", point.x());
184 if ((yPrecision < 0.0) && (fabs(point.y()) >= 1.0))
185 y.sprintf(
"%.*f", (
int)ceil(fabs(yPrecision)), point.y());
187 y.sprintf(
"%g", point.y());
189 return QwtText(x+
", "+y);
197 if (dynamic_cast<QWidget*>(painter->device())) {
198 QPen pen = painter->pen();
199 QColor penColor = pen.color();
201 penColor.setAlphaF(0.3);
202 pen.setColor(penColor);
204 painter->setPen(pen);
207 QwtPlotPicker::drawRubberBand(painter);
213 bool active = isActive();
215 QwtPlotPicker::begin();
217 if (!active && isActive())
222 QPointF newPosition = invTransform(point);
232 QwtPlotPicker::move(point);
236 bool active = isActive();
238 bool result = QwtPlotPicker::end(ok);
240 if (active && !isActive())
247 if (
object == plot()->canvas()) {
248 if (event->type() == QEvent::Enter)
250 else if (event->type() == QEvent::Leave)
252 else if (event->type() == QEvent::MouseButtonRelease)
256 bool result = QwtPlotPicker::eventFilter(
object, event);
258 if (isActive() &&
object == plot()->canvas()) {
259 if (event->type() == QEvent::Resize)
269 QwtPlotPicker::updateDisplay();
278 QwtScaleMap map = plot()->canvasMap(xAxis());
279 double maxDistance = fabs(map.invTransform(1.0)-map.invTransform(0.0));
281 for (QwtPlotItemIterator it = plot()->itemList().begin();
282 it != plot()->itemList().end(); ++it) {
283 if ((*it)->rtti() == QwtPlotItem::Rtti_PlotCurve) {
284 QwtPlotCurve* curve = (QwtPlotCurve*)(*it);
291 if (!indexes.isEmpty()) {
293 trackedPoint.
color = curve->pen().color();
295 double minDistance = std::numeric_limits<double>::max();
297 for (
size_t index = 0; index < indexes.count(); ++index) {
298 QPointF point = data->
getPoint(indexes[index]);
300 double distance = vector.x()*vector.x()+vector.y()*vector.y();
302 if (distance < minDistance) {
304 minDistance = distance;
321 QPoint point = transform(position);
323 if (dynamic_cast<QWidget*>(painter->device()))
326 painter->fillRect(point.x()-3, point.y()-3, 6, 6,
327 painter->pen().color());
329 QRect textRect =
getTextRect(position, painter->font());
331 if (!textRect.isEmpty()) {
332 if (dynamic_cast<QWidget*>(painter->device())) {
335 if (!label.isEmpty())
336 label.draw(painter, textRect);
339 painter->fillRect(textRect, painter->pen().color());
353 newPosition.setX(plot()->canvasMap(xAxis()).invTransform(
354 pickedPoints()[0].x()));
365 QPoint newPosition = pickedPoints()[0];
367 newPosition.setX(plot()->canvasMap(xAxis()).transform(
382 newPosition.setY(plot()->canvasMap(yAxis()).invTransform(
383 pickedPoints()[0].y()));
394 QPoint newPosition = pickedPoints()[0];
396 newPosition.setY(plot()->canvasMap(yAxis()).transform(
QRect getTextRect(const QPointF &point, const QFont &font) const
void updateTrackedPoints()
void drawTrackedPoints(QPainter *painter) const
bool arePointsTracked() const
virtual QVector< size_t > getPointsInDistance(double x, double maxDistance) const
QwtText trackerTextF(const QPointF &point) const
void plotXAxisScaleDivChanged()
PlotCursor(QwtPlotCanvas *canvas)
bool eventFilter(QObject *object, QEvent *event)
void move(const QPoint &point)
void plotYAxisScaleDivChanged()
void setActive(bool active, const QPointF &position=QPointF(0.0, 0.0))
bool hasMouseControl() const
QVector< TrackedPoint > trackedPoints_
virtual QPointF getPoint(size_t index) const =0
void setCurrentPosition(const QPointF &position)
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
void currentPositionChanged(const QPointF &position)
void activeChanged(bool active)
void setTrackPoints(bool track)
void drawRubberBand(QPainter *painter) const
const QPointF & getCurrentPosition() const