Added Sorting method Enum
[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 "stationlistproxymodel.h"
26 #include "keypressforwarder.h"
27 #include "settingsdialog.h"
28
29 #include <QActionGroup>
30 #include <QDebug>
31 #include <QKeyEvent>
32 #include <QSortFilterProxyModel>
33 #include <QStringListModel>
34
35 StationListView::StationListView(StationListModel *model, QWidget *parent) :
36     QMainWindow(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
43 {
44 #ifdef Q_WS_MAEMO_5
45     setAttribute(Qt::WA_Maemo5StackedWindow);
46     setAttribute(Qt::WA_Maemo5AutoOrientation);
47 #endif
48     ui->setupUi(this);
49     viewSelectionGroup->addAction(ui->sortByNameAction);
50     viewSelectionGroup->addAction(ui->sortNearFirstAction);
51     viewSelectionGroup->addAction(ui->sortRecentFirstAction);
52     filterModel->setSourceModel(stationListModel);
53     filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
54     ui->listView->setModel(filterModel);
55     ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
56     ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
57     ui->filterEdit->hide();
58
59     keyPressForwarder->setTarget(ui->filterEdit);
60     ui->listView->installEventFilter(keyPressForwarder);
61
62     connect(ui->showAboutAction, SIGNAL(triggered()),
63             this, SIGNAL(aboutTriggered()));
64     connect(ui->listView,
65             SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
66     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
67             SLOT(handleFilterChanges(const QString &)));
68     //filterModel->setSortRole(StationListModel::PositionRole);
69     filterModel->setSortRole(Qt::DisplayRole);
70     filterModel->sort(0);
71 }
72
73
74 StationListView::~StationListView()
75 {
76     delete ui;
77 }
78
79 void StationListView::showSettings(void)
80 {
81     qDebug() << "Show Settings";
82     SettingsDialog *settingsDialog = new SettingsDialog(this);
83     if (settingsDialog->exec() == QDialog::Accepted) {
84         // TODO Use new settings
85     }
86
87     delete settingsDialog;
88 }
89
90 void StationListView::showStation(const QModelIndex &index)
91 {
92     qDebug() << "Show Station" << index.data();
93     emit stationSelected(index.data().toString());
94 }
95
96 void StationListView::handleFilterChanges(const QString &filter)
97 {
98     if (!filter.isEmpty())
99         ui->filterEdit->show();
100     else
101         ui->filterEdit->hide();
102     filterModel->setFilterFixedString(filter);
103 }
104
105 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
106 {
107     qDebug() << "Position update received" << update;
108     filterModel->setUserPosition(update.coordinate());
109     filterModel->invalidate();
110     filterModel->sort(0);
111 }
112
113 void StationListView::handleSortingChange(const QAction *action)
114 {
115     SortingMode mode;
116     if (action == ui->sortByNameAction) {
117         mode = AlphaSorting;
118         qDebug() << "sort by name";
119     } else if (action == ui->sortNearFirstAction) {
120         mode = DistanceSorting;
121         qDebug() << "sort by distance";
122     } else if (action == ui->sortRecentFirstAction) {
123         mode = RecentUsageSorting;
124         qDebug() << "sort by recent use";
125     }
126     setSortingMode(mode);
127 }
128
129 void StationListView::setSortingMode(StationListView::SortingMode mode)
130 {
131     m_sortingMode = mode;
132 }
133
134 StationListView::SortingMode StationListView::sortingMode()
135 {
136     return m_sortingMode;
137 }