mainwindow.cpp 76.4 KB
Newer Older
1
2
3
4
5
/* This file is part of KsirK.
   Copyright (C) 2008 Gael de Chalendar <kleag@free.fr>

   KsirK is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
6
7
   License as published by the Free Software Foundation, either version 2
   of the License, or (at your option) any later version.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA
*/

// application specific includes
#include "mainwindow.h"
#include "ksirkSkinEditorConfigDialog.h"
#include "ksirkskineditorsettings.h"
24
25
26
#include "ksirkskineditorscene.h"
#include "ksirkskindefinition.h"
#include "ksirkcountrydefinition.h"
27
28
#include "ksirkcontinentdefinition.h"
#include "ksirkgoaldefinition.h"
29
#include "ksirknationalitydefinition.h"
30
#include "ksirkspritesdefinition.h"
31
#include "ksirkskineditorpixmapitem.h"
32
33
#include "ksirkskineditortextitem.h"
#include "ksirkskineditorcountriesselectiondialog.h"
34
#include "skinSpritesData.h"
Gaël de Chalendar's avatar
Gaël de Chalendar committed
35
#include "goal.h"
36
37

//include files for QT
Yuri Chornoivan's avatar
Yuri Chornoivan committed
38
#include <QAction>
39
#include <QDockWidget>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
40
#include <QFileDialog>
41
42
43
44
#include <QTreeView>
#include <QGridLayout>
#include <QString>
#include <QMovie>
45
46
#include <QGraphicsScene>
#include <QGraphicsView>
47
#include <QGraphicsPixmapItem>
48
#include <QGraphicsSvgItem>
49
#include <QBitmap>
50
#include <QInputDialog>
Nemanja Hiršl's avatar
Nemanja Hiršl committed
51
#include <QMenuBar>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
52
53
#include <QStatusBar>
#include <QSvgRenderer>
54

55
// include files for KDE
56
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
57
#include <KLocalizedString>
58
59
60
61
#include <KConfig>
#include <KStandardGameAction>
#include <KStandardAction>
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
62
#include "ksirkskineditor_debug.h"
63
64
65
#include <phonon/mediaobject.h>
#include <KToolBar>
#include <KAboutApplicationDialog>
66
#include <KRecentFilesAction>
67
68
69
70
71
72
73
74
75


#include <assert.h>


namespace KsirkSkinEditor
{

MainWindow::MainWindow(QWidget* parent) :
76
  KXmlGuiWindow(parent),
77
  m_selectedSprite(None),
78
  m_onu(0),
79
  m_rfa(0),
80
  m_mapItem(0),
81
  m_updateHighlightPosition(false)
82
{
Laurent Montel's avatar
Laurent Montel committed
83
  qCDebug(KSIRKSKINEDITOR_LOG) << "MainWindow constructor begin";
84
85
  KSirkSkinEditorWidget* mainWidget = new KSirkSkinEditorWidget(this);
  setCentralWidget(mainWidget);
86
  
87
  m_mapScene = new Scene(0,0,1024,768,this);
Laurent Montel's avatar
Laurent Montel committed
88
89
90
  connect(m_mapScene,SIGNAL(position(QPointF)),this,SLOT(slotPosition(QPointF)));
  connect(m_mapScene,SIGNAL(pressPosition(QPointF)),this,SLOT(slotPressPosition(QPointF)));
  connect(m_mapScene,SIGNAL(releasePosition(QPointF)),this,SLOT(slotReleasePosition(QPointF)));
91
92
  m_mapView = new QGraphicsView(m_mapScene,mainWidget->mapScrollArea);
  m_mapView->setInteractive(true);
93
94
95
96
97
98
99

  mainWidget->mapScrollArea->setBackgroundRole(QPalette::Dark);
  mainWidget->mapScrollArea->setWidget(m_mapView);
  
  QHBoxLayout* layout = new QHBoxLayout;
  mainWidget->buttonsScrollArea->setLayout(layout);

100
101
102
  m_flagButton = new QPushButton(mainWidget->buttonsScrollArea);
  layout->addWidget(m_flagButton);
  m_flagButton->setCheckable(true);
103
  m_flagButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
104
  connect(m_flagButton,&QAbstractButton::clicked,this,&MainWindow::slotFlagButtonClicked);
105

106
107
108
  m_infantryButton = new QPushButton(mainWidget->buttonsScrollArea);
  layout->addWidget(m_infantryButton);
  m_infantryButton->setCheckable(true);
109
  m_infantryButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
110
  connect(m_infantryButton,&QAbstractButton::clicked,this,&MainWindow::slotInfantryButtonClicked);
111

112
113
114
  m_cavalryButton = new QPushButton(mainWidget->buttonsScrollArea);
  layout->addWidget(m_cavalryButton);
  m_cavalryButton->setCheckable(true);
115
  m_cavalryButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
116
  connect(m_cavalryButton,&QAbstractButton::clicked,this,&MainWindow::slotCavalryButtonClicked);
117

118
119
120
  m_cannonButton = new QPushButton(mainWidget->buttonsScrollArea);
  layout->addWidget(m_cannonButton);
  m_cannonButton->setCheckable(true);
121
  m_cannonButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
122
  connect(m_cannonButton,&QAbstractButton::clicked,this,&MainWindow::slotCannonButtonClicked);
123

Laurent Montel's avatar
Laurent Montel committed
124
  QString anchorFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("cross.png"));
125
126
  if (anchorFileName.isNull())
  {
127
    KMessageBox::error(0, i18n("Cannot load anchor icon<br>Program cannot continue"), i18n("Error"));
128
129
130
131
132
133
134
    exit(2);
  }
  QPixmap anchorPix = QPixmap(anchorFileName);
  m_anchorButton = new QPushButton(mainWidget->buttonsScrollArea);
  m_anchorButton->setIcon(anchorPix);
  layout->addWidget(m_anchorButton);
  m_anchorButton->setCheckable(true);
135
  m_anchorButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
136
  connect(m_anchorButton,&QAbstractButton::clicked,this,&MainWindow::slotAnchorButtonClicked);
137
  
Laurent Montel's avatar
Laurent Montel committed
138
  QString centerFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("target.png"));
139
140
  if (centerFileName.isNull())
  {
141
    KMessageBox::error(0, i18n("Cannot load center icon<br>Program cannot continue"), i18n("Error"));
142
143
144
145
146
147
148
    exit(2);
  }
  QPixmap centerPix = QPixmap(centerFileName);
  m_centerButton = new QPushButton(mainWidget->buttonsScrollArea);
  m_centerButton->setIcon(centerPix);
  layout->addWidget(m_centerButton);
  m_centerButton->setCheckable(true);
