File indexing completed on 2024-04-28 15:32:02

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2000 Alexander Neundorf <neundorf@kde.org>
0005     SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KEDITLISTWIDGET_H
0011 #define KEDITLISTWIDGET_H
0012 
0013 #include <kwidgetsaddons_export.h>
0014 
0015 #include <QWidget>
0016 #include <memory>
0017 
0018 class QLineEdit;
0019 class QComboBox;
0020 class QListView;
0021 class QPushButton;
0022 class QItemSelection;
0023 
0024 /**
0025  * @class KEditListWidget keditlistwidget.h KEditListWidget
0026  *
0027  * An editable listbox
0028  *
0029  * This class provides an editable listbox, this means
0030  * a listbox which is accompanied by a line edit to enter new
0031  * items into the listbox and pushbuttons to add and remove
0032  * items from the listbox and two buttons to move items up and down.
0033  *
0034  * \image html keditlistbox.png "KEditListWidget"
0035  *
0036  * @since 4.6
0037  */
0038 class KWIDGETSADDONS_EXPORT KEditListWidget : public QWidget
0039 {
0040     Q_OBJECT
0041 
0042     Q_PROPERTY(Buttons buttons READ buttons WRITE setButtons)
0043     Q_PROPERTY(QStringList items READ items WRITE setItems NOTIFY changed USER true)
0044     Q_PROPERTY(bool checkAtEntering READ checkAtEntering WRITE setCheckAtEntering)
0045 
0046 public:
0047     /**
0048      * Custom editor class
0049      */
0050     class KWIDGETSADDONS_EXPORT CustomEditor
0051     {
0052     public:
0053         CustomEditor();
0054         CustomEditor(QWidget *repWidget, QLineEdit *edit);
0055         CustomEditor(QComboBox *combo);
0056         virtual ~CustomEditor();
0057 
0058         void setRepresentationWidget(QWidget *repWidget);
0059         void setLineEdit(QLineEdit *edit);
0060 
0061         virtual QWidget *representationWidget() const;
0062         virtual QLineEdit *lineEdit() const;
0063 
0064     private:
0065         friend class KEditListWidgetCustomEditorPrivate;
0066         std::unique_ptr<class KEditListWidgetCustomEditorPrivate> const d;
0067 
0068         Q_DISABLE_COPY(CustomEditor)
0069     };
0070 
0071 public:
0072     /**
0073      * Enumeration of the buttons, the listbox offers. Specify them in the
0074      * constructor in the buttons parameter, or in setButtons.
0075      * @see Buttons
0076      */
0077     enum Button {
0078         Add = 0x0001,
0079         Remove = 0x0002,
0080         UpDown = 0x0004,
0081         All = Add | Remove | UpDown,
0082     };
0083 
0084     /**
0085      * Stores a combination of #Button values.
0086      */
0087     Q_DECLARE_FLAGS(Buttons, Button)
0088     Q_FLAG(Buttons)
0089 
0090     /**
0091      * Create an editable listbox.
0092      */
0093     explicit KEditListWidget(QWidget *parent = nullptr);
0094 
0095     /**
0096      * Constructor which allows to use a custom editing widget
0097      * instead of the standard QLineEdit widget. E.g. you can use a
0098      * KUrlRequester or a QComboBox as input widget. The custom
0099      * editor must consist of a lineedit and optionally another widget that
0100      * is used as representation. A QComboBox or a KUrlRequester have a
0101      * QLineEdit as child-widget for example, so the QComboBox is used as
0102      * the representation widget.
0103      *
0104      * @see KUrlRequester::customEditor(), setCustomEditor
0105      */
0106     KEditListWidget(const CustomEditor &customEditor, QWidget *parent = nullptr, bool checkAtEntering = false, Buttons buttons = All);
0107 
0108     ~KEditListWidget() override;
0109 
0110     /**
0111      * @returns a pointer to the embedded QListView.
0112      */
0113     QListView *listView() const;
0114     /**
0115      * @returns a pointer to the embedded QLineEdit.
0116      */
0117     QLineEdit *lineEdit() const;
0118     /**
0119      * @returns a pointer to the Add button
0120      */
0121     QPushButton *addButton() const;
0122     /**
0123      * @returns a pointer to the Remove button
0124      */
0125     QPushButton *removeButton() const;
0126     /**
0127      * @returns a pointer to the Up button
0128      */
0129     QPushButton *upButton() const;
0130     /**
0131      * @returns a pointer to the Down button
0132      */
0133     QPushButton *downButton() const;
0134 
0135     /**
0136      * @returns the count of elements in the list
0137      */
0138     int count() const;
0139 
0140     /**
0141      * Inserts a @p list of elements from the @p index element
0142      * If @p index is negative, the elements will be appended
0143      */
0144     void insertStringList(const QStringList &list, int index = -1);
0145 
0146     /**
0147      * Inserts a @p text element at the @p index position
0148      * If @p index is negative, the element will be appended
0149      */
0150     void insertItem(const QString &text, int index = -1);
0151 
0152     /**
0153      * Clears both the listbox and the line edit.
0154      */
0155     void clear();
0156 
0157     /**
0158      * @returns the text at the @p index position
0159      */
0160     QString text(int index) const;
0161 
0162     /**
0163      * @returns the currently selected item
0164      */
0165     int currentItem() const;
0166 
0167     /**
0168      * @returns the currently selected item's text
0169      */
0170     QString currentText() const;
0171 
0172     /**
0173      * @returns a list with the text of all items in the listbox
0174      */
0175     QStringList items() const;
0176 
0177     /**
0178      * Clears the listbox and sets the contents to @p items
0179      */
0180     void setItems(const QStringList &items);
0181 
0182     /**
0183      * @returns which buttons are visible
0184      */
0185     Buttons buttons() const;
0186 
0187     /**
0188      * Specifies which @p buttons are visible
0189      */
0190     void setButtons(Buttons buttons);
0191 
0192     /**
0193      * If @p check is true, after every character you type
0194      * in the line edit KEditListWidget will enable or disable
0195      * the Add-button, depending whether the current content of the
0196      * line edit is already in the listbox. Maybe this can become a
0197      * performance hit with large lists on slow machines.
0198      * If @p check is false,
0199      * it will be checked if you press the Add-button. It is not
0200      * possible to enter items twice into the listbox.
0201      * Default is false.
0202      */
0203     void setCheckAtEntering(bool check);
0204 
0205     /**
0206      * @returns true if check at entering is enabled.
0207      */
0208     bool checkAtEntering();
0209 
0210     /**
0211      * Allows to use a custom editing widget
0212      * instead of the standard QLineEdit widget. E.g. you can use a
0213      * KUrlRequester or a QComboBox as input widget. The custom
0214      * editor must consist of a lineedit and optionally another widget that
0215      * is used as representation. A QComboBox or a KUrlRequester have a
0216      * QLineEdit as child-widget for example, so the QComboBox is used as
0217      * the representation widget.
0218      */
0219     void setCustomEditor(const CustomEditor &editor);
0220 
0221     /**
0222      * Reimplemented for internal reasons. The API is not affected.
0223      */
0224     bool eventFilter(QObject *o, QEvent *e) override;
0225 
0226 Q_SIGNALS:
0227     void changed();
0228 
0229     /**
0230      * This signal is emitted when the user adds a new string to the list,
0231      * the parameter is the added string.
0232      */
0233     void added(const QString &text);
0234 
0235     /**
0236      * This signal is emitted when the user removes a string from the list,
0237      * the parameter is the removed string.
0238      */
0239     void removed(const QString &text);
0240 
0241 private Q_SLOTS:
0242     KWIDGETSADDONS_NO_EXPORT void moveItemUp();
0243     KWIDGETSADDONS_NO_EXPORT void moveItemDown();
0244     KWIDGETSADDONS_NO_EXPORT void addItem();
0245     KWIDGETSADDONS_NO_EXPORT void removeItem();
0246     KWIDGETSADDONS_NO_EXPORT void enableMoveButtons(const QModelIndex &, const QModelIndex &);
0247     KWIDGETSADDONS_NO_EXPORT void typedSomething(const QString &text);
0248     KWIDGETSADDONS_NO_EXPORT void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
0249 
0250 private:
0251     friend class KEditListWidgetPrivate;
0252     std::unique_ptr<class KEditListWidgetPrivate> const d;
0253 
0254     Q_DISABLE_COPY(KEditListWidget)
0255 };
0256 
0257 Q_DECLARE_OPERATORS_FOR_FLAGS(KEditListWidget::Buttons)
0258 
0259 #endif