File indexing completed on 2024-04-14 14:32:01

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 <QHBoxLayout>
0018 #include <QListWidget>
0019 #include <QPushButton>
0020 #include <QApplication>
0021 #include <QStyle>
0022 #include <QFontDatabase>
0023 
0024 #include <kiconloader.h>
0025 #include <klocalizedstring.h>
0026 #include <KStandardGuiItem>
0027 
0028 #include <estate.h>
0029 #include <player.h>
0030 
0031 #include "estatedetails.h"
0032 
0033 static QIcon iconForCommandButton(const QString &command)
0034 {
0035     if (command == QLatin1String(".r"))
0036         return KDE::icon(QStringLiteral("roll"));
0037     if (command == QLatin1String(".eb"))
0038         return KDE::icon(QStringLiteral("atlantik_buy_estate"));
0039     if (command == QLatin1String(".ea"))
0040         return KDE::icon(QStringLiteral("auction"));
0041     if (command == QLatin1String(".E"))
0042         return KDE::icon(QStringLiteral("games-endturn"));
0043     if (command == QLatin1String(".jp"))
0044         return KDE::icon(QStringLiteral("jail_pay"));
0045     return QIcon();
0046 }
0047 
0048 EstateDetails::EstateDetails(Estate *estate, const QString &text, QWidget *parent)
0049     : EstateDetailsBase(estate, parent)
0050     , m_closeButton(nullptr)
0051 {
0052     m_buttons.reserve(3); // Usually there are no more than 3 action buttons
0053 
0054     QWidget *w = widget();
0055 
0056     QVBoxLayout *mainLayout = new QVBoxLayout(w);
0057     mainLayout->setContentsMargins(0, 0, 0, 0);
0058 
0059     m_infoListView = new QListWidget(w);
0060     m_infoListView->setWordWrap(true);
0061     mainLayout->addWidget(m_infoListView);
0062 
0063     appendText(text);
0064 
0065     m_buttonBox = new QHBoxLayout();
0066     mainLayout->addItem(m_buttonBox);
0067 
0068     m_buttonBox->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
0069 }
0070 
0071 EstateDetails::~EstateDetails()
0072 {
0073     delete m_infoListView;
0074 }
0075 
0076 void EstateDetails::addDetails()
0077 {
0078     Estate *e = estate();
0079 
0080     if (e)
0081     {
0082         QListWidgetItem *infoText = nullptr;
0083         const QIcon infoIcon = KDE::icon(QStringLiteral("document-properties"));
0084 
0085         // Price
0086         if (e->price())
0087         {
0088             infoText = new QListWidgetItem();
0089             infoText->setText(i18n("Price: %1", e->price()));
0090             infoText->setIcon(infoIcon);
0091             m_infoListView->addItem(infoText);
0092         }
0093 
0094         // Owner, houses, isMortgaged
0095         if (e->canBeOwned())
0096         {
0097             infoText = new QListWidgetItem();
0098             infoText->setText(i18n("Owner: %1", e->owner() ? e->owner()->name() : i18n("unowned")));
0099             infoText->setIcon(infoIcon);
0100             m_infoListView->addItem(infoText);
0101 
0102             if (e->isOwned())
0103             {
0104                 infoText = new QListWidgetItem();
0105                 infoText->setText(i18n("Houses: %1", e->houses()));
0106                 infoText->setIcon(infoIcon);
0107                 m_infoListView->addItem(infoText);
0108 
0109                 infoText = new QListWidgetItem();
0110                 infoText->setText(i18n("Mortgaged: %1", e->isMortgaged() ? i18n("Yes") : i18n("No")));
0111                 infoText->setIcon(infoIcon);
0112                 m_infoListView->addItem(infoText);
0113             }
0114         }
0115     }
0116 }
0117 
0118 void EstateDetails::addButton(const QString &command, const QString &caption, bool enabled)
0119 {
0120     QPushButton *button = new QPushButton(iconForCommandButton(command), caption, widget());
0121     m_buttons.append(button);
0122     m_buttonBox->addWidget(button);
0123 
0124     Estate *e = estate();
0125 
0126     if (e)
0127     {
0128         QColor bgColor, fgColor;
0129         bgColor = e->bgColor().lighter(110);
0130         fgColor = ( bgColor.red() + bgColor.green() + bgColor.blue() < 255 ) ? Qt::white : Qt::black;
0131 
0132         QPalette pal = button->palette();
0133         pal.setColor( button->foregroundRole(), fgColor );
0134         pal.setColor( button->backgroundRole(), bgColor );
0135         button->setPalette( pal );
0136     }
0137     button->setEnabled(enabled);
0138     button->show();
0139 
0140     connect(button, &QPushButton::pressed, this, [this, command]() { buttonCommand(command); });
0141 }
0142 
0143 void EstateDetails::addCloseButton()
0144 {
0145     if (!m_closeButton)
0146     {
0147         m_closeButton = new QPushButton(widget());
0148         KStandardGuiItem::assign(m_closeButton, KStandardGuiItem::Close);
0149         m_buttonBox->addWidget(m_closeButton);
0150         m_closeButton->show();
0151         connect(m_closeButton, SIGNAL(pressed()), this, SIGNAL(buttonClose()));
0152     }
0153 }
0154 
0155 void EstateDetails::setText(const QString &text)
0156 {
0157     m_infoListView->clear();
0158     appendText(text);
0159 }
0160 
0161 void EstateDetails::appendText(const QString &text)
0162 {
0163     if ( text.isEmpty() )
0164         return;
0165 
0166     QListWidgetItem *infoText = new QListWidgetItem();
0167     infoText->setText(text);
0168     if (text.contains(QLatin1String(" rolls ")))
0169         infoText->setIcon(KDE::icon(QStringLiteral("roll")));
0170     else
0171         infoText->setIcon(KDE::icon(QStringLiteral("atlantik")));
0172     m_infoListView->addItem(infoText);
0173 
0174     m_infoListView->scrollToItem(infoText);
0175 }
0176 
0177 void EstateDetails::clearButtons()
0178 {
0179     if (m_closeButton)
0180     {
0181         delete m_closeButton;
0182         m_closeButton = nullptr;
0183     }
0184 
0185     // Delete buttons
0186     qDeleteAll(m_buttons);
0187     m_buttons.resize(0); // Keep the capacity allocated
0188 }
0189 
0190 #include "moc_estatedetails.cpp"