change account creation wizard photobooth

the first page of new ring account creation was provided
a photobooth to set a profile photo. However this functionality
was only accessible by clicking on the placeholder avatar,
making it a practically hidden functionality.
this patch makes the photobooth appear directly on this page.

Change-Id: I1273efe3bd70d9db78e09e0592c5023421ffcd5a
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
diff --git a/RingWinClient.pro b/RingWinClient.pro
index 15fae25..81d5306 100644
--- a/RingWinClient.pro
+++ b/RingWinClient.pro
@@ -79,7 +79,8 @@
     quickactcontactrequestwidget.cpp \
     contactrequestlistwidget.cpp \
     deleteaccountdialog.cpp \
-    bannedcontactswidget.cpp
+    bannedcontactswidget.cpp \
+    photoboothwidget.cpp
 
 HEADERS  += mainwindow.h \
     callwidget.h \
@@ -122,7 +123,8 @@
     quickactcontactrequestwidget.h \
     contactrequestlistwidget.h \
     deleteaccountdialog.h \
-    bannedcontactswidget.h
+    bannedcontactswidget.h \
+    photoboothwidget.h
 
 contains(DEFINES, URI_PROTOCOL) {
  HEADERS += shmclient.h
@@ -151,7 +153,8 @@
     contactrequestwidget.ui \
     quickactcontactrequestwidget.ui \
     deleteaccountdialog.ui \
-    bannedcontactswidget.ui
+    bannedcontactswidget.ui \
+    photoboothwidget.ui
 
 win32: LIBS += -lole32 -luuid -lshlwapi
 LIBS += -lqrencode
diff --git a/photoboothwidget.cpp b/photoboothwidget.cpp
new file mode 100644
index 0000000..537f544
--- /dev/null
+++ b/photoboothwidget.cpp
@@ -0,0 +1,86 @@
+/***************************************************************************
+ * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
+ * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
+ *                                                                         *
+ * 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>
+
+#include "video/previewmanager.h"
+
+PhotoboothWidget::PhotoboothWidget(QWidget *parent) :
+    QWidget(parent),
+    fileName_(QStandardPaths::standardLocations(QStandardPaths::TempLocation).first()
+              + QStringLiteral("profile.png")),
+    ui(new Ui::PhotoboothWidget)
+{
+    ui->setupUi(this);
+    ui->videoFeed->setIsFullPreview(true);
+    ui->videoFeed->setPhotoMode(true);
+    startBooth();
+}
+
+PhotoboothWidget::~PhotoboothWidget()
+{
+    Video::PreviewManager::instance().stopPreview();
+    delete ui;
+}
+
+void PhotoboothWidget::startBooth()
+{
+    // // // //
+    // stop (or start before) to give Preview manager some time to start
+    // TODO go modify the daemon to ensure starting upon calling videomanager::startCamera
+    Video::PreviewManager::instance().stopPreview();
+    // // // //
+
+    Video::PreviewManager::instance().startPreview();
+    ui->videoFeed->show();
+}
+
+void PhotoboothWidget::stopBooth()
+{
+    Video::PreviewManager::instance().stopPreview();
+    hide();
+}
+
+void
+PhotoboothWidget::on_importButton_clicked()
+{
+    fileName_ = QFileDialog::getOpenFileName(this, tr("Choose File"),
+                                            "",
+                                            tr("Files (*)"));
+    if (fileName_.isEmpty())
+        fileName_ = QStandardPaths::standardLocations(
+                    QStandardPaths::TempLocation).first()
+                + QStringLiteral("profile.png");
+    else {
+        Video::PreviewManager::instance().stopPreview();
+    }
+    emit photoTaken(fileName_);
+}
+
+void
+PhotoboothWidget::on_takePhotoButton_clicked()
+{
+    auto photo = ui->videoFeed->takePhoto();
+    Video::PreviewManager::instance().stopPreview();
+    photo.save(fileName_);
+    emit photoTaken(fileName_);
+}
diff --git a/photoboothwidget.h b/photoboothwidget.h
new file mode 100644
index 0000000..c5020a2
--- /dev/null
+++ b/photoboothwidget.h
@@ -0,0 +1,50 @@
+/***************************************************************************
+ * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
+ * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
+ *                                                                         *
+ * 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/>.   *
+ **************************************************************************/
+
+#ifndef PHOTOBOOTHWIDGET_H
+#define PHOTOBOOTHWIDGET_H
+
+#include <QWidget>
+
+namespace Ui {
+class PhotoboothWidget;
+}
+
+class PhotoboothWidget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit PhotoboothWidget(QWidget *parent = 0);
+    ~PhotoboothWidget();
+    void startBooth();
+    void stopBooth();
+
+private slots:
+    void on_importButton_clicked();
+    void on_takePhotoButton_clicked();
+
+private:
+    QString fileName_;
+    Ui::PhotoboothWidget *ui;
+
+signals:
+    void photoTaken(QString fileName);
+};
+
+#endif // PHOTOBOOTHWIDGET_H
diff --git a/photoboothwidget.ui b/photoboothwidget.ui
new file mode 100644
index 0000000..ddcd8a4
--- /dev/null
+++ b/photoboothwidget.ui
@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>PhotoboothWidget</class>
+ <widget class="QWidget" name="PhotoboothWidget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>558</width>
+    <height>458</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout" stretch="9,0">
+   <item>
+    <widget class="VideoWidget" name="videoFeed" native="true">
+     <property name="autoFillBackground">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <property name="topMargin">
+      <number>10</number>
+     </property>
+     <item>
+      <spacer name="horizontalSpacer_3">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item>
+      <widget class="QPushButton" name="takePhotoButton">
+       <property name="enabled">
+        <bool>true</bool>
+       </property>
+       <property name="toolTip">
+        <string>Take photo</string>
+       </property>
+       <property name="text">
+        <string/>
+       </property>
+       <property name="icon">
+        <iconset resource="ressources.qrc">
+         <normaloff>:/images/icons/ic_photo_camera_white_24dp_2x.png</normaloff>:/images/icons/ic_photo_camera_white_24dp_2x.png</iconset>
+       </property>
+       <property name="iconSize">
+        <size>
+         <width>36</width>
+         <height>36</height>
+        </size>
+       </property>
+      </widget>
+     </item>
+     <item alignment="Qt::AlignHCenter">
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>or</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="importButton">
+       <property name="minimumSize">
+        <size>
+         <width>60</width>
+         <height>30</height>
+        </size>
+       </property>
+       <property name="toolTip">
+        <string>Import photo</string>
+       </property>
+       <property name="text">
+        <string>Import</string>
+       </property>
+       <property name="iconSize">
+        <size>
+         <width>16</width>
+         <height>30</height>
+        </size>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <spacer name="horizontalSpacer_4">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <customwidgets>
+  <customwidget>
+   <class>VideoWidget</class>
+   <extends>QWidget</extends>
+   <header>videowidget.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
+ <resources>
+  <include location="ressources.qrc"/>
+ </resources>
+ <connections/>
+</ui>
diff --git a/wizarddialog.cpp b/wizarddialog.cpp
index cc2f17c..467bac7 100644
--- a/wizarddialog.cpp
+++ b/wizarddialog.cpp
@@ -28,9 +28,9 @@
 #include "account.h"
 #include "profilemodel.h"
 #include "profile.h"
