QTfrontend/ui/widget/mapContainer.cpp
changeset 6938 217ed62e872c
parent 6937 7f77fa908a4e
child 6939 970389573788
equal deleted inserted replaced
6937:7f77fa908a4e 6938:217ed62e872c
    66     mapLayout->addWidget(imageButt, 0, 0, 1, 2);
    66     mapLayout->addWidget(imageButt, 0, 0, 1, 2);
    67     connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomMap()));
    67     connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomMap()));
    68 
    68 
    69     chooseMap = new QComboBox(mapWidget);
    69     chooseMap = new QComboBox(mapWidget);
    70     chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    70     chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    71 
    71     m_mapModel = DataManager::instance().mapModel();
    72     loadMapList();
    72     chooseMap->setModel(m_mapModel);
    73     connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(loadMapList()));
    73 
    74 
    74     // update model views after model changes (to e.g. re-adjust separators)
    75     connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
    75     connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(updateModelViews()));
       
    76 
    76     mapLayout->addWidget(chooseMap, 1, 1);
    77     mapLayout->addWidget(chooseMap, 1, 1);
    77 
    78 
    78     QLabel * lblMap = new QLabel(tr("Map"), mapWidget);
    79     QLabel * lblMap = new QLabel(tr("Map"), mapWidget);
    79     mapLayout->addWidget(lblMap, 1, 0);
    80     mapLayout->addWidget(lblMap, 1, 0);
    80 
    81 
   170     seedLabel->setVisible(false);
   171     seedLabel->setVisible(false);
   171     seedEdit->setVisible(false);
   172     seedEdit->setVisible(false);
   172 
   173 
   173     setRandomSeed();
   174     setRandomSeed();
   174     setRandomTheme();
   175     setRandomTheme();
       
   176 
       
   177     chooseMap->setCurrentIndex(0);
       
   178     m_mapInfo = MapModel::mapInfoFromData(chooseMap->itemData(0));
       
   179     mapChanged(0);
       
   180     connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
       
   181 
       
   182     updateModelViews();
   175 }
   183 }
   176 
   184 
   177 void HWMapContainer::setImage(const QImage newImage)
   185 void HWMapContainer::setImage(const QImage newImage)
   178 {
   186 {
   179     QPixmap px(256, 128);
   187     QPixmap px(256, 128);
   200     hhLimit = newHHLimit;
   208     hhLimit = newHHLimit;
   201 }
   209 }
   202 
   210 
   203 void HWMapContainer::mapChanged(int index)
   211 void HWMapContainer::mapChanged(int index)
   204 {
   212 {
   205     switch(index)
   213     m_mapInfo = MapModel::mapInfoFromData(chooseMap->itemData(chooseMap->currentIndex()));
   206     {
   214 
   207         case MAPGEN_REGULAR:
   215     switch(m_mapInfo.type)
       
   216     {
       
   217         case MapModel::GeneratedMap:
   208             mapgen = MAPGEN_REGULAR;
   218             mapgen = MAPGEN_REGULAR;
   209             updatePreview();
   219             updatePreview();
   210             gbThemes->show();
   220             gbThemes->show();
   211             lblFilter->show();
   221             lblFilter->show();
   212             cbTemplateFilter->show();
   222             cbTemplateFilter->show();
   213             maze_size_label->hide();
   223             maze_size_label->hide();
   214             cbMazeSize->hide();
   224             cbMazeSize->hide();
   215             emit mapChanged("+rnd+");
   225             emit mapChanged("+rnd+");
   216             emit themeChanged(chooseMap->itemData(index).toList()[1].toString());
   226             emit themeChanged(m_mapInfo.theme);
   217             break;
   227             break;
   218         case MAPGEN_MAZE:
   228         case MapModel::GeneratedMaze:
   219             mapgen = MAPGEN_MAZE;
   229             mapgen = MAPGEN_MAZE;
   220             updatePreview();
   230             updatePreview();
   221             gbThemes->show();
   231             gbThemes->show();
   222             lblFilter->hide();
   232             lblFilter->hide();
   223             cbTemplateFilter->hide();
   233             cbTemplateFilter->hide();
   224             maze_size_label->show();
   234             maze_size_label->show();
   225             cbMazeSize->show();
   235             cbMazeSize->show();
   226             emit mapChanged("+maze+");
   236             emit mapChanged("+maze+");
   227             emit themeChanged(chooseMap->itemData(index).toList()[1].toString());
   237             emit themeChanged(m_mapInfo.theme);
   228             break;
   238             break;
   229         case MAPGEN_DRAWN:
   239         case MapModel::HandDrawnMap:
   230             mapgen = MAPGEN_DRAWN;
   240             mapgen = MAPGEN_DRAWN;
   231             updatePreview();
   241             updatePreview();
   232             gbThemes->show();
   242             gbThemes->show();
   233             lblFilter->hide();
   243             lblFilter->hide();
   234             cbTemplateFilter->hide();
   244             cbTemplateFilter->hide();
   235             maze_size_label->hide();
   245             maze_size_label->hide();
   236             cbMazeSize->hide();
   246             cbMazeSize->hide();
   237             emit mapChanged("+drawn+");
   247             emit mapChanged("+drawn+");
   238             emit themeChanged(chooseMap->itemData(index).toList()[1].toString());
   248             emit themeChanged(m_mapInfo.theme);
   239             break;
   249             break;
   240         default:
   250         default:
   241             mapgen = MAPGEN_MAP;
   251             mapgen = MAPGEN_MAP;
   242             updatePreview();
   252             updatePreview();
   243             gbThemes->hide();
   253             gbThemes->hide();
   244             lblFilter->hide();
   254             lblFilter->hide();
   245             cbTemplateFilter->hide();
   255             cbTemplateFilter->hide();
   246             maze_size_label->hide();
   256             maze_size_label->hide();
   247             cbMazeSize->hide();
   257             cbMazeSize->hide();
   248             emit mapChanged(chooseMap->itemData(index).toList()[0].toString());
   258             emit mapChanged(m_mapInfo.name);
   249     }
   259     }
   250 
   260 
   251     emit mapgenChanged(mapgen);
   261     emit mapgenChanged(mapgen);
   252 }
   262 }
   253 
   263 
   314     return m_seed;
   324     return m_seed;
   315 }
   325 }
   316 
   326 
   317 QString HWMapContainer::getCurrentMap() const
   327 QString HWMapContainer::getCurrentMap() const
   318 {
   328 {
   319     if(chooseMap->currentIndex() < MAPGEN_MAP) return QString();
   329     return(m_mapInfo.name);
   320     return chooseMap->itemData(chooseMap->currentIndex()).toList()[0].toString();
       
   321 }
   330 }
   322 
   331 
   323 QString HWMapContainer::getCurrentTheme() const
   332 QString HWMapContainer::getCurrentTheme() const
   324 {
   333 {
   325     return chooseMap->itemData(chooseMap->currentIndex()).toList()[1].toString();
   334     return(m_mapInfo.theme);
   326 }
   335 }
   327 
   336 
   328 bool HWMapContainer::getCurrentIsMission() const
   337 bool HWMapContainer::getCurrentIsMission() const
   329 {
   338 {
   330     if(!chooseMap->currentIndex()) return false;
   339     return(m_mapInfo.type == MapModel::MissionMap);
   331     return chooseMap->itemData(chooseMap->currentIndex()).toList()[3].toBool();
       
   332 }
   340 }
   333 
   341 
   334 int HWMapContainer::getCurrentHHLimit() const
   342 int HWMapContainer::getCurrentHHLimit() const
   335 {
   343 {
   336     return hhLimit;
   344     return hhLimit;
   337 }
   345 }
   338 
   346 
   339 QString HWMapContainer::getCurrentScheme() const
   347 QString HWMapContainer::getCurrentScheme() const
   340 {
   348 {
   341     return chooseMap->itemData(chooseMap->currentIndex()).toList()[4].toString();
   349     return(m_mapInfo.scheme);
   342 }
   350 }
   343 
   351 
   344 QString HWMapContainer::getCurrentWeapons() const
   352 QString HWMapContainer::getCurrentWeapons() const
   345 {
   353 {
   346     return chooseMap->itemData(chooseMap->currentIndex()).toList()[5].toString();
   354     return(m_mapInfo.weapons);
   347 }
   355 }
   348 
   356 
   349 quint32 HWMapContainer::getTemplateFilter() const
   357 quint32 HWMapContainer::getTemplateFilter() const
   350 {
   358 {
   351     return cbTemplateFilter->itemData(cbTemplateFilter->currentIndex()).toInt();
   359     return cbTemplateFilter->itemData(cbTemplateFilter->currentIndex()).toInt();
   365 }
   373 }
   366 
   374 
   367 void HWMapContainer::setSeed(const QString & seed)
   375 void HWMapContainer::setSeed(const QString & seed)
   368 {
   376 {
   369     intSetSeed(seed);
   377     intSetSeed(seed);
   370     if (chooseMap->currentIndex() < MAPGEN_DRAWN)
   378     if ((m_mapInfo.type == MapModel::GeneratedMap) || (m_mapInfo.type == MapModel::GeneratedMaze))
   371         updatePreview();
   379         updatePreview();
   372 }
   380 }
   373 
   381 
   374 void HWMapContainer::intSetMap(const QString & map)
   382 void HWMapContainer::intSetMap(const QString & map)
   375 {
   383 {
   376     int id = 0;
   384     int id = 0;
   377     for(int i = 0; i < chooseMap->count(); i++)
   385     for(int i = 0; i < chooseMap->count(); i++)
   378         if(!chooseMap->itemData(i).isNull() && chooseMap->itemData(i).toList()[0].toString() == map)
   386     {
       
   387         MapModel::MapInfo mapInfo = MapModel::mapInfoFromData(chooseMap->itemData(i));
       
   388 
       
   389         if (mapInfo.name == map) 
   379         {
   390         {
   380             id = i;
   391             id = i;
   381             break;
   392             break;
   382         }
   393         }
       
   394     }
   383 
   395 
   384     if(id > 0)
   396     if(id > 0)
   385     {
   397     {
   386         if (pMap)
   398         if (pMap)
   387         {
   399         {
   408 }
   420 }
   409 
   421 
   410 void HWMapContainer::setRandomMap()
   422 void HWMapContainer::setRandomMap()
   411 {
   423 {
   412     setRandomSeed();
   424     setRandomSeed();
   413     switch(chooseMap->currentIndex())
   425     switch(m_mapInfo.type)
   414     {
   426     {
   415         case MAPGEN_REGULAR:
   427         case MapModel::GeneratedMap:
   416         case MAPGEN_MAZE:
   428         case MapModel::GeneratedMaze:
   417             setRandomTheme();
   429             setRandomTheme();
   418             break;
   430             break;
   419         case MAPGEN_DRAWN:
   431         case MapModel::HandDrawnMap:
   420             emit drawMapRequested();
   432             emit drawMapRequested();
   421             break;
   433             break;
   422         default:
   434         case MapModel::MissionMap:
   423             if(chooseMap->currentIndex() <= numMissions + MAPGEN_MAP + 1)
   435             setRandomMission();
   424                 setRandomMission();
   436             break;
   425             else
   437         case MapModel::StaticMap:
   426                 setRandomStatic();
   438             setRandomStatic();
   427             break;
   439             break;
       
   440         case MapModel::Invalid:
       
   441             Q_ASSERT(false);
   428     }
   442     }
   429 }
   443 }
   430 
   444 
   431 void HWMapContainer::setRandomStatic()
   445 void HWMapContainer::setRandomStatic()
   432 {
   446 {
   442     mapChanged(i);
   456     mapChanged(i);
   443 }
   457 }
   444 
   458 
   445 void HWMapContainer::setRandomSeed()
   459 void HWMapContainer::setRandomSeed()
   446 {
   460 {
   447     m_seed = QUuid::createUuid().toString();
   461     setSeed(QUuid::createUuid().toString());
   448     seedEdit->setText(m_seed);
       
   449     emit seedChanged(m_seed);
   462     emit seedChanged(m_seed);
   450     if (chooseMap->currentIndex() < MAPGEN_MAP)
       
   451         updatePreview();
       
   452 }
   463 }
   453 
   464 
   454 void HWMapContainer::setRandomTheme()
   465 void HWMapContainer::setRandomTheme()
   455 {
   466 {
   456     if(!m_themeModel->rowCount()) return;
   467     if(!m_themeModel->rowCount()) return;
   550     updatePreview();
   561     updatePreview();
   551 }
   562 }
   552 
   563 
   553 void HWMapContainer::updatePreview()
   564 void HWMapContainer::updatePreview()
   554 {
   565 {
   555     int curIndex = chooseMap->currentIndex();
   566     switch(m_mapInfo.type)
   556 
   567     {
   557     switch(curIndex)
   568         case MapModel::GeneratedMap:
   558     {
       
   559         case MAPGEN_REGULAR:
       
   560             askForGeneratedPreview();
   569             askForGeneratedPreview();
   561             break;
   570             break;
   562         case MAPGEN_MAZE:
   571         case MapModel::GeneratedMaze:
   563             askForGeneratedPreview();
   572             askForGeneratedPreview();
   564             break;
   573             break;
   565         case MAPGEN_DRAWN:
   574         case MapModel::HandDrawnMap:
   566             askForGeneratedPreview();
   575             askForGeneratedPreview();
   567             break;
   576             break;
   568         default:
   577         default:
   569             QPixmap mapImage;
   578             QPixmap mapImage;
   570             QFile tmpfile;
   579             QFile tmpfile;
   571             tmpfile.setFileName(cfgdir->absolutePath() + "/Data/Maps/" + chooseMap->itemData(curIndex).toList()[0].toString() + "/preview.png");
   580             tmpfile.setFileName(cfgdir->absolutePath() + "/Data/Maps/" + m_mapInfo.name + "/preview.png");
   572             if (!tmpfile.exists()) tmpfile.setFileName(datadir->absolutePath() + "/Maps/" + chooseMap->itemData(curIndex).toList()[0].toString() + "/preview.png");
   581             if (!tmpfile.exists()) tmpfile.setFileName(datadir->absolutePath() + "/Maps/" + m_mapInfo.name + "/preview.png");
   573             if(!mapImage.load(QFileInfo(tmpfile).absoluteFilePath()))
   582             if(!mapImage.load(QFileInfo(tmpfile).absoluteFilePath()))
   574             {
   583             {
   575                 imageButt->setIcon(QIcon());
   584                 imageButt->setIcon(QIcon());
   576                 return;
   585                 return;
   577             }
   586             }
   578 
   587 
   579             hhLimit = chooseMap->itemData(curIndex).toList()[2].toInt();
   588             hhLimit = m_mapInfo.limit;
   580             addInfoToPreview(mapImage);
   589             addInfoToPreview(mapImage);
   581     }
   590     }
   582 }
   591 }
   583 
   592 
   584 void HWMapContainer::setAllMapParameters(const QString &map, MapGenerator m, int mazesize, const QString &seed, int tmpl)
   593 void HWMapContainer::setAllMapParameters(const QString &map, MapGenerator m, int mazesize, const QString &seed, int tmpl)
   591 
   600 
   592     updatePreview();
   601     updatePreview();
   593 }
   602 }
   594 
   603 
   595 
   604 
   596 void HWMapContainer::loadMapList()
   605 void HWMapContainer::updateModelViews()
   597 {
   606 {
   598     // TODO: convert to model
   607     numMissions = m_mapModel->missionCount();
   599 
   608 
   600     // remember previous selection
   609     intSetMap(m_mapInfo.name);
   601     QString selMap = getCurrentMap();
   610 
   602 
   611 /*
   603     chooseMap->clear();
   612     int nGenMaps = m_mapModel->generatorCount();
   604 
   613 
   605     chooseMap->addItem(
   614     // insert double separator after random maps/mazes/etc
   606 // FIXME - need real icons. Disabling until then
   615     chooseMap->insertSeparator(nGenMaps);
   607 //QIcon(":/res/mapRandom.png"),
   616     chooseMap->insertSeparator(nGenMaps);
   608         QComboBox::tr("generated map..."));
   617 
   609     chooseMap->addItem(
   618     // separator between missions and regular maps
   610 // FIXME - need real icons. Disabling until then
   619     chooseMap->insertSeparator(nGenMaps + m_mapModel->missionCount());
   611 //QIcon(":/res/mapMaze.png"),
   620 */
   612         QComboBox::tr("generated maze..."));
   621 }
   613 
       
   614     chooseMap->addItem(QComboBox::tr("hand drawn map..."));
       
   615 
       
   616     chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
       
   617     chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
       
   618 
       
   619     int missionindex = chooseMap->count();
       
   620     numMissions = 0;
       
   621     QFile mapLuaFile;
       
   622     QFile mapCfgFile;
       
   623 
       
   624     DataManager & dataMgr = DataManager::instance();
       
   625 
       
   626     QStringList mapList = dataMgr.entryList(
       
   627                               QString("Maps"),
       
   628                               QDir::Dirs | QDir::NoDotAndDotDot
       
   629                           );
       
   630 
       
   631     foreach (QString map, mapList)
       
   632     {
       
   633         mapCfgFile.setFileName(
       
   634             dataMgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
       
   635         mapLuaFile.setFileName(
       
   636             dataMgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
       
   637 
       
   638         if (mapCfgFile.open(QFile::ReadOnly))
       
   639         {
       
   640             QString theme;
       
   641             quint32 limit = 0;
       
   642             QString scheme;
       
   643             QString weapons;
       
   644             QList<QVariant> mapInfo;
       
   645             bool isMission = mapLuaFile.exists();
       
   646 
       
   647             QTextStream input(&mapCfgFile);
       
   648             input >> theme;
       
   649             input >> limit;
       
   650             input >> scheme;
       
   651             input >> weapons;
       
   652             mapInfo.push_back(map);
       
   653             mapInfo.push_back(theme);
       
   654             if (limit)
       
   655                 mapInfo.push_back(limit);
       
   656             else
       
   657                 mapInfo.push_back(18);
       
   658 
       
   659 
       
   660             mapInfo.push_back(isMission);
       
   661 
       
   662             if (scheme.isEmpty())
       
   663                 scheme = "locked";
       
   664             scheme.replace("_", " ");
       
   665 
       
   666             if (weapons.isEmpty())
       
   667                 weapons = "locked";
       
   668             weapons.replace("_", " ");
       
   669 
       
   670             mapInfo.push_back(scheme);
       
   671             mapInfo.push_back(weapons);
       
   672 
       
   673             if(isMission)
       
   674             {
       
   675                 chooseMap->insertItem(missionindex++,
       
   676 // FIXME - need real icons. Disabling until then
       
   677 //QIcon(":/res/mapMission.png"),
       
   678                                       QComboBox::tr("Mission") + ": " + map, mapInfo);
       
   679                 numMissions++;
       
   680             }
       
   681             else
       
   682                 chooseMap->addItem(
       
   683 // FIXME - need real icons. Disabling until then
       
   684 //QIcon(":/res/mapCustom.png"),
       
   685                     map, mapInfo);
       
   686             mapCfgFile.close();
       
   687         }
       
   688     }
       
   689 
       
   690     chooseMap->insertSeparator(missionindex); // separator between missions and maps
       
   691 
       
   692     // if a map was selected already let's reselect it after reloading the map list
       
   693     if (!selMap.isEmpty()) {
       
   694         setMap(selMap);
       
   695     }
       
   696 }