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>
33 #include <QSortFilterProxyModel>
34 #include <QStringListModel>
36 StationListView::StationListView(StationListModel *model, QWidget *parent) :
38 ui(new Ui::StationListView),
39 viewSelectionGroup(new QActionGroup(0)),
40 stationListModel(model),
41 filterModel(new StationListProxyModel(this)),
42 keyPressForwarder(new KeyPressForwarder(this)),
43 m_sortingMode(NoSorting)
47 setAttribute(Qt::WA_Maemo5StackedWindow);
48 setAttribute(Qt::WA_Maemo5AutoOrientation);
51 viewSelectionGroup->addAction(ui->sortByNameAction);
52 viewSelectionGroup->addAction(ui->sortByDistanceAction);
53 viewSelectionGroup->addAction(ui->sortRecentFirstAction);
54 filterModel->setSourceModel(stationListModel);
55 filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
56 ui->listView->setModel(filterModel);
57 ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
58 ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
59 ui->filterEdit->hide();
61 keyPressForwarder->setTarget(ui->filterEdit);
62 ui->listView->installEventFilter(keyPressForwarder);
64 connect(ui->showAboutAction, SIGNAL(triggered()),
65 this, SIGNAL(aboutTriggered()));
66 connect(ui->showSettingsAction, SIGNAL(triggered()),
67 this, SIGNAL(settingsChangeRequested()));
69 SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
70 connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
71 SLOT(handleFilterChanges(const QString &)));
72 connect(viewSelectionGroup, SIGNAL(triggered(QAction*)),
73 SLOT(handleSortingChange(QAction*)));
76 SortingMode mode = static_cast<SortingMode>(
77 settings.value("StationListView/SortingMode",
78 AlphaSorting).toInt());
79 filterModel->setRecentStations(
80 settings.value("RecentStations").toString().split(","));
85 StationListView::~StationListView()
90 void StationListView::showStation(const QModelIndex &index)
92 qDebug() << "Show Station" << index.data();
93 emit stationSelected(index.data().toString());
96 void StationListView::handleFilterChanges(const QString &filter)
98 if (!filter.isEmpty())
99 ui->filterEdit->show();
101 ui->filterEdit->hide();
102 filterModel->setFilterFixedString(filter);
105 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
107 qDebug() << "Position update received" << update;
108 filterModel->setUserPosition(update.coordinate());
109 filterModel->invalidate();
110 filterModel->sort(0);
113 void StationListView::handleSortingChange(QAction *action)
115 SortingMode mode = NoSorting;
116 if (action == ui->sortByNameAction) {
118 qDebug() << "sort by name";
119 } else if (action == ui->sortByDistanceAction) {
120 mode = DistanceSorting;
121 qDebug() << "sort by distance";
122 } else if (action == ui->sortRecentFirstAction) {
123 mode = RecentUsageSorting;
124 qDebug() << "sort by recent use";
128 settings.setValue("StationListView/SortingMode", mode);
130 setSortingMode(mode);
133 void StationListView::setSortingMode(StationListView::SortingMode mode)
135 qDebug() << "setSorting Mode" << mode << "called";
136 if (mode != m_sortingMode) {
137 m_sortingMode = mode;
138 filterModel->setRecentOnlyFilter(false);
142 filterModel->setSortRole(Qt::DisplayRole);
143 ui->sortByNameAction->setChecked(true);
145 case DistanceSorting:
146 filterModel->setSortRole(StationListModel::PositionRole);
147 ui->sortByDistanceAction->setChecked(true);
149 case RecentUsageSorting:
150 ui->sortRecentFirstAction->setChecked(true);
151 filterModel->setRecentOnlyFilter(true);
157 filterModel->invalidate();
158 filterModel->sort(0);
159 emit sortingModeChanged(mode);
163 StationListView::SortingMode StationListView::sortingMode()
165 return m_sortingMode;