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