nheko/src/dialogs/ImageOverlay.cpp

103 lines
3.0 KiB
C++
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QApplication>
#include <QDesktopWidget>
#include <QGuiApplication>
#include <QPainter>
#include <QScreen>
#include "dialogs/ImageOverlay.h"
2018-07-17 15:37:25 +02:00
#include "Utils.h"
using namespace dialogs;
ImageOverlay::ImageOverlay(QPixmap image, QWidget *parent)
2017-11-05 22:04:55 +01:00
: QWidget{parent}
, originalImage_{image}
{
2021-09-18 00:22:33 +02:00
setMouseTracking(true);
setParent(nullptr);
2021-09-18 00:22:33 +02:00
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
2021-10-20 00:41:32 +02:00
setWindowRole("imageoverlay");
2021-09-18 00:22:33 +02:00
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_DeleteOnClose, true);
setWindowState(Qt::WindowFullScreen);
close_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Escape), this);
2021-09-18 00:22:33 +02:00
connect(close_shortcut_, &QShortcut::activated, this, &ImageOverlay::closing);
connect(this, &ImageOverlay::closing, this, &ImageOverlay::close);
2021-09-18 00:22:33 +02:00
raise();
}
2017-08-20 12:47:22 +02:00
void
ImageOverlay::paintEvent(QPaintEvent *event)
{
2021-09-18 00:22:33 +02:00
Q_UNUSED(event);
2021-09-18 00:22:33 +02:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
2021-09-18 00:22:33 +02:00
// Full screen overlay.
painter.fillRect(QRect(0, 0, width(), height()), QColor(55, 55, 55, 170));
2021-09-18 00:22:33 +02:00
// Left and Right margins
int outer_margin = width() * 0.12;
int buttonSize = 36;
int margin = outer_margin * 0.1;
2021-09-18 00:22:33 +02:00
int max_width = width() - 2 * outer_margin;
int max_height = height();
2021-09-18 00:22:33 +02:00
image_ = utils::scaleDown(max_width, max_height, originalImage_);
2021-09-18 00:22:33 +02:00
int diff_x = max_width - image_.width();
int diff_y = max_height - image_.height();
2021-09-18 00:22:33 +02:00
content_ = QRect(outer_margin + diff_x / 2, diff_y / 2, image_.width(), image_.height());
close_button_ = QRect(width() - margin - buttonSize, margin, buttonSize, buttonSize);
save_button_ = QRect(width() - (2 * margin) - (2 * buttonSize), margin, buttonSize, buttonSize);
2021-09-18 00:22:33 +02:00
// Draw main content_.
painter.drawPixmap(content_, image_);
2021-09-18 00:22:33 +02:00
// Draw top right corner X.
QPen pen;
pen.setCapStyle(Qt::RoundCap);
pen.setWidthF(5);
pen.setColor("gray");
2021-09-18 00:22:33 +02:00
auto center = close_button_.center();
2021-09-18 00:22:33 +02:00
painter.setPen(pen);
painter.drawLine(center - QPointF(15, 15), center + QPointF(15, 15));
painter.drawLine(center + QPointF(15, -15), center - QPointF(15, -15));
2021-09-18 00:22:33 +02:00
// Draw download button
center = save_button_.center();
painter.drawLine(center - QPointF(0, 15), center + QPointF(0, 15));
painter.drawLine(center - QPointF(15, 0), center + QPointF(0, 15));
painter.drawLine(center + QPointF(0, 15), center + QPointF(15, 0));
}
2017-08-20 12:47:22 +02:00
void
ImageOverlay::mousePressEvent(QMouseEvent *event)
{
2021-09-18 00:22:33 +02:00
if (event->button() != Qt::LeftButton)
return;
if (close_button_.contains(event->pos()))
emit closing();
else if (save_button_.contains(event->pos()))
emit saving();
else if (!content_.contains(event->pos()))
emit closing();
}