Bumped version to 0.2.0
[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     m_sortingMode(NoSorting)
43
44 {
45 #ifdef Q_WS_MAEMO_5
46     setAttribute(Qt::WA_Maemo5StackedWindow);
47     setAttribute(Qt::WA_Maemo5AutoOrientation);
48 #endif
49     ui->setupUi(this);
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();
59
60     keyPressForwarder->setTarget(ui->filterEdit);
61     ui->listView->installEventFilter(keyPressForwarder);
62
63     connect(ui->showAboutAction, SIGNAL(triggered()),
64             this, SIGNAL(aboutTriggered()));
65     connect(ui->listView,
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*)));
71
72     setSortingMode(AlphaSorting);
73 }
74
75
76 StationListView::~StationListView()
77 {
78     delete ui;
79 }
80
81 void StationListView::showSettings(void)
82 {
83     qDebug() << "Show Settings";
84     SettingsDialog *settingsDialog = new SettingsDialog(this);
85     if (settingsDialog->exec() == QDialog::Accepted) {
86         // TODO Use new settings
87     }
88
89     delete settingsDialog;
90 }
91
92 void StationListView::showStation(const QModelIndex &index)
93 {
94     qDebug() << "Show Station" << index.data();
95     emit stationSelected(index.data().toString());
96 }
97
98 void StationListView::handleFilterChanges(const QString &filter)
99 {
100     if (!filter.isEmpty())
101         ui->filterEdit->show();
102     else
103         ui->filterEdit->hide();
104     filterModel->setFilterFixedString(filter);
105 }
106
107 void StationListView::updatePosition(const QtMobility::QGeoPositionInfo &update)
108 {
109     qDebug() << "Position update received" << update;
110     filterModel->setUserPosition(update.coordinate());
111     filterModel->invalidate();
112     filterModel->sort(0);
113 }
114
115 void StationListView::handleSortingChange(QAction *action)
116 {
117     SortingMode mode = NoSorting;
118     if (action == ui->sortByNameAction) {
119         mode = AlphaSorting;
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";
127     }
128     setSortingMode(mode);
129 }
130
131 void StationListView::setSortingMode(StationListView::SortingMode mode)
132 {
133     if (mode != m_sortingMode) {
134         m_sortingMode = mode;
135         switch (mode) {
136         case AlphaSorting:
137             filterModel->setSortRole(Qt::DisplayRole);
138             ui->sortByNameAction->setChecked(true);
139             break;
140         case DistanceSorting:
141             filterModel->setSortRole(StationListModel::PositionRole);
142             ui->sortByDistanceAction->setChecked(true);
143             break;
144         case RecentUsageSorting:
145             ui->sortRecentFirstAction->setChecked(true);
146             break;
147         case NoSorting:
148         default:
149             break;
150         }
151         filterModel->invalidate();
152         filterModel->sort(0);
153         emit sortingModeChanged(mode);
154     }
155 }
156
157 StationListView::SortingMode StationListView::sortingMode()
158 {
159     return m_sortingMode;
160 }