+#include "namedirectory.h"
 
 #include "utils.h"
-#include "photoboothdialog.h"
 
 const QString DEFAULT_RING_ACCT_ALIAS = QObject::tr("Ring account", "Default alias for new Ring account");
 
@@ -64,7 +64,7 @@
         ui->usernameEdit->setEnabled(false);
         ui->usernameEdit->setText(toBeMigrated->displayName());
         ui->previousButton->hide();
-        ui->avatarButton->hide();
+        ui->photoBooth->hide();
         ui->pinEdit->hide();
         ui->usernameLabel->setText(tr("Your account needs to be migrated. Choose a password."));
     } else
@@ -76,6 +76,8 @@
 
     nameLookupTimer_.setSingleShot(true);
     connect(&nameLookupTimer_, QTimer::timeout, this, WizardDialog::timeoutNameLookupTimer);
+    connect(ui->photoBooth, &PhotoboothWidget::photoTaken, this, WizardDialog::on_photoTaken);
+    ui->avatarLabel->hide();
 }
 
 WizardDialog::~WizardDialog()
@@ -189,16 +191,14 @@
 }
 
 void
-WizardDialog::on_avatarButton_clicked()
+WizardDialog::on_photoTaken(QString fileName)
 {
-    PhotoBoothDialog dlg;
-    dlg.exec();
-    if (dlg.result() == QDialog::Accepted) {
-        auto image = QImage(dlg.fileName_);
-        auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
-        ProfileModel::instance().selectedProfile()->person()->setPhoto(avatar);
-        ui->avatarButton->setIcon(QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarButton->width())));
-    }
+    auto image = QImage(fileName);
+    auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
+    ProfileModel::instance().selectedProfile()->person()->setPhoto(avatar);
+    ui->avatarLabel->setPixmap(QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarLabel->width())));
+    ui->photoBooth->stopBooth();
+    ui->avatarLabel->show();
 }
 
 void
