]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp
Output of licensedigger + manual cleanup afterwards.
[dolphin.git] / src / kitemviews / private / kitemlistkeyboardsearchmanager.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "kitemlistkeyboardsearchmanager.h"
10
11 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) :
12 QObject(parent),
13 m_isSearchRestarted(false),
14 m_timeout(1000)
15 {
16 m_keyboardInputTime.invalidate();
17 }
18
19 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
20 {
21 }
22
23 bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
24 {
25 const bool keyboardTimeWasValid = m_keyboardInputTime.isValid();
26 const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart();
27 return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid;
28 }
29
30 void KItemListKeyboardSearchManager::addKeys(const QString& keys)
31 {
32 if (shouldClearSearchIfInputTimeReached()) {
33 m_searchedString.clear();
34 }
35
36 const bool newSearch = m_searchedString.isEmpty();
37
38 // Do not start a new search if the user pressed Space. Only add
39 // it to the search string if a search is in progress already.
40 if (newSearch && keys == QLatin1Char(' ')) {
41 return;
42 }
43
44 if (!keys.isEmpty()) {
45 m_searchedString.append(keys);
46
47 // Special case:
48 // If the same key is pressed repeatedly, the next item matching that key should be highlighted
49 const QChar firstKey = m_searchedString.length() > 0 ? m_searchedString.at(0) : QChar();
50 const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length();
51
52 // Searching for a matching item should start from the next item if either
53 // 1. a new search is started and a search has not been restarted or
54 // 2. a 'repeated key' search is done.
55 const bool searchFromNextItem = (!m_isSearchRestarted && newSearch) || sameKey;
56
57 // to remember not to searchFromNextItem if selection was deselected
58 // loosing keyboard search context basically
59 m_isSearchRestarted = false;
60
61 emit changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem);
62 }
63 m_keyboardInputTime.start();
64 }
65
66 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds)
67 {
68 m_timeout = milliseconds;
69 }
70
71 qint64 KItemListKeyboardSearchManager::timeout() const
72 {
73 return m_timeout;
74 }
75
76 void KItemListKeyboardSearchManager::cancelSearch()
77 {
78 m_isSearchRestarted = true;
79 m_searchedString.clear();
80 }
81
82 void KItemListKeyboardSearchManager::slotCurrentChanged(int current, int previous)
83 {
84 Q_UNUSED(previous)
85
86 if (current < 0) {
87 // The current item has been removed. We should cancel the search.
88 cancelSearch();
89 }
90 }
91
92 void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet& current, const KItemSet& previous)
93 {
94 if (!previous.isEmpty() && current.isEmpty() && previous.count() > 0 && current.count() == 0) {
95 // The selection has been emptied. We should cancel the search.
96 cancelSearch();
97 }
98 }