summary refs log tree commit diff
path: root/src/dialogs/PreviewImageOverlay.cc
blob: 31ef00ed0b2ffffa6eae4d69915b356972954024 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QBuffer>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "Config.h"

#include "dialogs/PreviewImageOverlay.h"

using namespace dialogs;

static constexpr const char *DEFAULT = "Upload image?";
static constexpr const char *ERROR   = "Failed to load image type '%1'. Continue upload?";

PreviewImageOverlay::PreviewImageOverlay(QWidget *parent)
  : QWidget{parent}
  , titleLabel_{tr(DEFAULT), this}
  , imageLabel_{this}
  , imageName_{tr("clipboard"), this}
  , upload_{tr("Upload"), this}
  , cancel_{tr("Cancel"), this}
{
        auto hlayout = new QHBoxLayout;
        hlayout->addWidget(&upload_);
        hlayout->addWidget(&cancel_);

        auto vlayout = new QVBoxLayout{this};
        vlayout->addWidget(&titleLabel_);
        vlayout->addWidget(&imageLabel_);
        vlayout->addWidget(&imageName_);
        vlayout->addLayout(hlayout);

        connect(&upload_, &QPushButton::clicked, [&]() {
                emit confirmImageUpload(imageData_, imageName_.text());
                close();
        });
        connect(&cancel_, &QPushButton::clicked, [&]() { close(); });
}

void
PreviewImageOverlay::init()
{
        auto window   = QApplication::activeWindow();
        auto winsize  = window->frameGeometry().size();
        auto center   = window->frameGeometry().center();
        auto img_size = image_.size();

        imageName_.setText(QFileInfo{imagePath_}.fileName());

        setAutoFillBackground(true);
        setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::WindowModal);

        titleLabel_.setStyleSheet(
          QString{"font-weight: bold; font-size: %1px;"}.arg(conf::headerFontSize));
        titleLabel_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        titleLabel_.setAlignment(Qt::AlignCenter);
        imageLabel_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
        imageLabel_.setAlignment(Qt::AlignCenter);
        imageName_.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        imageName_.setAlignment(Qt::AlignCenter);
        upload_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        cancel_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        upload_.setFontSize(conf::btn::fontSize);
        cancel_.setFontSize(conf::btn::fontSize);

        // Scale image preview to the size of the current window if it is larger.
        if ((img_size.height() * img_size.width()) > (winsize.height() * winsize.width())) {
                imageLabel_.setPixmap(image_.scaled(winsize, Qt::KeepAspectRatio));
        } else {
                imageLabel_.setPixmap(image_);
                move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
        }
        imageLabel_.setScaledContents(false);

        raise();
}

void
PreviewImageOverlay::setImageAndCreate(const QByteArray data, const QString &type)
{
        imageData_  = data;
        imagePath_  = "clipboard." + type;
        auto loaded = image_.loadFromData(imageData_);
        if (!loaded) {
                titleLabel_.setText(QString{tr(ERROR)}.arg(type));
        } else {
                titleLabel_.setText(tr(DEFAULT));
        }

        init();
}

void
PreviewImageOverlay::setImageAndCreate(const QString &path)
{
        QFile file{path};
        imagePath_ = path;

        if (!file.open(QIODevice::ReadOnly)) {
                qWarning() << "Failed to open image from:" << path;
                qWarning() << "Reason:" << file.errorString();
                close();
                return;
        }

        if ((imageData_ = file.readAll()).isEmpty()) {
                qWarning() << "Failed to read image:" << file.errorString();
                close();
                return;
        }

        auto loaded = image_.loadFromData(imageData_);
        if (!loaded) {
                auto t = QFileInfo{path}.suffix();
                titleLabel_.setText(QString{tr(ERROR)}.arg(t));
        } else {
                titleLabel_.setText(tr(DEFAULT));
        }

        init();
}