184
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
|
|
4 |
*
|
|
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
|
|
8 |
*
|
|
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.
|
|
13 |
*
|
|
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
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include "mapContainer.h"
|
|
20 |
|
|
21 |
#include <QPushButton>
|
|
22 |
#include <QBuffer>
|
|
23 |
#include <QUuid>
|
|
24 |
#include <QBitmap>
|
|
25 |
#include <QPainter>
|
|
26 |
#include <QLinearGradient>
|
|
27 |
#include <QColor>
|
249
|
28 |
#include <QTextStream>
|
|
29 |
|
|
30 |
#include "hwconsts.h"
|
184
|
31 |
|
|
32 |
HWMapContainer::HWMapContainer(QWidget * parent) :
|
|
33 |
QWidget(parent), mainLayout(this)
|
|
34 |
{
|
|
35 |
imageButt=new QPushButton(this);
|
|
36 |
imageButt->setMaximumSize(256, 128);
|
|
37 |
imageButt->setFlat(true);
|
|
38 |
imageButt->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
39 |
mainLayout.addWidget(imageButt);
|
|
40 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(changeImage()));
|
|
41 |
changeImage();
|
249
|
42 |
|
|
43 |
chooseMap=new QComboBox(this);
|
|
44 |
QDir tmpdir;
|
|
45 |
tmpdir.cd(datadir->absolutePath());
|
|
46 |
tmpdir.cd("Maps");
|
|
47 |
tmpdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
48 |
QStringList mapList=tmpdir.entryList(QStringList("*"));
|
|
49 |
mapList.push_front("generated map...");
|
|
50 |
chooseMap->addItems(mapList);
|
|
51 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
|
|
52 |
|
|
53 |
mainLayout.addWidget(chooseMap);
|
184
|
54 |
}
|
|
55 |
|
|
56 |
void HWMapContainer::setImage(const QImage newImage)
|
|
57 |
{
|
|
58 |
QPixmap px(256, 128);
|
|
59 |
QPixmap pxres(256, 128);
|
|
60 |
QPainter p(&pxres);
|
|
61 |
|
|
62 |
px.fill(Qt::yellow);
|
|
63 |
QBitmap bm = QBitmap::fromImage(newImage);
|
|
64 |
px.setMask(bm);
|
|
65 |
|
|
66 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
|
216
|
67 |
linearGrad.setColorAt(1, QColor(0, 0, 192));
|
|
68 |
linearGrad.setColorAt(0, QColor(66, 115, 225));
|
184
|
69 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad);
|
|
70 |
p.drawPixmap(QPoint(0, 0), px);
|
|
71 |
|
|
72 |
imageButt->setIcon(pxres);
|
|
73 |
imageButt->setIconSize(QSize(256, 128));
|
249
|
74 |
chooseMap->setCurrentIndex(0);
|
|
75 |
}
|
|
76 |
|
|
77 |
void HWMapContainer::mapChanged(int index)
|
|
78 |
{
|
|
79 |
if(!index) {
|
|
80 |
changeImage();
|
|
81 |
return;
|
|
82 |
}
|
|
83 |
|
|
84 |
QPixmap mapImage;
|
|
85 |
if(!mapImage.load(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.png")) {
|
|
86 |
changeImage();
|
|
87 |
chooseMap->setCurrentIndex(0);
|
|
88 |
return;
|
|
89 |
}
|
|
90 |
imageButt->setIcon(mapImage.scaled(256,128));
|
184
|
91 |
}
|
|
92 |
|
|
93 |
void HWMapContainer::changeImage()
|
|
94 |
{
|
|
95 |
pMap=new HWMap();
|
|
96 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
|
|
97 |
m_seed = QUuid::createUuid().toString();
|
|
98 |
pMap->getImage(m_seed.toStdString());
|
|
99 |
}
|
|
100 |
|
|
101 |
QString HWMapContainer::getCurrentSeed() const
|
|
102 |
{
|
|
103 |
return m_seed;
|
|
104 |
}
|
|
105 |
|
249
|
106 |
QString HWMapContainer::getCurrentMap() const
|
|
107 |
{
|
|
108 |
if(!chooseMap->currentIndex()) throw MapFileErrorException();
|
|
109 |
return chooseMap->currentText();
|
|
110 |
}
|
|
111 |
|
|
112 |
QString HWMapContainer::getCurrentTheme() const
|
|
113 |
{
|
|
114 |
if(!chooseMap->currentIndex()) throw MapFileErrorException();
|
|
115 |
QFile mapCfgFile(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.cfg");
|
|
116 |
if (mapCfgFile.open(QFile::ReadOnly)) {
|
|
117 |
QTextStream input(&mapCfgFile);
|
|
118 |
QString theme;
|
|
119 |
input >> theme;
|
|
120 |
if(theme.length()>256) throw MapFileErrorException(); // theme name too long
|
|
121 |
return theme;
|
|
122 |
mapCfgFile.close();
|
|
123 |
} else {
|
|
124 |
throw MapFileErrorException();
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
184
|
128 |
void HWMapContainer::resizeEvent ( QResizeEvent * event )
|
|
129 |
{
|
|
130 |
//imageButt->setIconSize(imageButt->size());
|
|
131 |
}
|