149
  m_centerButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
150
  connect(m_centerButton,&QAbstractButton::clicked,this,&MainWindow::slotCenterButtonClicked);
151

152
153
  
  //   m_accels.setEnabled(true);
154
  
Laurent Montel's avatar
Laurent Montel committed
155
  QString iconFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("ksirkskineditor.png"));
156
157
/*  if (iconFileName.isNull())
  {
158
      KMessageBox::error(0, i18n("Cannot load icon<br>Program cannot continue"), i18n("Error"));
159
160
161
162
      exit(2);
  }*/
  QPixmap icon(iconFileName);

Laurent Montel's avatar
Laurent Montel committed
163
//    qCDebug(KSIRKSKINEDITOR_LOG) << "Before initActions";
164
165
  initActions();

166
167
  m_skinDefWidget = new KSirkSkinDefinitionWidget(this);
  addDockWidget ( Qt::LeftDockWidgetArea, m_skinDefWidget);
168
  
Laurent Montel's avatar
Laurent Montel committed
169
170
171
  connect(m_skinDefWidget->fontrequester, &KFontRequester::fontSelected, this, &MainWindow::slotFontSelected);
  connect(m_skinDefWidget->fgcolorbutton, &KColorButton::changed, this, &MainWindow::slotFgSelected);
  connect(m_skinDefWidget->bgcolorbutton, &KColorButton::changed, this, &MainWindow::slotBgColorSelected);
172

173
  m_countryDefWidget = new KsirkCountryDefinitionWidget(this);
174
175
  m_countryDefWidget->neighbourslist->setSortingEnabled(true);
  m_countryDefWidget->neighbourslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
176
177
178
179
180
181
182
183
184
185
186
187
  addDockWidget ( Qt::RightDockWidgetArea, m_countryDefWidget);
  m_countryDefWidget->hide();
  
  m_continentDefWidget = new KsirkContinentDefinitionWidget(this);
  m_continentDefWidget->countrieslist->setSortingEnabled(true);
  m_continentDefWidget->countrieslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
  addDockWidget ( Qt::RightDockWidgetArea, m_continentDefWidget);
  m_continentDefWidget->hide();
  
  m_goalDefWidget = new KsirkGoalDefinitionWidget(this);
  addDockWidget ( Qt::RightDockWidgetArea, m_goalDefWidget);
  m_goalDefWidget->hide();
Laurent Montel's avatar
Laurent Montel committed
188
189
190
191
192
  connect(m_goalDefWidget->worldtype,&QAbstractButton::clicked,this,&MainWindow::slotGoalTypeWorldClicked);
  connect(m_goalDefWidget->playertype,&QAbstractButton::clicked,this,&MainWindow::slotGoalTypePlayerClicked);
  connect(m_goalDefWidget->countriestype,&QAbstractButton::clicked,this,&MainWindow::slotGoalTypeCountriesClicked);
  connect(m_goalDefWidget->continentstype,&QAbstractButton::clicked,this,&MainWindow::slotGoalTypeContinentsClicked);
  connect (m_goalDefWidget->description,&QTextEdit::textChanged, this, &MainWindow::slotGoalDescriptionEdited);
Gaël de Chalendar's avatar
Gaël de Chalendar committed
193
194
  connect (m_goalDefWidget->nbcountries,SIGNAL(valueChanged(int)), this, SLOT(slotGoalNbCountriesChanged(int)));
  connect (m_goalDefWidget->armiesbycountry,SIGNAL(valueChanged(int)), this, SLOT(slotGoalNbArmiesByCountryChanged(int)));
Laurent Montel's avatar
Laurent Montel committed
195
196
197
198
  connect (m_goalDefWidget->anycontinent,&QCheckBox::stateChanged, this, &MainWindow::slotGoalAnyContinentChanged);
  connect (m_goalDefWidget->selectcontinentsbutton, &QAbstractButton::clicked, this, &MainWindow::slotGoalContinents);
  connect(m_skinDefWidget->newGoalButton, &QAbstractButton::clicked, this, &MainWindow::slotNewGoal);
  connect(m_skinDefWidget->deleteGoalButton, &QAbstractButton::clicked, this, &MainWindow::slotDeleteGoal);
Gaël de Chalendar's avatar
Gaël de Chalendar committed
199
  
200
201
202
  m_nationalityDefWidget = new KsirkNationalityDefinitionWidget(this);
  addDockWidget ( Qt::RightDockWidgetArea, m_nationalityDefWidget);
  m_nationalityDefWidget->hide();
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  
  m_spritesDefWidget = new KsirkSpritesDefinitionWidget(this);
  addDockWidget ( Qt::RightDockWidgetArea, m_spritesDefWidget);
  m_spritesDefWidget->hide();
  connect(m_spritesDefWidget->flagw, SIGNAL(valueChanged(int)), this, SLOT(slotFlagWidthChanged(int)));
  connect(m_spritesDefWidget->flagh, SIGNAL(valueChanged(int)), this, SLOT(slotFlagHeightChanged(int)));
  connect(m_spritesDefWidget->flagf, SIGNAL(valueChanged(int)), this, SLOT(slotFlagFramesChanged(int)));
  connect(m_spritesDefWidget->flagv, SIGNAL(valueChanged(int)), this, SLOT(slotFlagVersionsChanged(int)));

  connect(m_spritesDefWidget->infantryw, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryWidthChanged(int)));
  connect(m_spritesDefWidget->infantryh, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryHeightChanged(int)));
  connect(m_spritesDefWidget->infantryf, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryFramesChanged(int)));
  connect(m_spritesDefWidget->infantryv, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryVersionsChanged(int)));
  
  connect(m_spritesDefWidget->cavalryw, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryWidthChanged(int)));
  connect(m_spritesDefWidget->cavalryh, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryHeightChanged(int)));
  connect(m_spritesDefWidget->cavalryf, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryFramesChanged(int)));
  connect(m_spritesDefWidget->cavalryv, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryVersionsChanged(int)));
