From df20c7ec8f36c4f98c97ac4f30d0f07e9785347c Mon Sep 17 00:00:00 2001 From: crackcat <63863112+Cr4ckC4t@users.noreply.github.com> Date: Wed, 1 Sep 2021 13:45:20 +0200 Subject: [PATCH] Add a tool to take an inverted screenshot (#1849) * Add invert tool to build commands * Add icons for invert tool * Integrate invert tool * Add shortcut for invert tool * Add invert tool * Add translation for invert tool * Run clang-format * Update tool to invert a region instead of everything * Update shortcut for invert tool * Update button order of inverter and counter * Update translations for invert tool * Run clang-format * Revert "Update translations for invert tool" This reverts commit c1fd5a162be6c64b26790d53eab3749b3a124bcf. * Revert "Add translation for invert tool" This reverts commit b0c1da5f36e844fa8b5f1492f5f507d85bb7aa7b. * Update drawSearchArea to match pixelate * Update invert icon to one from Material Design Co-authored-by: crackcat <> --- data/graphics.qrc | 4 +- data/img/material/black/invert.svg | 5 ++ data/img/material/white/invert.svg | 5 ++ src/CMakeLists.txt | 1 + src/tools/CMakeLists.txt | 1 + src/tools/capturetool.h | 3 +- src/tools/invert/inverttool.cpp | 88 +++++++++++++++++++++++ src/tools/invert/inverttool.h | 30 ++++++++ src/tools/toolfactory.cpp | 4 ++ src/utils/configshortcuts.cpp | 2 + src/widgets/capture/capturetoolbutton.cpp | 30 ++++---- src/widgets/capture/capturetoolbutton.h | 21 +++--- 12 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 data/img/material/black/invert.svg create mode 100644 data/img/material/white/invert.svg create mode 100644 src/tools/invert/inverttool.cpp create mode 100644 src/tools/invert/inverttool.h diff --git a/data/graphics.qrc b/data/graphics.qrc index ac8046a8d1..10d7007c65 100644 --- a/data/graphics.qrc +++ b/data/graphics.qrc @@ -78,5 +78,7 @@ img/material/white/plus.svg img/material/black/minus.svg img/material/white/minus.svg + img/material/black/invert.svg + img/material/white/invert.svg - \ No newline at end of file + diff --git a/data/img/material/black/invert.svg b/data/img/material/black/invert.svg new file mode 100644 index 0000000000..21e880bff0 --- /dev/null +++ b/data/img/material/black/invert.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/data/img/material/white/invert.svg b/data/img/material/white/invert.svg new file mode 100644 index 0000000000..c4b6644f04 --- /dev/null +++ b/data/img/material/white/invert.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 78d4afeef7..34c86668a8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,6 +148,7 @@ target_include_directories( $ $ $ + $ $ $ $ diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 827630145b..b99671f096 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -37,6 +37,7 @@ target_sources( target_sources(flameshot PRIVATE rectangle/rectangletool.h rectangle/rectangletool.cpp) target_sources(flameshot PRIVATE redo/redotool.h redo/redotool.cpp) target_sources(flameshot PRIVATE save/savetool.h save/savetool.cpp) +target_sources(flameshot PRIVATE invert/inverttool.h invert/inverttool.cpp) target_sources(flameshot PRIVATE selection/selectiontool.h selection/selectiontool.cpp) target_sources(flameshot PRIVATE sizeindicator/sizeindicatortool.h sizeindicator/sizeindicatortool.cpp) target_sources( diff --git a/src/tools/capturetool.h b/src/tools/capturetool.h index 7cde00d77f..bc4b0607c5 100644 --- a/src/tools/capturetool.h +++ b/src/tools/capturetool.h @@ -27,13 +27,14 @@ enum class ToolType RECTANGLE, REDO, SAVE, + INVERT, SELECTION, SIZEINDICATOR, TEXT, UNDO, UPLOAD, SIZEINCREASE, - SIZEDECREASE + SIZEDECREASE, }; class CaptureTool : public QObject diff --git a/src/tools/invert/inverttool.cpp b/src/tools/invert/inverttool.cpp new file mode 100644 index 0000000000..144a2ac002 --- /dev/null +++ b/src/tools/invert/inverttool.cpp @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors + +#include "inverttool.h" +#include +#include +#include +#include +#include +#include +#include + +InvertTool::InvertTool(QObject* parent) + : AbstractTwoPointTool(parent) +{} + +QIcon InvertTool::icon(const QColor& background, bool inEditor) const +{ + Q_UNUSED(inEditor) + return QIcon(iconPath(background) + "invert.svg"); +} + +QString InvertTool::name() const +{ + return tr("Invert"); +} + +ToolType InvertTool::type() const +{ + return ToolType::INVERT; +} + +QString InvertTool::description() const +{ + return tr("Set Inverter as the paint tool"); +} + +CaptureTool* InvertTool::copy(QObject* parent) +{ + auto* tool = new InvertTool(parent); + copyParams(this, tool); + return tool; +} + +void InvertTool::process(QPainter& painter, const QPixmap& pixmap) +{ + QPoint p0 = points().first; + QPoint p1 = points().second; + QRect selection = QRect(p0, p1).normalized(); + + // Invert selection + QPixmap inv = pixmap.copy(selection); + QImage img = inv.toImage(); + img.invertPixels(); + + painter.drawImage(selection, img); +} + +void InvertTool::drawSearchArea(QPainter& painter, const QPixmap& pixmap) +{ + Q_UNUSED(pixmap) + painter.fillRect(std::min(points().first.x(), points().second.x()), + std::min(points().first.y(), points().second.y()), + std::abs(points().first.x() - points().second.x()), + std::abs(points().first.y() - points().second.y()), + QBrush(Qt::black)); +} + +void InvertTool::paintMousePreview(QPainter& painter, + const CaptureContext& context) +{ + Q_UNUSED(context) + Q_UNUSED(painter) +} + +void InvertTool::pressed(const CaptureContext& context) +{ + Q_UNUSED(context) +} + +void InvertTool::drawObjectSelection(QPainter& painter) +{ + QRect rect = QRect(std::min(points().first.x(), points().second.x()), + std::min(points().first.y(), points().second.y()), + std::abs(points().first.x() - points().second.x()), + std::abs(points().first.y() - points().second.y())); + drawObjectSelectionRect(painter, rect); +} diff --git a/src/tools/invert/inverttool.h b/src/tools/invert/inverttool.h new file mode 100644 index 0000000000..a39dbd4013 --- /dev/null +++ b/src/tools/invert/inverttool.h @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors + +#pragma once + +#include "src/tools/abstracttwopointtool.h" + +class InvertTool : public AbstractTwoPointTool +{ + Q_OBJECT +public: + explicit InvertTool(QObject* parent = nullptr); + + QIcon icon(const QColor& background, bool inEditor) const override; + QString name() const override; + QString description() const override; + + CaptureTool* copy(QObject* parent = nullptr) override; + void process(QPainter& painter, const QPixmap& pixmap) override; + void drawSearchArea(QPainter& painter, const QPixmap& pixmap) override; + void paintMousePreview(QPainter& painter, + const CaptureContext& context) override; + void drawObjectSelection(QPainter& painter) override; + +protected: + ToolType type() const override; + +public slots: + void pressed(const CaptureContext& context) override; +}; diff --git a/src/tools/toolfactory.cpp b/src/tools/toolfactory.cpp index 2dc146a2ae..f57310bd3b 100644 --- a/src/tools/toolfactory.cpp +++ b/src/tools/toolfactory.cpp @@ -8,6 +8,7 @@ #include "copy/copytool.h" #include "exit/exittool.h" #include "imgur/imguruploadertool.h" +#include "invert/inverttool.h" #include "launcher/applaunchertool.h" #include "line/linetool.h" #include "marker/markertool.h" @@ -68,6 +69,9 @@ CaptureTool* ToolFactory::CreateTool(CaptureToolButton::ButtonType t, case CaptureToolButton::TYPE_SAVE: tool = new SaveTool(parent); break; + case CaptureToolButton::TYPE_INVERT: + tool = new InvertTool(parent); + break; case CaptureToolButton::TYPE_SELECTION: tool = new SelectionTool(parent); break; diff --git a/src/utils/configshortcuts.cpp b/src/utils/configshortcuts.cpp index 5463f482df..b49cdb1486 100644 --- a/src/utils/configshortcuts.cpp +++ b/src/utils/configshortcuts.cpp @@ -104,6 +104,8 @@ const QKeySequence& ConfigShortcuts::captureShortcutDefault( m_ks = QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z); } else if (buttonType == "TYPE_TEXT") { m_ks = QKeySequence(Qt::Key_T); + } else if (buttonType == "TYPE_INVERT") { + m_ks = QKeySequence(Qt::Key_I); } else if (buttonType == "TYPE_TOGGLE_PANEL") { m_ks = QKeySequence(Qt::Key_Space); } else if (buttonType == "TYPE_RESIZE_LEFT") { diff --git a/src/widgets/capture/capturetoolbutton.cpp b/src/widgets/capture/capturetoolbutton.cpp index 8f0782c0ca..3048f1d571 100644 --- a/src/widgets/capture/capturetoolbutton.cpp +++ b/src/widgets/capture/capturetoolbutton.cpp @@ -136,23 +136,24 @@ static std::map buttonTypeOrder { CaptureToolButton::TYPE_MARKER, 6 }, { CaptureToolButton::TYPE_TEXT, 7 }, { CaptureToolButton::TYPE_PIXELATE, 8 }, - { CaptureToolButton::TYPE_CIRCLECOUNT, 9 }, - { CaptureToolButton::TYPE_SELECTIONINDICATOR, 10 }, - { CaptureToolButton::TYPE_MOVESELECTION, 11 }, - { CaptureToolButton::TYPE_UNDO, 12 }, - { CaptureToolButton::TYPE_REDO, 13 }, - { CaptureToolButton::TYPE_COPY, 14 }, - { CaptureToolButton::TYPE_SAVE, 15 }, - { CaptureToolButton::TYPE_IMAGEUPLOADER, 16 }, + { CaptureToolButton::TYPE_INVERT, 9 }, + { CaptureToolButton::TYPE_CIRCLECOUNT, 10 }, + { CaptureToolButton::TYPE_SELECTIONINDICATOR, 11 }, + { CaptureToolButton::TYPE_MOVESELECTION, 12 }, + { CaptureToolButton::TYPE_UNDO, 13 }, + { CaptureToolButton::TYPE_REDO, 14 }, + { CaptureToolButton::TYPE_COPY, 15 }, + { CaptureToolButton::TYPE_SAVE, 16 }, + { CaptureToolButton::TYPE_IMAGEUPLOADER, 17 }, #if !defined(Q_OS_MACOS) - { CaptureToolButton::TYPE_OPEN_APP, 17 }, - { CaptureToolButton::TYPE_EXIT, 18 }, { CaptureToolButton::TYPE_PIN, 19 }, + { CaptureToolButton::TYPE_OPEN_APP, 18 }, + { CaptureToolButton::TYPE_EXIT, 19 }, { CaptureToolButton::TYPE_PIN, 20 }, #else - { CaptureToolButton::TYPE_EXIT, 17 }, { CaptureToolButton::TYPE_PIN, 18 }, + { CaptureToolButton::TYPE_EXIT, 18 }, { CaptureToolButton::TYPE_PIN, 19 }, #endif - { CaptureToolButton::TYPE_SIZEINCREASE, 20 }, - { CaptureToolButton::TYPE_SIZEDECREASE, 21 }, + { CaptureToolButton::TYPE_SIZEINCREASE, 21 }, + { CaptureToolButton::TYPE_SIZEDECREASE, 22 }, }; int CaptureToolButton::getPriorityByButton(CaptureToolButton::ButtonType b) @@ -173,6 +174,8 @@ QVector CaptureToolButton::TYPE_MARKER, CaptureToolButton::TYPE_TEXT, CaptureToolButton::TYPE_PIXELATE, + CaptureToolButton::TYPE_INVERT, + CaptureToolButton::TYPE_CIRCLECOUNT, CaptureToolButton::TYPE_SELECTIONINDICATOR, CaptureToolButton::TYPE_MOVESELECTION, CaptureToolButton::TYPE_UNDO, @@ -185,7 +188,6 @@ QVector CaptureToolButton::TYPE_OPEN_APP, #endif CaptureToolButton::TYPE_PIN, - CaptureToolButton::TYPE_CIRCLECOUNT, CaptureToolButton::TYPE_SIZEINCREASE, CaptureToolButton::TYPE_SIZEDECREASE, }; diff --git a/src/widgets/capture/capturetoolbutton.h b/src/widgets/capture/capturetoolbutton.h index 7ae676bead..121fce8dde 100644 --- a/src/widgets/capture/capturetoolbutton.h +++ b/src/widgets/capture/capturetoolbutton.h @@ -32,16 +32,17 @@ class CaptureToolButton : public CaptureButton TYPE_UNDO = 9, TYPE_COPY = 10, TYPE_SAVE = 11, - TYPE_EXIT = 12, - TYPE_IMAGEUPLOADER = 13, - TYPE_OPEN_APP = 14, - TYPE_PIXELATE = 15, - TYPE_REDO = 16, - TYPE_PIN = 17, - TYPE_TEXT = 18, - TYPE_CIRCLECOUNT = 19, - TYPE_SIZEINCREASE = 20, - TYPE_SIZEDECREASE = 21, + TYPE_INVERT = 12, + TYPE_EXIT = 13, + TYPE_IMAGEUPLOADER = 14, + TYPE_OPEN_APP = 15, + TYPE_PIXELATE = 16, + TYPE_REDO = 17, + TYPE_PIN = 18, + TYPE_TEXT = 19, + TYPE_CIRCLECOUNT = 20, + TYPE_SIZEINCREASE = 21, + TYPE_SIZEDECREASE = 22, }; Q_ENUM(ButtonType)