@@ -219,13 +219,15 @@
     if (existingAccount) { // If user want to add a device
         ui->accountLabel->setText(tr("Add a device"));
         ui->stackedWidget->setCurrentWidget(ui->explanationPage);
+        ui->photoBooth->hide();
     } else { // If user want to create a new account
         ui->accountLabel->setText(tr("Create your account"));
         ui->stackedWidget->setCurrentWidget(ui->profilePage);
+        ui->photoBooth->startBooth();
+        ui->photoBooth->show();
     }
     ui->navBarWidget->show();
-
-    ui->avatarButton->setHidden(existingAccount);
+    ui->avatarLabel->setHidden(true);
     ui->usernameLabel->setHidden(existingAccount);
     ui->usernameEdit->setHidden(existingAccount);
     ui->signUpCheckbox->setHidden(existingAccount);
diff --git a/wizarddialog.h b/wizarddialog.h
index 917685d..e2984e1 100644
--- a/wizarddialog.h
+++ b/wizarddialog.h
@@ -52,7 +52,6 @@
 
 //UI Slots
 private slots:
-    void on_avatarButton_clicked();
     void on_existingPushButton_clicked();
     void on_newAccountButton_clicked();
     void on_nextButton_clicked();
@@ -65,6 +64,7 @@
     void handle_registeredNameFound(Account *account, NameDirectory::LookupStatus status, const QString& address, const QString& name);
     void handle_nameRegistrationEnded(NameDirectory::RegisterNameStatus status, const QString& name);
     void timeoutNameLookupTimer();
+    void on_photoTaken(QString fileName);
 
 private:
     Ui::WizardDialog* ui;
diff --git a/wizarddialog.ui b/wizarddialog.ui
index 80e67ac..543b30d 100644
--- a/wizarddialog.ui
+++ b/wizarddialog.ui
@@ -222,40 +222,60 @@
         </spacer>
        </item>
        <item alignment="Qt::AlignHCenter">
-        <widget class="QPushButton" name="avatarButton">
+        <widget class="PhotoboothWidget" name="photoBooth" native="true">
          <property name="sizePolicy">
-          <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+          <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="minimumSize">
           <size>
+           <width>300</width>
+           <height>300</height>
+          </size>
+         </property>
+        </widget>
+       </item>
+       <item alignment="Qt::AlignHCenter">
+        <widget class="QLabel" name="avatarLabel">
+         <property name="enabled">
+          <bool>true</bool>
+         </property>
+         <property name="minimumSize">
+          <size>
            <width>100</width>
            <height>100</height>
           </size>
          </property>
-         <property name="maximumSize">
-          <size>
-           <width>100</width>
-           <height>100</height>
-          </size>
+         <property name="font">
+          <font>
+           <kerning>true</kerning>
+          </font>
+         </property>
+         <property name="mouseTracking">
+          <bool>false</bool>
+         </property>
+         <property name="autoFillBackground">
+          <bool>false</bool>
          </property>
          <property name="text">
           <string/>
          </property>
-         <property name="icon">
-          <iconset resource="ressources.qrc">
-           <normaloff>:/images/user/btn-default-userpic.svg</normaloff>:/images/user/btn-default-userpic.svg</iconset>
+         <property name="textFormat">
+          <enum>Qt::AutoText</enum>
          </property>
-         <property name="iconSize">
-          <size>
-           <width>100</width>
-           <height>100</height>
-          </size>
+         <property name="pixmap">
+          <pixmap resource="ressources.qrc">:/images/user/btn-default-userpic.svg</pixmap>
          </property>
-         <property name="flat">
-          <bool>true</bool>
+         <property name="scaledContents">
+          <bool>false</bool>
+         </property>
+         <property name="openExternalLinks">
+          <bool>false</bool>
+         </property>
+         <property name="textInteractionFlags">
+          <set>Qt::NoTextInteraction</set>
          </property>
         </widget>
        </item>
@@ -684,6 +704,14 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>PhotoboothWidget</class>
+   <extends>QWidget</extends>
+   <header>photoboothwidget.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources>
   <include location="ressources.qrc"/>
  </resources>