File indexing completed on 2025-04-27 03:58:21

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-08-21
0007  * Description : a combo box with a width not depending of text
0008  *               content size
0009  *
0010  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText:      2008 by Andi Clemens <andi dot clemens at googlemail dot com>
0012  * SPDX-FileCopyrightText:      2005 by Tom Albers <tomalbers at kde dot nl>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #ifndef DIGIKAM_SQUEEZED_COMBOBOX_H
0019 #define DIGIKAM_SQUEEZED_COMBOBOX_H
0020 
0021 // Qt includes
0022 
0023 #include <QComboBox>
0024 
0025 // Local includes
0026 
0027 #include "digikam_export.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 /**
0033  * @class SqueezedComboBox
0034  *
0035  * This widget is a QComboBox, but then a little bit
0036  * different. It only shows the right part of the items
0037  * depending on de size of the widget. When it is not
0038  * possible to show the complete item, it will be shortened
0039  * and "..." will be prepended.
0040  */
0041 class DIGIKAM_EXPORT SqueezedComboBox : public QComboBox
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046 
0047     /**
0048      * Constructor
0049      * @param parent the parent widget
0050      * @param name the name to give to the widget
0051      */
0052     explicit SqueezedComboBox(QWidget* const parent = nullptr, const char* name = nullptr);
0053 
0054     /**
0055      * destructor
0056      */
0057     ~SqueezedComboBox()                                                    override;
0058 
0059     /**
0060      *
0061      * Returns true if the combobox contains the original (not-squeezed)
0062      * version of text.
0063      * @param text the original (not-squeezed) text to check for
0064      */
0065     bool contains(const QString& text)                               const;
0066 
0067     /**
0068      * Returns the index of the combobox if found the original (not-squeezed)
0069      * version of text.
0070      * @param text the original (not-squeezed) text to find for
0071      * @param cs case sensitive or case insensitive search
0072      */
0073     int findOriginalText(const QString& text,
0074                          Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0075 
0076     /**
0077      * This inserts a item to the list. See QComboBox::insertItem()
0078      * for details. Please do not use QComboBox::insertItem() to this
0079      * widget, as that will fail.
0080      * @param newItem the original (long version) of the item which needs
0081      *                to be added to the combobox
0082      * @param index the position in the widget.
0083      * @param userData custom meta-data assigned to new item.
0084      */
0085     void insertSqueezedItem(const QString& newItem, int index,
0086                             const QVariant& userData = QVariant());
0087 
0088     /**
0089      * This inserts items to the list. See QComboBox::insertItems()
0090      * for details. Please do not use QComboBox:: insertItems() to this
0091      * widget, as that will fail.
0092      * @param newItems the originals (long version) of the items which needs
0093      *                 to be added to the combobox
0094      * @param index the position in the widget.
0095      */
0096     void insertSqueezedList(const QStringList& newItems, int index);
0097 
0098     /**
0099      * Append an item.
0100      * @param newItem the original (long version) of the item which needs
0101      *                to be added to the combobox
0102      * @param userData custom meta-data assigned to new item.
0103      */
0104     void addSqueezedItem(const QString& newItem,
0105                          const QVariant& userData = QVariant());
0106 
0107     /**
0108      * Set the current item to the one matching the given text.
0109      *
0110      * @param itemText the original (long version) of the item text
0111      */
0112     void setCurrent(const QString& itemText);
0113 
0114     /**
0115      * This method returns the full text (not squeezed) of the currently
0116      * highlighted item.
0117      * @return full text of the highlighted item
0118      */
0119     QString itemHighlighted()                                        const;
0120 
0121     /**
0122      * This method returns the full text (not squeezed) for the index.
0123      * @param index the position in the widget.
0124      * @return full text of the item
0125      */
0126     QString item(int index)                                          const;
0127 
0128     /**
0129      * Sets the sizeHint() of this widget.
0130      */
0131     QSize sizeHint()                                                 const override;
0132 
0133 Q_SIGNALS:
0134 
0135     void signalItemActivated(const QString&);
0136 
0137 private Q_SLOTS:
0138 
0139     void slotTimeOut();
0140     void slotUpdateToolTip(int index);
0141 
0142 private:
0143 
0144     void    resizeEvent(QResizeEvent*)                                     override;
0145     QString squeezeText(const QString& original)                     const;
0146 
0147     /// Prevent these from being used.
0148     QString currentText()                                            const;
0149     void    setCurrentText(const QString& itemText);
0150     void    insertItem(const QString& text);
0151     void    insertItem(qint32 index, const QString& text);
0152     void    insertItem(int index,
0153                        const QIcon& icon,
0154                        const QString& text,
0155                        const QVariant& userData = QVariant());
0156     void    insertItems(int index, const QStringList& list);
0157     void    addItem(const QString& text);
0158     void    addItem(const QIcon& icon,
0159                     const QString& text,
0160                     const QVariant& userData = QVariant());
0161     void    addItems(const QStringList& texts);
0162     QString itemText(int index)                                      const;
0163 
0164 private:
0165 
0166     class Private;
0167     Private* const d;
0168 };
0169 
0170 } // namespace Digikam
0171 
0172 #endif // DIGIKAM_SQUEEZED_COMBOBOX_H