blob: cab2241670fc525e0d8321613b01be4b4d80e1b6 [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 -040032PhotoboothWidget::PhotoboothWidget(QWidget *parent) :
33 QWidget(parent),
Andreas Traczyk6c323c62018-12-26 13:04:22 -050034 fileName_(""),
Andreas Traczyk07f83492018-12-26 14:01:46 -050035 ui(new Ui::PhotoboothWidget),
36 hasAvatar_(false)
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040037{
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 Traczyk07f83492018-12-26 14:01:46 -050067 hasAvatar_ = false;
Andreas Traczyk6c323c62018-12-26 13:04:22 -050068 ui->videoFeed->setResetPreview(true);
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040069 Video::PreviewManager::instance().stopPreview();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040070 Video::PreviewManager::instance().startPreview();
71 ui->videoFeed->show();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050072 ui->avatarLabel->hide();
73 takePhotoState_ = true;
Andreas Traczyk6c323c62018-12-26 13:04:22 -050074 ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-camera_alt-24px.svg"));
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040075}
76
77void PhotoboothWidget::stopBooth()
78{
79 Video::PreviewManager::instance().stopPreview();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050080 ui->videoFeed->hide();
81 ui->avatarLabel->show();
82 takePhotoState_ = false;
Andreas Traczyk6c323c62018-12-26 13:04:22 -050083 ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-refresh-24px.svg"));
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040084}
85
86void
87PhotoboothWidget::on_importButton_clicked()
88{
Andreas Traczyk6c323c62018-12-26 13:04:22 -050089 Video::PreviewManager::instance().stopPreview();
90 auto picturesDir = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040091 fileName_ = QFileDialog::getOpenFileName(this, tr("Choose File"),
Andreas Traczyk6c323c62018-12-26 13:04:22 -050092 picturesDir,
Andreas Traczyk129985f2019-01-09 20:36:57 -050093 tr("Image Files (*.jpg *.jpeg *.png)"));
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050094 if (fileName_.isEmpty()) {
Andreas Traczyk6c323c62018-12-26 13:04:22 -050095 Video::PreviewManager::instance().startPreview();
96 return;
Olivier SOLDANO3dad1752017-07-13 14:08:53 -040097 }
Andreas Traczyk6c323c62018-12-26 13:04:22 -050098 Video::PreviewManager::instance().stopPreview();
Andreas Traczyk6ace34f2018-12-14 14:31:23 -050099 auto image = QImage(fileName_);
100 auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
Andreas Traczyk07f83492018-12-26 14:01:46 -0500101 avatarPixmap_ = QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width()));
102 ui->avatarLabel->setPixmap(avatarPixmap_);
103 hasAvatar_ = true;
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500104 stopBooth();
Olivier SOLDANO3dad1752017-07-13 14:08:53 -0400105}
106
107void
108PhotoboothWidget::on_takePhotoButton_clicked()
109{
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500110 if (!takePhotoState_) {
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500111 startBooth();
112 return;
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500113 } else {
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500114 auto videoRect = ui->videoFeed->rect();
115 QPoint avatarLabelPos = ui->videoFeed->mapTo(this, videoRect.topLeft());
116 flashOverlay_->setGeometry(
117 avatarLabelPos.x(),
118 avatarLabelPos.y(),
119 videoRect.width(),
120 videoRect.height()
121 );
122 flashOverlay_->show();
123 flashAnimation_->start(QPropertyAnimation::KeepWhenStopped);
124
125 QtConcurrent::run(
Andreas Traczyk07f83492018-12-26 14:01:46 -0500126 [this] {
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500127 Video::PreviewManager::instance().stopPreview();
128 auto photo = ui->videoFeed->takePhoto();
129 auto avatar = photo.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
Andreas Traczyk07f83492018-12-26 14:01:46 -0500130 avatarPixmap_ = QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width()));
131 ui->avatarLabel->setPixmap(avatarPixmap_);
132 hasAvatar_ = true;
Andreas Traczyk6c323c62018-12-26 13:04:22 -0500133 stopBooth();
134 });
Andreas Traczyk6ace34f2018-12-14 14:31:23 -0500135 }
Olivier SOLDANO3dad1752017-07-13 14:08:53 -0400136}
Andreas Traczyk07f83492018-12-26 14:01:46 -0500137
138const QPixmap&
139PhotoboothWidget::getAvatarPixmap()
140{
141 return avatarPixmap_;
142}
143
144bool
145PhotoboothWidget::hasAvatar()
146{
147 return hasAvatar_;
148}