2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2018-02-18 23:52:31 +03:00
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "dialogs/PreviewUploadOverlay.h"
|
|
|
|
|
2018-02-18 23:52:31 +03:00
|
|
|
#include "Config.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Logging.h"
|
2018-07-15 20:58:10 +03:00
|
|
|
#include "MainWindow.h"
|
2018-02-18 23:52:31 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
using namespace dialogs;
|
|
|
|
|
2018-06-17 00:23:49 +03:00
|
|
|
constexpr const char *DEFAULT = "Upload %1?";
|
|
|
|
constexpr const char *ERR_MSG = "Failed to load image type '%1'. Continue upload?";
|
2018-02-18 23:52:31 +03:00
|
|
|
|
|
|
|
PreviewUploadOverlay::PreviewUploadOverlay(QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
, titleLabel_{this}
|
|
|
|
, fileName_{this}
|
2018-09-25 14:41:47 +03:00
|
|
|
, upload_{tr("Upload"), this}
|
|
|
|
, cancel_{tr("Cancel"), this}
|
2018-02-18 23:52:31 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto hlayout = new QHBoxLayout;
|
|
|
|
hlayout->addStretch(1);
|
|
|
|
hlayout->addWidget(&cancel_);
|
|
|
|
hlayout->addWidget(&upload_);
|
|
|
|
hlayout->setMargin(0);
|
|
|
|
|
|
|
|
auto vlayout = new QVBoxLayout{this};
|
|
|
|
vlayout->addWidget(&titleLabel_);
|
|
|
|
vlayout->addWidget(&infoLabel_);
|
|
|
|
vlayout->addWidget(&fileName_);
|
|
|
|
vlayout->addLayout(hlayout);
|
|
|
|
vlayout->setSpacing(conf::modals::WIDGET_SPACING);
|
|
|
|
vlayout->setMargin(conf::modals::WIDGET_MARGIN);
|
|
|
|
|
|
|
|
upload_.setDefault(true);
|
|
|
|
connect(&upload_, &QPushButton::clicked, [this]() {
|
|
|
|
emit confirmUpload(data_, mediaType_, fileName_.text());
|
|
|
|
close();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(&fileName_, &QLineEdit::returnPressed, this, [this]() {
|
|
|
|
emit confirmUpload(data_, mediaType_, fileName_.text());
|
|
|
|
close();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(&cancel_, &QPushButton::clicked, this, [this]() {
|
|
|
|
emit aborted();
|
|
|
|
close();
|
|
|
|
});
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PreviewUploadOverlay::init()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QSize winsize;
|
|
|
|
QPoint center;
|
|
|
|
|
|
|
|
auto window = MainWindow::instance();
|
|
|
|
if (window) {
|
|
|
|
winsize = window->frameGeometry().size();
|
|
|
|
center = window->frameGeometry().center();
|
|
|
|
} else {
|
|
|
|
nhlog::ui()->warn("unable to retrieve MainWindow's size");
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName_.setText(QFileInfo{filePath_}.fileName());
|
|
|
|
|
|
|
|
setAutoFillBackground(true);
|
|
|
|
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
|
|
|
|
setWindowModality(Qt::WindowModal);
|
|
|
|
|
|
|
|
QFont font;
|
|
|
|
font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO);
|
|
|
|
|
|
|
|
titleLabel_.setFont(font);
|
|
|
|
titleLabel_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
titleLabel_.setAlignment(Qt::AlignCenter);
|
|
|
|
infoLabel_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
|
|
|
fileName_.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
|
|
fileName_.setAlignment(Qt::AlignCenter);
|
|
|
|
upload_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
cancel_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
|
|
|
|
if (isImage_) {
|
|
|
|
infoLabel_.setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
|
|
const auto maxWidth = winsize.width() * 0.8;
|
|
|
|
const auto maxHeight = winsize.height() * 0.8;
|
|
|
|
|
|
|
|
// Scale image preview to fit into the application window.
|
|
|
|
infoLabel_.setPixmap(utils::scaleDown(maxWidth, maxHeight, image_));
|
|
|
|
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
|
|
|
|
} else {
|
|
|
|
infoLabel_.setAlignment(Qt::AlignLeft);
|
|
|
|
}
|
|
|
|
infoLabel_.setScaledContents(false);
|
|
|
|
|
|
|
|
show();
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-19 23:09:21 +03:00
|
|
|
PreviewUploadOverlay::setLabels(const QString &type, const QString &mime, uint64_t upload_size)
|
2018-02-18 23:52:31 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (mediaType_.split('/')[0] == "image") {
|
|
|
|
if (!image_.loadFromData(data_)) {
|
|
|
|
titleLabel_.setText(QString{tr(ERR_MSG)}.arg(type));
|
2018-02-18 23:52:31 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
titleLabel_.setText(QString{tr(DEFAULT)}.arg(mediaType_));
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
isImage_ = true;
|
|
|
|
} else {
|
|
|
|
auto const info = QString{tr("Media type: %1\n"
|
|
|
|
"Media size: %2\n")}
|
|
|
|
.arg(mime)
|
|
|
|
.arg(utils::humanReadableFileSize(upload_size));
|
|
|
|
|
|
|
|
titleLabel_.setText(QString{tr(DEFAULT)}.arg("file"));
|
|
|
|
infoLabel_.setText(info);
|
|
|
|
}
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 03:38:44 +03:00
|
|
|
void
|
|
|
|
PreviewUploadOverlay::setPreview(const QImage &src, const QString &mime)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->info(
|
|
|
|
"Pasting image with size: {}x{}, format: {}", src.height(), src.width(), mime.toStdString());
|
|
|
|
|
|
|
|
auto const &split = mime.split('/');
|
|
|
|
auto const &type = split[1];
|
2019-07-22 03:38:44 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QBuffer buffer(&data_);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
if (src.save(&buffer, type.toStdString().c_str()))
|
2019-07-22 03:38:44 +03:00
|
|
|
titleLabel_.setText(QString{tr(DEFAULT)}.arg("image"));
|
2021-09-18 01:22:33 +03:00
|
|
|
else
|
|
|
|
titleLabel_.setText(QString{tr(ERR_MSG)}.arg(type));
|
|
|
|
|
|
|
|
mediaType_ = mime;
|
|
|
|
filePath_ = "clipboard." + type;
|
|
|
|
image_.convertFromImage(src);
|
|
|
|
isImage_ = true;
|
|
|
|
|
|
|
|
titleLabel_.setText(QString{tr(DEFAULT)}.arg("image"));
|
|
|
|
init();
|
2019-07-22 03:38:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 23:52:31 +03:00
|
|
|
void
|
|
|
|
PreviewUploadOverlay::setPreview(const QByteArray data, const QString &mime)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto const &split = mime.split('/');
|
|
|
|
auto const &type = split[1];
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
data_ = data;
|
|
|
|
mediaType_ = mime;
|
|
|
|
filePath_ = "clipboard." + type;
|
|
|
|
isImage_ = false;
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setLabels(type, mime, data_.size());
|
|
|
|
init();
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PreviewUploadOverlay::setPreview(const QString &path)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QFile file{path};
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
nhlog::ui()->warn(
|
|
|
|
"Failed to open file ({}): {}", path.toStdString(), file.errorString().toStdString());
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QMimeDatabase db;
|
|
|
|
auto mime = db.mimeTypeForFileNameAndData(path, &file);
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if ((data_ = file.readAll()).isEmpty()) {
|
|
|
|
nhlog::ui()->warn("Failed to read media: {}", file.errorString().toStdString());
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto const &split = mime.name().split('/');
|
2018-02-18 23:52:31 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mediaType_ = mime.name();
|
|
|
|
filePath_ = file.fileName();
|
|
|
|
isImage_ = false;
|
|
|
|
|
|
|
|
setLabels(split[1], mime.name(), data_.size());
|
|
|
|
init();
|
2018-02-18 23:52:31 +03:00
|
|
|
}
|
2021-03-20 13:18:16 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
PreviewUploadOverlay::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (event->matches(QKeySequence::Cancel)) {
|
|
|
|
emit aborted();
|
|
|
|
close();
|
|
|
|
} else {
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
}
|
2021-03-20 13:18:16 +03:00
|
|
|
}
|