Implemented minimal settings dialog
[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->showSettingsAction, SIGNAL(triggered()),
67             this, SIGNAL(settingsChangeRequested()));
68     connect(ui->listView,
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*)));
74
75     QSettings settings;
76     SortingMode mode = static_cast<SortingMode>(
77                 settings.value("StationListView/SortingMode",
78                                AlphaSorting).toInt());
79     filterModel->setRecentStations(
80                 settings.value("RecentStations").toString().split(","));
81     setSortingMode(mode);
82 }
83
84
85 StationListView::~StationListView()
86 {
87     delete ui;
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(QAction *action)
114 {
115     SortingMode mode = NoSorting;
116     if (action == ui->sortByNameAction) {
117         mode = AlphaSorting;
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";
125     }
126
127     QSettings settings;
128     settings.setValue("StationListView/SortingMode", mode);
129
130     setSortingMode(mode);
131 }
132
133 void StationListView::setSortingMode(StationListView::SortingMode mode)
134 {
135     qDebug() << "setSorting Mode" << mode << "called";
136     if (mode != m_sortingMode) {
137         m_sortingMode = mode;
138         filterModel->setRecentOnlyFilter(false);
139
140         switch (mode) {
141         case AlphaSorting:
142             filterModel->setSortRole(Qt::DisplayRole);
143             ui->sortByNameAction->setChecked(true);
144             break;
145         case DistanceSorting:
146             filterModel->setSortRole(StationListModel::PositionRole);
147             ui->sortByDistanceAction->setChecked(true);
148             break;
149         case RecentUsageSorting:
150             ui->sortRecentFirstAction->setChecked(true);
151             filterModel->setRecentOnlyFilter(true);
152             break;
153         case NoSorting:
154         default:
155             break;
156         }
157         filterModel->invalidate();
158         filterModel->sort(0);
159         emit sortingModeChanged(mode);
160     }
161 }
162
163 StationListView::SortingMode StationListView::sortingMode()
164 {
165     return m_sortingMode;
166 }