author | unc0rr |
Wed, 08 Oct 2008 18:46:02 +0000 | |
changeset 1324 | 4b48ae1f0f53 |
parent 1318 | 18da1c5e960d |
child 1333 | b0b0510eb82d |
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())); |
1318 | 53 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomTheme())); |
249 | 54 |
|
1209 | 55 |
chooseMap = new QComboBox(this); |
56 |
chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
1210 | 57 |
chooseMap->addItem(QComboBox::tr("generated map...")); |
58 |
chooseMap->addItems(*mapList); |
|
249 | 59 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int))); |
1209 | 60 |
mainLayout.addWidget(chooseMap, 1, 1); |
249 | 61 |
|
1209 | 62 |
QLabel * lblMap = new QLabel(tr("Map"), this); |
63 |
mainLayout.addWidget(lblMap, 1, 0); |
|
64 |
||
65 |
gbThemes = new IconedGroupBox(this); |
|
1248 | 66 |
gbThemes->setTitleTextPadding(60); |
1209 | 67 |
gbThemes->setTitle(tr("Themes")); |
1248 | 68 |
|
69 |
//gbThemes->setStyleSheet("padding: 0px"); // doesn't work - stylesheet is set with icon |
|
1209 | 70 |
mainLayout.addWidget(gbThemes, 0, 2, 2, 1); |
1210 | 71 |
|
1209 | 72 |
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes); |
1228 | 73 |
gbTLayout->setContentsMargins(0, 0, 0 ,0); |
74 |
gbTLayout->setSpacing(0); |
|
1209 | 75 |
lwThemes = new QListWidget(this); |
1210 | 76 |
lwThemes->setMinimumHeight(30); |
1248 | 77 |
lwThemes->setFixedWidth(120); |
1211 | 78 |
for (int i = 0; i < Themes->size(); ++i) { |
79 |
QListWidgetItem * lwi = new QListWidgetItem(); |
|
80 |
lwi->setText(Themes->at(i)); |
|
81 |
lwi->setTextAlignment(Qt::AlignHCenter); |
|
82 |
lwThemes->addItem(lwi); |
|
83 |
} |
|
1215 | 84 |
connect(lwThemes, SIGNAL(currentRowChanged(int)), this, SLOT(themeSelected(int))); |
1210 | 85 |
|
1209 | 86 |
gbTLayout->addWidget(lwThemes); |
87 |
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); |
|
88 |
||
452 | 89 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
1215 | 90 |
|
91 |
setRandomSeed(); |
|
1318 | 92 |
setRandomTheme(); |
184 | 93 |
} |
94 |
||
95 |
void HWMapContainer::setImage(const QImage newImage) |
|
96 |
{ |
|
97 |
QPixmap px(256, 128); |
|
98 |
QPixmap pxres(256, 128); |
|
99 |
QPainter p(&pxres); |
|
100 |
||
101 |
px.fill(Qt::yellow); |
|
102 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
103 |
px.setMask(bm); |
|
104 |
||
105 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 106 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
107 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 108 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
109 |
p.drawPixmap(QPoint(0, 0), px); |
|
110 |
||
111 |
imageButt->setIcon(pxres); |
|
112 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 113 |
chooseMap->setCurrentIndex(0); |
114 |
} |
|
115 |
||
116 |
void HWMapContainer::mapChanged(int index) |
|
117 |
{ |
|
118 |
if(!index) { |
|
119 |
changeImage(); |
|
120 |
return; |
|
121 |
} |
|
122 |
||
331 | 123 |
loadMap(index); |
124 |
||
125 |
emit mapChanged(chooseMap->currentText()); |
|
126 |
} |
|
127 |
||
128 |
void HWMapContainer::loadMap(int index) |
|
129 |
{ |
|
249 | 130 |
QPixmap mapImage; |
320 | 131 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 132 |
changeImage(); |
133 |
chooseMap->setCurrentIndex(0); |
|
134 |
return; |
|
135 |
} |
|
320 | 136 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
137 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
138 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
139 |
QTextStream input(&mapCfgFile); |
|
140 |
input >> theme; |
|
141 |
mapCfgFile.close(); |
|
142 |
} |
|
184 | 143 |
} |
144 |
||
145 |
void HWMapContainer::changeImage() |
|
146 |
{ |
|
1214 | 147 |
pMap = new HWMap(); |
148 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
|
149 |
pMap->getImage(m_seed.toStdString()); |
|
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)); |
|
320 | 200 |
} |
201 |
||
202 |
void HWMapContainer::setRandomSeed() |
|
203 |
{ |
|
204 |
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
|
205 |
emit seedChanged(m_seed); |
320 | 206 |
changeImage(); |
207 |
} |
|
1318 | 208 |
|
209 |
void HWMapContainer::setRandomTheme() |
|
210 |
{ |
|
211 |
if(!Themes->size()) return; |
|
212 |
quint32 themeNum = rand() % Themes->size(); |
|
213 |
lwThemes->setCurrentRow(themeNum); |
|
214 |
} |