File indexing completed on 2024-05-05 04:05:12

0001 /* This file is part of KsirK.
0002    Copyright (C) 2001-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KsirK is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, either version 2
0007    of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017    02110-1301, USA
0018 */
0019 
0020 /* begin                : Wed Jul 18 2001 */
0021 
0022 #include "country.h"
0023 #include "onu.h"
0024 
0025 #include <KLocalizedString>
0026 #include "ksirkskineditor_debug.h"
0027 #include <QSvgRenderer>
0028 #include <QApplication>
0029 #include <QDataStream>
0030 #include <QBitmap>
0031 #include <QGraphicsSvgItem>
0032 #include <QGraphicsScene>
0033 #include <QDomNode>
0034 
0035 namespace KsirkSkinEditor
0036 {
0037 
0038 Country::Country(
0039                   const QString& theName,
0040                   const QPointF& anchorPoint,
0041                   const QPointF& centralPoint,
0042                   const QPointF& flagPoint,
0043                   const QPointF& cannonPoint, 
0044                   const QPointF& cavalryPoint,
0045                   const QPointF& infantryPoint/*, 
0046                   unsigned int id*/) :
0047   m_name(theName),
0048   m_anchorPoint(anchorPoint),
0049   m_centralPoint(centralPoint),
0050   m_pointFlag(flagPoint),
0051   m_pointCannon(cannonPoint), 
0052   m_pointCavalry(cavalryPoint),
0053   m_pointInfantry(infantryPoint),
0054 //   m_id(id),
0055   m_highlighting(nullptr),
0056   m_renderer(new QSvgRenderer()),
0057   m_highlighting_locked(false)
0058 {
0059 //   qCDebug(KSIRKSKINEDITOR_LOG) << m_name << ", " << this ;
0060 }
0061 
0062 Country::~Country()
0063 {
0064 //   qCDebug(KSIRKSKINEDITOR_LOG) << "Deleting country " << m_name << ", " << this ;
0065   delete m_renderer;
0066 }
0067 
0068 void Country::reset()
0069 {
0070 //   qCDebug(KSIRKSKINEDITOR_LOG) << "Country::reset " << m_name ;
0071 }
0072 
0073 bool Country::communicateWith(const Country* otherCountry) const
0074 {
0075   if (!otherCountry)
0076   {
0077     qCDebug(KSIRKSKINEDITOR_LOG) << "OUT otherCountry null Country::communicateWith";
0078     return false;
0079   }
0080 
0081   // a country is considered to communicate with itself
0082   if (otherCountry == this) {return true;}
0083 
0084 //    qCDebug(KSIRKSKINEDITOR_LOG) << "Country::communicateWith (" << name() << ", " << otherCountry-> name() << ")" << endl << flush;
0085   unsigned int nbNeighbours = neighbours().size();
0086   for (unsigned int i = 0; i < nbNeighbours; i++)
0087   {
0088     if (neighbours().at(i) == otherCountry)
0089     {
0090 //            qCDebug(KSIRKSKINEDITOR_LOG) << "OUT true Country::communicateWith" << endl << flush;
0091       return true;
0092     }
0093   }
0094 //    qCDebug(KSIRKSKINEDITOR_LOG) << "OUT false Country::communicateWith" << endl << flush;
0095   return false;
0096 }
0097 
0098 const QString& Country::name() const
0099 {
0100   return (m_name);
0101 }
0102 
0103 const QPointF& Country::anchorPoint() const
0104 {
0105   return (m_anchorPoint);
0106 }
0107 
0108 const QPointF& Country::centralPoint() const
0109 {
0110   return (m_centralPoint);
0111 }
0112 
0113 const QPointF& Country::pointFlag() const
0114 {
0115   return (m_pointFlag);
0116 }
0117 
0118 const QPointF& Country::pointCannon() const
0119 {
0120   return (m_pointCannon);
0121 }
0122 
0123 const QPointF& Country::pointCavalry() const
0124 {
0125   return (m_pointCavalry);
0126 }
0127 
0128 const QPointF& Country::pointInfantry() const
0129 {
0130   return (m_pointInfantry);
0131 }
0132 
0133 void Country::anchorPoint(const QPointF pt)
0134 {
0135   qCDebug(KSIRKSKINEDITOR_LOG) << pt;
0136   m_anchorPoint = pt;
0137 }
0138 
0139 void Country::centralPoint(const QPointF pt)
0140 {
0141   m_centralPoint = pt;
0142 }
0143 
0144 void Country::pointFlag(const QPointF pt)
0145 {
0146   m_pointFlag = pt;
0147 }
0148 
0149 void Country::pointCannon(const QPointF pt)
0150 {
0151   m_pointCannon = pt;
0152 }
0153 
0154 void Country::pointCavalry(const QPointF pt)
0155 {
0156   m_pointCavalry = pt;
0157 }
0158 
0159 void Country::pointInfantry(const QPointF pt)
0160 {
0161   m_pointInfantry = pt;
0162 }
0163 
0164 /** No descriptions */
0165 void Country::neighbours(const QList<Country*>& neighboursVect)
0166 {
0167   m_neighbours = neighboursVect;
0168 }
0169 
0170 /** No descriptions */
0171 QList< Country* >& Country::neighbours()
0172 {
0173 //    qCDebug(KSIRKSKINEDITOR_LOG) << "Country::neighbours" << endl << flush;
0174   return m_neighbours;
0175 }
0176 
0177 /** No descriptions */
0178 const QList< Country* >& Country::neighbours() const
0179 {
0180 //    qCDebug(KSIRKSKINEDITOR_LOG) << "Country::neighbours const" << endl << flush;
0181   return m_neighbours;
0182 }
0183 
0184 void Country::highlight(QGraphicsScene* scene, ONU* onu, const QColor& color, qreal opacity)
0185 {
0186   qCDebug(KSIRKSKINEDITOR_LOG) << m_name << color << opacity;
0187   if (m_highlighting_locked)
0188   {
0189     return;
0190   }
0191   clearHighlighting();
0192 
0193   QDomNode countryElement = onu->svgDom()->elementById(m_name);
0194   if (countryElement.isNull())
0195   {
0196     qCWarning(KSIRKSKINEDITOR_LOG) << "Got a null element";
0197     return;
0198   }
0199 //   qCDebug(KSIRKSKINEDITOR_LOG) <<"got country";
0200 
0201   onu->svgDom()->setCurrentNode(countryElement);
0202   onu->svgDom()->setStyleProperty(QStringLiteral("fill"), color.name());
0203   onu->svgDom()->setStyleProperty(QStringLiteral("fill-opacity"), QString::number(opacity));
0204 
0205 //   qCDebug(KSIRKSKINEDITOR_LOG) <<"loading";
0206   QByteArray svg = onu->svgDom()->nodeToByteArray();
0207   m_renderer->load(svg);
0208 
0209 //   qCDebug(KSIRKSKINEDITOR_LOG) <<"loaded";
0210   m_highlighting = new QGraphicsSvgItem();
0211   m_highlighting->setSharedRenderer(m_renderer);
0212   m_highlighting->setElementId(m_name);
0213   qCDebug(KSIRKSKINEDITOR_LOG) << "anchor point=" << m_anchorPoint;
0214   qCDebug(KSIRKSKINEDITOR_LOG) << "set highlighting pos to " << (m_anchorPoint.x()-m_highlighting->boundingRect().width()/2)
0215   << (m_anchorPoint.y()-m_highlighting->boundingRect().height()/2) ;
0216   m_highlighting->setPos(
0217       (m_anchorPoint.x()-m_highlighting->boundingRect().width()/2),
0218       (m_anchorPoint.y()-m_highlighting->boundingRect().height()/2));
0219   m_highlighting->setZValue(5);
0220   scene->addItem(m_highlighting);
0221 //   m_highlighting->scale(onu->zoom(), onu->zoom());
0222 //   qCDebug(KSIRKSKINEDITOR_LOG) << "done";
0223 }
0224 
0225 void Country::clearHighlighting()
0226 {
0227 //   qCDebug(KSIRKSKINEDITOR_LOG) << m_highlighting_locked << (void*)m_highlighting;
0228   if (!m_highlighting_locked && m_highlighting!=nullptr)
0229   {
0230     m_highlighting->hide();
0231     delete m_highlighting;
0232     m_highlighting = nullptr;
0233   }
0234 //   qCDebug(KSIRKSKINEDITOR_LOG) << "done";
0235 }
0236 
0237 bool Country::isHighlightingLocked()
0238 {
0239 //   qCDebug(KSIRKSKINEDITOR_LOG);
0240   return m_highlighting_locked;
0241 }
0242 
0243 void Country::releaseHighlightingLock()
0244 {
0245 //   qCDebug(KSIRKSKINEDITOR_LOG);
0246   m_highlighting_locked=false;
0247 }
0248 
0249 
0250 QDataStream& operator>>(QDataStream& stream, Country* country)
0251 {
0252   country->reset();
0253   quint32 nbArmies, nbAddedArmies;
0254   QString ownerName;
0255   stream >> ownerName >> nbArmies >> nbAddedArmies;
0256   qCDebug(KSIRKSKINEDITOR_LOG) << ownerName << nbArmies << nbAddedArmies;
0257 //   country->owner(country->automaton()->playerNamed(ownerName));
0258   return stream;
0259 }
0260 
0261 
0262 }