File indexing completed on 2023-09-24 08:13:58

0001 /*
0002     This file is part of the KDE games kwin4 program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef BUTTON_SPRITE_H
0009 #define BUTTON_SPRITE_H
0010 
0011 // own
0012 #include "pixmapsprite.h"
0013 #include "spritenotify.h"
0014 #include "thememanager.h"
0015 // Qt
0016 #include <QGraphicsPixmapItem>
0017 #include <QGraphicsTextItem>
0018 #include <QMouseEvent>
0019 
0020 /**
0021  * The sprite for a interactive buttons. The button can either be
0022  * a push button which reacts on mouse press events or a toggle button
0023  * which is toggled on mouse release.
0024  *
0025  * NOTE: An own event handler is implemented currently in the DisplayIntro
0026  *       class which uses this buttons because the Qt4.3 QGraphicsView event
0027  *       handling system is faulty!!!!
0028  */
0029 class ButtonSprite : public PixmapSprite
0030 {
0031 public:
0032     /**
0033      * Constructor for the score sprite.
0034      * @param pushButton      True: push button, False: Toggle button
0035      * @param id              The theme id
0036      * @param theme           The theme manager
0037      * @param no              A used defined number (unused)
0038      * @param scene           The graphics scene
0039      */
0040     ButtonSprite(bool pushButton, const QString &id, ThemeManager *theme, int no, QGraphicsScene *scene);
0041 
0042     /**
0043      * Destructor
0044      */
0045     ~ButtonSprite() override;
0046 
0047     /**
0048      * Standard QGI advance function.
0049      *  @param phase The advance phase
0050      */
0051     void advance(int phase) override;
0052 
0053     /**
0054      * Retrieve the type of QGI. This item is UserType+10
0055      *  @return The type of item.
0056      */
0057     int type() const override
0058     {
0059         return QGraphicsItem::UserType + 100;
0060     }
0061 
0062     /**
0063      * Main theme change function. On call of this the item needs to redraw and
0064      * resize.
0065      */
0066     void changeTheme() override;
0067 
0068     /**
0069      * Store the button text.
0070      * @param s  The text
0071      */
0072     void setText(const QString &s);
0073 
0074     /**
0075      * Set the status of the button to on or off.
0076      * @param status The status
0077      */
0078     void setStatus(const bool status);
0079 
0080     /**
0081      * Retrieve the status of the button (on or off).
0082      * @return The status
0083      */
0084     bool status() const;
0085 
0086     /**
0087      * Allow the sprite to emit a signal. Used for button events.
0088      * @return The notify object.
0089      */
0090     SpriteNotify *notify()
0091     {
0092         return mSignal;
0093     }
0094 
0095     /**
0096      * A mouse press event is received.
0097      * @param event The mouse event
0098      */
0099     void mousePressEvent(QMouseEvent *event);
0100 
0101     /**
0102      * A mouse release event is received.
0103      * @param event The mouse event
0104      */
0105     void mouseReleaseEvent(QMouseEvent *event);
0106 
0107     /**
0108      * A hover in event is received (mouse tracking must be on).
0109      * @param event The mouse event
0110      */
0111     void hoverEnterEvent(QMouseEvent *event);
0112 
0113     /**
0114      * A hover out event is received (mouse tracking must be on).
0115      * @param event The mouse event
0116      */
0117     void hoverLeaveEvent(QMouseEvent *event);
0118 
0119     // Silence gcc warnings
0120     using QGraphicsPixmapItem::hoverEnterEvent;
0121     using QGraphicsPixmapItem::hoverLeaveEvent;
0122     using QGraphicsPixmapItem::mousePressEvent;
0123     using QGraphicsPixmapItem::mouseReleaseEvent;
0124 
0125 protected:
0126     /**
0127      * Change the frame of the sprite to match the button state.
0128      */
0129     void changeFrame();
0130 
0131 private:
0132     // Button text
0133     QGraphicsTextItem *mText;
0134     // State of button
0135     bool mButtonPressed;
0136     // State of hover
0137     bool mHover;
0138     // Is push button?
0139     bool mPushButton;
0140 
0141     // The notification object
0142     SpriteNotify *mSignal;
0143 };
0144 
0145 #endif