File indexing completed on 2024-04-28 12:07:08
0001 /************************************************************************ 0002 * * 0003 * This file is part of Kooka, a scanning/OCR application using * 0004 * Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>. * 0005 * * 0006 * Copyright (C) 2016 Jonathan Marten <jjm@keelhaul.me.uk> * 0007 * * 0008 * Kooka is free software; you can redistribute it and/or modify it * 0009 * under the terms of the GNU Library General Public License as * 0010 * published by the Free Software Foundation and appearing in the * 0011 * file COPYING included in the packaging of this file; either * 0012 * version 2 of the License, or (at your option) any later version. * 0013 * * 0014 * As a special exception, permission is given to link this program * 0015 * with any version of the KADMOS OCR/ICR engine (a product of * 0016 * reRecognition GmbH, Kreuzlingen), and distribute the resulting * 0017 * executable without including the source code for KADMOS in the * 0018 * source distribution. * 0019 * * 0020 * This program is distributed in the hope that it will be useful, * 0021 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0023 * GNU General Public License for more details. * 0024 * * 0025 * You should have received a copy of the GNU General Public * 0026 * License along with this program; see the file COPYING. If * 0027 * not, see <http://www.gnu.org/licenses/>. * 0028 * * 0029 ************************************************************************/ 0030 0031 #include "imagefilter.h" 0032 0033 #include <qimagereader.h> 0034 #include <qimagewriter.h> 0035 #include <qmimedatabase.h> 0036 #include <qmimetype.h> 0037 0038 #include <klocalizedstring.h> 0039 0040 0041 static bool commentLessThan(const QString &s1, const QString &s2) 0042 { 0043 const int idx1 = s1.indexOf('|'); 0044 const int idx2 = s2.indexOf('|'); 0045 return (s1.mid(idx1+1).toLower()<s2.mid(idx2+1).toLower()); 0046 } 0047 0048 0049 static QStringList filterList(ImageFilter::FilterMode mode, ImageFilter::FilterOptions options, bool kdeFormat) 0050 { 0051 QStringList list; 0052 QStringList allPatterns; 0053 0054 QList<QByteArray> mimeTypes; 0055 if (mode==ImageFilter::Writing) mimeTypes = QImageWriter::supportedMimeTypes(); 0056 else mimeTypes = QImageReader::supportedMimeTypes(); 0057 0058 QMimeDatabase db; 0059 0060 for (const QByteArray &mimeType : qAsConst(mimeTypes)) 0061 { 0062 QMimeType mime = db.mimeTypeForName(mimeType); 0063 if (!mime.isValid()) continue; 0064 0065 QStringList pats = mime.globPatterns(); 0066 list.append(pats.join(' ')+'|'+mime.comment()); 0067 if (options & ImageFilter::AllImages) allPatterns.append(pats); 0068 } 0069 0070 if (!(options & ImageFilter::Unsorted)) // unless list wanted unsorted, 0071 { // sort by the mime type comment 0072 std::sort(list.begin(), list.end(), commentLessThan); 0073 } 0074 0075 if (!kdeFormat) 0076 { 0077 // We generated a KDE format filter above (for ease of sorting), 0078 // so if we want a Qt format filter then it needs to be rearranged. 0079 for (QStringList::iterator it = list.begin(); it!=list.end(); ++it) 0080 { 0081 QString &filter = (*it); // modifying list in place 0082 int idx = filter.indexOf('|'); 0083 if (idx==-1) continue; 0084 filter = filter.mid(idx+1)+" ("+filter.left(idx)+')'; 0085 (*it) = filter; 0086 } 0087 } 0088 0089 if (!allPatterns.isEmpty()) // want an "All Images" entry 0090 { 0091 if (kdeFormat) list.prepend(i18n("%1|All Image Files", allPatterns.join(' '))); 0092 else list.prepend(i18n("All Image Files (%1)", allPatterns.join(' '))); 0093 } 0094 0095 if (options & ImageFilter::AllFiles) // want an "All Files" entry 0096 { 0097 if (kdeFormat) list.append(i18n("*|All Files")); 0098 else list.append(i18n("All Files (*)")); 0099 } 0100 0101 return (list); 0102 } 0103 0104 0105 QString ImageFilter::qtFilterString(ImageFilter::FilterMode mode, ImageFilter::FilterOptions options) 0106 { 0107 return (qtFilterList(mode, options).join(";;")); 0108 } 0109 0110 0111 QStringList ImageFilter::qtFilterList(ImageFilter::FilterMode mode, ImageFilter::FilterOptions options) 0112 { 0113 return (filterList(mode, options, false)); 0114 } 0115 0116 0117 QString ImageFilter::kdeFilter(ImageFilter::FilterMode mode, ImageFilter::FilterOptions options) 0118 { 0119 return (filterList(mode, options, true).join('\n')); 0120 }