From 423383dca981e40b289191b2a9e207d4be34fde0 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Fri, 8 Sep 2017 23:47:56 +0200 Subject: [PATCH] Fix the hotspots --- src/cropeditor/cropscene.cpp | 49 +++++++++++++++------------ src/cropeditor/cropscene.hpp | 4 ++- src/cropeditor/selectionrectangle.cpp | 4 +++ src/cropeditor/selectionrectangle.hpp | 1 + src/main.cpp | 1 + 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/cropeditor/cropscene.cpp b/src/cropeditor/cropscene.cpp index aefc21f..4ed9a47 100644 --- a/src/cropeditor/cropscene.cpp +++ b/src/cropeditor/cropscene.cpp @@ -246,7 +246,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { cursorItem->setPos(cursorPos); updateMag(); - if (rect) { + if (rect && !drawingRect) { // qAbs(e->scenePos().() - rect->rect().()) < 10 bool close = false; QRectF newRect = rect->rect(); @@ -254,21 +254,21 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { if (qAbs(e->scenePos().y() - rect->rect().bottom()) < 10) { close = true; views()[0]->setCursor(Qt::SizeFDiagCursor); - if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setBottomRight(cursorPos); + if (e->buttons() & Qt::LeftButton) newRect.setBottomRight(cursorPos); } else if (qAbs(e->scenePos().y() - rect->rect().top()) < 10) { close = true; views()[0]->setCursor(Qt::SizeBDiagCursor); - if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setTopRight(cursorPos); + if (e->buttons() & Qt::LeftButton) newRect.setTopRight(cursorPos); } } else if (qAbs(e->scenePos().x() - rect->rect().left()) < 10) { if (qAbs(e->scenePos().y() - rect->rect().top()) < 10) { close = true; views()[0]->setCursor(Qt::SizeFDiagCursor); - if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setTopLeft(cursorPos); + if (e->buttons() & Qt::LeftButton) newRect.setTopLeft(cursorPos); } else if (qAbs(e->scenePos().y() - rect->rect().bottom()) < 10) { close = true; views()[0]->setCursor(Qt::SizeBDiagCursor); - if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setBottomLeft(cursorPos); + if (e->buttons() & Qt::LeftButton) newRect.setBottomLeft(cursorPos); } } if (!close) @@ -276,6 +276,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { else { rect->setRect(newRect); prevButtons = e->buttons(); + updatePoly(); return; } } @@ -292,6 +293,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { } else { QPointF p = cursorPos; if (rect == nullptr) { + drawingRect = true; rect = new SelectionRectangle(p.x(), p.y(), 1, 1); initPos = p; QPen pen(Qt::NoBrush, 1); @@ -308,22 +310,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { qAbs(initPos.y() - p.y()))); } } - QPolygonF poly; - QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom()); - poly << sceneRect().topLeft(); - poly << sceneRect().topRight(); - poly << sceneRect().bottomRight(); - poly << theMagicWikipediaPoint; - poly << rect->rect().bottomRight(); - poly << rect->rect().topRight(); - poly << rect->rect().topLeft(); - poly << rect->rect().bottomLeft(); - poly << rect->rect().bottomRight(); - poly << theMagicWikipediaPoint; - poly << sceneRect().bottomLeft(); - poly << sceneRect().topLeft(); - - this->polyItem->setPolygon(poly); + updatePoly(); e->accept(); } } @@ -333,6 +320,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { } void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { + drawingRect = false; if (drawingSelection) { drawingSelection->mouseDragEndEvent(e, this); delete drawingSelection; @@ -424,6 +412,25 @@ void CropScene::updateMag() { magnifier->setPos(magnifierPos); } +void CropScene::updatePoly() { + QPolygonF poly; + QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom()); + poly << sceneRect().topLeft(); + poly << sceneRect().topRight(); + poly << sceneRect().bottomRight(); + poly << theMagicWikipediaPoint; + poly << rect->rect().bottomRight(); + poly << rect->rect().topRight(); + poly << rect->rect().topLeft(); + poly << rect->rect().bottomLeft(); + poly << rect->rect().bottomRight(); + poly << theMagicWikipediaPoint; + poly << sceneRect().bottomLeft(); + poly << sceneRect().topLeft(); + + this->polyItem->setPolygon(poly); +} + void CropScene::initMagnifierGrid() { if (!gridRectsX.isEmpty() || !gridRectsY.isEmpty()) return; diff --git a/src/cropeditor/cropscene.hpp b/src/cropeditor/cropscene.hpp index c9d0a1b..9387557 100644 --- a/src/cropeditor/cropscene.hpp +++ b/src/cropeditor/cropscene.hpp @@ -78,13 +78,15 @@ private slots: private: void updateMag(); + void updatePoly(); void initMagnifierGrid(); void addDrawingAction(QMenuBar *menu, QString name, QString icon, std::function item); QPointF cursorPos; std::function drawingSelectionMaker; QFlags prevButtons; QPixmap _pixmap; - SelectionRectangle *rect = nullptr; + QGraphicsRectItem *rect = nullptr; + bool drawingRect = true; QGraphicsPixmapItem *magnifier = nullptr; QGraphicsRectItem *magnifierBox = nullptr; QGraphicsTextItem *magnifierHint = nullptr; diff --git a/src/cropeditor/selectionrectangle.cpp b/src/cropeditor/selectionrectangle.cpp index b7c3245..bebb9a4 100644 --- a/src/cropeditor/selectionrectangle.cpp +++ b/src/cropeditor/selectionrectangle.cpp @@ -12,6 +12,10 @@ SelectionRectangle::SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGrap : QGraphicsRectItem(x, y, w, h, parent) { } +SelectionRectangle::SelectionRectangle(QRectF rect, QGraphicsItem *parent) +: SelectionRectangle(rect.left(), rect.top(), rect.width(), rect.height()) { +} + void SelectionRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) { QRect rect = this->rect().toRect(); if (rect.height() > 30 && rect.width() > 30) { diff --git a/src/cropeditor/selectionrectangle.hpp b/src/cropeditor/selectionrectangle.hpp index e4e0d9a..e8a27f9 100644 --- a/src/cropeditor/selectionrectangle.hpp +++ b/src/cropeditor/selectionrectangle.hpp @@ -8,6 +8,7 @@ class SelectionRectangle : public QGraphicsRectItem { public: SelectionRectangle(); explicit SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr); + explicit SelectionRectangle(QRectF rect, QGraphicsItem *parent = nullptr); protected: void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) override; diff --git a/src/main.cpp b/src/main.cpp index db39488..1538572 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,5 +108,6 @@ int main(int argc, char *argv[]) { a.connect(&a, &QApplication::aboutToQuit, [] { stillAlive = false; }); if (!parser.isSet(h)) w.show(); + qDebug() << "lol"; return a.exec(); }