Add the ability to set the frame thickness

And some other minor tweaks.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2021-03-08 14:41:42 +08:00
parent f70158a276
commit 70b257adb6
6 changed files with 115 additions and 30 deletions

View File

@ -96,24 +96,52 @@ QColor QtAcrylicEffectHelper::getFrameColor() const
return m_frameColor;
}
qreal QtAcrylicEffectHelper::getFrameThickness() const
{
return m_frameThickness;
}
void QtAcrylicEffectHelper::setTintColor(const QColor &value)
{
m_tintColor = value;
if (!value.isValid()) {
qWarning() << value << "is not a valid color.";
return;
}
if (m_tintColor != value) {
m_tintColor = value;
}
}
void QtAcrylicEffectHelper::setTintOpacity(qreal value)
void QtAcrylicEffectHelper::setTintOpacity(const qreal value)
{
m_tintOpacity = value;
if (m_tintOpacity != value) {
m_tintOpacity = value;
}
}
void QtAcrylicEffectHelper::setNoiseOpacity(qreal value)
void QtAcrylicEffectHelper::setNoiseOpacity(const qreal value)
{
m_noiseOpacity = value;
if (m_noiseOpacity != value) {
m_noiseOpacity = value;
}
}
void QtAcrylicEffectHelper::setFrameColor(const QColor &value)
{
m_frameColor = value;
if (!value.isValid()) {
qWarning() << value << "is not a valid color.";
return;
}
if (m_frameColor != value) {
m_frameColor = value;
}
}
void QtAcrylicEffectHelper::setFrameThickness(const qreal value)
{
if (m_frameThickness != value) {
m_frameThickness = value;
}
}
QtAcrylicEffectHelper::~QtAcrylicEffectHelper() = default;
@ -122,7 +150,11 @@ void QtAcrylicEffectHelper::paintWindowBackground(QPainter *painter, const QRegi
{
Q_ASSERT(painter);
Q_ASSERT(!clip.isEmpty());
if (!painter || clip.isEmpty() || !m_window) {
if (!painter || clip.isEmpty()) {
return;
}
if (!m_window) {
qWarning() << "m_window is null, forgot to call \"QtAcrylicEffectHelper::install()\"?";
return;
}
painter->save();
@ -134,7 +166,11 @@ void QtAcrylicEffectHelper::paintWindowBackground(QPainter *painter, const QRect
{
Q_ASSERT(painter);
Q_ASSERT(rect.isValid());
if (!painter || !rect.isValid() || !m_window) {
if (!painter || !rect.isValid()) {
return;
}
if (!m_window) {
qWarning() << "m_window is null, forgot to call \"QtAcrylicEffectHelper::install()\"?";
return;
}
painter->save();
@ -146,7 +182,11 @@ void QtAcrylicEffectHelper::paintBackground(QPainter *painter, const QRect &rect
{
Q_ASSERT(painter);
Q_ASSERT(rect.isValid());
if (!painter || !rect.isValid() || !m_window) {
if (!painter || !rect.isValid()) {
return;
}
if (!m_window) {
qWarning() << "m_window is null, forgot to call \"QtAcrylicEffectHelper::install()\"?";
return;
}
if (Utilities::isAcrylicEffectSupported()) {
@ -168,7 +208,11 @@ void QtAcrylicEffectHelper::paintBackground(QPainter *painter, const QRect &rect
void QtAcrylicEffectHelper::paintWindowFrame(QPainter *painter, const QRect &rect)
{
Q_ASSERT(painter);
if (!painter || !m_window) {
if (!painter) {
return;
}
if (!m_window) {
qWarning() << "m_window is null, forgot to call \"QtAcrylicEffectHelper::install()\"?";
return;
}
if (m_window->windowState() != Qt::WindowNoState) {
@ -178,14 +222,14 @@ void QtAcrylicEffectHelper::paintWindowFrame(QPainter *painter, const QRect &rec
const int width = rect.isValid() ? rect.width() : m_window->width();
const int height = rect.isValid() ? rect.height() : m_window->height();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
const QList<QLine> lines = {
const QList<QLineF> lines = {
#else
const QVector<QLine> lines = {
const QVector<QLineF> lines = {
#endif
{0, 0, width, 0},
{width - 1, 0, width - 1, height},
{width, height - 1, 0, height - 1},
{0, height, 0, 0}
{0, 0, static_cast<qreal>(width), 0},
{width - m_frameThickness, 0, width - m_frameThickness, static_cast<qreal>(height)},
{static_cast<qreal>(width), height - m_frameThickness, 0, height - m_frameThickness},
{0, static_cast<qreal>(height), 0, 0}
};
const bool active = m_window->isActive();
const QColor color = (active && m_frameColor.isValid() && (m_frameColor != Qt::transparent)) ? m_frameColor : Utilities::getNativeWindowFrameColor(active);

View File

@ -46,11 +46,13 @@ public:
qreal getNoiseOpacity() const;
QPixmap getBluredWallpaper() const;
QColor getFrameColor() const;
qreal getFrameThickness() const;
void setTintColor(const QColor &value);
void setTintOpacity(qreal value);
void setNoiseOpacity(qreal value);
void setTintOpacity(const qreal value);
void setNoiseOpacity(const qreal value);
void setFrameColor(const QColor &value);
void setFrameThickness(const qreal value);
void paintWindowBackground(QPainter *painter, const QRegion &clip);
void paintWindowBackground(QPainter *painter, const QRect &rect);
@ -69,4 +71,5 @@ private:
qreal m_noiseOpacity = 0.04;
QPixmap m_bluredWallpaper = {};
QColor m_frameColor = {};
qreal m_frameThickness = 1.0;
};

View File

@ -84,7 +84,7 @@ qreal QtAcrylicItem::tintOpacity() const
return m_acrylicHelper.getTintOpacity();
}
void QtAcrylicItem::setTintOpacity(qreal value)
void QtAcrylicItem::setTintOpacity(const qreal value)
{
if (m_acrylicHelper.getTintOpacity() != value) {
m_acrylicHelper.setTintOpacity(value);
@ -99,7 +99,7 @@ qreal QtAcrylicItem::noiseOpacity() const
return m_acrylicHelper.getNoiseOpacity();
}
void QtAcrylicItem::setNoiseOpacity(qreal value)
void QtAcrylicItem::setNoiseOpacity(const qreal value)
{
if (m_acrylicHelper.getNoiseOpacity() != value) {
m_acrylicHelper.setNoiseOpacity(value);
@ -114,7 +114,7 @@ bool QtAcrylicItem::frameVisible() const
return m_frameVisible;
}
void QtAcrylicItem::setFrameVisible(bool value)
void QtAcrylicItem::setFrameVisible(const bool value)
{
if (m_frameVisible != value) {
m_frameVisible = value;
@ -136,3 +136,17 @@ void QtAcrylicItem::setFrameColor(const QColor &value)
Q_EMIT frameColorChanged();
}
}
qreal QtAcrylicItem::frameThickness() const
{
return m_acrylicHelper.getFrameThickness();
}
void QtAcrylicItem::setFrameThickness(const qreal value)
{
if (m_acrylicHelper.getFrameThickness() != value) {
m_acrylicHelper.setFrameThickness(value);
update();
Q_EMIT frameThicknessChanged();
}
}

View File

@ -40,6 +40,7 @@ class FRAMELESSHELPER_EXPORT QtAcrylicItem : public QQuickPaintedItem
Q_PROPERTY(qreal noiseOpacity READ noiseOpacity WRITE setNoiseOpacity NOTIFY noiseOpacityChanged)
Q_PROPERTY(bool frameVisible READ frameVisible WRITE setFrameVisible NOTIFY frameVisibleChanged)
Q_PROPERTY(QColor frameColor READ frameColor WRITE setFrameColor NOTIFY frameColorChanged)
Q_PROPERTY(qreal frameThickness READ frameThickness WRITE setFrameThickness NOTIFY frameThicknessChanged)
public:
explicit QtAcrylicItem(QQuickItem *parent = nullptr);
@ -51,23 +52,27 @@ public:
void setTintColor(const QColor &value);
qreal tintOpacity() const;
void setTintOpacity(qreal value);
void setTintOpacity(const qreal value);
qreal noiseOpacity() const;
void setNoiseOpacity(qreal value);
void setNoiseOpacity(const qreal value);
bool frameVisible() const;
void setFrameVisible(bool value);
void setFrameVisible(const bool value);
QColor frameColor() const;
void setFrameColor(const QColor &value);
qreal frameThickness() const;
void setFrameThickness(const qreal value);
Q_SIGNALS:
void tintColorChanged();
void tintOpacityChanged();
void noiseOpacityChanged();
void frameVisibleChanged();
void frameColorChanged();
void frameThicknessChanged();
private:
QtAcrylicEffectHelper m_acrylicHelper;

View File

@ -71,7 +71,7 @@ qreal QtAcrylicWidget::tintOpacity() const
return m_acrylicHelper.getTintOpacity();
}
void QtAcrylicWidget::setTintOpacity(qreal value)
void QtAcrylicWidget::setTintOpacity(const qreal value)
{
if (m_acrylicHelper.getTintOpacity() != value) {
m_acrylicHelper.setTintOpacity(value);
@ -86,7 +86,7 @@ qreal QtAcrylicWidget::noiseOpacity() const
return m_acrylicHelper.getNoiseOpacity();
}
void QtAcrylicWidget::setNoiseOpacity(qreal value)
void QtAcrylicWidget::setNoiseOpacity(const qreal value)
{
if (m_acrylicHelper.getNoiseOpacity() != value) {
m_acrylicHelper.setNoiseOpacity(value);
@ -101,7 +101,7 @@ bool QtAcrylicWidget::frameVisible() const
return m_frameVisible;
}
void QtAcrylicWidget::setFrameVisible(bool value)
void QtAcrylicWidget::setFrameVisible(const bool value)
{
if (m_frameVisible != value) {
m_frameVisible = value;
@ -124,6 +124,20 @@ void QtAcrylicWidget::setFrameColor(const QColor &value)
}
}
qreal QtAcrylicWidget::frameThickness() const
{
return m_acrylicHelper.getFrameThickness();
}
void QtAcrylicWidget::setFrameThickness(const qreal value)
{
if (m_acrylicHelper.getFrameThickness() != value) {
m_acrylicHelper.setFrameThickness(value);
update();
Q_EMIT frameThicknessChanged();
}
}
void QtAcrylicWidget::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);

View File

@ -37,6 +37,7 @@ class FRAMELESSHELPER_EXPORT QtAcrylicWidget : public QWidget
Q_PROPERTY(qreal noiseOpacity READ noiseOpacity WRITE setNoiseOpacity NOTIFY noiseOpacityChanged)
Q_PROPERTY(bool frameVisible READ frameVisible WRITE setFrameVisible NOTIFY frameVisibleChanged)
Q_PROPERTY(QColor frameColor READ frameColor WRITE setFrameColor NOTIFY frameColorChanged)
Q_PROPERTY(qreal frameThickness READ frameThickness WRITE setFrameThickness NOTIFY frameThicknessChanged)
public:
explicit QtAcrylicWidget(QWidget *parent = nullptr);
@ -46,23 +47,27 @@ public:
void setTintColor(const QColor &value);
qreal tintOpacity() const;
void setTintOpacity(qreal value);
void setTintOpacity(const qreal value);
qreal noiseOpacity() const;
void setNoiseOpacity(qreal value);
void setNoiseOpacity(const qreal value);
bool frameVisible() const;
void setFrameVisible(bool value);
void setFrameVisible(const bool value);
QColor frameColor() const;
void setFrameColor(const QColor &value);
qreal frameThickness() const;
void setFrameThickness(const qreal value);
Q_SIGNALS:
void tintColorChanged();
void tintOpacityChanged();
void noiseOpacityChanged();
void frameVisibleChanged();
void frameColorChanged();
void frameThicknessChanged();
protected:
void showEvent(QShowEvent *event) override;