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

# Cppcheck report - digiKam-nl-search-more-criteria-rev-e2466e0973: ../../core/libs/widgets/itemview/dcategorizedsortfilterproxymodel.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
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2010-01-16
     * Description : categorize item view based on DCategorizedView
     *
     * SPDX-FileCopyrightText: 2007      by Rafael Fernández López <ereslibre at kde dot org>
     * SPDX-FileCopyrightText: 2007      by John Tapsell <tapsell at kde dot org>
     * SPDX-FileCopyrightText: 2009-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
     * SPDX-FileCopyrightText: 2011-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "dcategorizedsortfilterproxymodel_p.h"

    // Qt includes

    #include <QItemSelection>
    #include <QStringList>
    #include <QCollator>
    #include <QAction>
    #include <QSize>

    // Local includes

    #include "categorizeditemmodel.h"

    namespace Digikam
    {

    DCategorizedSortFilterProxyModel::DCategorizedSortFilterProxyModel(QObject* const parent)
        : QSortFilterProxyModel(parent),
          d                    (new Private)
    {
        setDynamicSortFilter(false);
    }

    DCategorizedSortFilterProxyModel::~DCategorizedSortFilterProxyModel()
    {
        delete d;
    }

    void DCategorizedSortFilterProxyModel::sort(int column, Qt::SortOrder order)
    {
        d->sortColumn = column;
        d->sortOrder  = order;

        QSortFilterProxyModel::sort(column, order);
    }

    bool DCategorizedSortFilterProxyModel::isCategorizedModel() const
    {
        return d->categorizedModel;
    }

    void DCategorizedSortFilterProxyModel::setCategorizedModel(bool categorizedModel)
    {
        if (categorizedModel == d->categorizedModel)
        {
            return;
        }

        d->categorizedModel = categorizedModel;

        invalidate();
    }

    int DCategorizedSortFilterProxyModel::sortColumn() const
    {
        return d->sortColumn;
    }

    Qt::SortOrder DCategorizedSortFilterProxyModel::sortOrder() const
    {
        return d->sortOrder;
    }

    void DCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)<--- Shadow argument
    {
        if (sortCategoriesByNaturalComparison == d->sortCategoriesByNaturalComparison)
        {
            return;
        }

        d->sortCategoriesByNaturalComparison = sortCategoriesByNaturalComparison;

        invalidate();
    }

    bool DCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison() const
    {
        return d->sortCategoriesByNaturalComparison;
    }

    bool DCategorizedSortFilterProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
    {
        if (d->categorizedModel)
        {
            int compare = compareCategories(left, right);

            if      (compare > 0) // left is greater than right
            {
                return false;
            }
            else if (compare < 0) // left is less than right
            {
                return true;
            }
        }

        return subSortLessThan(left, right);
    }

    bool DCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex& left, const QModelIndex& right) const
    {
        return QSortFilterProxyModel::lessThan(left, right);
    }

    int DCategorizedSortFilterProxyModel::compareCategories(const QModelIndex& left, const QModelIndex& right) const
    {
        QVariant l = (left.model()  ? left.model()->data(left,   CategorySortRole) : QVariant());
        QVariant r = (right.model() ? right.model()->data(right, CategorySortRole) : QVariant());

        Q_ASSERT(l.isValid());
        Q_ASSERT(r.isValid());

    #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

        Q_ASSERT(l.typeId() == r.typeId());

        if (l.typeId() == QMetaType::QString)

    #else

        Q_ASSERT(l.type() == r.type());

        if (l.type() == QVariant::String)

    #endif

        {
            QString lstr = l.toString();
            QString rstr = r.toString();

            if (d->sortCategoriesByNaturalComparison)
            {
                return (d->collator.compare(lstr, rstr));
            }
            else
            {
                if (lstr < rstr)
                {
                    return (-1);
                }

                if (lstr > rstr)
                {
                    return 1;
                }

                return 0;
            }
        }

        qlonglong lint = l.toLongLong();
        qlonglong rint = r.toLongLong();

        if (lint < rint)
        {
            return -1;
        }

        if (lint > rint)
        {
            return 1;
        }

        return 0;
    }

    // -----------------------------------------------------------------------------------------------------------------------

    ActionSortFilterProxyModel::ActionSortFilterProxyModel(QObject* const parent)
        : DCategorizedSortFilterProxyModel(parent)
    {
    }

    bool ActionSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
    {
        QModelIndex index     = sourceModel()->index(source_row, 0, source_parent);
        QAction* const action = ActionItemModel::actionForIndex(index);

        return (action && action->isVisible());
    }

    } // namespace Digikam

    #include "moc_dcategorizedsortfilterproxymodel.cpp"