Implement highlighter tool

Fixes #56
This commit is contained in:
ArsenArsen 2018-02-17 23:12:49 +01:00
parent 85d57fcc70
commit c15ba008ff
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
7 changed files with 121 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#include <cropeditor/drawing/bluritem.hpp> #include <cropeditor/drawing/bluritem.hpp>
#include <cropeditor/drawing/ellipseitem.hpp> #include <cropeditor/drawing/ellipseitem.hpp>
#include <cropeditor/drawing/eraseritem.hpp> #include <cropeditor/drawing/eraseritem.hpp>
#include <cropeditor/drawing/highlighter.hpp>
#include <cropeditor/drawing/lineitem.hpp> #include <cropeditor/drawing/lineitem.hpp>
#include <cropeditor/drawing/pathitem.hpp> #include <cropeditor/drawing/pathitem.hpp>
#include <cropeditor/drawing/rectitem.hpp> #include <cropeditor/drawing/rectitem.hpp>
@ -41,6 +42,7 @@ CropScene::CropScene(QObject *parent, QPixmap pixmap)
addDrawingAction(menu, tr("Straight line"), ":/icons/line.png", [] { return new LineItem; }); addDrawingAction(menu, tr("Straight line"), ":/icons/line.png", [] { return new LineItem; });
addDrawingAction(menu, tr("Text"), ":/icons/text.png", [] { return new TextItem; }); addDrawingAction(menu, tr("Text"), ":/icons/text.png", [] { return new TextItem; });
addDrawingAction(menu, tr("Rectangle"), ":/icons/rectangle.png", [] { return new RectItem; }); 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("Ellipse"), ":/icons/circle.png", [] { return new EllipseItem; });
addDrawingAction(menu, tr("Arrow"), ":/icons/arrow.png", [] { return new ArrowItem; }); addDrawingAction(menu, tr("Arrow"), ":/icons/arrow.png", [] { return new ArrowItem; });

View File

@ -0,0 +1,37 @@
#include "highlighter.hpp"
#include <QImage>
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;
}

View File

@ -0,0 +1,21 @@
#ifndef HIGHLIGHTER_HPP
#define HIGHLIGHTER_HPP
#include "drawitem.hpp"
#include <QPointF>
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 */

View File

@ -17,5 +17,6 @@
<file>icons/cancel.png</file> <file>icons/cancel.png</file>
<file>icons/crop.png</file> <file>icons/crop.png</file>
<file>icons/circle.png</file> <file>icons/circle.png</file>
<file>icons/highlighter.png</file>
</qresource> </qresource>
</RCC> </RCC>

BIN
src/icons/highlighter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 B

58
src/icons/highlighter.svg Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
viewBox="0 0 24 24"
version="1.1"
id="svg92"
sodipodi:docname="highlighter.svg"
inkscape:version="0.92.2 (5c3e80d, 2017-08-06)"
width="24"
height="24"
inkscape:export-filename="/home/arsen/Projects/KShare/src/icons/highlighter.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<metadata
id="metadata98">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs96" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="785"
inkscape:window-height="855"
id="namedview94"
showgrid="false"
inkscape:zoom="16"
inkscape:cx="9.6803279"
inkscape:cy="12.750987"
inkscape:window-x="805"
inkscape:window-y="35"
inkscape:window-maximized="0"
inkscape:current-layer="svg92" />
<path
style="fill:#000000;stroke-width:1.84643126"
d="M 16.610669,0 C 11.083055,5.5425551 5.5348512,11.064519 0,16.599837 0,19.066559 0,21.533279 0,24 2.4667167,24 4.9334335,24 7.4001502,24 12.937932,18.467607 18.467824,12.927324 24,7.3893312 21.536286,4.9268223 19.077388,2.4594998 16.610669,0 Z M 15.53238,3.6856499 c 1.592788,1.5939894 3.185579,3.187979 4.778363,4.7819684 C 18.717959,6.8736289 17.125168,5.2796393 15.53238,3.6856499 Z m 0,0 c 1.592788,1.5939894 3.185579,3.187979 4.778363,4.7819684 C 15.95333,12.84467 11.588321,17.214185 7.2090156,21.569343 5.615026,19.975355 4.0210366,18.381364 2.4270471,16.787375 6.7954916,12.420133 11.163938,8.0528919 15.53238,3.6856499 Z"
id="path90"
inkscape:connector-curvature="0" />
</svg>

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -61,6 +61,7 @@ SOURCES += main.cpp\
cropeditor/drawing/eraseritem.cpp \ cropeditor/drawing/eraseritem.cpp \
cropeditor/drawing/rectitem.cpp \ cropeditor/drawing/rectitem.cpp \
cropeditor/drawing/ellipseitem.cpp \ cropeditor/drawing/ellipseitem.cpp \
cropeditor/drawing/highlighter.cpp \
hotkeyinputdialog.cpp \ hotkeyinputdialog.cpp \
cropeditor/drawing/arrowitem.cpp \ cropeditor/drawing/arrowitem.cpp \
uploaders/default/imgursettingsdialog.cpp \ uploaders/default/imgursettingsdialog.cpp \
@ -113,6 +114,7 @@ HEADERS += mainwindow.hpp \
cropeditor/drawing/eraseritem.hpp \ cropeditor/drawing/eraseritem.hpp \
cropeditor/drawing/rectitem.hpp \ cropeditor/drawing/rectitem.hpp \
cropeditor/drawing/ellipseitem.hpp \ cropeditor/drawing/ellipseitem.hpp \
cropeditor/drawing/highlighter.hpp \
hotkeyinputdialog.hpp \ hotkeyinputdialog.hpp \
cropeditor/drawing/arrowitem.hpp \ cropeditor/drawing/arrowitem.hpp \
uploaders/default/imgursettingsdialog.hpp \ uploaders/default/imgursettingsdialog.hpp \