1 /***************************************************************************
2 * Copyright (C) 2011 by Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs *
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. *
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. *
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 ***************************************************************************/
22 #include "kitemlistkeyboardsearchmanager.h"
24 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject
* parent
) :
26 m_isSearchRestarted(false),
29 m_keyboardInputTime
.invalidate();
32 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
36 bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
38 const bool keyboardTimeWasValid
= m_keyboardInputTime
.isValid();
39 const qint64 keyboardInputTimeElapsed
= m_keyboardInputTime
.restart();
40 return (keyboardInputTimeElapsed
> m_timeout
) || !keyboardTimeWasValid
;
43 void KItemListKeyboardSearchManager::addKeys(const QString
& keys
)
45 if (shouldClearSearchIfInputTimeReached()) {
46 m_searchedString
.clear();
49 const bool newSearch
= m_searchedString
.isEmpty();
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(' ')) {
57 if (!keys
.isEmpty()) {
58 m_searchedString
.append(keys
);
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();
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
;
70 // to remember not to searchFromNextItem if selection was deselected
71 // loosing keyboard search context basically
72 m_isSearchRestarted
= false;
74 emit
changeCurrentItem(sameKey
? firstKey
: m_searchedString
, searchFromNextItem
);
76 m_keyboardInputTime
.start();
79 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds
)
81 m_timeout
= milliseconds
;
84 qint64
KItemListKeyboardSearchManager::timeout() const
89 void KItemListKeyboardSearchManager::cancelSearch()
91 m_isSearchRestarted
= true;
92 m_searchedString
.clear();
95 void KItemListKeyboardSearchManager::slotCurrentChanged(int current
, int previous
)
100 // The current item has been removed. We should cancel the search.
105 void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
)
107 if (!previous
.isEmpty() && current
.isEmpty() && previous
.count() > 0 && current
.count() == 0) {
108 // The selection has been emptied. We should cancel the search.