author | unc0rr |
Fri, 15 Aug 2008 07:54:13 +0000 | |
changeset 1218 | ada50f82ea53 |
parent 1215 | 0c1c294c0f7e |
child 1224 | 0b9fbee5ceda |
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> |
|
249 | 31 |
|
32 |
#include "hwconsts.h" |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
682
diff
changeset
|
33 |
#include "mapContainer.h" |
1209 | 34 |
#include "igbox.h" |
184 | 35 |
|
36 |
HWMapContainer::HWMapContainer(QWidget * parent) : |
|
37 |
QWidget(parent), mainLayout(this) |
|
38 |
{ |
|
679 | 39 |
#if QT_VERSION >= 0x040300 |
682 | 40 |
mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin), |
41 |
1, |
|
42 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin), |
|
43 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin)); |
|
679 | 44 |
#endif |
1209 | 45 |
imageButt = new QPushButton(this); |
530 | 46 |
imageButt->setObjectName("imageButt"); |
1214 | 47 |
imageButt->setFixedSize(256 + 6, 128 + 6); |
184 | 48 |
imageButt->setFlat(true); |
452 | 49 |
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum); |
1209 | 50 |
mainLayout.addWidget(imageButt, 0, 0, 1, 2); |
320 | 51 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed())); |
249 | 52 |
|
1209 | 53 |
chooseMap = new QComboBox(this); |
54 |
chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
1210 | 55 |
chooseMap->addItem(QComboBox::tr("generated map...")); |
56 |
chooseMap->addItems(*mapList); |
|
249 | 57 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int))); |
1209 | 58 |
mainLayout.addWidget(chooseMap, 1, 1); |
249 | 59 |
|
1209 | 60 |
QLabel * lblMap = new QLabel(tr("Map"), this); |
61 |
mainLayout.addWidget(lblMap, 1, 0); |
|
62 |
||
63 |
gbThemes = new IconedGroupBox(this); |
|
64 |
gbThemes->setTitle(tr("Themes")); |
|
65 |
gbThemes->setStyleSheet("padding: 0px;"); |
|
66 |
mainLayout.addWidget(gbThemes, 0, 2, 2, 1); |
|
1210 | 67 |
|
1209 | 68 |
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes); |
69 |
lwThemes = new QListWidget(this); |
|
1210 | 70 |
lwThemes->setMinimumHeight(30); |
1209 | 71 |
lwThemes->setFixedWidth(100); |
1211 | 72 |
for (int i = 0; i < Themes->size(); ++i) { |
73 |
QListWidgetItem * lwi = new QListWidgetItem(); |
|
74 |
lwi->setText(Themes->at(i)); |
|
75 |
lwi->setTextAlignment(Qt::AlignHCenter); |
|
76 |
lwThemes->addItem(lwi); |
|
77 |
} |
|
1215 | 78 |
connect(lwThemes, SIGNAL(currentRowChanged(int)), this, SLOT(themeSelected(int))); |
1210 | 79 |
|
1209 | 80 |
gbTLayout->addWidget(lwThemes); |
81 |
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); |
|
82 |
||
452 | 83 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
1215 | 84 |
|
85 |
setRandomSeed(); |
|
184 | 86 |
} |
87 |
||
88 |
void HWMapContainer::setImage(const QImage newImage) |
|
89 |
{ |
|
90 |
QPixmap px(256, 128); |
|
91 |
QPixmap pxres(256, 128); |
|
92 |
QPainter p(&pxres); |
|
93 |
||
94 |
px.fill(Qt::yellow); |
|
95 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
96 |
px.setMask(bm); |
|
97 |
||
98 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 99 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
100 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 101 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
102 |
p.drawPixmap(QPoint(0, 0), px); |
|
103 |
||
104 |
imageButt->setIcon(pxres); |
|
105 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 106 |
chooseMap->setCurrentIndex(0); |
107 |
} |
|
108 |
||
109 |
void HWMapContainer::mapChanged(int index) |
|
110 |
{ |
|
111 |
if(!index) { |
|
112 |
changeImage(); |
|
113 |
return; |
|
114 |
} |
|
115 |
||
331 | 116 |
loadMap(index); |
117 |
||
118 |
emit mapChanged(chooseMap->currentText()); |
|
119 |
} |
|
120 |
||
121 |
void HWMapContainer::loadMap(int index) |
|
122 |
{ |
|
249 | 123 |
QPixmap mapImage; |
320 | 124 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 125 |
changeImage(); |
126 |
chooseMap->setCurrentIndex(0); |
|
127 |
return; |
|
128 |
} |
|
320 | 129 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
130 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
131 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
132 |
QTextStream input(&mapCfgFile); |
|
133 |
input >> theme; |
|
134 |
mapCfgFile.close(); |
|
135 |
} |
|
184 | 136 |
} |
137 |
||
138 |
void HWMapContainer::changeImage() |
|
139 |
{ |
|
1214 | 140 |
pMap = new HWMap(); |
141 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
|
142 |
pMap->getImage(m_seed.toStdString()); |
|
143 |
||
144 |
if(!Themes->size()) return; |
|
145 |
quint32 themeNum = rand() % Themes->size(); |
|
146 |
lwThemes->setCurrentRow(themeNum); |
|
184 | 147 |
} |
148 |
||
1215 | 149 |
void HWMapContainer::themeSelected(int currentRow) |
150 |
{ |
|
151 |
theme = Themes->at(currentRow); |
|
152 |
emit themeChanged(theme); |
|
153 |
} |
|
154 |
||
184 | 155 |
QString HWMapContainer::getCurrentSeed() const |
156 |
{ |
|
157 |
return m_seed; |
|
158 |
} |
|
159 |
||
249 | 160 |
QString HWMapContainer::getCurrentMap() const |
161 |
{ |
|
320 | 162 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 163 |
return chooseMap->currentText(); |
164 |
} |
|
165 |
||
166 |
QString HWMapContainer::getCurrentTheme() const |
|
167 |
{ |
|
320 | 168 |
return theme; |
249 | 169 |
} |
170 |
||
184 | 171 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
172 |
{ |
|
173 |
//imageButt->setIconSize(imageButt->size()); |
|
174 |
} |
|
320 | 175 |
|
176 |
void HWMapContainer::setSeed(const QString & seed) |
|
177 |
{ |
|
178 |
m_seed = seed; |
|
179 |
changeImage(); |
|
180 |
} |
|
181 |
||
182 |
void HWMapContainer::setMap(const QString & map) |
|
183 |
{ |
|
331 | 184 |
int id = chooseMap->findText(map); |
185 |
if(id >= 0) { |
|
186 |
chooseMap->setCurrentIndex(id); |
|
187 |
loadMap(id); |
|
188 |
} |
|
320 | 189 |
} |
190 |
||
191 |
void HWMapContainer::setTheme(const QString & theme) |
|
192 |
{ |
|
1215 | 193 |
QList<QListWidgetItem *> items = lwThemes->findItems(theme, Qt::MatchExactly); |
194 |
if(items.size()) |
|
195 |
lwThemes->setCurrentItem(items.at(0)); |
|
196 |
//this->theme = theme; |
|
320 | 197 |
} |
198 |
||
199 |
void HWMapContainer::setRandomSeed() |
|
200 |
{ |
|
201 |
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
|
202 |
emit seedChanged(m_seed); |
320 | 203 |
changeImage(); |
204 |
} |