File indexing completed on 2024-04-14 04:02:25

0001 /*
0002     This file is part of the KDE games lskat 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 "scoresprite.h"
0009 #include "lskat_debug.h"
0010 
0011 // General includes
0012 #include <math.h>
0013 
0014 // Qt includes
0015 #include <QFont>
0016 #include <QGraphicsScene>
0017 
0018 // KF includes
0019 #include <KConfig>
0020 #include <KConfigGroup>
0021 #include <KLocalizedString>
0022 
0023 // Constructor for the score sprite
0024 ScoreSprite::ScoreSprite(const QString &id, ThemeManager *theme, int advancePeriod, int no, QGraphicsScene *scene)
0025            :  Themable(id, theme), PixmapSprite(advancePeriod, no, scene)
0026 {
0027     // Create all sub sprites
0028     mName    = new QGraphicsTextItem(this);
0029     mPoints  = new QGraphicsTextItem(this);
0030     mScore   = new QGraphicsTextItem(this);
0031     mGames   = new QGraphicsTextItem(this);
0032     mInput   = new PixmapSprite(QStringLiteral("scoreinput"), theme, advancePeriod, 0, scene);
0033     if (!mInput) qCCritical(LSKAT_LOG) << "Cannot load sprite " << "scoreinput";
0034     mInput->setParentItem(this);
0035     mInput->setOffsetStatus(false);
0036     mInputFrame = 0;
0037 
0038     mTrump   = new PixmapSprite(QStringLiteral("scoretrump"), theme, advancePeriod, 0, scene);
0039     if (!mTrump) qCCritical(LSKAT_LOG) << "Cannot load sprite " << "scoretrump";
0040     mTrump->setParentItem(this);
0041     mTrump->setOffsetStatus(false);
0042     mTrumpFrame = 0;
0043 
0044     // Redraw us
0045     if (theme) theme->updateTheme(this);
0046 }
0047 
0048 // Destructor
0049 ScoreSprite::~ScoreSprite()
0050 {
0051     // Clean up
0052     delete mName;
0053     delete mPoints;
0054     delete mScore;
0055     delete mGames;
0056     delete mInput;
0057     delete mTrump;
0058 }
0059 
0060 // Redraw the theme.
0061 void ScoreSprite::changeTheme()
0062 {
0063     // The main display is handled by the parent
0064     PixmapSprite::changeTheme();
0065 
0066     // Retrieve our size
0067     double width  = this->boundingRect().width();
0068     double height = this->boundingRect().height();
0069 
0070     // Retrieve theme data
0071     KConfigGroup config = thememanager()->config(id());
0072     QPointF posName     = config.readEntry("posName", QPointF(1.0, 1.0));
0073     QPointF posPoints   = config.readEntry("posPoints", QPointF(1.0, 1.0));
0074     QPointF posScore    = config.readEntry("posScore", QPointF(1.0, 1.0));
0075     QPointF posGames    = config.readEntry("posGames", QPointF(1.0, 1.0));
0076 
0077     // Calculate proper font size
0078     double fontHeightPoints = config.readEntry("fontHeightPoints", 1.0);
0079     fontHeightPoints       *= height;
0080     double fontHeight       = config.readEntry("fontHeight", 1.0);
0081     fontHeight             *= height;
0082     double fontWidthName;
0083     double fontWidthPoints;
0084 
0085     if (!config.hasKey("fontWidthName") && config.hasKey("fontWidthUpper"))
0086     {
0087         qCWarning(LSKAT_LOG) << "Theme uses obsolete \"fontWidthUpper\" instead of \"fontWidthName\"";
0088         fontWidthName = config.readEntry("fontWidthUpper", 1.0);
0089     }
0090     else
0091     {
0092         fontWidthName = config.readEntry("fontWidthName", 1.0);
0093     }
0094 
0095     if (!config.hasKey("fontWidthPoints") && config.hasKey("fontWidthUpper"))
0096     {
0097         qCWarning(LSKAT_LOG) << "Theme uses obsolete \"fontWidthUpper\" instead of \"fontWidthPoints\"";
0098         fontWidthPoints = config.readEntry("fontWidthUpper", 1.0);
0099     }
0100     else
0101     {
0102         fontWidthPoints = config.readEntry("fontWidthPoints", 1.0);
0103     }
0104 
0105     double fontWidthLower   = config.readEntry("fontWidthLower", 1.0);
0106     fontWidthName          *= width;
0107     fontWidthPoints        *= width;
0108     fontWidthLower         *= width;
0109 
0110     // Retrieve font color
0111     QColor fontColor;
0112     fontColor = config.readEntry("fontColorPlayer", QColor(Qt::white));
0113 
0114     // Set position of sub sprites
0115     mName->setPos(posName.x() * width, posName.y() * height);
0116     mPoints->setPos(posPoints.x() * width, posPoints.y() * height);
0117     mScore->setPos(posScore.x() * width, posScore.y() * height);
0118     mGames->setPos(posGames.x() * width, posGames.y() * height);
0119 
0120     // Create and set current font
0121     QFont fontPoints;
0122     fontPoints.setPixelSize(int(fontHeightPoints));
0123     QFont font;
0124     font.setPixelSize(int(fontHeight));
0125 
0126     // Set font and color for all text items
0127     mName->setFont(font);
0128     mPoints->setFont(fontPoints);
0129     mScore->setFont(font);
0130     mGames->setFont(font);
0131 
0132     mName->setDefaultTextColor(fontColor);
0133     mPoints->setDefaultTextColor(fontColor);
0134     mScore->setDefaultTextColor(fontColor);
0135     mGames->setDefaultTextColor(fontColor);
0136 
0137     mName->setTextWidth(fontWidthName);
0138     mPoints->setTextWidth(fontWidthPoints);
0139     mScore->setTextWidth(fontWidthLower);
0140     mGames->setTextWidth(fontWidthLower);
0141 
0142     // Restore the frame of the input device sprite
0143     if (mInputFrame >= 0) mInput->setFrame(mInputFrame);
0144 }
0145 
0146 // QGI advance method
0147 void ScoreSprite::advance(int phase)
0148 {
0149     // Advance time and animation etc
0150     PixmapSprite::advance(phase);
0151 }
0152 
0153 // Store and display the name of a player
0154 void ScoreSprite::setPlayerName(const QString &s)
0155 {
0156     mName->setPlainText(s);
0157     update();
0158 }
0159 
0160 // Store and display amount of points
0161 void ScoreSprite::setPoints(int points)
0162 {
0163     QString s = QStringLiteral("%1").arg(points, 3);
0164     mPoints->setPlainText(s);
0165     update();
0166 }
0167 
0168 // Store and display score
0169 void ScoreSprite::setScore(int score)
0170 {
0171     QString s = i18nc("Score in score widget", "Score: %1", score);
0172     mScore->setPlainText(s);
0173     update();
0174 }
0175 
0176 // Store and display amount of games
0177 void ScoreSprite::setGames(int won, int all)
0178 {
0179     QString s = i18nc("Won and overall games in score widget", "Games: %1 / %2", won, all);
0180     mGames->setPlainText(s);
0181     update();
0182 }
0183 
0184 // Store and display input device
0185 void ScoreSprite::setInput(int device)
0186 {
0187     mInputFrame = device;
0188     mInput->setFrame(mInputFrame);
0189     mInput->show();
0190     update();
0191 }
0192 
0193 // Store and display trump icon
0194 void ScoreSprite::setTrump(int suite)
0195 {
0196     mTrumpFrame = suite;
0197     mTrump->setFrame(mTrumpFrame);
0198     mTrump->show();
0199     update();
0200 }