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

0001 // Copyright (c) 2002-2004 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 <QVBoxLayout>
0020 #include <QApplication>
0021 #include <QStyle>
0022 #include <QFontDatabase>
0023 #include <QPaintEvent>
0024 #include <QResizeEvent>
0025 
0026 #include <estate.h>
0027 #include <estategroup.h>
0028 #include <player.h>
0029 
0030 #include "estatedetailsbase.h"
0031 
0032 static const int StaticTitleHeight = 50;
0033 
0034 EstateDetailsBase::EstateDetailsBase(Estate *estate, QWidget *parent)
0035     : QWidget(parent)
0036     , m_estate(nullptr)
0037     , m_pixmap(nullptr)
0038     , m_quartzBlocks(nullptr)
0039     , b_recreate(true)
0040     , m_recreateQuartz(true)
0041 {
0042     setAutoFillBackground(true);
0043 
0044     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0045 
0046     mainLayout->addItem(new QSpacerItem(5, StaticTitleHeight, QSizePolicy::Fixed, QSizePolicy::Minimum));
0047 
0048     m_widget = new QWidget(this);
0049     mainLayout->addWidget(m_widget);
0050 
0051     setEstate(estate);
0052 
0053     // If 'estate' is not null, setEstate initialized the palette with
0054     // its color
0055     if (!estate)
0056     {
0057         QPalette pal = palette();
0058         pal.setColor(backgroundRole(), Qt::white);
0059         setPalette(pal);
0060     }
0061 }
0062 
0063 EstateDetailsBase::~EstateDetailsBase()
0064 {
0065     delete m_pixmap;
0066     delete m_quartzBlocks;
0067 }
0068 
0069 void EstateDetailsBase::paintEvent(QPaintEvent *e)
0070 {
0071     if (m_recreateQuartz)
0072     {
0073 /*
0074         if (m_quartzBlocks)
0075         {
0076             delete m_quartzBlocks;
0077             m_quartzBlocks = nullptr;
0078         }
0079 
0080         if (m_estate->color().isValid())
0081         {
0082             m_quartzBlocks = new QPixmap();
0083 
0084             if (m_orientation == North || m_orientation == South)
0085                 m_quartzBlocks->resize(25, m_titleHeight-2);
0086             else
0087                 m_quartzBlocks->resize(25, m_titleWidth-2);
0088 
0089             drawQuartzBlocks(m_quartzBlocks, *m_quartzBlocks, m_estate->color().light(60), m_estate->color());
0090             m_quartzBlocks = rotatePixmap(m_quartzBlocks);
0091         }
0092 */
0093         m_recreateQuartz = false;
0094         b_recreate = true;
0095     }
0096 
0097     if (b_recreate)
0098     {
0099         delete m_pixmap;
0100         m_pixmap = nullptr;
0101 
0102         if (m_estate)
0103         {
0104             m_pixmap = new QPixmap(width(), StaticTitleHeight);
0105 
0106             QColor greenHouse(0, 255, 0);
0107             QColor redHotel(255, 51, 51);
0108             QPainter painter;
0109             painter.begin(m_pixmap);
0110 
0111             painter.setPen(Qt::black);
0112 
0113             QColor titleColor = (m_estate->color().isValid() ? m_estate->color() : m_estate->bgColor().lighter(80));
0114             const int leftMarginHint = QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
0115             const int topMarginHint = QApplication::style()->pixelMetric(QStyle::PM_LayoutTopMargin);
0116             const int spacingHint = QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
0117             const QFont generalFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0118 
0119             painter.setBrush(titleColor);
0120             painter.drawRect(0, 0, width()-1, StaticTitleHeight-1);
0121 
0122             if (m_quartzBlocks)
0123             {
0124                 painter.drawPixmap(1, 1, *m_quartzBlocks);
0125             }
0126 
0127             if (m_estate->houses() > 0)
0128             {
0129                 int titleWidth = width() / 5;
0130 
0131                 if (m_estate->houses() == 5)
0132                 {
0133                     // Hotel
0134                     painter.setBrush(redHotel);
0135                     painter.drawRect(2, 2, titleWidth-4-1, StaticTitleHeight-4-1);
0136                 }
0137                 else
0138                 {
0139                     // Houses
0140                     painter.setBrush(greenHouse);
0141                         int h = StaticTitleHeight-4, w = titleWidth-4;
0142                         for ( unsigned int i=0 ; i < m_estate->houses() ; i++ )
0143                         painter.drawRect(2+(i*(w+2)), 2, w-1, h-1);
0144                 }
0145             }
0146 
0147             // TODO: steal blur code from kicker/taskbar/taskcontainer.cpp
0148 
0149             // Estate name
0150             painter.setPen(Qt::white);
0151             int fontSize = generalFont.pointSize();
0152             if (fontSize == -1)
0153                 fontSize = generalFont.pixelSize();
0154 
0155             painter.setFont(QFont(generalFont.family(), fontSize * 2, QFont::Bold));
0156             painter.drawText(leftMarginHint, topMarginHint, width()-leftMarginHint, StaticTitleHeight, Qt::AlignJustify, m_estate->name());
0157 
0158             painter.setPen(Qt::black);
0159 
0160             int xText = 0;
0161 
0162             // Estate group
0163             if (m_estate->estateGroup())
0164             {
0165                 xText = StaticTitleHeight - fontSize - spacingHint;
0166                 painter.setFont(QFont(generalFont.family(), fontSize, QFont::Bold));
0167                 painter.drawText(5, xText, width()-10, StaticTitleHeight, Qt::AlignRight, m_estate->estateGroup()->name().toUpper());
0168             }
0169 
0170             xText = StaticTitleHeight + fontSize + 5;
0171             painter.setFont(QFont(generalFont.family(), fontSize, QFont::Normal));
0172         }
0173         b_recreate = false;
0174 
0175     }
0176     QPainter painter(this);
0177     // Draw the border, as simple polyline
0178     painter.setPen(Qt::black);
0179     const QPointF points[5] = {
0180         QPointF(0, 0),
0181         QPointF(0, height() - 1),
0182         QPointF(width() - 1, height() - 1),
0183         QPointF(width() - 1, 0),
0184         QPointF(0, 0),
0185     };
0186     painter.drawPolyline(points, sizeof(points)/sizeof(points[0]));
0187     // Draw the title, if its area was damaged
0188     if (m_pixmap && e->rect().intersects(QRect(0, 0, width(), StaticTitleHeight)))
0189         painter.drawPixmap(0, 0, *m_pixmap);
0190 }
0191 
0192 void EstateDetailsBase::resizeEvent(QResizeEvent *e)
0193 {
0194     if (e->oldSize().width() != e->size().width())
0195     {
0196         m_recreateQuartz = true;
0197         b_recreate = true;
0198     }
0199 }
0200 
0201 void EstateDetailsBase::setEstate(Estate *estate)
0202 {
0203     if (m_estate != estate)
0204     {
0205         m_estate = estate;
0206 
0207         QPalette pal = palette();
0208         pal.setColor(backgroundRole(), m_estate ? m_estate->bgColor() : Qt::white);
0209         setPalette(pal);
0210 
0211         b_recreate = true;
0212         update();
0213     }
0214 }
0215 
0216 #include "moc_estatedetailsbase.cpp"