QTfrontend/mapContainer.cpp
author unc0rr
Sun, 19 Nov 2006 12:23:02 +0000
changeset 251 e4058e1ba394
parent 249 ff85fa029541
child 314 83773ccf4f09
permissions -rw-r--r--
Add translation

/*
 * Hedgewars, a worms-like game
 * Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "mapContainer.h"

#include <QPushButton>
#include <QBuffer>
#include <QUuid>
#include <QBitmap>
#include <QPainter>
#include <QLinearGradient>
#include <QColor>
#include <QTextStream>

#include "hwconsts.h"

HWMapContainer::HWMapContainer(QWidget * parent) :
  QWidget(parent), mainLayout(this)
{
  imageButt=new QPushButton(this);
  imageButt->setMaximumSize(256, 128);
  imageButt->setFlat(true);
  imageButt->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  mainLayout.addWidget(imageButt);
  connect(imageButt, SIGNAL(clicked()), this, SLOT(changeImage()));
  changeImage();

  chooseMap=new QComboBox(this);
  QDir tmpdir;
  tmpdir.cd(datadir->absolutePath());
  tmpdir.cd("Maps");
  tmpdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  QStringList mapList=tmpdir.entryList(QStringList("*"));
  mapList.push_front(QComboBox::tr("generated map..."));
  chooseMap->addItems(mapList);
  connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));

  mainLayout.addWidget(chooseMap);
}

void HWMapContainer::setImage(const QImage newImage)
{
  QPixmap px(256, 128);
  QPixmap pxres(256, 128);
  QPainter p(&pxres);

  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);

  imageButt->setIcon(pxres);
  imageButt->setIconSize(QSize(256, 128));
  chooseMap->setCurrentIndex(0);
}

void HWMapContainer::mapChanged(int index)
{
  if(!index) {
    changeImage();
    return;
  }

  QPixmap mapImage;
  if(!mapImage.load(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.png")) {
    changeImage();
    chooseMap->setCurrentIndex(0);
    return;
  }
  imageButt->setIcon(mapImage.scaled(256,128));
}

void HWMapContainer::changeImage()
{
  pMap=new HWMap();
  connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
  m_seed = QUuid::createUuid().toString();
  pMap->getImage(m_seed.toStdString());
}

QString HWMapContainer::getCurrentSeed() const
{
  return m_seed;
}

QString HWMapContainer::getCurrentMap() const
{
  if(!chooseMap->currentIndex()) throw MapFileErrorException();
  return chooseMap->currentText();
}

QString HWMapContainer::getCurrentTheme() const
{
  if(!chooseMap->currentIndex()) throw MapFileErrorException();
  QFile mapCfgFile(datadir->absolutePath()+"/Maps/"+chooseMap->currentText()+"/map.cfg");
  if (mapCfgFile.open(QFile::ReadOnly)) {
    QTextStream input(&mapCfgFile);
    QString theme;
    input >> theme;
    if(theme.length()>256) throw MapFileErrorException(); // theme name too long
    return theme;
    mapCfgFile.close();
  } else {
    throw MapFileErrorException();
  }
}

void HWMapContainer::resizeEvent ( QResizeEvent * event )
{
  //imageButt->setIconSize(imageButt->size());
}