void RegionGrabber::resizeEvent( QResizeEvent* e ) { Q_UNUSED( e ); if ( selection.isNull() ) return; QRect r = selection; r.setTopLeft( limitPointToRect( r.topLeft(), rect() ) ); r.setBottomRight( limitPointToRect( r.bottomRight(), rect() ) ); if ( r.width() <= 1 || r.height() <= 1 ) //this just results in ugly drawing... selection = QRect(); else selection = normalizeSelection(r); }
void RegionGrabber::mouseMoveEvent( QMouseEvent* e ) { bool shouldShowHelp = !helpTextRect.contains( e->pos() ); if (shouldShowHelp != showHelp) { showHelp = shouldShowHelp; update(); } if ( mouseDown ) { if ( newSelection ) { QPoint p = e->pos(); QRect r = rect(); selection = normalizeSelection(QRect( dragStartPoint, limitPointToRect( p, r ) )); } else if ( mouseOverHandle == 0 ) // moving the whole selection { QRect r = rect().normalized(), s = selectionBeforeDrag.normalized(); QPoint p = s.topLeft() + e->pos() - dragStartPoint; r.setBottomRight( r.bottomRight() - QPoint( s.width(), s.height() ) + QPoint( 1, 1 ) ); if ( !r.isNull() && r.isValid() ) selection.moveTo( limitPointToRect( p, r ) ); } else // dragging a handle { QRect r = selectionBeforeDrag; QPoint offset = e->pos() - dragStartPoint; if ( mouseOverHandle == &TLHandle || mouseOverHandle == &THandle || mouseOverHandle == &TRHandle ) // dragging one of the top handles { r.setTop( r.top() + offset.y() ); } if ( mouseOverHandle == &TLHandle || mouseOverHandle == &LHandle || mouseOverHandle == &BLHandle ) // dragging one of the left handles { r.setLeft( r.left() + offset.x() ); } if ( mouseOverHandle == &BLHandle || mouseOverHandle == &BHandle || mouseOverHandle == &BRHandle ) // dragging one of the bottom handles { r.setBottom( r.bottom() + offset.y() ); } if ( mouseOverHandle == &TRHandle || mouseOverHandle == &RHandle || mouseOverHandle == &BRHandle ) // dragging one of the right handles { r.setRight( r.right() + offset.x() ); } r.setTopLeft( limitPointToRect( r.topLeft(), rect() ) ); r.setBottomRight( limitPointToRect( r.bottomRight(), rect() ) ); selection = normalizeSelection(r); } update(); } else { if ( selection.isNull() ) return; bool found = false; foreach( QRect* r, handles ) { if ( r->contains( e->pos() ) ) { mouseOverHandle = r; found = true; break; } } if ( !found ) { mouseOverHandle = 0; if ( selection.contains( e->pos() ) ) setCursor( Qt::OpenHandCursor ); else setCursor( Qt::CrossCursor ); } else { if ( mouseOverHandle == &TLHandle || mouseOverHandle == &BRHandle ) setCursor( Qt::SizeFDiagCursor ); if ( mouseOverHandle == &TRHandle || mouseOverHandle == &BLHandle ) setCursor( Qt::SizeBDiagCursor ); if ( mouseOverHandle == &LHandle || mouseOverHandle == &RHandle ) setCursor( Qt::SizeHorCursor ); if ( mouseOverHandle == &THandle || mouseOverHandle == &BHandle ) setCursor( Qt::SizeVerCursor ); } } }
void RegionGrab::mouseMoveEvent(QMouseEvent *event) { const bool shouldShowHelp = !m_helpTextRect.contains(event->pos()); if (shouldShowHelp != m_showHelp) { m_showHelp = shouldShowHelp; update(); } if (m_mouseDown) { if (m_newSelection) { QPoint p = event->pos(); QRect r = rect(); m_selection = normalizeSelection(QRect(m_dragStartPoint, limitPointToRect(p, r))); } // moving the whole selection else if (m_mouseOverHandle == 0) { QRect r = rect().normalized(), s = m_selectionBeforeDrag.normalized(); QPoint p = s.topLeft() + event->pos() - m_dragStartPoint; r.setBottomRight(r.bottomRight() - QPoint(s.width(), s.height()) + QPoint(1, 1)); if (!r.isNull() && r.isValid()) m_selection.moveTo(limitPointToRect(p, r)); } // dragging a handle else { QRect r = m_selectionBeforeDrag; QPoint offset = event->pos() - m_dragStartPoint; // dragging one of the top handles if (m_mouseOverHandle == &m_TLHandle || m_mouseOverHandle == &m_THandle || m_mouseOverHandle == &m_TRHandle) r.setTop(r.top() + offset.y()); // dragging one of the left handles if (m_mouseOverHandle == &m_TLHandle || m_mouseOverHandle == &m_LHandle || m_mouseOverHandle == &m_BLHandle) r.setLeft(r.left() + offset.x()); // dragging one of the bottom handles if (m_mouseOverHandle == &m_BLHandle || m_mouseOverHandle == &m_BHandle || m_mouseOverHandle == &m_BRHandle) r.setBottom(r.bottom() + offset.y()); // dragging one of the right handles if (m_mouseOverHandle == &m_TRHandle || m_mouseOverHandle == &m_RHandle || m_mouseOverHandle == &m_BRHandle) r.setRight(r.right() + offset.x()); r.setTopLeft(limitPointToRect(r.topLeft(), rect())); r.setBottomRight(limitPointToRect(r.bottomRight(), rect())); m_selection = normalizeSelection(r); } update(); } else { if (m_selection.isNull()) return; bool found = false; foreach (QRect* r, m_handles) { if (r->contains(event->pos())) { m_mouseOverHandle = r; found = true; break; } } if (!found) { m_mouseOverHandle = 0; if (m_selection.contains(event->pos())) setCursor(Qt::OpenHandCursor); else setCursor(Qt::CrossCursor); } else { if (m_mouseOverHandle == &m_TLHandle || m_mouseOverHandle == &m_BRHandle) setCursor(Qt::SizeFDiagCursor); if (m_mouseOverHandle == &m_TRHandle || m_mouseOverHandle == &m_BLHandle) setCursor(Qt::SizeBDiagCursor); if (m_mouseOverHandle == &m_LHandle || m_mouseOverHandle == &m_RHandle) setCursor(Qt::SizeHorCursor); if (m_mouseOverHandle == &m_THandle || m_mouseOverHandle == &m_BHandle) setCursor(Qt::SizeVerCursor); } } }