221
  
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  connect(m_spritesDefWidget->cannonw, SIGNAL(valueChanged(int)), this, SLOT(slotCannonWidthChanged(int)));
  connect(m_spritesDefWidget->cannonh, SIGNAL(valueChanged(int)), this, SLOT(slotCannonHeightChanged(int)));
  connect(m_spritesDefWidget->cannonf, SIGNAL(valueChanged(int)), this, SLOT(slotCannonFramesChanged(int)));
  connect(m_spritesDefWidget->cannonv, SIGNAL(valueChanged(int)), this, SLOT(slotCannonVersionsChanged(int)));
  
  connect(m_spritesDefWidget->firingw, SIGNAL(valueChanged(int)), this, SLOT(slotFiringWidthChanged(int)));
  connect(m_spritesDefWidget->firingh, SIGNAL(valueChanged(int)), this, SLOT(slotFiringHeightChanged(int)));
  connect(m_spritesDefWidget->firingf, SIGNAL(valueChanged(int)), this, SLOT(slotFiringFramesChanged(int)));
  connect(m_spritesDefWidget->firingv, SIGNAL(valueChanged(int)), this, SLOT(slotFiringVersionsChanged(int)));
  
  connect(m_spritesDefWidget->explodingw, SIGNAL(valueChanged(int)), this, SLOT(slotExplodingWidthChanged(int)));
  connect(m_spritesDefWidget->explodingh, SIGNAL(valueChanged(int)), this, SLOT(slotExplodingHeightChanged(int)));
  connect(m_spritesDefWidget->explodingf, SIGNAL(valueChanged(int)), this, SLOT(slotExplodingFramesChanged(int)));
  connect(m_spritesDefWidget->explodingv, SIGNAL(valueChanged(int)), this, SLOT(slotExplodingVersionsChanged(int)));
  
  
Laurent Montel's avatar
Laurent Montel committed
238
  connect(m_skinDefWidget->qtabwidget, &QTabWidget::currentChanged, this, &MainWindow::slotSkinPartTabChanged);
Nemanja Hiršl's avatar
Nemanja Hiršl committed
239
  m_skinDefWidget->qtabwidget-> setCurrentIndex(0);
240
  
241
  m_skinDefWidget->countrieslist->setSortingEnabled (true);
Laurent Montel's avatar
Laurent Montel committed
242
  connect(m_skinDefWidget->countrieslist, &QListWidget::itemClicked, this, &MainWindow::slotCountrySelected);
243

Laurent Montel's avatar
Laurent Montel committed
244
  connect(m_skinDefWidget->goalslist, &QListWidget::itemClicked, this, &MainWindow::slotGoalSelected);
Gaël de Chalendar's avatar
Gaël de Chalendar committed
245
  
Laurent Montel's avatar
Laurent Montel committed
246
  connect (m_countryDefWidget->neighboursbutton, &QAbstractButton::clicked, this, &MainWindow::slotNeighbours);
247
248
249
250
251
252
253
254
255
256
257
258
259
  connect (m_countryDefWidget->flagx, SIGNAL(valueChanged(int)), this, SLOT(slotFLagxValueChanged(int)));
  connect (m_countryDefWidget->flagy, SIGNAL(valueChanged(int)), this, SLOT(slotFLagyValueChanged(int)));
  connect (m_countryDefWidget->centerx, SIGNAL(valueChanged(int)), this, SLOT(slotCenterxValueChanged(int)));
  connect (m_countryDefWidget->centery, SIGNAL(valueChanged(int)), this, SLOT(slotCenteryValueChanged(int)));
  connect (m_countryDefWidget->anchorx, SIGNAL(valueChanged(int)), this, SLOT(slotAnchorxValueChanged(int)));
  connect (m_countryDefWidget->anchory, SIGNAL(valueChanged(int)), this, SLOT(slotAnchoryValueChanged(int)));
  connect (m_countryDefWidget->infantryx, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryxValueChanged(int)));
  connect (m_countryDefWidget->infantryy, SIGNAL(valueChanged(int)), this, SLOT(slotInfantryyValueChanged(int)));
  connect (m_countryDefWidget->cavalryx, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryxValueChanged(int)));
  connect (m_countryDefWidget->cavalryy, SIGNAL(valueChanged(int)), this, SLOT(slotCavalryyValueChanged(int)));
  connect (m_countryDefWidget->cannonx, SIGNAL(valueChanged(int)), this, SLOT(slotCannonxValueChanged(int)));
  connect (m_countryDefWidget->cannony, SIGNAL(valueChanged(int)), this, SLOT(slotCannonyValueChanged(int)));
  
260

Laurent Montel's avatar
Laurent Montel committed
261
  connect (m_skinDefWidget->skinNameLineEdit, &QLineEdit::editingFinished, this, &MainWindow::slotSkinNameEdited);
262

263
264
  connect (m_skinDefWidget->widthLineEdit, SIGNAL(valueChanged(int)), this, SLOT(slotSkinWidthEdited(int)));
  connect (m_skinDefWidget->heightLineEdit, SIGNAL(valueChanged(int)), this, SLOT(slotSkinHeightEdited(int)));
265

Laurent Montel's avatar
Laurent Montel committed
266
  connect (m_skinDefWidget->descriptionTextEdit,&QTextEdit::textChanged, this, &MainWindow::slotSkinDescriptionEdited);
267
268

  QIntValidator* widthDiffValidator = new QIntValidator(this);
269
  m_spritesDefWidget->widthDiffLineEdit->setValidator(widthDiffValidator);
Laurent Montel's avatar
Laurent Montel committed
270
  connect (m_spritesDefWidget->widthDiffLineEdit, &QLineEdit::editingFinished, this, &MainWindow::slotSkinWidthDiffEdited);
271
272

  m_skinDefWidget->nationalitieslist->setSortingEnabled (true);
Laurent Montel's avatar
Laurent Montel committed
273
274
275
  connect(m_skinDefWidget->nationalitieslist, &QListWidget::itemClicked, this, &MainWindow::slotNationalitySelected);
  connect(m_skinDefWidget->newNationalityButton, &QAbstractButton::clicked, this, &MainWindow::slotNewNationality);
  connect(m_skinDefWidget->deleteNationalityButton, &QAbstractButton::clicked, this, &MainWindow::slotDeleteNationality);
276

Laurent Montel's avatar
Laurent Montel committed
277
278
  connect(m_nationalityDefWidget->name,&QLineEdit::editingFinished, this, &MainWindow::slotNationalityNameEdited);
  connect(m_nationalityDefWidget->leader,&QLineEdit::editingFinished, this, &MainWindow::slotNationalityLeaderNameEdited);
279
  connect(m_nationalityDefWidget->flag,SIGNAL(currentIndexChanged(int)), this, SLOT(slotNationalityFlagEdited(int)));
280
281
  
  
Laurent Montel's avatar
Laurent Montel committed
282
283
  connect(m_skinDefWidget->newCountryButton, &QAbstractButton::clicked, this, &MainWindow::slotNewCountry);
  connect(m_skinDefWidget->deleteCountryButton, &QAbstractButton::clicked, this, &MainWindow::slotDeleteCountry);    
