1/* This file is part of the KDE project
2 Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17
18*/
19
20#ifndef KFILEPLACESVIEW_P_H
21#define KFILEPLACESVIEW_P_H
22
23#include <QMouseEvent>
24
25class KFilePlacesEventWatcher
26 : public QObject
27{
28Q_OBJECT
29
30public:
31 KFilePlacesEventWatcher(QObject *parent = 0)
32 : QObject(parent) {}
33
34 const QModelIndex &hoveredIndex() const
35 {
36 return m_hoveredIndex;
37 }
38
39 const QModelIndex &focusedIndex() const
40 {
41 return m_focusedIndex;
42 }
43
44Q_SIGNALS:
45 void entryEntered(const QModelIndex &index);
46 void entryLeft(const QModelIndex &index);
47
48public Q_SLOTS:
49 void currentIndexChanged(const QModelIndex &index)
50 {
51 if (m_focusedIndex.isValid() && m_focusedIndex != m_hoveredIndex) {
52 emit entryLeft(m_focusedIndex);
53 }
54 if (index == m_hoveredIndex) {
55 m_focusedIndex = m_hoveredIndex;
56 return;
57 }
58 if (index.isValid()) {
59 emit entryEntered(index);
60 }
61 m_focusedIndex = index;
62 }
63
64protected:
65 virtual bool eventFilter(QObject *watched, QEvent *event)
66 {
67 switch (event->type()) {
68 case QEvent::MouseMove: {
69 QAbstractItemView *view = qobject_cast<QAbstractItemView*>(watched->parent());
70 const QModelIndex index = view->indexAt(static_cast<QMouseEvent*>(event)->pos());
71 if (index != m_hoveredIndex) {
72 if (m_hoveredIndex.isValid() && m_hoveredIndex != m_focusedIndex) {
73 emit entryLeft(m_hoveredIndex);
74 }
75 if (index.isValid() && index != m_focusedIndex) {
76 emit entryEntered(index);
77 }
78 m_hoveredIndex = index;
79 }
80 }
81 break;
82 case QEvent::Leave:
83 if (m_hoveredIndex.isValid() && m_hoveredIndex != m_focusedIndex) {
84 emit entryLeft(m_hoveredIndex);
85 }
86 m_hoveredIndex = QModelIndex();
87 break;
88 case QEvent::MouseButtonPress:
89 case QEvent::MouseButtonDblClick: {
90 // Prevent the selection clearing by clicking on the viewport directly
91 QAbstractItemView *view = qobject_cast<QAbstractItemView*>(watched->parent());
92 if (!view->indexAt(static_cast<QMouseEvent*>(event)->pos()).isValid()) {
93 return true;
94 }
95 }
96 break;
97 default:
98 return false;
99 }
100
101 return false;
102 }
103
104private:
105 QPersistentModelIndex m_hoveredIndex;
106 QPersistentModelIndex m_focusedIndex;
107};
108
109#endif
110