From e52278b698d4652ba9fb10e70654d90492f53a46 Mon Sep 17 00:00:00 2001
From: Yang Wang <yang.wang@savoirfairelinux.com>
Date: Mon, 12 Aug 2019 13:11:57 -0400
Subject: [PATCH] videoview: fix the preview reposition problems

Gitlab: #495
Change-Id: I29cd215147b673e82b692b194ef79099f58cc01a
---
 videoview.cpp   | 18 +++++++++++++-----
 videowidget.cpp | 14 ++++++++++----
 videowidget.h   |  8 +++++---
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/videoview.cpp b/videoview.cpp
index f15af67..66bbc33 100644
--- a/videoview.cpp
+++ b/videoview.cpp
@@ -67,6 +67,7 @@ VideoView::VideoView(QWidget* parent) :
         this, SLOT(showContextMenu(const QPoint&)));
     connect(overlay_, &VideoOverlay::setChatVisibility, [=](bool visible) {
         emit this->setChatVisibility(visible);
+    connect(this, SIGNAL(toggleFullScreenClicked()), ui->videoWidget, SLOT(slotToggleFullScreenClicked()));
     });
 
 }
@@ -81,6 +82,7 @@ VideoView::~VideoView()
 void
 VideoView::resizeEvent(QResizeEvent* event)
 {
+    int marginWidth = ui->videoWidget->getPreviewMargin();
     QRect& previewRect = ui->videoWidget->getPreviewRect();
     int deltaW = event->size().width() - event->oldSize().width();
     int deltaH = event->size().height() - event->oldSize().height();
@@ -102,16 +104,20 @@ VideoView::resizeEvent(QResizeEvent* event)
     }
 
     if (previewRect.left() <= 0)
-        previewRect.moveLeft(1);
+        previewRect.moveLeft(marginWidth);
+    previewRect.moveRight(width() - marginWidth);
 
     if (previewRect.right() >= width())
-        previewRect.moveRight(width() - 1);
+        previewRect.moveRight(width() - marginWidth);
 
     if (previewRect.top() <= 0)
-        previewRect.moveTop(1);
+        previewRect.moveTop(marginWidth);
+    previewRect.moveBottom(height() - marginWidth);
 
     if (previewRect.bottom() >= height())
-        previewRect.moveBottom(height() - 1);
+        previewRect.moveBottom(height() - marginWidth);
+
+    ui->videoWidget->resetPreview();
 
     overlay_->resize(this->size());
     overlay_->show();
@@ -164,8 +170,10 @@ VideoView::slotCallStatusChanged(const std::string& callId)
         }
         return;
     }
-    default:
+    case Status::ENDED:
         emit closing(call.id);
+    default:
+        //emit closing(call.id);
         break;
     }
     QObject::disconnect(timerConnection_);
diff --git a/videowidget.cpp b/videowidget.cpp
index 88ebab9..a7ccf79 100644
--- a/videowidget.cpp
+++ b/videowidget.cpp
@@ -109,6 +109,12 @@ VideoWidget::renderFrame(const std::string& id)
     }
 }
 
+void
+VideoWidget::slotToggleFullScreenClicked()
+{
+    this->update();
+}
+
 void
 VideoWidget::paintEvent(QPaintEvent* e)
 {
@@ -154,9 +160,9 @@ VideoWidget::paintEvent(QPaintEvent* e)
             hasFrame_ = true;
         }
         if (previewImage_) {
-            if(resetPreview_) {
-                auto previewHeight = fullPreview_ ? height() : height() / 4;
-                auto previewWidth = fullPreview_  ? width() : width() / 4;
+            if (resetPreview_) {
+                auto previewHeight = fullPreview_ ? height() : height() / 6;
+                auto previewWidth = fullPreview_ ? width() : width() / 6;
                 QImage scaledPreview;
                 if (photoMode_)
                     scaledPreview = Utils::getCirclePhoto(*previewImage_, previewHeight);
@@ -164,8 +170,8 @@ VideoWidget::paintEvent(QPaintEvent* e)
                     scaledPreview = previewImage_->scaled(previewWidth, previewHeight, Qt::KeepAspectRatio);
                 auto xDiff = (previewWidth - scaledPreview.width()) / 2;
                 auto yDiff = (previewHeight - scaledPreview.height()) / 2;
-                auto yPos = fullPreview_ ? yDiff : height() - previewHeight - previewMargin_;
                 auto xPos = fullPreview_ ? xDiff : width() - scaledPreview.width() - previewMargin_;
+                auto yPos = fullPreview_ ? yDiff : height() - scaledPreview.height() - previewMargin_;
                 previewGeometry_.setRect(xPos, yPos, scaledPreview.width(), scaledPreview.height());
                 painter.drawImage(previewGeometry_, scaledPreview);
                 resetPreview_ = false;
diff --git a/videowidget.h b/videowidget.h
index 9c1364b..4bb4ad6 100644
--- a/videowidget.h
+++ b/videowidget.h
@@ -43,11 +43,14 @@ public:
     inline void setResetPreview(bool reset) { resetPreview_ = reset; hasFrame_=false; }
     void setPhotoMode(bool isPhotoMode);
     QImage takePhoto();
+    int getPreviewMargin(){ return previewMargin_; }
+    void resetPreview() { resetPreview_ = true; }
 
 protected:
     void paintEvent(QPaintEvent* e);
 
 public slots:
+    void slotToggleFullScreenClicked();
     void slotRendererStarted(const std::string& id);
     void renderFrame(const std::string& id);
     inline QRect& getPreviewRect(){ return previewGeometry_; }
@@ -57,6 +60,8 @@ private:
         QMetaObject::Connection started, stopped, updated;
     } rendererConnections_;
 
+    void paintBackgroundColor(QPainter* painter, QColor color);
+
     video::Renderer* previewRenderer_;
     video::Frame previewFrame_;
     std::unique_ptr<QImage> previewImage_;
@@ -77,7 +82,4 @@ private:
     bool hasFrame_ = false;
 
     constexpr static int previewMargin_ = 15;
-
-private:
-    void paintBackgroundColor(QPainter* painter, QColor color);
 };
-- 
GitLab