284

285
  m_skinDefWidget->continentslist->setSortingEnabled (true);
Laurent Montel's avatar
Laurent Montel committed
286
  connect(m_skinDefWidget->continentslist, &QListWidget::itemClicked, this, &MainWindow::slotContinentSelected);
287

Laurent Montel's avatar
Laurent Montel committed
288
289
  connect(m_skinDefWidget->newContinentButton, &QAbstractButton::clicked, this, &MainWindow::slotNewContinent);
  connect(m_skinDefWidget->deleteContinentButton, &QAbstractButton::clicked, this, &MainWindow::slotDeleteContinent);
290
  
Laurent Montel's avatar
Laurent Montel committed
291
  connect (m_continentDefWidget->selectcountriesbutton, &QAbstractButton::clicked, this, &MainWindow::slotContinentCountries);
292
  
293
  connect (m_continentDefWidget->bonus, SIGNAL(valueChanged(int)), this, SLOT(slotContinentBonusEdited(int)));
294
  
Laurent Montel's avatar
Laurent Montel committed
295
  qCDebug(KSIRKSKINEDITOR_LOG) << "Setting up GUI";
296
297
  setupGUI();
  
Laurent Montel's avatar
Laurent Montel committed
298
  //    qCDebug(KSIRKSKINEDITOR_LOG) << "Before initStatusBar";
299
300
301
302
303
304
  initStatusBar();
  
  menuBar()-> show();
  
  setMouseTracking(true);
  
305
306
307
308
}

MainWindow::~MainWindow()
{
Laurent Montel's avatar
Laurent Montel committed
309
  qCDebug(KSIRKSKINEDITOR_LOG);
Laurent Montel's avatar
Laurent Montel committed
310
  KSharedConfig::Ptr config = KSharedConfig::openConfig();
311
312
  if (m_rfa != 0)
  {
Laurent Montel's avatar
Laurent Montel committed
313
    qCDebug(KSIRKSKINEDITOR_LOG) << "saving recent files";
Laurent Montel's avatar
Laurent Montel committed
314
    m_rfa->saveEntries(KSharedConfig::openConfig()->group("ksirkskineditor"));
315
  }
316
317
318
319
}

void MainWindow::initActions()
{
Laurent Montel's avatar
Port++    
Laurent Montel committed
320
  QAction *action;
321
  // standard game actions
322
323
  action = KStandardGameAction::load(this, SLOT(slotOpenSkin()), this);
  actionCollection()->addAction(action->objectName(), action);
324
  action->setToolTip(i18n("Open a saved skin..."));
325

Laurent Montel's avatar
Laurent Montel committed
326
  m_rfa = KStandardGameAction::loadRecent (this, SLOT(slotURLSelected(QUrl)), this);
327
  actionCollection()->addAction(m_rfa->objectName(), m_rfa);
328
329
330
  m_rfa->setText(i18n("Load &Recent"));
  m_rfa->setToolTip(i18n("Open a recently saved skin..."));

Laurent Montel's avatar
Laurent Montel committed
331
  KSharedConfig::Ptr config = KSharedConfig::openConfig();
Laurent Montel's avatar
Laurent Montel committed
332
  qCDebug(KSIRKSKINEDITOR_LOG) << "loading recent files";
Laurent Montel's avatar
Laurent Montel committed
333
  m_rfa->loadEntries(KSharedConfig::openConfig()->group("ksirkskineditor"));
334
  
335
336
  m_saveGameAction = KStandardGameAction::save(this, SLOT(slotSaveSkin()), this);
  actionCollection()->addAction(m_saveGameAction->objectName(), m_saveGameAction);
337
338
  m_saveGameAction->setToolTip(i18n("Save the current skin"));

339
340
  action = KStandardGameAction::quit(this, SLOT(close()), this);
  actionCollection()->addAction(action->objectName(), action);
341

342
343
344
//   action = KStandardGameAction::gameNew(this, SLOT(slotNewGame()), this);
//   actionCollection()->addAction(action->objectName(), action);

345
346
//   action = KStandardAction::zoomIn(this, SLOT(slotZoomIn()), this);
//   actionCollection()->addAction(action->objectName(), action);
347

348
349
//   action = KStandardAction::zoomOut(this, SLOT(slotZoomOut()), this);
//   actionCollection()->addAction(action->objectName(), action);
350

Laurent Montel's avatar
Laurent Montel committed
351
  KStandardAction::preferences( this, SLOT(optionsConfigure()), actionCollection() );
352
353
354
355
356
357
358
359

}

void MainWindow::initStatusBar()
{
  statusBar()-> setSizeGripEnabled(true);
}

360
361
void MainWindow::slotOpenSkin(const QString& dir)
{
Laurent Montel's avatar
Laurent Montel committed
362
  qCDebug(KSIRKSKINEDITOR_LOG) << dir;
363
364
365
  QString skinDir = dir;
  if (dir.isNull())
  {
366
    skinDir = QFileDialog::getExistingDirectory(this, i18nc("@title:window", "Choose the Root Folder of the Skin to Open"), QString());
367
  }
368
369
370
371
372
  if (skinDir.isEmpty())
  {
    return;
  }

373
374
  if (m_rfa != 0)
  {
Laurent Montel's avatar
Laurent Montel committed
375
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding" << skinDir << "to recent files";
Laurent Montel's avatar
Laurent Montel committed
376
    m_rfa->addUrl(QUrl::fromLocalFile(skinDir));
377
378
379
380
381
382
383
  }
  
  m_mapScene->clear();
  m_skinDefWidget->countrieslist->clear();
  m_skinDefWidget->continentslist->clear();
  m_skinDefWidget->goalslist->clear();
  
Laurent Montel's avatar
Laurent Montel committed
384
385
  delete m_onu;
  m_onu = new ONU(skinDir, this);
386
387
388
389

  m_skinDefWidget->fontrequester->setFont(m_onu->foregroundFont());
  m_skinDefWidget->fgcolorbutton->setColor(m_onu->foregroundColor());
  m_skinDefWidget->bgcolorbutton->setColor(m_onu->backgroundColor());
390
391
  
  m_skinDefWidget->skinNameLineEdit->setText(m_onu->name());
392
393
  m_skinDefWidget->widthLineEdit->setValue(m_onu->width());
  m_skinDefWidget->heightLineEdit->setValue(m_onu->height());
394
  m_skinDefWidget->descriptionTextEdit->setText(m_onu->description());
Laurent Montel's avatar
Laurent Montel committed
395
  m_spritesDefWidget->widthDiffLineEdit->setText(QString::number(SkinSpritesData::changeable().intData(QStringLiteral("width-between-flag-and-fighter"))));
396

Laurent Montel's avatar
Laurent Montel committed
397
398
399
400
  m_spritesDefWidget->flagw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("flag-width")));
  m_spritesDefWidget->flagh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("flag-height")));
  m_spritesDefWidget->flagf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("flag-frames")));
  m_spritesDefWidget->flagv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("flag-versions")));
401
  
Laurent Montel's avatar
Laurent Montel committed
402
403
404
405
  m_spritesDefWidget->infantryw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("infantry-width")));
  m_spritesDefWidget->infantryh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("infantry-height")));
  m_spritesDefWidget->infantryf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("infantry-frames")));
  m_spritesDefWidget->infantryv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("infantry-versions")));
406
  
Laurent Montel's avatar
Laurent Montel committed
407
408
409
410
  m_spritesDefWidget->cavalryw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cavalry-width")));
  m_spritesDefWidget->cavalryh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cavalry-height")));
  m_spritesDefWidget->cavalryf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cavalry-frames")));
  m_spritesDefWidget->cavalryv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cavalry-versions")));
411
  
Laurent Montel's avatar
Laurent Montel committed
412
413
414
415
  m_spritesDefWidget->cannonw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cannon-width")));
  m_spritesDefWidget->cannonh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cannon-height")));
  m_spritesDefWidget->cannonf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cannon-frames")));
  m_spritesDefWidget->cannonv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("cannon-versions")));
416
  
Laurent Montel's avatar
Laurent Montel committed
417
418
419
420
  m_spritesDefWidget->firingw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("firing-width")));
  m_spritesDefWidget->firingh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("firing-height")));
  m_spritesDefWidget->firingf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("firing-frames")));
  m_spritesDefWidget->firingv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("firing-versions")));
421
  
Laurent Montel's avatar
Laurent Montel committed
422
423
424
425
  m_spritesDefWidget->explodingw->setValue(SkinSpritesData::changeable().intData(QStringLiteral("exploding-width")));
  m_spritesDefWidget->explodingh->setValue(SkinSpritesData::changeable().intData(QStringLiteral("exploding-height")));
  m_spritesDefWidget->explodingf->setValue(SkinSpritesData::changeable().intData(QStringLiteral("exploding-frames")));
  m_spritesDefWidget->explodingv->setValue(SkinSpritesData::changeable().intData(QStringLiteral("exploding-versions")));
426
427
428
429

  m_nationalityDefWidget->flag->clear();
  foreach(const QString& key, m_onu->poolIds())
  {
Laurent Montel's avatar
Laurent Montel committed
430
431
432
433
    int flagWidth = SkinSpritesData::changeable().intData(QStringLiteral("flag-width"));
    int flagHeight = SkinSpritesData::changeable().intData(QStringLiteral("flag-height"));
    int flagFrames = SkinSpritesData::changeable().intData(QStringLiteral("flag-frames"));
    int flagVersions = SkinSpritesData::changeable().intData(QStringLiteral("flag-versions"));
434
435
436
437
    QPixmap flagIcon = m_onu->pixmapForId(key,flagWidth*flagFrames,flagHeight*flagVersions).copy(0,0,flagWidth,flagHeight);
    m_nationalityDefWidget->flag->insertItem(m_nationalityDefWidget->flag->count(),QIcon(flagIcon),key);
  }

438
  
Laurent Montel's avatar
Laurent Montel committed
439
  QPixmap mapPixmap(m_onu->pixmapForId(QStringLiteral("map"), m_onu->width(), m_onu->height()));
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  m_mapItem = m_mapScene->addPixmap(mapPixmap);
  
  m_flagButton->setIcon(m_onu->flagIcon());
  //   m_flagButton->setIconSize(QSize(flagWidth,flagHeight));
  
  m_infantryButton->setIcon(m_onu->infantryIcon());
  //   m_infantryButton->setIconSize(QSize(infantryWidth,infantryHeight));
  
  m_cavalryButton->setIcon(m_onu->cavalryIcon());
  //   m_cavalryButton->setIconSize(QSize(cavalryWidth,cavalryHeight));
  
  m_cannonButton->setIcon(m_onu->cannonIcon());
  //   m_cannonButton->setIconSize(QSize(cannonWidth,cannonHeight));
  
Laurent Montel's avatar
Laurent Montel committed
454
  qCDebug(KSIRKSKINEDITOR_LOG) << "Adding nationalities items";
455
456
  foreach (Nationality* nationality, m_onu->nationalities())
  {
Laurent Montel's avatar
Laurent Montel committed
457
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding "<<nationality->name()<<" items";
458
459
    m_skinDefWidget->nationalitieslist->addItem(nationality->name());
  }
460
  
Laurent Montel's avatar
Laurent Montel committed
461
  qCDebug(KSIRKSKINEDITOR_LOG) << "Adding countries items";
462
463
  foreach (Country* country, m_onu->countries())
  {
Laurent Montel's avatar
Laurent Montel committed
464
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding "<<country->name()<<" items";
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    m_skinDefWidget->countrieslist->addItem(country->name());
    
    if (!country->pointFlag().isNull())
    {
      createPixmapFor(country, Flag);
    }
    if (!country->pointInfantry().isNull())
    {
      createPixmapFor(country, Infantry);
    }
    if (!country->pointCavalry().isNull())
    {
      createPixmapFor(country, Cavalry);
    }
    if (!country->pointCannon().isNull())
    {
      createPixmapFor(country, Cannon);
    }
    if (!country->anchorPoint().isNull())
    {
      createPixmapFor(country, Anchor);
    }
    if (!country->centralPoint().isNull())
    {
      createPixmapFor(country, Center);
    }
  }

Laurent Montel's avatar
Laurent Montel committed
493
  qCDebug(KSIRKSKINEDITOR_LOG) << "Adding continents items";
494
495
  foreach (Continent* continent, m_onu->continents())
  {
Laurent Montel's avatar
Laurent Montel committed
496
497
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding "<<continent<<" items";
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding "<<continent->name()<<" items";
498
499
    m_skinDefWidget->continentslist->addItem(continent->name());
  }
Gaël de Chalendar's avatar
Gaël de Chalendar committed
500

Laurent Montel's avatar
Laurent Montel committed
501
  qCDebug(KSIRKSKINEDITOR_LOG) << "Adding goals items";
Gaël de Chalendar's avatar
Gaël de Chalendar committed
502
503
  for (int i = 1; i <= m_onu->goals().size(); i++)
  {
Laurent Montel's avatar
Laurent Montel committed
504
    qCDebug(KSIRKSKINEDITOR_LOG) << "Adding goal"<<i<<" items";
Gaël de Chalendar's avatar
Gaël de Chalendar committed
505
506
    m_skinDefWidget->goalslist->addItem("goal" + QString::number(i));
  }
507
508
509
510
}

void MainWindow::slotSaveSkin()
{
Laurent Montel's avatar
Laurent Montel committed
511
  qCDebug(KSIRKSKINEDITOR_LOG);
512
513
514
  m_onu->saveConfig();
}

515
516
517
518
519
/**
  * Reimplementation of the inherited function called when a window close event arise
  */
bool MainWindow::queryClose()
{
Laurent Montel's avatar
Laurent Montel committed
520
  qCDebug(KSIRKSKINEDITOR_LOG);
521
  // TODO : Test si jeu en cours
Gaël de Chalendar's avatar
Gaël de Chalendar committed
522
  if (m_onu && m_onu->dirty())
523
  {
524
525
    switch (KMessageBox::warningYesNoCancel(this,i18n("There are unsaved changes. What do you want to do?"),
      i18n("Exit Anyway?"),
526
527
                                             KGuiItem(i18n("Quit without saving")),
                                             KGuiItem(i18n("Save then quit")),
528
                                             KGuiItem(i18n("Do not quit"))))
529
530
531
532
533
534
535
536
537
538
539
540
    {
      case KMessageBox::Yes:
        return true;
        break;
      case KMessageBox::No:
        m_onu->saveConfig();
        return true;
        break;
      default:
        return false;
    }
  }
Laurent Montel's avatar
Laurent Montel committed
541
  KSharedConfig::openConfig()->sync();
542
543
544
545
546
547
548
549
  return true;
}

void MainWindow::optionsConfigure()
{
  //An instance of your dialog could be already created and could be cached, 
  //in which case you want to display the cached dialog instead of creating 
  //another one 
Laurent Montel's avatar
Laurent Montel committed
550
  if ( KsirkSkinEditorConfigurationDialog::showDialog( QStringLiteral("settings") ) ) 
551
    return;
552
553
554
555
556
557
558
559
560
 
  //KConfigDialog didn't find an instance of this dialog, so lets create it : 
  KsirkSkinEditorConfigurationDialog* dialog = new KsirkSkinEditorConfigurationDialog(
      this, "settings",
      KsirkSkinEditorSettings::self() ); 

  dialog->show();
}

561
562
// void MainWindow::slotZoomIn()
// {
Laurent Montel's avatar
Laurent Montel committed
563
//   qCDebug(KSIRKSKINEDITOR_LOG);
564
// }
565

566
567
// void MainWindow::slotZoomOut()
// {
Laurent Montel's avatar
Laurent Montel committed
568
//   qCDebug(KSIRKSKINEDITOR_LOG);
569
// }
570
571
572

void MainWindow::slotShowAboutApplication()
{
Laurent Montel's avatar
Laurent Montel committed
573
#if 0 //QT5
574
575
  KAboutApplicationDialog dialog(KGlobal::mainComponent().aboutData(), this);
  dialog.exec();
Laurent Montel's avatar
Laurent Montel committed
576
#endif
577
578
}

579
580
void MainWindow::slotFlagButtonClicked()
{
581
582
583
584
585
  m_infantryButton->setChecked(false);
  m_cavalryButton->setChecked(false);
  m_cannonButton->setChecked(false);
  m_anchorButton->setChecked(false);
  m_centerButton->setChecked(false);
586
587
588
589
590
591
  if (m_selectedSprite == Flag)
  {
    m_flagButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
592
  {
593
    m_mapView->setCursor(QCursor(m_onu->flagIcon(),0,0));
594
    m_selectedSprite = Flag;
595
    m_flagButton->setChecked(true);
596
  }
597
598
599
600
}

void MainWindow::slotInfantryButtonClicked()
{
601
602
603
604
605
  m_flagButton->setChecked(false);
  m_cavalryButton->setChecked(false);
  m_cannonButton->setChecked(false);
  m_anchorButton->setChecked(false);
  m_centerButton->setChecked(false);
606
607
608
609
610
611
  if (m_selectedSprite == Infantry)
  {
    m_infantryButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
612
  {
613
    m_mapView->setCursor(QCursor(m_onu->infantryIcon(),0,0));
614
    m_selectedSprite = Infantry;
615
    m_infantryButton->setChecked(true);
616
  }
617
618
619
620
}

void MainWindow::slotCavalryButtonClicked()
{
621
622
623
624
625
  m_flagButton->setChecked(false);
  m_infantryButton->setChecked(false);
  m_cannonButton->setChecked(false);
  m_anchorButton->setChecked(false);
  m_centerButton->setChecked(false);
626
627
628
629
630
631
  if (m_selectedSprite == Cavalry)
  {
    m_cavalryButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
632
  {
633
    m_mapView->setCursor(QCursor(m_onu->cavalryIcon(),0,0));
634
    m_selectedSprite = Cavalry;
635
    m_cavalryButton->setChecked(true);
636
  }
637
638
639
640
}

void MainWindow::slotCannonButtonClicked()
{
641
642
643
644
645
  m_flagButton->setChecked(false);
  m_infantryButton->setChecked(false);
  m_cavalryButton->setChecked(false);
  m_anchorButton->setChecked(false);
  m_centerButton->setChecked(false);
646
647
648
649
650
651
  if (m_selectedSprite == Cannon)
  {
    m_cannonButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
652
  {
653
    m_mapView->setCursor(QCursor(m_onu->cannonIcon(),0,0));
654
    m_selectedSprite = Cannon;
655
    m_cannonButton->setChecked(true);
656
  }
657
658
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
void MainWindow::slotAnchorButtonClicked()
{
  m_flagButton->setChecked(false);
  m_infantryButton->setChecked(false);
  m_cavalryButton->setChecked(false);
  m_cannonButton->setChecked(false);
  m_centerButton->setChecked(false);
  if (m_selectedSprite == Anchor)
  {
    m_anchorButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
  {
Laurent Montel's avatar
Laurent Montel committed
673
    QString anchorFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("cross.png"));
674
675
    if (anchorFileName.isNull())
    {
676
      KMessageBox::error(0, i18n("Cannot load anchor icon<br>Program cannot continue"), i18n("Error"));
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
      exit(2);
    }
    QPixmap anchorPix = QPixmap(anchorFileName);
    anchorPix = anchorPix.scaled(16,16);
    
    m_mapView->setCursor(QCursor(anchorPix,0,0));
    m_selectedSprite = Anchor;
    m_anchorButton->setChecked(true);
  }
}

void MainWindow::slotCenterButtonClicked()
{
  m_flagButton->setChecked(false);
  m_infantryButton->setChecked(false);
  m_cavalryButton->setChecked(false);
  m_cannonButton->setChecked(false);
  m_anchorButton->setChecked(false);
  if (m_selectedSprite == Center)
  {
    m_centerButton->setChecked(false);
    m_mapView->unsetCursor();
  }
  else if (m_skinDefWidget->countrieslist->currentItem()!=0)
  {
Laurent Montel's avatar
Laurent Montel committed
702
    QString centerFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("target.png"));
703
704
    if (centerFileName.isNull())
    {
705
      KMessageBox::error(0, i18n("Cannot load center icon<br>Program cannot continue"), i18n("Error"));
706
707
708
709
710
711
712
713
714
715
      exit(2);
    }
    QPixmap centerPix = QPixmap(centerFileName);
    centerPix = centerPix.scaled(16,16);
    m_mapView->setCursor(QCursor(centerPix,0,0));
    m_selectedSprite = Center;
    m_centerButton->setChecked(true);
  }
}

716
717
void MainWindow::slotPosition(const QPointF& point)
{
Laurent Montel's avatar
Laurent Montel committed
718
  QString message(QLatin1String(""));
719
720
721
  QTextStream ts( &message );
  ts << point.x() << " x " << point.y();
  statusBar()->showMessage(message);
Laurent Montel's avatar
Laurent Montel committed
722
//   qCDebug(KSIRKSKINEDITOR_LOG) << "selected sprite:" << m_selectedSprite;
723
  if (currentCountry() != 0 && m_selectedSprite == Anchor
724
725
    && m_onu->itemFor(currentCountry(), m_selectedSprite) != 0 && m_updateHighlightPosition
    && currentCountry()->highlighting() != 0)
726
  {
Laurent Montel's avatar
Laurent Montel committed
727
    qCDebug(KSIRKSKINEDITOR_LOG) << point << (void*)currentCountry() << (void*)m_onu->itemFor(currentCountry(), m_selectedSprite);
728
    QGraphicsItem* anchorItem = m_onu->itemFor(currentCountry(), Anchor);
Laurent Montel's avatar
Laurent Montel committed
729
    qCDebug(KSIRKSKINEDITOR_LOG) << "anchorItem=" << anchorItem;
730
    currentCountry()->anchorPoint(point);
731
732
733
734
735
    QPointF anchorPoint = currentCountry()->anchorPoint();

    double hw = currentCountry()->highlighting()->boundingRect().width();
    double hh = currentCountry()->highlighting()->boundingRect().height();
    currentCountry()->highlighting()->setPos(anchorPoint - QPointF(hw/2,hh/2 ));
736
737
738

    return;
  }
739
740
}

741
void MainWindow::slotPressPosition(const QPointF& clickedPoint)
742
{
Laurent Montel's avatar
Laurent Montel committed
743
  qCDebug(KSIRKSKINEDITOR_LOG) << clickedPoint << (void*)currentCountry() << m_selectedSprite;
744
  QPixmap pix;
745
  QPixmap alphacopy;
746
  QString fileName;
747
  QPointF point = clickedPoint;
748
  m_updateHighlightPosition = true;
749
750
751
  if (currentCountry() == 0
    || m_onu->itemFor(currentCountry(), m_selectedSprite) != 0)
  {
Laurent Montel's avatar
Laurent Montel committed
752
    qCDebug(KSIRKSKINEDITOR_LOG) << (void*)currentCountry() << (void*)m_onu->itemFor(currentCountry(), m_selectedSprite);
753
754
755
756
    if (currentCountry() != 0)
    {
      currentCountry()->clearHighlighting();
    }
757
758
759
    return;
  }
  PixmapItem* item = new PixmapItem();
760
  item->setZValue(10);
Laurent Montel's avatar
Laurent Montel committed
761
762
  connect(item, &PixmapItem::pressed,this, &MainWindow::slotItemPressed);
  connect(item, &PixmapItem::placed,this, &MainWindow::slotItemPlaced);
763
  
764
765
766
  switch (m_selectedSprite)
  {
    case Flag:
767
768
      pix = m_onu->flagIcon();
//       pix = pix.scaled(20,20);
769
770
      m_countryDefWidget->flagx->setValue(point.x());
      m_countryDefWidget->flagy->setValue(point.y());
771
      currentCountry()->pointFlag(point);
772
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Flag));
773
774
      break;
    case Infantry:
775
776
      pix = m_onu->infantryIcon();
//       pix = pix.scaled(23,32);
777
778
      m_countryDefWidget->infantryx->setValue(point.x());
      m_countryDefWidget->infantryy->setValue(point.y());
779
      currentCountry()->pointInfantry(point);
780
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Infantry));
781
782
      break;
    case Cavalry:
783
784
      pix = m_onu->cavalryIcon();
//       pix = pix.scaled(32,32);
785
786
      m_countryDefWidget->cavalryx->setValue(point.x());
      m_countryDefWidget->cavalryy->setValue(point.y());
787
      currentCountry()->pointCavalry(point);
788
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Cavalry));
789
790
      break;
    case Cannon:
Laurent Montel's avatar
Laurent Montel committed
791
      qCDebug(KSIRKSKINEDITOR_LOG) << "Adding cannon";
792
793
      pix = m_onu->cannonIcon();
//       pix = pix.scaled(32,32);
794
795
      m_countryDefWidget->cannonx->setValue(point.x());
      m_countryDefWidget->cannony->setValue(point.y());
796
      currentCountry()->pointCannon(point);
797
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Cannon));
798
      break;
799
    case Anchor:
Laurent Montel's avatar
Laurent Montel committed
800
      qCDebug(KSIRKSKINEDITOR_LOG) << "Adding anchor";
Laurent Montel's avatar
Laurent Montel committed
801
      fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("cross.png"));
802
803
      if (fileName.isNull())
      {
804
        KMessageBox::error(0, i18n("Cannot load anchor icon<br>Program cannot continue"), i18n("Error"));
805
806
807
808
        exit(2);
      }
      pix = QPixmap(fileName);
      pix = pix.scaled(16,16);
809
      point += QPointF(-pix.width()/2,-pix.height()/2);
810
811
      m_countryDefWidget->anchorx->setValue(point.x());
      m_countryDefWidget->anchory->setValue(point.y());
