QTfrontend/ui/widget/mapContainer.cpp
branchios-develop
changeset 13413 ba39a1d396c0
parent 13266 df1db657bbb4
child 13676 05fde8e30041
equal deleted inserted replaced
13411:6e8b807bda4b 13413:ba39a1d396c0
    62     m_staticViewSetup = false;
    62     m_staticViewSetup = false;
    63     m_script = QString();
    63     m_script = QString();
    64     m_prevMapFeatureSize = 12;
    64     m_prevMapFeatureSize = 12;
    65     m_mapFeatureSize = 12;
    65     m_mapFeatureSize = 12;
    66     m_withoutDLC = false;
    66     m_withoutDLC = false;
       
    67     m_missingMap = false;
    67 
    68 
    68     hhSmall.load(":/res/hh_small.png");
    69     hhSmall.load(":/res/hh_small.png");
    69     hhLimit = 18;
    70     hhLimit = 18;
    70     templateFilter = 0;
    71     templateFilter = 0;
    71     m_master = true;
    72     m_master = true;
    72 
    73 
    73     linearGrad = QLinearGradient(QPoint(128, 0), QPoint(128, 128));
    74     linearGradNormal = QLinearGradient(QPoint(128, 0), QPoint(128, 128));
    74     linearGrad.setColorAt(1, QColor(0, 0, 192));
    75     linearGradNormal.setColorAt(1, QColor(0, 0, 192));
    75     linearGrad.setColorAt(0, QColor(66, 115, 225));
    76     linearGradNormal.setColorAt(0, QColor(66, 115, 225));
       
    77 
       
    78     linearGradLoading = QLinearGradient(QPoint(128, 0), QPoint(128, 128));
       
    79     linearGradLoading.setColorAt(1, QColor(58, 58, 137));
       
    80     linearGradLoading.setColorAt(0, QColor(90, 109, 153));
       
    81 
       
    82     linearGradMapError = QLinearGradient(QPoint(128, 0), QPoint(128, 128));
       
    83     linearGradMapError.setColorAt(1, QColor(255, 1, 0));
       
    84     linearGradMapError.setColorAt(0, QColor(255, 119, 0));
       
    85 
       
    86     linearGradNoPreview = QLinearGradient(QPoint(128, 0), QPoint(128, 128));
       
    87     linearGradNoPreview.setColorAt(1, QColor(15, 9, 72));
       
    88     linearGradNoPreview.setColorAt(0, QColor(15, 9, 72));
    76 
    89 
    77     mainLayout.setContentsMargins(HWApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
    90     mainLayout.setContentsMargins(HWApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
    78                                   10,
    91                                   10,
    79                                   HWApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin),
    92                                   HWApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin),
    80                                   HWApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
    93                                   HWApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
    88     QWidget * topWidget = new QWidget();
   101     QWidget * topWidget = new QWidget();
    89     QHBoxLayout * topLayout = new QHBoxLayout(topWidget);
   102     QHBoxLayout * topLayout = new QHBoxLayout(topWidget);
    90     topWidget->setContentsMargins(0, 0, 0, 0);
   103     topWidget->setContentsMargins(0, 0, 0, 0);
    91     topLayout->setContentsMargins(0, 0, 0, 0);
   104     topLayout->setContentsMargins(0, 0, 0, 0);
    92 
   105 
    93     QHBoxLayout * twoColumnLayout = new QHBoxLayout();
   106     twoColumnLayout = new QHBoxLayout();
    94     QVBoxLayout * leftLayout = new QVBoxLayout();
   107     QVBoxLayout * leftLayout = new QVBoxLayout();
       
   108     leftLayout->setAlignment(Qt::AlignLeft);
    95     QVBoxLayout * rightLayout = new QVBoxLayout();
   109     QVBoxLayout * rightLayout = new QVBoxLayout();
    96     twoColumnLayout->addLayout(leftLayout, 0);
   110     twoColumnLayout->addLayout(leftLayout, 0);
    97     twoColumnLayout->addStretch(1);
       
    98     twoColumnLayout->addLayout(rightLayout, 0);
   111     twoColumnLayout->addLayout(rightLayout, 0);
    99     QVBoxLayout * drawnControls = new QVBoxLayout();
   112     QVBoxLayout * drawnControls = new QVBoxLayout();
   100 
   113 
       
   114     /* Map type label */
       
   115 
       
   116     QLabel* lblMapType = new QLabel(tr("Map type:"));
       
   117     topLayout->setSpacing(10);
       
   118     topLayout->addWidget(lblMapType, 0);
       
   119     m_childWidgets << lblMapType;
       
   120 
   101     /* Map type combobox */
   121     /* Map type combobox */
   102 
   122 
   103     topLayout->setSpacing(10);
       
   104     topLayout->addWidget(new QLabel(tr("Map type:")), 0);
       
   105     cType = new QComboBox(this);
   123     cType = new QComboBox(this);
   106     topLayout->addWidget(cType, 1);
   124     topLayout->addWidget(cType, 1);
   107     cType->insertItem(0, tr("Image map"), MapModel::StaticMap);
   125     cType->insertItem(0, tr("Image map"), MapModel::StaticMap);
   108     cType->insertItem(1, tr("Mission map"), MapModel::MissionMap);
   126     cType->insertItem(1, tr("Mission map"), MapModel::MissionMap);
   109     cType->insertItem(2, tr("Hand-drawn"), MapModel::HandDrawnMap);
   127     cType->insertItem(2, tr("Hand-drawn"), MapModel::HandDrawnMap);
   146     /* Map preview label */
   164     /* Map preview label */
   147 
   165 
   148     QLabel * lblMapPreviewText = new QLabel(this);
   166     QLabel * lblMapPreviewText = new QLabel(this);
   149     lblMapPreviewText->setText(tr("Map preview:"));
   167     lblMapPreviewText->setText(tr("Map preview:"));
   150     leftLayout->addWidget(lblMapPreviewText, 0);
   168     leftLayout->addWidget(lblMapPreviewText, 0);
       
   169     m_childWidgets << lblMapPreviewText;
   151 
   170 
   152     /* Map Preview */
   171     /* Map Preview */
   153 
   172 
   154     mapPreview = new QPushButton(this);
   173     mapPreview = new QPushButton(this);
   155     mapPreview->setObjectName("mapPreview");
   174     mapPreview->setObjectName("mapPreview");
   156     mapPreview->setFlat(true);
   175     mapPreview->setFlat(true);
   157     mapPreview->setFixedSize(256 + 6, 128 + 6);
   176     mapPreview->setFixedSize(256 + 6, 128 + 6);
   158     mapPreview->setContentsMargins(0, 0, 0, 0);
   177     mapPreview->setContentsMargins(0, 0, 0, 0);
   159     leftLayout->addWidget(mapPreview, 0);
   178     leftLayout->addWidget(mapPreview, 0);
   160     connect(mapPreview, SIGNAL(clicked()), this, SLOT(previewClicked()));
   179     connect(mapPreview, SIGNAL(clicked()), this, SLOT(previewClicked()));
       
   180     m_childWidgets << mapPreview;
   161 
   181 
   162     /* Bottom-Left layout */
   182     /* Bottom-Left layout */
   163 
   183 
   164     QVBoxLayout * bottomLeftLayout = new QVBoxLayout();
   184     QVBoxLayout * bottomLeftLayout = new QVBoxLayout();
   165     leftLayout->addLayout(bottomLeftLayout, 1);
   185     leftLayout->addLayout(bottomLeftLayout, 1);
   166 
   186 
   167     /* Map list label */
   187     /* Map list label */
   168 
   188 
   169     lblMapList = new QLabel(this);
   189     lblMapList = new QLabel(this);
       
   190     lblMapList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
       
   191     lblMapList->setAlignment(Qt::AlignTop | Qt::AlignLeft);
   170     rightLayout->addWidget(lblMapList, 0);
   192     rightLayout->addWidget(lblMapList, 0);
       
   193     m_childWidgets << lblMapList;
   171 
   194 
   172     /* Static maps list */
   195     /* Static maps list */
   173 
   196 
   174     staticMapList = new QListView(this);
   197     staticMapList = new QListView(this);
   175     rightLayout->addWidget(staticMapList, 1);
   198     rightLayout->addWidget(staticMapList, 1);
   178     /* Mission maps list */
   201     /* Mission maps list */
   179 
   202 
   180     missionMapList = new QListView(this);
   203     missionMapList = new QListView(this);
   181     rightLayout->addWidget(missionMapList, 1);
   204     rightLayout->addWidget(missionMapList, 1);
   182     m_childWidgets << missionMapList;
   205     m_childWidgets << missionMapList;
       
   206 
       
   207     /* Map name (when not room master) */
       
   208     /* We use a QTextEdit instead of QLabel because it is able
       
   209        to wrap at any character. */
       
   210     teMapName = new QTextEdit(this);
       
   211     teMapName->setObjectName("mapName");
       
   212     teMapName->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
       
   213     teMapName->setAlignment(Qt::AlignTop | Qt::AlignLeft);
       
   214 
       
   215     /* Boilerplate to emulate a QLabel */
       
   216     teMapName->setReadOnly(true);
       
   217     teMapName->setAcceptRichText(false);
       
   218     teMapName->setFrameStyle(QFrame::NoFrame);
       
   219     teMapName->setStyleSheet("background-color: transparent");
       
   220 
       
   221     teMapName->setLineWrapMode(QTextEdit::WidgetWidth);
       
   222     teMapName->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
       
   223 
       
   224     rightLayout->addWidget(teMapName, 1);
       
   225     m_childWidgets << teMapName;
   183 
   226 
   184     /* Map load and edit buttons */
   227     /* Map load and edit buttons */
   185 
   228 
   186     drawnControls->addStretch(1);
   229     drawnControls->addStretch(1);
   187 
   230 
   232     //mapFeatureSize->setTickPosition(QSlider::TicksBelow);
   275     //mapFeatureSize->setTickPosition(QSlider::TicksBelow);
   233     mapFeatureSize->setMaximum(25);
   276     mapFeatureSize->setMaximum(25);
   234     mapFeatureSize->setMinimum(1);
   277     mapFeatureSize->setMinimum(1);
   235     //mapFeatureSize->setFixedWidth(259);
   278     //mapFeatureSize->setFixedWidth(259);
   236     mapFeatureSize->setValue(m_mapFeatureSize);
   279     mapFeatureSize->setValue(m_mapFeatureSize);
   237     mapFeatureSize->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   280     mapFeatureSize->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
   238     bottomLeftLayout->addWidget(mapFeatureSize, 0);
   281     bottomLeftLayout->addWidget(mapFeatureSize, 0);
   239     connect(mapFeatureSize, SIGNAL(valueChanged(int)), this, SLOT(setFeatureSize(int)));
   282     connect(mapFeatureSize, SIGNAL(valueChanged(int)), this, SLOT(setFeatureSize(int)));
   240     m_childWidgets << mapFeatureSize;
   283     m_childWidgets << mapFeatureSize;
   241 
   284 
   242     /* Mission description */
   285     /* Mission description */
   243 
   286 
   244     lblDesc = new QLabel();
   287     lblDesc = new QLabel();
   245     lblDesc->setWordWrap(true);
   288     lblDesc->setWordWrap(true);
   246     lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   289     lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   247     lblDesc->setAlignment(Qt::AlignTop | Qt::AlignLeft);
   290     lblDesc->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
   248     lblDesc->setStyleSheet("font: 10px;");
   291     lblDesc->setStyleSheet("font: 10px;");
   249     bottomLeftLayout->addWidget(lblDesc, 100);
   292     bottomLeftLayout->addWidget(lblDesc, 100);
   250 
   293 
   251     /* Add stretch above theme button */
   294     /* Add stretch above theme button */
   252 
   295 
   290     staticMapChanged(m_staticMapModel->index(0, 0));
   333     staticMapChanged(m_staticMapModel->index(0, 0));
   291     missionMapChanged(m_missionMapModel->index(0, 0));
   334     missionMapChanged(m_missionMapModel->index(0, 0));
   292     changeMapType(MapModel::GeneratedMap);
   335     changeMapType(MapModel::GeneratedMap);
   293 }
   336 }
   294 
   337 
       
   338 void HWMapContainer::onImageReceived(const QPixmap &newImage)
       
   339 {
       
   340     // When image received from the engine.
       
   341     switch (m_mapInfo.type)
       
   342     {
       
   343         case MapModel::GeneratedMap:
       
   344         case MapModel::GeneratedMaze:
       
   345         case MapModel::GeneratedPerlin:
       
   346         case MapModel::HandDrawnMap:
       
   347         case MapModel::FortsMap:
       
   348             setImage(newImage);
       
   349             break;
       
   350         // Throw away image if we have switched the map mode in the meantime
       
   351         default:
       
   352             return;
       
   353     }
       
   354 }
       
   355 
   295 void HWMapContainer::setImage(const QPixmap &newImage)
   356 void HWMapContainer::setImage(const QPixmap &newImage)
   296 {
   357 {
   297     addInfoToPreview(newImage);
   358     addInfoToPreview(newImage);
   298     pMap = 0;
   359     pMap = 0;
   299 
   360 
   300     cType->setEnabled(isMaster());
   361     cType->setEnabled(isMaster());
   301 }
   362 }
   302 
   363 
       
   364 void HWMapContainer::setImage(const QPixmap &newImage, const QLinearGradient &linearGrad, bool showHHLimit)
       
   365 {
       
   366     addInfoToPreview(newImage, linearGrad, showHHLimit);
       
   367 
       
   368     pMap = 0;
       
   369 
       
   370     cType->setEnabled(isMaster());
       
   371 }
       
   372 
       
   373 
   303 void HWMapContainer::setHHLimit(int newHHLimit)
   374 void HWMapContainer::setHHLimit(int newHHLimit)
   304 {
   375 {
   305     hhLimit = newHHLimit;
   376     hhLimit = newHHLimit;
   306 }
   377 }
   307 
   378 
       
   379 void HWMapContainer::addInfoToPreview(const QPixmap &image)
       
   380 {
       
   381     addInfoToPreview(image, linearGradNormal, true);
       
   382 }
       
   383 
   308 // Should this add text to identify map size?
   384 // Should this add text to identify map size?
   309 void HWMapContainer::addInfoToPreview(const QPixmap &image)
   385 void HWMapContainer::addInfoToPreview(const QPixmap &image, const QLinearGradient &linearGrad, bool drawHHLimit)
   310 {
   386 {
   311     QPixmap finalImage = QPixmap(image.size());
   387     QPixmap finalImage = QPixmap(image.size());
   312 //finalImage.fill(QColor(0, 0, 0, 0));
       
   313 
       
   314     QPainter p(&finalImage);
   388     QPainter p(&finalImage);
       
   389 
   315     p.fillRect(finalImage.rect(), linearGrad);
   390     p.fillRect(finalImage.rect(), linearGrad);
   316     p.drawPixmap(finalImage.rect(), image);
   391     p.drawPixmap(finalImage.rect(), image);
   317     //p.setPen(QColor(0xf4,0x9e,0xe9));
   392 
   318     p.setPen(QColor(0xff,0xcc,0x00));
   393     if (drawHHLimit)
   319     p.setBrush(QColor(0, 0, 0));
   394     {
   320     p.drawRect(finalImage.rect().width() - hhSmall.rect().width() - 28, 3, 40, 20);
   395         p.setPen(QColor(0xff,0xcc,0x00));
   321     p.setFont(QFont("MS Shell Dlg", 10));
   396         p.setBrush(QColor(0, 0, 0));
   322     QString text = (hhLimit > 0) ? QString::number(hhLimit) : "?";
   397         p.setFont(QFont("MS Shell Dlg", 10));
   323     p.drawText(finalImage.rect().width() - hhSmall.rect().width() - 14 - (hhLimit > 9 ? 10 : 0), 18, text);
   398 
   324     p.drawPixmap(finalImage.rect().width() - hhSmall.rect().width() - 5, 5, hhSmall.rect().width(), hhSmall.rect().height(), hhSmall);
   399         p.drawRect(finalImage.rect().width() - hhSmall.rect().width() - 28, 3, 40, 20);
   325 
   400 
   326     // Shrink, crop, and center preview image
   401         QString text = (hhLimit > 0) ? QString::number(hhLimit) : "?";
   327     /*QPixmap centered(QSize(m_previewSize.width() - 6, m_previewSize.height() - 6));
   402         p.drawText(finalImage.rect().width() - hhSmall.rect().width() - 14 - (hhLimit > 9 ? 10 : 0), 18, text);
   328     QPainter pc(&centered);
   403         p.drawPixmap(finalImage.rect().width() - hhSmall.rect().width() - 5, 5, hhSmall.rect().width(), hhSmall.rect().height(), hhSmall);
   329     pc.fillRect(centered.rect(), linearGrad);
   404     }
   330     pc.drawPixmap(-3, -3, finalImage);*/
   405 
   331 
   406     // Set the map preview image. Make sure it is always colored the same,
   332     mapPreview->setIcon(QIcon(finalImage));
   407     // no matter if disabled or not.
       
   408     QIcon mapPreviewIcon = QIcon();
       
   409     mapPreviewIcon.addPixmap(finalImage, QIcon::Normal);
       
   410     mapPreviewIcon.addPixmap(finalImage, QIcon::Disabled);
       
   411     mapPreview->setIcon(mapPreviewIcon);
   333     mapPreview->setIconSize(finalImage.size());
   412     mapPreview->setIconSize(finalImage.size());
   334 }
   413 }
   335 
   414 
   336 void HWMapContainer::askForGeneratedPreview()
   415 void HWMapContainer::askForGeneratedPreview()
   337 {
   416 {
   338     pMap = new HWMap(this);
   417     pMap = new HWMap(this);
   339     connect(pMap, SIGNAL(ImageReceived(QPixmap)), this, SLOT(setImage(QPixmap)));
   418     connect(pMap, SIGNAL(ImageReceived(QPixmap)), this, SLOT(onImageReceived(const QPixmap)));
   340     connect(pMap, SIGNAL(HHLimitReceived(int)), this, SLOT(setHHLimit(int)));
   419     connect(pMap, SIGNAL(HHLimitReceived(int)), this, SLOT(setHHLimit(int)));
   341     connect(pMap, SIGNAL(destroyed(QObject *)), this, SLOT(onPreviewMapDestroyed(QObject *)));
   420     connect(pMap, SIGNAL(destroyed(QObject *)), this, SLOT(onPreviewMapDestroyed(QObject *)));
   342     pMap->getImage(m_seed,
   421     pMap->getImage(m_seed,
   343                    getTemplateFilter(),
   422                    getTemplateFilter(),
   344                    get_mapgen(),
   423                    get_mapgen(),
   349 		           m_mapFeatureSize
   428 		           m_mapFeatureSize
   350                   );
   429                   );
   351 
   430 
   352     setHHLimit(0);
   431     setHHLimit(0);
   353 
   432 
       
   433     QPixmap waitImage(m_previewSize);
       
   434     waitImage.fill(Qt::transparent);
       
   435 
       
   436     QPainter p(&waitImage);
   354     const QPixmap waitIcon(":/res/iconTime.png");
   437     const QPixmap waitIcon(":/res/iconTime.png");
   355 
       
   356     QPixmap waitImage(m_previewSize);
       
   357     QPainter p(&waitImage);
       
   358 
       
   359     p.fillRect(waitImage.rect(), linearGrad);
       
   360     int x = (waitImage.width() - waitIcon.width()) / 2;
   438     int x = (waitImage.width() - waitIcon.width()) / 2;
   361     int y = (waitImage.height() - waitIcon.height()) / 2;
   439     int y = (waitImage.height() - waitIcon.height()) / 2;
   362     p.drawPixmap(QPoint(x, y), waitIcon);
   440     p.drawPixmap(QPoint(x, y), waitIcon);
   363 
   441 
   364     addInfoToPreview(waitImage);
   442     setImage(waitImage, linearGradLoading, false);
   365 
   443 
   366     cType->setEnabled(false);
   444     cType->setEnabled(false);
   367 }
   445 }
   368 
   446 
   369 void HWMapContainer::previewClicked()
   447 void HWMapContainer::previewClicked()
   484     {
   562     {
   485         //nuffin
   563         //nuffin
   486     }
   564     }
   487     else if (m_staticMapModel->mapExists(map))
   565     else if (m_staticMapModel->mapExists(map))
   488     {
   566     {
       
   567         m_missingMap = false;
   489         changeMapType(MapModel::StaticMap, m_staticMapModel->index(m_staticMapModel->findMap(map), 0));
   568         changeMapType(MapModel::StaticMap, m_staticMapModel->index(m_staticMapModel->findMap(map), 0));
   490     }
   569     }
   491     else if (m_missionMapModel->mapExists(map))
   570     else if (m_missionMapModel->mapExists(map))
   492     {
   571     {
       
   572         m_missingMap = false;
   493         changeMapType(MapModel::MissionMap, m_missionMapModel->index(m_missionMapModel->findMap(map), 0));
   573         changeMapType(MapModel::MissionMap, m_missionMapModel->index(m_missionMapModel->findMap(map), 0));
   494     } else
   574     } else
   495     {
   575     {
   496         qDebug() << "HWMapContainer::intSetMap: Map doesn't exist: " << map;
   576         qDebug() << "HWMapContainer::intSetMap: Map doesn't exist: " << map;
       
   577         m_missingMap = true;
       
   578         m_curMap = map;
       
   579         m_mapInfo.name = map;
       
   580         setMapNameLabel(map, false);
       
   581         if (m_mapInfo.type == MapModel::StaticMap)
       
   582             setupStaticMapsView(m_curMap);
       
   583         else if (m_mapInfo.type == MapModel::MissionMap)
       
   584             setupMissionMapsView(m_curMap);
       
   585         else
       
   586         {
       
   587             m_mapInfo.type = MapModel::StaticMap;
       
   588             setupStaticMapsView(m_curMap);
       
   589             changeMapType(m_mapInfo.type, QModelIndex());
       
   590         }
       
   591         updatePreview();
   497     }
   592     }
   498 }
   593 }
   499 
   594 
   500 void HWMapContainer::setMap(const QString & map)
   595 void HWMapContainer::setMap(const QString & map)
   501 {
   596 {
   502     if ((m_mapInfo.type == MapModel::Invalid) || (map != m_mapInfo.name))
   597     if ((m_mapInfo.type == MapModel::Invalid) || (map != m_mapInfo.name) || m_missingMap)
   503         intSetMap(map);
   598         intSetMap(map);
   504 }
   599 }
   505 
   600 
   506 void HWMapContainer::setTheme(const QString & theme)
   601 void HWMapContainer::setTheme(const QString & theme)
   507 {
   602 {
   508     QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), ThemeModel::ActualNameRole, theme);
   603     QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), ThemeModel::ActualNameRole, theme);
   509 
   604 
   510     if(mdl.size())
   605     if(mdl.size())
   511         updateTheme(mdl.at(0));
   606         updateTheme(mdl.at(0));
   512     else
   607     else
   513         intSetIconlessTheme(theme);
   608         setMissingTheme(theme);
   514 }
   609 }
   515 
   610 
   516 void HWMapContainer::setRandomMap()
   611 void HWMapContainer::setRandomMap()
   517 {
   612 {
   518     if (!m_master) return;
   613     if (!m_master) return;
   566 void HWMapContainer::setRandomTheme()
   661 void HWMapContainer::setRandomTheme()
   567 {
   662 {
   568     QAbstractItemModel * tmodel;
   663     QAbstractItemModel * tmodel;
   569 
   664 
   570     if (m_withoutDLC)
   665     if (m_withoutDLC)
   571         tmodel = m_themeModel->withoutDLC();
   666         tmodel = m_themeModel->withoutDLCOrHidden();
   572     else
   667     else
   573         tmodel = m_themeModel;
   668         tmodel = m_themeModel->withoutHidden();
   574 
   669 
   575     if(!tmodel->rowCount()) return;
   670     if(!tmodel->rowCount()) return;
   576     quint32 themeNum = rand() % tmodel->rowCount();
   671     quint32 themeNum = rand() % tmodel->rowCount();
   577     updateTheme(tmodel->index(themeNum,0));
   672     updateTheme(tmodel->index(themeNum,0));
   578     emit themeChanged(m_theme);
   673     emit themeChanged(m_theme);
   699     updatePreview();
   794     updatePreview();
   700 }
   795 }
   701 
   796 
   702 void HWMapContainer::showEvent(QShowEvent * event)
   797 void HWMapContainer::showEvent(QShowEvent * event)
   703 {
   798 {
   704     if (!m_previewEnabled) {
   799     if (!m_previewEnabled)
       
   800     {
   705         m_previewEnabled = true;
   801         m_previewEnabled = true;
   706         setRandomTheme();
   802         setRandomTheme();
   707         updatePreview();
   803         updatePreview();
   708     }
   804     }
   709     QWidget::showEvent(event);
   805     QWidget::showEvent(event);
   715     if (!m_previewEnabled)
   811     if (!m_previewEnabled)
   716         return;
   812         return;
   717 
   813 
   718     if (pMap)
   814     if (pMap)
   719     {
   815     {
   720         disconnect(pMap, 0, this, SLOT(setImage(const QPixmap)));
   816         disconnect(pMap, 0, this, SLOT(onImageReceived(const QPixmap)));
   721         disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
   817         disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
       
   818         disconnect(pMap, 0, this, SLOT(onPreviewMapDestroyed(QObject *)));
   722         pMap = 0;
   819         pMap = 0;
   723     }
   820     }
   724 
   821 
   725     QPixmap failIcon;
   822     QPixmap failPixmap;
       
   823     QIcon failIcon;
   726 
   824 
   727     switch(m_mapInfo.type)
   825     switch(m_mapInfo.type)
   728     {
   826     {
   729         case MapModel::Invalid:
   827         case MapModel::Invalid:
   730             failIcon = QPixmap(":/res/btnDisabled.png");
   828             // Map error image
   731             mapPreview->setIcon(QIcon(failIcon));
   829             failPixmap = QPixmap(":/res/missingMap.png");
   732             mapPreview->setIconSize(failIcon.size());
   830             setImage(failPixmap, linearGradMapError, false);
       
   831             lblDesc->clear();
   733             break;
   832             break;
   734         case MapModel::GeneratedMap:
   833         case MapModel::GeneratedMap:
   735         case MapModel::GeneratedMaze:
   834         case MapModel::GeneratedMaze:
   736         case MapModel::GeneratedPerlin:
   835         case MapModel::GeneratedPerlin:
   737         case MapModel::HandDrawnMap:
   836         case MapModel::HandDrawnMap:
   738         case MapModel::FortsMap:
   837         case MapModel::FortsMap:
   739             askForGeneratedPreview();
   838             askForGeneratedPreview();
   740             break;
   839             break;
   741         default:
   840         default:
   742             QPixmap mapImage;
   841             // For maps loaded from image
   743             bool success = mapImage.load("physfs://Maps/" + m_mapInfo.name + "/preview.png");
   842             if(m_missingMap)
   744 
       
   745             if(!success)
       
   746             {
   843             {
   747                 mapPreview->setIcon(QIcon());
   844                 // Map error image due to missing map
   748                 return;
   845                 failPixmap = QPixmap(":/res/missingMap.png");
       
   846                 setImage(failPixmap, linearGradMapError, false);
       
   847                 lblDesc->clear();
       
   848                 break;
   749             }
   849             }
   750 
   850             else
   751             hhLimit = m_mapInfo.limit;
   851             {
   752             addInfoToPreview(mapImage);
   852                 // Draw map preview
       
   853                 QPixmap mapImage;
       
   854                 bool success = mapImage.load("physfs://Maps/" + m_mapInfo.name + "/preview.png");
       
   855 
       
   856                 setHHLimit(m_mapInfo.limit);
       
   857                 if(!success)
       
   858                 {
       
   859                     // Missing preview image
       
   860                     QPixmap empty = QPixmap(m_previewSize);
       
   861                     empty.fill(Qt::transparent);
       
   862                     setImage(empty, linearGradNoPreview, true);
       
   863                     return;
       
   864                 }
       
   865                 setImage(mapImage);
       
   866             }
   753     }
   867     }
   754 }
   868 }
   755 
   869 
   756 void HWMapContainer::setAllMapParameters(const QString &map, MapGenerator m, int mazesize, const QString &seed, int tmpl, int featureSize)
   870 void HWMapContainer::setAllMapParameters(const QString &map, MapGenerator m, int mazesize, const QString &seed, int tmpl, int featureSize)
   757 {
   871 {
   767 
   881 
   768 void HWMapContainer::updateModelViews()
   882 void HWMapContainer::updateModelViews()
   769 {
   883 {
   770     // restore theme selection
   884     // restore theme selection
   771     // do this before map selection restore, because map may overwrite theme
   885     // do this before map selection restore, because map may overwrite theme
   772     if (!m_theme.isEmpty())
   886     if (!m_theme.isNull() && !m_theme.isEmpty())
   773     {
   887     {
   774         QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), Qt::DisplayRole, m_theme);
   888         QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), Qt::DisplayRole, m_theme);
   775         if (mdl.size() > 0)
   889         if (mdl.size() > 0)
   776             updateTheme(mdl.at(0));
   890             updateTheme(mdl.at(0));
   777         else
   891         else
   778             setRandomTheme();
   892             setRandomTheme();
   779     }
   893     }
   780 
   894 
   781     // restore map selection
   895     // restore map selection
   782     if (!m_curMap.isEmpty())
   896     if (!m_curMap.isNull() && !m_curMap.isEmpty())
   783         intSetMap(m_curMap);
   897         intSetMap(m_curMap);
   784     else
   898     else
   785         updatePreview();
   899         updatePreview();
   786 }
   900 }
   787 
   901 
   837 
   951 
   838 void HWMapContainer::changeMapType(MapModel::MapType type, const QModelIndex & newMap)
   952 void HWMapContainer::changeMapType(MapModel::MapType type, const QModelIndex & newMap)
   839 {
   953 {
   840     staticMapList->hide();
   954     staticMapList->hide();
   841     missionMapList->hide();
   955     missionMapList->hide();
       
   956     teMapName->hide();
   842     lblMapList->hide();
   957     lblMapList->hide();
   843     generationStyles->hide();
   958     generationStyles->hide();
   844     mazeStyles->hide();
   959     mazeStyles->hide();
   845     lblDesc->hide();
   960     lblDesc->hide();
   846     btnLoadMap->hide();
   961     btnLoadMap->hide();
   881             setupMissionMapsView();
   996             setupMissionMapsView();
   882             mapgen = MAPGEN_MAP;
   997             mapgen = MAPGEN_MAP;
   883             missionMapChanged(newMap.isValid() ? newMap : missionMapList->currentIndex());
   998             missionMapChanged(newMap.isValid() ? newMap : missionMapList->currentIndex());
   884             lblMapList->setText(tr("Mission:"));
   999             lblMapList->setText(tr("Mission:"));
   885             lblMapList->show();
  1000             lblMapList->show();
   886             missionMapList->show();
  1001             setMapNameLabel(m_curMap, !m_missingMap);
       
  1002             if(m_master)
       
  1003             {
       
  1004                 missionMapList->show();
       
  1005             }
       
  1006             else
       
  1007             {
       
  1008                 teMapName->show();
       
  1009             }
   887             mapFeatureSize->hide();
  1010             mapFeatureSize->hide();
   888             lblDesc->setText(m_mapInfo.desc);
  1011             lblDesc->setText(m_mapInfo.desc);
   889             lblDesc->show();
  1012             lblDesc->show();
   890             emit mapChanged(m_curMap);
  1013             emit mapChanged(m_curMap);
   891             break;
  1014             break;
   893             setupStaticMapsView();
  1016             setupStaticMapsView();
   894             mapgen = MAPGEN_MAP;
  1017             mapgen = MAPGEN_MAP;
   895             staticMapChanged(newMap.isValid() ? newMap : staticMapList->currentIndex());
  1018             staticMapChanged(newMap.isValid() ? newMap : staticMapList->currentIndex());
   896             lblMapList->setText(tr("Map:"));
  1019             lblMapList->setText(tr("Map:"));
   897             lblMapList->show();
  1020             lblMapList->show();
       
  1021             setMapNameLabel(m_curMap, !m_missingMap);
       
  1022             if(m_master)
       
  1023             {
       
  1024                 staticMapList->show();
       
  1025             }
       
  1026             else
       
  1027             {
       
  1028                 teMapName->show();
       
  1029             }
   898             mapFeatureSize->hide();
  1030             mapFeatureSize->hide();
   899             staticMapList->show();
       
   900             emit mapChanged(m_curMap);
  1031             emit mapChanged(m_curMap);
   901             break;
  1032             break;
   902         case MapModel::FortsMap:
  1033         case MapModel::FortsMap:
   903             mapgen = MAPGEN_FORTS;
  1034             mapgen = MAPGEN_FORTS;
   904             setMapInfo(MapModel::MapInfoForts);
  1035             setMapInfo(MapModel::MapInfoForts);
   982 
  1113 
   983 void HWMapContainer::updateTheme(const QModelIndex & current)
  1114 void HWMapContainer::updateTheme(const QModelIndex & current)
   984 {
  1115 {
   985     m_theme = selectedTheme = current.data(ThemeModel::ActualNameRole).toString();
  1116     m_theme = selectedTheme = current.data(ThemeModel::ActualNameRole).toString();
   986     m_themeID = current.row();
  1117     m_themeID = current.row();
   987     QIcon icon = qVariantValue<QIcon>(current.data(Qt::DecorationRole));
  1118     QIcon icon = current.data(Qt::DecorationRole).value<QIcon>();
   988     //QSize iconSize = icon.actualSize(QSize(65535, 65535));
       
   989     //btnTheme->setFixedHeight(64);
       
   990     //btnTheme->setIconSize(iconSize);
       
   991     btnTheme->setIcon(icon);
  1119     btnTheme->setIcon(icon);
   992     btnTheme->setText(tr("Theme: %1").arg(current.data(Qt::DisplayRole).toString()));
  1120     QString themeLabel = tr("Theme: %1").arg(current.data(Qt::DisplayRole).toString());
       
  1121     btnTheme->setText(themeLabel);
   993     updateThemeButtonSize();
  1122     updateThemeButtonSize();
   994 }
  1123 }
   995 
  1124 
   996 void HWMapContainer::staticMapChanged(const QModelIndex & map, const QModelIndex & old)
  1125 void HWMapContainer::staticMapChanged(const QModelIndex & map, const QModelIndex & old)
   997 {
  1126 {
  1006 // Type: 0 = static, 1 = mission
  1135 // Type: 0 = static, 1 = mission
  1007 void HWMapContainer::mapChanged(const QModelIndex & map, int type, const QModelIndex & old)
  1136 void HWMapContainer::mapChanged(const QModelIndex & map, int type, const QModelIndex & old)
  1008 {
  1137 {
  1009     QListView * mapList;
  1138     QListView * mapList;
  1010 
  1139 
  1011     if (type == 0)      mapList = staticMapList;
  1140     if (type == 0)
  1012     else if (type == 1) mapList = missionMapList;
  1141     {
  1013     else                return;
  1142         mapList = staticMapList;
       
  1143         m_mapInfo.type = MapModel::StaticMap;
       
  1144     }
       
  1145     else if (type == 1)
       
  1146     {
       
  1147         mapList = missionMapList;
       
  1148         m_mapInfo.type = MapModel::MissionMap;
       
  1149     }
       
  1150     else
       
  1151         return;
  1014 
  1152 
  1015     // Make sure it is a valid index
  1153     // Make sure it is a valid index
  1016     if (!map.isValid())
  1154     if (!map.isValid())
  1017     {
  1155     {
       
  1156         // Make sure there's always a valid selection in the map list
  1018         if (old.isValid())
  1157         if (old.isValid())
  1019         {
  1158         {
  1020             mapList->setCurrentIndex(old);
  1159             mapList->setCurrentIndex(old);
  1021             mapList->scrollTo(old);
  1160             mapList->scrollTo(old);
  1022         }
  1161         }
  1023         else
  1162         m_mapInfo.type = MapModel::Invalid;
  1024         {
  1163         m_missingMap = true;
  1025             m_mapInfo.type = MapModel::Invalid;
  1164         updatePreview();
  1026             updatePreview();
       
  1027         }
       
  1028 
       
  1029         return;
  1165         return;
  1030     }
  1166     }
  1031 
  1167 
  1032     // If map changed, update list selection
  1168     // If map changed, update list selection
  1033     if (mapList->currentIndex() != map)
  1169     if (mapList->currentIndex() != map)
  1034     {
  1170     {
  1035         mapList->setCurrentIndex(map);
  1171         mapList->setCurrentIndex(map);
  1036         mapList->scrollTo(map);
  1172         mapList->scrollTo(map);
  1037     }
  1173     }
       
  1174     if (m_missingMap)
       
  1175     {
       
  1176         m_missingMap = false;
       
  1177         updatePreview();
       
  1178     }
  1038 
  1179 
  1039     Q_ASSERT(map.data(Qt::UserRole + 1).canConvert<MapModel::MapInfo>()); // Houston, we have a problem.
  1180     Q_ASSERT(map.data(Qt::UserRole + 1).canConvert<MapModel::MapInfo>()); // Houston, we have a problem.
  1040     setMapInfo(map.data(Qt::UserRole + 1).value<MapModel::MapInfo>());
  1181     setMapInfo(map.data(Qt::UserRole + 1).value<MapModel::MapInfo>());
  1041 }
  1182 }
  1042 
  1183 
  1044 {
  1185 {
  1045     m_mapInfo = mapInfo;
  1186     m_mapInfo = mapInfo;
  1046     m_curMap = m_mapInfo.name;
  1187     m_curMap = m_mapInfo.name;
  1047 
  1188 
  1048     // the map has no pre-defined theme, so let's use the selected one
  1189     // the map has no pre-defined theme, so let's use the selected one
  1049     if (m_mapInfo.theme.isEmpty())
  1190     if (m_mapInfo.theme.isNull() || m_mapInfo.theme.isEmpty())
  1050     {
  1191     {
  1051         if (!selectedTheme.isEmpty())
  1192         if (!selectedTheme.isNull() && !selectedTheme.isEmpty())
  1052         {
  1193         {
  1053             setTheme(selectedTheme);
  1194             setTheme(selectedTheme);
  1054             emit themeChanged(selectedTheme);
  1195             emit themeChanged(selectedTheme);
  1055         }
  1196         }
  1056     }
  1197     }
  1066     emit mapChanged(m_curMap);
  1207     emit mapChanged(m_curMap);
  1067 }
  1208 }
  1068 
  1209 
  1069 void HWMapContainer::loadDrawing()
  1210 void HWMapContainer::loadDrawing()
  1070 {
  1211 {
  1071     QString fileName = QFileDialog::getOpenFileName(NULL, tr("Load drawn map"), ".", tr("Drawn Maps") + " (*.hwmap);;" + tr("All files") + " (*)");
  1212     QString loadDir = QDir(cfgdir->absolutePath() + "/DrawnMaps").absolutePath();
       
  1213     QString fileName = QFileDialog::getOpenFileName(this, tr("Load drawn map"), loadDir, tr("Drawn Maps") + " (*.hwmap);;" + tr("All files") + " (*)");
  1072 
  1214 
  1073     if(fileName.isEmpty()) return;
  1215     if(fileName.isEmpty()) return;
  1074 
  1216 
  1075     QFile f(fileName);
  1217     QFile f(fileName);
  1076 
  1218 
  1107     if (master == m_master) return;
  1249     if (master == m_master) return;
  1108     m_master = master;
  1250     m_master = master;
  1109 
  1251 
  1110     foreach (QWidget *widget, m_childWidgets)
  1252     foreach (QWidget *widget, m_childWidgets)
  1111         widget->setEnabled(master);
  1253         widget->setEnabled(master);
  1112 }
  1254 
  1113 
  1255     if(m_mapInfo.type == MapModel::StaticMap)
  1114 void HWMapContainer::intSetIconlessTheme(const QString & name)
  1256     {
  1115 {
  1257         teMapName->setHidden(master);
  1116     if (name.isEmpty()) return;
  1258         staticMapList->setVisible(master);
       
  1259     }
       
  1260     else if(m_mapInfo.type == MapModel::MissionMap)
       
  1261     {
       
  1262         teMapName->setHidden(master);
       
  1263         missionMapList->setVisible(master);
       
  1264     }
       
  1265 
       
  1266     if(master)
       
  1267     {
       
  1268         // Room delegation cleanup if we get room control.
       
  1269 
       
  1270         if(m_missingMap)
       
  1271         {
       
  1272             // Reset map if we don't have the host's map
       
  1273             m_missingMap = false;
       
  1274             if(m_mapInfo.type == MapModel::MissionMap)
       
  1275             {
       
  1276                 missionMapList->selectionModel()->setCurrentIndex(m_missionMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
       
  1277             }
       
  1278             else
       
  1279             {
       
  1280                 if(m_mapInfo.type != MapModel::StaticMap)
       
  1281                 {
       
  1282                     changeMapType(MapModel::StaticMap);
       
  1283                 }
       
  1284                 staticMapList->selectionModel()->setCurrentIndex(m_staticMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
       
  1285             }
       
  1286         }
       
  1287         else
       
  1288         {
       
  1289             // Set random theme if we don't have it
       
  1290             QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), ThemeModel::ActualNameRole, m_theme);
       
  1291             if(!mdl.size())
       
  1292                 setRandomTheme();
       
  1293         }
       
  1294     }
       
  1295     else
       
  1296     {
       
  1297         setMapNameLabel(m_curMap, true);
       
  1298     }
       
  1299 }
       
  1300 
       
  1301 void HWMapContainer::setMissingTheme(const QString & name)
       
  1302 {
       
  1303     if (name.isNull() || name.isEmpty()) return;
  1117 
  1304 
  1118     m_theme = name;
  1305     m_theme = name;
  1119     btnTheme->setIcon(QIcon());
  1306     QPixmap pixMissing = QPixmap(":/res/missingTheme@2x.png");
  1120     btnTheme->setText(tr("Theme: %1").arg(name));
  1307     QIcon iconMissing  = QIcon();
  1121 }
  1308     iconMissing.addPixmap(pixMissing, QIcon::Normal);
  1122 
  1309     iconMissing.addPixmap(pixMissing, QIcon::Disabled);
  1123 void HWMapContainer::setupMissionMapsView()
  1310     btnTheme->setIcon(iconMissing);
       
  1311     // Question mark in front of theme name denotes it's missing
       
  1312     btnTheme->setText(tr("Theme: %1").arg("?" + name));
       
  1313     updateThemeButtonSize();
       
  1314 }
       
  1315 
       
  1316 void HWMapContainer::setupMissionMapsView(const QString & initialMap)
  1124 {
  1317 {
  1125     if(m_missionsViewSetup) return;
  1318     if(m_missionsViewSetup) return;
  1126     m_missionsViewSetup = true;
  1319     m_missionsViewSetup = true;
  1127 
  1320 
  1128     m_missionMapModel->loadMaps();
  1321     m_missionMapModel->loadMaps();
  1131     QItemSelectionModel * missionSelectionModel = missionMapList->selectionModel();
  1324     QItemSelectionModel * missionSelectionModel = missionMapList->selectionModel();
  1132     connect(missionSelectionModel,
  1325     connect(missionSelectionModel,
  1133             SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
  1326             SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
  1134             this,
  1327             this,
  1135             SLOT(missionMapChanged(const QModelIndex &, const QModelIndex &)));
  1328             SLOT(missionMapChanged(const QModelIndex &, const QModelIndex &)));
  1136     missionSelectionModel->setCurrentIndex(m_missionMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
  1329     int m = 0;
  1137 }
  1330     if(!initialMap.isNull())
  1138 
  1331         m = m_missionMapModel->findMap(initialMap);
  1139 void HWMapContainer::setupStaticMapsView()
  1332     missionSelectionModel->setCurrentIndex(m_missionMapModel->index(m, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
       
  1333 }
       
  1334 
       
  1335 void HWMapContainer::setupStaticMapsView(const QString & initialMap)
  1140 {
  1336 {
  1141     if(m_staticViewSetup) return;
  1337     if(m_staticViewSetup) return;
  1142     m_staticViewSetup = true;
  1338     m_staticViewSetup = true;
  1143 
  1339 
  1144     m_staticMapModel->loadMaps();
  1340     m_staticMapModel->loadMaps();
  1147     QItemSelectionModel * staticSelectionModel = staticMapList->selectionModel();
  1343     QItemSelectionModel * staticSelectionModel = staticMapList->selectionModel();
  1148     connect(staticSelectionModel,
  1344     connect(staticSelectionModel,
  1149             SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
  1345             SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
  1150             this,
  1346             this,
  1151             SLOT(staticMapChanged(const QModelIndex &, const QModelIndex &)));
  1347             SLOT(staticMapChanged(const QModelIndex &, const QModelIndex &)));
  1152     staticSelectionModel->setCurrentIndex(m_staticMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
  1348     int m = 0;
  1153 }
  1349     if(!initialMap.isNull())
       
  1350         m = m_staticMapModel->findMap(initialMap);
       
  1351     staticSelectionModel->setCurrentIndex(m_staticMapModel->index(m, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
       
  1352 }
       
  1353 
       
  1354 // Call this function instead of setting the text of the map name label
       
  1355 // directly.
       
  1356 void HWMapContainer::setMapNameLabel(QString mapName, bool validMap)
       
  1357 {
       
  1358     // Cut off insanely long names to be displayed
       
  1359     if(mapName.length() >= 90)
       
  1360     {
       
  1361         mapName.truncate(84);
       
  1362         mapName.append(" (...)");
       
  1363     }
       
  1364     teMapName->setPlainText(mapName);
       
  1365     if(validMap)
       
  1366         teMapName->setStyleSheet("background-color: transparent;");
       
  1367     else
       
  1368         teMapName->setStyleSheet("background-color: transparent; color: #b50000;");
       
  1369 }