From 61201385233af6b7f2843d1f50e1fd1de48feef0 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Wed, 6 Sep 2017 23:50:54 +0200 Subject: [PATCH] Add hotspots to the selection rectangle --- src/cropeditor/cropscene.cpp | 46 ++++++++++++++++++++++++--- src/cropeditor/cropscene.hpp | 4 +-- src/cropeditor/drawing/bluritem.hpp | 1 + src/cropeditor/selectionrectangle.cpp | 39 +++++++++++++++++++++++ src/cropeditor/selectionrectangle.hpp | 16 ++++++++++ src/src.pro | 6 ++-- 6 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 src/cropeditor/selectionrectangle.cpp create mode 100644 src/cropeditor/selectionrectangle.hpp diff --git a/src/cropeditor/cropscene.cpp b/src/cropeditor/cropscene.cpp index f24aa40..155d9a9 100644 --- a/src/cropeditor/cropscene.cpp +++ b/src/cropeditor/cropscene.cpp @@ -1,4 +1,5 @@ #include "cropscene.hpp" +#include "selectionrectangle.hpp" #include #include #include @@ -193,7 +194,7 @@ void CropScene::setDrawingSelection(QString name, std::function dr drawingName = name; display->setText(drawingName); if (drawingSelection) - if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; }); + if (!drawingSelection->init(this)) setDrawingSelection(tr("None"), [] { return nullptr; }); } QGraphicsItem *CropScene::whichItem(QPointF scenePos) { @@ -245,6 +246,40 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { cursorItem->setPos(cursorPos); updateMag(); + if (rect) { + // qAbs(e->scenePos().() - rect->rect().()) < 10 + bool close = false; + QRectF newRect = rect->rect(); + if (qAbs(e->scenePos().x() - rect->rect().right()) < 10) { + 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.setBottomLeft(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); + } + } 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); + } 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 (!close) + views()[0]->setCursor(Qt::BlankCursor); + else { + rect->setRect(newRect); + prevButtons = e->buttons(); + return; + } + } + auto buttons = e->buttons(); if (e->modifiers() & Qt::ControlModifier && buttons == Qt::LeftButton) { auto item = whichItem(cursorPos); @@ -257,7 +292,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { } else { QPointF p = cursorPos; if (rect == nullptr) { - rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1); + rect = new SelectionRectangle(p.x(), p.y(), 1, 1); initPos = p; QPen pen(Qt::NoBrush, 1); pen.setColor(_highlight); @@ -303,7 +338,7 @@ void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { delete drawingSelection; drawingSelection = drawingSelectionMaker(); if (drawingSelection) - if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; }); + if (!drawingSelection->init(this)) setDrawingSelection(tr("None"), [] { return nullptr; }); } else if (settings::settings().value("quickMode", false).toBool()) done(true); prevButtons = Qt::NoButton; @@ -414,15 +449,16 @@ void CropScene::initMagnifierGrid() { void CropScene::done(bool notEsc) { if (notEsc && rect) { + QRectF rect2 = rect->rect(); hint->setVisible(false); - rect->setPen(QPen(Qt::NoPen)); + rect->setRect(QRect(-100, -100, 0, 0)); magnifier->setVisible(false); proxyMenu->setVisible(false); cursorItem->setVisible(false); magnifierBox->setVisible(false); magnifierHint->setVisible(false); magnifierHintBox->setVisible(false); - emit closedWithRect(rect->rect().toRect()); + emit closedWithRect(rect2.toRect()); } else emit closedWithRect(QRect()); } diff --git a/src/cropeditor/cropscene.hpp b/src/cropeditor/cropscene.hpp index 2630443..c9d0a1b 100644 --- a/src/cropeditor/cropscene.hpp +++ b/src/cropeditor/cropscene.hpp @@ -2,13 +2,13 @@ #define CROPSCENE_HPP #include -#include #include #include #include #include #include #include +#include #include #include #include @@ -84,7 +84,7 @@ private: std::function drawingSelectionMaker; QFlags prevButtons; QPixmap _pixmap; - QGraphicsRectItem *rect = nullptr; + SelectionRectangle *rect = nullptr; QGraphicsPixmapItem *magnifier = nullptr; QGraphicsRectItem *magnifierBox = nullptr; QGraphicsTextItem *magnifierHint = nullptr; diff --git a/src/cropeditor/drawing/bluritem.hpp b/src/cropeditor/drawing/bluritem.hpp index 2882a5a..4e9bbef 100644 --- a/src/cropeditor/drawing/bluritem.hpp +++ b/src/cropeditor/drawing/bluritem.hpp @@ -11,6 +11,7 @@ public: return "Blur"; } ~BlurItem() { + return; } bool init(CropScene *) override; diff --git a/src/cropeditor/selectionrectangle.cpp b/src/cropeditor/selectionrectangle.cpp new file mode 100644 index 0000000..b7c3245 --- /dev/null +++ b/src/cropeditor/selectionrectangle.cpp @@ -0,0 +1,39 @@ +#include "selectionrectangle.hpp" + +#include +#include +#include +#include + +SelectionRectangle::SelectionRectangle() { +} + +SelectionRectangle::SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent) +: QGraphicsRectItem(x, y, w, h, parent) { +} + +void SelectionRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) { + QRect rect = this->rect().toRect(); + if (rect.height() > 30 && rect.width() > 30) { + painter->setPen(Qt::NoPen); + painter->setBrush(pen().color()); + + // Before you panick, the * 16 things are needed because here angles are 1/16th of their value + // The - 10 is accounting for sizes. + // Trail and error ftw + + // Bottom left + painter->drawPie(rect.left() - 10, rect.bottom() - 10, 20, 20, 0, 90 * 16); + // Top right + painter->drawPie(rect.right() - 10, rect.bottom() - 10, 20, 20, 90 * 16, 90 * 16); + + + // Top left + painter->drawPie(rect.left() - 10, rect.top() - 10, 20, 20, 270 * 16, 90 * 16); + // Top right + painter->drawPie(rect.right() - 10, rect.top() - 10, 20, 20, 180 * 16, 90 * 16); + } + painter->setBrush(brush()); + painter->setPen(pen()); + QGraphicsRectItem::paint(painter, options, widget); +} diff --git a/src/cropeditor/selectionrectangle.hpp b/src/cropeditor/selectionrectangle.hpp new file mode 100644 index 0000000..e4e0d9a --- /dev/null +++ b/src/cropeditor/selectionrectangle.hpp @@ -0,0 +1,16 @@ +#ifndef SELECTIONRECTANGLE_HPP +#define SELECTIONRECTANGLE_HPP + +#include +#include + +class SelectionRectangle : public QGraphicsRectItem { +public: + SelectionRectangle(); + explicit SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr); + +protected: + void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) override; +}; + +#endif // SELECTIONRECTANGLE_HPP diff --git a/src/src.pro b/src/src.pro index ca91179..236faf1 100644 --- a/src/src.pro +++ b/src/src.pro @@ -67,7 +67,8 @@ SOURCES += main.cpp\ logs/requestlogging.cpp \ logs/historydialog.cpp \ monospacetextdialog.cpp \ - screenoverlayview.cpp + screenoverlayview.cpp \ + cropeditor/selectionrectangle.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -116,7 +117,8 @@ HEADERS += mainwindow.hpp \ logs/historydialog.hpp \ screenoverlayview.hpp \ screenoverlayview.hpp \ - monospacetextdialog.hpp + monospacetextdialog.hpp \ + cropeditor/selectionrectangle.hpp nopkg { # win32 {