File indexing completed on 2024-05-12 05:47:30

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com>
0003  *
0004  * Based on the Itemviews NG project from Trolltech Labs
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KITEMLISTKEYBOARDSEARCHMANAGER_H
0010 #define KITEMLISTKEYBOARDSEARCHMANAGER_H
0011 
0012 #include "dolphin_export.h"
0013 #include "kitemviews/kitemset.h"
0014 
0015 #include <QElapsedTimer>
0016 #include <QObject>
0017 #include <QString>
0018 
0019 /**
0020  * @brief Controls the keyboard searching ability for a KItemListController.
0021  *
0022  * @see KItemListController
0023  * @see KItemModelBase
0024  */
0025 class DOLPHIN_EXPORT KItemListKeyboardSearchManager : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit KItemListKeyboardSearchManager(QObject *parent = nullptr);
0031     ~KItemListKeyboardSearchManager() override;
0032 
0033     /**
0034      * Add \a keys to the text buffer used for searching.
0035      */
0036     void addKeys(const QString &keys);
0037 
0038     /**
0039      * Sets the delay after which the search is cancelled to \a milliseconds.
0040      * If the time interval between two calls of addKeys(const QString&) is
0041      * larger than this, the second call will start a new search, rather than
0042      * combining the keys received from both calls to a single search string.
0043      */
0044     void setTimeout(qint64 milliseconds);
0045     qint64 timeout() const;
0046 
0047     void cancelSearch();
0048 
0049     /**
0050      * @return \c true if search as you type is active, or \c false otherwise.
0051      */
0052     bool isSearchAsYouTypeActive() const;
0053 
0054 public Q_SLOTS:
0055 
0056     void slotCurrentChanged(int current, int previous);
0057     void slotSelectionChanged(const KItemSet &current, const KItemSet &previous);
0058 
0059 Q_SIGNALS:
0060     /**
0061      * Is emitted if the current item should be changed corresponding
0062      * to \a text.
0063      * @param searchFromNextItem If true start searching from item next to the
0064      *                           current item. Otherwise, search from the
0065      *                           current item.
0066      */
0067     // TODO: Think about getting rid of the bool parameter
0068     // (see https://doc.qt.io/archives/qq/qq13-apis.html#thebooleanparametertrap)
0069     void changeCurrentItem(const QString &string, bool searchFromNextItem);
0070 
0071 private:
0072     bool shouldClearSearchIfInputTimeReached();
0073 
0074     QString m_searchedString;
0075     bool m_isSearchRestarted;
0076     /** Measures the time since the last key press. */
0077     QElapsedTimer m_keyboardInputTime;
0078     /** Time in milliseconds in which a key press is considered as a continuation of the previous search input. */
0079     qint64 m_timeout;
0080 };
0081 
0082 #endif