Cppcheck - HTML report - digiKam-nl-search-more-criteria-rev-e2466e0973

# Cppcheck report - digiKam-nl-search-more-criteria-rev-e2466e0973: ../../core/libs/widgets/combo/squeezedcombobox.cpp

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2008-08-21
     * Description : a combo box with a width not depending of text
     *               content size
     *
     * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     * SPDX-FileCopyrightText:      2008 by Andi Clemens <andi dot clemens at googlemail dot com>
     * SPDX-FileCopyrightText:      2005 by Tom Albers <tomalbers at kde dot nl>
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "squeezedcombobox.h"

    // Qt includes

    #include <QTimer>
    #include <QStyle>
    #include <QApplication>
    #include <QResizeEvent>

    namespace Digikam
    {

    class Q_DECL_HIDDEN SqueezedComboBox::Private
    {
    public:

        Private() = default;

        QMap<int, QString> originalItems;

        QTimer*            timer = nullptr;
    };

    SqueezedComboBox::SqueezedComboBox(QWidget* const parent, const char* name)
        : QComboBox(parent),
          d        (new Private)
    {
        setObjectName(QString::fromUtf8(name));
        setMinimumWidth(100);

        d->timer = new QTimer(this);
        d->timer->setSingleShot(true);

        connect(d->timer, &QTimer::timeout,
                this, &SqueezedComboBox::slotTimeOut);

        connect(this, static_cast<void (SqueezedComboBox::*)(int)>(&SqueezedComboBox::activated),
                this, &SqueezedComboBox::slotUpdateToolTip);
    }

    SqueezedComboBox::~SqueezedComboBox()
    {
        d->originalItems.clear();

        delete d->timer;
        delete d;
    }

    bool SqueezedComboBox::contains(const QString& text) const
    {
        if (text.isEmpty())
        {
            return false;
        }

        for (QMap<int, QString>::const_iterator it = d->originalItems.constBegin() ;
             it != d->originalItems.constEnd() ; ++it)
        {
            if (it.value() == text)
            {
                return true;
            }
        }

        return false;
    }

    int SqueezedComboBox::findOriginalText(const QString& text,
                                           Qt::CaseSensitivity cs) const
    {
        if (text.isEmpty())
        {
            return -1;
        }

        for (QMap<int, QString>::const_iterator it = d->originalItems.constBegin() ;
             it != d->originalItems.constEnd() ; ++it)
        {
            if (it.value().compare(text, cs) == 0)
            {
                return it.key();
            }
        }

        return -1;
    }

    QSize SqueezedComboBox::sizeHint() const
    {
        ensurePolished();
        QFontMetrics fm = fontMetrics();
        int maxW        = count() ? 18 : 7 * fm.horizontalAdvance(QLatin1Char('x')) + 18;
        int maxH        = qMax(fm.lineSpacing(), 14) + 2;

        QStyleOptionComboBox options;
        options.initFrom(this);

        return style()->sizeFromContents(QStyle::CT_ComboBox, &options, QSize(maxW, maxH), this);
    }

    void SqueezedComboBox::insertSqueezedItem(const QString& newItem, int index,
                                              const QVariant& userData)
    {
        d->originalItems[index] = newItem;
        QComboBox::insertItem(index, squeezeText(newItem), userData);

        // if this is the first item, set the tooltip.

        if (index == 0)
        {
            slotUpdateToolTip(0);
        }
    }

    void SqueezedComboBox::insertSqueezedList(const QStringList& newItems, int index)
    {
        for (QStringList::const_iterator it = newItems.constBegin() ;
             it != newItems.constEnd() ; ++it)
        {
            insertSqueezedItem(*it, index);
            ++index;
        }
    }

    void SqueezedComboBox::addSqueezedItem(const QString& newItem,
                                           const QVariant& userData)
    {
        insertSqueezedItem(newItem, count(), userData);
    }

    void SqueezedComboBox::setCurrent(const QString& itemText)<--- Shadow argument
    {
        int itemIndex = findOriginalText(itemText);

        if (itemIndex >= 0)
        {
            setCurrentIndex(itemIndex);
        }
    }

    void SqueezedComboBox::resizeEvent(QResizeEvent *)
    {
        d->timer->start(200);
    }

    void SqueezedComboBox::slotTimeOut()
    {
        for (QMap<int, QString>::iterator it = d->originalItems.begin() ;
             it != d->originalItems.end() ; ++it)
        {
            setItemText(it.key(), squeezeText(it.value()));
        }
    }

    QString SqueezedComboBox::squeezeText(const QString& original) const
    {
        // not the complete widgetSize is usable. Need to compensate for that.

        int widgetSize = width() - 30;
        QFontMetrics fm(fontMetrics());

        // If we can fit the full text, return that.

        if (fm.horizontalAdvance(original) < widgetSize)
        {
            return (original);
        }

        // We need to squeeze.

        QString sqItem = original; // prevent empty return value;
        widgetSize     = widgetSize-fm.horizontalAdvance(QLatin1String("..."));

        for (int i = 0 ; i != original.length() ; ++i)
        {
            if ((int)fm.horizontalAdvance(original.right(i)) > widgetSize)
            {
                sqItem = QString(original.left(i) + QLatin1String("..."));
                break;
            }
        }

        return sqItem;
    }

    void SqueezedComboBox::slotUpdateToolTip(int index)
    {
         setToolTip(d->originalItems[index]);

         Q_EMIT signalItemActivated(item(index));
    }

    QString SqueezedComboBox::itemHighlighted() const
    {
        int curItem = currentIndex();

        return d->originalItems[curItem];
    }

    QString SqueezedComboBox::item(int index) const
    {
        return d->originalItems[index];
    }

    } // namespace Digikam

    #include "moc_squeezedcombobox.cpp"