photoboothwidget.cpp 5.76 KB
Newer Older
1
/***************************************************************************
Andreas Traczyk's avatar
Andreas Traczyk committed
2
 * Copyright (C) 2015-2019 by Savoir-faire Linux                           *
3
 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
Andreas Traczyk's avatar
Andreas Traczyk committed
4
 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>          *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "photoboothwidget.h"
#include "ui_photoboothwidget.h"

#include <QFileDialog>
#include <QStandardPaths>
Andreas Traczyk's avatar
Andreas Traczyk committed
25 26
#include <QGraphicsOpacityEffect>
#include <QtConcurrent/QtConcurrent>
27 28

#include "video/previewmanager.h"
Andreas Traczyk's avatar
Andreas Traczyk committed
29 30

#include "utils.h"
Andreas Traczyk's avatar
Andreas Traczyk committed
31
#include "lrcinstance.h"
Andreas Traczyk's avatar
Andreas Traczyk committed
32

33 34
PhotoboothWidget::PhotoboothWidget(QWidget *parent) :
    QWidget(parent),
Andreas Traczyk's avatar
Andreas Traczyk committed
35
    fileName_(""),
36 37
    ui(new Ui::PhotoboothWidget),
    hasAvatar_(false)
38 39 40 41
{
    ui->setupUi(this);
    ui->videoFeed->setIsFullPreview(true);
    ui->videoFeed->setPhotoMode(true);
Andreas Traczyk's avatar
Andreas Traczyk committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    flashOverlay_ = new QLabel(this);
    flashOverlay_->setStyleSheet("background-color:#fff");
    flashOverlay_->hide();
    QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
    flashOverlay_->setGraphicsEffect(effect);
    flashAnimation_ = new QPropertyAnimation(this);
    flashAnimation_->setTargetObject(effect);
    flashAnimation_->setPropertyName("opacity");
    flashAnimation_->setDuration(600);
    flashAnimation_->setStartValue(1);
    flashAnimation_->setEndValue(0);
    flashAnimation_->setEasingCurve(QEasingCurve::OutCubic);

    takePhotoState_ = true;
Andreas Traczyk's avatar
Andreas Traczyk committed
57
    ui->takePhotoButton->setIcon(QIcon(":/images/icons/baseline-camera_alt-24px.svg"));
58 59 60 61 62 63 64 65 66 67
}

PhotoboothWidget::~PhotoboothWidget()
{
    Video::PreviewManager::instance().stopPreview();
    delete ui;
}

void PhotoboothWidget::startBooth()
{
68
    hasAvatar_ = false;
Andreas Traczyk's avatar
Andreas Traczyk committed
69
    ui->videoFeed->setResetPreview(true);
70 71 72
    Video::PreviewManager::instance().stopPreview();
    Video::PreviewManager::instance().startPreview();
    ui->videoFeed->show();
Andreas Traczyk's avatar
Andreas Traczyk committed
73 74
    ui->avatarLabel->hide();
    takePhotoState_ = true;
Andreas Traczyk's avatar
Andreas Traczyk committed
75
    ui->takePhotoButton->setIcon(QIcon(":/images/icons/baseline-camera_alt-24px.svg"));
76 77 78 79 80
}

void PhotoboothWidget::stopBooth()
{
    Video::PreviewManager::instance().stopPreview();
Andreas Traczyk's avatar
Andreas Traczyk committed
81 82 83
    ui->videoFeed->hide();
    ui->avatarLabel->show();
    takePhotoState_ = false;
Andreas Traczyk's avatar
Andreas Traczyk committed
84
    ui->takePhotoButton->setIcon(QIcon(":/images/icons/baseline-refresh-24px.svg"));
85 86 87 88 89
}

void
PhotoboothWidget::on_importButton_clicked()
{
Andreas Traczyk's avatar
Andreas Traczyk committed
90 91
    Video::PreviewManager::instance().stopPreview();
    auto picturesDir = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first();
92
    fileName_ = QFileDialog::getOpenFileName(this, tr("Choose File"),
Andreas Traczyk's avatar
Andreas Traczyk committed
93
                                             picturesDir,
94
                                             tr("Image Files (*.jpg *.jpeg *.png)"));
Andreas Traczyk's avatar
Andreas Traczyk committed
95
    if (fileName_.isEmpty()) {
Andreas Traczyk's avatar
Andreas Traczyk committed
96 97
        Video::PreviewManager::instance().startPreview();
        return;
98
    }
Andreas Traczyk's avatar
Andreas Traczyk committed
99
    Video::PreviewManager::instance().stopPreview();
Andreas Traczyk's avatar
Andreas Traczyk committed
100 101 102 103
    auto image = Utils::cropImage(QImage(fileName_));
    auto avatar = image.scaled(224, 224, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    avatarPixmap_ = QPixmap::fromImage(avatar);
    ui->avatarLabel->setPixmap(QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width())));
104
    hasAvatar_ = true;
Andreas Traczyk's avatar
Andreas Traczyk committed
105
    emit photoTaken();
Andreas Traczyk's avatar
Andreas Traczyk committed
106
    stopBooth();
107 108 109 110 111
}

void
PhotoboothWidget::on_takePhotoButton_clicked()
{
Andreas Traczyk's avatar
Andreas Traczyk committed
112 113
    if (!takePhotoState_) { // restart
        emit clearedPhoto();
Andreas Traczyk's avatar
Andreas Traczyk committed
114 115
        startBooth();
        return;
Andreas Traczyk's avatar
Andreas Traczyk committed
116
    } else {
Andreas Traczyk's avatar
Andreas Traczyk committed
117 118 119 120 121 122 123 124 125 126 127 128
        auto videoRect = ui->videoFeed->rect();
        QPoint avatarLabelPos = ui->videoFeed->mapTo(this, videoRect.topLeft());
        flashOverlay_->setGeometry(
            avatarLabelPos.x(),
            avatarLabelPos.y(),
            videoRect.width(),
            videoRect.height()
        );
        flashOverlay_->show();
        flashAnimation_->start(QPropertyAnimation::KeepWhenStopped);

        QtConcurrent::run(
129
            [this] {
Andreas Traczyk's avatar
Andreas Traczyk committed
130
                Video::PreviewManager::instance().stopPreview();
Andreas Traczyk's avatar
Andreas Traczyk committed
131 132 133 134
                auto photo = Utils::cropImage(ui->videoFeed->takePhoto());
                auto avatar = photo.scaled(224, 224, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
                avatarPixmap_ = QPixmap::fromImage(avatar);
                ui->avatarLabel->setPixmap(QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width())));
135
                hasAvatar_ = true;
Andreas Traczyk's avatar
Andreas Traczyk committed
136
                emit photoTaken();
Andreas Traczyk's avatar
Andreas Traczyk committed
137 138
                stopBooth();
            });
Andreas Traczyk's avatar
Andreas Traczyk committed
139
    }
140
}
141

Andreas Traczyk's avatar
Andreas Traczyk committed
142 143 144 145 146 147 148 149 150 151
void
PhotoboothWidget::setAvatarPixmap(const QPixmap& avatarPixmap, bool default)
{
    ui->avatarLabel->setPixmap(avatarPixmap);
    stopBooth();
    if (default) {
        ui->takePhotoButton->setIcon(QIcon(":/images/icons/round-add_a_photo-24px.svg"));
    }
}

152 153 154 155 156 157 158 159 160 161 162
const QPixmap&
PhotoboothWidget::getAvatarPixmap()
{
    return avatarPixmap_;
}

bool
PhotoboothWidget::hasAvatar()
{
    return hasAvatar_;
}