QwtPanner provides panning of a widget. More...
#include <qwt_panner.h>
Classes | |
class | PrivateData |
Signals | |
void | panned (int dx, int dy) |
void | moved (int dx, int dy) |
Public Member Functions | |
QwtPanner (QWidget *parent) | |
virtual | ~QwtPanner () |
Destructor. | |
void | setEnabled (bool) |
En/disable the panner. | |
bool | isEnabled () const |
void | setMouseButton (int button, int buttonState=Qt::NoButton) |
void | getMouseButton (int &button, int &buttonState) const |
Get the mouse button. | |
void | setAbortKey (int key, int state=Qt::NoButton) |
void | getAbortKey (int &key, int &state) const |
Get the abort key. | |
void | setCursor (const QCursor &) |
const QCursor | cursor () const |
void | enableOrientation (Qt::Orientation, bool enable) |
bool | isOrientationEnabled (Qt::Orientation) const |
virtual bool | eventFilter (QObject *, QEvent *) |
Event filter. | |
Protected Member Functions | |
virtual void | widgetMousePressEvent (QMouseEvent *) |
virtual void | widgetMouseReleaseEvent (QMouseEvent *) |
virtual void | widgetMouseMoveEvent (QMouseEvent *) |
virtual void | widgetKeyPressEvent (QKeyEvent *) |
virtual void | widgetKeyReleaseEvent (QKeyEvent *) |
virtual void | paintEvent (QPaintEvent *) |
Paint event. |
QwtPanner provides panning of a widget.
QwtPanner grabs the contents of a widget, that can be dragged in all directions. The offset between the start and the end position is emitted by the panned signal.
QwtPanner grabs the content of the widget into a pixmap and moves the pixmap around, without initiating any repaint events for the widget. Areas, that are not part of content are not painted while panning in in process. This makes panning fast enough for widgets, where repaints are too slow for mouse movements.
For widgets, where repaints are very fast it might be better to implement panning manually by mapping mouse events into paint events.
Definition at line 35 of file qwt_panner.h.
QwtPanner::QwtPanner | ( | QWidget * | parent ) |
Creates an panner that is enabled for the left mouse button.
parent | Parent widget to be panned |
Definition at line 121 of file qwt_panner.cpp.
References setEnabled().
: QWidget(parent) { d_data = new PrivateData(); #if QT_VERSION >= 0x040000 setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_NoSystemBackground); setFocusPolicy(Qt::NoFocus); #else setBackgroundMode(Qt::NoBackground); setFocusPolicy(QWidget::NoFocus); #endif hide(); setEnabled(true); }
QwtPanner::~QwtPanner | ( | ) | [virtual] |
const QCursor QwtPanner::cursor | ( | ) | const |
Definition at line 202 of file qwt_panner.cpp.
References QwtPanner::PrivateData::cursor.
void QwtPanner::enableOrientation | ( | Qt::Orientation | o, |
bool | enable | ||
) |
Definition at line 264 of file qwt_panner.cpp.
References QwtPanner::PrivateData::orientations.
{ if ( o == Qt::Vertical || o == Qt::Horizontal ) d_data->orientations[o] = enable; }
bool QwtPanner::eventFilter | ( | QObject * | o, |
QEvent * | e | ||
) | [virtual] |
Event filter.
When isEnabled() the mouse events of the observed widget are filtered.
Definition at line 344 of file qwt_panner.cpp.
References widgetKeyPressEvent(), widgetKeyReleaseEvent(), widgetMouseMoveEvent(), widgetMousePressEvent(), and widgetMouseReleaseEvent().
{ if ( o == NULL || o != parentWidget() ) return false; switch(e->type()) { case QEvent::MouseButtonPress: { widgetMousePressEvent((QMouseEvent *)e); break; } case QEvent::MouseMove: { widgetMouseMoveEvent((QMouseEvent *)e); break; } case QEvent::MouseButtonRelease: { widgetMouseReleaseEvent((QMouseEvent *)e); break; } case QEvent::KeyPress: { widgetKeyPressEvent((QKeyEvent *)e); break; } case QEvent::KeyRelease: { widgetKeyReleaseEvent((QKeyEvent *)e); break; } case QEvent::Paint: { if ( isVisible() ) return true; break; } default:; } return false; }
void QwtPanner::getAbortKey | ( | int & | key, |
int & | state | ||
) | const |
Get the abort key.
Definition at line 176 of file qwt_panner.cpp.
References QwtPanner::PrivateData::abortKey, and QwtPanner::PrivateData::abortKeyState.
{ key = d_data->abortKey; state = d_data->abortKeyState; }
void QwtPanner::getMouseButton | ( | int & | button, |
int & | buttonState | ||
) | const |
Get the mouse button.
Definition at line 156 of file qwt_panner.cpp.
References QwtPanner::PrivateData::button, and QwtPanner::PrivateData::buttonState.
{ button = d_data->button; buttonState = d_data->buttonState; }
bool QwtPanner::isEnabled | ( | ) | const |
Definition at line 290 of file qwt_panner.cpp.
References QwtPanner::PrivateData::isEnabled.
{ return d_data->isEnabled; }
bool QwtPanner::isOrientationEnabled | ( | Qt::Orientation | o ) | const |
Return true if a orientatio is enabled
Definition at line 275 of file qwt_panner.cpp.
References QwtPanner::PrivateData::orientations.
Referenced by widgetMouseMoveEvent(), and widgetMouseReleaseEvent().
{ #if QT_VERSION >= 0x040000 return d_data->orientations & o; #else if ( o == Qt::Vertical || o == Qt::Horizontal ) return d_data->orientations[o]; return false; #endif }
void QwtPanner::moved | ( | int | dx, |
int | dy | ||
) | [signal] |
Signal emitted, while the widget moved, but panning is not finished.
dx | Offset in horizontal direction |
dy | Offset in vertical direction |
Referenced by widgetMouseMoveEvent().
void QwtPanner::paintEvent | ( | QPaintEvent * | pe ) | [protected, virtual] |
Paint event.
Repaint the grabbed pixmap on its current position and fill the empty spaces by the background of the parent widget.
pe | Paint event |
Definition at line 303 of file qwt_panner.cpp.
References QwtPanner::PrivateData::initialPos, QwtPanner::PrivateData::pixmap, and QwtPanner::PrivateData::pos.
{ QPixmap pm(size()); QPainter painter(&pm); const QColor bg = #if QT_VERSION < 0x040000 parentWidget()->palette().color( QPalette::Normal, QColorGroup::Background); #else parentWidget()->palette().color( QPalette::Normal, QPalette::Background); #endif painter.setPen(Qt::NoPen); painter.setBrush(QBrush(bg)); painter.drawRect(0, 0, pm.width(), pm.height()); int dx = d_data->pos.x() - d_data->initialPos.x(); int dy = d_data->pos.y() - d_data->initialPos.y(); QRect r(0, 0, d_data->pixmap.width(), d_data->pixmap.height()); r.moveCenter(QPoint(r.center().x() + dx, r.center().y() + dy)); painter.drawPixmap(r, d_data->pixmap); painter.end(); painter.begin(this); painter.setClipRegion(pe->region()); painter.drawPixmap(0, 0, pm); }
void QwtPanner::panned | ( | int | dx, |
int | dy | ||
) | [signal] |
Signal emitted, when panning is done
dx | Offset in horizontal direction |
dy | Offset in vertical direction |
Referenced by QwtPlotPanner::QwtPlotPanner(), and widgetMouseReleaseEvent().
void QwtPanner::setAbortKey | ( | int | key, |
int | state = Qt::NoButton |
||
) |
Change the abort key The defaults are Qt::Key_Escape and Qt::NoButton
key | Key ( See Qt::Keycode ) |
state | State |
Definition at line 169 of file qwt_panner.cpp.
References QwtPanner::PrivateData::abortKey, and QwtPanner::PrivateData::abortKeyState.
{ d_data->abortKey = key; d_data->abortKeyState = state; }
void QwtPanner::setCursor | ( | const QCursor & | cursor ) |
Change the cursor, that is active while panning The default is the cursor of the parent widget.
cursor | New cursor |
Definition at line 191 of file qwt_panner.cpp.
References QwtPanner::PrivateData::cursor.
void QwtPanner::setEnabled | ( | bool | on ) |
En/disable the panner.
When enabled is true an event filter is installed for the observed widget, otherwise the event filter is removed.
on | true or false |
Definition at line 223 of file qwt_panner.cpp.
References QwtPanner::PrivateData::isEnabled.
Referenced by QwtPanner(), and widgetMousePressEvent().
void QwtPanner::setMouseButton | ( | int | button, |
int | buttonState = Qt::NoButton |
||
) |
Change the mouse button The defaults are Qt::LeftButton and Qt::NoButton
Definition at line 149 of file qwt_panner.cpp.
References QwtPanner::PrivateData::button, and QwtPanner::PrivateData::buttonState.
{ d_data->button = button; d_data->buttonState = buttonState; }
void QwtPanner::widgetKeyPressEvent | ( | QKeyEvent * | ke ) | [protected, virtual] |
Handle a key press event for the observed widget.
ke | Key event |
Definition at line 509 of file qwt_panner.cpp.
References QwtPanner::PrivateData::abortKey, QwtPanner::PrivateData::abortKeyState, and QwtPanner::PrivateData::pixmap.
Referenced by eventFilter().
{ if ( ke->key() == d_data->abortKey ) { const bool matched = #if QT_VERSION < 0x040000 (ke->state() & Qt::KeyButtonMask) == (d_data->abortKeyState & Qt::KeyButtonMask); #else (ke->modifiers() & Qt::KeyboardModifierMask) == (int)(d_data->abortKeyState & Qt::KeyboardModifierMask); #endif if ( matched ) { hide(); #ifndef QT_NO_CURSOR showCursor(false); #endif d_data->pixmap = QPixmap(); } } }
void QwtPanner::widgetKeyReleaseEvent | ( | QKeyEvent * | ) | [protected, virtual] |
Handle a key release event for the observed widget.
Definition at line 536 of file qwt_panner.cpp.
Referenced by eventFilter().
{ }
void QwtPanner::widgetMouseMoveEvent | ( | QMouseEvent * | me ) | [protected, virtual] |
Handle a mouse move event for the observed widget.
me | Mouse event |
Definition at line 449 of file qwt_panner.cpp.
References QwtPanner::PrivateData::initialPos, isOrientationEnabled(), moved(), and QwtPanner::PrivateData::pos.
Referenced by eventFilter().
{ if ( !isVisible() ) return; QPoint pos = me->pos(); if ( !isOrientationEnabled(Qt::Horizontal) ) pos.setX(d_data->initialPos.x()); if ( !isOrientationEnabled(Qt::Vertical) ) pos.setY(d_data->initialPos.y()); if ( pos != d_data->pos && rect().contains(pos) ) { d_data->pos = pos; update(); emit moved(d_data->pos.x() - d_data->initialPos.x(), d_data->pos.y() - d_data->initialPos.y()); } }
void QwtPanner::widgetMousePressEvent | ( | QMouseEvent * | me ) | [protected, virtual] |
Handle a mouse press event for the observed widget.
me | Mouse event |
Definition at line 395 of file qwt_panner.cpp.
References activePickers(), QwtPanner::PrivateData::button, QwtPanner::PrivateData::buttonState, QwtPanner::PrivateData::initialPos, int(), QwtPanner::PrivateData::pixmap, QwtPanner::PrivateData::pos, and setEnabled().
Referenced by eventFilter().
{ if ( me->button() != d_data->button ) return; QWidget *w = parentWidget(); if ( w == NULL ) return; #if QT_VERSION < 0x040000 if ( (me->state() & Qt::KeyButtonMask) != (d_data->buttonState & Qt::KeyButtonMask) ) #else if ( (me->modifiers() & Qt::KeyboardModifierMask) != (int)(d_data->buttonState & Qt::KeyboardModifierMask) ) #endif { return; } #ifndef QT_NO_CURSOR showCursor(true); #endif d_data->initialPos = d_data->pos = me->pos(); QRect cr = parentWidget()->rect(); if ( parentWidget()->inherits("QFrame") ) { const QFrame* frame = (QFrame*)parentWidget(); cr = frame->contentsRect(); } setGeometry(cr); // We don't want to grab the picker ! QwtArray<QwtPicker *> pickers = activePickers(parentWidget()); for ( int i = 0; i < (int)pickers.size(); i++ ) pickers[i]->setEnabled(false); d_data->pixmap = QPixmap::grabWidget(parentWidget(), cr.x(), cr.y(), cr.width(), cr.height()); for ( int i = 0; i < (int)pickers.size(); i++ ) pickers[i]->setEnabled(true); show(); }
void QwtPanner::widgetMouseReleaseEvent | ( | QMouseEvent * | me ) | [protected, virtual] |
Handle a mouse release event for the observed widget.
me | Mouse event |
Definition at line 477 of file qwt_panner.cpp.
References QwtPanner::PrivateData::initialPos, isOrientationEnabled(), panned(), QwtPanner::PrivateData::pixmap, and QwtPanner::PrivateData::pos.
Referenced by eventFilter().
{ if ( isVisible() ) { hide(); #ifndef QT_NO_CURSOR showCursor(false); #endif QPoint pos = me->pos(); if ( !isOrientationEnabled(Qt::Horizontal) ) pos.setX(d_data->initialPos.x()); if ( !isOrientationEnabled(Qt::Vertical) ) pos.setY(d_data->initialPos.y()); d_data->pixmap = QPixmap(); d_data->pos = pos; if ( d_data->pos != d_data->initialPos ) { emit panned(d_data->pos.x() - d_data->initialPos.x(), d_data->pos.y() - d_data->initialPos.y()); } } }