Add basic code for the Pin tool

This commit is contained in:
lupoDharkael
2018-04-17 00:06:57 +02:00
parent 344d327528
commit 205cd7c87b
13 changed files with 293 additions and 2 deletions

62
src/tools/pin/pintool.cpp Executable file
View File

@@ -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 <http://www.gnu.org/licenses/>.
#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);
}

44
src/tools/pin/pintool.h Executable file
View File

@@ -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 <http://www.gnu.org/licenses/>.
#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;
};

View File

@@ -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 <http://www.gnu.org/licenses/>.
#include "pinwidget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QWheelEvent>
#include <QApplication>
#include <QShortcut>
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);
}

44
src/tools/pin/pinwidget.h Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
#pragma once
#include <QWidget>
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;
};

View File

@@ -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;