File indexing completed on 2024-07-21 03:49:35

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 "textsprite.h"
0009 #include "lskat_debug.h"
0010 
0011 // Qt includes
0012 #include <QGraphicsScene>
0013 #include <QFont>
0014 // KF includes
0015 #include <KConfigGroup>
0016 
0017 // Constructor for the sprite
0018 TextSprite::TextSprite(const QString &text, const QString &id, ThemeManager *theme, QGraphicsScene *scene)
0019           : Themable(id, theme), QGraphicsTextItem(nullptr)
0020 {
0021     scene->addItem(this);
0022     setPlainText(text);
0023     hide();
0024 
0025     if (theme) theme->updateTheme(this);
0026 }
0027 
0028 // Constructor for the sprite
0029 TextSprite::TextSprite(const QString &id, ThemeManager *theme, QGraphicsScene *scene)
0030           : Themable(id, theme), QGraphicsTextItem(nullptr)
0031 {
0032     scene->addItem(this);
0033     hide();
0034 
0035     if (theme) theme->updateTheme(this);
0036 }
0037 
0038 void TextSprite::setText(const QString &text)
0039 {
0040     setPlainText(text);
0041     thememanager()->updateTheme(this);
0042 }
0043 
0044 // Main themable function. Called for any theme change. The sprites needs to
0045 // resize and redraw here.
0046 void TextSprite::changeTheme()
0047 {
0048     // Get scaling change
0049     double scale    = thememanager()->getScale();
0050     Themable::setScale(scale);
0051 
0052     // Retrieve theme data from configuration
0053     KConfigGroup config = thememanager()->config(id());
0054     QPoint offset = thememanager()->getOffset();
0055 
0056     // Size
0057     double width  = config.readEntry("width", 1.0);
0058     double height = config.readEntry("height", 0.0);
0059     width *= scale;
0060     height *= scale;
0061 
0062     // Position
0063     QPointF pos = config.readEntry("pos", QPointF(1.0, 1.0));
0064     pos *= scale;
0065     setPos(pos.x(), pos.y());
0066 
0067     // z-Value
0068     double zValue = config.readEntry("zValue", 0.0);
0069     setZValue(zValue);
0070 
0071     // Text font
0072     bool bold = config.readEntry("bold", false);
0073     QFont font;
0074     font.setPixelSize(int(height));
0075     font.setBold(bold);
0076     setFont(font);
0077     if (width < boundingRect().width())
0078     {
0079         setTextWidth(width);
0080     }
0081 
0082     // Retrieve font color
0083     QColor fontColor;
0084     fontColor = config.readEntry("fontColor", QColor(Qt::white));
0085     setDefaultTextColor(fontColor);
0086 
0087     // Centering
0088     bool center = config.readEntry("center", false);
0089     resetTransform();
0090     if (center)
0091     {
0092         setTransform(QTransform::fromTranslate(-boundingRect().width() / 2.0 + offset.x(), offset.y()), true);
0093     }
0094     else
0095     {
0096         setTransform(QTransform::fromTranslate(offset.x(), offset.y()), true);
0097     }
0098 
0099     update();
0100 }