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

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