Added autohiding for Station List filter
[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
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(QWidget *parent) :
35     QMainWindow(parent),
36     ui(new Ui::StationListView),
37     viewSelectionGroup(new QActionGroup(0)),
38     stationListModel(new QStringListModel(this)),
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     QStringList stationList;
52     stationList << "Savona"
53                 << "Albisola"
54                 << "Celle"
55                 << "Varazze"
56                 << "Cogoleto"
57                 << "Arenzano"
58                 << "Genova Voltri"
59                 << "Genova Pra"
60                 << "Genova Pegli"
61                 << "Genova Sestri Ponente"
62                 << "Genova Cornigliano"
63                 << "Genova Sampierdarena"
64                 << "Genova Rivarolo"
65                 << "Genova Bolzaneto"
66                 << "Genova San Biagio"
67                 << "Genova Pontedecimo"
68                 << "Piano Orzzontale dei Giovi"
69                 << "Mignanego"
70                 << "Busalla"
71                 << "Genova Via di Francia"
72                 << "Genova Piazza Principe"
73                 << "Genova Brignole"
74                 << "Genova Sturla"
75                 << "Genova Quarto dei Mille"
76                 << "Genova Quinto al Mare"
77                 << "Genova Nervi"
78                 << "Bogliasco"
79                 << "Pontetto"
80                 << "Pieve Ligure"
81                 << "Sori"
82                 << "Mulinetti"
83                 << "Recco"
84                 << "Camogli";
85     stationListModel->setStringList(stationList);
86     filterModel->setSourceModel(stationListModel);
87     filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
88     ui->listView->setModel(filterModel);
89     ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
90     ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
91     ui->filterEdit->hide();
92
93     keyPressForwarder->setTarget(ui->filterEdit);
94     ui->listView->installEventFilter(keyPressForwarder);
95
96     connect(ui->listView,
97             SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
98     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
99             SLOT(handleFilterChanges(const QString &)));
100 }
101
102 StationListView::~StationListView()
103 {
104     delete ui;
105 }
106
107 void StationListView::showSettings(void)
108 {
109     qDebug() << "Show Settings";
110     SettingsDialog *settingsDialog = new SettingsDialog(this);
111     if (settingsDialog->exec() == QDialog::Accepted) {
112         // TODO Use new settings
113     }
114
115     delete settingsDialog;
116 }
117
118 void StationListView::showStation(const QModelIndex &index)
119 {
120     qDebug() << "Show Station" << index.data();
121     emit stationSelected(index.data().toString());
122 }
123
124 void StationListView::handleFilterChanges(const QString &filter)
125 {
126     if (!filter.isEmpty())
127         ui->filterEdit->show();
128     else
129         ui->filterEdit->hide();
130     filterModel->setFilterFixedString(filter);
131 }