Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 1 | /************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2019-2019 by Savoir-faire Linux * |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 3 | * 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 | |
| 19 | #include <QtMultimedia\QCameraInfo> |
| 20 | #include <QFileDialog> |
| 21 | #include <QAbstractScrollArea> |
| 22 | #include <QScreen> |
| 23 | #include <QWindow> |
| 24 | #include <QtMultimedia\QCameraImageCapture> |
| 25 | #include <QBuffer> |
| 26 | |
| 27 | #include "setavatardialog.h" |
| 28 | #include "ui_setavatardialog.h" |
| 29 | |
| 30 | #include "lrcinstance.h" |
| 31 | #include "accountlistmodel.h" |
| 32 | #include "currentaccountcombobox.h" |
| 33 | |
| 34 | SetAvatarDialog::SetAvatarDialog(QWidget* parent) |
| 35 | : QDialog(parent), |
| 36 | ui(new Ui::SetAvatarDialog), |
| 37 | videoWidget_(new QCameraViewfinder(this)), |
| 38 | camera_(new QCamera) |
| 39 | { |
| 40 | ui->setupUi(this); |
| 41 | |
| 42 | connect(ui->cancelButton, &QPushButton::clicked, [&]() { |
| 43 | done(0); |
| 44 | } |
| 45 | ); |
| 46 | |
| 47 | connect(ui->saveButton, &QPushButton::clicked, this, &SetAvatarDialog::saveAvatar); |
| 48 | |
| 49 | connect(ui->selectFileButton, &QPushButton::clicked, this, &SetAvatarDialog::openFileManager); |
| 50 | |
| 51 | connect(ui->takePictureButton, &QPushButton::clicked, this, &SetAvatarDialog::captureImage); |
| 52 | |
| 53 | connect(ui->cameraButton, &QPushButton::clicked, this, &SetAvatarDialog::pictureMode); |
| 54 | |
| 55 | ui->graphicsView->setAvatarSize(avatarSize_); |
| 56 | |
| 57 | videoWidget_->setGeometry(ui->graphicsView->x(), ui->graphicsView->y(), |
| 58 | ui->graphicsView->width(), ui->graphicsView->height()); |
| 59 | |
| 60 | pictureMode(); |
| 61 | startCamera(); |
| 62 | |
| 63 | ui->horizontalLayout_2->update(); |
| 64 | } |
| 65 | |
| 66 | SetAvatarDialog::~SetAvatarDialog() |
| 67 | { |
| 68 | disconnect(this); |
| 69 | delete mediaPlayer_; |
| 70 | delete videoWidget_; |
| 71 | delete imageCapture_; |
| 72 | delete camera_; |
| 73 | delete ui; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | SetAvatarDialog::startCamera() |
| 78 | { |
| 79 | imageCapture_ = new QCameraImageCapture(camera_, this); |
| 80 | camera_->setCaptureMode(QCamera::CaptureViewfinder); |
| 81 | connect(imageCapture_, SIGNAL(imageSaved(int, const QString&)), |
| 82 | this, SLOT(imageCaptureSlot(int, const QString&))); |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | SetAvatarDialog::checkCamAvailable() |
| 87 | { |
| 88 | if (QCameraInfo::availableCameras().count() > 0) { |
| 89 | return true; |
| 90 | } |
| 91 | else { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // no support yet for back-facing camera [todo]; no support for portrait mode [todo] |
| 97 | void |
| 98 | SetAvatarDialog::pictureMode() |
| 99 | { |
| 100 | if (checkCamAvailable()) { |
| 101 | camera_ = new QCamera(QCamera::FrontFace); |
| 102 | camera_->setViewfinder(videoWidget_); |
| 103 | |
| 104 | ui->graphicsView->hide(); |
| 105 | videoWidget_->show(); |
| 106 | |
| 107 | camera_->start(); |
| 108 | |
| 109 | ui->saveButton->hide(); |
| 110 | ui->cameraButton->hide(); |
| 111 | |
| 112 | ui->takePictureButton->show(); |
| 113 | ui->selectFileButton->show(); |
| 114 | } |
| 115 | else { |
| 116 | openFileManager(); // no camera detected so open file manager |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | SetAvatarDialog::editMode() |
| 122 | { |
| 123 | camera_->unlock(); |
| 124 | camera_->stop(); |
| 125 | |
| 126 | ui->takePictureButton->hide(); |
| 127 | |
| 128 | ui->saveButton->show(); |
| 129 | ui->cameraButton->show(); |
| 130 | ui->selectFileButton->show(); |
| 131 | |
| 132 | videoWidget_->hide(); |
| 133 | ui->graphicsView->show(); |
| 134 | configureAvatarScene(rawPixmap_); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | SetAvatarDialog::openFileManager() |
| 139 | { |
| 140 | rawPixmap_.load(QFileDialog::getOpenFileName(this, |
| 141 | tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"))); |
| 142 | editMode(); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | SetAvatarDialog::configureAvatarScene(const QPixmap& pixMap) |
| 147 | { |
| 148 | if (!pixMap.isNull()) { |
| 149 | const int margin = 8; |
| 150 | QPen pen; |
| 151 | pen.setStyle(Qt::NoPen); |
| 152 | |
| 153 | graphicsScene_.clear(); |
| 154 | |
| 155 | graphicsScene_.addPixmap(pixMap); |
| 156 | graphicsScene_.addRect(-(pixMap.width()*margin - avatarSize_) / 2, |
| 157 | -(pixMap.height() * margin - avatarSize_) / 2, pixMap.width() * margin, pixMap.height() * margin, pen, QBrush()); |
| 158 | ui->graphicsView->setScene(&graphicsScene_); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | SetAvatarDialog::captureImage() |
| 164 | { |
| 165 | startCamera(); |
| 166 | |
| 167 | camera_->setCaptureMode(QCamera::CaptureStillImage); |
| 168 | imageCapture_->setCaptureDestination(QCameraImageCapture::CaptureToFile); |
| 169 | |
| 170 | if (imageCapture_->isCaptureDestinationSupported(QCameraImageCapture::CaptureToFile)) { |
| 171 | camera_->searchAndLock(); |
| 172 | camera_->start(); |
| 173 | imageCapture_->capture(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // save avatar image and exit SetAvatarDialog |
| 178 | void |
| 179 | SetAvatarDialog::saveAvatar() |
| 180 | { |
| 181 | avatarSize_ *= 2; |
| 182 | |
| 183 | QRect avatarRect((ui->graphicsView->width() - avatarSize_)/2 + ui->graphicsView->x(), (ui->graphicsView->height() - avatarSize_)/2 + ui->graphicsView->y(), |
| 184 | avatarSize_, avatarSize_); |
| 185 | |
| 186 | QRegion avatarRegion(avatarRect.x() - ui->graphicsView->x(), avatarRect.y() - ui->graphicsView->y(), |
| 187 | avatarSize_, avatarSize_, QRegion::Ellipse); |
| 188 | ui->graphicsView->setMask(avatarRegion); |
| 189 | |
| 190 | finalAvatarPixmap_ = grab(avatarRect); |
| 191 | |
| 192 | done(0); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | SetAvatarDialog::done(int exCode) |
| 197 | { |
| 198 | QDialog::done(exCode); |
| 199 | if (!finalAvatarPixmap_.isNull()) { |
| 200 | QByteArray ba; |
| 201 | QBuffer bu(&ba); |
| 202 | finalAvatarPixmap_.save(&bu, "PNG"); |
| 203 | emit pixmapSignal(ba.toBase64().toStdString()); |
| 204 | } |
| 205 | else { |
| 206 | emit pixmapSignal(std::string()); |
| 207 | } |
| 208 | camera_->unlock(); |
| 209 | camera_->stop(); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | SetAvatarDialog::imageCaptureSlot(int useless, const QString& text) |
| 214 | { |
| 215 | Q_UNUSED(useless); |
| 216 | |
| 217 | rawPixmap_ = rawPixmap_.fromImage(QImage(text)); |
| 218 | |
| 219 | if (!rawPixmap_.isNull()){ |
| 220 | editMode(); |
| 221 | } |
| 222 | QFile file(text); |
| 223 | file.remove(); // erase file |
| 224 | } |