QTfrontend/pagetraining.cpp
changeset 6059 ddf020d0941a
parent 6058 ae8684864a6e
equal deleted inserted replaced
6058:ae8684864a6e 6059:ddf020d0941a
    21 #include <QLabel>
    21 #include <QLabel>
    22 #include <QListWidget>
    22 #include <QListWidget>
    23 #include <QListWidgetItem>
    23 #include <QListWidgetItem>
    24 #include <QPushButton>
    24 #include <QPushButton>
    25 
    25 
       
    26 #include <QFile>
       
    27 #include <QLocale>
    26 #include <QSettings>
    28 #include <QSettings>
    27 
    29 
    28 #include "pagetraining.h"
    30 #include "pagetraining.h"
    29 #include "hwconsts.h"
    31 #include "hwconsts.h"
    30 
    32 
   108 
   110 
   109 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
   111 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
   110 {
   112 {
   111     initPage();
   113     initPage();
   112 
   114 
   113     // preload localized descriptions/etc  TODO; this is just mockup code
   115     // get locale
   114     m_info = new QSettings(datadir->absolutePath() + "/Locale/missions_en.txt",
   116     QSettings settings(cfgdir->absolutePath() + "/hedgewars.ini",
   115                            QSettings::IniFormat, this);
   117                        QSettings::IniFormat);
       
   118 
       
   119     QString loc = settings.value("misc/locale", "").toString();
       
   120     if (loc.isEmpty())
       
   121         loc = QLocale::system().name();
       
   122 
       
   123     QString infoFile = 
       
   124                 datadir->absolutePath() + "/Locale/missions_" + loc + ".txt";
       
   125 
       
   126     // if file is non-existant try with language only
       
   127     if (!QFile::exists(infoFile))
       
   128         infoFile = datadir->absolutePath() + "/Locale/missions_" + 
       
   129                 loc.replace(QRegExp("_.*$"),"") + ".txt";
       
   130 
       
   131     // fallback if file for current locale is non-existant
       
   132     if (!QFile::exists(infoFile))
       
   133         infoFile = datadir->absolutePath() + "/Locale/missions_en.txt";
       
   134 
       
   135     // preload mission info for current locale
       
   136     m_info = new QSettings(infoFile, QSettings::IniFormat, this);
   116 
   137 
   117 //  TODO -> this should be done in a tool "DataDir" class
   138 //  TODO -> this should be done in a tool "DataDir" class
   118     QDir tmpdir;
   139     QDir tmpdir;
   119     tmpdir.cd(cfgdir->absolutePath());
   140     tmpdir.cd(cfgdir->absolutePath());
   120     tmpdir.cd("Data/Missions/Training");
   141     tmpdir.cd("Data/Missions/Training");
   136     // add default scripts that have names different from detected user scripts
   157     // add default scripts that have names different from detected user scripts
   137     foreach (const QString & mission, missionList)
   158     foreach (const QString & mission, missionList)
   138     {
   159     {
   139         QListWidgetItem * item = new QListWidgetItem(mission);
   160         QListWidgetItem * item = new QListWidgetItem(mission);
   140 
   161 
   141         // replace underscores in mission name with spaces
   162         // fallback name: replace underscores in mission name with spaces
   142         item->setText(item->text().replace("_", " "));
   163         QString name = item->text().replace("_", " ");
       
   164 
       
   165         // see if we can get a prettier/translated name
       
   166         name = m_info->value(mission + ".name", name).toString();
       
   167 
       
   168         item->setText(name);
   143 
   169 
   144         // store original name in data
   170         // store original name in data
   145         item->setData(Qt::UserRole, mission);
   171         item->setData(Qt::UserRole, mission);
   146 
   172 
   147         lstMissions->addItem(item);
   173         lstMissions->addItem(item);