Added autohiding for Station List filter
authorLuciano Montanaro <mikelima@gmail.com>
Thu, 19 May 2011 22:53:00 +0000 (00:53 +0200)
committerLuciano Montanaro <mikelima@zaphod>
Thu, 19 May 2011 22:53:00 +0000 (00:53 +0200)
application/application.pro
application/keypressforwarder.cpp [new file with mode: 0644]
application/keypressforwarder.h [new file with mode: 0644]
application/stationlistview.cpp
application/stationlistview.h

index 68dcd95..0510fcf 100644 (file)
@@ -15,13 +15,15 @@ SOURCES += main.cpp \
     settingsdialog.cpp \
     stationview.cpp \
     app.cpp \
-    stationlistview.cpp
+    stationlistview.cpp \
+    keypressforwarder.cpp
 
 HEADERS += \
     settingsdialog.h \
     stationview.h \
     app.h \
-    stationlistview.h
+    stationlistview.h \
+    keypressforwarder.h
 
 FORMS += \
     settingsdialog.ui \
diff --git a/application/keypressforwarder.cpp b/application/keypressforwarder.cpp
new file mode 100644 (file)
index 0000000..761386c
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+
+Copyright (C) 2011 mikelima
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.
+
+*/
+
+#include "keypressforwarder.h"
+
+#include <QApplication>
+#include <QDebug>
+#include <QEvent>
+#include <QKeyEvent>
+#include <QWidget>
+
+KeyPressForwarder::KeyPressForwarder(QObject *parent) :
+    QObject(parent)
+{
+}
+
+bool KeyPressForwarder::eventFilter(QObject *obj, QEvent *event)
+{
+    if (event->type() == QEvent::KeyPress) {
+        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
+        qDebug() << keyEvent->key();
+        if (target) {
+            target->show();
+            target->setFocus();
+            return QApplication::sendEvent(target, event);
+        }
+    }
+    return QObject::eventFilter(obj, event);
+}
+
+void KeyPressForwarder::setTarget(QWidget *w)
+{
+    target = w;
+}
diff --git a/application/keypressforwarder.h b/application/keypressforwarder.h
new file mode 100644 (file)
index 0000000..919ca42
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+
+Copyright (C) 2011 mikelima
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.
+
+*/
+
+#ifndef EDITFIELDENABLER_H
+#define EDITFIELDENABLER_H
+
+#include <QObject>
+
+class QWidget;
+
+class KeyPressForwarder : public QObject
+{
+    Q_OBJECT
+public:
+    explicit KeyPressForwarder(QObject *parent = 0);
+
+    void setTarget(QWidget *w);
+
+protected:
+    bool eventFilter(QObject *obj, QEvent *event);
+
+private:
+    QWidget *target;
+};
+
+#endif // EDITFIELDENABLER_H
index 18552df..660ddf2 100644 (file)
@@ -22,11 +22,12 @@ Boston, MA 02110-1301, USA.
 #include "stationlistview.h"
 #include "ui_stationlistview.h"
 
+#include "keypressforwarder.h"
 #include "settingsdialog.h"
-#include "stationview.h"
 
 #include <QActionGroup>
 #include <QDebug>
+#include <QKeyEvent>
 #include <QSortFilterProxyModel>
 #include <QStringListModel>
 
@@ -36,7 +37,7 @@ StationListView::StationListView(QWidget *parent) :
     viewSelectionGroup(new QActionGroup(0)),
     stationListModel(new QStringListModel(this)),
     filterModel(new QSortFilterProxyModel(this)),
-    stationView(0)
+    keyPressForwarder(new KeyPressForwarder(this))
 
 {
 #ifdef Q_WS_MAEMO_5
@@ -83,14 +84,19 @@ StationListView::StationListView(QWidget *parent) :
                 << "Camogli";
     stationListModel->setStringList(stationList);
     filterModel->setSourceModel(stationListModel);
+    filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
     ui->listView->setModel(filterModel);
     ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
     ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
-    //ui->filterEdit->hide();
+    ui->filterEdit->hide();
+
+    keyPressForwarder->setTarget(ui->filterEdit);
+    ui->listView->installEventFilter(keyPressForwarder);
+
     connect(ui->listView,
             SIGNAL(activated(QModelIndex)), SLOT(showStation(QModelIndex)));
     connect(ui->filterEdit, SIGNAL(textChanged(const QString &)),
-            filterModel, SLOT(setFilterFixedString(const QString &)));
+            SLOT(handleFilterChanges(const QString &)));
 }
 
 StationListView::~StationListView()
@@ -114,3 +120,12 @@ void StationListView::showStation(const QModelIndex &index)
     qDebug() << "Show Station" << index.data();
     emit stationSelected(index.data().toString());
 }
+
+void StationListView::handleFilterChanges(const QString &filter)
+{
+    if (!filter.isEmpty())
+        ui->filterEdit->show();
+    else
+        ui->filterEdit->hide();
+    filterModel->setFilterFixedString(filter);
+}
index d44bf15..bce0d9f 100644 (file)
@@ -11,6 +11,7 @@ namespace Ui {
 class QActionGroup;
 class QStringListModel;
 class QSortFilterProxyModel;
+class KeyPressForwarder;
 
 class StationView;
 
@@ -28,13 +29,14 @@ signals:
 private slots:
     void showSettings(void);
     void showStation(const QModelIndex &index);
+    void handleFilterChanges(const QString &filter);
 
 private:
     Ui::StationListView *ui;
     QActionGroup *viewSelectionGroup;
     QStringListModel *stationListModel;
     QSortFilterProxyModel *filterModel;
-    StationView *stationView;
+    KeyPressForwarder *keyPressForwarder;
 };
 
 #endif // STATIONLISTVIEW_H