1 /***************************************************************************
2 * Copyright (C) 2011 by Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #include "kitemlistkeyboardsearchmanager.h"
25 #include <QApplication>
26 #include <QElapsedTimer>
28 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject
* parent
) :
32 m_keyboardInputTime
.invalidate();
35 KItemListKeyboardSearchManager::~KItemListKeyboardSearchManager()
39 void KItemListKeyboardSearchManager::addKeys(const QString
& keys
)
41 const bool keyboardTimeWasValid
= m_keyboardInputTime
.isValid();
42 const qint64 keyboardInputTimeElapsed
= m_keyboardInputTime
.restart();
43 if (keyboardInputTimeElapsed
> m_timeout
|| !keyboardTimeWasValid
) {
44 m_searchedString
.clear();
47 const bool newSearch
= m_searchedString
.isEmpty();
49 if (!keys
.isEmpty()) {
50 m_searchedString
.append(keys
);
53 // If the same key is pressed repeatedly, the next item matching that key should be highlighted
54 const QChar firstKey
= m_searchedString
.length() > 0 ? m_searchedString
.at(0) : QChar();
55 const bool sameKey
= m_searchedString
.length() > 1 && m_searchedString
.count(firstKey
) == m_searchedString
.length();
57 // Searching for a matching item should start from the next item if either
58 // 1. a new search is started, or
59 // 2. a 'repeated key' search is done.
60 const bool searchFromNextItem
= newSearch
|| sameKey
;
62 emit
changeCurrentItem(sameKey
? firstKey
: m_searchedString
, searchFromNextItem
);
64 m_keyboardInputTime
.start();
67 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds
)
69 m_timeout
= milliseconds
;
72 qint64
KItemListKeyboardSearchManager::timeout() const
77 void KItemListKeyboardSearchManager::cancelSearch()
79 m_searchedString
.clear();
82 void KItemListKeyboardSearchManager::slotCurrentChanged(int current
, int previous
)
87 // The current item has been removed. We should cancel the search.