Commit 463afd27 authored by Ming Rui Zhang's avatar Ming Rui Zhang

refactor: add generic rounded corner drawing function to remove redundant code

Change-Id: I38a53c106f1f89b90b528b3a69ee49086c9a9d23
parent b9ec6278
...@@ -22,12 +22,14 @@ ...@@ -22,12 +22,14 @@
#include "utils.h" #include "utils.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QPainter>
PopupDialog::PopupDialog(QWidget *parent, PopupDialog::PopupDialog(QWidget *parent,
QColor spikeColor, QColor spikeColor,
SpikeLabelAlignment spikeAlignment) : SpikeLabelAlignment spikeAlignment) :
QDialog(parent), QDialog(parent),
ui(new Ui::PopupDialog) ui(new Ui::PopupDialog),
spikeColor_(spikeColor)
{ {
ui->setupUi(this); ui->setupUi(this);
...@@ -79,3 +81,14 @@ PopupDialog::mousePressEvent(QMouseEvent* event) ...@@ -79,3 +81,14 @@ PopupDialog::mousePressEvent(QMouseEvent* event)
} }
QDialog::mousePressEvent(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);
}
...@@ -48,9 +48,12 @@ signals: ...@@ -48,9 +48,12 @@ signals:
protected: protected:
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private: private:
Ui::PopupDialog *ui; Ui::PopupDialog *ui;
QColor spikeColor_;
constexpr static qreal cornerRadius_ = 10.0f;
void setSpikeLabelAlignment(SpikeLabelAlignment spikeAlignment); void setSpikeLabelAlignment(SpikeLabelAlignment spikeAlignment);
}; };
...@@ -282,34 +282,17 @@ VideoRecordPreviewWidget::paintEvent(QPaintEvent* e) ...@@ -282,34 +282,17 @@ VideoRecordPreviewWidget::paintEvent(QPaintEvent* e)
scaledPreview = previewImage->scaled(previewWidth, previewHeight, Qt::KeepAspectRatio); scaledPreview = previewImage->scaled(previewWidth, previewHeight, Qt::KeepAspectRatio);
scaledPreviewImage_ = scaledPreview; scaledPreviewImage_ = scaledPreview;
scaledPreviewImageXOffset_ = xOffset;
scaledPreviewImageYOffset_ = yOffset;
// draw rounded corner image // draw rounded corner image
QBrush brush(scaledPreview); Utils::fillRoundRectPath(painter, scaledPreview, rect(), cornerRadius_, xOffset, yOffset);
brush.setTransform(QTransform::fromTranslate(
this->rect().x() + xOffset,
this->rect().y() + yOffset));
QPainterPath previewPath;
previewPath.addRoundRect(this->rect(), cornerRadius_);
painter.fillPath(previewPath, brush);
} else { } else {
if (paintBackground_) { if (paintBackground_) {
paintBackground(&painter); Utils::fillRoundRectPath(painter, QColor(Qt::black), rect(), cornerRadius_);
scaledPreviewImage_ = QImage(); scaledPreviewImage_ = QImage();
} else if (drawLastFrame_) { } else if (drawLastFrame_) {
QBrush brush(scaledPreviewImage_); Utils::fillRoundRectPath(painter, scaledPreviewImage_, rect(), cornerRadius_, scaledPreviewImageXOffset_, scaledPreviewImageYOffset_);
brush.setTransform(QTransform::fromTranslate(this->rect().x(), this->rect().y()));
QPainterPath previewPath;
previewPath.addRoundRect(this->rect(), cornerRadius_);
painter.fillPath(previewPath, brush);
} }
} }
} }
void
VideoRecordPreviewWidget::paintBackground(QPainter * painter)
{
QBrush brush(Qt::black);
QPainterPath path;
path.addRoundRect(this->rect(), cornerRadius_);
painter->fillPath(path, brush);
}
...@@ -104,9 +104,10 @@ public: ...@@ -104,9 +104,10 @@ public:
protected: protected:
void paintEvent(QPaintEvent* e) override; void paintEvent(QPaintEvent* e) override;
void paintBackground(QPainter* painter) override;
private: private:
int scaledPreviewImageXOffset_{ 0 };
int scaledPreviewImageYOffset_{ 0 };
constexpr static qreal cornerRadius_ = 10.0f; constexpr static qreal cornerRadius_ = 10.0f;
QImage scaledPreviewImage_; QImage scaledPreviewImage_;
......
...@@ -829,12 +829,6 @@ QPushButton#panelButton_9:pressed, QPushButton#panelButton_hash:pressed, QPushBu ...@@ -829,12 +829,6 @@ QPushButton#panelButton_9:pressed, QPushButton#panelButton_hash:pressed, QPushBu
background: #58b; background: #58b;
} }
QWidget#containerWidget {
border-radius: 10px;
border: solid 1px transparent;
background-color: white;
}
QProgressBar#audioInputMeter { QProgressBar#audioInputMeter {
border: 0px solid #606060; border: 0px solid #606060;
background: lightgrey; background: lightgrey;
......
...@@ -108,6 +108,20 @@ QImage scaleAndFrame(const QImage photo, const QSize& size = IMAGE_SIZE); ...@@ -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 accountPhoto(const lrc::api::account::Info& accountInfo, const QSize& size = IMAGE_SIZE);
QImage cropImage(const QImage& img); 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 // time
QString formattedTime(int seconds); QString formattedTime(int seconds);
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#include "widgethelpers.h" #include "widgethelpers.h"
#include "utils.h"
FadeOutable::FadeOutable(QWidget* parent) FadeOutable::FadeOutable(QWidget* parent)
: QWidget(parent) : QWidget(parent)
{ {
...@@ -162,16 +164,8 @@ VignetteWidget::paintEvent(QPaintEvent *event) ...@@ -162,16 +164,8 @@ VignetteWidget::paintEvent(QPaintEvent *event)
gradient.setColorAt(0.5, QColor(0, 0, 0, 32)); gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
gradient.setColorAt(1, Qt::transparent); gradient.setColorAt(1, Qt::transparent);
// draw rounded corner // draw upper rounded corner or just gradient itself
if (drawRoundedCorner_) { fillRoundRectPath(painter, gradient);
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);
}
// bottom // bottom
gradient.setStart(0, rect().bottom() - height_); gradient.setStart(0, rect().bottom() - height_);
...@@ -180,15 +174,17 @@ VignetteWidget::paintEvent(QPaintEvent *event) ...@@ -180,15 +174,17 @@ VignetteWidget::paintEvent(QPaintEvent *event)
gradient.setColorAt(0.5, QColor(0, 0, 0, 32)); gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
gradient.setColorAt(1, QColor(0, 0, 0, 96)); 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_) { if (drawRoundedCorner_) {
QBrush brush(gradient); Utils::fillRoundRectPath(painter, gradient, rect(), cornerRadius_);
brush.setTransform(QTransform::fromTranslate(this->rect().x(), this->rect().y()));
QPainterPath previewPath;
previewPath.addRoundRect(this->rect(), cornerRadius_);
painter.fillPath(previewPath, brush);
} else { } else {
painter.fillRect(rect(), gradient); painter.fillRect(rect(), gradient);
} }
} }
...@@ -106,6 +106,8 @@ protected: ...@@ -106,6 +106,8 @@ protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
private: private:
void fillRoundRectPath(QPainter& painter, const QLinearGradient& gradient);
FadeAnimation* fadeOutAnimation_; FadeAnimation* fadeOutAnimation_;
constexpr static qreal cornerRadius_ = 10.0f; constexpr static qreal cornerRadius_ = 10.0f;
quint64 height_{ 128 }; quint64 height_{ 128 };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment