QTfrontend/pagetraining.cpp
changeset 6052 5e3e7d19bdb5
parent 6050 07561ade87ca
child 6054 d3751a18cecb
equal deleted inserted replaced
6050:07561ade87ca 6052:5e3e7d19bdb5
    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 <QVBoxLayout>
    20 #include <QLabel>
    21 #include <QLabel>
    21 #include <QListWidget>
    22 #include <QListWidget>
    22 #include <QListWidgetItem>
    23 #include <QListWidgetItem>
    23 #include <QPushButton>
    24 #include <QPushButton>
    24 
    25 
    30     QGridLayout * pageLayout = new QGridLayout();
    31     QGridLayout * pageLayout = new QGridLayout();
    31 
    32 
    32 // left column
    33 // left column
    33 
    34 
    34     // declare start button, caption and description
    35     // declare start button, caption and description
    35     btnStart = formattedButton(":/res/Trainings.png", true);
    36     btnPreview = formattedButton(":/res/Trainings.png", true);
    36     btnStart->setToolTip(QPushButton::tr("Go!"));
    37     btnPreview->setToolTip(QPushButton::tr("Go!"));
    37     lblCaption = new QLabel(this);
    38 
    38     lblDescription = new QLabel(this);
    39     // make both rows equal height
    39     lblDescription->setWordWrap(true);
    40     pageLayout->setRowStretch(0, 1);
       
    41     pageLayout->setRowStretch(1, 1);
    40 
    42 
    41     // add start button, caption and description to 3 different rows
    43     // add start button, caption and description to 3 different rows
    42     pageLayout->addWidget(btnStart, 0, 0);
    44     pageLayout->addWidget(btnPreview, 0, 0);
    43     pageLayout->addWidget(lblCaption, 1, 0);
       
    44     pageLayout->addWidget(lblDescription, 2, 0);
       
    45 
    45 
    46     // make first and last row stretch vertically
    46     // center preview
    47     pageLayout->setRowStretch(0, 1);
    47     pageLayout->setAlignment(btnPreview, Qt::AlignRight | Qt::AlignVCenter);
    48     pageLayout->setRowStretch(1, 0);
       
    49     pageLayout->setRowStretch(2, 1);
       
    50 
    48 
    51     // make both columns equal width
       
    52     pageLayout->setColumnStretch(0, 1);
       
    53     pageLayout->setColumnStretch(1, 1);
       
    54 
       
    55     // center widgets within their grid cells
       
    56     pageLayout->setAlignment(btnStart, Qt::AlignHCenter | Qt::AlignVCenter);
       
    57     pageLayout->setAlignment(lblCaption, Qt::AlignHCenter | Qt::AlignVCenter);
       
    58     pageLayout->setAlignment(lblDescription, Qt::AlignHCenter | Qt::AlignVCenter);
       
    59 
    49 
    60 // right column
    50 // right column
    61 
    51 
       
    52     // info area (caption on top, description below)
       
    53     QVBoxLayout * infoLayout = new QVBoxLayout();
       
    54 
       
    55     lblCaption = new QLabel();
       
    56     lblCaption->setMinimumWidth(360);
       
    57     lblCaption->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
       
    58     lblCaption->setWordWrap(true);
       
    59     lblDescription = new QLabel();
       
    60     lblDescription->setMinimumWidth(360);
       
    61     lblDescription->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
       
    62     lblDescription->setWordWrap(true);
       
    63 
       
    64     infoLayout->addWidget(lblCaption);
       
    65     infoLayout->addWidget(lblDescription);
       
    66 
       
    67     pageLayout->addLayout(infoLayout, 0, 1);
       
    68     pageLayout->setAlignment(infoLayout, Qt::AlignLeft);
       
    69 
       
    70 
       
    71     // mission list
    62     lstMissions = new QListWidget(this);
    72     lstMissions = new QListWidget(this);
    63     pageLayout->addWidget(lstMissions, 0, 1, 3, 1); // spans over 3 rows
    73     pageLayout->addWidget(lstMissions, 1, 0, 1, 2); // span 2 columns
       
    74 
       
    75     // let's not make the list use more space than needed
       
    76     lstMissions->setFixedWidth(360);
       
    77     pageLayout->setAlignment(lstMissions, Qt::AlignHCenter);
    64 
    78 
    65     return pageLayout;
    79     return pageLayout;
       
    80 }
       
    81 
       
    82 QLayout * PageTraining::footerLayoutDefinition()
       
    83 {
       
    84     QBoxLayout * bottomLayout = new QVBoxLayout();
       
    85 
       
    86     btnStart = formattedButton(QPushButton::tr("Go!"));
       
    87     btnStart->setFixedWidth(140);
       
    88 
       
    89     bottomLayout->addWidget(btnStart);
       
    90 
       
    91     bottomLayout->setAlignment(btnStart, Qt::AlignRight | Qt::AlignVCenter);
       
    92 
       
    93     return bottomLayout;
    66 }
    94 }
    67 
    95 
    68 
    96 
    69 void PageTraining::connectSignals()
    97 void PageTraining::connectSignals()
    70 {
    98 {
    71     connect(lstMissions, SIGNAL(itemSelectionChanged()), this, SLOT(updateInfo()));
    99     connect(lstMissions, SIGNAL(itemSelectionChanged()), this, SLOT(updateInfo()));
    72     connect(lstMissions, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(startSelected()));
   100     connect(lstMissions, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(startSelected()));
       
   101     connect(btnPreview, SIGNAL(clicked()), this, SLOT(startSelected()));
    73     connect(btnStart, SIGNAL(clicked()), this, SLOT(startSelected()));
   102     connect(btnStart, SIGNAL(clicked()), this, SLOT(startSelected()));
    74 }
   103 }
    75 
   104 
    76 
   105 
    77 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
   106 PageTraining::PageTraining(QWidget* parent) : AbstractPage(parent)
    78 {
   107 {
    79     initPage();
   108     initPage();
    80 
   109 
       
   110 //  TODO -> this should be done in a tool "DataDir" class
    81     QDir tmpdir;
   111     QDir tmpdir;
    82     tmpdir.cd(cfgdir->absolutePath());
   112     tmpdir.cd(cfgdir->absolutePath());
    83     tmpdir.cd("Data/Missions/Training");
   113     tmpdir.cd("Data/Missions/Training");
    84     QStringList missionList = scriptList(tmpdir);
   114     QStringList missionList = scriptList(tmpdir);
    85     missionList.sort();
   115     missionList.sort();
    87     tmpdir.cd(datadir->absolutePath());
   117     tmpdir.cd(datadir->absolutePath());
    88     tmpdir.cd("Missions/Training");
   118     tmpdir.cd("Missions/Training");
    89     QStringList defaultList = scriptList(tmpdir);
   119     QStringList defaultList = scriptList(tmpdir);
    90     defaultList.sort();
   120     defaultList.sort();
    91 
   121 
    92     missionList << defaultList;
   122     // add non-duplicate default scripts to the list
       
   123     foreach (const QString & mission, defaultList)
       
   124     {
       
   125         if (!missionList.contains(mission))
       
   126             missionList.append(mission);
       
   127     }
    93 
   128 
    94     // add only default scripts that have names different from detected user scripts
   129     // add only default scripts that have names different from detected user scripts
    95     foreach (const QString & mission, missionList)
   130     foreach (const QString & mission, missionList)
    96     {
   131     {
    97         QListWidgetItem * item = new QListWidgetItem(mission);
   132         QListWidgetItem * item = new QListWidgetItem(mission);
   104 
   139 
   105         lstMissions->addItem(item);
   140         lstMissions->addItem(item);
   106     }
   141     }
   107 
   142 
   108     updateInfo();
   143     updateInfo();
       
   144 
       
   145     // pre-select first mission
       
   146     if (lstMissions->count() > 0)
       
   147         lstMissions->setCurrentRow(0);
   109 }
   148 }
   110 
   149 
   111 QStringList PageTraining::scriptList(const QDir & scriptDir) const
   150 QStringList PageTraining::scriptList(const QDir & scriptDir) const
   112 {
   151 {
   113     QDir dir = scriptDir;
   152     QDir dir = scriptDir;
   130     if (lstMissions->currentItem())
   169     if (lstMissions->currentItem())
   131     {
   170     {
   132         // TODO also use .pngs in userdata folder
   171         // TODO also use .pngs in userdata folder
   133         QString thumbFile = datadir->absolutePath() + "/Graphics/Missions/Training/" + lstMissions->currentItem()->data(Qt::UserRole).toString() + ".png";
   172         QString thumbFile = datadir->absolutePath() + "/Graphics/Missions/Training/" + lstMissions->currentItem()->data(Qt::UserRole).toString() + ".png";
   134         if (QFile::exists(thumbFile))
   173         if (QFile::exists(thumbFile))
   135             btnStart->setIcon(QIcon(thumbFile));
   174             btnPreview->setIcon(QIcon(thumbFile));
   136         else
   175         else
   137             btnStart->setIcon(QIcon(":/res/Trainings.png"));
   176             btnPreview->setIcon(QIcon(":/res/Trainings.png"));
   138 
   177 
   139         lblCaption->setText(lstMissions->currentItem()->text());
   178         lblCaption->setText("<h2>" + lstMissions->currentItem()->text()+"</h2>");
   140         // TODO load mission description from file
   179         // TODO load mission description from file
   141         lblDescription->setText("< Imagine\nMission\nDescription\nhere >\n\nThank you.");
   180         lblDescription->setText("< Imagine Mission Description here >\n\nThank you.");
   142     }
   181     }
   143     else
   182     else
   144     {
   183     {
   145         btnStart->setIcon(QIcon(":/res/Trainings.png"));
   184         btnPreview->setIcon(QIcon(":/res/Trainings.png"));
   146         lblCaption->setText(tr("Select a mission on the right -->"));
   185         lblCaption->setText(tr("Select a mission!"));
   147         // TODO better text and tr()
   186         // TODO better text and tr()
   148         lblDescription->setText("Welcome to the Training screen.\n\n\n...\nWHAT?\nIt's not finished yet...");
   187         lblDescription->setText("");
   149     }
   188     }
   150 }
   189 }