From 205cd7c87bfb984cc98f45e50dd8289ea7de5c54 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Tue, 17 Apr 2018 00:06:57 +0200 Subject: [PATCH] Add basic code for the Pin tool --- flameshot.pro | 8 ++- graphics.qrc | 2 + img/buttonIconsBlack/pin.png | Bin 0 -> 218 bytes img/buttonIconsBlack/pin.svg | 5 ++ img/buttonIconsWhite/pin.png | Bin 0 -> 230 bytes img/buttonIconsWhite/pin.svg | 48 +++++++++++++++++ src/tools/pin/pintool.cpp | 62 +++++++++++++++++++++ src/tools/pin/pintool.h | 44 +++++++++++++++ src/tools/pin/pinwidget.cpp | 75 ++++++++++++++++++++++++++ src/tools/pin/pinwidget.h | 44 +++++++++++++++ src/tools/toolfactory.cpp | 4 ++ src/widgets/capture/capturebutton.cpp | 2 + src/widgets/capture/capturebutton.h | 1 + 13 files changed, 293 insertions(+), 2 deletions(-) create mode 100644 img/buttonIconsBlack/pin.png create mode 100644 img/buttonIconsBlack/pin.svg create mode 100644 img/buttonIconsWhite/pin.png create mode 100644 img/buttonIconsWhite/pin.svg create mode 100755 src/tools/pin/pintool.cpp create mode 100755 src/tools/pin/pintool.h create mode 100644 src/tools/pin/pinwidget.cpp create mode 100644 src/tools/pin/pinwidget.h diff --git a/flameshot.pro b/flameshot.pro index 36c2907f..2fef15d4 100644 --- a/flameshot.pro +++ b/flameshot.pro @@ -113,6 +113,7 @@ SOURCES += src/main.cpp \ src/utils/desktopfileparse.cpp \ src/tools/launcher/launcheritemdelegate.cpp \ src/tools/blur/blurtool.cpp \ + src/tools/pin/pintool.cpp \ src/tools/launcher/terminallauncher.cpp \ src/config/visualseditor.cpp \ src/config/extendedslider.cpp \ @@ -128,7 +129,8 @@ SOURCES += src/main.cpp \ src/utils/globalvalues.cpp \ src/widgets/capture/utilitypanel.cpp \ src/widgets/capture/hovereventfilter.cpp \ - src/widgets/capture/selectionwidget.cpp + src/widgets/capture/selectionwidget.cpp \ + src/tools/pin/pinwidget.cpp HEADERS += src/widgets/capture/buttonhandler.h \ src/widgets/infowindow.h \ @@ -178,6 +180,7 @@ HEADERS += src/widgets/capture/buttonhandler.h \ src/utils/desktopfileparse.h \ src/tools/launcher/launcheritemdelegate.h \ src/tools/blur/blurtool.h \ + src/tools/pin/pintool.h \ src/tools/launcher/terminallauncher.h \ src/config/visualseditor.h \ src/config/extendedslider.h \ @@ -193,7 +196,8 @@ HEADERS += src/widgets/capture/buttonhandler.h \ src/tools/abstractactiontool.h \ src/widgets/capture/utilitypanel.h \ src/widgets/capture/hovereventfilter.h \ - src/widgets/capture/selectionwidget.h + src/widgets/capture/selectionwidget.h \ + src/tools/pin/pinwidget.h unix:!macx { SOURCES += src/core/flameshotdbusadapter.cpp \ diff --git a/graphics.qrc b/graphics.qrc index 095114e8..963c8b1f 100644 --- a/graphics.qrc +++ b/graphics.qrc @@ -45,5 +45,7 @@ img/buttonIconsWhite/blur.png img/buttonIconsBlack/redo-variant.png img/buttonIconsWhite/redo-variant.png + img/buttonIconsBlack/pin.png + img/buttonIconsWhite/pin.png diff --git a/img/buttonIconsBlack/pin.png b/img/buttonIconsBlack/pin.png new file mode 100644 index 0000000000000000000000000000000000000000..7a3eee8827387b38699e9f45d23b51cd35e846dd GIT binary patch literal 218 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`GjEX7WqAsj$Z!;#Vf4nJ z@ErkR#;MwT(m+AU64!{5;QX|b^2DN4hVt@qz0ADq;^f4FRK5J7^x5xhq=1UTJzX3_ zJUWvTBwjU4Z1_{}bv9Ayp6(MyC%c3Mg*V)L(k|R#Uo6%WIp(yqgu=`o ziJr?sH9IHVDB@TjY0}RebWl=5O^T;NOKNMvhoy@SvNJ4yAaI~FW4{H^3I + + + + diff --git a/img/buttonIconsWhite/pin.png b/img/buttonIconsWhite/pin.png new file mode 100644 index 0000000000000000000000000000000000000000..2b6e113bedb5d18360d9d64aaf51ad6c15d460c6 GIT binary patch literal 230 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`GjEX7WqAsj$Z!;#Vf4nJ z@ErkR#;MwT(m+AU64!{5;QX|b^2DN4hVt@qz0ADq;^f4FRK5J7^x5xhq=1T&JzX3_ zJUZV_apXIoz`-(o-<@Rn=ku3szvqy-$dk8P_00yyDgX4kyBLDi($<}yI#*-0L8FPE zps#+eM?eFU4jV&m3d5cGCmfRF^p5O3bhG}2X^~#lPre`~s|&`q?e>?M<}76IaqyfV TwAt!6&^iWBS3j3^P6 +image/svg+xml \ No newline at end of file diff --git a/src/tools/pin/pintool.cpp b/src/tools/pin/pintool.cpp new file mode 100755 index 00000000..de0cdaeb --- /dev/null +++ b/src/tools/pin/pintool.cpp @@ -0,0 +1,62 @@ +// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "pintool.h" +#include "src/tools/pin/pinwidget.h" + +PinTool::PinTool(QObject *parent) : AbstractActionTool(parent) { + +} + +bool PinTool::closeOnButtonPressed() const { + return true; +} + +QIcon PinTool::icon(const QColor &background, bool inEditor) const { + Q_UNUSED(inEditor); + return QIcon(iconPath(background) + "pin.png"); +} +QString PinTool::name() const { + return tr("Pin Tool"); +} + +QString PinTool::nameID() { + return ""; +} + +QString PinTool::description() const { + return tr("Pin image on the desktop"); +} + +QWidget* PinTool::widget() { + PinWidget *w = new PinWidget(m_pixmap); + w->setGeometry(m_geometry); + return w; +} + +CaptureTool* PinTool::copy(QObject *parent) { + return new PinTool(parent); +} + +void PinTool::pressed(const CaptureContext &context) { + emit requestAction(REQ_CAPTURE_DONE_OK); + m_geometry = context.selection; + m_geometry.setTopLeft(m_geometry.topLeft() + context.widgetOffset); + m_pixmap = context.selectedScreenshotArea(); + emit requestAction(REQ_ADD_EXTERNAL_WIDGETS); + +} diff --git a/src/tools/pin/pintool.h b/src/tools/pin/pintool.h new file mode 100755 index 00000000..045e4b02 --- /dev/null +++ b/src/tools/pin/pintool.h @@ -0,0 +1,44 @@ +// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#pragma once + +#include "src/tools/abstractactiontool.h" + +class PinTool : public AbstractActionTool { + Q_OBJECT +public: + explicit PinTool(QObject *parent = nullptr); + + bool closeOnButtonPressed() const; + + QIcon icon(const QColor &background, bool inEditor) const override; + QString name() const override; + static QString nameID(); + QString description() const override; + + QWidget* widget() override; + + CaptureTool* copy(QObject *parent = nullptr) override; + +public slots: + void pressed(const CaptureContext &context) override; + +private: + QRect m_geometry; + QPixmap m_pixmap; +}; diff --git a/src/tools/pin/pinwidget.cpp b/src/tools/pin/pinwidget.cpp new file mode 100644 index 00000000..d4b63145 --- /dev/null +++ b/src/tools/pin/pinwidget.cpp @@ -0,0 +1,75 @@ +// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "pinwidget.h" +#include +#include +#include +#include +#include + +PinWidget::PinWidget(const QPixmap &pixmap, QWidget *parent) : + QWidget(parent), m_pixmap(pixmap) +{ + setWindowFlags(Qt::WindowStaysOnTopHint + | Qt::FramelessWindowHint); + m_layout = new QVBoxLayout(this); + + m_label = new QLabel(); + m_label->setPixmap(m_pixmap); + m_layout->addWidget(m_label); + + new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close())); + new QShortcut(Qt::Key_Escape, this, SLOT(close())); +} + +void PinWidget::wheelEvent(QWheelEvent *e) { + int val = e->delta() > 0 ? 5 : -5; + int newWidth = qBound(50, m_label->width() + val, maximumWidth()); + int newHeight = qBound(50, m_label->height() + val, maximumHeight()); + + QSize size(newWidth, newHeight); + setScaledPixmap(size); + adjustSize(); + + e->accept(); +} + +void PinWidget::mouseDoubleClickEvent(QMouseEvent *) { + close(); +} + +void PinWidget::mousePressEvent(QMouseEvent *e) { + m_dragStart = e->globalPos(); + m_offsetX = e->localPos().x() / width(); + m_offsetY = e->localPos().y() / height(); +} + +void PinWidget::mouseMoveEvent(QMouseEvent *e) { + const QPoint delta = e->globalPos() - m_dragStart; + int offsetW = width() * m_offsetX; + int offsetH = height() * m_offsetY; + move(m_dragStart.x() + delta.x() - offsetW, m_dragStart.y() + delta.y() - offsetH); +} + +void PinWidget::setScaledPixmap(const QSize &size) { + const qreal scale = qApp->devicePixelRatio(); + QPixmap scaledPixmap = m_pixmap.scaled(size * scale, Qt::KeepAspectRatio, + Qt::SmoothTransformation); + scaledPixmap.setDevicePixelRatio(scale); + m_label->setPixmap(scaledPixmap); +} diff --git a/src/tools/pin/pinwidget.h b/src/tools/pin/pinwidget.h new file mode 100644 index 00000000..75b8c1a5 --- /dev/null +++ b/src/tools/pin/pinwidget.h @@ -0,0 +1,44 @@ +// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#pragma once + +#include + +class QVBoxLayout; +class QLabel; + +class PinWidget : public QWidget { + Q_OBJECT +public: + explicit PinWidget(const QPixmap &pixmap, QWidget *parent = nullptr); + +protected: + void wheelEvent(QWheelEvent *e); + void mouseDoubleClickEvent(QMouseEvent *); + void mousePressEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + +private: + void setScaledPixmap(const QSize &size); + + QPixmap m_pixmap; + QVBoxLayout *m_layout; + QLabel *m_label; + QPoint m_dragStart; + qreal m_offsetX, m_offsetY; +}; diff --git a/src/tools/toolfactory.cpp b/src/tools/toolfactory.cpp index 468b7734..c7ed19ec 100644 --- a/src/tools/toolfactory.cpp +++ b/src/tools/toolfactory.cpp @@ -33,6 +33,7 @@ #include "launcher/applaunchertool.h" #include "blur/blurtool.h" #include "redo/redotool.h" +#include "pin/pintool.h" ToolFactory::ToolFactory(QObject *parent) : QObject(parent) { @@ -95,6 +96,9 @@ CaptureTool* ToolFactory::CreateTool( case CaptureButton::TYPE_BLUR: tool = new BlurTool(parent); break; + case CaptureButton::TYPE_PIN: + tool = new PinTool(parent); + break; default: tool = nullptr; break; diff --git a/src/widgets/capture/capturebutton.cpp b/src/widgets/capture/capturebutton.cpp index c426ee6c..d4240497 100644 --- a/src/widgets/capture/capturebutton.cpp +++ b/src/widgets/capture/capturebutton.cpp @@ -166,6 +166,7 @@ static std::map buttonTypeOrder { { CaptureButton::TYPE_EXIT, 14 }, { CaptureButton::TYPE_IMAGEUPLOADER, 15 }, { CaptureButton::TYPE_OPEN_APP, 16 }, + { CaptureButton::TYPE_PIN, 17 }, { CaptureButton::TYPE_BLUR, 7 }, { CaptureButton::TYPE_REDO, 11 }, }; @@ -193,4 +194,5 @@ QVector CaptureButton::iterableButtonTypes = { CaptureButton::TYPE_EXIT, CaptureButton::TYPE_IMAGEUPLOADER, CaptureButton::TYPE_OPEN_APP, + CaptureButton::TYPE_PIN, }; diff --git a/src/widgets/capture/capturebutton.h b/src/widgets/capture/capturebutton.h index ec68f218..7ed11ddd 100644 --- a/src/widgets/capture/capturebutton.h +++ b/src/widgets/capture/capturebutton.h @@ -50,6 +50,7 @@ public: TYPE_OPEN_APP = 14, TYPE_BLUR = 15, TYPE_REDO = 16, + TYPE_PIN = 17, }; CaptureButton() = delete;