File indexing completed on 2024-05-05 05:35:16

0001 /*
0002     SPDX-FileCopyrightText: 2013 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003     SPDX-FileCopyrightText: 2008 Long Huynh Huu <long.upcase@googlemail.com>
0004     SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2007 Casper Boemann <cbr@boemann.dk>
0006     SPDX-FileCopyrightText: 2015 David Edmundson <davidedmundson@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 */
0010 
0011 #include "oxygendecohelper.h"
0012 
0013 #include <KColorUtils>
0014 #include <QPainter>
0015 
0016 #include <cmath>
0017 
0018 namespace Oxygen
0019 {
0020 //______________________________________________________________________________
0021 DecoHelper::DecoHelper()
0022     : Helper(KSharedConfig::openConfig(QStringLiteral("oxygenrc")))
0023 {
0024 }
0025 
0026 //______________________________________________________________________________
0027 void DecoHelper::invalidateCaches(void)
0028 {
0029     // base class call
0030     Helper::invalidateCaches();
0031 
0032     // local caches
0033     _windecoButtonCache.clear();
0034 }
0035 
0036 //______________________________________________________________________________
0037 QPixmap DecoHelper::windecoButton(const QColor &color, const QColor &glow, bool sunken, int size)
0038 {
0039     Oxygen::Cache<QPixmap>::Value cache(_windecoButtonCache.get(color));
0040 
0041     const quint64 key((colorKey(glow) << 32) | (sunken << 23) | size);
0042 
0043     if (QPixmap *cachedPixmap = cache->object(key)) {
0044         return *cachedPixmap;
0045     }
0046 
0047     QPixmap pixmap(size, size);
0048     pixmap.fill(Qt::transparent);
0049 
0050     QPainter p(&pixmap);
0051     p.setRenderHints(QPainter::Antialiasing);
0052     p.setPen(Qt::NoPen);
0053     p.setWindow(0, 0, 21, 21);
0054 
0055     // button shadow
0056     if (color.isValid()) {
0057         p.save();
0058         p.translate(0, -0.2);
0059         drawShadow(p, calcShadowColor(color), 21);
0060         p.restore();
0061     }
0062 
0063     // button glow
0064     if (glow.isValid()) {
0065         p.save();
0066         p.translate(0, -0.2);
0067         drawOuterGlow(p, glow, 21);
0068         p.restore();
0069     }
0070 
0071     // button slab
0072     p.translate(0, 1);
0073     p.setWindow(0, 0, 18, 18);
0074     if (color.isValid()) {
0075         p.translate(0, (0.5 - 0.668));
0076 
0077         const QColor light(calcLightColor(color));
0078         const QColor dark(calcDarkColor(color));
0079 
0080         {
0081             // plain background
0082             QLinearGradient lg(0, 1.665, 0, (12.33 + 1.665));
0083             if (sunken) {
0084                 lg.setColorAt(1, light);
0085                 lg.setColorAt(0, dark);
0086             } else {
0087                 lg.setColorAt(0, light);
0088                 lg.setColorAt(1, dark);
0089             }
0090 
0091             const QRectF r(0.5 * (18 - 12.33), 1.665, 12.33, 12.33);
0092             p.setBrush(lg);
0093             p.drawEllipse(r);
0094         }
0095 
0096         {
0097             // outline circle
0098             const qreal penWidth(0.7);
0099             QLinearGradient lg(0, 1.665, 0, (2.0 * 12.33 + 1.665));
0100             lg.setColorAt(0, light);
0101             lg.setColorAt(1, dark);
0102             const QRectF r(0.5 * (18 - 12.33 + penWidth), (1.665 + penWidth), (12.33 - penWidth), (12.33 - penWidth));
0103             p.setPen(QPen(lg, penWidth));
0104             p.setBrush(Qt::NoBrush);
0105             p.drawEllipse(r);
0106         }
0107     }
0108 
0109     p.end();
0110     cache->insert(key, new QPixmap(pixmap));
0111 
0112     return pixmap;
0113 }
0114 }