blob: aa2d8d99f2a39762ad61074eb033eaf56a25ca9b [file] [log] [blame]
Isa Nanic6e4a39a2018-12-04 14:26:02 -05001/**************************************************************************
Sébastien Blin68abac92019-01-02 17:41:31 -05002* Copyright (C) 2019-2019 by Savoir-faire Linux *
Isa Nanic6e4a39a2018-12-04 14:26:02 -05003* Author: Isa Nanic <isa.nanic@savoirfairelinux.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 3 of the License, or *
8* (at your option) any later version. *
9* *
10* This program is distributed in the hope that it will be useful, *
11* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13* GNU General Public License for more details. *
14* *
15* You should have received a copy of the GNU General Public License *
16* along with this program. If not, see <https://www.gnu.org/licenses/>. *
17**************************************************************************/
18
Sébastien Blind2e8d0e2019-01-10 14:18:36 -050019#include <QtMultimedia/QCameraInfo>
Isa Nanic6e4a39a2018-12-04 14:26:02 -050020#include <QFileDialog>
21#include <QAbstractScrollArea>
22#include <QScreen>
23#include <QWindow>
Sébastien Blind2e8d0e2019-01-10 14:18:36 -050024#include <QtMultimedia/QCameraImageCapture>
Isa Nanic6e4a39a2018-12-04 14:26:02 -050025#include <QBuffer>
Andreas Traczyk129985f2019-01-09 20:36:57 -050026#include <QStandardPaths>
Isa Nanic6e4a39a2018-12-04 14:26:02 -050027
28#include "setavatardialog.h"
29#include "ui_setavatardialog.h"
30
31#include "lrcinstance.h"
32#include "accountlistmodel.h"
33#include "currentaccountcombobox.h"
34
35SetAvatarDialog::SetAvatarDialog(QWidget* parent)
36 : QDialog(parent),
37 ui(new Ui::SetAvatarDialog),
38 videoWidget_(new QCameraViewfinder(this)),
39 camera_(new QCamera)
40{
41 ui->setupUi(this);
42
43 connect(ui->cancelButton, &QPushButton::clicked, [&]() {
44 done(0);
45 }
46 );
47
48 connect(ui->saveButton, &QPushButton::clicked, this, &SetAvatarDialog::saveAvatar);
49
50 connect(ui->selectFileButton, &QPushButton::clicked, this, &SetAvatarDialog::openFileManager);
51
52 connect(ui->takePictureButton, &QPushButton::clicked, this, &SetAvatarDialog::captureImage);
53
54 connect(ui->cameraButton, &QPushButton::clicked, this, &SetAvatarDialog::pictureMode);
55
56 ui->graphicsView->setAvatarSize(avatarSize_);
57
58 videoWidget_->setGeometry(ui->graphicsView->x(), ui->graphicsView->y(),
59 ui->graphicsView->width(), ui->graphicsView->height());
60
61 pictureMode();
62 startCamera();
63
64 ui->horizontalLayout_2->update();
65}
66
67SetAvatarDialog::~SetAvatarDialog()
68{
69 disconnect(this);
70 delete mediaPlayer_;
71 delete videoWidget_;
72 delete imageCapture_;
73 delete camera_;
74 delete ui;
75}
76
77void
78SetAvatarDialog::startCamera()
79{
80 imageCapture_ = new QCameraImageCapture(camera_, this);
81 camera_->setCaptureMode(QCamera::CaptureViewfinder);
82 connect(imageCapture_, SIGNAL(imageSaved(int, const QString&)),
83 this, SLOT(imageCaptureSlot(int, const QString&)));
84}
85
86bool
87SetAvatarDialog::checkCamAvailable()
88{
89 if (QCameraInfo::availableCameras().count() > 0) {
90 return true;
91 }
92 else {
93 return false;
94 }
95}
96
97// no support yet for back-facing camera [todo]; no support for portrait mode [todo]
98void
99SetAvatarDialog::pictureMode()
100{
101 if (checkCamAvailable()) {
102 camera_ = new QCamera(QCamera::FrontFace);
103 camera_->setViewfinder(videoWidget_);
104
105 ui->graphicsView->hide();
106 videoWidget_->show();
107
108 camera_->start();
109
110 ui->saveButton->hide();
111 ui->cameraButton->hide();
112
113 ui->takePictureButton->show();
114 ui->selectFileButton->show();
115 }
116 else {
117 openFileManager(); // no camera detected so open file manager
118 }
119}
120
121void
122SetAvatarDialog::editMode()
123{
124 camera_->unlock();
125 camera_->stop();
126
127 ui->takePictureButton->hide();
128
129 ui->saveButton->show();
130 ui->cameraButton->show();
131 ui->selectFileButton->show();
132
133 videoWidget_->hide();
134 ui->graphicsView->show();
135 configureAvatarScene(rawPixmap_);
136}
137
138void
139SetAvatarDialog::openFileManager()
140{
Andreas Traczyk129985f2019-01-09 20:36:57 -0500141 auto picturesDir = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first();
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500142 rawPixmap_.load(QFileDialog::getOpenFileName(this,
Andreas Traczyk129985f2019-01-09 20:36:57 -0500143 tr("Open Image"), picturesDir, tr("Image Files (*.jpg *.jpeg *.png *.bmp)")));
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500144 editMode();
145}
146
147void
148SetAvatarDialog::configureAvatarScene(const QPixmap& pixMap)
149{
150 if (!pixMap.isNull()) {
151 const int margin = 8;
152 QPen pen;
153 pen.setStyle(Qt::NoPen);
154
155 graphicsScene_.clear();
156
157 graphicsScene_.addPixmap(pixMap);
158 graphicsScene_.addRect(-(pixMap.width()*margin - avatarSize_) / 2,
159 -(pixMap.height() * margin - avatarSize_) / 2, pixMap.width() * margin, pixMap.height() * margin, pen, QBrush());
160 ui->graphicsView->setScene(&graphicsScene_);
161 }
162}
163
164void
165SetAvatarDialog::captureImage()
166{
167 startCamera();
168
169 camera_->setCaptureMode(QCamera::CaptureStillImage);
170 imageCapture_->setCaptureDestination(QCameraImageCapture::CaptureToFile);
171
172 if (imageCapture_->isCaptureDestinationSupported(QCameraImageCapture::CaptureToFile)) {
173 camera_->searchAndLock();
174 camera_->start();
175 imageCapture_->capture();
176 }
177}
178
179// save avatar image and exit SetAvatarDialog
180void
181SetAvatarDialog::saveAvatar()
182{
183 avatarSize_ *= 2;
184
185 QRect avatarRect((ui->graphicsView->width() - avatarSize_)/2 + ui->graphicsView->x(), (ui->graphicsView->height() - avatarSize_)/2 + ui->graphicsView->y(),
186 avatarSize_, avatarSize_);
187
188 QRegion avatarRegion(avatarRect.x() - ui->graphicsView->x(), avatarRect.y() - ui->graphicsView->y(),
189 avatarSize_, avatarSize_, QRegion::Ellipse);
190 ui->graphicsView->setMask(avatarRegion);
191
192 finalAvatarPixmap_ = grab(avatarRect);
193
194 done(0);
195}
196
197void
198SetAvatarDialog::done(int exCode)
199{
200 QDialog::done(exCode);
201 if (!finalAvatarPixmap_.isNull()) {
202 QByteArray ba;
203 QBuffer bu(&ba);
204 finalAvatarPixmap_.save(&bu, "PNG");
205 emit pixmapSignal(ba.toBase64().toStdString());
206 }
207 else {
208 emit pixmapSignal(std::string());
209 }
210 camera_->unlock();
211 camera_->stop();
212}
213
214void
215SetAvatarDialog::imageCaptureSlot(int useless, const QString& text)
216{
217 Q_UNUSED(useless);
218
219 rawPixmap_ = rawPixmap_.fromImage(QImage(text));
220
221 if (!rawPixmap_.isNull()){
222 editMode();
223 }
224 QFile file(text);
225 file.remove(); // erase file
226}