File indexing completed on 2024-04-28 15:39:06
0001 // SPDX-FileCopyrightText: 2020-2022 Tobias Leupold <tl at stonemx dot de> 0002 // 0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 0005 // Local includes 0006 #include "ImagesListFilter.h" 0007 #include "Coordinates.h" 0008 #include "ImagesModel.h" 0009 #include "MimeHelper.h" 0010 0011 // Qt includes 0012 #include <QDebug> 0013 #include <QMimeData> 0014 #include <QUrl> 0015 0016 ImagesListFilter::ImagesListFilter(QObject *parent, KGeoTag::ImagesListType type) 0017 : QSortFilterProxyModel(parent), 0018 m_listType(type) 0019 { 0020 } 0021 0022 void ImagesListFilter::setListType(KGeoTag::ImagesListType type) 0023 { 0024 m_listType = type; 0025 invalidateFilter(); 0026 } 0027 0028 void ImagesListFilter::setSourceModel(QAbstractItemModel *sourceModel) 0029 { 0030 QSortFilterProxyModel::setSourceModel(sourceModel); 0031 m_imagesModel = qobject_cast<ImagesModel *>(sourceModel); 0032 } 0033 0034 bool ImagesListFilter::filterAcceptsRow(int sourceRow, const QModelIndex &) const 0035 { 0036 if (m_listType == KGeoTag::AllImages) { 0037 return true; 0038 } 0039 0040 const bool coordinatesSet = sourceModel()->index(sourceRow, 0).data( 0041 KGeoTag::CoordinatesRole).value<Coordinates>().isSet(); 0042 0043 return (m_listType == KGeoTag::AssignedImages && coordinatesSet) 0044 || (m_listType == KGeoTag::UnAssignedImages && ! coordinatesSet); 0045 } 0046 0047 Qt::DropActions ImagesListFilter::supportedDropActions() const 0048 { 0049 return Qt::CopyAction | Qt::MoveAction; 0050 } 0051 0052 Qt::ItemFlags ImagesListFilter::flags(const QModelIndex &index) const 0053 { 0054 auto defaultFlags = QSortFilterProxyModel::flags(index); 0055 0056 if (index.isValid()) { 0057 return defaultFlags; 0058 } else { 0059 return Qt::ItemIsDropEnabled | defaultFlags; 0060 } 0061 } 0062 0063 bool ImagesListFilter::canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, 0064 const QModelIndex &) const 0065 { 0066 if (! (action & (Qt::CopyAction | Qt::MoveAction)) || ! data->hasUrls()) { 0067 return false; 0068 } 0069 0070 // Check if the drag's origin is an image list 0071 const auto source = data->data(KGeoTag::SourceImagesListMimeType); 0072 if (! source.isEmpty()) { 0073 // Don't allow drops on the "assigned" images list 0074 if (m_listType == KGeoTag::AssignedImages) { 0075 return false; 0076 } 0077 // Don't allow drops on the origin list 0078 if (source == KGeoTag::SourceImagesList.value(m_listType)) { 0079 return false; 0080 } 0081 0082 } else { 0083 // The user wants to drop files from an external source 0084 if (MimeHelper::getUsablePaths(KGeoTag::DroppedOnImageList, data).isEmpty()) { 0085 return false; 0086 } 0087 } 0088 0089 return true; 0090 } 0091 0092 bool ImagesListFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int, int, 0093 const QModelIndex &) 0094 { 0095 if (! (action & (Qt::CopyAction | Qt::MoveAction)) || ! data->hasUrls()) { 0096 return false; 0097 } 0098 0099 QVector<QString> paths; 0100 QVector<QString> removeCoordinates; 0101 0102 const auto usablePaths = MimeHelper::getUsablePaths(KGeoTag::DroppedOnImageList, data); 0103 for (const auto &path : usablePaths) { 0104 if (! m_imagesModel->contains(path)) { 0105 paths.append(path); 0106 } else { 0107 if (m_listType == KGeoTag::UnAssignedImages 0108 && m_imagesModel->coordinates(path).isSet()) { 0109 0110 removeCoordinates.append(path); 0111 } 0112 } 0113 } 0114 0115 if (! removeCoordinates.isEmpty()) { 0116 Q_EMIT requestRemoveCoordinates(removeCoordinates); 0117 } 0118 0119 if (! paths.isEmpty()) { 0120 Q_EMIT requestAddingImages(paths); 0121 } 0122 0123 return true; 0124 }