]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kstandarditemmodel.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / kitemviews / kstandarditemmodel.cpp
index cde86af49ee1e6647e5826c3c5ffde6b0b99bf7b..128841ca8496f25a56dec4897aeb76955a900875 100644 (file)
@@ -1,25 +1,11 @@
-/***************************************************************************
- *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "kstandarditemmodel.h"
 
-#include <KDebug>
 #include "kstandarditem.h"
 
 KStandardItemModel::KStandardItemModel(QObject* parent) :
@@ -43,7 +29,7 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item)
         return;
     }
 
-    if (item && !m_indexesForItems.contains(item)) {
+    if (!m_indexesForItems.contains(item)) {
         item->m_model = this;
         m_items.insert(index, item);
         m_indexesForItems.insert(item, index);
@@ -57,13 +43,13 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item)
         // TODO: no hierarchical items are handled yet
 
         onItemInserted(index);
-        emit itemsInserted(KItemRangeList() << KItemRange(index, 1));
+        Q_EMIT itemsInserted(KItemRangeList() << KItemRange(index, 1));
     }
 }
 
 void KStandardItemModel::changeItem(int index, KStandardItem* item)
 {
-    if (index < 0 || index > count() || !item) {
+    if (index < 0 || index >= count() || !item) {
         delete item;
         return;
     }
@@ -89,13 +75,13 @@ void KStandardItemModel::changeItem(int index, KStandardItem* item)
 
     m_indexesForItems.remove(oldItem);
     delete oldItem;
-    oldItem = 0;
+    oldItem = nullptr;
 
     m_items[index] = item;
     m_indexesForItems.insert(item, index);
 
     onItemChanged(index, changedRoles);
-    emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
+    Q_EMIT itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
 }
 
 void KStandardItemModel::removeItem(int index)
@@ -113,19 +99,28 @@ void KStandardItemModel::removeItem(int index)
 
         onItemRemoved(index, item);
 
-        delete item;
-        item = 0;
+        item->deleteLater();
+        item = nullptr;
 
-        emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
+        Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(index, 1));
 
         // TODO: no hierarchical items are handled yet
     }
 }
 
+void KStandardItemModel::clear()
+{
+    int size = m_items.size();
+    m_items.clear();
+    m_indexesForItems.clear();
+
+    Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(0, size));
+}
+
 KStandardItem* KStandardItemModel::item(int index) const
 {
     if (index < 0 || index >= m_items.count()) {
-        return 0;
+        return nullptr;
     }
     return m_items[index];
 }
@@ -158,7 +153,7 @@ QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
 
 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
 {
-    Q_UNUSED(values);
+    Q_UNUSED(values)
     if (index < 0 || index >= count()) {
         return false;
     }
@@ -166,28 +161,28 @@ bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& v
     return true;
 }
 
-QMimeData* KStandardItemModel::createMimeData(const QSet<int>& indexes) const
+QMimeData* KStandardItemModel::createMimeData(const KItemSet& indexes) const
 {
-    Q_UNUSED(indexes);
-    return 0;
+    Q_UNUSED(indexes)
+    return nullptr;
 }
 
 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
 {
-    Q_UNUSED(text);
-    Q_UNUSED(startFromIndex);
+    Q_UNUSED(text)
+    Q_UNUSED(startFromIndex)
     return -1;
 }
 
 bool KStandardItemModel::supportsDropping(int index) const
 {
-    Q_UNUSED(index);
+    Q_UNUSED(index)
     return false;
 }
 
 QString KStandardItemModel::roleDescription(const QByteArray& role) const
 {
-    Q_UNUSED(role);
+    Q_UNUSED(role)
     return QString();
 }
 
@@ -195,7 +190,7 @@ QList<QPair<int, QVariant> > KStandardItemModel::groups() const
 {
     QList<QPair<int, QVariant> > groups;
 
-    const QByteArray role = sortRole();
+    const QByteArray role = sortRole().isEmpty() ? "group" : sortRole();
     bool isFirstGroupValue = true;
     QString groupValue;
     const int maxIndex = count() - 1;
@@ -213,20 +208,18 @@ QList<QPair<int, QVariant> > KStandardItemModel::groups() const
 
 void KStandardItemModel::onItemInserted(int index)
 {
-    Q_UNUSED(index);
+    Q_UNUSED(index)
 }
 
 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
 {
-    Q_UNUSED(index);
-    Q_UNUSED(changedRoles);
+    Q_UNUSED(index)
+    Q_UNUSED(changedRoles)
 }
 
 void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem)
 {
-    Q_UNUSED(index);
-    Q_UNUSED(removedItem);
+    Q_UNUSED(index)
+    Q_UNUSED(removedItem)
 }
 
-
-#include "kstandarditemmodel.moc"