1 /*
2 Drawpile - a collaborative drawing program.
3
4 Copyright (C) 2018-2019 Calle Laakkonen
5
6 Drawpile is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Drawpile is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Drawpile. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "avatarimport.h"
21 #include "utils/avatarlistmodel.h"
22
23 #include "ui_avatarimport.h"
24
25 #include <QImageReader>
26 #include <QFileDialog>
27 #include <QMessageBox>
28
29 namespace dialogs {
30
AvatarImport(const QImage & source,QWidget * parent)31 AvatarImport::AvatarImport(const QImage &source, QWidget *parent)
32 : QDialog(parent), m_ui(new Ui_AvatarImport), m_originalImage(source)
33 {
34 const int maxSize = qMin(source.width(), source.height());
35
36 m_ui->setupUi(this);
37 m_ui->resizer->setImage(source);
38 m_ui->resizer->setOriginalSize(source.size());
39 m_ui->resizer->setTargetSize(QSize { maxSize, maxSize });
40
41 m_ui->sizeSlider->setMaximum(maxSize);
42 m_ui->sizeSlider->setValue(maxSize);
43
44 connect(m_ui->sizeSlider, &QSlider::valueChanged, this, [this](int value) {
45 m_ui->resizer->setTargetSize(QSize { value, value });
46 });
47 }
48
~AvatarImport()49 AvatarImport::~AvatarImport()
50 {
51 delete m_ui;
52 }
53
croppedAvatar() const54 QImage AvatarImport::croppedAvatar() const
55 {
56 const QPoint offset = m_ui->resizer->offset();
57 const QSize size = m_ui->resizer->targetSize();
58
59 return m_originalImage.copy(
60 -offset.x(),
61 -offset.y(),
62 size.width(),
63 size.height()
64 );
65 }
66
importAvatar(AvatarListModel * avatarList,QPointer<QWidget> parentWindow)67 void AvatarImport::importAvatar(AvatarListModel *avatarList, QPointer<QWidget> parentWindow)
68 {
69 QString formats;
70 for(QByteArray format : QImageReader::supportedImageFormats()) {
71 formats += "*." + format + " ";
72 }
73
74 QString path = QFileDialog::getOpenFileName(parentWindow, tr("Import Avatar"), QString(),
75 tr("Images (%1)").arg(formats) + ";;" +
76 QApplication::tr("All files (*)")
77 );
78
79 if(path.isEmpty())
80 return;
81
82 const QImage picture(path);
83 if(picture.isNull()) {
84 QMessageBox::warning(parentWindow, tr("Import Avatar"), tr("Couldn't read image"));
85 return;
86 }
87
88 if(picture.width() < Size || picture.height() < Size) {
89 QMessageBox::warning(parentWindow, tr("Import Avatar"), tr("Picture is too small"));
90 return;
91 }
92
93 if(picture.width() != picture.height()) {
94 // Not square format: needs cropping
95 auto *dlg = new dialogs::AvatarImport(picture, parentWindow);
96 dlg->setModal(true);
97 dlg->setAttribute(Qt::WA_DeleteOnClose);
98 connect(dlg, &QDialog::accepted, avatarList, [parentWindow, dlg, avatarList]() {
99 avatarList->addAvatar(QPixmap::fromImage(dlg->croppedAvatar().scaled(Size, Size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
100 });
101
102 dlg->show();
103
104 } else {
105 avatarList->addAvatar(QPixmap::fromImage(picture.scaled(Size, Size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
106 }
107 }
108
109 }
110