File indexing completed on 2024-06-23 05:16:27

0001 /*
0002   This file is part of libkdepim.
0003 
0004   SPDX-FileCopyrightText: 2008 Thomas Thrainer <tom_t@gmx.at>
0005   SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0008 
0009 */
0010 
0011 #pragma once
0012 
0013 #include "kdepim_export.h"
0014 
0015 #include <QComboBox>
0016 #include <QModelIndex>
0017 
0018 namespace KPIM
0019 {
0020 /**
0021  * A combobox that shows its items in such a way that they can be checked in the
0022  * drop menu. It provides methods to set the default text when no items are selected
0023  * and the separator that is used to show the items that are selected in the line
0024  * edit.
0025  */
0026 class KDEPIM_EXPORT KCheckComboBox : public QComboBox
0027 {
0028     Q_OBJECT
0029 
0030     Q_PROPERTY(QString separator READ separator WRITE setSeparator)
0031     Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
0032     Q_PROPERTY(bool squeezeText READ squeezeText WRITE setSqueezeText)
0033     Q_PROPERTY(QStringList checkedItems READ checkedItems WRITE setCheckedItems)
0034 
0035 public:
0036     /**
0037      * Creates a new checkable combobox.
0038      *
0039      * @param parent The parent widget.
0040      */
0041     explicit KCheckComboBox(QWidget *parent = nullptr);
0042 
0043     /**
0044      * Destroys the time zone combobox.
0045      */
0046     ~KCheckComboBox() override;
0047 
0048     /**
0049      * Hides the popup list if it is currently shown.
0050      */
0051     void hidePopup() override;
0052 
0053     /**
0054      * Returns the default text that is shown when no items are selected.
0055      */
0056     [[nodiscard]] QString defaultText() const;
0057 
0058     /**
0059      * Sets the default text that is shown when no items are selected.
0060      *
0061      * @param text The new default text
0062      */
0063     void setDefaultText(const QString &text);
0064 
0065     /**
0066      * Returns whether the default text is always shown, even if there are
0067      * no checked items.
0068      */
0069     bool alwaysShowDefaultText() const;
0070 
0071     /**
0072      * Sets if the default text should always be shown even if there are
0073      * no checked items.
0074      *
0075      * Default is false.
0076      */
0077     void setAlwaysShowDefaultText(bool always);
0078 
0079     /**
0080      * Returns whether or not the text will be squeezed to fit in the combo's line
0081      * edit. This property is false by default.
0082      *
0083      * @see KSqueezedTextLabel
0084      */
0085     [[nodiscard]] bool squeezeText() const;
0086 
0087     /**
0088      * Sets whether or not the text must be squeezed.
0089      *
0090      * @param squeeze The new squeeze status
0091      */
0092     void setSqueezeText(bool squeeze);
0093 
0094     /**
0095      * Return whether or not the item at @param index is enabled, i.e. if the
0096      * user can (un)check the item.
0097      */
0098     [[nodiscard]] bool itemEnabled(int index);
0099 
0100     /**
0101      * Set the item at @param index to @param enabled, i.e. if the
0102      * user can (un)check the item.
0103      */
0104     void setItemEnabled(int index, bool enabled = true);
0105 
0106     /**
0107      * Returns the check state of item at given index.
0108      *
0109      * @param index The index for which to return the check state.
0110      */
0111     [[nodiscard]] Qt::CheckState itemCheckState(int index) const;
0112 
0113     /**
0114      * Changes the check state of the given index to the given state.
0115      *
0116      * @param index The index of which the state needs to be changed
0117      * @param state The new state
0118      */
0119     void setItemCheckState(int index, Qt::CheckState state);
0120 
0121     /**
0122      * Returns the current separator used to separate the selected items in the
0123      * line edit of the combo box.
0124      */
0125     [[nodiscard]] QString separator() const;
0126 
0127     /**
0128      * Sets the separator used to separate items in the line edit.
0129      *
0130      * @param separator The new separator
0131      */
0132     void setSeparator(const QString &separator);
0133 
0134     /**
0135      * Returns The currently selected items.
0136      * @param role The role the returned values belong to.
0137      */
0138     [[nodiscard]] QStringList checkedItems(int role = Qt::DisplayRole) const;
0139 
0140 public Q_SLOTS:
0141     /**
0142      * Sets the currently selected items. Items that are not found in the model
0143      * are silently ignored.
0144      *
0145      * @param items The items that will be set to checked.
0146      * @param role The role @p items belong to.
0147      */
0148     void setCheckedItems(const QStringList &items, int role = Qt::DisplayRole);
0149 
0150 Q_SIGNALS:
0151     /**
0152      * Signal to notify listeners that the current selections has changed.
0153      *
0154      * @param items The new selection.
0155      */
0156     void checkedItemsChanged(const QStringList &items);
0157 
0158 protected:
0159     bool eventFilter(QObject *receiver, QEvent *event) override;
0160     void keyPressEvent(QKeyEvent *event) override;
0161     void resizeEvent(QResizeEvent *event) override;
0162 #ifndef QT_NO_WHEELEVENT
0163     void wheelEvent(QWheelEvent *event) override;
0164 #endif
0165 
0166 private:
0167     //@cond PRIVATE
0168     class KCheckComboBoxPrivate;
0169     std::unique_ptr<KCheckComboBoxPrivate> const d;
0170     //@endcond
0171 };
0172 }