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()));
67 SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
68 connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
69 SLOT(handleFilterChanges(const QString &)));
70 connect(viewSelectionGroup, SIGNAL(triggered(QAction*)),
71 SLOT(handleSortingChange(QAction*)));
74 SortingMode mode = static_cast<SortingMode>(
75 settings.value("StationListView/SortingMode",
76 AlphaSorting).toInt());
81 StationListView::~StationListView()
86 void StationListView::showSettings(void)
88 qDebug() << "Show Settings";
89 SettingsDialog *settingsDialog = new SettingsDialog(this);
90 if (settingsDialog->exec() == QDialog::Accepted) {
91 // TODO Use new settings
94 delete settingsDialog;
97 void StationListView::showStation(const QModelIndex &index)
99 qDebug() << "Show Station" << index.data();
100 emit stationSelected(index.data().toString());
103 void StationListView::handleFilterChanges(const QString &filter)
105 if (!filter.isEmpty())
106 ui->filterEdit->show();
108 ui->filterEdit->hide();
109 filterModel->setFilterFixedString(filter);
112 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
114 qDebug() << "Position update received" << update;
115 filterModel->setUserPosition(update.coordinate());
116 filterModel->invalidate();
117 filterModel->sort(0);
120 void StationListView::handleSortingChange(QAction *action)
122 SortingMode mode = NoSorting;
123 if (action == ui->sortByNameAction) {
125 qDebug() << "sort by name";
126 } else if (action == ui->sortByDistanceAction) {
127 mode = DistanceSorting;
128 qDebug() << "sort by distance";
129 } else if (action == ui->sortRecentFirstAction) {
130 mode = RecentUsageSorting;
131 qDebug() << "sort by recent use";
135 settings.setValue("StationListView/SortingMode", mode);
137 setSortingMode(mode);
140 void StationListView::setSortingMode(StationListView::SortingMode mode)
142 if (mode != m_sortingMode) {
143 m_sortingMode = mode;
146 filterModel->setSortRole(Qt::DisplayRole);
147 ui->sortByNameAction->setChecked(true);
149 case DistanceSorting:
150 filterModel->setSortRole(StationListModel::PositionRole);
151 ui->sortByDistanceAction->setChecked(true);
153 case RecentUsageSorting:
154 ui->sortRecentFirstAction->setChecked(true);
160 filterModel->invalidate();
161 filterModel->sort(0);
162 emit sortingModeChanged(mode);
166 StationListView::SortingMode StationListView::sortingMode()
168 return m_sortingMode;