author | unc0rr |
Mon, 13 Oct 2008 19:04:27 +0000 | |
changeset 1351 | aa7aefec5c1b |
parent 1348 | 0eb0b3a955b8 |
child 1365 | 2ffe20744a4a |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com> |
184 | 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 <QPushButton> |
|
20 |
#include <QBuffer> |
|
21 |
#include <QUuid> |
|
22 |
#include <QBitmap> |
|
23 |
#include <QPainter> |
|
24 |
#include <QLinearGradient> |
|
25 |
#include <QColor> |
|
249 | 26 |
#include <QTextStream> |
677
9d0bcc3c903a
Save some vertical pixels using fewer margin in HWMapContainer
unc0rr
parents:
530
diff
changeset
|
27 |
#include <QApplication> |
1209 | 28 |
#include <QLabel> |
29 |
#include <QListWidget> |
|
30 |
#include <QVBoxLayout> |
|
1224 | 31 |
#include <QIcon> |
249 | 32 |
|
33 |
#include "hwconsts.h" |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
682
diff
changeset
|
34 |
#include "mapContainer.h" |
1209 | 35 |
#include "igbox.h" |
184 | 36 |
|
37 |
HWMapContainer::HWMapContainer(QWidget * parent) : |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
38 |
QWidget(parent), |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
39 |
mainLayout(this), |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
40 |
pMap(0) |
184 | 41 |
{ |
679 | 42 |
#if QT_VERSION >= 0x040300 |
682 | 43 |
mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin), |
44 |
1, |
|
45 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin), |
|
46 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin)); |
|
679 | 47 |
#endif |
1209 | 48 |
imageButt = new QPushButton(this); |
530 | 49 |
imageButt->setObjectName("imageButt"); |
1214 | 50 |
imageButt->setFixedSize(256 + 6, 128 + 6); |
184 | 51 |
imageButt->setFlat(true); |
452 | 52 |
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum); |
1209 | 53 |
mainLayout.addWidget(imageButt, 0, 0, 1, 2); |
320 | 54 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed())); |
1318 | 55 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomTheme())); |
249 | 56 |
|
1209 | 57 |
chooseMap = new QComboBox(this); |
58 |
chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
1210 | 59 |
chooseMap->addItem(QComboBox::tr("generated map...")); |
60 |
chooseMap->addItems(*mapList); |
|
249 | 61 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int))); |
1209 | 62 |
mainLayout.addWidget(chooseMap, 1, 1); |
249 | 63 |
|
1209 | 64 |
QLabel * lblMap = new QLabel(tr("Map"), this); |
65 |
mainLayout.addWidget(lblMap, 1, 0); |
|
66 |
||
67 |
gbThemes = new IconedGroupBox(this); |
|
1248 | 68 |
gbThemes->setTitleTextPadding(60); |
1209 | 69 |
gbThemes->setTitle(tr("Themes")); |
1248 | 70 |
|
71 |
//gbThemes->setStyleSheet("padding: 0px"); // doesn't work - stylesheet is set with icon |
|
1209 | 72 |
mainLayout.addWidget(gbThemes, 0, 2, 2, 1); |
1210 | 73 |
|
1209 | 74 |
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes); |
1228 | 75 |
gbTLayout->setContentsMargins(0, 0, 0 ,0); |
76 |
gbTLayout->setSpacing(0); |
|
1209 | 77 |
lwThemes = new QListWidget(this); |
1210 | 78 |
lwThemes->setMinimumHeight(30); |
1248 | 79 |
lwThemes->setFixedWidth(120); |
1211 | 80 |
for (int i = 0; i < Themes->size(); ++i) { |
81 |
QListWidgetItem * lwi = new QListWidgetItem(); |
|
82 |
lwi->setText(Themes->at(i)); |
|
83 |
lwi->setTextAlignment(Qt::AlignHCenter); |
|
84 |
lwThemes->addItem(lwi); |
|
85 |
} |
|
1215 | 86 |
connect(lwThemes, SIGNAL(currentRowChanged(int)), this, SLOT(themeSelected(int))); |
1210 | 87 |
|
1209 | 88 |
gbTLayout->addWidget(lwThemes); |
89 |
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); |
|
90 |
||
452 | 91 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
1215 | 92 |
|
93 |
setRandomSeed(); |
|
1318 | 94 |
setRandomTheme(); |
184 | 95 |
} |
96 |
||
97 |
void HWMapContainer::setImage(const QImage newImage) |
|
98 |
{ |
|
99 |
QPixmap px(256, 128); |
|
100 |
QPixmap pxres(256, 128); |
|
101 |
QPainter p(&pxres); |
|
102 |
||
103 |
px.fill(Qt::yellow); |
|
104 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
105 |
px.setMask(bm); |
|
106 |
||
107 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 108 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
109 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 110 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
111 |
p.drawPixmap(QPoint(0, 0), px); |
|
112 |
||
113 |
imageButt->setIcon(pxres); |
|
114 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 115 |
chooseMap->setCurrentIndex(0); |
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
116 |
pMap = 0; |
249 | 117 |
} |
118 |
||
119 |
void HWMapContainer::mapChanged(int index) |
|
120 |
{ |
|
121 |
if(!index) { |
|
122 |
changeImage(); |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
123 |
emit mapChanged("+rnd+"); |
249 | 124 |
return; |
125 |
} |
|
126 |
||
331 | 127 |
loadMap(index); |
128 |
||
129 |
emit mapChanged(chooseMap->currentText()); |
|
130 |
} |
|
131 |
||
132 |
void HWMapContainer::loadMap(int index) |
|
133 |
{ |
|
249 | 134 |
QPixmap mapImage; |
1348 | 135 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/preview.png")) { |
249 | 136 |
changeImage(); |
137 |
chooseMap->setCurrentIndex(0); |
|
138 |
return; |
|
139 |
} |
|
1348 | 140 |
imageButt->setIcon(mapImage); |
320 | 141 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
142 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
143 |
QTextStream input(&mapCfgFile); |
|
144 |
input >> theme; |
|
145 |
mapCfgFile.close(); |
|
146 |
} |
|
184 | 147 |
} |
148 |
||
149 |
void HWMapContainer::changeImage() |
|
150 |
{ |
|
1214 | 151 |
pMap = new HWMap(); |
152 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
|
153 |
pMap->getImage(m_seed.toStdString()); |
|
184 | 154 |
} |
155 |
||
1215 | 156 |
void HWMapContainer::themeSelected(int currentRow) |
157 |
{ |
|
158 |
theme = Themes->at(currentRow); |
|
1224 | 159 |
gbThemes->setIcon(QIcon(QString("%1/Themes/%2/icon.png").arg(datadir->absolutePath()).arg(theme))); |
1215 | 160 |
emit themeChanged(theme); |
161 |
} |
|
162 |
||
184 | 163 |
QString HWMapContainer::getCurrentSeed() const |
164 |
{ |
|
165 |
return m_seed; |
|
166 |
} |
|
167 |
||
249 | 168 |
QString HWMapContainer::getCurrentMap() const |
169 |
{ |
|
320 | 170 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 171 |
return chooseMap->currentText(); |
172 |
} |
|
173 |
||
174 |
QString HWMapContainer::getCurrentTheme() const |
|
175 |
{ |
|
320 | 176 |
return theme; |
249 | 177 |
} |
178 |
||
184 | 179 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
180 |
{ |
|
181 |
//imageButt->setIconSize(imageButt->size()); |
|
182 |
} |
|
320 | 183 |
|
184 |
void HWMapContainer::setSeed(const QString & seed) |
|
185 |
{ |
|
186 |
m_seed = seed; |
|
187 |
changeImage(); |
|
188 |
} |
|
189 |
||
190 |
void HWMapContainer::setMap(const QString & map) |
|
191 |
{ |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
192 |
if(map == "+rnd+") |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
193 |
{ |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
194 |
changeImage(); |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
195 |
return; |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
196 |
} |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
197 |
|
331 | 198 |
int id = chooseMap->findText(map); |
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
199 |
if(id > 0) { |
331 | 200 |
chooseMap->setCurrentIndex(id); |
201 |
loadMap(id); |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
202 |
if (pMap) |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1318
diff
changeset
|
203 |
disconnect(pMap, 0, this, SLOT(setImage(const QImage))); |
331 | 204 |
} |
320 | 205 |
} |
206 |
||
207 |
void HWMapContainer::setTheme(const QString & theme) |
|
208 |
{ |
|
1215 | 209 |
QList<QListWidgetItem *> items = lwThemes->findItems(theme, Qt::MatchExactly); |
210 |
if(items.size()) |
|
211 |
lwThemes->setCurrentItem(items.at(0)); |
|
320 | 212 |
} |
213 |
||
214 |
void HWMapContainer::setRandomSeed() |
|
215 |
{ |
|
216 |
m_seed = QUuid::createUuid().toString(); |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
325
diff
changeset
|
217 |
emit seedChanged(m_seed); |
320 | 218 |
changeImage(); |
219 |
} |
|
1318 | 220 |
|
221 |
void HWMapContainer::setRandomTheme() |
|
222 |
{ |
|
223 |
if(!Themes->size()) return; |
|
224 |
quint32 themeNum = rand() % Themes->size(); |
|
225 |
lwThemes->setCurrentRow(themeNum); |
|
226 |
} |