QTfrontend/pagetraining.cpp
changeset 6048 ae213ef7030a
parent 6042 8b5345758f62
child 6050 07561ade87ca
equal deleted inserted replaced
6046:d681b8127523 6048:ae213ef7030a
    15  * along with this program; if not, write to the Free Software
    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
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 #include <QGridLayout>
    19 #include <QGridLayout>
       
    20 #include <QLabel>
       
    21 #include <QListWidget>
       
    22 #include <QListWidgetItem>
    20 #include <QPushButton>
    23 #include <QPushButton>
    21 #include <QComboBox>
       
    22 
    24 
    23 #include "pagetraining.h"
    25 #include "pagetraining.h"
    24 #include "hwconsts.h"
    26 #include "hwconsts.h"
    25 
    27 
    26 QLayout * PageTraining::bodyLayoutDefinition()
    28 QLayout * PageTraining::bodyLayoutDefinition()
    27 {
    29 {
    28     QGridLayout * pageLayout = new QGridLayout();
    30     QGridLayout * pageLayout = new QGridLayout();
    29 
    31 
    30     pageLayout->setColumnStretch(0, 1);
    32 // left column
    31     pageLayout->setColumnStretch(1, 2);
    33 
    32     pageLayout->setColumnStretch(2, 1);
    34     // declare start button, caption and description
       
    35     btnStart = formattedButton(":/res/Trainings.png", true);
       
    36     btnStart->setToolTip(QPushButton::tr("Go!"));
       
    37     lblCaption = new QLabel(this);
       
    38     lblDescription = new QLabel(this);
       
    39     lblDescription->setWordWrap(true);
       
    40 
       
    41     // add start button, caption and description to 3 different rows
       
    42     pageLayout->addWidget(btnStart, 0, 0);
       
    43     pageLayout->addWidget(lblCaption, 1, 0);
       
    44     pageLayout->addWidget(lblDescription, 2, 0);
       
    45 
       
    46     // make first and last row stretch vertically
    33     pageLayout->setRowStretch(0, 1);
    47     pageLayout->setRowStretch(0, 1);
       
    48     pageLayout->setRowStretch(1, 0);
    34     pageLayout->setRowStretch(2, 1);
    49     pageLayout->setRowStretch(2, 1);
    35 
    50 
    36     CBSelect = new QComboBox(this);
    51     // make both columns equal width
       
    52     pageLayout->setColumnStretch(0, 1);
       
    53     pageLayout->setColumnStretch(1, 1);
    37 
    54 
    38     pageLayout->addWidget(CBSelect, 1, 1);
    55     // center widgets within their grid cells
    39     
    56     pageLayout->setAlignment(btnStart, Qt::AlignHCenter | Qt::AlignVCenter);
    40     BtnStartTrain = new QPushButton(this);
    57     pageLayout->setAlignment(lblCaption, Qt::AlignHCenter | Qt::AlignVCenter);
    41     BtnStartTrain->setFont(*font14);
    58     pageLayout->setAlignment(lblDescription, Qt::AlignHCenter | Qt::AlignVCenter);
    42     BtnStartTrain->setText(QPushButton::tr("Go!"));
    59 
    43     pageLayout->addWidget(BtnStartTrain, 1, 2);
    60 // right column
       
    61 
       
    62     lstMissions = new QListWidget(this);
       
    63     pageLayout->addWidget(lstMissions, 0, 1, 3, 1); // spans over 3 rows
    44 
    64 
    45     return pageLayout;
    65     return pageLayout;
    46 }
    66 }
    47 
    67 
       
    68 
    48 void PageTraining::connectSignals()
    69 void PageTraining::connectSignals()
    49 {
    70 {
    50     //TODO
    71     connect(lstMissions, SIGNAL(itemSelectionChanged()), this, SLOT(updateInfo()));
       
    72     connect(lstMissions, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(startSelected()));
       
    73     connect(btnStart, SIGNAL(clicked()), this, SLOT(startSelected()));
    51 }
    74 }
       
    75 
    52 
    76 
    53 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
    77 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
    54 {
    78 {
    55     initPage();
    79     initPage();
    56 
    80 
    57     QDir tmpdir;
    81     QDir tmpdir;
    58     tmpdir.cd(cfgdir->absolutePath());
    82     tmpdir.cd(cfgdir->absolutePath());
    59     tmpdir.cd("Data/Missions/Training");
    83     tmpdir.cd("Data/Missions/Training");
    60     QStringList userlist = scriptList(tmpdir);
    84     QStringList missionList = scriptList(tmpdir);
       
    85     missionList.sort();
       
    86     missionList.replaceInStrings(QRegExp("$")," *");
    61 
    87 
    62     tmpdir.cd(datadir->absolutePath());
    88     tmpdir.cd(datadir->absolutePath());
    63     tmpdir.cd("Missions/Training");
    89     tmpdir.cd("Missions/Training");
    64     QStringList defaultlist = scriptList(tmpdir);
    90     QStringList defaultList = scriptList(tmpdir);
       
    91     defaultList.sort();
    65 
    92 
    66     CBSelect->addItems(userlist);
    93     missionList << defaultList;
    67 
    94 
    68     // add only default scripts that have names different from detected user scripts
    95     // add only default scripts that have names different from detected user scripts
    69     foreach (const QString & line, defaultlist)
    96     foreach (const QString & mission, missionList)
    70     {
    97     {
    71         if (!userlist.contains(line,Qt::CaseInsensitive)) CBSelect->addItem(line);
    98         QListWidgetItem * item = new QListWidgetItem(mission);
       
    99 
       
   100         // replace underscores in mission name with spaces
       
   101         item->setText(item->text().replace("_", " "));
       
   102 
       
   103         // store original name in data
       
   104         item->setData(Qt::UserRole, mission);
       
   105 
       
   106         lstMissions->addItem(item);
    72     }
   107     }
    73 
   108 
    74     // replace underscores with spaces in the displayed that
   109     updateInfo();
    75     for(int i = 0; i < CBSelect->count(); i++)
       
    76     {
       
    77         QString text = CBSelect->itemText(i);
       
    78         CBSelect->setItemData(i, text);
       
    79         CBSelect->setItemText(i, text.replace("_", " "));
       
    80 //        if (userlist.contains(text))
       
    81 //            CBSelect->setItemText(i, text + " (" + AbstractPage::tr("custom") + ")");
       
    82     }
       
    83 }
   110 }
    84 
   111 
    85 QStringList PageTraining::scriptList(const QDir & scriptDir) const
   112 QStringList PageTraining::scriptList(const QDir & scriptDir) const
    86 {
   113 {
    87     QDir dir = scriptDir;
   114     QDir dir = scriptDir;
    88     dir.setFilter(QDir::Files);
   115     dir.setFilter(QDir::Files);
    89     return dir.entryList(QStringList("*.lua")).replaceInStrings(QRegExp("^(.*)\\.lua"), "\\1");
   116     return dir.entryList(QStringList("*.lua")).replaceInStrings(QRegExp("^(.*)\\.lua"), "\\1");
    90 }
   117 }
       
   118 
       
   119 
       
   120 void PageTraining::startSelected()
       
   121 {
       
   122     emit startMission(lstMissions->currentItem()->data(Qt::UserRole).toString());
       
   123 }
       
   124 
       
   125 
       
   126 void PageTraining::updateInfo()
       
   127 {
       
   128     if (lstMissions->currentItem())
       
   129     {
       
   130         QString thumbFile = datadir->absolutePath() + "/Graphics/Missions/Training/" + lstMissions->currentItem()->data(Qt::UserRole).toString() + ".png";
       
   131         if (QFile::exists(thumbFile))
       
   132             btnStart->setIcon(QIcon(thumbFile));
       
   133         else
       
   134             btnStart->setIcon(QIcon(":/res/Trainings.png"));
       
   135 
       
   136         lblCaption->setText(lstMissions->currentItem()->text());
       
   137         // TODO load mission description from file
       
   138         lblDescription->setText("< Imagine\nMission\nDescription\nhere >\n\nThank you.");
       
   139     }
       
   140     else
       
   141     {
       
   142         btnStart->setIcon(QIcon(":/res/Trainings.png"));
       
   143         lblCaption->setText(tr("Select a mission on the right -->"));
       
   144         // TODO better text and tr()
       
   145         lblDescription->setText("Welcome to the Training screen.\n\n\n...\nWHAT?\nIt's not finished yet...");
       
   146     }
       
   147 }