19 #include <qpainterpath.h> 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 );
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 )
80 painter->setPen( m_picker->rubberBandPen() );
81 m_picker->drawRubberBand( painter );
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 );
111 return m_picker->trackerMask();
123 stateMachine( NULL ),
128 trackerPosition( -1, -1 ),
129 mouseTracking( false ),
185 init( parent, rubberBand, trackerMode );
210 if ( parent->focusPolicy() == Qt::NoFocus )
211 parent->setFocusPolicy( Qt::WheelFocus );
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 );
385 w->installEventFilter(
this );
387 w->removeEventFilter(
this );
497 label = QString::number( pos.y() );
500 label = QString::number( pos.x() );
503 label = QString::number( pos.x() ) +
", " + QString::number( pos.y() );
543 switch ( selectionType )
548 if ( pa.count() < 1 )
551 const QPoint pos = pa[0];
554 const QRect pRect =
pickArea().boundingRect().toRect();
560 pos.x(), pRect.bottom() ), pw );
566 pRect.right(), pos.y() ), pw );
572 pos.x(), pRect.bottom() ), pw );
574 pRect.right(), pos.y() ), pw );
584 if ( pa.count() < 2 )
593 const QRect r = QRect( pa.first(), pa.last() );
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 );
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 );
827 const QSizeF textSize = text.
textSize( font );
828 QRect textRect( 0, 0,
qwtCeil( textSize.width() ),
qwtCeil( textSize.height() ) );
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 );
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() ) )
1083 #if QT_VERSION < 0x050e00 1084 const QPoint wheelPos = wheelEvent->pos();
1086 const QPoint wheelPos = wheelEvent->position().toPoint();
1088 if (
pickArea().contains( wheelPos ) )
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 ) ) );
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] )
1348 Q_EMIT
moved( pos );
1363 #if QT_VERSION >= 0x050100 1385 Q_UNUSED( selection );
1419 if ( oldSize.isEmpty() )
1426 const double xRatio = double( newSize.width() ) /
double( oldSize.width() );
1427 const double yRatio = double( newSize.height() ) /
double( oldSize.height() );
1432 p.setX( qRound( p.x() * xRatio ) );
1433 p.setY( qRound( p.y() * yRatio ) );
1461 widget->setMouseTracking(
true );
1480 path.addRect( widget->contentsRect() );
1490 bool showRubberband =
false;
1491 bool showTracker =
false;
1498 showRubberband =
true;
1513 if ( showRubberband )
1517 rw =
new Rubberband(
this, NULL );
1518 rw->setObjectName(
"PickerRubberBand" );
1520 rw->resize( w->size() );
1528 rw->updateOverlay();
1553 tw =
new Tracker(
this, NULL );
1554 tw->setObjectName(
"PickerTracker" );
1556 tw->resize( w->size() );
1559 tw->updateOverlay();
1593 #include "moc_qwt_picker.cpp"
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
virtual void widgetKeyPressEvent(QKeyEvent *)
virtual void stretchSelection(const QSize &oldSize, const QSize &newSize)
void setRubberBand(RubberBand)
QPen rubberBandPen() const
void setTrackerFont(const QFont &)
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *)=0
Transition.
virtual void widgetEnterEvent(QEvent *)
virtual void widgetKeyReleaseEvent(QKeyEvent *)
A crosshair ( only for QwtPickerMachine::PointSelection )
static QRegion qwtMaskRegion(const QRect &r, int penWidth)
virtual QRegion rubberBandMask() const
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
virtual QRegion trackerMask() const
ResizeMode resizeMode() const
The state machine not usable for any type of selection.
QWidget * parentWidget()
Return the parent widget, where the selection happens.
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
virtual QRect trackerRect(const QFont &) const
void reset()
Set the current state to 0.
const QwtPickerMachine * stateMachine() const
virtual bool eventFilter(QObject *, QEvent *) QWT_OVERRIDE
Event filter.
virtual void drawTracker(QPainter *) const
virtual bool accept(QPolygon &) const
Validate and fix up the selection.
void selected(const QPolygon &polygon)
virtual void widgetLeaveEvent(QEvent *)
void changed(const QPolygon &selection)
All points are scaled according to the new size,.
const QwtWidgetOverlay * rubberBandOverlay() const
void moved(const QPoint &pos)
The state machine is for selecting a polygon (many points).
void setMouseTracking(bool)
void draw(QPainter *painter, const QRectF &rect) const
DisplayMode trackerMode() const
QwtPicker::ResizeMode resizeMode
The state machine is for selecting a single point.
RubberBand rubberBand() const
A polygon ( only for QwtPickerMachine::PolygonSelection )
SelectionType selectionType() const
Return the selection type.
void setStateMachine(QwtPickerMachine *)
virtual void widgetMouseMoveEvent(QMouseEvent *)
virtual void transition(const QEvent *)
An ellipse ( only for QwtPickerMachine::RectSelection )
virtual void widgetMouseDoubleClickEvent(QMouseEvent *)
QwtPicker::DisplayMode trackerMode
A class representing a text.
A state machine for QwtPicker selections.
virtual void move(const QPoint &)
virtual QPainterPath pickArea() const
QPoint trackerPosition() const
QPointer< Tracker > trackerOverlay
A horizontal line ( only for QwtPickerMachine::PointSelection )
virtual void widgetWheelEvent(QWheelEvent *)
QwtPicker::RubberBand rubberBand
virtual void updateDisplay()
Update the state of rubber band and tracker label.
static void drawEllipse(QPainter *, const QRectF &)
Wrapper for QPainter::drawEllipse()
void setEnabled(bool)
En/disable the picker.
void appended(const QPoint &pos)
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
A rectangle ( only for QwtPickerMachine::RectSelection )
QPolygon selection() const
QFont trackerFont() const
bool keyMatch(KeyPatternCode, const QKeyEvent *) const
Compare a key event with an event pattern.
virtual void widgetMouseReleaseEvent(QMouseEvent *)
void setResizeMode(ResizeMode)
Set the resize mode.
virtual QPolygon adjustedPoints(const QPolygon &) const
Map the pickedPoints() into a selection()
virtual void drawRubberBand(QPainter *) const
A vertical line ( only for QwtPickerMachine::PointSelection )
void removed(const QPoint &pos)
virtual QwtText trackerText(const QPoint &pos) const
Return the label for a position.
QwtPicker(QWidget *parent)
QPointer< Rubberband > rubberBandOverlay
virtual ~QwtPicker()
Destructor.
qreal penWidth(const QPainter *painter, const QwtScaleDraw *scaleDraw)
const QPolygon & pickedPoints() const
const QwtWidgetOverlay * trackerOverlay() const
void setRubberBandPen(const QPen &)
virtual void widgetMousePressEvent(QMouseEvent *)
void init(QWidget *, RubberBand rubberBand, DisplayMode trackerMode)
Initialize the picker - used by the constructors.
void setTrackerPen(const QPen &)
QwtPicker provides selections on a widget.
The state machine is for selecting a rectangle (2 points).
QwtPickerMachine * stateMachine
virtual void append(const QPoint &)
Display only when the selection is active.