]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[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_timeout(1000)
14 {
15 m_keyboardInputTime.invalidate();
16 }
17
18 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
19 {
20 }
21
22 bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
23 {
24 const bool keyboardTimeWasValid = m_keyboardInputTime.isValid();
25 const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart();
26 return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid;
27 }
28
29 bool KItemListKeyboardSearchManager::isSearchAsYouTypeActive() const
30 {
31 return !m_searchedString.isEmpty() && !m_keyboardInputTime.hasExpired(m_timeout);
32 }
33
34 void KItemListKeyboardSearchManager::addKeys(const QString &keys)
35 {
36 if (shouldClearSearchIfInputTimeReached()) {
37 m_searchedString.clear();
38 }
39
40 const bool newSearch = m_searchedString.isEmpty();
41
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(' ')) {
45 return;
46 }
47
48 if (!keys.isEmpty()) {
49 m_searchedString.append(keys);
50
51 // Special case:
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();
55
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;
60
61 Q_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_searchedString.clear();
79 }
80
81 void KItemListKeyboardSearchManager::slotCurrentChanged(int current, int previous)
82 {
83 Q_UNUSED(previous)
84
85 if (current < 0) {
86 // The current item has been removed. We should cancel the search.
87 cancelSearch();
88 }
89 }
90
91 void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet &current, const KItemSet &previous)
92 {
93 if (!previous.isEmpty() && current.isEmpty() && previous.count() > 0 && current.count() == 0) {
94 // The selection has been emptied. We should cancel the search.
95 cancelSearch();
96 }
97 }
98
99 #include "moc_kitemlistkeyboardsearchmanager.cpp"