]> cloud.milkyroute.net Git - dolphin.git/commitdiff
KItemListKeyboardSearchManager: make it possible to choose the timeout
authorFrank Reininghaus <frank78ac@googlemail.com>
Thu, 29 Mar 2012 17:56:53 +0000 (19:56 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Thu, 29 Mar 2012 18:05:49 +0000 (20:05 +0200)
It's now possible to change the timeout, after which any call of
KItemListKeyboardSearchManager::addKeys(const QString&) starts a new
search, from the default value of 5000 milliseconds. This is not used
in Dolphin at the moment, but it permits to reduce the timeout to a
small value in the unit test. Before this change, the unit test took
more than 5 seconds to complete.
(cherry picked from commit 82fc1b54bd01768f50aba7d328cdcde7de1483d7)

src/kitemviews/kitemlistkeyboardsearchmanager.cpp
src/kitemviews/kitemlistkeyboardsearchmanager_p.h
src/tests/kitemlistkeyboardsearchmanagertest.cpp

index 1ff60d09c82b77884ace5cbfe40aae6e2559cd03..f4dc1a54745b444de7638621177e18cbc98fffcb 100644 (file)
@@ -28,7 +28,8 @@
 #include <KDebug>
 
 KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) :
-    QObject(parent)
+    QObject(parent),
+    m_timeout(5000)
 {
     m_keyboardInputTime.invalidate();
 }
@@ -41,8 +42,7 @@ void KItemListKeyboardSearchManager::addKeys(const QString& keys)
 {
     const bool keyboardTimeWasValid = m_keyboardInputTime.isValid();
     const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart();
-    const qint64 timeout = 5000;
-    if (keyboardInputTimeElapsed > timeout || !keyboardTimeWasValid || keys.isEmpty()) {
+    if (keyboardInputTimeElapsed > m_timeout || !keyboardTimeWasValid || keys.isEmpty()) {
         m_searchedString.clear();
     }
 
@@ -65,3 +65,14 @@ void KItemListKeyboardSearchManager::addKeys(const QString& keys)
     }
     m_keyboardInputTime.start();
 }
+
+void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds)
+{
+    m_timeout = milliseconds;
+}
+
+qint64 KItemListKeyboardSearchManager::timeout() const
+{
+    return m_timeout;
+}
+
index 05de76a8caf9ac0660d18652f0b08811c9e0127a..d6a6686dbcfb5387c2d54822c275c8cc11318da3 100644 (file)
@@ -49,6 +49,15 @@ public:
      */
     void addKeys(const QString& keys);
 
+    /**
+     * Sets the delay after which the search is cancelled to \a milliseconds.
+     * If the time interval between two calls of addKeys(const QString&) is
+     * larger than this, the second call will start a new search, rather than
+     * combining the keys received from both calls to a single search string.
+     */
+    void setTimeout(qint64 milliseconds);
+    qint64 timeout() const;
+
 signals:
     /**
      * Is emitted if the current item should be changed corresponding
@@ -64,6 +73,7 @@ signals:
 private:
     QString m_searchedString;
     QElapsedTimer m_keyboardInputTime;
+    qint64 m_timeout;
 };
 
 #endif
index 14a09a31cc82b89737bbad61c1bd1695ed922cd8..aeca1f7798813bc998e98bb3e1b612c9225a67f6 100644 (file)
@@ -65,6 +65,10 @@ void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
 
 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
 {
+    // Set the timeout to a small value (the default is 5000 milliseconds)
+    // to save time when running this test.
+    m_keyboardSearchManager.setTimeout(100);
+
     QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
 
     m_keyboardSearchManager.addKeys("f");
@@ -75,9 +79,9 @@ void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
     QCOMPARE(spy.count(), 1);
     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
 
-    // If the delay between two key presses is larger than 5000 milliseconds,
+    // If the delay between two key presses is larger than the chosen timeout,
     // a new search is started. We add a small safety margin to avoid race conditions.
-    QTest::qWait(5000 + 10);
+    QTest::qWait(m_keyboardSearchManager.timeout() + 10);
 
     m_keyboardSearchManager.addKeys("l");
     QCOMPARE(spy.count(), 1);