File indexing completed on 2024-04-28 07:53:28

0001 /* This file is part of KsirK.
0002    Copyright (C) 2001-2010 Gael de Chalendar <kleag@free.fr>
0003    Copyright (C) 2010 Cyril Tostivint <cyril@tostivint.com>
0004    
0005    KsirK is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation, either version 2
0008    of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; if not, write to the Free Software
0017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018    02110-1301, USA
0019 */
0020 
0021 /*  begin                : mer jui 11 22:27:28 EDT 2001   */
0022 
0023 // application specific includes
0024 #include "InvasionSlider.h"
0025 
0026 #include "kgamewin.h"
0027 #include "GameLogic/country.h"
0028 #include "ksirk_debug.h"
0029 
0030 //include files for QT
0031 #include <QGridLayout>
0032 #include <QHBoxLayout>
0033 #include <QVBoxLayout>
0034 #include <QSlider>
0035 #include <QLabel>
0036 #include <QDialogButtonBox>
0037 
0038 // include files for KDE
0039 #include <KLocalizedString>
0040 
0041 
0042 namespace Ksirk
0043 {
0044 using namespace GameLogic;
0045 
0046 // port all occurrences of m_accels
0047 // port all occurrences of setBarPos
0048 
0049 InvasionSlider::InvasionSlider(KGameWindow* game, GameLogic::Country * attack, GameLogic::Country * defender, InvasionType invasionType):m_game(game)
0050 {
0051   m_nbLArmy = attack->nbArmies();
0052   m_nbRArmy = defender->nbArmies();
0053 
0054   m_nbLArmies = new QLabel(QString::number(m_nbLArmy));
0055   m_nbRArmies = new QLabel(QString::number(m_nbRArmy));
0056 
0057   QVBoxLayout* dialogLayout = new QVBoxLayout(this);
0058   QDialogButtonBox* buttonBox = nullptr;
0059 
0060   if (invasionType == Invasion)
0061   {
0062     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0063   }
0064   else if (invasionType == Moving)
0065   {
0066     buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
0067   }
0068 
0069   QWidget* widget = new QWidget(this);
0070 
0071   //Infantery picture
0072   InfantrySprite *sprite = new InfantrySprite(1.0, game->backGnd());
0073   sprite-> setDestination(nullptr);             // Sprite immobile
0074   QPixmap soldat = sprite->image(0);
0075   soldat = soldat.scaled(QSize(24, 24) * soldat.devicePixelRatio(), Qt::KeepAspectRatioByExpanding);
0076   delete sprite;
0077   
0078   QLabel * nb = new QLabel();
0079   nb->setPixmap(soldat);
0080   nb->setFixedSize(35,35);
0081 
0082   m_invadeSlide = new QSlider(Qt::Horizontal,widget);
0083 
0084   m_invadeSlide->setTickInterval(1);
0085   m_invadeSlide->setMinimum(0);
0086   m_invadeSlide->setMaximum(attack->nbArmies()-1);
0087   m_invadeSlide->setTickPosition(QSlider::TicksBelow);
0088   m_currentSlideValue = m_invadeSlide->value();
0089 
0090   QGridLayout * wSlideLayout = new QGridLayout(widget);
0091   QHBoxLayout * center = new QHBoxLayout(); // remove parameter to avoid message "which already has a layout"
0092   QVBoxLayout * left = new QVBoxLayout();
0093   QVBoxLayout * right = new QVBoxLayout();
0094   
0095   //init. main layout
0096   if (invasionType == Invasion)
0097   {
0098     setWindowTitle(i18nc("@title:window", "Invasion"));
0099     wSlideLayout->addWidget(new QLabel(i18n("You are invading <font color=\"blue\">%1</font> with <font color=\"red\">%2</font>!", defender->i18name(), attack->i18name())),0,0);
0100     wSlideLayout->addWidget(new QLabel(i18n("<br><i>Choose the number of invading armies.</i>")),1,0);
0101   }
0102   else if (invasionType == Moving)
0103   {
0104     setWindowTitle(i18nc("@title:window", "Moving"));
0105     wSlideLayout->addWidget(new QLabel(i18n("You are moving armies from <font color=\"red\">%1</font> to <font color=\"blue\">%2</font>!", attack->i18name(), defender->i18name())),0,0);
0106     wSlideLayout->addWidget(new QLabel(i18n("<br><i>Choose the number of armies to move.</i>")),1,0);
0107   }
0108 
0109   wSlideLayout->addLayout(center,2,0);
0110   wSlideLayout->addWidget(m_invadeSlide,3,0);
0111 
0112   //init. center layout
0113   center->addLayout(left);
0114   center->addWidget(nb);
0115   center->addLayout(right);
0116 
0117   //init. left layout
0118   left->addWidget(new QLabel("<b>"+attack->i18name()+"</b>"),Qt::AlignCenter);
0119   left->addWidget(m_nbLArmies,Qt::AlignCenter);
0120 
0121   //init. right layout
0122   right->addWidget(new QLabel("<b>"+defender->i18name()+"</b>"),Qt::AlignCenter);
0123   right->addWidget(m_nbRArmies,Qt::AlignCenter);
0124 
0125   //val->setText(QString::number(invadeSlide->value()));
0126   connect(m_invadeSlide,&QAbstractSlider::valueChanged,this,&InvasionSlider::slideMove);
0127   connect(m_invadeSlide,&QAbstractSlider::sliderReleased,this,&InvasionSlider::slideReleased);
0128   connect(buttonBox,&QDialogButtonBox::accepted,this,&InvasionSlider::slideClose);
0129   if (invasionType == Moving)
0130   {
0131     connect(buttonBox,&QDialogButtonBox::rejected,this,&InvasionSlider::slideCancel);
0132   }
0133 
0134   widget->setLayout(wSlideLayout);
0135 
0136   dialogLayout->addWidget(widget);
0137   if (buttonBox)
0138       dialogLayout->addWidget(buttonBox);
0139   setWindowModality(Qt::ApplicationModal);
0140 }
0141 
0142 void InvasionSlider::slideMove(int v)
0143 {
0144   qCDebug(KSIRK_LOG) << v;
0145   m_nbLArmy = m_nbLArmy-(v-m_currentSlideValue);
0146   m_nbRArmy = m_nbRArmy+(v-m_currentSlideValue);
0147   m_nbLArmies->setText(QString::number(m_nbLArmy));
0148   m_nbRArmies->setText(QString::number(m_nbRArmy));
0149   update();
0150   m_currentSlideValue = v;
0151   
0152   m_game->automaton()->currentPlayerPlayed(true);
0153 }
0154 
0155 void InvasionSlider::slideReleased()
0156 {
0157   qCDebug(KSIRK_LOG) << "do nothing";
0158 }
0159 
0160 void InvasionSlider::slideClose()
0161 {
0162   qCDebug(KSIRK_LOG) << m_currentSlideValue;
0163   
0164   QByteArray buffer;
0165   QDataStream stream(&buffer, QIODevice::WriteOnly);
0166   stream << quint32(m_currentSlideValue);
0167   m_game->automaton()->sendMessage(buffer,Invade);
0168   
0169   QByteArray buffer2;
0170   m_game->automaton()->sendMessage(buffer2,InvasionFinished);
0171   m_game->automaton()->currentPlayerPlayed(true);
0172   QPointF point;
0173   m_game->automaton()->gameEvent("actionNextPlayer", point);
0174   accept();
0175 }
0176 
0177 void InvasionSlider::slideCancel()
0178 {
0179   qCDebug(KSIRK_LOG) << "Move cancel";
0180   reject();
0181 }
0182 
0183 } // closing namespace Ksirk
0184 
0185 #include "moc_InvasionSlider.cpp"