author | unc0rr |
Thu, 11 Sep 2008 15:30:04 +0000 | |
changeset 1251 | 1f545d9a10ca |
parent 1248 | 8c77eec56bf4 |
child 1318 | 18da1c5e960d |
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); |
|
1248 | 65 |
gbThemes->setTitleTextPadding(60); |
1209 | 66 |
gbThemes->setTitle(tr("Themes")); |
1248 | 67 |
|
68 |
//gbThemes->setStyleSheet("padding: 0px"); // doesn't work - stylesheet is set with icon |
|
1209 | 69 |
mainLayout.addWidget(gbThemes, 0, 2, 2, 1); |
1210 | 70 |
|
1209 | 71 |
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes); |
1228 | 72 |
gbTLayout->setContentsMargins(0, 0, 0 ,0); |
73 |
gbTLayout->setSpacing(0); |
|
1209 | 74 |
lwThemes = new QListWidget(this); |
1210 | 75 |
lwThemes->setMinimumHeight(30); |
1248 | 76 |
lwThemes->setFixedWidth(120); |
1211 | 77 |
for (int i = 0; i < Themes->size(); ++i) { |
78 |
QListWidgetItem * lwi = new QListWidgetItem(); |
|
79 |
lwi->setText(Themes->at(i)); |
|
80 |
lwi->setTextAlignment(Qt::AlignHCenter); |
|
81 |
lwThemes->addItem(lwi); |
|
82 |
} |
|
1215 | 83 |
connect(lwThemes, SIGNAL(currentRowChanged(int)), this, SLOT(themeSelected(int))); |
1210 | 84 |
|
1209 | 85 |
gbTLayout->addWidget(lwThemes); |
86 |
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); |
|
87 |
||
452 | 88 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
1215 | 89 |
|
90 |
setRandomSeed(); |
|
184 | 91 |
} |
92 |
||
93 |
void HWMapContainer::setImage(const QImage newImage) |
|
94 |
{ |
|
95 |
QPixmap px(256, 128); |
|
96 |
QPixmap pxres(256, 128); |
|
97 |
QPainter p(&pxres); |
|
98 |
||
99 |
px.fill(Qt::yellow); |
|
100 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
101 |
px.setMask(bm); |
|
102 |
||
103 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 104 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
105 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 106 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
107 |
p.drawPixmap(QPoint(0, 0), px); |
|
108 |
||
109 |
imageButt->setIcon(pxres); |
|
110 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 111 |
chooseMap->setCurrentIndex(0); |
112 |
} |
|
113 |
||
114 |
void HWMapContainer::mapChanged(int index) |
|
115 |
{ |
|
116 |
if(!index) { |
|
117 |
changeImage(); |
|
118 |
return; |
|
119 |
} |
|
120 |
||
331 | 121 |
loadMap(index); |
122 |
||
123 |
emit mapChanged(chooseMap->currentText()); |
|
124 |
} |
|
125 |
||
126 |
void HWMapContainer::loadMap(int index) |
|
127 |
{ |
|
249 | 128 |
QPixmap mapImage; |
320 | 129 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 130 |
changeImage(); |
131 |
chooseMap->setCurrentIndex(0); |
|
132 |
return; |
|
133 |
} |
|
320 | 134 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
135 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
136 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
137 |
QTextStream input(&mapCfgFile); |
|
138 |
input >> theme; |
|
139 |
mapCfgFile.close(); |
|
140 |
} |
|
184 | 141 |
} |
142 |
||
143 |
void HWMapContainer::changeImage() |
|
144 |
{ |
|
1214 | 145 |
pMap = new HWMap(); |
146 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
|
147 |
pMap->getImage(m_seed.toStdString()); |
|
148 |
||
149 |
if(!Themes->size()) return; |
|
150 |
quint32 themeNum = rand() % Themes->size(); |
|
151 |
lwThemes->setCurrentRow(themeNum); |
|
184 | 152 |
} |
153 |
||
1215 | 154 |
void HWMapContainer::themeSelected(int currentRow) |
155 |
{ |
|
156 |
theme = Themes->at(currentRow); |
|
1224 | 157 |
gbThemes->setIcon(QIcon(QString("%1/Themes/%2/icon.png").arg(datadir->absolutePath()).arg(theme))); |
1215 | 158 |
emit themeChanged(theme); |
159 |
} |
|
160 |
||
184 | 161 |
QString HWMapContainer::getCurrentSeed() const |
162 |
{ |
|
163 |
return m_seed; |
|
164 |
} |
|
165 |
||
249 | 166 |
QString HWMapContainer::getCurrentMap() const |
167 |
{ |
|
320 | 168 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 169 |
return chooseMap->currentText(); |
170 |
} |
|
171 |
||
172 |
QString HWMapContainer::getCurrentTheme() const |
|
173 |
{ |
|
320 | 174 |
return theme; |
249 | 175 |
} |
176 |
||
184 | 177 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
178 |
{ |
|
179 |
//imageButt->setIconSize(imageButt->size()); |
|
180 |
} |
|
320 | 181 |
|
182 |
void HWMapContainer::setSeed(const QString & seed) |
|
183 |
{ |
|
184 |
m_seed = seed; |
|
185 |
changeImage(); |
|
186 |
} |
|
187 |
||
188 |
void HWMapContainer::setMap(const QString & map) |
|
189 |
{ |
|
331 | 190 |
int id = chooseMap->findText(map); |
191 |
if(id >= 0) { |
|
192 |
chooseMap->setCurrentIndex(id); |
|
193 |
loadMap(id); |
|
194 |
} |
|
320 | 195 |
} |
196 |
||
197 |
void HWMapContainer::setTheme(const QString & theme) |
|
198 |
{ |
|
1215 | 199 |
QList<QListWidgetItem *> items = lwThemes->findItems(theme, Qt::MatchExactly); |
200 |
if(items.size()) |
|
201 |
lwThemes->setCurrentItem(items.at(0)); |
|
202 |
//this->theme = theme; |
|
320 | 203 |
} |
204 |
||
205 |
void HWMapContainer::setRandomSeed() |
|
206 |
{ |
|
207 |
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
|
208 |
emit seedChanged(m_seed); |
320 | 209 |
changeImage(); |
210 |
} |