Warning, file /utilities/skanpage/src/FormatModel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "FormatModel.h"
0008 
0009 #include <QImageWriter>
0010 #include <QMimeType>
0011 #include <QMimeDatabase>
0012 
0013 #include <KLocalizedString>
0014 
0015 class FormatModelPrivate
0016 {
0017 public:
0018     QList<QMimeType> m_formatList;
0019     QVariantList m_formatFilter;
0020 };
0021 
0022 FormatModel::FormatModel(QObject *parent)
0023     : QAbstractListModel(parent)
0024     , d(std::make_unique<FormatModelPrivate>())
0025 {
0026     QList<QByteArray> tempList = QImageWriter::supportedMimeTypes();
0027     const QMimeDatabase mimeDB;
0028     // Put first class citizens at first place
0029     tempList.removeAll(QByteArray("image/jpeg"));
0030     tempList.removeAll(QByteArray("image/tiff"));
0031     tempList.removeAll(QByteArray("image/png"));
0032     tempList.insert(0, QByteArray("application/pdf"));
0033     tempList.insert(1, QByteArray("image/png"));
0034     tempList.insert(2, QByteArray("image/jpeg"));
0035     tempList.insert(3, QByteArray("image/tiff"));
0036 
0037     for (const auto &mimeString : tempList) {
0038         const QMimeType mimeType = mimeDB.mimeTypeForName(QString::fromLatin1(mimeString));
0039         d->m_formatList.append(mimeType);
0040         // craft a string that QML's FileDialog understands
0041         d->m_formatFilter.append({mimeType.comment() + QStringLiteral("(*.") + mimeType.preferredSuffix() + QStringLiteral(")")});
0042     }
0043 }
0044 
0045 FormatModel::~FormatModel()
0046 {
0047 }
0048 
0049 QHash<int, QByteArray> FormatModel::roleNames() const
0050 {
0051     QHash<int, QByteArray> roles;
0052     roles[NameRole] = "name";
0053     roles[SuffixRole] = "suffix";
0054     roles[CommentRole] = "comment";
0055     roles[NameFilterRole] = "nameFilter";
0056     return roles;
0057 }
0058 
0059 int FormatModel::rowCount(const QModelIndex &) const
0060 {
0061     return d->m_formatList.count();
0062 }
0063 
0064 QVariant FormatModel::data(const QModelIndex &index, int role) const
0065 {
0066     if (!index.isValid()) {
0067         return QVariant();
0068     }
0069 
0070     return getData(index.row(), role);
0071 }
0072 
0073 QVariant FormatModel::getData(int index, int role) const
0074 {
0075     if (index >= d->m_formatList.size() || index < 0) {
0076         return QVariant();
0077     }
0078 
0079     switch (role) {
0080     case NameRole:
0081         return d->m_formatList.at(index).name();
0082         break;
0083     case SuffixRole:
0084         return d->m_formatList.at(index).preferredSuffix();
0085         break;
0086     case CommentRole:
0087         return d->m_formatList.at(index).comment();
0088         break;
0089     case NameFilterRole:
0090         return d->m_formatFilter.at(index);
0091         break;
0092     default:
0093         break;
0094     }
0095     return QVariant();
0096 }
0097 
0098 QVariantList FormatModel::formatFilter() const
0099 {
0100     return d->m_formatFilter;
0101 }
0102 
0103 QString FormatModel::pdfFormatFilter() const
0104 {
0105     return d->m_formatFilter.at(0).toString();
0106 }