2
* Hedgewars, a free turn based strategy game
3
* Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 2 of the License
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
#include <QPushButton>
24
#include <QLinearGradient>
26
#include <QTextStream>
27
#include <QApplication>
29
#include <QListWidget>
30
#include <QVBoxLayout>
34
#include "mapContainer.h"
37
HWMapContainer::HWMapContainer(QWidget * parent) :
41
mapgen(MAPGEN_REGULAR),
44
hhSmall.load(":/res/hh_small.png");
48
mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
50
QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin),
51
QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
53
imageButt = new QPushButton(this);
54
imageButt->setObjectName("imageButt");
55
imageButt->setFixedSize(256 + 6, 128 + 6);
56
imageButt->setFlat(true);
57
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum);
58
mainLayout.addWidget(imageButt, 0, 0, 1, 2);
59
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed()));
60
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomTheme()));
62
chooseMap = new QComboBox(this);
63
chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
65
// FIXME - need real icons. Disabling until then
66
//QIcon(":/res/mapRandom.png"),
67
QComboBox::tr("generated map..."));
69
// FIXME - need real icons. Disabling until then
70
//QIcon(":/res/mapMaze.png"),
71
QComboBox::tr("generated maze..."));
72
chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
74
int missionindex = chooseMap->count();
75
for (int i = 0; i < mapList->size(); ++i) {
76
QString map = (*mapList)[i];
78
QString("%1/Maps/%2/map.cfg")
79
.arg(datadir->absolutePath())
82
QString("%1/Maps/%2/map.lua")
83
.arg(datadir->absolutePath())
86
if (mapCfgFile.open(QFile::ReadOnly)) {
89
QList<QVariant> mapInfo;
90
QTextStream input(&mapCfgFile);
93
mapInfo.push_back(map);
94
mapInfo.push_back(theme);
96
mapInfo.push_back(limit);
98
mapInfo.push_back(18);
99
mapInfo.push_back(mapLuaFile.exists());
100
if(mapLuaFile.exists())
101
chooseMap->insertItem(missionindex++,
102
// FIXME - need real icons. Disabling until then
103
//QIcon(":/res/mapMission.png"),
104
QComboBox::tr("Mission") + ": " + map, mapInfo);
107
// FIXME - need real icons. Disabling until then
108
//QIcon(":/res/mapCustom.png"),
113
chooseMap->insertSeparator(missionindex); // separator between missions and maps
115
connect(chooseMap, SIGNAL(currentIndexChanged(int)), this, SLOT(mapChanged(int)));
116
mainLayout.addWidget(chooseMap, 1, 1);
118
QLabel * lblMap = new QLabel(tr("Map"), this);
119
mainLayout.addWidget(lblMap, 1, 0);
121
lblFilter = new QLabel(tr("Filter"), this);
122
mainLayout.addWidget(lblFilter, 2, 0);
124
CB_TemplateFilter = new QComboBox(this);
125
CB_TemplateFilter->addItem(tr("All"), 0);
126
CB_TemplateFilter->addItem(tr("Small"), 1);
127
CB_TemplateFilter->addItem(tr("Medium"), 2);
128
CB_TemplateFilter->addItem(tr("Large"), 3);
129
CB_TemplateFilter->addItem(tr("Cavern"), 4);
130
CB_TemplateFilter->addItem(tr("Wacky"), 5);
131
mainLayout.addWidget(CB_TemplateFilter, 2, 1);
133
connect(CB_TemplateFilter, SIGNAL(currentIndexChanged(int)), this, SLOT(templateFilterChanged(int)));
135
maze_size_label = new QLabel(tr("Type"), this);
136
mainLayout.addWidget(maze_size_label, 2, 0);
137
maze_size_label->hide();
138
maze_size_selection = new QComboBox(this);
139
maze_size_selection->addItem(tr("Small tunnels"), 0);
140
maze_size_selection->addItem(tr("Medium tunnels"), 1);
141
maze_size_selection->addItem(tr("Large tunnels"), 2);
142
maze_size_selection->addItem(tr("Small floating islands"), 3);
143
maze_size_selection->addItem(tr("Medium floating islands"), 4);
144
maze_size_selection->addItem(tr("Large floating islands"), 5);
145
maze_size_selection->setCurrentIndex(1);
147
mainLayout.addWidget(maze_size_selection, 2, 1);
148
maze_size_selection->hide();
149
connect(maze_size_selection, SIGNAL(currentIndexChanged(int)), this, SLOT(setMaze_size(int)));
151
gbThemes = new IconedGroupBox(this);
152
gbThemes->setTitleTextPadding(60);
153
gbThemes->setContentTopPadding(6);
154
gbThemes->setTitle(tr("Themes"));
156
//gbThemes->setStyleSheet("padding: 0px"); // doesn't work - stylesheet is set with icon
157
mainLayout.addWidget(gbThemes, 0, 2, 3, 1);
159
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes);
160
gbTLayout->setContentsMargins(0, 0, 0 ,0);
161
gbTLayout->setSpacing(0);
162
lwThemes = new QListWidget(this);
163
lwThemes->setMinimumHeight(30);
164
lwThemes->setFixedWidth(140);
165
for (int i = 0; i < Themes->size(); ++i) {
166
QListWidgetItem * lwi = new QListWidgetItem();
167
lwi->setText(Themes->at(i));
168
lwi->setIcon(QIcon(QString("%1/Themes/%2/icon.png").arg(datadir->absolutePath()).arg(Themes->at(i))));
169
//lwi->setTextAlignment(Qt::AlignHCenter);
170
lwThemes->addItem(lwi);
172
connect(lwThemes, SIGNAL(currentRowChanged(int)), this, SLOT(themeSelected(int)));
174
// override default style to tighten up theme scroller
175
lwThemes->setStyleSheet(QString(
179
"border-radius: 0px;"
180
"border-color: transparent;"
181
"background-color: #0d0544;"
188
gbTLayout->addWidget(lwThemes);
189
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
191
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize
197
void HWMapContainer::setImage(const QImage newImage)
199
QPixmap px(256, 128);
200
QPixmap pxres(256, 128);
204
QBitmap bm = QBitmap::fromImage(newImage);
207
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
208
linearGrad.setColorAt(1, QColor(0, 0, 192));
209
linearGrad.setColorAt(0, QColor(66, 115, 225));
210
p.fillRect(QRect(0, 0, 256, 128), linearGrad);
211
p.drawPixmap(QPoint(0, 0), px);
213
addInfoToPreview(pxres);
214
chooseMap->setCurrentIndex(mapgen);
218
void HWMapContainer::setHHLimit(int newHHLimit)
220
hhLimit = newHHLimit;
223
void HWMapContainer::mapChanged(int index)
227
mapgen = MAPGEN_REGULAR;
231
CB_TemplateFilter->show();
232
maze_size_label->hide();
233
maze_size_selection->hide();
234
emit mapChanged("+rnd+");
235
emit mapgenChanged(mapgen);
236
emit themeChanged(chooseMap->itemData(index).toList()[1].toString());
239
mapgen = MAPGEN_MAZE;
243
CB_TemplateFilter->hide();
244
maze_size_label->show();
245
maze_size_selection->show();
246
emit mapChanged("+maze+");
247
emit mapgenChanged(mapgen);
248
emit themeChanged(chooseMap->itemData(index).toList()[1].toString());
254
CB_TemplateFilter->hide();
255
maze_size_label->hide();
256
maze_size_selection->hide();
257
emit mapChanged(chooseMap->itemData(index).toList()[0].toString());
261
void HWMapContainer::loadMap(int index)
264
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->itemData(index).toList()[0].toString() + "/preview.png")) {
266
chooseMap->setCurrentIndex(0);
270
hhLimit = chooseMap->itemData(index).toList()[2].toInt();
271
addInfoToPreview(mapImage);
274
// Should this add text to identify map size?
275
void HWMapContainer::addInfoToPreview(QPixmap image)
277
QPixmap finalImage = QPixmap(image.size());
278
finalImage.fill(QColor(0, 0, 0, 0));
280
QPainter p(&finalImage);
281
p.drawPixmap(image.rect(), image);
282
//p.setPen(QColor(0xf4,0x9e,0xe9));
283
p.setPen(QColor(0xff,0xcc,0x00));
284
p.setBrush(QColor(0, 0, 0));
285
p.drawRect(image.rect().width() - hhSmall.rect().width() - 28, 3, 40, 20);
286
p.setFont(QFont("MS Shell Dlg", 10));
287
p.drawText(image.rect().width() - hhSmall.rect().width() - 14 - (hhLimit > 9 ? 10 : 0), 18, QString::number(hhLimit));
288
p.drawPixmap(image.rect().width() - hhSmall.rect().width() - 5, 5, hhSmall.rect().width(), hhSmall.rect().height(), hhSmall);
290
imageButt->setIcon(finalImage);
291
imageButt->setIconSize(image.size());
294
void HWMapContainer::changeImage()
298
disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
299
disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
304
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
305
connect(pMap, SIGNAL(HHLimitReceived(int)), this, SLOT(setHHLimit(int)));
306
pMap->getImage(m_seed.toStdString(), getTemplateFilter(), mapgen, maze_size);
309
void HWMapContainer::themeSelected(int currentRow)
311
QString theme = Themes->at(currentRow);
312
QList<QVariant> mapInfoRegular;
313
mapInfoRegular.push_back(QString("+rnd+"));
314
mapInfoRegular.push_back(theme);
315
mapInfoRegular.push_back(18);
316
mapInfoRegular.push_back(false);
317
chooseMap->setItemData(0, mapInfoRegular);
318
QList<QVariant> mapInfoMaze;
319
mapInfoMaze.push_back(QString("+maze+"));
320
mapInfoMaze.push_back(theme);
321
mapInfoMaze.push_back(18);
322
mapInfoMaze.push_back(false);
323
chooseMap->setItemData(1, mapInfoMaze);
324
gbThemes->setIcon(QIcon(QString("%1/Themes/%2/icon.png").arg(datadir->absolutePath()).arg(theme)));
325
emit themeChanged(theme);
328
QString HWMapContainer::getCurrentSeed() const
333
QString HWMapContainer::getCurrentMap() const
335
if(chooseMap->currentIndex() <= 1) return QString();
336
return chooseMap->itemData(chooseMap->currentIndex()).toList()[0].toString();
339
QString HWMapContainer::getCurrentTheme() const
341
return chooseMap->itemData(chooseMap->currentIndex()).toList()[1].toString();
344
bool HWMapContainer::getCurrentIsMission() const
346
if(!chooseMap->currentIndex()) return false;
347
return chooseMap->itemData(chooseMap->currentIndex()).toList()[3].toBool();
350
int HWMapContainer::getCurrentHHLimit() const
355
quint32 HWMapContainer::getTemplateFilter() const
357
return CB_TemplateFilter->itemData(CB_TemplateFilter->currentIndex()).toInt();
360
void HWMapContainer::resizeEvent ( QResizeEvent * event )
362
//imageButt->setIconSize(imageButt->size());
365
void HWMapContainer::setSeed(const QString & seed)
371
void HWMapContainer::setMap(const QString & map)
373
if(map == "+rnd+" || map == "+maze+")
380
for(int i = 0; i < chooseMap->count(); i++)
381
if(!chooseMap->itemData(i).isNull() && chooseMap->itemData(i).toList()[0].toString() == map)
390
disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
391
disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
394
chooseMap->setCurrentIndex(id);
399
void HWMapContainer::setTheme(const QString & theme)
401
QList<QListWidgetItem *> items = lwThemes->findItems(theme, Qt::MatchExactly);
403
lwThemes->setCurrentItem(items.at(0));
406
void HWMapContainer::setRandomSeed()
408
m_seed = QUuid::createUuid().toString();
409
emit seedChanged(m_seed);
413
void HWMapContainer::setRandomTheme()
415
if(!Themes->size()) return;
416
quint32 themeNum = rand() % Themes->size();
417
lwThemes->setCurrentRow(themeNum);
420
void HWMapContainer::setTemplateFilter(int filter)
422
CB_TemplateFilter->setCurrentIndex(filter);
425
void HWMapContainer::templateFilterChanged(int filter)
427
emit newTemplateFilter(filter);
431
MapGenerator HWMapContainer::get_mapgen(void) const
436
int HWMapContainer::get_maze_size(void) const
441
void HWMapContainer::setMaze_size(int size)
444
maze_size_selection->setCurrentIndex(size);
445
emit maze_sizeChanged(size);
449
void HWMapContainer::setMapgen(MapGenerator m)
452
emit mapgenChanged(m);