blob: 20eb0c0e32073a533fcac9b211378f939c42dc27 [file] [log] [blame]
Olivier SOLDANO3dad1752017-07-13 14:08:53 -04001/***************************************************************************
2 * Copyright (C) 2015-2017 by Savoir-faire Linux *
3 * Author: Olivier Soldano <olivier.soldano@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 <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "photoboothwidget.h"
20#include "ui_photoboothwidget.h"
21
22#include <QFileDialog>
23#include <QStandardPaths>
Andreas Traczyk6c323c62018-12-26 13:04:22 -050024#include <QGraphicsOpacityEffect>
25#include <QtConcurrent/QtConcurrent>
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040026
27#include "video/previewmanager.h"
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050028
29#include "utils.h"
Andreas Traczyk6c323c62018-12-26 13:04:22 -050030#include "lrcinstance.h"
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050031
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040032
33PhotoboothWidget::PhotoboothWidget(QWidget *parent) :
34 QWidget(parent),
Andreas Traczyk6c323c62018-12-26 13:04:22 -050035 fileName_(""),
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040036 ui(new Ui::PhotoboothWidget)
37{
38 ui->setupUi(this);
39 ui->videoFeed->setIsFullPreview(true);
40 ui->videoFeed->setPhotoMode(true);
Andreas Traczyk6c323c62018-12-26 13:04:22 -050041
42 flashOverlay_ = new QLabel(this);
43 flashOverlay_->setStyleSheet("background-color:#fff");
44 flashOverlay_->hide();
45 QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
46 flashOverlay_->setGraphicsEffect(effect);
47 flashAnimation_ = new QPropertyAnimation(this);
48 flashAnimation_->setTargetObject(effect);
49 flashAnimation_->setPropertyName("opacity");
50 flashAnimation_->setDuration(600);
51 flashAnimation_->setStartValue(1);
52 flashAnimation_->setEndValue(0);
53 flashAnimation_->setEasingCurve(QEasingCurve::OutCubic);
54
55 takePhotoState_ = true;
56 ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-camera_alt-24px.svg"));
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040057}
58
59PhotoboothWidget::~PhotoboothWidget()
60{
61 Video::PreviewManager::instance().stopPreview();
62 delete ui;
63}
64
65void PhotoboothWidget::startBooth()
66{
Andreas Traczyk6c323c62018-12-26 13:04:22 -050067 ui->videoFeed->setResetPreview(true);
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040068 Video::PreviewManager::instance().stopPreview();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040069 Video::PreviewManager::instance().startPreview();
70 ui->videoFeed->show();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050071 ui->avatarLabel->hide();
72 takePhotoState_ = true;
Andreas Traczyk6c323c62018-12-26 13:04:22 -050073 ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-camera_alt-24px.svg"));
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040074}
75
76void PhotoboothWidget::stopBooth()
77{
78 Video::PreviewManager::instance().stopPreview();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050079 ui->videoFeed->hide();
80 ui->avatarLabel->show();
81 takePhotoState_ = false;
Andreas Traczyk6c323c62018-12-26 13:04:22 -050082 ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-refresh-24px.svg"));
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040083}
84
85void
86PhotoboothWidget::on_importButton_clicked()
87{
Andreas Traczyk6c323c62018-12-26 13:04:22 -050088 Video::PreviewManager::instance().stopPreview();
89 auto picturesDir = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040090 fileName_ = QFileDialog::getOpenFileName(this, tr("Choose File"),
Andreas Traczyk6c323c62018-12-26 13:04:22 -050091 picturesDir,
92 tr("Image Files (*.jpg, *.jpeg, *png)"));
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050093 if (fileName_.isEmpty()) {
Andreas Traczyk6c323c62018-12-26 13:04:22 -050094 Video::PreviewManager::instance().startPreview();
95 return;
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040096 }
Andreas Traczyk6c323c62018-12-26 13:04:22 -050097 Video::PreviewManager::instance().stopPreview();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050098 auto image = QImage(fileName_);
99 auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500100 auto avatarPixmap = QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width()));
101 LRCInstance::setCurrAccAvatar(avatarPixmap);
102 ui->avatarLabel->setPixmap(avatarPixmap);
103 stopBooth();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -0400104}
105
106void
107PhotoboothWidget::on_takePhotoButton_clicked()
108{
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500109 if (!takePhotoState_) {
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500110 startBooth();
111 return;
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500112 } else {
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500113 auto videoRect = ui->videoFeed->rect();
114 QPoint avatarLabelPos = ui->videoFeed->mapTo(this, videoRect.topLeft());
115 flashOverlay_->setGeometry(
116 avatarLabelPos.x(),
117 avatarLabelPos.y(),
118 videoRect.width(),
119 videoRect.height()
120 );
121 flashOverlay_->show();
122 flashAnimation_->start(QPropertyAnimation::KeepWhenStopped);
123
124 QtConcurrent::run(
125 [=] {
126 Video::PreviewManager::instance().stopPreview();
127 auto photo = ui->videoFeed->takePhoto();
128 auto avatar = photo.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
129 auto avatarPixmap = QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width()));
130 LRCInstance::setCurrAccAvatar(avatarPixmap);
131 ui->avatarLabel->setPixmap(avatarPixmap);
132 stopBooth();
133 });
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500134 }
Olivier SOLDANO3dad1752017-07-13 14:08:53 -0400135}