File indexing completed on 2024-04-28 03:53:09

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2000, 2001 Dawit Alemayehu <adawit@kde.org>
0005     SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #ifndef KHistoryComboBoxBOX_H
0011 #define KHistoryComboBoxBOX_H
0012 
0013 #include <kcombobox.h>
0014 #include <kcompletion_export.h>
0015 
0016 #include <functional>
0017 
0018 class KHistoryComboBoxPrivate;
0019 
0020 /**
0021  * @class KHistoryComboBox khistorycombobox.h KHistoryComboBox
0022  *
0023  * @short A combobox for offering a history and completion
0024  *
0025  * A combobox which implements a history like a unix shell. You can navigate
0026  * through all the items by using the Up or Down arrows (configurable of
0027  * course). Additionally, weighted completion is available. So you should
0028  * load and save the completion list to preserve the weighting between
0029  * sessions.
0030  *
0031  * KHistoryComboBox obeys the HISTCONTROL environment variable to determine
0032  * whether duplicates in the history should be tolerated in
0033  * addToHistory() or not. During construction of KHistoryComboBox,
0034  * duplicates will be disabled when HISTCONTROL is set to "ignoredups" or
0035  * "ignoreboth". Otherwise, duplicates are enabled by default.
0036  *
0037  * \image html khistorycombobox.png "KHistoryComboBox widget"
0038  *
0039  * @author Carsten Pfeiffer <pfeiffer@kde.org>
0040  */
0041 class KCOMPLETION_EXPORT KHistoryComboBox : public KComboBox
0042 {
0043     Q_OBJECT
0044 
0045     Q_PROPERTY(QStringList historyItems READ historyItems WRITE setHistoryItems)
0046 
0047 public:
0048     /**
0049      * Constructs a "read-write" combobox. A read-only history combobox
0050      * doesn't make much sense, so it is only available as read-write.
0051      * Completion will be used automatically for the items in the combo.
0052      *
0053      * The insertion-policy is set to NoInsert, you have to add the items
0054      * yourself via the slot addToHistory. If you want every item added,
0055      * use
0056      *
0057      * \code
0058      * connect( combo, SIGNAL( activated( const QString& )),
0059      *          combo, SLOT( addToHistory( const QString& )));
0060      * \endcode
0061      *
0062      * Use QComboBox::setMaxCount() to limit the history.
0063      *
0064      * @p parent the parent object of this widget.
0065      */
0066     explicit KHistoryComboBox(QWidget *parent = nullptr);
0067 
0068     /**
0069      * Same as the previous constructor, but additionally has the option
0070      * to specify whether you want to let KHistoryComboBox handle completion
0071      * or not. If set to @c true, KHistoryComboBox will sync the completion to the
0072      * contents of the combobox.
0073      */
0074     explicit KHistoryComboBox(bool useCompletion, QWidget *parent = nullptr);
0075 
0076     /**
0077      * Destructs the combo and the completion-object
0078      */
0079     ~KHistoryComboBox() override;
0080 
0081     /**
0082      * Inserts @p items into the combobox. @p items might get
0083      * truncated if it is longer than maxCount()
0084      *
0085      * @see historyItems
0086      */
0087     void setHistoryItems(const QStringList &items);
0088 
0089     /**
0090      * Inserts @p items into the combobox. @p items might get
0091      * truncated if it is longer than maxCount()
0092      *
0093      * Set @c setCompletionList to true, if you don't have a list of
0094      * completions. This tells KHistoryComboBox to use all the items for the
0095      * completion object as well.
0096      * You won't have the benefit of weighted completion though, so normally
0097      * you should do something like
0098      * \code
0099      * KConfigGroup config(KSharedConfig::openConfig(), "somegroup");
0100      *
0101      * // load the history and completion list after creating the history combo
0102      * QStringList list;
0103      * list = config.readEntry("Completion list", QStringList());
0104      * combo->completionObject()->setItems(list);
0105      * list = config.readEntry("History list", QStringList());
0106      * combo->setHistoryItems(list);
0107      *
0108      * [...]
0109      *
0110      * // save the history and completion list when the history combo is
0111      * // destroyed
0112      * QStringList list;
0113      * KConfigGroup config(KSharedConfig::openConfig(), "somegroup");
0114      * list = combo->completionObject()->items();
0115      * config.writeEntry("Completion list", list);
0116      * list = combo->historyItems();
0117      * config.writeEntry("History list", list);
0118      * \endcode
0119      *
0120      * Be sure to use different names for saving with KConfig if you have more
0121      * than one KHistoryComboBox.
0122      *
0123      * @note When @c setCompletionList is true, the items are inserted into the
0124      * KCompletion object with mode KCompletion::Insertion and the mode is set
0125      * to KCompletion::Weighted afterwards.
0126      *
0127      * @see historyItems
0128      * @see KComboBox::completionObject
0129      * @see KCompletion::setItems
0130      * @see KCompletion::items
0131      */
0132     void setHistoryItems(const QStringList &items, bool setCompletionList);
0133 
0134     /**
0135      * Returns the list of history items. Empty, when this is not a read-write
0136      * combobox.
0137      *
0138      * @see setHistoryItems
0139      */
0140     QStringList historyItems() const;
0141 
0142     /**
0143      * Removes all items named @p item.
0144      *
0145      * @return @c true if at least one item was removed.
0146      *
0147      * @see addToHistory
0148      */
0149     bool removeFromHistory(const QString &item);
0150 
0151     /**
0152      * Sets an icon provider, so that items in the combobox can have an icon.
0153      * The provider is a function that takes a QString and returns a QIcon
0154      * @since 5.66
0155      */
0156     void setIconProvider(std::function<QIcon(const QString &)> providerFunction);
0157 
0158     using QComboBox::insertItems;
0159 
0160 public Q_SLOTS:
0161     /**
0162      * Adds an item to the end of the history list and to the completion list.
0163      * If maxCount() is reached, the first item of the list will be
0164      * removed.
0165      *
0166      * If the last inserted item is the same as @p item, it will not be
0167      * inserted again.
0168      *
0169      * If duplicatesEnabled() is false, any equal existing item will be
0170      * removed before @p item is added.
0171      *
0172      * @note By using this method and not the Q and KComboBox insertItem()
0173      * methods, you make sure that the combobox stays in sync with the
0174      * completion. It would be annoying if completion would give an item
0175      * not in the combobox, and vice versa.
0176      *
0177      * @see removeFromHistory
0178      * @see QComboBox::setDuplicatesEnabled
0179      */
0180     void addToHistory(const QString &item);
0181 
0182     /**
0183      * Clears the history and the completion list.
0184      */
0185     void clearHistory();
0186 
0187     /**
0188      * Resets the current position of the up/down history. Call this
0189      * when you manually call setCurrentItem() or clearEdit().
0190      */
0191     void reset();
0192 
0193 Q_SIGNALS:
0194     /**
0195      * Emitted when the history was cleared by the entry in the popup menu.
0196      */
0197     void cleared();
0198 
0199 protected:
0200     /**
0201      * Handling key-events, the shortcuts to rotate the items.
0202      */
0203     void keyPressEvent(QKeyEvent *) override;
0204 
0205     /**
0206      * Handling wheel-events, to rotate the items.
0207      */
0208     void wheelEvent(QWheelEvent *ev) override;
0209 
0210     /**
0211      * Inserts @p items into the combo, honoring setIconProvider()
0212      * Does not update the completionObject.
0213      *
0214      * @note duplicatesEnabled() is not honored here.
0215      *
0216      * Called from setHistoryItems()
0217      */
0218     void insertItems(const QStringList &items);
0219 
0220     /**
0221      * @returns if we can modify the completion object or not.
0222      */
0223     bool useCompletion() const;
0224 
0225 private:
0226     Q_DECLARE_PRIVATE(KHistoryComboBox)
0227 
0228     Q_DISABLE_COPY(KHistoryComboBox)
0229 };
0230 
0231 #endif