File indexing completed on 2024-04-21 15:08:01
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 <QSpinBox> 0018 #include <QLabel> 0019 #include <QVBoxLayout> 0020 #include <QGroupBox> 0021 #include <QHeaderView> 0022 #include <QTreeWidget> 0023 #include <QPushButton> 0024 0025 #include <kiconloader.h> 0026 #include <klocalizedstring.h> 0027 0028 #include <atlantic_core.h> 0029 #include <player.h> 0030 #include <estate.h> 0031 #include <auction.h> 0032 0033 #include "auction_widget.h" 0034 0035 #include <ui_auction.h> 0036 0037 AuctionWidget::AuctionWidget(AtlanticCore *atlanticCore, Auction *auction, QWidget *parent) 0038 : EstateDetailsBase(auction->estate(), parent) 0039 , m_ui(new Ui::AuctionWidget) 0040 , m_atlanticCore(atlanticCore) 0041 , m_auction(auction) 0042 { 0043 connect(m_auction, SIGNAL(changed()), this, SLOT(auctionChanged())); 0044 connect(m_auction, SIGNAL(updateBid(Player *, int)), this, SLOT(updateBid(Player *, int))); 0045 connect(this, SIGNAL(bid(Auction *, int)), m_auction, SIGNAL(bid(Auction *, int))); 0046 0047 m_ui->setupUi(widget()); 0048 widget()->layout()->setContentsMargins(0, 0, 0, 0); 0049 0050 // Player list 0051 m_ui->playerList->header()->setSectionsClickable(false); 0052 m_ui->playerList->header()->setSectionResizeMode(QHeaderView::ResizeToContents); 0053 0054 QList<QTreeWidgetItem *> items; 0055 Player *pSelf = m_atlanticCore->playerSelf(); 0056 0057 foreach (Player *player, m_atlanticCore->players()) 0058 { 0059 if (player->game() == pSelf->game() && !player->isSpectator()) 0060 items << createPlayerItem(player); 0061 0062 // Monitor all the players, as they could join this auction 0063 connect(player, SIGNAL(changed(Player *)), this, SLOT(playerChanged(Player *))); 0064 } 0065 m_ui->playerList->addTopLevelItems(items); 0066 0067 // Bid spinbox and button 0068 connect(m_ui->bidButton, SIGNAL(clicked()), this, SLOT(slotBidButtonClicked())); 0069 0070 // Monitor for players added/removed 0071 connect(m_atlanticCore, SIGNAL(createGUI(Player *)), this, SLOT(playerCreated(Player *))); 0072 connect(m_atlanticCore, SIGNAL(removeGUI(Player *)), this, SLOT(playerRemoved(Player *))); 0073 } 0074 0075 AuctionWidget::~AuctionWidget() 0076 { 0077 delete m_ui; 0078 } 0079 0080 void AuctionWidget::auctionChanged() 0081 { 0082 QString status; 0083 switch (m_auction->status()) 0084 { 0085 case 1: 0086 status = i18n("Going once..."); 0087 break; 0088 0089 case 2: 0090 status = i18n("Going twice..."); 0091 break; 0092 0093 case 3: 0094 status = i18n("Sold!"); 0095 break; 0096 0097 default: 0098 status.clear(); 0099 } 0100 m_ui->statusLabel->setText(status); 0101 } 0102 0103 void AuctionWidget::playerChanged(Player *player) 0104 { 0105 if (!player) 0106 return; 0107 0108 Game *pSelfGame = m_atlanticCore->playerSelf()->game(); 0109 QTreeWidgetItem *item = m_playerItems.value(player, nullptr); 0110 0111 if (item) 0112 { 0113 // There was an item for this player, so the player was 0114 // already in the game 0115 0116 if (player->game() != pSelfGame) 0117 { 0118 // The player is no more in the game 0119 delete item; 0120 m_playerItems.remove(player); 0121 } 0122 else 0123 { 0124 // The player is still in the game 0125 item->setText(0, player->name()); 0126 } 0127 } 0128 else if ((player->game() == pSelfGame) && !player->isSpectator()) 0129 { 0130 // The player was not already in the game, but it is now, 0131 // and they are not a spectator -- add it 0132 m_ui->playerList->addTopLevelItem(createPlayerItem(player)); 0133 } 0134 } 0135 0136 void AuctionWidget::playerCreated(Player *player) 0137 { 0138 connect(player, SIGNAL(changed(Player *)), this, SLOT(playerChanged(Player *))); 0139 0140 if (player->game() != m_atlanticCore->playerSelf()->game()) 0141 return; 0142 0143 m_ui->playerList->addTopLevelItem(createPlayerItem(player)); 0144 } 0145 0146 void AuctionWidget::playerRemoved(Player *player) 0147 { 0148 delete m_playerItems.take(player); 0149 } 0150 0151 void AuctionWidget::updateBid(Player *player, int amount) 0152 { 0153 if (!player) 0154 return; 0155 0156 QTreeWidgetItem *item; 0157 if (!(item = m_playerItems.value(player, nullptr))) 0158 return; 0159 0160 item->setText(1, QString::number(amount)); 0161 m_ui->bidSpinBox->setMinimum(amount+1); 0162 } 0163 0164 void AuctionWidget::slotBidButtonClicked() 0165 { 0166 Q_EMIT bid(m_auction, m_ui->bidSpinBox->value()); 0167 } 0168 0169 QTreeWidgetItem *AuctionWidget::createPlayerItem(Player *player) 0170 { 0171 QTreeWidgetItem *item = new QTreeWidgetItem(); 0172 item->setText(0, player->name()); 0173 item->setText(1, QStringLiteral("0")); 0174 item->setIcon(0, KDE::icon(QStringLiteral("user-identity"))); 0175 m_playerItems.insert(player, item); 0176 return item; 0177 } 0178 0179 #include "moc_auction_widget.cpp"