QTfrontend/ui/page/pagetraining.cpp
branchios-develop
changeset 13418 ba39a1d396c0
parent 13279 e381f5260b45
child 13637 4c49a4944884
equal deleted inserted replaced
13416:6e8b807bda4b 13418:ba39a1d396c0
    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 <QTextStream>
    26 #include <QFile>
    27 #include <QFile>
    27 #include <QLocale>
    28 #include <QLocale>
    28 #include <QSettings>
    29 #include <QSettings>
    29 
    30 
    30 #include "hwconsts.h"
    31 #include "hwconsts.h"
   154 
   155 
   155     // get locale
   156     // get locale
   156     QSettings settings(dataMgr.settingsFileName(),
   157     QSettings settings(dataMgr.settingsFileName(),
   157                        QSettings::IniFormat);
   158                        QSettings::IniFormat);
   158 
   159 
   159     QString loc = settings.value("misc/locale", "").toString();
   160     QString loc = QLocale().name();
   160     if (loc.isEmpty())
       
   161         loc = QLocale::system().name();
       
   162 
   161 
   163     QString infoFile = QString("physfs://Locale/missions_" + loc + ".txt");
   162     QString infoFile = QString("physfs://Locale/missions_" + loc + ".txt");
   164 
   163 
   165     // if file is non-existant try with language only
   164     // if file is non-existant try with language only
   166     if (!QFile::exists(infoFile))
   165     if (!QFile::exists(infoFile))
   192             case 3:
   191             case 3:
   193                 subFolder = "Scenario";
   192                 subFolder = "Scenario";
   194                 m_widget = lstScenarios;
   193                 m_widget = lstScenarios;
   195                 break;
   194                 break;
   196         }
   195         }
       
   196         // scripts to load
       
   197         // first, load scripts in order specified in order.cfg (if present)
       
   198         QFile orderFile(QString("physfs://Missions/%1/order.cfg").arg(subFolder));
       
   199         QStringList orderedMissions;
       
   200         if (orderFile.open(QFile::ReadOnly))
       
   201         {
       
   202             QString m_id;
       
   203             QTextStream input(&orderFile);
       
   204             while(true)
       
   205             {
       
   206                 m_id = input.readLine();
       
   207                 if(m_id.isNull() || m_id.isEmpty())
       
   208                 {
       
   209                     break;
       
   210                 }
       
   211                 QListWidgetItem * item = new QListWidgetItem(m_id);
       
   212                 QString name = item->text().replace("_", " ");
       
   213                 name = m_info->value(m_id + ".name", name).toString();
       
   214                 item->setText(name);
       
   215                 item->setData(Qt::UserRole, m_id);
       
   216                 m_widget->addItem(item);
       
   217 
       
   218                 orderedMissions << m_id;
       
   219             }
       
   220         }
       
   221 
       
   222         // then, just load anything else in no particular order
   197         m_list = dataMgr.entryList(
   223         m_list = dataMgr.entryList(
   198                     "Missions/" + subFolder,
   224                     "Missions/" + subFolder,
   199                     QDir::Files, QStringList("*.lua")).
   225                     QDir::Files, QStringList("*.lua")).
   200                replaceInStrings(QRegExp("\\.lua$"), "");
   226                replaceInStrings(QRegExp("\\.lua$"), "");
   201 
   227 
   202         // scripts to load - TODO: model?
       
   203         foreach (const QString & m_id, m_list)
   228         foreach (const QString & m_id, m_list)
   204         {
   229         {
       
   230             // Disallow duplicates from order.cfg
       
   231             if (orderedMissions.contains(m_id))
       
   232             {
       
   233                 continue;
       
   234             }
       
   235 
   205             QListWidgetItem * item = new QListWidgetItem(m_id);
   236             QListWidgetItem * item = new QListWidgetItem(m_id);
   206 
   237 
   207             // fallback name: replace underscores in mission name with spaces
   238             // fallback name: replace underscores in mission name with spaces
   208             QString name = item->text().replace("_", " ");
   239             QString name = item->text().replace("_", " ");
   209 
   240