QTfrontend/pagetraining.cpp
changeset 6048 ae213ef7030a
parent 6042 8b5345758f62
child 6050 07561ade87ca
--- a/QTfrontend/pagetraining.cpp	Mon Sep 26 21:29:40 2011 -0400
+++ b/QTfrontend/pagetraining.cpp	Tue Sep 27 04:31:08 2011 +0200
@@ -17,8 +17,10 @@
  */
 
 #include <QGridLayout>
+#include <QLabel>
+#include <QListWidget>
+#include <QListWidgetItem>
 #include <QPushButton>
-#include <QComboBox>
 
 #include "pagetraining.h"
 #include "hwconsts.h"
@@ -27,29 +29,51 @@
 {
     QGridLayout * pageLayout = new QGridLayout();
 
-    pageLayout->setColumnStretch(0, 1);
-    pageLayout->setColumnStretch(1, 2);
-    pageLayout->setColumnStretch(2, 1);
+// left column
+
+    // declare start button, caption and description
+    btnStart = formattedButton(":/res/Trainings.png", true);
+    btnStart->setToolTip(QPushButton::tr("Go!"));
+    lblCaption = new QLabel(this);
+    lblDescription = new QLabel(this);
+    lblDescription->setWordWrap(true);
+
+    // add start button, caption and description to 3 different rows
+    pageLayout->addWidget(btnStart, 0, 0);
+    pageLayout->addWidget(lblCaption, 1, 0);
+    pageLayout->addWidget(lblDescription, 2, 0);
+
+    // make first and last row stretch vertically
     pageLayout->setRowStretch(0, 1);
+    pageLayout->setRowStretch(1, 0);
     pageLayout->setRowStretch(2, 1);
 
-    CBSelect = new QComboBox(this);
+    // make both columns equal width
+    pageLayout->setColumnStretch(0, 1);
+    pageLayout->setColumnStretch(1, 1);
 
-    pageLayout->addWidget(CBSelect, 1, 1);
-    
-    BtnStartTrain = new QPushButton(this);
-    BtnStartTrain->setFont(*font14);
-    BtnStartTrain->setText(QPushButton::tr("Go!"));
-    pageLayout->addWidget(BtnStartTrain, 1, 2);
+    // center widgets within their grid cells
+    pageLayout->setAlignment(btnStart, Qt::AlignHCenter | Qt::AlignVCenter);
+    pageLayout->setAlignment(lblCaption, Qt::AlignHCenter | Qt::AlignVCenter);
+    pageLayout->setAlignment(lblDescription, Qt::AlignHCenter | Qt::AlignVCenter);
+
+// right column
+
+    lstMissions = new QListWidget(this);
+    pageLayout->addWidget(lstMissions, 0, 1, 3, 1); // spans over 3 rows
 
     return pageLayout;
 }
 
+
 void PageTraining::connectSignals()
 {
-    //TODO
+    connect(lstMissions, SIGNAL(itemSelectionChanged()), this, SLOT(updateInfo()));
+    connect(lstMissions, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(startSelected()));
+    connect(btnStart, SIGNAL(clicked()), this, SLOT(startSelected()));
 }
 
+
 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
 {
     initPage();
@@ -57,29 +81,32 @@
     QDir tmpdir;
     tmpdir.cd(cfgdir->absolutePath());
     tmpdir.cd("Data/Missions/Training");
-    QStringList userlist = scriptList(tmpdir);
+    QStringList missionList = scriptList(tmpdir);
+    missionList.sort();
+    missionList.replaceInStrings(QRegExp("$")," *");
 
     tmpdir.cd(datadir->absolutePath());
     tmpdir.cd("Missions/Training");
-    QStringList defaultlist = scriptList(tmpdir);
+    QStringList defaultList = scriptList(tmpdir);
+    defaultList.sort();
 
-    CBSelect->addItems(userlist);
+    missionList << defaultList;
 
     // add only default scripts that have names different from detected user scripts
-    foreach (const QString & line, defaultlist)
+    foreach (const QString & mission, missionList)
     {
-        if (!userlist.contains(line,Qt::CaseInsensitive)) CBSelect->addItem(line);
+        QListWidgetItem * item = new QListWidgetItem(mission);
+
+        // replace underscores in mission name with spaces
+        item->setText(item->text().replace("_", " "));
+
+        // store original name in data
+        item->setData(Qt::UserRole, mission);
+
+        lstMissions->addItem(item);
     }
 
-    // replace underscores with spaces in the displayed that
-    for(int i = 0; i < CBSelect->count(); i++)
-    {
-        QString text = CBSelect->itemText(i);
-        CBSelect->setItemData(i, text);
-        CBSelect->setItemText(i, text.replace("_", " "));
-//        if (userlist.contains(text))
-//            CBSelect->setItemText(i, text + " (" + AbstractPage::tr("custom") + ")");
-    }
+    updateInfo();
 }
 
 QStringList PageTraining::scriptList(const QDir & scriptDir) const
@@ -88,3 +115,33 @@
     dir.setFilter(QDir::Files);
     return dir.entryList(QStringList("*.lua")).replaceInStrings(QRegExp("^(.*)\\.lua"), "\\1");
 }
+
+
+void PageTraining::startSelected()
+{
+    emit startMission(lstMissions->currentItem()->data(Qt::UserRole).toString());
+}
+
+
+void PageTraining::updateInfo()
+{
+    if (lstMissions->currentItem())
+    {
+        QString thumbFile = datadir->absolutePath() + "/Graphics/Missions/Training/" + lstMissions->currentItem()->data(Qt::UserRole).toString() + ".png";
+        if (QFile::exists(thumbFile))
+            btnStart->setIcon(QIcon(thumbFile));
+        else
+            btnStart->setIcon(QIcon(":/res/Trainings.png"));
+
+        lblCaption->setText(lstMissions->currentItem()->text());
+        // TODO load mission description from file
+        lblDescription->setText("< Imagine\nMission\nDescription\nhere >\n\nThank you.");
+    }
+    else
+    {
+        btnStart->setIcon(QIcon(":/res/Trainings.png"));
+        lblCaption->setText(tr("Select a mission on the right -->"));
+        // TODO better text and tr()
+        lblDescription->setText("Welcome to the Training screen.\n\n\n...\nWHAT?\nIt's not finished yet...");
+    }
+}