3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
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.
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.
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.
22 #include "stationlistview.h"
23 #include "ui_stationlistview.h"
24 #include "stationlistmodel.h"
25 #include "stationlistproxymodel.h"
26 #include "keypressforwarder.h"
27 #include "settingsdialog.h"
29 #include <QActionGroup>
32 #include <QSortFilterProxyModel>
33 #include <QStringListModel>
35 StationListView::StationListView(StationListModel *model, QWidget *parent) :
37 ui(new Ui::StationListView),
38 viewSelectionGroup(new QActionGroup(0)),
39 stationListModel(model),
40 filterModel(new StationListProxyModel(this)),
41 keyPressForwarder(new KeyPressForwarder(this)),
42 m_sortingMode(NoSorting)
46 setAttribute(Qt::WA_Maemo5StackedWindow);
47 setAttribute(Qt::WA_Maemo5AutoOrientation);
50 viewSelectionGroup->addAction(ui->sortByNameAction);
51 viewSelectionGroup->addAction(ui->sortByDistanceAction);
52 viewSelectionGroup->addAction(ui->sortRecentFirstAction);
53 filterModel->setSourceModel(stationListModel);
54 filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
55 ui->listView->setModel(filterModel);
56 ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
57 ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
58 ui->filterEdit->hide();
60 keyPressForwarder->setTarget(ui->filterEdit);
61 ui->listView->installEventFilter(keyPressForwarder);
63 connect(ui->showAboutAction, SIGNAL(triggered()),
64 this, SIGNAL(aboutTriggered()));
66 SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
67 connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
68 SLOT(handleFilterChanges(const QString &)));
69 connect(viewSelectionGroup, SIGNAL(triggered(QAction*)),
70 SLOT(handleSortingChange(QAction*)));
72 setSortingMode(AlphaSorting);
76 StationListView::~StationListView()
81 void StationListView::showSettings(void)
83 qDebug() << "Show Settings";
84 SettingsDialog *settingsDialog = new SettingsDialog(this);
85 if (settingsDialog->exec() == QDialog::Accepted) {
86 // TODO Use new settings
89 delete settingsDialog;
92 void StationListView::showStation(const QModelIndex &index)
94 qDebug() << "Show Station" << index.data();
95 emit stationSelected(index.data().toString());
98 void StationListView::handleFilterChanges(const QString &filter)
100 if (!filter.isEmpty())
101 ui->filterEdit->show();
103 ui->filterEdit->hide();
104 filterModel->setFilterFixedString(filter);
107 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
109 qDebug() << "Position update received" << update;
110 filterModel->setUserPosition(update.coordinate());
111 filterModel->invalidate();
112 filterModel->sort(0);
115 void StationListView::handleSortingChange(QAction *action)
117 SortingMode mode = NoSorting;
118 if (action == ui->sortByNameAction) {
120 qDebug() << "sort by name";
121 } else if (action == ui->sortByDistanceAction) {
122 mode = DistanceSorting;
123 qDebug() << "sort by distance";
124 } else if (action == ui->sortRecentFirstAction) {
125 mode = RecentUsageSorting;
126 qDebug() << "sort by recent use";
128 setSortingMode(mode);
131 void StationListView::setSortingMode(StationListView::SortingMode mode)
133 if (mode != m_sortingMode) {
134 m_sortingMode = mode;
137 filterModel->setSortRole(Qt::DisplayRole);
138 ui->sortByNameAction->setChecked(true);
140 case DistanceSorting:
141 filterModel->setSortRole(StationListModel::PositionRole);
142 ui->sortByDistanceAction->setChecked(true);
144 case RecentUsageSorting:
145 ui->sortRecentFirstAction->setChecked(true);
151 filterModel->invalidate();
152 filterModel->sort(0);
153 emit sortingModeChanged(mode);
157 StationListView::SortingMode StationListView::sortingMode()
159 return m_sortingMode;