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 #include <qpainter.h>
14 #include <qpixmap.h>
15 #include <qevent.h>
16 #include <qcursor.h>
17 #include <qbitmap.h>
18 
19 static QVector<QwtPicker *> qwtActivePickers( QWidget *w )
20 {
21  QVector<QwtPicker *> pickers;
22 
23  QObjectList children = w->children();
24  for ( int i = 0; i < children.size(); i++ )
25  {
26  QwtPicker *picker = qobject_cast<QwtPicker *>( children[i] );
27  if ( picker && picker->isEnabled() )
28  pickers += picker;
29  }
30 
31  return pickers;
32 }
33 
35 {
36 public:
38  button( Qt::LeftButton ),
39  buttonModifiers( Qt::NoModifier ),
40  abortKey( Qt::Key_Escape ),
41  abortKeyModifiers( Qt::NoModifier ),
42 #ifndef QT_NO_CURSOR
43  cursor( NULL ),
44  restoreCursor( NULL ),
45  hasCursor( false ),
46 #endif
47  isEnabled( false )
48  {
49  orientations = Qt::Vertical | Qt::Horizontal;
50  }
51 
53  {
54 #ifndef QT_NO_CURSOR
55  delete cursor;
56  delete restoreCursor;
57 #endif
58  }
59 
60  Qt::MouseButton button;
61  Qt::KeyboardModifiers buttonModifiers;
62 
63  int abortKey;
64  Qt::KeyboardModifiers abortKeyModifiers;
65 
66  QPoint initialPos;
67  QPoint pos;
68 
69  QPixmap pixmap;
70  QBitmap contentsMask;
71 
72 #ifndef QT_NO_CURSOR
73  QCursor *cursor;
74  QCursor *restoreCursor;
75  bool hasCursor;
76 #endif
77  bool isEnabled;
78  Qt::Orientations orientations;
79 };
80 
86 QwtPanner::QwtPanner( QWidget *parent ):
87  QWidget( parent )
88 {
89  d_data = new PrivateData();
90 
91  setAttribute( Qt::WA_TransparentForMouseEvents );
92  setAttribute( Qt::WA_NoSystemBackground );
93  setFocusPolicy( Qt::NoFocus );
94  hide();
95 
96  setEnabled( true );
97 }
98 
101 {
102  delete d_data;
103 }
104 
109 void QwtPanner::setMouseButton( Qt::MouseButton button,
110  Qt::KeyboardModifiers modifiers )
111 {
112  d_data->button = button;
113  d_data->buttonModifiers = modifiers;
114 }
115 
117 void QwtPanner::getMouseButton( Qt::MouseButton &button,
118  Qt::KeyboardModifiers &modifiers ) const
119 {
120  button = d_data->button;
121  modifiers = d_data->buttonModifiers;
122 }
123 
131 void QwtPanner::setAbortKey( int key,
132  Qt::KeyboardModifiers modifiers )
133 {
134  d_data->abortKey = key;
135  d_data->abortKeyModifiers = modifiers;
136 }
137 
139 void QwtPanner::getAbortKey( int &key,
140  Qt::KeyboardModifiers &modifiers ) const
141 {
142  key = d_data->abortKey;
143  modifiers = d_data->abortKeyModifiers;
144 }
145 
154 #ifndef QT_NO_CURSOR
155 void QwtPanner::setCursor( const QCursor &cursor )
156 {
157  d_data->cursor = new QCursor( cursor );
158 }
159 #endif
160 
165 #ifndef QT_NO_CURSOR
166 const QCursor QwtPanner::cursor() const
167 {
168  if ( d_data->cursor )
169  return *d_data->cursor;
170 
171  if ( parentWidget() )
172  return parentWidget()->cursor();
173 
174  return QCursor();
175 }
176 #endif
177 
187 void QwtPanner::setEnabled( bool on )
188 {
189  if ( d_data->isEnabled != on )
190  {
191  d_data->isEnabled = on;
192 
193  QWidget *w = parentWidget();
194  if ( w )
195  {
196  if ( d_data->isEnabled )
197  {
198  w->installEventFilter( this );
199  }
200  else
201  {
202  w->removeEventFilter( this );
203  hide();
204  }
205  }
206  }
207 }
208 
215 void QwtPanner::setOrientations( Qt::Orientations o )
216 {
217  d_data->orientations = o;
218 }
219 
221 Qt::Orientations QwtPanner::orientations() const
222 {
223  return d_data->orientations;
224 }
225 
230 bool QwtPanner::isOrientationEnabled( Qt::Orientation o ) const
231 {
232  return d_data->orientations & o;
233 }
234 
240 {
241  return d_data->isEnabled;
242 }
243 
252 void QwtPanner::paintEvent( QPaintEvent *pe )
253 {
254  int dx = d_data->pos.x() - d_data->initialPos.x();
255  int dy = d_data->pos.y() - d_data->initialPos.y();
256 
257  QRect r( 0, 0, d_data->pixmap.width(), d_data->pixmap.height() );
258  r.moveCenter( QPoint( r.center().x() + dx, r.center().y() + dy ) );
259 
260  QPixmap pm( size() );
261  QwtPainter::fillPixmap( parentWidget(), pm );
262 
263  QPainter painter( &pm );
264 
265  if ( !d_data->contentsMask.isNull() )
266  {
267  QPixmap masked = d_data->pixmap;
268  masked.setMask( d_data->contentsMask );
269  painter.drawPixmap( r, masked );
270  }
271  else
272  {
273  painter.drawPixmap( r, d_data->pixmap );
274  }
275 
276  painter.end();
277 
278  if ( !d_data->contentsMask.isNull() )
279  pm.setMask( d_data->contentsMask );
280 
281  painter.begin( this );
282  painter.setClipRegion( pe->region() );
283  painter.drawPixmap( 0, 0, pm );
284 }
285 
295 QBitmap QwtPanner::contentsMask() const
296 {
297  return QBitmap();
298 }
299 
304 QPixmap QwtPanner::grab() const
305 {
306 #if QT_VERSION >= 0x050000
307  return parentWidget()->grab( parentWidget()->rect() );
308 #else
309  return QPixmap::grabWidget( parentWidget() );
310 #endif
311 }
312 
328 bool QwtPanner::eventFilter( QObject *object, QEvent *event )
329 {
330  if ( object == NULL || object != parentWidget() )
331  return false;
332 
333  switch ( event->type() )
334  {
335  case QEvent::MouseButtonPress:
336  {
337  widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
338  break;
339  }
340  case QEvent::MouseMove:
341  {
342  widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
343  break;
344  }
345  case QEvent::MouseButtonRelease:
346  {
347  widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
348  break;
349  }
350  case QEvent::KeyPress:
351  {
352  widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
353  break;
354  }
355  case QEvent::KeyRelease:
356  {
357  widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
358  break;
359  }
360  case QEvent::Paint:
361  {
362  if ( isVisible() )
363  return true;
364  break;
365  }
366  default:;
367  }
368 
369  return false;
370 }
371 
379 void QwtPanner::widgetMousePressEvent( QMouseEvent *mouseEvent )
380 {
381  if ( ( mouseEvent->button() != d_data->button )
382  || ( mouseEvent->modifiers() != d_data->buttonModifiers ) )
383  {
384  return;
385  }
386 
387  QWidget *w = parentWidget();
388  if ( w == NULL )
389  return;
390 
391 #ifndef QT_NO_CURSOR
392  showCursor( true );
393 #endif
394 
395  d_data->initialPos = d_data->pos = mouseEvent->pos();
396 
397  setGeometry( parentWidget()->rect() );
398 
399  // We don't want to grab the picker !
400  QVector<QwtPicker *> pickers = qwtActivePickers( parentWidget() );
401  for ( int i = 0; i < pickers.size(); i++ )
402  pickers[i]->setEnabled( false );
403 
404  d_data->pixmap = grab();
406 
407  for ( int i = 0; i < pickers.size(); i++ )
408  pickers[i]->setEnabled( true );
409 
410  show();
411 }
412 
419 void QwtPanner::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
420 {
421  if ( !isVisible() )
422  return;
423 
424  QPoint pos = mouseEvent->pos();
425  if ( !isOrientationEnabled( Qt::Horizontal ) )
426  pos.setX( d_data->initialPos.x() );
427  if ( !isOrientationEnabled( Qt::Vertical ) )
428  pos.setY( d_data->initialPos.y() );
429 
430  if ( pos != d_data->pos && rect().contains( pos ) )
431  {
432  d_data->pos = pos;
433  update();
434 
435  Q_EMIT moved( d_data->pos.x() - d_data->initialPos.x(),
436  d_data->pos.y() - d_data->initialPos.y() );
437  }
438 }
439 
447 void QwtPanner::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
448 {
449  if ( isVisible() )
450  {
451  hide();
452 #ifndef QT_NO_CURSOR
453  showCursor( false );
454 #endif
455 
456  QPoint pos = mouseEvent->pos();
457  if ( !isOrientationEnabled( Qt::Horizontal ) )
458  pos.setX( d_data->initialPos.x() );
459  if ( !isOrientationEnabled( Qt::Vertical ) )
460  pos.setY( d_data->initialPos.y() );
461 
462  d_data->pixmap = QPixmap();
463  d_data->contentsMask = QBitmap();
464  d_data->pos = pos;
465 
466  if ( d_data->pos != d_data->initialPos )
467  {
468  Q_EMIT panned( d_data->pos.x() - d_data->initialPos.x(),
469  d_data->pos.y() - d_data->initialPos.y() );
470  }
471  }
472 }
473 
480 void QwtPanner::widgetKeyPressEvent( QKeyEvent *keyEvent )
481 {
482  if ( ( keyEvent->key() == d_data->abortKey )
483  && ( keyEvent->modifiers() == d_data->abortKeyModifiers ) )
484  {
485  hide();
486 
487 #ifndef QT_NO_CURSOR
488  showCursor( false );
489 #endif
490  d_data->pixmap = QPixmap();
491  }
492 }
493 
500 void QwtPanner::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
501 {
502  Q_UNUSED( keyEvent );
503 }
504 
505 #ifndef QT_NO_CURSOR
506 void QwtPanner::showCursor( bool on )
507 {
508  if ( on == d_data->hasCursor )
509  return;
510 
511  QWidget *w = parentWidget();
512  if ( w == NULL || d_data->cursor == NULL )
513  return;
514 
515  d_data->hasCursor = on;
516 
517  if ( on )
518  {
519  if ( w->testAttribute( Qt::WA_SetCursor ) )
520  {
521  delete d_data->restoreCursor;
522  d_data->restoreCursor = new QCursor( w->cursor() );
523  }
524  w->setCursor( *d_data->cursor );
525  }
526  else
527  {
528  if ( d_data->restoreCursor )
529  {
530  w->setCursor( *d_data->restoreCursor );
531  delete d_data->restoreCursor;
532  d_data->restoreCursor = NULL;
533  }
534  else
535  w->unsetCursor();
536  }
537 }
538 #endif
const QCursor cursor() const
Definition: qwt_panner.cpp:166
virtual void widgetKeyReleaseEvent(QKeyEvent *)
Definition: qwt_panner.cpp:500
QwtPanner(QWidget *parent)
Definition: qwt_panner.cpp:86
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
void setCursor(const QCursor &)
Definition: qwt_panner.cpp:155
void setOrientations(Qt::Orientations)
Definition: qwt_panner.cpp:215
void setAbortKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
Definition: qwt_panner.cpp:131
virtual QBitmap contentsMask() const
Calculate a mask for the contents of the panned widget.
Definition: qwt_panner.cpp:295
void moved(int dx, int dy)
PrivateData * d_data
Definition: qwt_panner.h:99
virtual void widgetMouseMoveEvent(QMouseEvent *)
Definition: qwt_panner.cpp:419
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition: qwt_panner.cpp:447
Qt::KeyboardModifiers abortKeyModifiers
Definition: qwt_panner.cpp:64
bool isOrientationEnabled(Qt::Orientation) const
Definition: qwt_panner.cpp:230
virtual void paintEvent(QPaintEvent *)
Paint event.
Definition: qwt_panner.cpp:252
virtual void widgetKeyPressEvent(QKeyEvent *)
Definition: qwt_panner.cpp:480
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
Qt::Orientations orientations
Definition: qwt_panner.cpp:78
bool isEnabled
Definition: qwt_picker.h:101
virtual void widgetMousePressEvent(QMouseEvent *)
Definition: qwt_panner.cpp:379
void panned(int dx, int dy)
Qt::Orientations orientations() const
Return the orientation, where paning is enabled.
Definition: qwt_panner.cpp:221
void getAbortKey(int &key, Qt::KeyboardModifiers &) const
Get the abort key and modifiers.
Definition: qwt_panner.cpp:139
virtual QPixmap grab() const
Definition: qwt_panner.cpp:304
uintptr_t size
virtual ~QwtPanner()
Destructor.
Definition: qwt_panner.cpp:100
TFSIMD_FORCE_INLINE const tfScalar & w() const
Qt::KeyboardModifiers buttonModifiers
Definition: qwt_panner.cpp:61
bool isEnabled() const
Definition: qwt_panner.cpp:239
static QVector< QwtPicker * > qwtActivePickers(QWidget *w)
Definition: qwt_panner.cpp:19
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
Definition: qwt_panner.cpp:109
int i
Qt::MouseButton button
Definition: qwt_panner.cpp:60
void showCursor(bool)
Definition: qwt_panner.cpp:506
virtual bool eventFilter(QObject *, QEvent *)
Event filter.
Definition: qwt_panner.cpp:328
void setEnabled(bool)
En/disable the panner.
Definition: qwt_panner.cpp:187
QwtPicker provides selections on a widget.
Definition: qwt_picker.h:95
void getMouseButton(Qt::MouseButton &button, Qt::KeyboardModifiers &) const
Get mouse button and modifiers used for panning.
Definition: qwt_panner.cpp:117


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17