diff --git a/src/cropeditor/cropscene.cpp b/src/cropeditor/cropscene.cpp index d33e5cb..8dd7f99 100644 --- a/src/cropeditor/cropscene.cpp +++ b/src/cropeditor/cropscene.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap) addDrawingAction(menu, tr("Straight line"), ":/icons/line.png", [] { return new LineItem; }); addDrawingAction(menu, tr("Text"), ":/icons/text.png", [] { return new TextItem; }); addDrawingAction(menu, tr("Rectangle"), ":/icons/rectangle.png", [] { return new RectItem; }); + addDrawingAction(menu, tr("Highlighter"), ":/icons/highlighter.png", [] { return new Highlighter; }); addDrawingAction(menu, tr("Ellipse"), ":/icons/circle.png", [] { return new EllipseItem; }); addDrawingAction(menu, tr("Arrow"), ":/icons/arrow.png", [] { return new ArrowItem; }); diff --git a/src/cropeditor/drawing/highlighter.cpp b/src/cropeditor/drawing/highlighter.cpp new file mode 100644 index 0000000..31e64f9 --- /dev/null +++ b/src/cropeditor/drawing/highlighter.cpp @@ -0,0 +1,37 @@ +#include "highlighter.hpp" + +#include + +void Highlighter::mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) { + if (pos.isNull()) { + pos = scene->cursorPos(); + rect = scene->addRect(QRect(scene->cursorPos().toPoint(), QSize(1, 1)), QPen(scene->highlight()), Qt::NoBrush); + pixmap = scene->addPixmap(scene->pixmap().copy(rect->rect().toRect())); + pixmap->setPos(scene->cursorPos()); + } else { + QPointF p = scene->cursorPos(); + rect->setRect(QRect(qMin(pos.x(), p.x()), qMin(pos.y(), p.y()), qAbs(pos.x() - p.x()), qAbs(pos.y() - p.y()))); + auto area = rect->rect(); + if (area.width() > 1 && area.height() > 1 && area.top() > 1 && area.left() > 1) { + // toImage conversion needed for access to the underlying image data for altering + auto pm = scene->pixmap().copy(rect->rect().toRect()).toImage(); + if (pm.format() != QImage::Format_RGB32) { + pm = pm.convertToFormat(QImage::Format_RGB32); + } + for (int y = 0; y < pm.height(); y++) { + QRgb *line = (QRgb *)pm.scanLine(y); + for (int x = 0; x < pm.width(); x++) { + QRgb &pix = line[x]; + pix = qRgb(qMin(255, qRed(pix)), qMin(255, qGreen(pix)), 0); + } + } + pixmap->setPixmap(QPixmap::fromImage(pm)); + } + pixmap->setPos(rect->rect().topLeft()); + } +} + +void Highlighter::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) { + delete rect; + rect = 0; +} diff --git a/src/cropeditor/drawing/highlighter.hpp b/src/cropeditor/drawing/highlighter.hpp new file mode 100644 index 0000000..b8d4c61 --- /dev/null +++ b/src/cropeditor/drawing/highlighter.hpp @@ -0,0 +1,21 @@ +#ifndef HIGHLIGHTER_HPP +#define HIGHLIGHTER_HPP + +#include "drawitem.hpp" +#include + +class Highlighter : public DrawItem { +public: + QString name() override { + return "Highlighter"; + } + void mouseDragEvent(QGraphicsSceneMouseEvent *, CropScene *scene) override; + void mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) override; + +private: + QPointF pos; + QGraphicsRectItem *rect = 0; + QGraphicsPixmapItem *pixmap; +}; + +#endif /* HIGHLIGHTER_HPP */ diff --git a/src/icon.qrc b/src/icon.qrc index 3c1d69c..23c098b 100644 --- a/src/icon.qrc +++ b/src/icon.qrc @@ -17,5 +17,6 @@ icons/cancel.png icons/crop.png icons/circle.png + icons/highlighter.png diff --git a/src/icons/highlighter.png b/src/icons/highlighter.png new file mode 100644 index 0000000..e70d900 Binary files /dev/null and b/src/icons/highlighter.png differ diff --git a/src/icons/highlighter.svg b/src/icons/highlighter.svg new file mode 100644 index 0000000..1aded2b --- /dev/null +++ b/src/icons/highlighter.svg @@ -0,0 +1,58 @@ + + + + + + image/svg+xml + + + + + + + + diff --git a/src/src.pro b/src/src.pro index b5d22d1..83f37be 100644 --- a/src/src.pro +++ b/src/src.pro @@ -61,6 +61,7 @@ SOURCES += main.cpp\ cropeditor/drawing/eraseritem.cpp \ cropeditor/drawing/rectitem.cpp \ cropeditor/drawing/ellipseitem.cpp \ + cropeditor/drawing/highlighter.cpp \ hotkeyinputdialog.cpp \ cropeditor/drawing/arrowitem.cpp \ uploaders/default/imgursettingsdialog.cpp \ @@ -113,6 +114,7 @@ HEADERS += mainwindow.hpp \ cropeditor/drawing/eraseritem.hpp \ cropeditor/drawing/rectitem.hpp \ cropeditor/drawing/ellipseitem.hpp \ + cropeditor/drawing/highlighter.hpp \ hotkeyinputdialog.hpp \ cropeditor/drawing/arrowitem.hpp \ uploaders/default/imgursettingsdialog.hpp \