QTfrontend/ui/page/pagetraining.cpp
changeset 6159 c780b8cf4d75
parent 6073 e6ce1a337e0f
child 6160 863d3edf5690
equal deleted inserted replaced
6158:cf034cc88e39 6159:c780b8cf4d75
    25 
    25 
    26 #include <QFile>
    26 #include <QFile>
    27 #include <QLocale>
    27 #include <QLocale>
    28 #include <QSettings>
    28 #include <QSettings>
    29 
    29 
       
    30 #include "hwconsts.h"
       
    31 
       
    32 #include "HWDataManager.h"
       
    33 
    30 #include "pagetraining.h"
    34 #include "pagetraining.h"
    31 #include "hwconsts.h"
       
    32 
    35 
    33 QLayout * PageTraining::bodyLayoutDefinition()
    36 QLayout * PageTraining::bodyLayoutDefinition()
    34 {
    37 {
    35     QGridLayout * pageLayout = new QGridLayout();
    38     QGridLayout * pageLayout = new QGridLayout();
    36 
    39 
   118 
   121 
   119     QString loc = settings.value("misc/locale", "").toString();
   122     QString loc = settings.value("misc/locale", "").toString();
   120     if (loc.isEmpty())
   123     if (loc.isEmpty())
   121         loc = QLocale::system().name();
   124         loc = QLocale::system().name();
   122 
   125 
   123     QString infoFile = 
   126     QFile * infoFile = HWDataManager::instance().findFileForRead(QString(
   124                 datadir->absolutePath() + "/Locale/missions_" + loc + ".txt";
   127                                             "Locale/missions_" + loc + ".txt"));
   125 
   128 
   126     // if file is non-existant try with language only
   129     // if file is non-existant try with language only
   127     if (!QFile::exists(infoFile))
   130     if (!infoFile->exists())
   128         infoFile = datadir->absolutePath() + "/Locale/missions_" + 
   131     {
   129                 loc.replace(QRegExp("_.*$"),"") + ".txt";
   132         delete infoFile;
       
   133         infoFile = HWDataManager::instance().findFileForRead(QString(
       
   134                 "Locale/missions_" + loc.replace(QRegExp("_.*$"),"") + ".txt"));
       
   135     }
   130 
   136 
   131     // fallback if file for current locale is non-existant
   137     // fallback if file for current locale is non-existant
   132     if (!QFile::exists(infoFile))
   138     if (!infoFile->exists())
   133         infoFile = datadir->absolutePath() + "/Locale/missions_en.txt";
   139     {
       
   140         delete infoFile;
       
   141         infoFile = HWDataManager::instance().findFileForRead(QString(
       
   142                                                      "Locale/missions_en.txt"));
       
   143     }
       
   144 
   134 
   145 
   135     // preload mission info for current locale
   146     // preload mission info for current locale
   136     m_info = new QSettings(infoFile, QSettings::IniFormat, this);
   147     m_info =
   137 
   148         new QSettings(infoFile->fileName(), QSettings::IniFormat, this);
   138 //  TODO -> this should be done in a tool "DataDir" class
   149 
   139     QDir tmpdir;
   150     // we don't need infoFile anymore
   140     tmpdir.cd(cfgdir->absolutePath());
   151     delete infoFile;
   141     tmpdir.cd("Data/Missions/Training");
   152 
   142     QStringList missionList = scriptList(tmpdir);
   153     QStringList missionList =
   143     missionList.sort();
   154             HWDataManager::instance().entryList(QString("Missions/Training"),
   144 
   155                 QDir::Files,
   145     tmpdir.cd(datadir->absolutePath());
   156                 QStringList("*.lua")
   146     tmpdir.cd("Missions/Training");
   157                 ).replaceInStrings(QRegExp("\\.lua$"), "");
   147     QStringList defaultList = scriptList(tmpdir);
   158 
   148     defaultList.sort();
   159     // scripts to lost - TODO: model?
   149 
       
   150     // add non-duplicate default scripts to the list
       
   151     foreach (const QString & mission, defaultList)
       
   152     {
       
   153         if (!missionList.contains(mission))
       
   154             missionList.append(mission);
       
   155     }
       
   156 
       
   157     // add default scripts that have names different from detected user scripts
       
   158     foreach (const QString & mission, missionList)
   160     foreach (const QString & mission, missionList)
   159     {
   161     {
   160         QListWidgetItem * item = new QListWidgetItem(mission);
   162         QListWidgetItem * item = new QListWidgetItem(mission);
   161 
   163 
   162         // fallback name: replace underscores in mission name with spaces
   164         // fallback name: replace underscores in mission name with spaces
   176     updateInfo();
   178     updateInfo();
   177 
   179 
   178     // pre-select first mission
   180     // pre-select first mission
   179     if (lstMissions->count() > 0)
   181     if (lstMissions->count() > 0)
   180         lstMissions->setCurrentRow(0);
   182         lstMissions->setCurrentRow(0);
   181 }
       
   182 
       
   183 QStringList PageTraining::scriptList(const QDir & scriptDir) const
       
   184 {
       
   185     QDir dir = scriptDir;
       
   186     dir.setFilter(QDir::Files);
       
   187     return dir.entryList(QStringList("*.lua")).replaceInStrings(QRegExp("^(.*)\\.lua"), "\\1");
       
   188 }
   183 }
   189 
   184 
   190 
   185 
   191 void PageTraining::startSelected()
   186 void PageTraining::startSelected()
   192 {
   187 {