Initial Commit
[uktrainplanner] / src / favouritestations.cpp
1 #include "favouritestations.h"
2 #include "ui_favouritestations.h"
3
4 FavouriteStations::FavouriteStations(QWidget *parent) : QDialog(parent), ui(new Ui::FavouriteStations)
5 {
6     ui->setupUi(this);
7 }
8
9 /*
10  * Add a new favourite station to the list.
11  */
12 void FavouriteStations::addFavourite()
13 {
14     StationChooser chooser;
15
16     if(chooser.exec() == QDialog::Accepted)
17     {
18         QListWidgetItem * li = new QListWidgetItem(chooser.getChoice());
19         li->setData(Qt::UserRole, chooser.getChoiceCRS());
20         ui->listWidget->addItem(li);
21         ui->listWidget->sortItems();
22     }
23 }
24
25
26 /*
27  * Delete the currently selected station from the list of favourites
28  */
29 void FavouriteStations::deleteFavourite()
30 {
31     if(ui->listWidget->currentRow() != -1)
32         ui->listWidget->takeItem(ui->listWidget->currentRow());
33 }
34
35 /*
36  * Save the current list of favourites into the application's settings with QSettings
37  */
38 void FavouriteStations::save()
39 {
40     QStringList nameList;
41     QStringList codeList;
42
43     for(int i = 0; i < ui->listWidget->count(); i++)
44     {
45         nameList << ui->listWidget->item(i)->text();
46         codeList << ui->listWidget->item(i)->data(Qt::UserRole).toString();
47     }
48
49     settings.setValue(names, nameList);
50     settings.setValue(codes, codeList);
51 }
52
53 void FavouriteStations::accept()
54 {
55     save();
56     QDialog::accept();
57 }
58
59 void FavouriteStations::reject()
60 {
61     save();
62     QDialog::reject();
63 }
64
65 int FavouriteStations::exec(QString stationNames, QString stationCodes)
66 {
67     ui->listWidget->clear();
68
69     names = stationNames;
70     codes = stationCodes;
71
72     //Populate the list with the current favourite stations
73     QVariant nameV = settings.value(names, QVariant());
74     QVariant codeV = settings.value(codes, QVariant());
75
76     if(nameV != QVariant())
77     {
78         QStringList nameList = nameV.toStringList();
79         QStringList codeList = codeV.toStringList();
80
81         for(int i = 0; i < nameList.size(); i++)
82         {
83             QListWidgetItem * wi = new QListWidgetItem(nameList.at(i));
84             wi->setData(Qt::UserRole, codeList.at(i));
85             ui->listWidget->addItem(wi);
86         }
87     }
88
89     ui->listWidget->sortItems();
90     ui->listWidget->setCurrentRow(0);
91
92     return QDialog::exec();
93 }
94
95 FavouriteStations::~FavouriteStations()
96 {
97     delete ui;
98 }
99
100 void FavouriteStations::changeEvent(QEvent *e)
101 {
102     QDialog::changeEvent(e);
103     switch (e->type()) {
104     case QEvent::LanguageChange:
105         ui->retranslateUi(this);
106         break;
107     default:
108         break;
109     }
110 }