File indexing completed on 2024-03-24 15:43:20

0001 // Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com>
0002 //
0003 // This library is free software; you can redistribute it and/or
0004 // modify it under the terms of the GNU Lesser General Public
0005 // License version 2.1 as published by the Free Software Foundation.
0006 //
0007 // This library is distributed in the hope that it will be useful,
0008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0010 // Lesser General Public License for more details.
0011 //
0012 // You should have received a copy of the GNU Lesser General Public License
0013 // along with this library; see the file COPYING.LIB.  If not, write to
0014 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0015 // Boston, MA 02110-1301, USA.
0016 
0017 #include <qpainter.h>
0018 #include <qpixmap.h>
0019 #include <QFont>
0020 #include <QFile>
0021 #include <QFontDatabase>
0022 #include <QPaintEvent>
0023 
0024 #include "board.h"
0025 #include "estate.h"
0026 #include "player.h"
0027 
0028 #include "token.h"
0029 
0030 #define ICONSIZE    32
0031 
0032 Token::Token(Player *player, AtlantikBoard *parent)
0033     : QWidget(parent)
0034     , m_player(player)
0035     , m_location(m_player->location())
0036     , m_destination(nullptr)
0037     , m_inJail(m_player->inJail())
0038     , m_parentBoard(parent)
0039     , b_recreate(true)
0040     , qpixmap(nullptr)
0041     , m_image(nullptr)
0042     , m_playerName(m_player->name())
0043 {
0044         setAttribute(Qt::WA_NoSystemBackground, true);
0045 
0046     connect(m_player, SIGNAL(changed(Player *)), this, SLOT(playerChanged()));
0047 
0048     // Init icon
0049     loadIcon();
0050 
0051     setFixedSize(QSize(ICONSIZE, ICONSIZE + QFontDatabase::systemFont(QFontDatabase::GeneralFont).pointSize()));
0052 }
0053 
0054 Token::~Token()
0055 {
0056     delete m_image;
0057     delete qpixmap;
0058 }
0059 
0060 Player *Token::player() const
0061 {
0062     return m_player;
0063 }
0064 
0065 void Token::setLocation(Estate *location)
0066 {
0067     if (m_location != location)
0068         m_location = location;
0069 }
0070 
0071 void Token::setDestination(Estate *estateView)
0072 {
0073     if (m_destination != estateView)
0074         m_destination = estateView;
0075 }
0076 
0077 void Token::setTokenTheme(const TokenTheme &theme)
0078 {
0079     m_theme = theme;
0080     loadIcon();
0081     b_recreate = true;
0082     update();
0083 }
0084 
0085 void Token::playerChanged()
0086 {
0087     if (m_imageName != m_player->image())
0088     {
0089         loadIcon();
0090         b_recreate = true;
0091     }
0092     if (m_playerName != m_player->name())
0093     {
0094         m_playerName = m_player->name();
0095         b_recreate = true;
0096     }
0097 
0098     if (b_recreate)
0099         update();
0100 }
0101 
0102 void Token::loadIcon()
0103 {
0104     m_imageName = m_player->image();
0105 
0106     delete m_image;
0107     m_image = nullptr;
0108 
0109     if (!m_theme.isValid())
0110         return;
0111 
0112     QString imageFile;
0113     if (!m_imageName.isEmpty())
0114         imageFile = m_theme.tokenPath(m_imageName);
0115     if (imageFile.isEmpty())
0116         imageFile = m_theme.tokenPath(m_theme.fallbackIcon());
0117 
0118     const QPixmap pix(imageFile);
0119     if (pix.isNull())
0120         return;
0121 
0122     m_image = new QPixmap(pix.scaled(ICONSIZE, ICONSIZE, Qt::KeepAspectRatio));
0123 }
0124 
0125 void Token::paintEvent(QPaintEvent *e)
0126 {
0127     if (b_recreate)
0128     {
0129         const QFont generalFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0130         if (!qpixmap || qpixmap->size() != size())
0131         {
0132             delete qpixmap;
0133             qpixmap = new QPixmap(width(), height());
0134         }
0135 
0136         QPainter painter;
0137         painter.begin(qpixmap );
0138 
0139         if (m_image)
0140         {
0141             painter.setPen(Qt::black);
0142             painter.setBrush(Qt::white);
0143             painter.drawRect(0, 0, ICONSIZE-1, ICONSIZE-1);
0144 
0145             painter.drawPixmap((ICONSIZE - m_image->width()) / 2, (ICONSIZE - m_image->height()) / 2, *m_image);
0146         }
0147 
0148         painter.setPen(Qt::black);
0149         painter.setBrush(Qt::black);
0150         painter.drawRect(0, ICONSIZE, width()-1, generalFont.pointSize()-1);
0151 
0152         painter.setPen(Qt::white);
0153         painter.setFont(QFont(generalFont.family(), generalFont.pointSize(), QFont::DemiBold));
0154         painter.drawText(1, height()-1, m_playerName);
0155 
0156         b_recreate = false;
0157     }
0158     QPainter painter(this);
0159     painter.drawPixmap(e->rect(), *qpixmap, e->rect());
0160 }
0161 
0162 void Token::resizeEvent(QResizeEvent *)
0163 {
0164     b_recreate = true;
0165 }
0166 
0167 #include "moc_token.cpp"