File indexing completed on 2024-12-01 08:07:26

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "patternwidget.h"
0007 
0008 //! local
0009 #include "../../tools/commontools.h"
0010 
0011 //! Qt
0012 #include <QDebug>
0013 #include <QFont>
0014 #include <QHBoxLayout>
0015 #include <QPainter>
0016 #include <QPalette>
0017 #include <QStyleOption>
0018 
0019 //! KDE
0020 #include <KLocalizedString>
0021 
0022 namespace Latte {
0023 namespace Settings {
0024 namespace Widget {
0025 
0026 PatternWidget::PatternWidget(QWidget *parent, Qt::WindowFlags f)
0027     : QWidget(parent, f)
0028 {
0029     setMouseTracking(true);
0030 
0031     setProperty("isBackground", true);
0032 
0033     initUi();
0034 
0035     connect(this, &PatternWidget::backgroundChanged, this, &PatternWidget::updateUi);
0036     connect(this, &PatternWidget::textColorChanged, this, &PatternWidget::updateUi);
0037 }
0038 
0039 void PatternWidget::initUi()
0040 {
0041     QHBoxLayout *l = new QHBoxLayout(this);
0042     m_label = new QLabel(this);
0043 
0044     l->addWidget(m_label);
0045     l->setAlignment(Qt::AlignCenter);
0046     setLayout(l);
0047 
0048     m_label->setText(i18n("Sample Text"));
0049     QFont font  = m_label->font();
0050     font.setBold(true);
0051     m_label->setFont(font);
0052 
0053     //! shadow
0054     m_shadowEffect = new QGraphicsDropShadowEffect(this);
0055     m_shadowEffect->setXOffset(0);
0056     m_shadowEffect->setYOffset(0);
0057     m_label->setGraphicsEffect(m_shadowEffect);
0058 }
0059 
0060 QString PatternWidget::background() const
0061 {
0062     return m_background;
0063 }
0064 
0065 void PatternWidget::setBackground(const QString &file)
0066 {
0067     if (m_background != file) {
0068         m_background = file;
0069     }
0070 
0071     emit backgroundChanged();
0072 }
0073 
0074 void PatternWidget::setText(const QString &text)
0075 {
0076     m_label->setText(text);
0077 }
0078 
0079 QString PatternWidget::textColor() const
0080 {
0081     return m_textColor;
0082 }
0083 
0084 void PatternWidget::setTextColor(const QString &color)
0085 {
0086     if (m_textColor == color) {
0087         return;
0088     }
0089 
0090     m_textColor = color;
0091     m_textColorBrightness = Latte::colorBrightness(QColor(color));
0092 
0093     emit textColorChanged();
0094 }
0095 
0096 void PatternWidget::updateUi()
0097 {
0098     QPalette systemPalette;
0099 
0100     QColor textColor = systemPalette.color(QPalette::Active, QPalette::Text);
0101     QString background = "background-image: url(" + m_background + ");";
0102 
0103     int radius = qMax(2, height() / 4);
0104 
0105     if (m_background.isEmpty()) {
0106         background = "background-image: none;";
0107         m_shadowEffect->setColor(Qt::transparent);
0108     } else {
0109         m_shadowEffect->setColor("#020202");
0110     }
0111 
0112     if (m_textColorBrightness > 127) {
0113         m_shadowEffect->setBlurRadius(12);
0114     } else {
0115         m_shadowEffect->setBlurRadius(1);
0116     }
0117 
0118     setStyleSheet("[isBackground=true] {border: 1px solid " + textColor.name() + "; border-radius: " + QString::number(radius) + "px; " + background + "}");
0119     m_label->setStyleSheet("QLabel {border: 0px; background-image:none; color:" + m_textColor + "}");
0120 }
0121 
0122 void PatternWidget::enterEvent(QEvent *event)
0123 {
0124     setCursor(Qt::PointingHandCursor);
0125     QWidget::enterEvent(event);
0126 }
0127 
0128 void PatternWidget::mouseMoveEvent(QMouseEvent *event )
0129 {
0130     QWidget::mouseMoveEvent(event);
0131 }
0132 
0133 void PatternWidget::mouseReleaseEvent(QMouseEvent *event)
0134 {
0135     emit mouseReleased();
0136     QWidget::mouseReleaseEvent(event);
0137 }
0138 
0139 void PatternWidget::paintEvent(QPaintEvent *event)
0140 {
0141     //! it is needed from Qt, otherwise QWidget is not updated
0142     //! https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget
0143     QStyleOption opt;
0144     opt.init(this);
0145     QPainter p(this);
0146     p.setRenderHint(QPainter::Antialiasing, true);
0147 
0148     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
0149 
0150     QWidget::paintEvent(event);
0151 }
0152 
0153 }
0154 }
0155 }