qwt_panner.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_panner.h"
11 #include "qwt_picker.h"
12 #include "qwt_painter.h"
13 
14 #include <qpainter.h>
15 #include <qpixmap.h>
16 #include <qevent.h>
17 #include <qcursor.h>
18 #include <qbitmap.h>
19 
21 {
22  QVector<QwtPicker *> pickers;
23 
24  QObjectList children = w->children();
25  for ( int i = 0; i < children.size(); i++ )
26  {
27  QwtPicker *picker = qobject_cast<QwtPicker *>( children[i] );
28  if ( picker && picker->isEnabled() )
29  pickers += picker;
30  }
31 
32  return pickers;
33 }
34 
36 {
37 public:
39  button( Qt::LeftButton ),
40  buttonModifiers( Qt::NoModifier ),
41  abortKey( Qt::Key_Escape ),
42  abortKeyModifiers( Qt::NoModifier ),
43 #ifndef QT_NO_CURSOR
44  cursor( NULL ),
45  restoreCursor( NULL ),
46  hasCursor( false ),
47 #endif
48  isEnabled( false ),
49  orientations( Qt::Vertical | Qt::Horizontal )
50  {
51  }
52 
54  {
55 #ifndef QT_NO_CURSOR
56  delete cursor;
57  delete restoreCursor;
58 #endif
59  }
60 
61  Qt::MouseButton button;
62  Qt::KeyboardModifiers buttonModifiers;
63 
64  int abortKey;
65  Qt::KeyboardModifiers abortKeyModifiers;
66 
67  QPoint initialPos;
68  QPoint pos;
69 
70  QPixmap pixmap;
71  QBitmap contentsMask;
72 
73 #ifndef QT_NO_CURSOR
74  QCursor *cursor;
75  QCursor *restoreCursor;
76  bool hasCursor;
77 #endif
78  bool isEnabled;
79  Qt::Orientations orientations;
80 };
81 
87 QwtPanner::QwtPanner( QWidget *parent ):
88  QWidget( parent )
89 {
90  d_data = new PrivateData();
91 
92  setAttribute( Qt::WA_TransparentForMouseEvents );
93  setAttribute( Qt::WA_NoSystemBackground );
94  setFocusPolicy( Qt::NoFocus );
95  hide();
96 
97  setEnabled( true );
98 }
99 
102 {
103  delete d_data;
104 }
105 
110 void QwtPanner::setMouseButton( Qt::MouseButton button,
111  Qt::KeyboardModifiers modifiers )
112 {
113  d_data->button = button;
114  d_data->buttonModifiers = modifiers;
115 }
116 
118 void QwtPanner::getMouseButton( Qt::MouseButton &button,
119  Qt::KeyboardModifiers &modifiers ) const
120 {
121  button = d_data->button;
122  modifiers = d_data->buttonModifiers;
123 }
124 
133  Qt::KeyboardModifiers modifiers )
134 {
135  d_data->abortKey = key;
136  d_data->abortKeyModifiers = modifiers;
137 }
138 
140 void QwtPanner::getAbortKey( int &key,
141  Qt::KeyboardModifiers &modifiers ) const
142 {
143  key = d_data->abortKey;
144  modifiers = d_data->abortKeyModifiers;
145 }
146 
155 #ifndef QT_NO_CURSOR
156 void QwtPanner::setCursor( const QCursor &cursor )
157 {
158  d_data->cursor = new QCursor( cursor );
159 }
160 #endif
161 
166 #ifndef QT_NO_CURSOR
167 const QCursor QwtPanner::cursor() const
168 {
169  if ( d_data->cursor )
170  return *d_data->cursor;
171 
172  if ( parentWidget() )
173  return parentWidget()->cursor();
174 
175  return QCursor();
176 }
177 #endif
178 
188 void QwtPanner::setEnabled( bool on )
189 {
190  if ( d_data->isEnabled != on )
191  {
192  d_data->isEnabled = on;
193 
194  QWidget *w = parentWidget();
195  if ( w )
196  {
197  if ( d_data->isEnabled )
198  {
199  w->installEventFilter( this );
200  }
201  else
202  {
203  w->removeEventFilter( this );
204  hide();
205  }
206  }
207  }
208 }
209 
216 void QwtPanner::setOrientations( Qt::Orientations o )
217 {
218  d_data->orientations = o;
219 }
220 
222 Qt::Orientations QwtPanner::orientations() const
223 {
224  return d_data->orientations;
225 }
226 
231 bool QwtPanner::isOrientationEnabled( Qt::Orientation o ) const
232 {
233  return d_data->orientations & o;
234 }
235 
241 {
242  return d_data->isEnabled;
243 }
244 
253 void QwtPanner::paintEvent( QPaintEvent *event )
254 {
255  int dx = d_data->pos.x() - d_data->initialPos.x();
256  int dy = d_data->pos.y() - d_data->initialPos.y();
257 
258  QRectF r;
259  r.setSize( d_data->pixmap.size() / QwtPainter::devicePixelRatio( &d_data->pixmap ) );
260  r.moveCenter( QPointF( r.center().x() + dx, r.center().y() + dy ) );
261 
262  QPixmap pm = QwtPainter::backingStore( this, size() );
263  QwtPainter::fillPixmap( parentWidget(), pm );
264 
265  QPainter painter( &pm );
266 
267  if ( !d_data->contentsMask.isNull() )
268  {
269  QPixmap masked = d_data->pixmap;
270  masked.setMask( d_data->contentsMask );
271  painter.drawPixmap( r.toRect(), masked );
272  }
273  else
274  {
275  painter.drawPixmap( r.toRect(), d_data->pixmap );
276  }
277 
278  painter.end();
279 
280  if ( !d_data->contentsMask.isNull() )
281  pm.setMask( d_data->contentsMask );
282 
283  painter.begin( this );
284  painter.setClipRegion( event->region() );
285  painter.drawPixmap( 0, 0, pm );
286 }
287 
297 QBitmap QwtPanner::contentsMask() const
298 {
299  return QBitmap();
300 }
301 
306 QPixmap QwtPanner::grab() const
307 {
308 #if QT_VERSION >= 0x050000
309  return parentWidget()->grab( parentWidget()->rect() );
310 #else
311  return QPixmap::grabWidget( parentWidget() );
312 #endif
313 }
314 
330 bool QwtPanner::eventFilter( QObject *object, QEvent *event )
331 {
332  if ( object == NULL || object != parentWidget() )
333  return false;
334 
335  switch ( event->type() )
336  {
337  case QEvent::MouseButtonPress:
338  {
339  widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
340  break;
341  }
342  case QEvent::MouseMove:
343  {
344  widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
345  break;
346  }
347  case QEvent::MouseButtonRelease:
348  {
349  widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
350  break;
351  }
352  case QEvent::KeyPress:
353  {
354  widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
355  break;
356  }
357  case QEvent::KeyRelease:
358  {
359  widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
360  break;
361  }
362  case QEvent::Paint:
363  {
364  if ( isVisible() )
365  return true;
366  break;
367  }
368  default:;
369  }
370 
371  return false;
372 }
373 
381 void QwtPanner::widgetMousePressEvent( QMouseEvent *mouseEvent )
382 {
383  if ( ( mouseEvent->button() != d_data->button )
384  || ( mouseEvent->modifiers() != d_data->buttonModifiers ) )
385  {
386  return;
387  }
388 
389  QWidget *w = parentWidget();
390  if ( w == NULL )
391  return;
392 
393 #ifndef QT_NO_CURSOR
394  showCursor( true );
395 #endif
396 
397  d_data->initialPos = d_data->pos = mouseEvent->pos();
398 
399  setGeometry( parentWidget()->rect() );
400 
401  // We don't want to grab the picker !
402  QVector<QwtPicker *> pickers = qwtActivePickers( parentWidget() );
403  for ( int i = 0; i < pickers.size(); i++ )
404  pickers[i]->setEnabled( false );
405 
406  d_data->pixmap = grab();
408 
409  for ( int i = 0; i < pickers.size(); i++ )
410  pickers[i]->setEnabled( true );
411 
412  show();
413 }
414 
421 void QwtPanner::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
422 {
423  if ( !isVisible() )
424  return;
425 
426  QPoint pos = mouseEvent->pos();
427  if ( !isOrientationEnabled( Qt::Horizontal ) )
428  pos.setX( d_data->initialPos.x() );
429  if ( !isOrientationEnabled( Qt::Vertical ) )
430  pos.setY( d_data->initialPos.y() );
431 
432  if ( pos != d_data->pos && rect().contains( pos ) )
433  {
434  d_data->pos = pos;
435  update();
436 
437  Q_EMIT moved( d_data->pos.x() - d_data->initialPos.x(),
438  d_data->pos.y() - d_data->initialPos.y() );
439  }
440 }
441 
449 void QwtPanner::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
450 {
451  if ( isVisible() )
452  {
453  hide();
454 #ifndef QT_NO_CURSOR
455  showCursor( false );
456 #endif
457 
458  QPoint pos = mouseEvent->pos();
459  if ( !isOrientationEnabled( Qt::Horizontal ) )
460  pos.setX( d_data->initialPos.x() );
461  if ( !isOrientationEnabled( Qt::Vertical ) )
462  pos.setY( d_data->initialPos.y() );
463 
464  d_data->pixmap = QPixmap();
465  d_data->contentsMask = QBitmap();
466  d_data->pos = pos;
467 
468  if ( d_data->pos != d_data->initialPos )
469  {
470  Q_EMIT panned( d_data->pos.x() - d_data->initialPos.x(),
471  d_data->pos.y() - d_data->initialPos.y() );
472  }
473  }
474 }
475 
482 void QwtPanner::widgetKeyPressEvent( QKeyEvent *keyEvent )
483 {
484  if ( ( keyEvent->key() == d_data->abortKey )
485  && ( keyEvent->modifiers() == d_data->abortKeyModifiers ) )
486  {
487  hide();
488 
489 #ifndef QT_NO_CURSOR
490  showCursor( false );
491 #endif
492  d_data->pixmap = QPixmap();
493  }
494 }
495 
502 void QwtPanner::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
503 {
504  Q_UNUSED( keyEvent );
505 }
506 
507 #ifndef QT_NO_CURSOR
508 void QwtPanner::showCursor( bool on )
509 {
510  if ( on == d_data->hasCursor )
511  return;
512 
513  QWidget *w = parentWidget();
514  if ( w == NULL || d_data->cursor == NULL )
515  return;
516 
517  d_data->hasCursor = on;
518 
519  if ( on )
520  {
521  if ( w->testAttribute( Qt::WA_SetCursor ) )
522  {
523  delete d_data->restoreCursor;
524  d_data->restoreCursor = new QCursor( w->cursor() );
525  }
526  w->setCursor( *d_data->cursor );
527  }
528  else
529  {
530  if ( d_data->restoreCursor )
531  {
532  w->setCursor( *d_data->restoreCursor );
533  delete d_data->restoreCursor;
534  d_data->restoreCursor = NULL;
535  }
536  else
537  w->unsetCursor();
538  }
539 }
540 #endif
541 
542 #if QWT_MOC_INCLUDE
543 #include "moc_qwt_panner.cpp"
544 #endif
const QCursor cursor() const
Definition: qwt_panner.cpp:167
virtual void widgetKeyReleaseEvent(QKeyEvent *)
Definition: qwt_panner.cpp:502
QwtPanner(QWidget *parent)
Definition: qwt_panner.cpp:87
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
void setCursor(const QCursor &)
Definition: qwt_panner.cpp:156
void setOrientations(Qt::Orientations)
Definition: qwt_panner.cpp:216
void setAbortKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
Definition: qwt_panner.cpp:132
virtual QBitmap contentsMask() const
Calculate a mask for the contents of the panned widget.
Definition: qwt_panner.cpp:297
void moved(int dx, int dy)
PrivateData * d_data
Definition: qwt_panner.h:99
virtual void widgetMouseMoveEvent(QMouseEvent *)
Definition: qwt_panner.cpp:421
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition: qwt_panner.cpp:449
Qt::KeyboardModifiers abortKeyModifiers
Definition: qwt_panner.cpp:65
bool isOrientationEnabled(Qt::Orientation) const
Definition: qwt_panner.cpp:231
virtual void widgetKeyPressEvent(QKeyEvent *)
Definition: qwt_panner.cpp:482
Qt::Orientations orientations
Definition: qwt_panner.cpp:79
bool isEnabled
Definition: qwt_picker.h:110
static qreal devicePixelRatio(const QPaintDevice *)
virtual void widgetMousePressEvent(QMouseEvent *)
Definition: qwt_panner.cpp:381
virtual bool eventFilter(QObject *, QEvent *) QWT_OVERRIDE
Event filter.
Definition: qwt_panner.cpp:330
virtual void paintEvent(QPaintEvent *) QWT_OVERRIDE
Paint event.
Definition: qwt_panner.cpp:253
void panned(int dx, int dy)
Qt::Orientations orientations() const
Return the orientation, where paning is enabled.
Definition: qwt_panner.cpp:222
void getAbortKey(int &key, Qt::KeyboardModifiers &) const
Get the abort key and modifiers.
Definition: qwt_panner.cpp:140
virtual QPixmap grab() const
Definition: qwt_panner.cpp:306
virtual ~QwtPanner()
Destructor.
Definition: qwt_panner.cpp:101
Qt::KeyboardModifiers buttonModifiers
Definition: qwt_panner.cpp:62
bool isEnabled() const
Definition: qwt_panner.cpp:240
static QVector< QwtPicker * > qwtActivePickers(QWidget *w)
Definition: qwt_panner.cpp:20
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
Definition: qwt_panner.cpp:110
Qt::MouseButton button
Definition: qwt_panner.cpp:61
void showCursor(bool)
Definition: qwt_panner.cpp:508
void setEnabled(bool)
En/disable the panner.
Definition: qwt_panner.cpp:188
QwtPicker provides selections on a widget.
Definition: qwt_picker.h:104
static QPixmap backingStore(QWidget *, const QSize &)
void getMouseButton(Qt::MouseButton &button, Qt::KeyboardModifiers &) const
Get mouse button and modifiers used for panning.
Definition: qwt_panner.cpp:118


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10