File indexing completed on 2024-05-12 16:02:30

0001 /* ============================================================
0002  * Author: Tom Albers <tomalbers@kde.nl>
0003  * Date  : 2005-01-01
0004  * Description :
0005  *
0006  * SPDX-FileCopyrightText: 2005 Tom Albers <tomalbers@kde.nl>
0007  *
0008  * SPDX-License-Identifier: GPL-2.0-or-later
0009  *
0010  * ============================================================ */
0011 
0012 #ifndef KISSQUEEZEDCOMBOBOX_H
0013 #define KISSQUEEZEDCOMBOBOX_H
0014 
0015 class QTimer;
0016 class QResizeEvent;
0017 class QWidget;
0018 
0019 // Qt includes.
0020 
0021 #include <QComboBox>
0022 #include <QWidget>
0023 #include <QIcon>
0024 
0025 #include "kritawidgetutils_export.h"
0026 
0027 /** @class KisSqueezedComboBox
0028  *
0029  * This widget is a QComboBox, but then a little bit
0030  * different. It only shows the right part of the items
0031  * depending on de size of the widget. When it is not
0032  * possible to show the complete item, it will be shortened
0033  * and "..." will be prepended.
0034  *
0035  * @image html KisSqueezedComboBox.png "This is how it looks"
0036  * @author Tom Albers
0037  */
0038 class KRITAWIDGETUTILS_EXPORT KisSqueezedComboBox : public QComboBox
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043 
0044     /**
0045      * Constructor
0046      * @param parent parent widget
0047      * @param name name to give to the widget
0048      */
0049     KisSqueezedComboBox(QWidget *parent = 0, const char *name = 0);
0050     /**
0051      * destructor
0052      */
0053     ~KisSqueezedComboBox() override;
0054 
0055     /**
0056      *
0057      * Returns true if the combobox contains the original (not-squeezed)
0058      * version of text.
0059      * @param text the original (not-squeezed) text to check for
0060      */
0061     bool contains(const QString & text) const;
0062 
0063     /**
0064      * Returns index of a original text, -1 if the text isn't found
0065      * @param text the original (not-squeezed) text to search for
0066      */
0067     qint32 findOriginalText(const QString & text) const;
0068 
0069 
0070     /**
0071      * Return the list of original text items
0072      */
0073     QStringList originalTexts() const;
0074 
0075     /**
0076      * Reset the combo box and initialize it with the list of (original) text items
0077      */
0078     void resetOriginalTexts(const QStringList &texts);
0079 
0080     /**
0081      * This inserts a item to the list. See QComboBox::insertItem()
0082      * for details. Please do not use QComboBox::insertItem() to this
0083      * widget, as that will fail.
0084      * @param newItem the original (long version) of the item which needs
0085      *                to be added to the combobox
0086      * @param index the position in the widget.
0087      * @param userData the user data.
0088      */
0089     void insertSqueezedItem(const QString& newItem, int index, QVariant userData = QVariant());
0090     void insertSqueezedItem(const QIcon &icon, const QString& newItem, int index, QVariant userData = QVariant());
0091 
0092     /**
0093      * Append an item.
0094      * @param newItem the original (long version) of the item which needs
0095      *                to be added to the combobox
0096      * @param userData the user data.
0097      */
0098     void addSqueezedItem(const QString& newItem, QVariant userData = QVariant());
0099 
0100     /**
0101      * Append an item.
0102      * @param icon    the item icon
0103      * @param newItem the original (long version) of the item which needs
0104      *                to be added to the combobox
0105      * @param userData the user data
0106      */
0107     void addSqueezedItem(const QIcon &icon, const QString& newItem, QVariant userData = QVariant());
0108 
0109     /**
0110      * Set the current item to the one matching the given text.
0111      *
0112      * @param itemText the original (long version) of the item text
0113      */
0114     void setCurrent(const QString& itemText);
0115 
0116     /**
0117      * This method returns the full text (not squeezed) of the currently
0118      * highlighted item.
0119      * @return full text of the highlighted item
0120      */
0121     QString currentUnsqueezedText();
0122 
0123     /**
0124      * remove the squeezed item at index
0125      */
0126     void removeSqueezedItem(int index);
0127 
0128     /**
0129      * Sets the sizeHint() of this widget.
0130      */
0131     QSize sizeHint() const override;
0132 
0133     static QString squeezeText(const QString& original, const QWidget *widget);
0134 
0135 private Q_SLOTS:
0136     void slotTimeOut();
0137 
0138 private:
0139     void resizeEvent(QResizeEvent *) override;
0140 
0141     // Prevent these from being used.
0142     void setCurrentText(const QString& itemText);
0143     void insertItem(const QString &text);
0144     void insertItem(qint32 index, const QString &text);
0145     void addItem(const QString &text);
0146 
0147     QMap<int, QString> m_originalItems;
0148     QTimer *m_timer;
0149 
0150 };
0151 
0152 #endif // SQUEEZEDCOMBOBOX_H