File indexing completed on 2024-05-12 15:34:00

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2000 Carsten Pfeiffer <pfeiffer@kde.org>
0005     SPDX-FileCopyrightText: 2000 Stefan Schimanski <1Stein@gmx.de>
0006     SPDX-FileCopyrightText: 2000, 2001, 2002, 2003, 2004 Dawit Alemayehu <adawit@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #ifndef KCOMPLETIONBOX_H
0012 #define KCOMPLETIONBOX_H
0013 
0014 #include "kcompletion_export.h"
0015 #include <QListWidget>
0016 #include <memory>
0017 
0018 class KCompletionBoxPrivate;
0019 class QEvent;
0020 
0021 /**
0022  * @class KCompletionBox kcompletionbox.h KCompletionBox
0023  *
0024  * @short A helper widget for "completion-widgets" (KLineEdit, KComboBox))
0025  *
0026  * A little utility class for "completion-widgets", like KLineEdit or
0027  * KComboBox. KCompletionBox is a listbox, displayed as a rectangle without
0028  * any window decoration, usually directly under the lineedit or combobox.
0029  * It is filled with all possible matches for a completion, so the user
0030  * can select the one he wants.
0031  *
0032  * It is used when KCompletion::CompletionMode == CompletionPopup or CompletionPopupAuto.
0033  *
0034  * @author Carsten Pfeiffer <pfeiffer@kde.org>
0035  */
0036 class KCOMPLETION_EXPORT KCompletionBox : public QListWidget
0037 {
0038     Q_OBJECT
0039     Q_DECLARE_PRIVATE(KCompletionBox)
0040     Q_PROPERTY(bool isTabHandling READ isTabHandling WRITE setTabHandling)
0041     Q_PROPERTY(QString cancelledText READ cancelledText WRITE setCancelledText)
0042     Q_PROPERTY(bool activateOnSelect READ activateOnSelect WRITE setActivateOnSelect)
0043 
0044 public:
0045     /**
0046      * Constructs a KCompletionBox.
0047      *
0048      * The parent widget is used to give the focus back when pressing the
0049      * up-button on the very first item.
0050      */
0051     explicit KCompletionBox(QWidget *parent = nullptr);
0052 
0053     /**
0054      * Destroys the box
0055      */
0056     ~KCompletionBox() override;
0057 
0058     QSize sizeHint() const override;
0059 
0060     /**
0061      * @returns true if selecting an item results in the emission of the selected() signal.
0062      */
0063     bool activateOnSelect() const;
0064 
0065     /**
0066      * Returns a list of all items currently in the box.
0067      */
0068     QStringList items() const;
0069 
0070     /**
0071      * @returns true if this widget is handling Tab-key events to traverse the
0072      * items in the dropdown list, otherwise false.
0073      *
0074      * Default is true.
0075      *
0076      * @see setTabHandling
0077      */
0078     bool isTabHandling() const;
0079 
0080     /**
0081      * @returns the text set via setCancelledText() or QString().
0082      */
0083     QString cancelledText() const;
0084 
0085 public Q_SLOTS:
0086     /**
0087      * Inserts @p items into the box. Does not clear the items before.
0088      * @p index determines at which position @p items will be inserted.
0089      * (defaults to appending them at the end)
0090      */
0091     void insertItems(const QStringList &items, int index = -1);
0092 
0093     /**
0094      * Clears the box and inserts @p items.
0095      */
0096     void setItems(const QStringList &items);
0097 
0098     /**
0099      * Adjusts the size of the box to fit the width of the parent given in the
0100      * constructor and pops it up at the most appropriate place, relative to
0101      * the parent.
0102      *
0103      * Depending on the screensize and the position of the parent, this may
0104      * be a different place, however the default is to pop it up and the
0105      * lower left corner of the parent.
0106      *
0107      * Make sure to hide() the box when appropriate.
0108      */
0109     virtual void popup();
0110 
0111     /**
0112      * Makes this widget (when visible) capture Tab-key events to traverse the
0113      * items in the dropdown list (Tab goes down, Shift+Tab goes up).
0114      *
0115      * On by default, but should be turned off when used in combination with KUrlCompletion.
0116      * When off, KLineEdit handles Tab itself, making it select the current item from the completion box,
0117      * which is particularly useful when using KUrlCompletion.
0118      *
0119      * @see isTabHandling
0120      */
0121     void setTabHandling(bool enable);
0122 
0123     /**
0124      * Sets the text to be emitted if the user chooses not to
0125      * pick from the available matches.
0126      *
0127      * If the cancelled text is not set through this function, the
0128      * userCancelled signal will not be emitted.
0129      *
0130      * @see userCancelled( const QString& )
0131      * @param text the text to be emitted if the user cancels this box
0132      */
0133     void setCancelledText(const QString &text);
0134 
0135     /**
0136      * Set whether or not the selected signal should be emitted when an
0137      * item is selected. By default the selected() signal is emitted.
0138      *
0139      * @param doEmit false if the signal should not be emitted.
0140      */
0141     void setActivateOnSelect(bool doEmit);
0142 
0143     /**
0144      * Moves the selection one line down or select the first item if nothing is selected yet.
0145      */
0146     void down();
0147 
0148     /**
0149      * Moves the selection one line up or select the first item if nothing is selected yet.
0150      */
0151     void up();
0152 
0153     /**
0154      * Moves the selection one page down.
0155      */
0156     void pageDown();
0157 
0158     /**
0159      * Moves the selection one page up.
0160      */
0161     void pageUp();
0162 
0163     /**
0164      * Moves the selection up to the first item.
0165      */
0166     void home();
0167 
0168     /**
0169      * Moves the selection down to the last item.
0170      */
0171     void end();
0172 
0173     /**
0174      * Reimplemented for internal reasons. API is unaffected.
0175      * Call it only if you really need it (i.e. the widget was hidden before) to have better performance.
0176      */
0177     void setVisible(bool visible) override;
0178 
0179 Q_SIGNALS:
0180 #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(5, 81)
0181     /**
0182      * Emitted when an item is selected, @p text is the text of the selected item.
0183      *
0184      * @deprecated since 5.81, use the KCompletionBox::textActivated(const QString &) signal instead
0185      */
0186     KCOMPLETION_DEPRECATED_VERSION(5, 81, "Use the KCompletionBox::textActivated(const QString &) signal instead")
0187     void activated(const QString &text); // clazy:exclude=overloaded-signal
0188 #endif
0189 
0190     /**
0191      * Emitted when an item is selected, @p text is the text of the selected item.
0192      *
0193      * @since 5.81
0194      */
0195     void textActivated(const QString &text);
0196 
0197     /**
0198      * Emitted whenever the user chooses to ignore the available
0199      * selections and closes this box.
0200      */
0201     void userCancelled(const QString &);
0202 
0203 protected:
0204     /**
0205      * This calculates the size of the dropdown and the relative position of the top
0206      * left corner with respect to the parent widget. This matches the geometry and position
0207      * normally used by K/QComboBox when used with one.
0208      */
0209     QRect calculateGeometry() const;
0210 
0211 #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(5, 0)
0212     /**
0213      * @deprecated since 5.0, use resizeAndReposition instead.
0214      */
0215     KCOMPLETION_DEPRECATED_VERSION(5, 0, "Use KCompletionBox::resizeAndReposition()")
0216     void sizeAndPosition()
0217     {
0218         resizeAndReposition();
0219     }
0220 #endif
0221 
0222     /**
0223      * This properly resizes and repositions the listbox.
0224      *
0225      * @since 5.0
0226      */
0227     void resizeAndReposition();
0228 
0229     /**
0230      * Reimplemented from QListWidget to get events from the viewport (to hide
0231      * this widget on mouse-click, Escape-presses, etc.
0232      */
0233     bool eventFilter(QObject *, QEvent *) override;
0234 
0235     /**
0236      * The preferred global coordinate at which the completion box's top left corner
0237      * should be positioned.
0238      */
0239     virtual QPoint globalPositionHint() const;
0240 
0241 protected Q_SLOTS:
0242     /**
0243      * Called when an item is activated. Emits KCompletionBox::textActivated(const QString &) with the item text.
0244      *
0245      * @note For releases <= 5.81, this slot emitted KCompletionBox::activated(const QString &) with the item text.
0246      */
0247     virtual void slotActivated(QListWidgetItem *);
0248 
0249 private:
0250     std::unique_ptr<KCompletionBoxPrivate> const d_ptr;
0251 };
0252 
0253 #endif // KCOMPLETIONBOX_H