Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 24 | #include <QGraphicsOpacityEffect> |
| 25 | #include <QtConcurrent/QtConcurrent> |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 26 | |
| 27 | #include "video/previewmanager.h" |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 28 | |
| 29 | #include "utils.h" |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 30 | #include "lrcinstance.h" |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 31 | |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 32 | |
| 33 | PhotoboothWidget::PhotoboothWidget(QWidget *parent) : |
| 34 | QWidget(parent), |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 35 | fileName_(""), |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 36 | ui(new Ui::PhotoboothWidget) |
| 37 | { |
| 38 | ui->setupUi(this); |
| 39 | ui->videoFeed->setIsFullPreview(true); |
| 40 | ui->videoFeed->setPhotoMode(true); |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 41 | |
| 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 SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | PhotoboothWidget::~PhotoboothWidget() |
| 60 | { |
| 61 | Video::PreviewManager::instance().stopPreview(); |
| 62 | delete ui; |
| 63 | } |
| 64 | |
| 65 | void PhotoboothWidget::startBooth() |
| 66 | { |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 67 | ui->videoFeed->setResetPreview(true); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 68 | Video::PreviewManager::instance().stopPreview(); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 69 | Video::PreviewManager::instance().startPreview(); |
| 70 | ui->videoFeed->show(); |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 71 | ui->avatarLabel->hide(); |
| 72 | takePhotoState_ = true; |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 73 | ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-camera_alt-24px.svg")); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void PhotoboothWidget::stopBooth() |
| 77 | { |
| 78 | Video::PreviewManager::instance().stopPreview(); |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 79 | ui->videoFeed->hide(); |
| 80 | ui->avatarLabel->show(); |
| 81 | takePhotoState_ = false; |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 82 | ui->takePhotoButton->setIcon(QPixmap(":/images/icons/baseline-refresh-24px.svg")); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
| 86 | PhotoboothWidget::on_importButton_clicked() |
| 87 | { |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 88 | Video::PreviewManager::instance().stopPreview(); |
| 89 | auto picturesDir = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first(); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 90 | fileName_ = QFileDialog::getOpenFileName(this, tr("Choose File"), |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 91 | picturesDir, |
| 92 | tr("Image Files (*.jpg, *.jpeg, *png)")); |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 93 | if (fileName_.isEmpty()) { |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 94 | Video::PreviewManager::instance().startPreview(); |
| 95 | return; |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 96 | } |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 97 | Video::PreviewManager::instance().stopPreview(); |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 98 | auto image = QImage(fileName_); |
| 99 | auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 100 | auto avatarPixmap = QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width())); |
| 101 | LRCInstance::setCurrAccAvatar(avatarPixmap); |
| 102 | ui->avatarLabel->setPixmap(avatarPixmap); |
| 103 | stopBooth(); |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
| 107 | PhotoboothWidget::on_takePhotoButton_clicked() |
| 108 | { |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 109 | if (!takePhotoState_) { |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 110 | startBooth(); |
| 111 | return; |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 112 | } else { |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame^] | 113 | 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 Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 134 | } |
Olivier SOLDANO | 3dad175 | 2017-07-13 14:08:53 -0400 | [diff] [blame] | 135 | } |