Browse Source

Add hotspots to the selection rectangle

tags/v5.0.0^2
ArsenArsen 5 years ago
parent
commit
6120138523
  1. 46
      src/cropeditor/cropscene.cpp
  2. 4
      src/cropeditor/cropscene.hpp
  3. 1
      src/cropeditor/drawing/bluritem.hpp
  4. 39
      src/cropeditor/selectionrectangle.cpp
  5. 16
      src/cropeditor/selectionrectangle.hpp
  6. 6
      src/src.pro

46
src/cropeditor/cropscene.cpp

@ -1,4 +1,5 @@
#include "cropscene.hpp"
#include "selectionrectangle.hpp"
#include <QApplication>
#include <QColorDialog>
#include <QDebug>
@ -193,7 +194,7 @@ void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> 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().<axis>() - rect->rect().<edge>()) < 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());
}

4
src/cropeditor/cropscene.hpp

@ -2,13 +2,13 @@
#define CROPSCENE_HPP
#include <QFont>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMenu>
#include <QMenuBar>
#include <cropeditor/selectionrectangle.hpp>
#include <functional>
#include <screenoverlayview.hpp>
#include <screenshotutil.hpp>
@ -84,7 +84,7 @@ private:
std::function<DrawItem *()> drawingSelectionMaker;
QFlags<Qt::MouseButton> prevButtons;
QPixmap _pixmap;
QGraphicsRectItem *rect = nullptr;
SelectionRectangle *rect = nullptr;
QGraphicsPixmapItem *magnifier = nullptr;
QGraphicsRectItem *magnifierBox = nullptr;
QGraphicsTextItem *magnifierHint = nullptr;

1
src/cropeditor/drawing/bluritem.hpp

@ -11,6 +11,7 @@ public:
return "Blur";
}
~BlurItem() {
return;
}
bool init(CropScene *) override;

39
src/cropeditor/selectionrectangle.cpp

@ -0,0 +1,39 @@
#include "selectionrectangle.hpp"
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
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 <angle> * 16 things are needed because here angles are 1/16th of their value
// The <x/y> - 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);
}

16
src/cropeditor/selectionrectangle.hpp

@ -0,0 +1,16 @@
#ifndef SELECTIONRECTANGLE_HPP
#define SELECTIONRECTANGLE_HPP
#include <QGraphicsRectItem>
#include <QPainter>
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

6
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 {

Loading…
Cancel
Save