File indexing completed on 2024-09-29 06:37:50

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