Initial Commit
[uktrainplanner] / src / chooser.cpp
1 #include "chooser.h"
2 #include "ui_chooser.h"
3
4 Chooser::Chooser(QWidget *parent) :
5     QMainWindow(parent),
6     ui(new Ui::Chooser)
7 {
8     ui->setupUi(this);
9     ui->scrollArea->setProperty("FingerScrollable", true);
10     //Create the buttons for favourite stations
11     updateStationButtons();
12 }
13
14 /*
15  * Re-creates the display of the buttons for the user's favourite stations.
16  */
17 void Chooser::updateStationButtons()
18 {
19     //Delete all the currently displayed buttons
20     for(int i = 0; i < stationButtons.size(); i++)
21         delete stationButtons.at(i);
22
23     //Clear the list of pointers to displayed station buttons
24     stationButtons.clear();
25
26     //Get the user's favourite stations, and add a button for each
27     QStringList favourites = settings.value("FavouriteDepartureBoards", QVariant()).toStringList();
28     QStringList favouritesCRS = settings.value("FavouriteDepartureBoardsCRS", QVariant()).toStringList();
29
30     for(int i = 0; i < favourites.size(); i++)
31         addStationButton(favourites.at(i), favouritesCRS.at(i));
32
33     //Make a button to allow the user to edit their favourites...
34     QPushButton * b = new QPushButton("Edit Favourites...");
35     //...add it to the list of displayed buttons & the display widget
36     stationButtons.append(b);
37     ui->favourites_widget->layout()->addWidget(b);
38     b->setIcon(QIcon(":/icons/folder.png"));
39     connect(b, SIGNAL(clicked()), this, SLOT(editFavouriteDepartures()));
40
41     QPushButton * b2 = new QPushButton("Choose Station...");
42     //...add it to the list of displayed buttons & the display widget
43     stationButtons.append(b2);
44     ui->favourites_widget->layout()->addWidget(b2);
45     connect(b2, SIGNAL(clicked()), this, SLOT(chooseStation()));
46 }
47
48 void Chooser::chooseStation()
49 {
50     StationChooser chooser;
51
52     if(chooser.exec() == QDialog::Accepted)
53     {
54         departureBoard = new DepartureBoard(this);
55         departureBoard->show(chooser.getChoiceCRS());
56     }
57 }
58
59 /*
60  * Slot to show the dialog allowing the user to edit their favourite stations
61  */
62 void Chooser::editFavouriteDepartures()
63 {
64     favouriteStations.exec("FavouriteDepartureBoards", "FavouriteDepartureBoardsCRS");
65     //Update the button display in case the favourites have changed
66     updateStationButtons();
67 }
68
69 /*
70  * Adds a button to the display widget. Returns a pointer to the created button
71  */
72 QPushButton * Chooser::addStationButton(QString name, QString crs)
73 {
74     QPushButton * b = new QPushButton(name);
75     //Create the button and add it to the list of buttons, & the display widget
76     stationButtons.append(b);
77     ui->favourites_widget->layout()->addWidget(b);
78     //Set the button's name to the station's CRS code; we use this in the clicked() slot to find which button was clicked
79     b->setObjectName(crs);
80     connect(b, SIGNAL(clicked()), this, SLOT(stationSelected()));
81     return b;
82 }
83
84 /*
85  *  Slot for when one of the station buttons is clicked
86  */
87 void Chooser::stationSelected()
88 {
89     //Show the departure board for the station. The CRS code for the station is found from the button's objectName
90     departureBoard = new DepartureBoard(this);
91     departureBoard->show(sender()->objectName());
92 }
93
94 Chooser::~Chooser()
95 {
96     if(departureBoard != 0)
97         delete departureBoard;
98
99     delete ui;
100 }
101
102 /*
103  * Slot to show the journey planner
104  */
105 void Chooser::launchPlanner()
106 {
107     //Launch the webpage for the journey planner online, and quit
108     QDesktopServices::openUrl(QUrl("http://ojp.nationalrail.co.uk/en/s/planjourney/query"));
109     exit(0);
110 }
111
112 void Chooser::changeEvent(QEvent *e)
113 {
114     QMainWindow::changeEvent(e);
115     switch (e->type()) {
116     case QEvent::LanguageChange:
117         ui->retranslateUi(this);
118         break;
119     default:
120         break;
121     }
122 }