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