]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp
09b4eaf23ff1948e8e38d3a6e5021c0f40ba6bd5
[dolphin.git] / src / kitemviews / private / kitemlistkeyboardsearchmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistkeyboardsearchmanager.h"
24
25 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) :
26 QObject(parent),
27 m_isSearchRestarted(false),
28 m_timeout(1000)
29 {
30 m_keyboardInputTime.invalidate();
31 }
32
33 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
34 {
35 }
36
37 bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
38 {
39 const bool keyboardTimeWasValid = m_keyboardInputTime.isValid();
40 const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart();
41 return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid;
42 }
43
44 void KItemListKeyboardSearchManager::addKeys(const QString& keys)
45 {
46 if (shouldClearSearchIfInputTimeReached()) {
47 m_searchedString.clear();
48 }
49
50 const bool newSearch = m_searchedString.isEmpty();
51
52 // Do not start a new search if the user pressed Space. Only add
53 // it to the search string if a search is in progress already.
54 if (newSearch && keys == QLatin1Char(' ')) {
55 return;
56 }
57
58 if (!keys.isEmpty()) {
59 m_searchedString.append(keys);
60
61 // Special case:
62 // If the same key is pressed repeatedly, the next item matching that key should be highlighted
63 const QChar firstKey = m_searchedString.length() > 0 ? m_searchedString.at(0) : QChar();
64 const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length();
65
66 // Searching for a matching item should start from the next item if either
67 // 1. a new search is started and a search has not been restarted or
68 // 2. a 'repeated key' search is done.
69 const bool searchFromNextItem = (!m_isSearchRestarted && newSearch) || sameKey;
70
71 // to remember not to searchFromNextItem if selection was deselected
72 // loosing keyboard search context basically
73 m_isSearchRestarted = false;
74
75 emit changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem);
76 }
77 m_keyboardInputTime.start();
78 }
79
80 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds)
81 {
82 m_timeout = milliseconds;
83 }
84
85 qint64 KItemListKeyboardSearchManager::timeout() const
86 {
87 return m_timeout;
88 }
89
90 void KItemListKeyboardSearchManager::cancelSearch()
91 {
92 m_isSearchRestarted = true;
93 m_searchedString.clear();
94 }
95
96 void KItemListKeyboardSearchManager::slotCurrentChanged(int current, int previous)
97 {
98 Q_UNUSED(previous);
99
100 if (current < 0) {
101 // The current item has been removed. We should cancel the search.
102 cancelSearch();
103 }
104 }
105
106 void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet& current, const KItemSet& previous)
107 {
108 if (!previous.isEmpty() && current.isEmpty() && previous.count() > 0 && current.count() == 0) {
109 // The selection has been emptied. We should cancel the search.
110 cancelSearch();
111 }
112 }