812
813
      currentCountry()->anchorPoint(point);
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Anchor));
814
      m_updateHighlightPosition = true;
815
816
      break;
    case Center:
Laurent Montel's avatar
Laurent Montel committed
817
      qCDebug(KSIRKSKINEDITOR_LOG) << "Adding center";
Laurent Montel's avatar
Laurent Montel committed
818
      fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("target.png"));
819
820
      if (fileName.isNull())
      {
821
        KMessageBox::error(0, i18n("Cannot load center icon<br>Program cannot continue"), i18n("Error"));
822
823
824
825
        exit(2);
      }
      pix = QPixmap(fileName);
      pix = pix.scaled(16,16);
826
      point += QPointF(-pix.width()/2,-pix.height()/2);
827
828
      m_countryDefWidget->centerx->setValue(point.x());
      m_countryDefWidget->centery->setValue(point.y());
829
830
831
      currentCountry()->centralPoint(point);
      m_onu->itemsMap().insert(item,qMakePair(currentCountry(),Center));
      break;
832
833
834
835
    default: ;
  }
  if (item != 0)
  {
836
837
838
839
    item->setPixmap(pix);
    m_mapScene->addItem(item);
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
840
841
842
    item->setPos(point);
  }
}
843

844
void MainWindow::slotReleasePosition(const QPointF& point)
845
{
Laurent Montel's avatar
Laurent Montel committed
846
  qCDebug(KSIRKSKINEDITOR_LOG) << point;
847
  m_updateHighlightPosition = false;
848

Laurent Montel's avatar
Laurent Montel committed
849
  qCDebug(KSIRKSKINEDITOR_LOG) << "selected sprite:" << m_selectedSprite;
850
851
852
853
  if (currentCountry() != 0 && m_selectedSprite == Anchor
    && m_onu->itemFor(currentCountry(), m_selectedSprite) != 0 && m_updateHighlightPosition
    && currentCountry()->highlighting() != 0)
  {
Laurent Montel's avatar
Laurent Montel committed
854
    qCDebug(KSIRKSKINEDITOR_LOG) << (void*)currentCountry() << (void*)m_onu->itemFor(currentCountry(), m_selectedSprite);
855
856
857
858
859
    currentCountry()->anchorPoint(point);
    currentCountry()->highlighting()->setPos((currentCountry()->anchorPoint().x()-currentCountry()->highlighting()->boundingRect().width()/2),(currentCountry()->anchorPoint().y()-currentCountry()->highlighting()->boundingRect().height()/2));
    
    return;
  }
860
861
}

862
863
void MainWindow::slotNationalitySelected(QListWidgetItem* item)
{
Laurent Montel's avatar
Laurent Montel committed
864
  qCDebug(KSIRKSKINEDITOR_LOG);
865
866
867
868
869
870
871
872
873
  Nationality* nationality = m_onu->nationalityNamed(item->text());
  if (nationality != 0)
  {
    initNationalityWidgetWith(nationality);
  }
}

void MainWindow::initNationalityWidgetWith(Nationality* nationality)
{
Laurent Montel's avatar
Laurent Montel committed
874
  qCDebug(KSIRKSKINEDITOR_LOG);
875
876
  m_nationalityDefWidget->name->setText(nationality->name());
  m_nationalityDefWidget->leader->setText(nationality->leaderName());
877
878
879
880
881
  int item = m_nationalityDefWidget->flag->findText(nationality->flagFileName());
  if (item != -1)
  {
      m_nationalityDefWidget->flag->setCurrentIndex(item);
  }
882
883
}

884
885
void MainWindow::slotCountrySelected(QListWidgetItem* item)
{
Laurent Montel's avatar
Laurent Montel committed
886
  qCDebug(KSIRKSKINEDITOR_LOG);
887
888
889
  Country* country = m_onu->countryNamed(item->text());
  if (country != 0)
  {
890
891
    m_countryDefWidget->initWith(country);
    initSpritesButtonsWith(country);
892
  }
893
894
}

895
896
void MainWindow::slotContinentSelected(QListWidgetItem* item)
{
Laurent Montel's avatar
Laurent Montel committed
897
  qCDebug(KSIRKSKINEDITOR_LOG);
898
899
900
901
902
903
904
905
906
  Continent* continent = m_onu->continentNamed(item->text());
  if (continent != 0)
  {
    initContinentWidgetWith(continent);
  }
}

void MainWindow::initContinentWidgetWith(Continent* continent)
{
Laurent Montel's avatar
Laurent Montel committed
907
  qCDebug(KSIRKSKINEDITOR_LOG);
908
  m_continentDefWidget->bonus->setValue(continent->bonus());
909

910
  m_continentDefWidget->countrieslist->clear();
911
912
913
914
915
916
  foreach(Country* country, continent->members())
  {
    m_continentDefWidget->countrieslist->addItem(country->name());
  }
}

Gaël de Chalendar's avatar
Gaël de Chalendar committed
917
918
void MainWindow::slotGoalSelected(QListWidgetItem* item)
{
Laurent Montel's avatar
Laurent Montel committed
919
  qCDebug(KSIRKSKINEDITOR_LOG);
Gaël de Chalendar's avatar
Gaël de Chalendar committed
920
921
922
923
924
925
926
927
928
929
  int row = m_skinDefWidget->goalslist->row(item);
  Goal* goal = m_onu->goals()[row];
  if (goal != 0)
  {
    initGoalWidgetWith(goal);
  }
}

void MainWindow::initGoalWidgetWith(Goal* goal)
{
Laurent Montel's avatar
Laurent Montel committed
930
  qCDebug(KSIRKSKINEDITOR_LOG);
Gaël de Chalendar's avatar
Gaël de Chalendar committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
  switch (goal->type())
  {
    case Goal::NoGoal:
      m_goalDefWidget->worldtype->setChecked(true);
      m_goalDefWidget->playertype->setChecked(false);
      m_goalDefWidget->countriestype->setChecked(false);
      m_goalDefWidget->continentstype->setChecked(false);
      break;
    case Goal::GoalPlayer:
      m_goalDefWidget->worldtype->setChecked(false);
      m_goalDefWidget->playertype->setChecked(true);
      m_goalDefWidget->countriestype->setChecked(false);
      m_goalDefWidget->continentstype->setChecked(false);
      break;
    case Goal::Countries:
      m_goalDefWidget->worldtype->setChecked(false);
      m_goalDefWidget->playertype->setChecked(false);
      m_goalDefWidget->countriestype->setChecked(true);
      m_goalDefWidget