File indexing completed on 2024-05-19 05:31:40

0001 /*
0002     SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
0003     SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "effect/globals.h"
0011 #include "effect/offscreenquickview.h"
0012 
0013 #include <QFont>
0014 #include <QIcon>
0015 
0016 namespace KWin
0017 {
0018 
0019 class RenderTarget;
0020 class RenderViewport;
0021 
0022 /**
0023  * Style types used by @ref EffectFrame.
0024  * @since 4.6
0025  */
0026 enum EffectFrameStyle {
0027     EffectFrameNone, ///< Displays no frame around the contents.
0028     EffectFrameUnstyled, ///< Displays a basic box around the contents.
0029     EffectFrameStyled ///< Displays a Plasma-styled frame around the contents.
0030 };
0031 
0032 class EffectFrameQuickScene : public OffscreenQuickScene
0033 {
0034     Q_OBJECT
0035 
0036     Q_PROPERTY(QFont font READ font NOTIFY fontChanged)
0037     Q_PROPERTY(QIcon icon READ icon NOTIFY iconChanged)
0038     Q_PROPERTY(QSize iconSize READ iconSize NOTIFY iconSizeChanged)
0039     Q_PROPERTY(QString text READ text NOTIFY textChanged)
0040     Q_PROPERTY(qreal frameOpacity READ frameOpacity NOTIFY frameOpacityChanged)
0041     Q_PROPERTY(bool crossFadeEnabled READ crossFadeEnabled NOTIFY crossFadeEnabledChanged)
0042     Q_PROPERTY(qreal crossFadeProgress READ crossFadeProgress NOTIFY crossFadeProgressChanged)
0043 
0044 public:
0045     EffectFrameQuickScene(EffectFrameStyle style, bool staticSize, QPoint position, Qt::Alignment alignment);
0046     ~EffectFrameQuickScene() override;
0047 
0048     EffectFrameStyle style() const;
0049     bool isStatic() const;
0050 
0051     QFont font() const;
0052     void setFont(const QFont &font);
0053     Q_SIGNAL void fontChanged(const QFont &font);
0054 
0055     QIcon icon() const;
0056     void setIcon(const QIcon &icon);
0057     Q_SIGNAL void iconChanged(const QIcon &icon);
0058 
0059     QSize iconSize() const;
0060     void setIconSize(const QSize &iconSize);
0061     Q_SIGNAL void iconSizeChanged(const QSize &iconSize);
0062 
0063     QString text() const;
0064     void setText(const QString &text);
0065     Q_SIGNAL void textChanged(const QString &text);
0066 
0067     qreal frameOpacity() const;
0068     void setFrameOpacity(qreal frameOpacity);
0069     Q_SIGNAL void frameOpacityChanged(qreal frameOpacity);
0070 
0071     bool crossFadeEnabled() const;
0072     void setCrossFadeEnabled(bool enabled);
0073     Q_SIGNAL void crossFadeEnabledChanged(bool enabled);
0074 
0075     qreal crossFadeProgress() const;
0076     void setCrossFadeProgress(qreal progress);
0077     Q_SIGNAL void crossFadeProgressChanged(qreal progress);
0078 
0079     Qt::Alignment alignment() const;
0080     void setAlignment(Qt::Alignment alignment);
0081 
0082     QPoint position() const;
0083     void setPosition(const QPoint &point);
0084 
0085 private:
0086     void reposition();
0087 
0088     EffectFrameStyle m_style;
0089 
0090     // Position
0091     bool m_static;
0092     QPoint m_point;
0093     Qt::Alignment m_alignment;
0094 
0095     // Contents
0096     QFont m_font;
0097     QIcon m_icon;
0098     QSize m_iconSize;
0099     QString m_text;
0100     qreal m_frameOpacity = 0.0;
0101     bool m_crossFadeEnabled = false;
0102     qreal m_crossFadeProgress = 0.0;
0103 };
0104 
0105 /**
0106  * @short Helper class for displaying text and icons in frames.
0107  *
0108  * Paints text and/or and icon with an optional frame around them. The
0109  * available frames includes one that follows the default Plasma theme and
0110  * another that doesn't.
0111  * It is recommended to use this class whenever displaying text.
0112  */
0113 class KWIN_EXPORT EffectFrame : public QObject
0114 {
0115     Q_OBJECT
0116 
0117 public:
0118     explicit EffectFrame(EffectFrameStyle style, bool staticSize = true, QPoint position = QPoint(-1, -1),
0119                          Qt::Alignment alignment = Qt::AlignCenter);
0120     ~EffectFrame();
0121 
0122     /**
0123      * Delete any existing textures to free up graphics memory. They will
0124      * be automatically recreated the next time they are required.
0125      */
0126     void free();
0127 
0128     /**
0129      * Render the frame.
0130      */
0131     void render(const RenderTarget &renderTarget, const RenderViewport &viewport, const QRegion &region = infiniteRegion(), double opacity = 1.0, double frameOpacity = 1.0);
0132 
0133     void setPosition(const QPoint &point);
0134     /**
0135      * Set the text alignment for static frames and the position alignment
0136      * for non-static.
0137      */
0138     void setAlignment(Qt::Alignment alignment);
0139     Qt::Alignment alignment() const;
0140     void setGeometry(const QRect &geometry, bool force = false);
0141     QRect geometry() const;
0142 
0143     void setText(const QString &text);
0144     QString text() const;
0145     void setFont(const QFont &font);
0146     QFont font() const;
0147     /**
0148      * Set the icon that will appear on the left-hand size of the frame.
0149      */
0150     void setIcon(const QIcon &icon);
0151     QIcon icon() const;
0152     void setIconSize(const QSize &size);
0153     QSize iconSize() const;
0154 
0155     /**
0156      * @returns The style of this EffectFrame.
0157      */
0158     EffectFrameStyle style() const;
0159 
0160     /**
0161      * If @p enable is @c true cross fading between icons and text is enabled
0162      * By default disabled. Use setCrossFadeProgress to cross fade.
0163      * Cross Fading is currently only available if OpenGL is used.
0164      * @param enable @c true enables cross fading, @c false disables it again
0165      * @see isCrossFade
0166      * @see setCrossFadeProgress
0167      * @since 4.6
0168      */
0169     void enableCrossFade(bool enable);
0170     /**
0171      * @returns @c true if cross fading is enabled, @c false otherwise
0172      * @see enableCrossFade
0173      * @since 4.6
0174      */
0175     bool isCrossFade() const;
0176     /**
0177      * Sets the current progress for cross fading the last used icon/text
0178      * with current icon/text to @p progress.
0179      * A value of 0.0 means completely old icon/text, a value of 1.0 means
0180      * completely current icon/text.
0181      * Default value is 1.0. You have to enable cross fade before using it.
0182      * Cross Fading is currently only available if OpenGL is used.
0183      * @see enableCrossFade
0184      * @see isCrossFade
0185      * @see crossFadeProgress
0186      * @since 4.6
0187      */
0188     void setCrossFadeProgress(qreal progress);
0189     /**
0190      * @returns The current progress for cross fading
0191      * @see setCrossFadeProgress
0192      * @see enableCrossFade
0193      * @see isCrossFade
0194      * @since 4.6
0195      */
0196     qreal crossFadeProgress() const;
0197 
0198 private:
0199     EffectFrameQuickScene *m_view;
0200 };
0201 
0202 } // namespace KWin