Bump version to 0.9.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 <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(StationListProxyModel::NoSorting)
44 {
45     ui->setupUi(this);
46 #ifdef Q_WS_MAEMO_5
47     setAttribute(Qt::WA_Maemo5StackedWindow);
48     setAttribute(Qt::WA_Maemo5AutoOrientation);
49     ui->filterClear->setIcon(QIcon::fromTheme("general_close"));
50 #else
51     ui->filterClear->setIcon(QIcon::fromTheme("edit-clear"));
52 #endif
53
54     viewSelectionGroup->addAction(ui->sortByNameAction);
55     viewSelectionGroup->addAction(ui->sortByDistanceAction);
56     viewSelectionGroup->addAction(ui->sortRecentFirstAction);
57     filterModel->setSourceModel(stationListModel);
58     ui->listView->setModel(filterModel);
59     ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
60     ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
61     ui->filterFrame->hide();
62
63     keyPressForwarder->setTarget(ui->filterEdit);
64     ui->listView->installEventFilter(keyPressForwarder);
65
66     connect(ui->showAboutAction, SIGNAL(triggered()),
67             this, SIGNAL(aboutTriggered()));
68     connect(ui->showSettingsAction, SIGNAL(triggered()),
69             this, SIGNAL(settingsChangeRequested()));
70     connect(ui->listView, SIGNAL(activated(QModelIndex)),
71             SLOT(showStation(QModelIndex)));
72     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
73             SLOT(handleFilterChanges(const QString &)));
74     connect(ui->filterClear, SIGNAL(clicked()), SLOT(handlefilterClearClick()));
75     connect(viewSelectionGroup, SIGNAL(triggered(QAction*)),
76             SLOT(handleSortingChange(QAction*)));
77
78     QSettings settings;
79     StationListProxyModel::SortingMode mode =
80             static_cast<StationListProxyModel::SortingMode>(
81                 settings.value("StationListView/SortingMode",
82                                StationListProxyModel::AlphaSorting).toInt());
83     filterModel->setRecentStations(
84                 settings.value("RecentStations").toString().split(","));
85     setSortingMode(mode);
86     emit sortingModeChanged(mode);
87 }
88
89
90 StationListView::~StationListView()
91 {
92     delete ui;
93 }
94
95 void StationListView::showStation(const QModelIndex &index)
96 {
97     qDebug() << "Show Station" << index.data();
98     emit stationSelected(index.data().toString());
99 }
100
101 void StationListView::handleFilterChanges(const QString &filter)
102 {
103     if (!filter.isEmpty())
104         ui->filterFrame->show();
105     else
106         ui->filterFrame->hide();
107     filterModel->setFilterFixedString(filter);
108     qDebug() << "Filtering for" << filter;
109 }
110
111 void StationListView::handleSortingChange(QAction *action)
112 {
113     StationListProxyModel::SortingMode mode = StationListProxyModel::NoSorting;
114     if (action == ui->sortByNameAction) {
115         mode = StationListProxyModel::AlphaSorting;
116         qDebug() << "sort by name";
117     } else if (action == ui->sortByDistanceAction) {
118         mode = StationListProxyModel::DistanceSorting;
119         qDebug() << "sort by distance";
120     } else if (action == ui->sortRecentFirstAction) {
121         mode = StationListProxyModel::RecentUsageSorting;
122         qDebug() << "sort by recent use";
123     }
124
125     QSettings settings;
126     settings.setValue("StationListView/SortingMode", mode);
127
128     setSortingMode(mode);
129 }
130
131 void StationListView::setSortingMode(StationListProxyModel::SortingMode mode)
132 {
133     qDebug() << "setSorting Mode" << mode << "called";
134     if (mode != m_sortingMode) {
135         switch (mode) {
136         case StationListProxyModel::AlphaSorting:
137             ui->sortByNameAction->setChecked(true);
138             break;
139         case StationListProxyModel::DistanceSorting:
140             ui->sortByDistanceAction->setChecked(true);
141             break;
142         case StationListProxyModel::RecentUsageSorting:
143             ui->sortRecentFirstAction->setChecked(true);
144             break;
145         case StationListProxyModel::NoSorting:
146         default:
147             break;
148         }
149         m_sortingMode = mode;
150         filterModel->setSortingMode(mode);
151
152     }
153 }
154
155 StationListProxyModel::SortingMode StationListView::sortingMode()
156 {
157     return m_sortingMode;
158 }
159
160 void StationListView::handlefilterClearClick()
161 {
162     ui->filterEdit->clear();
163 }