Reorganize mapContainer widget. Fixes a bunch of bugs, improves widget.
authorunc0rr
Thu, 16 Oct 2008 16:54:44 +0000
changeset 1366 eba12c22f8ca
parent 1365 2ffe20744a4a
child 1367 e1aaa1a4901f
Reorganize mapContainer widget. Fixes a bunch of bugs, improves widget.
QTfrontend/mapContainer.cpp
QTfrontend/mapContainer.h
--- a/QTfrontend/mapContainer.cpp	Thu Oct 16 16:29:47 2008 +0000
+++ b/QTfrontend/mapContainer.cpp	Thu Oct 16 16:54:44 2008 +0000
@@ -39,30 +39,42 @@
 	mainLayout(this),
 	pMap(0)
 {
-#if QT_VERSION >= 0x040300
-  mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
-                1,
-                QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin),
-                QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
-#endif
-  imageButt = new QPushButton(this);
-  imageButt->setObjectName("imageButt");
-  imageButt->setFixedSize(256 + 6, 128 + 6);
-  imageButt->setFlat(true);
-  imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum);
-  mainLayout.addWidget(imageButt, 0, 0, 1, 2);
-  connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed()));
-  connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomTheme()));
+	mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
+		1,
+		QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin),
+		QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
+	imageButt = new QPushButton(this);
+	imageButt->setObjectName("imageButt");
+	imageButt->setFixedSize(256 + 6, 128 + 6);
+	imageButt->setFlat(true);
+	imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum);
+	mainLayout.addWidget(imageButt, 0, 0, 1, 2);
+	connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed()));
+	connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomTheme()));
 
-  chooseMap = new QComboBox(this);
-  chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
-  chooseMap->addItem(QComboBox::tr("generated map..."));
-  chooseMap->addItems(*mapList);
-  connect(chooseMap, SIGNAL(currentIndexChanged(int)), this, SLOT(mapChanged(int)));
-  mainLayout.addWidget(chooseMap, 1, 1);
+	chooseMap = new QComboBox(this);
+	chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
+	chooseMap->addItem(QComboBox::tr("generated map..."));
+	for (int i = 0; i < mapList->size(); ++i) {
+		QString map = (*mapList)[i];
+		QFile mapCfgFile(
+				QString("%1/Maps/%2/map.cfg")
+				.arg(datadir->absolutePath())
+				.arg(map));
+		if (mapCfgFile.open(QFile::ReadOnly)) {
+			QString theme;
+			QTextStream input(&mapCfgFile);
+			input >> theme;
+			chooseMap->addItem(map, theme);
+			mapCfgFile.close();
+		}
+	}
+	
+	connect(chooseMap, SIGNAL(currentIndexChanged(int)), this, SLOT(mapChanged(int)));
+	mainLayout.addWidget(chooseMap, 1, 1);
 
-  QLabel * lblMap = new QLabel(tr("Map"), this);
-  mainLayout.addWidget(lblMap, 1, 0);
+	QLabel * lblMap = new QLabel(tr("Map"), this);
+	mainLayout.addWidget(lblMap, 1, 0);
 
 	gbThemes = new IconedGroupBox(this);
 	gbThemes->setTitleTextPadding(60);
@@ -88,62 +100,58 @@
 	gbTLayout->addWidget(lwThemes);
 	lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
 	
-  mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize
+	mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize
 
-  setRandomSeed();
-  setRandomTheme();
+	setRandomSeed();
+	setRandomTheme();
 }
 
 void HWMapContainer::setImage(const QImage newImage)
 {
-  QPixmap px(256, 128);
-  QPixmap pxres(256, 128);
-  QPainter p(&pxres);
+	QPixmap px(256, 128);
+	QPixmap pxres(256, 128);
+	QPainter p(&pxres);
 
-  px.fill(Qt::yellow);
-  QBitmap bm = QBitmap::fromImage(newImage);
-  px.setMask(bm);
+	px.fill(Qt::yellow);
+	QBitmap bm = QBitmap::fromImage(newImage);
+	px.setMask(bm);
 
-  QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
-  linearGrad.setColorAt(1, QColor(0, 0, 192));
-  linearGrad.setColorAt(0, QColor(66, 115, 225));
-  p.fillRect(QRect(0, 0, 256, 128), linearGrad);
-  p.drawPixmap(QPoint(0, 0), px);
+	QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
+	linearGrad.setColorAt(1, QColor(0, 0, 192));
+	linearGrad.setColorAt(0, QColor(66, 115, 225));
+	p.fillRect(QRect(0, 0, 256, 128), linearGrad);
+	p.drawPixmap(QPoint(0, 0), px);
 
-  imageButt->setIcon(pxres);
-  imageButt->setIconSize(QSize(256, 128));
-  chooseMap->setCurrentIndex(0);
-  pMap = 0;
+	imageButt->setIcon(pxres);
+	imageButt->setIconSize(QSize(256, 128));
+	chooseMap->setCurrentIndex(0);
+	pMap = 0;
 }
 
 void HWMapContainer::mapChanged(int index)
 {
-  if(!index) {
-    changeImage();
-    emit mapChanged("+rnd+");
-    return;
-  }
-
-  loadMap(index);
-
-  emit mapChanged(chooseMap->currentText());
+	if(!index) {
+		changeImage();
+		gbThemes->show();
+		emit mapChanged("+rnd+");
+		emit themeChanged(chooseMap->itemData(0).toString());
+	} else
+	{
+		loadMap(index);
+		gbThemes->hide();
+		emit mapChanged(chooseMap->currentText());
+	}
 }
 
 void HWMapContainer::loadMap(int index)
 {
-  QPixmap mapImage;
-  if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/preview.png")) {
-    changeImage();
-    chooseMap->setCurrentIndex(0);
-    return;
-  }
-  imageButt->setIcon(mapImage);
-  QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg");
-  if (mapCfgFile.open(QFile::ReadOnly)) {
-    QTextStream input(&mapCfgFile);
-    input >> theme;
-    mapCfgFile.close();
-  }
+	QPixmap mapImage;
+	if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/preview.png")) {
+		changeImage();
+		chooseMap->setCurrentIndex(0);
+		return;
+	}
+	imageButt->setIcon(mapImage);
 }
 
 void HWMapContainer::changeImage()
@@ -155,7 +163,8 @@
 
 void HWMapContainer::themeSelected(int currentRow)
 {
-	theme = Themes->at(currentRow);
+	QString theme = Themes->at(currentRow);
+	chooseMap->setItemData(0, theme);
 	gbThemes->setIcon(QIcon(QString("%1/Themes/%2/icon.png").arg(datadir->absolutePath()).arg(theme)));
 	emit themeChanged(theme);
 }
@@ -173,7 +182,7 @@
 
 QString HWMapContainer::getCurrentTheme() const
 {
-	return theme;
+	return chooseMap->itemData(chooseMap->currentIndex()).toString();
 }
 
 void HWMapContainer::resizeEvent ( QResizeEvent * event )
--- a/QTfrontend/mapContainer.h	Thu Oct 16 16:29:47 2008 +0000
+++ b/QTfrontend/mapContainer.h	Thu Oct 16 16:54:44 2008 +0000
@@ -73,7 +73,6 @@
   QListWidget* lwThemes;
   HWMap* pMap;
   QString m_seed;
-  QString theme;
 
   void loadMap(int index);
 };