Changeset View
Changeset View
Standalone View
Standalone View
kdecoration/breezedecoration.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2014 Martin Gräßlin <mgraesslin@kde.org> | 2 | * Copyright 2014 Martin Gräßlin <mgraesslin@kde.org> | ||
3 | * Copyright 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr> | 3 | * Copyright 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr> | ||
4 | * Copyright 2018 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
4 | * | 5 | * | ||
5 | * This program is free software; you can redistribute it and/or | 6 | * This program is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU General Public License as | 7 | * modify it under the terms of the GNU General Public License as | ||
7 | * published by the Free Software Foundation; either version 2 of | 8 | * published by the Free Software Foundation; either version 2 of | ||
8 | * the License or (at your option) version 3 or any later version | 9 | * the License or (at your option) version 3 or any later version | ||
9 | * accepted by the membership of KDE e.V. (or its successor approved | 10 | * accepted by the membership of KDE e.V. (or its successor approved | ||
10 | * by the membership of KDE e.V.), which shall act as a proxy | 11 | * by the membership of KDE e.V.), which shall act as a proxy | ||
11 | * defined in Section 14 of version 3 of the license. | 12 | * defined in Section 14 of version 3 of the license. | ||
Show All 12 Lines | |||||
24 | #include "breeze.h" | 25 | #include "breeze.h" | ||
25 | #include "breezesettingsprovider.h" | 26 | #include "breezesettingsprovider.h" | ||
26 | #include "config-breeze.h" | 27 | #include "config-breeze.h" | ||
27 | #include "config/breezeconfigwidget.h" | 28 | #include "config/breezeconfigwidget.h" | ||
28 | 29 | | |||
29 | #include "breezebutton.h" | 30 | #include "breezebutton.h" | ||
30 | #include "breezesizegrip.h" | 31 | #include "breezesizegrip.h" | ||
31 | 32 | | |||
32 | #include "breezeboxshadowhelper.h" | 33 | #include "breezeboxshadowrenderer.h" | ||
33 | 34 | | |||
34 | #include <KDecoration2/DecoratedClient> | 35 | #include <KDecoration2/DecoratedClient> | ||
35 | #include <KDecoration2/DecorationButtonGroup> | 36 | #include <KDecoration2/DecorationButtonGroup> | ||
36 | #include <KDecoration2/DecorationSettings> | 37 | #include <KDecoration2/DecorationSettings> | ||
37 | #include <KDecoration2/DecorationShadow> | 38 | #include <KDecoration2/DecorationShadow> | ||
38 | 39 | | |||
39 | #include <KConfigGroup> | 40 | #include <KConfigGroup> | ||
40 | #include <KColorUtils> | 41 | #include <KColorUtils> | ||
▲ Show 20 Lines • Show All 683 Lines • ▼ Show 20 Line(s) | 715 | { | |||
724 | } | 725 | } | ||
725 | 726 | | |||
726 | auto withOpacity = [](const QColor &color, qreal opacity) -> QColor { | 727 | auto withOpacity = [](const QColor &color, qreal opacity) -> QColor { | ||
727 | QColor c(color); | 728 | QColor c(color); | ||
728 | c.setAlphaF(opacity); | 729 | c.setAlphaF(opacity); | ||
729 | return c; | 730 | return c; | ||
730 | }; | 731 | }; | ||
731 | 732 | | |||
732 | // In order to properly render a box shadow with a given radius `shadowSize`, | 733 | const QSize boxSize = BoxShadowRenderer::calculateMinimumBoxSize(params.shadow1.radius) | ||
733 | // the box size should be at least `2 * QSize(shadowSize, shadowSize)`. | 734 | .expandedTo(BoxShadowRenderer::calculateMinimumBoxSize(params.shadow2.radius)); | ||
734 | const int shadowSize = qMax(params.shadow1.radius, params.shadow2.radius); | | |||
735 | const QRect box(shadowSize, shadowSize, 2 * shadowSize + 1, 2 * shadowSize + 1); | | |||
736 | const QRect rect = box.adjusted(-shadowSize, -shadowSize, shadowSize, shadowSize); | | |||
737 | 735 | | |||
738 | QImage shadow(rect.size(), QImage::Format_ARGB32_Premultiplied); | 736 | BoxShadowRenderer shadowRenderer; | ||
739 | shadow.fill(Qt::transparent); | 737 | shadowRenderer.setBorderRadius(Metrics::Frame_FrameRadius + 0.5); | ||
740 | 738 | shadowRenderer.setBoxSize(boxSize); | |||
741 | QPainter painter(&shadow); | 739 | shadowRenderer.setDevicePixelRatio(1.0); // TODO: Create HiDPI shadows? | ||
742 | painter.setRenderHint(QPainter::Antialiasing); | | |||
743 | 740 | | |||
744 | const qreal strength = static_cast<qreal>(g_shadowStrength) / 255.0; | 741 | const qreal strength = static_cast<qreal>(g_shadowStrength) / 255.0; | ||
745 | 742 | shadowRenderer.addShadow(params.shadow1.offset, params.shadow1.radius, | |||
746 | // Draw the "shape" shadow. | | |||
747 | BoxShadowHelper::boxShadow( | | |||
748 | &painter, | | |||
749 | box, | | |||
750 | params.shadow1.offset, | | |||
751 | params.shadow1.radius, | | |||
752 | withOpacity(g_shadowColor, params.shadow1.opacity * strength)); | 743 | withOpacity(g_shadowColor, params.shadow1.opacity * strength)); | ||
753 | 744 | shadowRenderer.addShadow(params.shadow2.offset, params.shadow2.radius, | |||
754 | // Draw the "contrast" shadow. | | |||
755 | BoxShadowHelper::boxShadow( | | |||
756 | &painter, | | |||
757 | box, | | |||
758 | params.shadow2.offset, | | |||
759 | params.shadow2.radius, | | |||
760 | withOpacity(g_shadowColor, params.shadow2.opacity * strength)); | 745 | withOpacity(g_shadowColor, params.shadow2.opacity * strength)); | ||
761 | 746 | | |||
747 | QImage shadowTexture = shadowRenderer.render(); | ||||
748 | | ||||
749 | QPainter painter(&shadowTexture); | ||||
750 | painter.setRenderHint(QPainter::Antialiasing); | ||||
751 | | ||||
752 | const QRect outerRect = shadowTexture.rect(); | ||||
753 | | ||||
754 | QRect boxRect(QPoint(0, 0), boxSize); | ||||
755 | boxRect.moveCenter(outerRect.center()); | ||||
756 | | ||||
762 | // Mask out inner rect. | 757 | // Mask out inner rect. | ||
763 | const QMargins padding = QMargins( | 758 | const QMargins padding = QMargins( | ||
764 | shadowSize - Metrics::Shadow_Overlap - params.offset.x(), | 759 | boxRect.left() - outerRect.left() - Metrics::Shadow_Overlap - params.offset.x(), | ||
765 | shadowSize - Metrics::Shadow_Overlap - params.offset.y(), | 760 | boxRect.top() - outerRect.top() - Metrics::Shadow_Overlap - params.offset.y(), | ||
766 | shadowSize - Metrics::Shadow_Overlap + params.offset.x(), | 761 | outerRect.right() - boxRect.right() - Metrics::Shadow_Overlap + params.offset.x(), | ||
767 | shadowSize - Metrics::Shadow_Overlap + params.offset.y()); | 762 | outerRect.bottom() - boxRect.bottom() - Metrics::Shadow_Overlap + params.offset.y()); | ||
768 | const QRect innerRect = rect - padding; | 763 | const QRect innerRect = outerRect - padding; | ||
769 | 764 | | |||
770 | painter.setPen(Qt::NoPen); | 765 | painter.setPen(Qt::NoPen); | ||
771 | painter.setBrush(Qt::black); | 766 | painter.setBrush(Qt::black); | ||
772 | painter.setCompositionMode(QPainter::CompositionMode_DestinationOut); | 767 | painter.setCompositionMode(QPainter::CompositionMode_DestinationOut); | ||
773 | painter.drawRoundedRect( | 768 | painter.drawRoundedRect( | ||
774 | innerRect, | 769 | innerRect, | ||
775 | Metrics::Frame_FrameRadius + 0.5, | 770 | Metrics::Frame_FrameRadius + 0.5, | ||
776 | Metrics::Frame_FrameRadius + 0.5); | 771 | Metrics::Frame_FrameRadius + 0.5); | ||
777 | 772 | | |||
778 | // Draw outline. | 773 | // Draw outline. | ||
779 | painter.setPen(withOpacity(g_shadowColor, 0.2 * strength)); | 774 | painter.setPen(withOpacity(g_shadowColor, 0.2 * strength)); | ||
780 | painter.setBrush(Qt::NoBrush); | 775 | painter.setBrush(Qt::NoBrush); | ||
781 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); | 776 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); | ||
782 | painter.drawRoundedRect( | 777 | painter.drawRoundedRect( | ||
783 | innerRect, | 778 | innerRect, | ||
784 | Metrics::Frame_FrameRadius - 0.5, | 779 | Metrics::Frame_FrameRadius - 0.5, | ||
785 | Metrics::Frame_FrameRadius - 0.5); | 780 | Metrics::Frame_FrameRadius - 0.5); | ||
786 | 781 | | |||
787 | painter.end(); | 782 | painter.end(); | ||
788 | 783 | | |||
789 | g_sShadow = QSharedPointer<KDecoration2::DecorationShadow>::create(); | 784 | g_sShadow = QSharedPointer<KDecoration2::DecorationShadow>::create(); | ||
790 | g_sShadow->setPadding(padding); | 785 | g_sShadow->setPadding(padding); | ||
791 | g_sShadow->setInnerShadowRect(QRect(shadow.rect().center(), QSize(1, 1))); | 786 | g_sShadow->setInnerShadowRect(QRect(outerRect.center(), QSize(1, 1))); | ||
792 | g_sShadow->setShadow(shadow); | 787 | g_sShadow->setShadow(shadowTexture); | ||
793 | } | 788 | } | ||
794 | 789 | | |||
795 | setShadow(g_sShadow); | 790 | setShadow(g_sShadow); | ||
796 | } | 791 | } | ||
797 | 792 | | |||
798 | //_________________________________________________________________ | 793 | //_________________________________________________________________ | ||
799 | void Decoration::createSizeGrip() | 794 | void Decoration::createSizeGrip() | ||
800 | { | 795 | { | ||
Show All 36 Lines |