Changeset View
Changeset View
Standalone View
Standalone View
src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp
Show All 16 Lines | |||||
17 | * You should have received a copy of the GNU General Public License * | 17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the * | 18 | * along with this program; if not, write to the * | ||
19 | * Free Software Foundation, Inc., * | 19 | * Free Software Foundation, Inc., * | ||
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||
21 | ***************************************************************************/ | 21 | ***************************************************************************/ | ||
22 | 22 | | |||
23 | #include "kitemlistkeyboardsearchmanager.h" | 23 | #include "kitemlistkeyboardsearchmanager.h" | ||
24 | 24 | | |||
25 | | ||||
26 | KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) : | 25 | KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) : | ||
27 | QObject(parent), | 26 | QObject(parent), | ||
27 | m_isSearchRestarted(false), | ||||
28 | m_timeout(1000) | 28 | m_timeout(1000) | ||
29 | { | 29 | { | ||
30 | m_keyboardInputTime.invalidate(); | 30 | m_keyboardInputTime.invalidate(); | ||
31 | } | 31 | } | ||
32 | 32 | | |||
33 | KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager() | 33 | KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager() | ||
34 | { | 34 | { | ||
35 | } | 35 | } | ||
36 | 36 | | |||
37 | void KItemListKeyboardSearchManager::addKeys(const QString& keys) | 37 | bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached() | ||
38 | { | 38 | { | ||
39 | const bool keyboardTimeWasValid = m_keyboardInputTime.isValid(); | 39 | const bool keyboardTimeWasValid = m_keyboardInputTime.isValid(); | ||
40 | const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart(); | 40 | const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart(); | ||
41 | if (keyboardInputTimeElapsed > m_timeout || !keyboardTimeWasValid) { | 41 | return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid; | ||
42 | } | ||||
elvisangelaccio: This is an unrelated refactoring that should go in its own commit. Feel free to commit this… | |||||
43 | | ||||
44 | void KItemListKeyboardSearchManager::addKeys(const QString& keys) | ||||
45 | { | ||||
46 | if (shouldClearSearchIfInputTimeReached()) { | ||||
42 | m_searchedString.clear(); | 47 | m_searchedString.clear(); | ||
43 | } | 48 | } | ||
44 | 49 | | |||
45 | const bool newSearch = m_searchedString.isEmpty(); | 50 | const bool newSearch = m_searchedString.isEmpty(); | ||
46 | 51 | | |||
47 | // Do not start a new search if the user pressed Space. Only add | 52 | // Do not start a new search if the user pressed Space. Only add | ||
48 | // it to the search string if a search is in progress already. | 53 | // it to the search string if a search is in progress already. | ||
49 | if (newSearch && keys == QLatin1String(" ")) { | 54 | if (newSearch && keys == QLatin1String(" ")) { | ||
50 | return; | 55 | return; | ||
51 | } | 56 | } | ||
52 | 57 | | |||
53 | if (!keys.isEmpty()) { | 58 | if (!keys.isEmpty()) { | ||
54 | m_searchedString.append(keys); | 59 | m_searchedString.append(keys); | ||
55 | 60 | | |||
56 | // Special case: | 61 | // Special case: | ||
57 | // If the same key is pressed repeatedly, the next item matching that key should be highlighted | 62 | // If the same key is pressed repeatedly, the next item matching that key should be highlighted | ||
58 | const QChar firstKey = m_searchedString.length() > 0 ? m_searchedString.at(0) : QChar(); | 63 | const QChar firstKey = m_searchedString.length() > 0 ? m_searchedString.at(0) : QChar(); | ||
59 | const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length(); | 64 | const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length(); | ||
60 | 65 | | |||
61 | // Searching for a matching item should start from the next item if either | 66 | // Searching for a matching item should start from the next item if either | ||
62 | // 1. a new search is started, or | 67 | // 1. a new search is started and a search has not been restarted or | ||
63 | // 2. a 'repeated key' search is done. | 68 | // 2. a 'repeated key' search is done. | ||
64 | const bool searchFromNextItem = newSearch || sameKey; | 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; | ||||
65 | 74 | | |||
66 | emit changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem); | 75 | emit changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem); | ||
67 | } | 76 | } | ||
68 | m_keyboardInputTime.start(); | 77 | m_keyboardInputTime.start(); | ||
69 | } | 78 | } | ||
70 | 79 | | |||
71 | void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds) | 80 | void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds) | ||
72 | { | 81 | { | ||
73 | m_timeout = milliseconds; | 82 | m_timeout = milliseconds; | ||
74 | } | 83 | } | ||
75 | 84 | | |||
76 | qint64 KItemListKeyboardSearchManager::timeout() const | 85 | qint64 KItemListKeyboardSearchManager::timeout() const | ||
77 | { | 86 | { | ||
78 | return m_timeout; | 87 | return m_timeout; | ||
79 | } | 88 | } | ||
80 | 89 | | |||
81 | void KItemListKeyboardSearchManager::cancelSearch() | 90 | void KItemListKeyboardSearchManager::cancelSearch() | ||
82 | { | 91 | { | ||
92 | m_isSearchRestarted = true; | ||||
83 | m_searchedString.clear(); | 93 | m_searchedString.clear(); | ||
84 | } | 94 | } | ||
85 | 95 | | |||
86 | void KItemListKeyboardSearchManager::slotCurrentChanged(int current, int previous) | 96 | void KItemListKeyboardSearchManager::slotCurrentChanged(int current, int previous) | ||
87 | { | 97 | { | ||
88 | Q_UNUSED(previous); | 98 | Q_UNUSED(previous); | ||
89 | 99 | | |||
90 | if (current < 0) { | 100 | if (current < 0) { | ||
91 | // The current item has been removed. We should cancel the search. | 101 | // The current item has been removed. We should cancel the search. | ||
92 | cancelSearch(); | 102 | cancelSearch(); | ||
93 | } | 103 | } | ||
94 | } | 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 | } |
This is an unrelated refactoring that should go in its own commit. Feel free to commit this single change without review.