From 463afd278a5256cae5902e56380e8c1f118e9a1c Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Fri, 8 Nov 2019 10:35:40 -0500
Subject: [PATCH] refactor: add generic rounded corner drawing function to
 remove redundant  code

Change-Id: I38a53c106f1f89b90b528b3a69ee49086c9a9d23
---
 popupdialog.cpp   | 15 ++++++++++++++-
 popupdialog.h     |  3 +++
 previewwidget.cpp | 27 +++++----------------------
 previewwidget.h   |  3 ++-
 stylesheet.css    |  6 ------
 utils.h           | 14 ++++++++++++++
 widgethelpers.cpp | 30 +++++++++++++-----------------
 widgethelpers.h   |  2 ++
 8 files changed, 53 insertions(+), 47 deletions(-)

diff --git a/popupdialog.cpp b/popupdialog.cpp
index d53d68b..ed38dd6 100644
--- a/popupdialog.cpp
+++ b/popupdialog.cpp
@@ -22,12 +22,14 @@
 #include "utils.h"
 
 #include <QMouseEvent>
+#include <QPainter>
 
 PopupDialog::PopupDialog(QWidget *parent,
                          QColor spikeColor,
                          SpikeLabelAlignment spikeAlignment) :
     QDialog(parent),
-    ui(new Ui::PopupDialog)
+    ui(new Ui::PopupDialog),
+    spikeColor_(spikeColor)
 {
     ui->setupUi(this);
 
@@ -79,3 +81,14 @@ PopupDialog::mousePressEvent(QMouseEvent* event)
     }
     QDialog::mousePressEvent(event);
 }
+
+void
+PopupDialog::paintEvent(QPaintEvent * event)
+{
+    QPainter painter(this);
+    painter.setRenderHint(QPainter::Antialiasing, true);
+
+    // draw rounded corner
+    Utils::fillRoundRectPath(painter, spikeColor_, ui->containerWidget->rect(), cornerRadius_);
+    QDialog::paintEvent(event);
+}
diff --git a/popupdialog.h b/popupdialog.h
index 64f903c..8283f8c 100644
--- a/popupdialog.h
+++ b/popupdialog.h
@@ -48,9 +48,12 @@ signals:
 
 protected:
     void mousePressEvent(QMouseEvent *event);
+    void paintEvent(QPaintEvent *event);
 
 private:
     Ui::PopupDialog *ui;
+    QColor spikeColor_;
+    constexpr static qreal cornerRadius_ = 10.0f;
 
     void setSpikeLabelAlignment(SpikeLabelAlignment spikeAlignment);
 };
diff --git a/previewwidget.cpp b/previewwidget.cpp
index 8d87630..3e2ac12 100644
--- a/previewwidget.cpp
+++ b/previewwidget.cpp
@@ -282,34 +282,17 @@ VideoRecordPreviewWidget::paintEvent(QPaintEvent* e)
 
         scaledPreview = previewImage->scaled(previewWidth, previewHeight, Qt::KeepAspectRatio);
         scaledPreviewImage_ = scaledPreview;
+        scaledPreviewImageXOffset_ = xOffset;
+        scaledPreviewImageYOffset_ = yOffset;
 
         // draw rounded corner image
-        QBrush brush(scaledPreview);
-        brush.setTransform(QTransform::fromTranslate(
-            this->rect().x() + xOffset,
-            this->rect().y() + yOffset));
-        QPainterPath previewPath;
-        previewPath.addRoundRect(this->rect(), cornerRadius_);
-        painter.fillPath(previewPath, brush);
+        Utils::fillRoundRectPath(painter, scaledPreview, rect(), cornerRadius_, xOffset, yOffset);
     } else {
         if (paintBackground_) {
-            paintBackground(&painter);
+            Utils::fillRoundRectPath(painter, QColor(Qt::black), rect(), cornerRadius_);
             scaledPreviewImage_ = QImage();
         } else if (drawLastFrame_) {
-            QBrush brush(scaledPreviewImage_);
-            brush.setTransform(QTransform::fromTranslate(this->rect().x(), this->rect().y()));
-            QPainterPath previewPath;
-            previewPath.addRoundRect(this->rect(), cornerRadius_);
-            painter.fillPath(previewPath, brush);
+            Utils::fillRoundRectPath(painter, scaledPreviewImage_, rect(), cornerRadius_, scaledPreviewImageXOffset_, scaledPreviewImageYOffset_);
         }
     }
 }
-
-void
-VideoRecordPreviewWidget::paintBackground(QPainter * painter)
-{
-    QBrush brush(Qt::black);
-    QPainterPath path;
-    path.addRoundRect(this->rect(), cornerRadius_);
-    painter->fillPath(path, brush);
-}
diff --git a/previewwidget.h b/previewwidget.h
index ad73ae2..0c9eb23 100644
--- a/previewwidget.h
+++ b/previewwidget.h
@@ -104,9 +104,10 @@ public:
 
 protected:
     void paintEvent(QPaintEvent* e) override;
