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