10 #include "qwt_picker.h"
11 #include "qwt_picker_machine.h"
12 #include "qwt_painter.h"
14 #include "qwt_widget_overlay.h"
19 #include <qpainterpath.h>
23 static inline QRegion qwtMaskRegion(
const QRect& r,
int penWidth )
25 const int pw = qMax( penWidth, 1 );
26 const int pw2 = penWidth / 2;
28 int x1 = r.left() - pw2;
29 int x2 = r.right() + 1 + pw2 + ( pw % 2 );
31 int y1 = r.top() - pw2;
32 int y2 = r.bottom() + 1 + pw2 + ( pw % 2 );
36 region += QRect( x1, y1, x2 - x1, pw );
37 region += QRect( x1, y1, pw, y2 - y1 );
38 region += QRect( x1, y2 - pw, x2 - x1, pw );
39 region += QRect( x2 - pw, y1, pw, y2 - y1 );
44 static inline QRegion qwtMaskRegion(
const QLine& l,
int penWidth )
46 const int pw = qMax( penWidth, 1 );
47 const int pw2 = penWidth / 2;
51 if ( l.x1() == l.x2() )
53 region += QRect( l.x1() - pw2, l.y1(),
54 pw, l.y2() ).normalized();
56 else if ( l.y1() == l.y2() )
58 region += QRect( l.x1(), l.y1() - pw2,
59 l.x2(), pw ).normalized();
70 Rubberband(
QwtPicker* picker, QWidget* parent )
78 virtual void drawOverlay( QPainter* painter )
const QWT_OVERRIDE
80 painter->setPen( m_picker->rubberBandPen() );
81 m_picker->drawRubberBand( painter );
84 virtual QRegion
maskHint() const QWT_OVERRIDE
86 return m_picker->rubberBandMask();
95 Tracker(
QwtPicker* picker, QWidget* parent )
103 virtual void drawOverlay( QPainter* painter )
const QWT_OVERRIDE
105 painter->setPen( m_picker->trackerPen() );
106 m_picker->drawTracker( painter );
109 virtual QRegion
maskHint() const QWT_OVERRIDE
111 return m_picker->trackerMask();
118 class QwtPicker::PrivateData
129 mouseTracking( false ),
191 setMouseTracking(
false );
193 delete m_data->stateMachine;
194 delete m_data->rubberBandOverlay;
195 delete m_data->trackerOverlay;
201 void QwtPicker::init( QWidget* parent,
202 RubberBand rubberBand, DisplayMode trackerMode )
204 m_data =
new PrivateData;
210 if ( parent->focusPolicy() == Qt::NoFocus )
211 parent->setFocusPolicy( Qt::WheelFocus );
213 m_data->openGL = parent->inherits(
"QGLWidget" );
214 m_data->trackerFont = parent->font();
215 m_data->mouseTracking = parent->hasMouseTracking();
235 delete m_data->stateMachine;
238 if ( m_data->stateMachine )
239 m_data->stateMachine->reset();
249 return m_data->stateMachine;
258 return m_data->stateMachine;
264 QObject* obj = parent();
265 if ( obj && obj->isWidgetType() )
266 return static_cast< QWidget*
>( obj );
274 QObject* obj = parent();
275 if ( obj && obj->isWidgetType() )
276 return static_cast< const QWidget*
>( obj );
300 return m_data->rubberBand;
321 if ( m_data->trackerMode != mode )
323 m_data->trackerMode = mode;
324 setMouseTracking( m_data->trackerMode ==
AlwaysOn );
334 return m_data->trackerMode;
353 m_data->resizeMode = mode;
363 return m_data->resizeMode;
377 if ( m_data->enabled != enabled )
379 m_data->enabled = enabled;
385 w->installEventFilter(
this );
387 w->removeEventFilter(
this );
401 return m_data->enabled;
412 if ( font != m_data->trackerFont )
414 m_data->trackerFont = font;
426 return m_data->trackerFont;
437 if ( pen != m_data->trackerPen )
439 m_data->trackerPen = pen;
450 return m_data->trackerPen;
461 if ( pen != m_data->rubberBandPen )
463 m_data->rubberBandPen = pen;
474 return m_data->rubberBandPen;
497 label = QString::number( pos.y() );
500 label = QString::number( pos.x() );
503 label = QString::number( pos.x() ) +
", " + QString::number( pos.y() );
540 if ( m_data->stateMachine )
541 selectionType = m_data->stateMachine->selectionType();
543 switch ( selectionType )
548 if ( pa.count() < 1 )
551 const QPoint pos = pa[0];
554 const QRect pRect =
pickArea().boundingRect().toRect();
559 mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
560 pos.x(), pRect.bottom() ), pw );
565 mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
566 pRect.right(), pos.y() ), pw );
571 mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
572 pos.x(), pRect.bottom() ), pw );
573 mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
574 pRect.right(), pos.y() ), pw );
584 if ( pa.count() < 2 )
593 const QRect r = QRect( pa.first(), pa.last() );
594 mask = qwtMaskRegion( r.normalized(), pw );
599 const QRect r = QRect( pa.first(), pa.last() );
600 mask += r.adjusted( -pw, -pw, pw, pw );
616 const int off = 2 * pw;
617 const QRect r = pa.boundingRect();
618 mask += r.adjusted( -off, -off, off, off );
650 if ( m_data->stateMachine )
651 selectionType = m_data->stateMachine->selectionType();
653 switch ( selectionType )
658 if ( pa.count() < 1 )
661 const QPoint pos = pa[0];
663 const QRect pRect =
pickArea().boundingRect().toRect();
669 pRect.top(), pos.x(), pRect.bottom() );
675 pos.y(), pRect.right(), pos.y() );
681 pRect.top(), pos.x(), pRect.bottom() );
683 pos.y(), pRect.right(), pos.y() );
693 if ( pa.count() < 2 )
696 const QRect rect = QRect( pa.first(), pa.last() ).normalized();
717 painter->drawPolyline( pa );
734 const QRect textRect =
trackerRect( painter->font() );
735 if ( !textRect.isEmpty() )
739 label.
draw( painter, textRect );
800 return m_data->trackerPosition;
820 if ( m_data->trackerPosition.x() < 0 || m_data->trackerPosition.y() < 0 )
827 const QSizeF textSize = text.
textSize( font );
828 QRect textRect( 0, 0, qwtCeil( textSize.width() ), qwtCeil( textSize.height() ) );
830 const QPoint& pos = m_data->trackerPosition;
833 if (
isActive() && m_data->pickedPoints.count() > 1
837 m_data->pickedPoints[ m_data->pickedPoints.count() - 2 ];
839 alignment |= ( pos.x() >= last.x() ) ? Qt::AlignRight : Qt::AlignLeft;
840 alignment |= ( pos.y() > last.y() ) ? Qt::AlignBottom : Qt::AlignTop;
843 alignment = Qt::AlignTop | Qt::AlignRight;
845 const int margin = 5;
848 if ( alignment & Qt::AlignLeft )
849 x -= textRect.width() + margin;
850 else if ( alignment & Qt::AlignRight )
854 if ( alignment & Qt::AlignBottom )
856 else if ( alignment & Qt::AlignTop )
857 y -= textRect.height() + margin;
859 textRect.moveTopLeft( QPoint( x, y ) );
861 const QRect pickRect =
pickArea().boundingRect().toRect();
863 int right = qMin( textRect.right(), pickRect.right() - margin );
864 int bottom = qMin( textRect.bottom(), pickRect.bottom() - margin );
865 textRect.moveBottomRight( QPoint( right, bottom ) );
867 int left = qMax( textRect.left(), pickRect.left() + margin );
868 int top = qMax( textRect.top(), pickRect.top() + margin );
869 textRect.moveTopLeft( QPoint( left, top ) );
897 switch ( event->type() )
901 const QResizeEvent* re =
static_cast< QResizeEvent*
>( event );
909 if ( m_data->trackerOverlay )
910 m_data->trackerOverlay->resize( re->size() );
912 if ( m_data->rubberBandOverlay )
913 m_data->rubberBandOverlay->resize( re->size() );
915 if ( m_data->resizeMode ==
Stretch )
931 case QEvent::MouseButtonPress:
936 case QEvent::MouseButtonRelease:
941 case QEvent::MouseButtonDblClick:
946 case QEvent::MouseMove:
951 case QEvent::KeyPress:
956 case QEvent::KeyRelease:
998 if (
pickArea().contains( mouseEvent->pos() ) )
999 m_data->trackerPosition = mouseEvent->pos();
1001 m_data->trackerPosition = QPoint( -1, -1 );
1036 m_data->trackerPosition = QPoint( -1, -1 );
1083 #if QT_VERSION < 0x050e00
1084 const QPoint wheelPos = wheelEvent->pos();
1086 const QPoint wheelPos = wheelEvent->position().toPoint();
1088 if (
pickArea().contains( wheelPos ) )
1089 m_data->trackerPosition = wheelPos;
1091 m_data->trackerPosition = QPoint( -1, -1 );
1118 if ( keyEvent->isAutoRepeat() )
1136 if ( dx != 0 || dy != 0 )
1138 const QRect rect =
pickArea().boundingRect().toRect();
1139 const QPoint pos =
parentWidget()->mapFromGlobal( QCursor::pos() );
1141 int x = pos.x() + dx;
1142 x = qMax( rect.left(), x );
1143 x = qMin( rect.right(), x );
1145 int y = pos.y() + dy;
1146 y = qMax( rect.top(), y );
1147 y = qMin( rect.bottom(), y );
1149 QCursor::setPos(
parentWidget()->mapToGlobal( QPoint( x, y ) ) );
1178 if ( !m_data->stateMachine )
1182 m_data->stateMachine->transition( *
this, event );
1185 switch ( event->type() )
1187 case QEvent::MouseButtonDblClick:
1188 case QEvent::MouseButtonPress:
1189 case QEvent::MouseButtonRelease:
1190 case QEvent::MouseMove:
1192 const QMouseEvent* me =
1193 static_cast< const QMouseEvent*
>( event );
1201 for (
int i = 0; i < commandList.count(); i++ )
1203 switch ( commandList[i] )
1205 case QwtPickerMachine::Begin:
1210 case QwtPickerMachine::Append:
1215 case QwtPickerMachine::Move:
1220 case QwtPickerMachine::Remove:
1225 case QwtPickerMachine::End:
1241 if ( m_data->isActive )
1244 m_data->pickedPoints.clear();
1245 m_data->isActive =
true;
1250 if ( m_data->trackerPosition.x() < 0 || m_data->trackerPosition.y() < 0 )
1254 m_data->trackerPosition = w->mapFromGlobal( QCursor::pos() );
1259 setMouseTracking(
true );
1274 if ( m_data->isActive )
1276 setMouseTracking(
false );
1278 m_data->isActive =
false;
1282 m_data->trackerPosition = QPoint( -1, -1 );
1285 ok =
accept( m_data->pickedPoints );
1288 Q_EMIT
selected( m_data->pickedPoints );
1290 m_data->pickedPoints.clear();
1305 if ( m_data->stateMachine )
1306 m_data->stateMachine->reset();
1322 if ( m_data->isActive )
1324 m_data->pickedPoints += pos;
1340 if ( m_data->isActive && !m_data->pickedPoints.isEmpty() )
1342 QPoint& point = m_data->pickedPoints.last();
1348 Q_EMIT
moved( pos );
1361 if ( m_data->isActive && !m_data->pickedPoints.isEmpty() )
1363 #if QT_VERSION >= 0x050100
1364 const QPoint pos = m_data->pickedPoints.takeLast();
1366 const QPoint pos = m_data->pickedPoints.last();
1367 m_data->pickedPoints.resize( m_data->pickedPoints.count() - 1 );
1395 return m_data->isActive;
1405 return m_data->pickedPoints;
1419 if ( oldSize.isEmpty() )
1426 const double xRatio = double( newSize.width() ) / double( oldSize.width() );
1427 const double yRatio = double( newSize.height() ) / double( oldSize.height() );
1429 for (
int i = 0; i < m_data->pickedPoints.count(); i++ )
1431 QPoint& p = m_data->pickedPoints[i];
1432 p.setX( qRound( p.x() * xRatio ) );
1433 p.setY( qRound( p.y() * yRatio ) );
1435 Q_EMIT
changed( m_data->pickedPoints );
1452 void QwtPicker::setMouseTracking(
bool enable )
1460 m_data->mouseTracking = widget->hasMouseTracking();
1461 widget->setMouseTracking(
true );
1465 widget->setMouseTracking( m_data->mouseTracking );
1480 path.addRect( widget->contentsRect() );
1490 bool showRubberband =
false;
1491 bool showTracker =
false;
1493 if ( w && w->isVisible() && m_data->enabled )
1498 showRubberband =
true;
1512 QPointer< Rubberband >& rw = m_data->rubberBandOverlay;
1513 if ( showRubberband )
1517 rw =
new Rubberband(
this, NULL );
1518 rw->setObjectName(
"PickerRubberBand" );
1520 rw->resize( w->size() );
1528 rw->updateOverlay();
1532 if ( m_data->openGL )
1548 QPointer< Tracker >& tw = m_data->trackerOverlay;
1553 tw =
new Tracker(
this, NULL );
1554 tw->setObjectName(
"PickerTracker" );
1556 tw->resize( w->size() );
1558 tw->setFont( m_data->trackerFont );
1559 tw->updateOverlay();
1563 if ( m_data->openGL )
1583 return m_data->rubberBandOverlay;
1589 return m_data->trackerOverlay;
1593 #include "moc_qwt_picker.cpp"
bool keyMatch(KeyPatternCode, const QKeyEvent *) const
Compare a key event with an event pattern.
@ KeyAbort
Qt::Key_Escape.
static void drawEllipse(QPainter *, const QRectF &)
Wrapper for QPainter::drawEllipse()
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
QwtPicker provides selections on a widget.
@ AlwaysOn
Display always.
@ AlwaysOff
Display never.
@ ActiveOnly
Display only when the selection is active.
virtual void updateDisplay()
Update the state of rubber band and tracker label.
QFont trackerFont() const
ResizeMode resizeMode() const
virtual void drawRubberBand(QPainter *) const
RubberBand rubberBand() const
void setRubberBandPen(const QPen &)
virtual QRegion trackerMask() const
virtual void stretchSelection(const QSize &oldSize, const QSize &newSize)
virtual bool eventFilter(QObject *, QEvent *) override
Event filter.
void selected(const QPolygon &polygon)
QPen rubberBandPen() const
const QwtWidgetOverlay * rubberBandOverlay() const
virtual void widgetMousePressEvent(QMouseEvent *)
virtual void move(const QPoint &)
virtual ~QwtPicker()
Destructor.
virtual void append(const QPoint &)
void setStateMachine(QwtPickerMachine *)
virtual void drawTracker(QPainter *) const
virtual QRegion rubberBandMask() const
QPoint trackerPosition() const
virtual void widgetEnterEvent(QEvent *)
void setRubberBand(RubberBand)
const QwtWidgetOverlay * trackerOverlay() const
virtual void widgetMouseMoveEvent(QMouseEvent *)
virtual void widgetLeaveEvent(QEvent *)
virtual void transition(const QEvent *)
virtual bool accept(QPolygon &) const
Validate and fix up the selection.
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
virtual QPainterPath pickArea() const
virtual void widgetKeyReleaseEvent(QKeyEvent *)
void setTrackerFont(const QFont &)
void setEnabled(bool)
En/disable the picker.
DisplayMode trackerMode() const
virtual QPolygon adjustedPoints(const QPolygon &) const
Map the pickedPoints() into a selection()
@ VLineRubberBand
A vertical line ( only for QwtPickerMachine::PointSelection )
@ CrossRubberBand
A crosshair ( only for QwtPickerMachine::PointSelection )
@ EllipseRubberBand
An ellipse ( only for QwtPickerMachine::RectSelection )
@ PolygonRubberBand
A polygon ( only for QwtPickerMachine::PolygonSelection )
@ HLineRubberBand
A horizontal line ( only for QwtPickerMachine::PointSelection )
@ NoRubberBand
No rubberband.
@ RectRubberBand
A rectangle ( only for QwtPickerMachine::RectSelection )
@ Stretch
All points are scaled according to the new size,.
virtual QwtText trackerText(const QPoint &pos) const
Return the label for a position.
virtual void widgetMouseReleaseEvent(QMouseEvent *)
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
void appended(const QPoint &pos)
QWidget * parentWidget()
Return the parent widget, where the selection happens.
void moved(const QPoint &pos)
const QPolygon & pickedPoints() const
const QwtPickerMachine * stateMachine() const
void removed(const QPoint &pos)
void changed(const QPolygon &selection)
virtual QRect trackerRect(const QFont &) const
virtual void widgetMouseDoubleClickEvent(QMouseEvent *)
virtual void widgetKeyPressEvent(QKeyEvent *)
void setTrackerPen(const QPen &)
void setResizeMode(ResizeMode)
Set the resize mode.
virtual void widgetWheelEvent(QWheelEvent *)
QwtPicker(QWidget *parent)
QPolygon selection() const
A state machine for QwtPicker selections.
@ NoSelection
The state machine not usable for any type of selection.
@ RectSelection
The state machine is for selecting a rectangle (2 points).
@ PolygonSelection
The state machine is for selecting a polygon (many points).
@ PointSelection
The state machine is for selecting a single point.
A class representing a text.
void draw(QPainter *painter, const QRectF &rect) const