QTfrontend/pagetraining.cpp
changeset 6060 fdfc01419815
parent 6059 ddf020d0941a
child 6061 15b4b485a1c5
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QGridLayout>
       
    20 #include <QVBoxLayout>
       
    21 #include <QLabel>
       
    22 #include <QListWidget>
       
    23 #include <QListWidgetItem>
       
    24 #include <QPushButton>
       
    25 
       
    26 #include <QFile>
       
    27 #include <QLocale>
       
    28 #include <QSettings>
       
    29 
       
    30 #include "pagetraining.h"
       
    31 #include "hwconsts.h"
       
    32 
       
    33 QLayout * PageTraining::bodyLayoutDefinition()
       
    34 {
       
    35     QGridLayout * pageLayout = new QGridLayout();
       
    36 
       
    37 // left column
       
    38 
       
    39     // declare start button, caption and description
       
    40     btnPreview = formattedButton(":/res/Trainings.png", true);
       
    41     btnPreview->setToolTip(QPushButton::tr("Go!"));
       
    42 
       
    43     // make both rows equal height
       
    44     pageLayout->setRowStretch(0, 1);
       
    45     pageLayout->setRowStretch(1, 1);
       
    46 
       
    47     // add start button, caption and description to 3 different rows
       
    48     pageLayout->addWidget(btnPreview, 0, 0);
       
    49 
       
    50     // center preview
       
    51     pageLayout->setAlignment(btnPreview, Qt::AlignRight | Qt::AlignVCenter);
       
    52 
       
    53 
       
    54 // right column
       
    55 
       
    56     // info area (caption on top, description below)
       
    57     QVBoxLayout * infoLayout = new QVBoxLayout();
       
    58 
       
    59     lblCaption = new QLabel();
       
    60     lblCaption->setMinimumWidth(360);
       
    61     lblCaption->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
       
    62     lblCaption->setWordWrap(true);
       
    63     lblDescription = new QLabel();
       
    64     lblDescription->setMinimumWidth(360);
       
    65     lblDescription->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
       
    66     lblDescription->setWordWrap(true);
       
    67 
       
    68     infoLayout->addWidget(lblCaption);
       
    69     infoLayout->addWidget(lblDescription);
       
    70 
       
    71     pageLayout->addLayout(infoLayout, 0, 1);
       
    72     pageLayout->setAlignment(infoLayout, Qt::AlignLeft);
       
    73 
       
    74 
       
    75     // mission list
       
    76     lstMissions = new QListWidget(this);
       
    77     pageLayout->addWidget(lstMissions, 1, 0, 1, 2); // span 2 columns
       
    78 
       
    79     // let's not make the list use more space than needed
       
    80     lstMissions->setFixedWidth(360);
       
    81     pageLayout->setAlignment(lstMissions, Qt::AlignHCenter);
       
    82 
       
    83     return pageLayout;
       
    84 }
       
    85 
       
    86 QLayout * PageTraining::footerLayoutDefinition()
       
    87 {
       
    88     QBoxLayout * bottomLayout = new QVBoxLayout();
       
    89 
       
    90     btnStart = formattedButton(QPushButton::tr("Go!"));
       
    91     btnStart->setFixedWidth(140);
       
    92 
       
    93     bottomLayout->addWidget(btnStart);
       
    94 
       
    95     bottomLayout->setAlignment(btnStart, Qt::AlignRight | Qt::AlignVCenter);
       
    96 
       
    97     return bottomLayout;
       
    98 }
       
    99 
       
   100 
       
   101 void PageTraining::connectSignals()
       
   102 {
       
   103     connect(lstMissions, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(updateInfo()));
       
   104     connect(lstMissions, SIGNAL(clicked()), this, SLOT(updateInfo()));
       
   105     connect(lstMissions, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(startSelected()));
       
   106     connect(btnPreview, SIGNAL(clicked()), this, SLOT(startSelected()));
       
   107     connect(btnStart, SIGNAL(clicked()), this, SLOT(startSelected()));
       
   108 }
       
   109 
       
   110 
       
   111 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
       
   112 {
       
   113     initPage();
       
   114 
       
   115     // get locale
       
   116     QSettings settings(cfgdir->absolutePath() + "/hedgewars.ini",
       
   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);
       
   137 
       
   138 //  TODO -> this should be done in a tool "DataDir" class
       
   139     QDir tmpdir;
       
   140     tmpdir.cd(cfgdir->absolutePath());
       
   141     tmpdir.cd("Data/Missions/Training");
       
   142     QStringList missionList = scriptList(tmpdir);
       
   143     missionList.sort();
       
   144 
       
   145     tmpdir.cd(datadir->absolutePath());
       
   146     tmpdir.cd("Missions/Training");
       
   147     QStringList defaultList = scriptList(tmpdir);
       
   148     defaultList.sort();
       
   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)
       
   159     {
       
   160         QListWidgetItem * item = new QListWidgetItem(mission);
       
   161 
       
   162         // fallback name: replace underscores in mission name with spaces
       
   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);
       
   169 
       
   170         // store original name in data
       
   171         item->setData(Qt::UserRole, mission);
       
   172 
       
   173         lstMissions->addItem(item);
       
   174     }
       
   175 
       
   176     updateInfo();
       
   177 
       
   178     // pre-select first mission
       
   179     if (lstMissions->count() > 0)
       
   180         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 }
       
   189 
       
   190 
       
   191 void PageTraining::startSelected()
       
   192 {
       
   193     QListWidgetItem * curItem = lstMissions->currentItem();
       
   194 
       
   195     if (curItem != NULL)
       
   196         emit startMission(curItem->data(Qt::UserRole).toString());
       
   197 }
       
   198 
       
   199 
       
   200 void PageTraining::updateInfo()
       
   201 {
       
   202     if (lstMissions->currentItem())
       
   203     {
       
   204         // TODO also use .pngs in userdata folder
       
   205         QString thumbFile = datadir->absolutePath() +
       
   206                     "/Graphics/Missions/Training/" +
       
   207                     lstMissions->currentItem()->data(Qt::UserRole).toString() +
       
   208                     ".png";
       
   209 
       
   210         if (QFile::exists(thumbFile))
       
   211             btnPreview->setIcon(QIcon(thumbFile));
       
   212         else
       
   213             btnPreview->setIcon(QIcon(":/res/Trainings.png"));
       
   214 
       
   215         QString realName = lstMissions->currentItem()->data(
       
   216                            Qt::UserRole).toString();
       
   217 
       
   218         QString caption = m_info->value(realName + ".name", 
       
   219                           lstMissions->currentItem()->text()).toString();
       
   220 
       
   221         QString description = m_info->value(realName + ".desc",
       
   222                               tr("No description available")).toString();
       
   223 
       
   224         lblCaption->setText("<h2>" + caption +"</h2>");
       
   225         lblDescription->setText(description);
       
   226     }
       
   227     else
       
   228     {
       
   229         btnPreview->setIcon(QIcon(":/res/Trainings.png"));
       
   230         lblCaption->setText(tr("Select a mission!"));
       
   231         // TODO better text and tr()
       
   232         lblDescription->setText("");
       
   233     }
       
   234 }