2 * SPDX-FileCopyrightText: 2011 Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com>
4 * Based on the Itemviews NG project from Trolltech Labs
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "kitemlistkeyboardsearchmanager.h"
11 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject
*parent
)
15 m_keyboardInputTime
.invalidate();
18 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
22 bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
24 const bool keyboardTimeWasValid
= m_keyboardInputTime
.isValid();
25 const qint64 keyboardInputTimeElapsed
= m_keyboardInputTime
.restart();
26 return (keyboardInputTimeElapsed
> m_timeout
) || !keyboardTimeWasValid
;
29 bool KItemListKeyboardSearchManager::isSearchAsYouTypeActive() const
31 return !m_searchedString
.isEmpty() && !m_keyboardInputTime
.hasExpired(m_timeout
);
34 void KItemListKeyboardSearchManager::addKeys(const QString
&keys
)
36 if (shouldClearSearchIfInputTimeReached()) {
37 m_searchedString
.clear();
40 const bool newSearch
= m_searchedString
.isEmpty();
42 // Do not start a new search if the user pressed Space. Only add
43 // it to the search string if a search is in progress already.
44 if (newSearch
&& keys
== QLatin1Char(' ')) {
48 if (!keys
.isEmpty()) {
49 m_searchedString
.append(keys
);
52 // If the same key is pressed repeatedly, the next item matching that key should be highlighted
53 const QChar firstKey
= m_searchedString
.length() > 0 ? m_searchedString
.at(0) : QChar();
54 const bool sameKey
= m_searchedString
.length() > 1 && m_searchedString
.count(firstKey
) == m_searchedString
.length();
56 // Searching for a matching item should start from the next item if either
57 // 1. a new search is started, or
58 // 2. a 'repeated key' search is done.
59 const bool searchFromNextItem
= newSearch
|| sameKey
;
61 Q_EMIT
changeCurrentItem(sameKey
? firstKey
: m_searchedString
, searchFromNextItem
);
63 m_keyboardInputTime
.start();
66 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds
)
68 m_timeout
= milliseconds
;
71 qint64
KItemListKeyboardSearchManager::timeout() const
76 void KItemListKeyboardSearchManager::cancelSearch()
78 m_searchedString
.clear();
81 void KItemListKeyboardSearchManager::slotCurrentChanged(int current
, int previous
)
86 // The current item has been removed. We should cancel the search.
91 void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet
¤t
, const KItemSet
&previous
)
93 if (!previous
.isEmpty() && current
.isEmpty() && previous
.count() > 0 && current
.count() == 0) {
94 // The selection has been emptied. We should cancel the search.
99 #include "moc_kitemlistkeyboardsearchmanager.cpp"