Skip to content

Commit

Permalink
Adding pin transparency (flameshot-org#2944)
Browse files Browse the repository at this point in the history
* Adding pin transparency

* added hotkeys
  • Loading branch information
borgmanJeremy authored Oct 13, 2022
1 parent 134117b commit f3577a0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/tools/pin/pinwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors

#include <QGraphicsDropShadowEffect>
#include <QGraphicsOpacityEffect>
#include <QPinchGesture>

#include "pinwidget.h"
Expand Down Expand Up @@ -35,10 +35,10 @@ PinWidget::PinWidget(const QPixmap& pixmap,
{
setWindowIcon(QIcon(GlobalValues::iconPath()));
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
setFocusPolicy(Qt::StrongFocus);
// set the bottom widget background transparent
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_DeleteOnClose);

ConfigHandler conf;
m_baseColor = conf.uiColor();
m_hoverColor = conf.contrastUiColor();
Expand All @@ -49,6 +49,7 @@ PinWidget::PinWidget(const QPixmap& pixmap,
m_shadowEffect->setBlurRadius(BLUR_RADIUS);
m_shadowEffect->setOffset(0, 0);
setGraphicsEffect(m_shadowEffect);
setWindowOpacity(m_opacity);

m_label->setPixmap(m_pixmap);
m_layout->addWidget(m_label);
Expand Down Expand Up @@ -165,6 +166,32 @@ void PinWidget::mouseMoveEvent(QMouseEvent* e)
m_dragStart.y() + delta.y() - offsetH);
}

void PinWidget::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_0) {
m_opacity = 1.0;
} else if (event->key() == Qt::Key_9) {
m_opacity = 0.9;
} else if (event->key() == Qt::Key_8) {
m_opacity = 0.8;
} else if (event->key() == Qt::Key_7) {
m_opacity = 0.7;
} else if (event->key() == Qt::Key_6) {
m_opacity = 0.6;
} else if (event->key() == Qt::Key_5) {
m_opacity = 0.5;
} else if (event->key() == Qt::Key_4) {
m_opacity = 0.4;
} else if (event->key() == Qt::Key_3) {
m_opacity = 0.3;
} else if (event->key() == Qt::Key_2) {
m_opacity = 0.2;
} else if (event->key() == Qt::Key_1) {
m_opacity = 0.1;
}

setWindowOpacity(m_opacity);
}
bool PinWidget::gestureEvent(QGestureEvent* event)
{
if (QGesture* pinch = event->gesture(Qt::PinchGesture)) {
Expand All @@ -189,6 +216,25 @@ void PinWidget::rotateRight()
m_pixmap = m_pixmap.transformed(rotateTransform);
}

void PinWidget::increaseOpacity()
{
m_opacity += 0.1;
if (m_opacity > 1.0) {
m_opacity = 1.0;
}
setWindowOpacity(m_opacity);
}

void PinWidget::decreaseOpacity()
{
m_opacity -= 0.1;
if (m_opacity < 0.0) {
m_opacity = 0.0;
}

setWindowOpacity(m_opacity);
}

bool PinWidget::event(QEvent* event)
{
if (event->type() == QEvent::Gesture) {
Expand Down Expand Up @@ -268,6 +314,20 @@ void PinWidget::showContextMenu(const QPoint& pos)
&rotateLeftAction, &QAction::triggered, this, &PinWidget::rotateLeft);
contextMenu.addAction(&rotateLeftAction);

QAction increaseOpacityAction(tr("Increase Opacity"), this);
connect(&increaseOpacityAction,
&QAction::triggered,
this,
&PinWidget::increaseOpacity);
contextMenu.addAction(&increaseOpacityAction);

QAction decreaseOpacityAction(tr("Decrease Opacity"), this);
connect(&decreaseOpacityAction,
&QAction::triggered,
this,
&PinWidget::decreaseOpacity);
contextMenu.addAction(&decreaseOpacityAction);

QAction closePinAction(tr("Close"), this);
connect(&closePinAction, &QAction::triggered, this, &PinWidget::closePin);
contextMenu.addSeparator();
Expand Down
5 changes: 5 additions & 0 deletions src/tools/pin/pinwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PinWidget : public QWidget
void mouseDoubleClickEvent(QMouseEvent*) override;
void mousePressEvent(QMouseEvent*) override;
void mouseMoveEvent(QMouseEvent*) override;
void keyPressEvent(QKeyEvent*) override;
void enterEvent(QEvent*) override;
void leaveEvent(QEvent*) override;

Expand All @@ -38,6 +39,9 @@ class PinWidget : public QWidget
void rotateLeft();
void rotateRight();

void increaseOpacity();
void decreaseOpacity();

QPixmap m_pixmap;
QVBoxLayout* m_layout;
QLabel* m_label;
Expand All @@ -48,6 +52,7 @@ class PinWidget : public QWidget

bool m_expanding{ false };
qreal m_scaleFactor{ 1 };
qreal m_opacity{ 1 };
unsigned int m_rotateFactor{ 0 };
qreal m_currentStepScaleFactor{ 1 };
bool m_sizeChanged{ false };
Expand Down

0 comments on commit f3577a0

Please sign in to comment.