File indexing completed on 2024-04-28 04:05:03

0001 /*
0002     SPDX-FileCopyrightText: 2007 Dmitry Suzdalev <dimsuz@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef K_GAME_POPUP_ITEM_H
0008 #define K_GAME_POPUP_ITEM_H
0009 
0010 // own
0011 #include "kdegames_export.h"
0012 // Qt
0013 #include <QGraphicsItem>
0014 #include <QObject>
0015 // Std
0016 #include <memory>
0017 
0018 class KGamePopupItemPrivate;
0019 
0020 /**
0021  * \class KGamePopupItem kgamepopupitem.h <KGamePopupItem>
0022  *
0023  * QGraphicsItem capable of showing short popup messages
0024  * which do not interrupt the gameplay.
0025  * Message can stay on screen for specified amount of time
0026  * and automatically hide after (unless user hovers it with mouse).
0027  *
0028  * Example of use:
0029  * \code
0030  * KGamePopupItem *messageItem = new KGamePopupItem();
0031  * myGraphicsScene->addItem(messageItem);
0032  * ...
0033  * messageItem->setMessageTimeout( 3000 ); // 3 sec
0034  * messageItem->showMessage("Hello, I'm a game message! How do you do?", BottomLeft);
0035  * \endcode
0036  */
0037 class KDEGAMES_EXPORT KGamePopupItem : public QObject, public QGraphicsItem
0038 {
0039     Q_OBJECT
0040     Q_INTERFACES(QGraphicsItem)
0041 
0042 public:
0043     /**
0044      * Possible values for message showing mode in respect to a previous
0045      * message
0046      */
0047     enum ReplaceMode { LeavePrevious, ReplacePrevious };
0048     /**
0049      * Possible values for the popup angles sharpness
0050      */
0051     enum Sharpness { Square = 0, Sharp = 2, Soft = 5, Softest = 10 };
0052     /**
0053      * The possible places in the scene where a message can be shown
0054      */
0055     enum Position { TopLeft, TopRight, BottomLeft, BottomRight, Center };
0056     /**
0057      * Constructs a message item. It is hidden by default.
0058      */
0059     explicit KGamePopupItem(QGraphicsItem *parent = nullptr);
0060     /**
0061      * Destructs a message item
0062      */
0063     ~KGamePopupItem() override;
0064     /**
0065      * Shows the message: item will appear at specified place
0066      * of the scene using simple animation
0067      * Item will be automatically hidden after timeout set in setMessageTimeOut() passes
0068      * If item is hovered with mouse it won't hide until user moves
0069      * the mouse away
0070      *
0071      * Note that if pos == Center, message animation will be of fade in/out type,
0072      * rather than slide in/out
0073      *
0074      * @param text holds the message to show
0075      * @param pos position on the scene where the message will appear
0076      * @param mode how to handle an already shown message by this item:
0077        either leave it and ignore the new one or replace it
0078      */
0079     void showMessage(const QString &text, Position pos, ReplaceMode mode = LeavePrevious);
0080     /**
0081      * Sets the amount of time the item will stay visible on screen
0082      * before it goes away.
0083      * By default item is shown for 2000 msec
0084      * If item is hovered with mouse it will hide only after
0085      * user moves the mouse away
0086      *
0087      * @param msec amount of time in milliseconds.
0088      * if msec is 0, then message will stay visible until it
0089      * gets explicitly hidden by forceHide()
0090      */
0091     void setMessageTimeout(int msec);
0092     /**
0093      * @return timeout that is currently set
0094      */
0095     int messageTimeout() const;
0096     /**
0097      * Sets the message opacity from 0 (fully transparent) to 1 (fully opaque)
0098      * For example 0.5 is half transparent
0099      * It defaults to 1.0
0100      */
0101     void setMessageOpacity(qreal opacity);
0102     /**
0103      * @return current message opacity
0104      */
0105     qreal messageOpacity() const;
0106     /**
0107      * Sets custom pixmap to show instead of default icon on the left
0108      */
0109     void setMessageIcon(const QPixmap &pix);
0110     /**
0111      * Sets whether to hide this popup item on mouse click.
0112      * By default a mouse click will cause an item to hide
0113      */
0114     void setHideOnMouseClick(bool hide);
0115     /**
0116      * @return whether this popup item hides on mouse click.
0117      */
0118     bool hidesOnMouseClick() const;
0119     /**
0120      * Used to specify how to hide in forceHide() - instantly or animatedly
0121      */
0122     enum HideType { InstantHide, AnimatedHide };
0123     /**
0124      * Requests the item to be hidden immediately.
0125      */
0126     void forceHide(HideType type = AnimatedHide);
0127     /**
0128      * Sets brush used to paint item background
0129      * By default system-default brush is used
0130      * @see KColorScheme
0131      */
0132     void setBackgroundBrush(const QBrush &brush);
0133     /**
0134      * Sets default color for unformatted text
0135      * By default system-default color is used
0136      * @see KColorScheme
0137      */
0138     void setTextColor(const QColor &color);
0139     /**
0140      * @return the bounding rect of this item. Reimplemented from QGraphicsItem
0141      */
0142     QRectF boundingRect() const override;
0143     /**
0144      * Paints item. Reimplemented from QGraphicsItem
0145      */
0146     void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
0147     /**
0148      * Sets the popup angles sharpness
0149      */
0150     void setSharpness(Sharpness sharpness);
0151     /**
0152      * @return current popup angles sharpness
0153      */
0154     Sharpness sharpness() const;
0155 Q_SIGNALS:
0156     /**
0157      * Emitted when user clicks on a link in item
0158      */
0159     void linkActivated(const QString &link);
0160     /**
0161      * Emitted when user hovers a link in item
0162      */
0163     void linkHovered(const QString &link);
0164     /**
0165      * Emitted when the popup finishes hiding. This includes hiding caused by
0166      * both timeouts and mouse clicks.
0167      */
0168     void hidden();
0169 private Q_SLOTS:
0170     void animationFrame(int);
0171     void hideMe();
0172     void playHideAnimation();
0173     void onLinkHovered(const QString &);
0174     void onTextItemClicked();
0175 
0176 private:
0177     void setupTimeline();
0178     void mousePressEvent(QGraphicsSceneMouseEvent *) override;
0179     void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override;
0180     void hoverEnterEvent(QGraphicsSceneHoverEvent *) override;
0181     void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override;
0182 
0183 private:
0184     std::unique_ptr<KGamePopupItemPrivate> const d_ptr;
0185     Q_DECLARE_PRIVATE(KGamePopupItem)
0186 };
0187 
0188 #endif