File indexing completed on 2025-04-27 04:04:22
0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org> 0002 // SPDX-License-Identifier: LGPL-2.0-or-later 0003 0004 #include "openfilemodel.h" 0005 0006 #include "roles.h" 0007 #include <QMimeDatabase> 0008 0009 OpenFileModel::OpenFileModel(const QStringList &images, QObject *parent) 0010 : QAbstractListModel(parent) 0011 , m_images(images) 0012 { 0013 } 0014 0015 QHash<int, QByteArray> OpenFileModel::roleNames() const 0016 { 0017 return Roles::roleNames(); 0018 } 0019 0020 QVariant OpenFileModel::data(const QModelIndex &index, int role) const 0021 { 0022 Q_ASSERT(checkIndex(index, CheckIndexOption::ParentIsInvalid | CheckIndexOption::IndexIsValid)); 0023 0024 const int indexValue = index.row(); 0025 0026 switch (role) { 0027 case Roles::ContentRole: 0028 // TODO: return the filename component 0029 return m_images.at(indexValue); 0030 0031 case Roles::ImageUrlRole: 0032 return m_images.at(indexValue); 0033 0034 case Roles::ItemTypeRole: 0035 return Types::Image; 0036 0037 case Roles::MimeTypeRole: { 0038 QMimeDatabase db; 0039 QMimeType type = db.mimeTypeForFile(m_images.at(indexValue)); 0040 return type.name(); 0041 } 0042 0043 case Roles::SelectedRole: 0044 return false; 0045 } 0046 0047 return {}; 0048 } 0049 0050 int OpenFileModel::rowCount(const QModelIndex &parent) const 0051 { 0052 Q_UNUSED(parent) 0053 return m_images.size(); 0054 } 0055 0056 void OpenFileModel::updateOpenFiles(const QStringList &images) 0057 { 0058 if (!images.isEmpty()) { 0059 beginResetModel(); 0060 m_images = images; 0061 endResetModel(); 0062 Q_EMIT urlToOpenChanged(); 0063 Q_EMIT updatedImages(); 0064 } 0065 } 0066 0067 QString OpenFileModel::urlToOpen() const 0068 { 0069 if (m_images.length() == 1) { 0070 return m_images.value(0); 0071 } 0072 return QString(); 0073 }