-    void paintBackground(QPainter* painter) override;
 
 private:
+    int scaledPreviewImageXOffset_{ 0 };
+    int scaledPreviewImageYOffset_{ 0 };
     constexpr static qreal cornerRadius_ = 10.0f;
 
     QImage scaledPreviewImage_;
diff --git a/stylesheet.css b/stylesheet.css
index 28bcac9..d151342 100644
--- a/stylesheet.css
+++ b/stylesheet.css
@@ -829,12 +829,6 @@ QPushButton#panelButton_9:pressed, QPushButton#panelButton_hash:pressed, QPushBu
     background: #58b;
 }
 
-QWidget#containerWidget {
-    border-radius: 10px;
-    border: solid 1px transparent;
-    background-color: white;
-}
-
 QProgressBar#audioInputMeter {
     border: 0px solid #606060;
     background: lightgrey;
diff --git a/utils.h b/utils.h
index ca9b8f1..d8774a7 100644
--- a/utils.h
+++ b/utils.h
@@ -108,6 +108,20 @@ QImage scaleAndFrame(const QImage photo, const QSize& size = IMAGE_SIZE);
 QImage accountPhoto(const lrc::api::account::Info& accountInfo, const QSize& size = IMAGE_SIZE);
 QImage cropImage(const QImage& img);
 
+// rounded corner
+template<typename T>
+void
+fillRoundRectPath(QPainter& painter, const T& brushType, const QRect& rectToDraw, qreal cornerRadius, int xTransFormOffset = 0, int yTransFormOffset = 0)
+{
+    QBrush brush(brushType);
+    brush.setTransform(QTransform::fromTranslate(
+                       rectToDraw.x() + xTransFormOffset,
+                       rectToDraw.y() + yTransFormOffset));
+    QPainterPath painterPath;
+    painterPath.addRoundRect(rectToDraw, cornerRadius);
+    painter.fillPath(painterPath, brush);
+}
+
 // time
 QString formattedTime(int seconds);
 
diff --git a/widgethelpers.cpp b/widgethelpers.cpp
index 4ee6821..c3d0f36 100644
--- a/widgethelpers.cpp
+++ b/widgethelpers.cpp
@@ -18,6 +18,8 @@
 
 #include "widgethelpers.h"
 
+#include "utils.h"
+
 FadeOutable::FadeOutable(QWidget* parent)
     : QWidget(parent)
 {
@@ -162,16 +164,8 @@ VignetteWidget::paintEvent(QPaintEvent *event)
     gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
     gradient.setColorAt(1, Qt::transparent);
 
-    // draw rounded corner
-    if (drawRoundedCorner_) {
-        QBrush brush(gradient);
-        brush.setTransform(QTransform::fromTranslate(this->rect().x(), this->rect().y()));
-        QPainterPath previewPath;
-        previewPath.addRoundRect(this->rect(), cornerRadius_);
-        painter.fillPath(previewPath, brush);
-    } else {
-        painter.fillRect(rect(), gradient);
-    }
+    // draw upper rounded corner or just gradient itself
+    fillRoundRectPath(painter, gradient);
 
     // bottom
     gradient.setStart(0, rect().bottom() - height_);
@@ -180,15 +174,17 @@ VignetteWidget::paintEvent(QPaintEvent *event)
     gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
     gradient.setColorAt(1, QColor(0, 0, 0, 96));
 
-    // draw rounded corner
+    // draw bottom rounded corner or just gradient itself
+    fillRoundRectPath(painter, gradient);
+
+}
+
+void
+VignetteWidget::fillRoundRectPath(QPainter& painter, const QLinearGradient & gradient)
+{
     if (drawRoundedCorner_) {
-        QBrush brush(gradient);
-        brush.setTransform(QTransform::fromTranslate(this->rect().x(), this->rect().y()));
-        QPainterPath previewPath;
-        previewPath.addRoundRect(this->rect(), cornerRadius_);
-        painter.fillPath(previewPath, brush);
+        Utils::fillRoundRectPath(painter, gradient, rect(), cornerRadius_);
     } else {
         painter.fillRect(rect(), gradient);
     }
-
 }
diff --git a/widgethelpers.h b/widgethelpers.h
index 8c6587e..ba684e6 100644
--- a/widgethelpers.h
+++ b/widgethelpers.h
@@ -106,6 +106,8 @@ protected:
     void paintEvent(QPaintEvent *event) override;
 
 private:
+    void fillRoundRectPath(QPainter& painter, const QLinearGradient& gradient);
+
     FadeAnimation* fadeOutAnimation_;
     constexpr static qreal cornerRadius_ = 10.0f;
     quint64 height_{ 128 };
-- 
GitLab