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 @@
#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);
}
......@@ -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);
};
......@@ -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);
}
......@@ -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_;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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);
}
}
......@@ -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 };
......
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