File indexing completed on 2024-04-21 15:12:13

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 "scanicons.h"
0032 
0033 #include <qicon.h>
0034 
0035 #include <kiconloader.h>
0036 
0037 #include "libkookascan_logging.h"
0038 
0039 
0040 // This is safe - the ScanIcons instance will be not be constructed
0041 // until long after application startup.
0042 static QMap<QByteArray, ScanIcons::IconType> sModeMap;
0043 
0044 
0045 ScanIcons::ScanIcons()
0046 {
0047     KIconLoader::global()->addAppDir("libkookascan");   // access to our icons
0048 
0049     // The option display strings are translated via the 'sane-backends' message
0050     // catalogue, see KScanCombo::setList().  But finding a scan mode icon works on
0051     // the untranslated strings, which are really SANE values.  So the strings
0052     // here need to cover the full set of possible SANE values for all backends
0053     // (extra ones which a particular SANE backend doesn't support don't matter).
0054     //
0055     // These map key strings therefore need to be untranslated SANE option values.
0056     // So don't translate these strings or wrap them in any sort of i18n().
0057 
0058     sModeMap.insert("Lineart", ScanIcons::BlackWhite);
0059     sModeMap.insert("Binary", ScanIcons::BlackWhite);
0060     sModeMap.insert("Gray", ScanIcons::Greyscale);
0061     sModeMap.insert("Grayscale", ScanIcons::Greyscale);
0062     sModeMap.insert("Color", ScanIcons::Colour);
0063     sModeMap.insert("Halftone", ScanIcons::Halftone);
0064 }
0065 
0066 
0067 ScanIcons *ScanIcons::self()
0068 {
0069     static ScanIcons *instance = nullptr;
0070     if (instance==nullptr) instance = new ScanIcons;
0071     return (instance);
0072 }
0073 
0074 
0075 static QIcon findIcon(ScanIcons::IconType type, QIcon *var, const QString &name1, const QString &name2)
0076 {
0077     if (var->isNull())                  // not found yet
0078     {
0079         // First try to find an icon as installed by libksane, with canReturnNull
0080         // set so that a null path will be returned if the icon was not found.
0081         // They look better than our rather ancient ones, so use them
0082         // if possible.
0083         QString ip = KIconLoader::global()->iconPath(name1, KIconLoader::Small, true);
0084         // Then try our own icons, returning the 'unknown' icon if not found.
0085         if (ip.isEmpty()) ip = KIconLoader::global()->iconPath(name2, KIconLoader::Small);
0086         qCDebug(LIBKOOKASCAN_LOG) << "for" << type << "using" << ip;
0087         *var = QIcon(ip);               // save for next time
0088         Q_ASSERT(!var->isNull());
0089     }
0090 
0091     return (*var);
0092 }
0093 
0094 
0095 QIcon ScanIcons::icon(ScanIcons::IconType type)
0096 {
0097     switch (type)
0098     {
0099 case ScanIcons::BlackWhite: return (findIcon(type, &mBlackWhiteIcon, "black-white", "palette-lineart"));
0100 case ScanIcons::Greyscale:  return (findIcon(type, &mGreyscaleIcon, "gray-scale", "palette-gray"));
0101 case ScanIcons::Halftone:   return (findIcon(type, &mHalftoneIcon, "halftone", "palette-halftone"));
0102 case ScanIcons::Colour:     return (findIcon(type, &mColourIcon, "color", "palette-color"));
0103 default:            return (QIcon());
0104     }
0105 }
0106 
0107 
0108 QIcon ScanIcons::icon(const QByteArray &scanMode)
0109 {
0110     if (scanMode.isEmpty()) return (QIcon());
0111     if (!sModeMap.contains(scanMode))
0112     {
0113         qCWarning(LIBKOOKASCAN_LOG) << "Don't know what type of scan" << scanMode << "is. Please add it to ScanIcons::ScanIcons().";
0114         return (QIcon());
0115     }
0116 
0117     return (icon(sModeMap.value(scanMode)));
0118 }
0119 
0120 
0121 QList<QByteArray> ScanIcons::allModes() const
0122 {
0123     return (sModeMap.keys());
0124 }