File indexing completed on 2023-11-26 10:50:24

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 #include "buttonsprite.h"
0009 
0010 // own
0011 #include "kfontutils.h"
0012 #include "kfourinline_debug.h"
0013 // KF
0014 #include <KConfig>
0015 #include <KConfigGroup>
0016 // Qt
0017 #include <QFont>
0018 #include <QGraphicsScene>
0019 #include <QGraphicsSceneMouseEvent>
0020 // Std
0021 #include <cmath>
0022 
0023 // Constructor for the score sprite
0024 ButtonSprite::ButtonSprite(bool pushButton, const QString &id, ThemeManager *theme, int no, QGraphicsScene *scene)
0025     : Themeable(id, theme)
0026     , PixmapSprite(no, scene)
0027     , mSignal(new SpriteNotify(this))
0028 {
0029     mButtonPressed = false;
0030     mHover = false;
0031     mPushButton = pushButton;
0032 
0033 
0034     setAcceptedMouseButtons(Qt::NoButton);
0035     setAcceptHoverEvents(false);
0036 
0037     // Create sub sprites
0038     mText = new QGraphicsTextItem(this);
0039     scene->addItem(mText);
0040     mText->hide();
0041 
0042     // Redraw us
0043     if (theme)
0044         theme->updateTheme(this);
0045 }
0046 
0047 // Destructor
0048 ButtonSprite::~ButtonSprite()
0049 {
0050     // Clean up
0051     delete mText;
0052 }
0053 
0054 // Redraw the theme.
0055 void ButtonSprite::changeTheme()
0056 {
0057     // The main display is handled by the parent
0058     PixmapSprite::changeTheme();
0059 
0060     // Retrieve our size
0061     double width = this->boundingRect().width();
0062     double height = this->boundingRect().height();
0063 
0064     // Retrieve theme data
0065     KConfigGroup config = thememanager()->config(id());
0066 
0067     // Text available?
0068     if (mText->isVisible()) {
0069         // Calculate proper font size
0070         double fontHeight = config.readEntry("fontHeight", 1.0);
0071         fontHeight *= height;
0072         double textWidth = config.readEntry("textWidth", 1.0);
0073         textWidth *= width;
0074         double textHeight = config.readEntry("textHeight", 1.0);
0075         textHeight *= height;
0076 
0077         // Retrieve font color
0078         QColor fontColor;
0079         fontColor = config.readEntry("fontColor", QColor(Qt::white));
0080 
0081         // Create and set current font
0082         QFont font;
0083         fontHeight = KFontUtils::adaptFontSize(mText, textWidth, textHeight, fontHeight, 8.0);
0084         font.setPointSizeF(fontHeight);
0085 
0086         // Set font and color for all text items
0087         mText->setFont(font);
0088         mText->setDefaultTextColor(fontColor);
0089         mText->setTextWidth(textWidth);
0090 
0091         // Set position of sub sprites
0092         QRectF bounding = mText->boundingRect();
0093         mText->setPos((width - bounding.width()) / 2.0, (height - bounding.height()) / 2.0);
0094     }
0095 }
0096 
0097 // QGI advance method
0098 void ButtonSprite::advance(int phase)
0099 {
0100     // Advance time and animation etc
0101     PixmapSprite::advance(phase);
0102 }
0103 
0104 // Set the button status (on or off)
0105 void ButtonSprite::setStatus(const bool status)
0106 {
0107     mButtonPressed = status;
0108     changeFrame();
0109 }
0110 
0111 // Get the button status (on or off)
0112 bool ButtonSprite::status() const
0113 {
0114     return mButtonPressed;
0115 }
0116 
0117 // Store and display the button text
0118 void ButtonSprite::setText(const QString &s)
0119 {
0120     if (s.isNull()) {
0121         mText->hide();
0122     } else {
0123         // mText->setPlainText(s);
0124         //  Center
0125         mText->setHtml(QStringLiteral("<div align=\"center\">") + s + QStringLiteral("</div>"));
0126         mText->show();
0127         thememanager()->updateTheme(this);
0128     }
0129 }
0130 
0131 // Adjust the frame depending on the state
0132 void ButtonSprite::changeFrame()
0133 {
0134     int frame = 0;
0135     if (mButtonPressed)
0136         frame = 2;
0137     if (mHover)
0138         frame += 1;
0139     // Hover ignored for pressed pushbutton
0140     if (mPushButton && mButtonPressed)
0141         frame = 2;
0142     setFrame(frame);
0143 }
0144 
0145 // Hover enter event received
0146 void ButtonSprite::hoverEnterEvent(QMouseEvent * /*event*/)
0147 {
0148     mHover = true;
0149     changeFrame();
0150 }
0151 
0152 // Hover leave event received
0153 void ButtonSprite::hoverLeaveEvent(QMouseEvent * /*event*/)
0154 {
0155     mHover = false;
0156     if (mPushButton)
0157         mButtonPressed = false;
0158     changeFrame();
0159 }
0160 
0161 // Mouse press event received
0162 void ButtonSprite::mousePressEvent(QMouseEvent * /*event*/)
0163 {
0164     if (mPushButton) {
0165         mButtonPressed = !mButtonPressed;
0166         if (mButtonPressed)
0167             mSignal->emitSignal(number() | 0x100);
0168         else
0169             mSignal->emitSignal(number());
0170     }
0171     changeFrame();
0172 }
0173 
0174 // Mouse release event received
0175 void ButtonSprite::mouseReleaseEvent(QMouseEvent *event)
0176 {
0177     QPointF p = mapFromScene(QPointF(event->pos()));
0178     // qCDebug(KFOURINLINE_LOG) << "ButtonSprite::mouseReleaseEvent contains?"<< contains(p);
0179     if (!contains(p)) {
0180         mHover = false;
0181         if (mPushButton)
0182             mButtonPressed = false;
0183     } else {
0184         if (!mPushButton) {
0185             mButtonPressed = !mButtonPressed;
0186             if (mButtonPressed)
0187                 mSignal->emitSignal(number() | 0x100);
0188             else
0189                 mSignal->emitSignal(number());
0190 
0191         } else
0192             mButtonPressed = false;
0193     }
0194     changeFrame();
0195 }