Enable use of StationListModel
[quandoparte] / application / stationlistview.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "stationlistview.h"
23 #include "ui_stationlistview.h"
24 #include "stationlistmodel.h"
25 #include "keypressforwarder.h"
26 #include "settingsdialog.h"
27
28 #include <QActionGroup>
29 #include <QDebug>
30 #include <QKeyEvent>
31 #include <QSortFilterProxyModel>
32 #include <QStringListModel>
33
34 StationListView::StationListView(StationListModel *model, QWidget *parent) :
35     QMainWindow(parent),
36     ui(new Ui::StationListView),
37     viewSelectionGroup(new QActionGroup(0)),
38     stationListModel(model),
39     filterModel(new QSortFilterProxyModel(this)),
40     keyPressForwarder(new KeyPressForwarder(this))
41
42 {
43 #ifdef Q_WS_MAEMO_5
44     setAttribute(Qt::WA_Maemo5StackedWindow);
45     setAttribute(Qt::WA_Maemo5AutoOrientation);
46 #endif
47     ui->setupUi(this);
48     viewSelectionGroup->addAction(ui->sortByNameAction);
49     viewSelectionGroup->addAction(ui->sortNearFirstAction);
50     viewSelectionGroup->addAction(ui->sortRecentFirstAction);
51 #if 0
52     QStringList stationList;
53     stationList << "Savona"
54                 << "Albisola"
55                 << "Celle"
56                 << "Varazze"
57                 << "Cogoleto"
58                 << "Arenzano"
59                 << "Genova Voltri"
60                 << "Genova Pra"
61                 << "Genova Pegli"
62                 << "Genova Sestri Ponente"
63                 << "Genova Cornigliano"
64                 << "Genova Sampierdarena"
65                 << "Genova Borzoli"
66                 << "Genova Costa di Sestri Ponente"
67                 << "Genova Granara"
68                 << "Genova Acquasanta"
69                 << "Mele"
70                 << "Campo Ligure"
71                 << "Rossiglione"
72                 << "Ovada"
73                 << "Molare"
74                 << "Prasco Cremolino"
75                 << "Visone"
76                 << "Acqui Terme"
77                 << "Alice Belcolle"
78                 << "Mombaruzzo"
79                 << "Nizza Monferrato"
80                 << "Castelnuovo Calcea"
81                 << "Montegrosso"
82                 << "Vigliano"
83                 << "Mongardino"
84                 << "Asti"
85                 << "Genova Rivarolo"
86                 << "Genova Bolzaneto"
87                 << "Genova San Biagio"
88                 << "Genova Pontedecimo"
89                 << "Piano Orzzontale dei Giovi"
90                 << "Mignanego"
91                 << "Busalla"
92                 << "Borgo Fornari"
93                 << "Ronco Scrivia"
94                 << "Arquata Scrivia"
95                 << "Serravalle Scrivia"
96                 << "Novi Ligure"
97                 << "Alessandria"
98                 << "Genova Via di Francia"
99                 << "Genova Piazza Principe"
100                 << "Genova Brignole"
101                 << "Genova Sturla"
102                 << "Genova Quarto dei Mille"
103                 << "Genova Quinto al Mare"
104                 << "Genova Nervi"
105                 << "Bogliasco"
106                 << "Pontetto"
107                 << "Pieve Ligure"
108                 << "Sori"
109                 << "Mulinetti"
110                 << "Recco"
111                 << "Camogli";
112     stationListModel->setStringList(stationList);
113 #endif
114     filterModel->setSourceModel(stationListModel);
115     filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
116     ui->listView->setModel(filterModel);
117     ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
118     ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
119     ui->filterEdit->hide();
120
121     keyPressForwarder->setTarget(ui->filterEdit);
122     ui->listView->installEventFilter(keyPressForwarder);
123
124     connect(ui->showAboutAction, SIGNAL(triggered()),
125             this, SIGNAL(aboutTriggered()));
126     connect(ui->listView,
127             SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
128     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
129             SLOT(handleFilterChanges(const QString &)));
130 }
131
132 StationListView::~StationListView()
133 {
134     delete ui;
135 }
136
137 void StationListView::showSettings(void)
138 {
139     qDebug() << "Show Settings";
140     SettingsDialog *settingsDialog = new SettingsDialog(this);
141     if (settingsDialog->exec() == QDialog::Accepted) {
142         // TODO Use new settings
143     }
144
145     delete settingsDialog;
146 }
147
148 void StationListView::showStation(const QModelIndex &index)
149 {
150     qDebug() << "Show Station" << index.data();
151     emit stationSelected(index.data().toString());
152 }
153
154 void StationListView::handleFilterChanges(const QString &filter)
155 {
156     if (!filter.isEmpty())
157         ui->filterEdit->show();
158     else
159         ui->filterEdit->hide();
160     filterModel->setFilterFixedString(filter);
161 }