File indexing completed on 2024-04-28 04:03:13
0001 /*************************************************************************** 0002 File : knightsview.cpp 0003 Project : Knights 0004 Description : Main view of Knights 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2016-2017 Alexander Semke (alexander.semke@web.de) 0007 SPDX-FileCopyrightText: 2009-2011 Miha Čančula (miha@noughmad.eu) 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * SPDX-License-Identifier: GPL-2.0-or-later 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program; if not, write to the Free Software * 0022 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0023 * Boston, MA 02110-1301 USA * 0024 * * 0025 ***************************************************************************/ 0026 0027 #include "knightsview.h" 0028 #include "proto/protocol.h" 0029 #include "board.h" 0030 #include "gamemanager.h" 0031 #include "knightsdebug.h" 0032 #include "offerwidget.h" 0033 0034 #include "ui_knightsview_base.h" 0035 #include "ui_popup.h" 0036 0037 using namespace Knights; 0038 0039 KnightsView::KnightsView(QWidget* parent) : QWidget(parent), 0040 ui(new Ui::KnightsView), 0041 m_board(nullptr), 0042 m_showAllOffers(false) { 0043 0044 ui->setupUi ( this ); 0045 0046 // By default, show only one offer (set showAll to true then toggle it) 0047 //TODO 0048 updateOffers(); 0049 0050 connect ( ui->showAllOffers, &QPushButton::clicked, this, &KnightsView::showAllOffersToggled ); 0051 connect ( Manager::self(), &Manager::notification, this, &KnightsView::showPopup ); 0052 } 0053 0054 KnightsView::~KnightsView() { 0055 delete ui; 0056 } 0057 0058 void KnightsView::drawBoard(KGameThemeProvider* provider) { 0059 m_board = new Board(provider, this); 0060 ui->canvas->setScene(m_board); 0061 resizeScene(); 0062 0063 Colors playerColors; 0064 playerColors |= White; 0065 playerColors |= Black; 0066 m_board->setPlayerColors(playerColors); 0067 } 0068 0069 void KnightsView::setupBoard(KGameThemeProvider* provider) { 0070 if (m_board) 0071 delete m_board; 0072 0073 m_board = new Board ( provider, this ); 0074 ui->canvas->setScene ( m_board ); 0075 resizeScene(); 0076 0077 connect ( Manager::self(), &Manager::pieceMoved, m_board, &Board::movePiece ); 0078 connect ( Manager::self(), &Manager::activePlayerChanged, m_board, &Board::setCurrentColor ); 0079 connect ( m_board, &Board::displayedPlayerChanged, this, &KnightsView::displayedPlayerChanged ); 0080 connect ( m_board, &Board::pieceMoved, Manager::self(), &Manager::moveByBoard ); 0081 0082 Colors playerColors; 0083 if ( Protocol::white()->isLocal() ) 0084 playerColors |= White; 0085 if ( Protocol::black()->isLocal() ) 0086 playerColors |= Black; 0087 m_board->setPlayerColors(playerColors); 0088 } 0089 0090 void KnightsView::settingsChanged() { 0091 Q_EMIT signalChangeStatusbar ( i18n ( "Settings changed" ) ); 0092 if (m_board) 0093 m_board->updateTheme(); 0094 } 0095 0096 void KnightsView::resizeEvent(QResizeEvent* e) { 0097 Q_UNUSED(e) 0098 resizeScene(); 0099 } 0100 0101 void KnightsView::resizeScene() { 0102 if ( ui->canvas && m_board ) { 0103 m_board->setSceneRect ( ui->canvas->contentsRect() ); 0104 m_board->updateGraphics(); 0105 ui->canvas->setTransform ( QTransform() ); 0106 } 0107 } 0108 0109 void KnightsView::showPopup(const Offer& offer) { 0110 OfferWidget* widget = new OfferWidget(offer, ui->notificationWidget); 0111 connect ( widget, &OfferWidget::close, Manager::self(), &Manager::setOfferResult ); 0112 connect ( widget, &OfferWidget::close, this, &KnightsView::popupHidden ); 0113 m_offerWidgets << widget; 0114 updateOffers(); 0115 } 0116 0117 void KnightsView::showAllOffersToggled() { 0118 m_showAllOffers = !m_showAllOffers; 0119 updateOffers(); 0120 } 0121 0122 void KnightsView::popupHidden(int id) { 0123 qCDebug(LOG_KNIGHTS) << m_offerWidgets << id << m_showAllOffers; 0124 for (OfferWidget* widget : std::as_const(m_offerWidgets)) { 0125 if ( widget->id() == id ) 0126 m_offerWidgets.removeAll(widget); 0127 } 0128 updateOffers(); 0129 } 0130 0131 void KnightsView::updateOffers() { 0132 if ( m_offerWidgets.isEmpty() ) { 0133 ui->notificationWidget->hide(); 0134 return; 0135 } 0136 QGridLayout* layout = qobject_cast<QGridLayout*>(ui->notificationWidget->layout()); 0137 if ( !layout ) 0138 return; 0139 ui->showAllOffers->setIcon ( QIcon::fromTheme(QLatin1String( m_showAllOffers ? "arrow-up-double" : "arrow-down-double" )) ); 0140 ui->showAllOffers->setVisible ( m_offerWidgets.size() > 1 ); 0141 for (OfferWidget* widget : std::as_const(m_offerWidgets)) { 0142 layout->removeWidget ( widget ); 0143 widget->hide(); 0144 } 0145 if ( m_showAllOffers ) { 0146 for ( int i = 0; i < m_offerWidgets.size(); ++i ) { 0147 layout->addWidget ( m_offerWidgets[i], i+1, 0 ); 0148 m_offerWidgets[i]->show(); 0149 } 0150 } else { 0151 layout->addWidget ( m_offerWidgets.last(), 1, 0 ); 0152 m_offerWidgets.last()->show(); 0153 } 0154 ui->notificationWidget->show(); 0155 } 0156 0157 #include "moc_knightsview.cpp"