Browse Source

Screen grabber: Reject small selections (< 2px width/height)

When the user just clicks, or moves the cursor barely (less than 2px),
auto reject the region and show no selection.
pull/1552/merge
Stefan Merettig 11 years ago committed by tux3
parent
commit
96a1cf93d5
  1. 35
      src/widget/tool/screengrabberchooserrectitem.cpp
  2. 3
      src/widget/tool/screengrabberchooserrectitem.h
  3. 2
      src/widget/tool/screenshotgrabber.cpp

35
src/widget/tool/screengrabberchooserrectitem.cpp

@ -21,7 +21,10 @@
#include <QPainter> #include <QPainter>
#include <QCursor> #include <QCursor>
enum { HandleSize = 10 }; enum {
HandleSize = 10,
MinRectSize = 2,
};
ScreenGrabberChooserRectItem::ScreenGrabberChooserRectItem(QGraphicsScene* scene) ScreenGrabberChooserRectItem::ScreenGrabberChooserRectItem(QGraphicsScene* scene)
{ {
@ -62,13 +65,16 @@ QRectF ScreenGrabberChooserRectItem::boundingRect() const
return QRectF(0, 0, this->rectWidth, this->rectHeight); return QRectF(0, 0, this->rectWidth, this->rectHeight);
} }
void ScreenGrabberChooserRectItem::beginResize() void ScreenGrabberChooserRectItem::beginResize(QPointF mousePos)
{ {
this->rectWidth = this->rectHeight = 0; rectWidth = this->rectHeight = 0;
this->state = Resizing; mainRect->setRect(QRect());
state = Resizing;
startPos = mousePos;
setCursor(QCursor(Qt::CrossCursor)); setCursor(QCursor(Qt::CrossCursor));
hideHandles(); hideHandles();
this->mainRect->grabMouse(); mainRect->grabMouse();
} }
QRect ScreenGrabberChooserRectItem::chosenRect() const QRect ScreenGrabberChooserRectItem::chosenRect() const
@ -122,9 +128,9 @@ void ScreenGrabberChooserRectItem::mouseMove(QGraphicsSceneMouseEvent* event)
{ {
prepareGeometryChange(); prepareGeometryChange();
QPointF size = event->scenePos() - scenePos(); QPointF size = event->scenePos() - scenePos();
this->mainRect->setRect (0, 0, size.x(), size.y()); mainRect->setRect (0, 0, size.x(), size.y());
this->rectWidth = size.x(); rectWidth = size.x();
this->rectHeight = size.y(); rectHeight = size.y();
updateHandlePositions(); updateHandlePositions();
} }
@ -141,10 +147,21 @@ void ScreenGrabberChooserRectItem::mouseRelease(QGraphicsSceneMouseEvent* event)
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
setCursor(QCursor(Qt::OpenHandCursor)); setCursor(QCursor(Qt::OpenHandCursor));
QPointF delta = (event->scenePos() - startPos);
if (qAbs(delta.x()) < MinRectSize || qAbs(delta.y()) < MinRectSize)
{
rectWidth = rectHeight = 0;
mainRect->setRect(QRect());
}
else
{
showHandles();
}
emit regionChosen(chosenRect()); emit regionChosen(chosenRect());
this->state = None; this->state = None;
this->mainRect->ungrabMouse(); this->mainRect->ungrabMouse();
showHandles();
} }
} }

3
src/widget/tool/screengrabberchooserrectitem.h

@ -27,7 +27,7 @@ public:
~ScreenGrabberChooserRectItem(); ~ScreenGrabberChooserRectItem();
QRectF boundingRect() const; QRectF boundingRect() const;
void beginResize(); void beginResize(QPointF mousePos);
QRect chosenRect() const; QRect chosenRect() const;
@ -54,6 +54,7 @@ private:
State state = None; State state = None;
int rectWidth = 0; int rectWidth = 0;
int rectHeight = 0; int rectHeight = 0;
QPointF startPos;
void forwardMainRectEvent(QEvent* event); void forwardMainRectEvent(QEvent* event);
void forwardHandleEvent(QGraphicsItem* watched, QEvent* event); void forwardHandleEvent(QGraphicsItem* watched, QEvent* event);

2
src/widget/tool/screenshotgrabber.cpp

@ -179,5 +179,5 @@ void ScreenshotGrabber::beginRectChooser(QGraphicsSceneMouseEvent* event)
QPointF pos = event->scenePos(); QPointF pos = event->scenePos();
this->chooserRect->setX(pos.x()); this->chooserRect->setX(pos.x());
this->chooserRect->setY(pos.y()); this->chooserRect->setY(pos.y());
this->chooserRect->beginResize(); this->chooserRect->beginResize(event->scenePos());
} }

Loading…
Cancel
Save