2015-03-10 01:32:30 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2015 by The qTox Project
|
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2015-03-10 01:32:30 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2015-03-10 01:32:30 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2015-03-10 01:32:30 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2015-03-10 01:32:30 +08:00
|
|
|
*/
|
|
|
|
|
2015-03-25 09:48:26 +08:00
|
|
|
#include "screengrabberchooserrectitem.h"
|
2015-03-10 01:32:30 +08:00
|
|
|
|
2015-05-08 06:10:33 +08:00
|
|
|
#include <cstdlib>
|
2015-05-08 05:31:42 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QCursor>
|
|
|
|
|
2015-03-26 23:40:47 +08:00
|
|
|
enum {
|
|
|
|
HandleSize = 10,
|
|
|
|
MinRectSize = 2,
|
|
|
|
};
|
2015-03-10 01:32:30 +08:00
|
|
|
|
|
|
|
ScreenGrabberChooserRectItem::ScreenGrabberChooserRectItem(QGraphicsScene* scene)
|
|
|
|
{
|
|
|
|
scene->addItem(this);
|
|
|
|
setCursor(QCursor(Qt::OpenHandCursor));
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
this->mainRect = createHandleItem(scene);
|
|
|
|
this->topLeft = createHandleItem(scene);
|
|
|
|
this->topCenter = createHandleItem(scene);
|
|
|
|
this->topRight = createHandleItem(scene);
|
|
|
|
this->rightCenter = createHandleItem(scene);
|
|
|
|
this->bottomRight = createHandleItem(scene);
|
|
|
|
this->bottomCenter = createHandleItem(scene);
|
|
|
|
this->bottomLeft = createHandleItem(scene);
|
|
|
|
this->leftCenter = createHandleItem(scene);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
this->topLeft->setCursor(QCursor(Qt::SizeFDiagCursor));
|
|
|
|
this->bottomRight->setCursor(QCursor(Qt::SizeFDiagCursor));
|
|
|
|
this->topRight->setCursor(QCursor(Qt::SizeBDiagCursor));
|
|
|
|
this->bottomLeft->setCursor(QCursor(Qt::SizeBDiagCursor));
|
|
|
|
this->leftCenter->setCursor(QCursor(Qt::SizeHorCursor));
|
|
|
|
this->rightCenter->setCursor(QCursor(Qt::SizeHorCursor));
|
|
|
|
this->topCenter->setCursor(QCursor(Qt::SizeVerCursor));
|
|
|
|
this->bottomCenter->setCursor(QCursor(Qt::SizeVerCursor));
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
this->mainRect->setRect(QRect());
|
|
|
|
hideHandles();
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScreenGrabberChooserRectItem::~ScreenGrabberChooserRectItem()
|
|
|
|
{
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF ScreenGrabberChooserRectItem::boundingRect() const
|
|
|
|
{
|
2015-03-26 23:44:59 +08:00
|
|
|
return QRectF(-HandleSize - 1, -HandleSize - 1,
|
|
|
|
rectWidth + HandleSize + 1, rectHeight + HandleSize + 1);
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:40:47 +08:00
|
|
|
void ScreenGrabberChooserRectItem::beginResize(QPointF mousePos)
|
2015-03-10 01:32:30 +08:00
|
|
|
{
|
2015-03-26 23:40:47 +08:00
|
|
|
rectWidth = this->rectHeight = 0;
|
|
|
|
mainRect->setRect(QRect());
|
|
|
|
state = Resizing;
|
|
|
|
startPos = mousePos;
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
setCursor(QCursor(Qt::CrossCursor));
|
|
|
|
hideHandles();
|
2015-03-26 23:40:47 +08:00
|
|
|
mainRect->grabMouse();
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect ScreenGrabberChooserRectItem::chosenRect() const
|
|
|
|
{
|
2015-03-26 23:44:59 +08:00
|
|
|
QRect rect (x(), y(), rectWidth, rectHeight);
|
|
|
|
if (rectWidth < 0)
|
|
|
|
{
|
|
|
|
rect.setX(rect.x() + rectWidth);
|
|
|
|
rect.setWidth(-rectWidth);
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-26 23:44:59 +08:00
|
|
|
if (rectHeight < 0)
|
|
|
|
{
|
|
|
|
rect.setY(rect.y() + rectHeight);
|
|
|
|
rect.setHeight(-rectHeight);
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-26 23:44:59 +08:00
|
|
|
return rect;
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::showHandles()
|
|
|
|
{
|
|
|
|
this->topLeft->show();
|
|
|
|
this->topCenter->show();
|
|
|
|
this->topRight->show();
|
|
|
|
this->rightCenter->show();
|
|
|
|
this->bottomRight->show();
|
|
|
|
this->bottomCenter->show();
|
|
|
|
this->bottomLeft->show();
|
|
|
|
this->leftCenter->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::hideHandles()
|
|
|
|
{
|
|
|
|
this->topLeft->hide();
|
|
|
|
this->topCenter->hide();
|
|
|
|
this->topRight->hide();
|
|
|
|
this->rightCenter->hide();
|
|
|
|
this->bottomRight->hide();
|
|
|
|
this->bottomCenter->hide();
|
|
|
|
this->bottomLeft->hide();
|
|
|
|
this->leftCenter->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mousePress(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
this->state = Moving;
|
|
|
|
setCursor(QCursor(Qt::ClosedHandCursor));
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mouseMove(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (this->state == Moving)
|
|
|
|
{
|
|
|
|
QPointF delta = event->scenePos() - event->lastScenePos();
|
|
|
|
moveBy (delta.x(), delta.y());
|
|
|
|
}
|
|
|
|
else if (this->state == Resizing)
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
QPointF size = event->scenePos() - scenePos();
|
2015-03-26 23:40:47 +08:00
|
|
|
mainRect->setRect (0, 0, size.x(), size.y());
|
|
|
|
rectWidth = size.x();
|
|
|
|
rectHeight = size.y();
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
updateHandlePositions();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
emit regionChosen(chosenRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mouseRelease(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
setCursor(QCursor(Qt::OpenHandCursor));
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-26 23:40:47 +08:00
|
|
|
QPointF delta = (event->scenePos() - startPos);
|
|
|
|
if (qAbs(delta.x()) < MinRectSize || qAbs(delta.y()) < MinRectSize)
|
|
|
|
{
|
|
|
|
rectWidth = rectHeight = 0;
|
|
|
|
mainRect->setRect(QRect());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-26 23:53:58 +08:00
|
|
|
QRect normalized = chosenRect();
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-26 23:53:58 +08:00
|
|
|
rectWidth = normalized.width();
|
|
|
|
rectHeight = normalized.height();
|
|
|
|
setPos(normalized.x(), normalized.y());
|
|
|
|
mainRect->setRect(0, 0, rectWidth, rectHeight);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-26 23:53:58 +08:00
|
|
|
updateHandlePositions();
|
2015-03-26 23:40:47 +08:00
|
|
|
showHandles();
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
emit regionChosen(chosenRect());
|
2015-03-26 23:53:58 +08:00
|
|
|
state = None;
|
|
|
|
mainRect->ungrabMouse();
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mouseDoubleClick(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
emit doubleClicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mousePressHandle(int x, int y, QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(x);
|
|
|
|
Q_UNUSED(y);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
this->state = HandleResizing;
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mouseMoveHandle(int x, int y, QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (this->state != HandleResizing)
|
|
|
|
return;
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
QPointF delta = event->scenePos() - event->lastScenePos();
|
|
|
|
delta.rx() *= qreal(std::abs(x));
|
|
|
|
delta.ry() *= qreal(std::abs(y));
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
// We increase if the multiplier and the delta have the same sign
|
|
|
|
bool increaseX = ((x < 0) == (delta.x() < 0));
|
|
|
|
bool increaseY = ((y < 0) == (delta.y() < 0));
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if((delta.x() < 0 && increaseX) || (delta.x() >= 0 && !increaseX))
|
|
|
|
{
|
|
|
|
moveBy(delta.x(), 0);
|
|
|
|
delta.rx() *= -1;
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if((delta.y() < 0 && increaseY) || (delta.y() >= 0 && !increaseY))
|
|
|
|
{
|
|
|
|
moveBy(0, delta.y());
|
|
|
|
delta.ry() *= -1;
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
//
|
2015-03-10 01:32:30 +08:00
|
|
|
this->rectWidth += delta.x();
|
|
|
|
this->rectHeight += delta.y();
|
|
|
|
this->mainRect->setRect (0, 0, this->rectWidth, this->rectHeight);
|
|
|
|
updateHandlePositions();
|
|
|
|
emit regionChosen(chosenRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::mouseReleaseHandle(int x, int y, QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(x);
|
|
|
|
Q_UNUSED(y);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
this->state = None;
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QPoint ScreenGrabberChooserRectItem::getHandleMultiplier(QGraphicsItem* handle)
|
|
|
|
{
|
|
|
|
if (handle == this->topLeft)
|
|
|
|
return QPoint(-1, -1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->topCenter)
|
|
|
|
return QPoint(0, -1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->topRight)
|
|
|
|
return QPoint(1, -1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->rightCenter)
|
|
|
|
return QPoint(1, 0);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->bottomRight)
|
|
|
|
return QPoint(1, 1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->bottomCenter)
|
|
|
|
return QPoint(0, 1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->bottomLeft)
|
|
|
|
return QPoint(-1, 1);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (handle == this->leftCenter)
|
|
|
|
return QPoint(-1, 0);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
return QPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::updateHandlePositions()
|
|
|
|
{
|
|
|
|
this->topLeft->setPos(-HandleSize, -HandleSize);
|
|
|
|
this->topCenter->setPos((this->rectWidth - HandleSize) / 2, -HandleSize);
|
|
|
|
this->topRight->setPos(this->rectWidth, -HandleSize);
|
|
|
|
this->rightCenter->setPos(this->rectWidth, (this->rectHeight - HandleSize) / 2);
|
|
|
|
this->bottomRight->setPos(this->rectWidth, this->rectHeight);
|
|
|
|
this->bottomCenter->setPos((this->rectWidth - HandleSize) / 2, this->rectHeight);
|
|
|
|
this->bottomLeft->setPos(-HandleSize, this->rectHeight);
|
|
|
|
this->leftCenter->setPos(-HandleSize, (this->rectHeight - HandleSize) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsRectItem* ScreenGrabberChooserRectItem::createHandleItem(QGraphicsScene* scene)
|
|
|
|
{
|
|
|
|
QGraphicsRectItem* handle = new QGraphicsRectItem(0, 0, HandleSize, HandleSize);
|
|
|
|
handle->setPen(QPen(Qt::blue));
|
|
|
|
handle->setBrush(Qt::NoBrush);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
scene->addItem(handle);
|
|
|
|
addToGroup(handle);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
handle->installSceneEventFilter(this);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScreenGrabberChooserRectItem::sceneEventFilter(QGraphicsItem* watched, QEvent* event)
|
|
|
|
{
|
|
|
|
if (watched == this->mainRect)
|
|
|
|
forwardMainRectEvent(event);
|
|
|
|
else
|
|
|
|
forwardHandleEvent(watched, event);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::forwardMainRectEvent(QEvent* event)
|
|
|
|
{
|
|
|
|
QGraphicsSceneMouseEvent* mouseEvent = static_cast<QGraphicsSceneMouseEvent*>(event);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
switch(event->type())
|
|
|
|
{
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
return mousePress(mouseEvent);
|
|
|
|
case QEvent::GraphicsSceneMouseMove:
|
|
|
|
return mouseMove(mouseEvent);
|
|
|
|
case QEvent::GraphicsSceneMouseRelease:
|
|
|
|
return mouseRelease(mouseEvent);
|
|
|
|
case QEvent::GraphicsSceneMouseDoubleClick:
|
|
|
|
return mouseDoubleClick(mouseEvent);
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenGrabberChooserRectItem::forwardHandleEvent(QGraphicsItem* watched, QEvent* event)
|
|
|
|
{
|
|
|
|
QGraphicsSceneMouseEvent* mouseEvent = static_cast<QGraphicsSceneMouseEvent*>(event);
|
|
|
|
QPoint multiplier = getHandleMultiplier(watched);
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
if (multiplier.isNull())
|
|
|
|
return;
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
switch(event->type())
|
|
|
|
{
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
return mousePressHandle(multiplier.x(), multiplier.y(), mouseEvent);
|
|
|
|
case QEvent::GraphicsSceneMouseMove:
|
|
|
|
return mouseMoveHandle(multiplier.x(), multiplier.y(), mouseEvent);
|
|
|
|
case QEvent::GraphicsSceneMouseRelease:
|
|
|
|
return mouseReleaseHandle(multiplier.x(), multiplier.y(), mouseEvent);
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2015-03-10 01:32:30 +08:00
|
|
|
}
|