File indexing completed on 2024-05-12 05:46:33

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