File indexing completed on 2024-04-28 15:39:58
0001 // SPDX-FileCopyrightText: 2005 Stephan Binner <binner@kde.org> 0002 // SPDX-FileCopyrightText: 2005-2013 Jesper K. Pedersen <jesper.pedersen@kdab.com> 0003 // SPDX-FileCopyrightText: 2007 Baptiste Mathus <ml@batmat.net> 0004 // SPDX-FileCopyrightText: 2007 Dirk Mueller <mueller@kde.org> 0005 // SPDX-FileCopyrightText: 2007 Jan Kundrát <jkt@flaska.net> 0006 // SPDX-FileCopyrightText: 2007-2008 Laurent Montel <montel@kde.org> 0007 // SPDX-FileCopyrightText: 2008 Henner Zeller <h.zeller@acm.org> 0008 // SPDX-FileCopyrightText: 2008 Tuomas Suutari <tuomas@nepnep.net> 0009 // SPDX-FileCopyrightText: 2013-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at> 0010 // SPDX-FileCopyrightText: 2014-2020 Robert Krawitz <rlk@alum.mit.edu> 0011 // 0012 // SPDX-License-Identifier: GPL-2.0-or-later 0013 0014 #include "ImageInfoList.h" 0015 0016 #include "ImageInfo.h" 0017 0018 #include <Utilities/FastDateTime.h> 0019 #include <kpabase/FileNameList.h> 0020 #include <kpabase/Logging.h> 0021 0022 #include <KLocalizedString> 0023 #include <QVector> 0024 #include <QtAlgorithms> 0025 using namespace DB; 0026 0027 class SortableImageInfo 0028 { 0029 public: 0030 SortableImageInfo(const Utilities::FastDateTime &datetime, const QString &string, const ImageInfoPtr &info) 0031 : m_dt(datetime) 0032 , m_st(string) 0033 , m_in(info) 0034 { 0035 } 0036 SortableImageInfo() = default; 0037 const Utilities::FastDateTime &DateTime(void) const { return m_dt; } 0038 const QString &String(void) const { return m_st; } 0039 const ImageInfoPtr &ImageInfo(void) const { return m_in; } 0040 bool operator==(const SortableImageInfo &other) const { return m_dt == other.m_dt && m_st == other.m_st; } 0041 bool operator!=(const SortableImageInfo &other) const { return m_dt != other.m_dt || m_st != other.m_st; } 0042 bool operator>(const SortableImageInfo &other) const 0043 { 0044 if (m_dt != other.m_dt) { 0045 return m_dt > other.m_dt; 0046 } else { 0047 return m_st > other.m_st; 0048 } 0049 } 0050 bool operator<(const SortableImageInfo &other) const 0051 { 0052 if (m_dt != other.m_dt) { 0053 return m_dt < other.m_dt; 0054 } else { 0055 return m_st < other.m_st; 0056 } 0057 } 0058 bool operator>=(const SortableImageInfo &other) const { return *this == other || *this > other; } 0059 bool operator<=(const SortableImageInfo &other) const { return *this == other || *this < other; } 0060 0061 private: 0062 Utilities::FastDateTime m_dt; 0063 QString m_st; 0064 ImageInfoPtr m_in; 0065 }; 0066 0067 ImageInfoList ImageInfoList::sort() const 0068 { 0069 QVector<SortableImageInfo> vec; 0070 for (ImageInfoListConstIterator it = constBegin(); it != constEnd(); ++it) { 0071 vec.append(SortableImageInfo((*it)->date().start(), (*it)->fileName().absolute(), *it)); 0072 } 0073 0074 std::sort(vec.begin(), vec.end()); 0075 0076 ImageInfoList res; 0077 for (QVector<SortableImageInfo>::ConstIterator mapIt = vec.constBegin(); mapIt != vec.constEnd(); ++mapIt) { 0078 res.append(mapIt->ImageInfo()); 0079 } 0080 return res; 0081 } 0082 0083 void ImageInfoList::sortAndMergeBackIn(ImageInfoList &subListToSort) 0084 { 0085 if (subListToSort.isEmpty()) { 0086 return; 0087 } 0088 ImageInfoList sorted = subListToSort.sort(); 0089 0090 // depending on view sort order, the first or the last index may be the first index 0091 const int insertIndex = qMin(indexOf(subListToSort.constFirst()), indexOf(subListToSort.constLast())); 0092 Q_ASSERT(insertIndex >= 0); 0093 0094 // Delete the items we will merge in. 0095 for (ImageInfoListIterator it = sorted.begin(); it != sorted.end(); ++it) 0096 remove(*it); 0097 0098 ImageInfoListIterator insertIt = begin() + insertIndex; 0099 0100 // Now merge in the items 0101 for (ImageInfoListIterator it = sorted.begin(); it != sorted.end(); ++it) { 0102 insertIt = insert(insertIt, *it); 0103 ++insertIt; 0104 } 0105 } 0106 0107 void ImageInfoList::appendList(ImageInfoList &list) 0108 { 0109 for (ImageInfoListConstIterator it = list.constBegin(); it != list.constEnd(); ++it) { 0110 append(*it); 0111 } 0112 } 0113 0114 void ImageInfoList::printItems() 0115 { 0116 for (ImageInfoListConstIterator it = constBegin(); it != constEnd(); ++it) { 0117 qCDebug(DBLog) << (*it)->fileName().absolute(); 0118 } 0119 } 0120 0121 bool ImageInfoList::isSorted() 0122 { 0123 if (count() == 0) 0124 return true; 0125 0126 Utilities::FastDateTime prev = first()->date().start(); 0127 QString prevFile = first()->fileName().absolute(); 0128 for (ImageInfoListConstIterator it = constBegin(); it != constEnd(); ++it) { 0129 Utilities::FastDateTime cur = (*it)->date().start(); 0130 QString curFile = (*it)->fileName().absolute(); 0131 if (prev > cur || (prev == cur && prevFile > curFile)) 0132 return false; 0133 prev = cur; 0134 prevFile = curFile; 0135 } 0136 return true; 0137 } 0138 0139 void ImageInfoList::mergeIn(ImageInfoList other) 0140 { 0141 ImageInfoList tmp; 0142 0143 for (ImageInfoListConstIterator it = constBegin(); it != constEnd(); ++it) { 0144 Utilities::FastDateTime thisDate = (*it)->date().start(); 0145 QString thisFileName = (*it)->fileName().absolute(); 0146 while (other.count() != 0) { 0147 Utilities::FastDateTime otherDate = other.first()->date().start(); 0148 QString otherFileName = other.first()->fileName().absolute(); 0149 if (otherDate < thisDate || (otherDate == thisDate && otherFileName < thisFileName)) { 0150 tmp.append(other[0]); 0151 other.pop_front(); 0152 } else 0153 break; 0154 } 0155 tmp.append(*it); 0156 } 0157 tmp.appendList(other); 0158 *this = tmp; 0159 } 0160 0161 void ImageInfoList::remove(const ImageInfoPtr &info) 0162 { 0163 for (ImageInfoListIterator it = begin(); it != end(); ++it) { 0164 if ((*(*it)) == *info) { 0165 QList<ImageInfoPtr>::erase(it); 0166 return; 0167 } 0168 } 0169 } 0170 0171 DB::FileNameList ImageInfoList::files(DB::MediaType type) const 0172 { 0173 DB::FileNameList res; 0174 for (const ImageInfoPtr &info : *this) { 0175 if (info->mediaType() & type) { 0176 res.append(info->fileName()); 0177 } 0178 } 0179 return res; 0180 } 0181 // vi:expandtab:tabstop=4 shiftwidth=4: