Add cursor slowdown

WOW https://i.arsenarsen.com/khk0kdd2bh.png
This commit is contained in:
ArsenArsen 2017-07-08 21:40:09 +02:00
parent 97db7ddc0c
commit 15ca46f3d0
21 changed files with 100 additions and 69 deletions

View File

@ -21,7 +21,7 @@
#include <settings.hpp>
CropScene::CropScene(QObject *parent, QPixmap pixmap)
: QGraphicsScene(parent), drawingSelectionMaker([] { return nullptr; }), prevButtons(Qt::NoButton),
: QGraphicsScene(parent), cursorPos(0, 0), drawingSelectionMaker([] { return nullptr; }), prevButtons(Qt::NoButton),
_brush(Qt::SolidPattern), _font(settings::settings().value("font", QFont()).value<QFont>()) {
_pixmap = pixmap;
pen().setColor(settings::settings().value("penColor", pen().color()).value<QColor>());
@ -64,13 +64,23 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
show();
});
menu.addAction(settings);
QPolygonF cursorPoly;
cursorPoly << QPoint(-10, 0) //
<< QPoint(10, 0) //
<< QPoint(0, 0) //
<< QPoint(0, 10) //
<< QPoint(0, -10) //
<< QPoint(0, 0);
magnifier = addPixmap(pixmap.copy(0, 0, 11, 11).scaled(110, 110));
cursorItem = addPolygon(cursorPoly, QPen(Qt::white));
cursorItem->setZValue(3);
magnifier = addPixmap(QPixmap(110, 110));
magnifierBox = addRect(magnifier->boundingRect(), QPen(Qt::cyan));
magnifier->setZValue(1);
magnifier->setZValue(3);
magnifierBox->setZValue(1.1);
magnifierBox->setParentItem(magnifier);
magnifierHint = addText("ptr: (0, 0)\nsel: (0, 0, 0, 0)");
magnifierHint = addText("ptr: (0, 0)\nsel: (-1, -1, 0, 0)");
magnifierHint->setParentItem(magnifier);
magnifierHint->setY(magnifier->boundingRect().height());
QColor c(Qt::cyan);
@ -80,20 +90,25 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
magnifierHintBox->setZValue(1);
magnifierHint->setZValue(1.1);
initMagnifierGrid();
updateMag(QPointF(0, 0));
updateMag();
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
QPolygonF poly;
QRect prect = pixmap.rect();
poly.append(prect.topLeft());
poly.append(prect.topRight());
poly.append(prect.bottomRight());
poly.append(prect.bottomLeft());
polyItem = new QGraphicsPolygonItem(poly);
polyItem->setBrush(QBrush(QColor(0, 0, 0, 191)));
polyItem->setPen(QPen(Qt::NoPen));
addItem(polyItem);
QTimer::singleShot(0, [&] {
QPolygonF poly;
poly.append(sceneRect().topLeft());
poly.append(sceneRect().topRight());
poly.append(sceneRect().bottomRight());
poly.append(sceneRect().bottomLeft());
polyItem = new QGraphicsPolygonItem(poly);
polyItem->setBrush(QBrush(QColor(0, 0, 0, 191)));
polyItem->setPen(QPen(Qt::NoPen));
addItem(polyItem);
auto pf = views()[0]->mapFromGlobal(QCursor::pos());
cursorPos = QPoint(pf.x(), pf.y());
cursorItem->setPos(cursorPos);
updateMag();
});
}
@ -147,14 +162,20 @@ void CropScene::fontAsk() {
}
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
updateMag(e->scenePos());
QPointF delta = e->scenePos() - cursorPos;
if (e->modifiers() & Qt::ShiftModifier) {
cursorPos += delta / 2;
QCursor::setPos(views()[0]->mapToGlobal(cursorPos.toPoint()));
} else
cursorPos = e->scenePos();
cursorItem->setPos(cursorPos);
updateMag();
auto buttons = e->buttons();
if (e->modifiers() & Qt::ControlModifier && buttons == Qt::LeftButton) {
QTransform stupidThing = views()[0]->transform();
auto item = itemAt(e->scenePos(), stupidThing);
auto item = itemAt(cursorPos, stupidThing);
if (item && item != polyItem && item != rect && item->zValue() != -1) {
QPointF delta = e->scenePos() - e->lastScenePos();
item->moveBy(delta.x(), delta.y());
}
return;
@ -163,14 +184,14 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
if (drawingSelection) {
drawingSelection->mouseDragEvent(e, this);
} else {
QPointF p = e->scenePos();
QPointF p = cursorPos;
if (rect == nullptr) {
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
initPos = p;
QPen pen(Qt::NoBrush, 1);
pen.setColor(Qt::cyan);
rect->setPen(pen);
rect->setZValue(-0.1);
rect->setZValue(1);
addItem(rect);
} else {
if (prevButtons == Qt::NoButton) {
@ -218,7 +239,7 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
void CropScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
if (e->modifiers() & Qt::AltModifier) {
QTransform stupidThing = views()[0]->transform();
auto item = itemAt(e->scenePos(), stupidThing);
auto item = itemAt(cursorPos, stupidThing);
if (item && item != polyItem && item != rect && item->zValue() != -1) {
removeItem(item);
}
@ -240,7 +261,7 @@ void CropScene::wheelEvent(QGraphicsSceneWheelEvent *event) {
gridRectsY.clear();
initMagnifierGrid();
updateMag(event->scenePos());
updateMag();
}
void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item) {
@ -250,30 +271,32 @@ void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawIt
menu.addAction(action);
}
QPoint contextOffset(5, 5);
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
display->setText(drawingName);
menu.exec(e->screenPos());
menu.exec((cursorPos + contextOffset).toPoint());
e->accept();
}
void CropScene::keyReleaseEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Escape)
if (((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !drawingSelection) || event->key() == Qt::Key_Escape)
done(event->key() != Qt::Key_Escape);
}
void CropScene::updateMag(QPointF scenePos) {
QString rectStr("(0, 0, 0, 0)");
void CropScene::updateMag() {
QString rectStr("(-1, -1, 0, 0)");
if (rect) {
rectStr = "(%0, %1, %2, %3)";
rectStr = rectStr.arg(rect->rect().x()).arg(rect->rect().y()).arg(rect->rect().width()).arg(rect->rect().height());
}
magnifierHint->setPlainText(QString("ptr: (%0, %1)\nsel: %2").arg(scenePos.x()).arg(scenePos.y()).arg(rectStr));
magnifierHint->setPlainText(QString("ptr: (%0, %1)\nsel: %2").arg(qRound(cursorPos.x())).arg(qRound(cursorPos.y())).arg(rectStr));
magnifierHintBox->setRect(magnifierHint->boundingRect());
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
if (pixCnt % 2 == 0) pixCnt++;
QPointF magnifierTopLeft = scenePos - QPointF(pixCnt / 2., pixCnt / 2.);
QPointF magnifierPos = scenePos + QPointF(5, 5);
QPointF magnifierTopLeft = cursorPos - QPointF(pixCnt / 2., pixCnt / 2.);
QPointF magnifierPos = cursorPos + QPointF(5, 5);
magnifier->setPos(magnifierPos);
magnifier->setPixmap(_pixmap.copy(magnifierTopLeft.x(), magnifierTopLeft.y(), pixCnt, pixCnt).scaled(110, 110));
@ -316,6 +339,7 @@ void CropScene::done(bool notEsc) {
if (notEsc && rect) {
rect->setPen(QPen(Qt::NoPen));
magnifier->setVisible(false);
cursorItem->setVisible(false);
magnifierBox->setVisible(false);
magnifierHint->setVisible(false);
magnifierHintBox->setVisible(false);

View File

@ -34,6 +34,9 @@ public:
void hide();
void show();
void setVisible(bool visible);
QPointF cursorPosition() {
return cursorPos;
}
public slots:
void fontAsk();
@ -47,15 +50,15 @@ protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e) override;
void wheelEvent(QGraphicsSceneWheelEvent *event) override; // WHEEEEEEL
void contextMenuEvent(QGraphicsSceneContextMenuEvent *e) override;
void keyReleaseEvent(QKeyEvent *e) override;
private:
void updateMag(QPointF scenePos);
void updateMag();
void initMagnifierGrid();
void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item);
void done(bool notEsc);
bool fullscreen;
QPointF cursorPos;
std::function<DrawItem *()> drawingSelectionMaker;
QFlags<Qt::MouseButton> prevButtons;
QPixmap _pixmap;
@ -75,6 +78,7 @@ private:
QAction *display;
QList<QGraphicsRectItem *> gridRectsX;
QList<QGraphicsRectItem *> gridRectsY;
QGraphicsPolygonItem *cursorItem = nullptr;
};
#endif // CROPSCENE_HPP

View File

@ -9,4 +9,5 @@ CropView::CropView(QGraphicsScene *scene) : QGraphicsView(scene) {
setCursor(QCursor(Qt::CrossCursor));
setMouseTracking(true);
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
setCursor(Qt::BlankCursor);
}

View File

@ -2,9 +2,9 @@
#include <QtMath>
#include <settings.hpp>
void ArrowItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void ArrowItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (init.isNull()) {
init = e->scenePos();
init = scene->cursorPosition();
line = scene->addLine(QLineF(init, init), scene->pen());
QPainterPath poly;
qreal w = settings::settings().value("arrow/width", 20).toDouble() / 2;
@ -14,8 +14,9 @@ void ArrowItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
poly.lineTo(QPoint(w, h));
head = scene->addPath(poly, scene->pen(), scene->brush());
} else {
line->setLine(QLineF(init, e->scenePos()));
head->setRotation(270 + qRadiansToDegrees(qAtan2((init.y() - e->scenePos().y()), (init.x() - e->scenePos().x()))));
line->setLine(QLineF(init, scene->cursorPosition()));
head->setRotation(
270 + qRadiansToDegrees(qAtan2((init.y() - scene->cursorPosition().y()), (init.x() - scene->cursorPosition().x()))));
}
head->setPos(e->scenePos());
head->setPos(scene->cursorPosition());
}

View File

@ -11,7 +11,7 @@ public:
QString name() override {
return "Arrow";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override {
}

View File

@ -13,16 +13,16 @@ bool BlurItem::init(CropScene *) {
return true;
}
void BlurItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void BlurItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (pos.isNull()) {
pos = e->scenePos();
rect = scene->addRect(QRect(e->scenePos().toPoint(), QSize(1, 1)), QPen(Qt::cyan), Qt::NoBrush);
pos = scene->cursorPosition();
rect = scene->addRect(QRect(scene->cursorPosition().toPoint(), QSize(1, 1)), QPen(Qt::cyan), Qt::NoBrush);
pixmap = scene->addPixmap(scene->pixmap().copy(rect->rect().toRect()));
pixmap->setPos(e->scenePos());
pixmap->setPos(scene->cursorPosition());
pixmap->setZValue(rect->zValue() - 0.1);
pixmap->setGraphicsEffect(effect);
} else {
QPointF p = e->scenePos();
QPointF p = scene->cursorPosition();
rect->setRect(QRect(qMin(pos.x(), p.x()), qMin(pos.y(), p.y()), qAbs(pos.x() - p.x()), qAbs(pos.y() - p.y())));
pixmap->setPixmap(scene->pixmap().copy(rect->rect().toRect()));
pixmap->setPos(rect->rect().topLeft());

View File

@ -14,7 +14,7 @@ public:
}
bool init(CropScene *) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:

View File

@ -7,7 +7,7 @@ DotItem::~DotItem() {
}
void DotItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
scene->addEllipse(e->pos().x() - 1.5, e->pos().y() - 1.5, 3, 3, scene->pen(), scene->brush())->setPos(e->scenePos());
scene->addEllipse(e->pos().x() - 1.5, e->pos().y() - 1.5, 3, 3, scene->pen(), scene->brush())->setPos(scene->cursorPosition());
}
void DotItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) {

View File

@ -1,11 +1,11 @@
#include "ellipseitem.hpp"
void EllipseItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void EllipseItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (!ellie) {
ellie = scene->addEllipse(e->scenePos().x(), e->scenePos().y(), 0, 0, scene->pen(), scene->brush());
initPos = e->scenePos();
ellie = scene->addEllipse(scene->cursorPosition().x(), scene->cursorPosition().y(), 0, 0, scene->pen(), scene->brush());
initPos = scene->cursorPosition();
} else {
auto p = e->scenePos();
auto p = scene->cursorPosition();
ellie->setRect(QRectF(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()),
qAbs(initPos.y() - p.y())));
}

View File

@ -12,7 +12,7 @@ public:
}
~EllipseItem() {
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override {
}

View File

@ -6,9 +6,9 @@ EraserItem::EraserItem() {
EraserItem::~EraserItem() {
}
void EraserItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void EraserItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
for (auto i : scene->items()) {
if (i->contains(e->scenePos()) && i->zValue() != -1 && i != scene->polyItm() && i != scene->selRect()) {
if (i->contains(scene->cursorPosition()) && i->zValue() != -1 && i != scene->polyItm() && i != scene->selRect()) {
scene->removeItem(i);
break;
}

View File

@ -10,7 +10,7 @@ public:
QString name() override {
return "Eraser";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override {
}
};

View File

@ -3,12 +3,12 @@
LineItem::LineItem() {
}
void LineItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void LineItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (init.isNull()) {
init = e->scenePos();
init = scene->cursorPosition();
line = scene->addLine(QLineF(init, init), scene->pen());
} else {
line->setLine(QLineF(init, e->scenePos()));
line->setLine(QLineF(init, scene->cursorPosition()));
}
}

View File

@ -9,7 +9,7 @@ public:
QString name() override {
return "Straight line";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:

View File

@ -9,13 +9,13 @@ PathItem::~PathItem() {
delete path;
}
void PathItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void PathItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (path == nullptr) {
path = new QPainterPath(e->scenePos());
path = new QPainterPath(scene->cursorPosition());
pathItem = scene->addPath(*path, scene->pen(),
settings::settings().value("brushPath", false).toBool() ? scene->brush() : QBrush());
} else {
path->quadTo(path->currentPosition(), e->scenePos());
path->quadTo(path->currentPosition(), scene->cursorPosition());
pathItem->setPath(*path);
}
}

View File

@ -11,7 +11,7 @@ public:
QString name() {
return "Path";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene);
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene);
void mouseDragEndEvent(QGraphicsSceneMouseEvent *e, CropScene *scene);
private:

View File

@ -6,14 +6,14 @@ RectItem::RectItem() {
RectItem::~RectItem() {
}
void RectItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void RectItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (!rect) {
rect = scene->addRect(e->scenePos().x(), e->scenePos().y(), 0, 0);
rect = scene->addRect(scene->cursorPosition().x(), scene->cursorPosition().y(), 0, 0);
rect->setBrush(scene->brush());
rect->setPen(scene->pen());
initPos = e->scenePos();
initPos = scene->cursorPosition();
} else {
auto p = e->scenePos();
auto p = scene->cursorPosition();
rect->setRect(
QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), qAbs(initPos.y() - p.y())));
}

View File

@ -10,7 +10,7 @@ public:
QString name() override {
return "Rectangle";
}
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:

View File

@ -10,15 +10,15 @@ bool TextItem::init(CropScene *s) {
return ok;
}
void TextItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) {
void TextItem::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) {
if (!textItem) {
textItem = scene->addSimpleText(text, scene->font());
textItem->setPos(e->scenePos());
textItem->setPos(scene->cursorPosition());
textItem->setPen(scene->pen().color());
textItem->setBrush(scene->pen().color());
} else {
auto ee
= 180 + qRadiansToDegrees(qAtan2((textItem->pos().y() - e->scenePos().y()), (textItem->pos().x() - e->scenePos().x())));
auto ee = 180 + qRadiansToDegrees(qAtan2((textItem->pos().y() - scene->cursorPosition().y()),
(textItem->pos().x() - scene->cursorPosition().x())));
textItem->setRotation(ee);
}
}

View File

@ -8,7 +8,7 @@ class TextItem : public DrawItem {
public:
QString name() override;
bool init(CropScene *s) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) override;
void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override;
void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override;
private:

View File

@ -32,6 +32,7 @@ bool stillAlive = true;
}
void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
if (!verbose && msg.startsWith("QPixmap::fromWinHBITMAP")) return;
std::string stdMsg = msg.toStdString();
switch (type) {
case QtDebugMsg: