File indexing completed on 2024-04-21 15:08:02

0001 // Copyright (c) 2002 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 <QColor>
0018 #include <qpainter.h>
0019 #include <QMouseEvent>
0020 #include <QPaintEvent>
0021 
0022 #include "portfolioestate.h"
0023 #include "estate.h"
0024 
0025 PortfolioEstate::PortfolioEstate(Estate *estate, Player *player, bool alwaysOwned, QWidget *parent)
0026     : QWidget(parent)
0027     , m_estate(estate)
0028     , m_player(player)
0029     , b_recreate(true)
0030     , m_alwaysOwned(alwaysOwned)
0031 {
0032     QSize s(PE_WIDTH, PE_HEIGHT);
0033     setFixedSize(s);
0034 
0035     // react to changes in the estate only when we do not draw it
0036     // as always owned
0037     if (!m_alwaysOwned)
0038     {
0039         m_estateColor = m_estate->color();
0040         m_estateOwner = m_estate->owner();
0041         connect(m_estate, SIGNAL(changed()), this, SLOT(estateChanged()));
0042     }
0043 }
0044 
0045 void PortfolioEstate::estateChanged()
0046 {
0047     // if we get here, then we are not assuming the estate is
0048     // always owned
0049 
0050     // update if the color changed
0051     if (m_estate->color() != m_estateColor)
0052     {
0053         m_estateColor = m_estate->color();
0054         b_recreate = true;
0055     }
0056 
0057     // update if the owner changed ...
0058     if (m_estate->owner() != m_estateOwner)
0059     {
0060         Player *prevOwner = m_estateOwner;
0061         m_estateOwner = m_estate->owner();
0062 
0063         // ... but only if the previous owner or the new one
0064         // is the own player
0065         if (m_estate->owner() == m_player || prevOwner == m_player)
0066             b_recreate = true;
0067     }
0068 
0069     if (b_recreate)
0070         update();
0071 }
0072 
0073 QPixmap PortfolioEstate::drawPixmap(Estate *estate, Player *player, bool alwaysOwned)
0074 {
0075     QColor lightGray(204, 204, 204), darkGray(153, 153, 153);
0076     QPixmap qpixmap(PE_WIDTH, PE_HEIGHT);
0077     QPainter painter;
0078     painter.begin(&qpixmap);
0079 
0080     painter.setPen(lightGray);
0081     painter.setBrush(Qt::white);
0082     painter.drawRect(QRect(0, 0, PE_WIDTH-1 , PE_HEIGHT-1));
0083     if (alwaysOwned || (estate && estate->isOwned() && player == estate->owner()))
0084     {
0085         painter.setPen(darkGray);
0086         for (int y=5;y<=13;y+=2)
0087             painter.drawLine(2, y, 10, y);
0088 
0089         painter.setPen(Qt::white);
0090         painter.drawPoint(8, 5);
0091         painter.drawPoint(8, 7);
0092         painter.drawPoint(8, 9);
0093         painter.drawPoint(5, 11);
0094         painter.drawPoint(9, 11);
0095         painter.drawPoint(3, 13);
0096         painter.drawPoint(10, 13);
0097 
0098         painter.setPen(estate->color());
0099         painter.setBrush(estate->color());
0100     }
0101     else
0102     {
0103         painter.setPen(lightGray);
0104         painter.setBrush(lightGray);
0105     }
0106     painter.drawRect(0, 0, PE_WIDTH-1, 3-1);
0107 
0108     return qpixmap;
0109 }
0110 
0111 void PortfolioEstate::paintEvent(QPaintEvent *e)
0112 {
0113     if (b_recreate)
0114     {
0115         m_pixmap = drawPixmap(m_estate, m_player, m_alwaysOwned);
0116         b_recreate = false;
0117     }
0118     QPainter painter(this);
0119     painter.drawPixmap(e->rect(), m_pixmap, e->rect());
0120 }
0121 
0122 void PortfolioEstate::mousePressEvent(QMouseEvent *e)
0123 {
0124     if (e->button()==Qt::LeftButton)
0125         Q_EMIT estateClicked(m_estate);
0126 }
0127 
0128 #include "moc_portfolioestate.cpp"