File indexing completed on 2025-01-05 03:53:12

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-11-07
0007  * Description : a tool to print images
0008  *
0009  * SPDX-FileCopyrightText: 2007-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "advprintsettings.h"
0016 
0017 // Qt includes
0018 
0019 #include <QStandardPaths>
0020 
0021 // KDE includes
0022 
0023 #include <klocalizedstring.h>
0024 #include <kconfiggroup.h>
0025 
0026 // Local includes
0027 
0028 #include "advprintphoto.h"
0029 
0030 namespace DigikamGenericPrintCreatorPlugin
0031 {
0032 
0033 AdvPrintSettings::AdvPrintSettings()
0034     : selMode           (IMAGES),
0035       printerName       (outputName(FILES)),
0036 
0037       /// Select a different page to force a refresh in initPhotoSizes.
0038       pageSize          (QSizeF(-1, -1)),
0039 
0040       captionType       (NONE),
0041       captionColor      (QColor(Qt::yellow)),
0042       captionFont       (QFont(QLatin1String("Sans Serif"))),
0043       captionSize       (4),
0044       currentPreviewPage(0),
0045       currentCropPhoto  (0),
0046       disableCrop       (false),
0047       imageFormat       (JPEG),
0048       conflictRule      (FileSaveConflictBox::OVERWRITE),
0049       openInFileBrowser (true),
0050       outputLayouts     (nullptr),
0051       outputPrinter     (nullptr)
0052 {
0053 }
0054 
0055 AdvPrintSettings::~AdvPrintSettings()
0056 {
0057     for (int i = 0 ; i < photos.count() ; ++i)
0058     {
0059         delete photos.at(i);
0060     }
0061 
0062     photos.clear();
0063 }
0064 
0065 void AdvPrintSettings::readSettings(KConfigGroup& group)
0066 {
0067     selMode           = (Selection)group.readEntry("SelMode",
0068                         (int)IMAGES);
0069     imageFormat       = (ImageFormat)group.readEntry("ImageFormat",
0070                         (int)JPEG);
0071     savedPhotoSize    = group.readEntry("PhotoSize",
0072                         QString());
0073     printerName       = group.readEntry("Printer",
0074                         outputName(FILES));
0075     captionType       = (CaptionType)group.readEntry(QLatin1String("CaptionType"),
0076                         (int)NONE);
0077     captionColor      = group.readEntry(QLatin1String("CaptionColor"),
0078                         QColor(Qt::yellow));
0079     captionFont       = group.readEntry(QLatin1String("CaptionFont"),
0080                         QFont(QLatin1String("Sans Serif")));
0081     captionSize       = group.readEntry(QLatin1String("CaptionSize"),
0082                         4);
0083     captionTxt        = group.readEntry(QLatin1String("CustomCaption"),
0084                         QString());
0085     outputDir         = group.readEntry("OutputPath",
0086                         QUrl::fromLocalFile(QStandardPaths::writableLocation
0087                             (QStandardPaths::DocumentsLocation)));
0088     conflictRule      = (FileSaveConflictBox::ConflictRule)group.readEntry("ConflictRule",
0089                         (int)FileSaveConflictBox::OVERWRITE);
0090     openInFileBrowser = group.readEntry("OpenInFileBrowser",
0091                         true);
0092     imageFormat       = (ImageFormat)group.readEntry("ImageFormat",
0093                         (int)JPEG);
0094 }
0095 
0096 void AdvPrintSettings::writeSettings(KConfigGroup& group)
0097 {
0098     group.writeEntry("SelMode",           (int)selMode);
0099     group.writeEntry("ImageFormat",       (int)imageFormat);
0100     group.writeEntry("PhotoSize",         savedPhotoSize);
0101     group.writeEntry("Printer",           printerName);
0102     group.writeEntry("CaptionType",       (int)captionType);
0103     group.writeEntry("CaptionColor",      captionColor);
0104     group.writeEntry("CaptionFont",       captionFont);
0105     group.writeEntry("CaptionSize",       captionSize);
0106     group.writeEntry("CustomCaption",     captionTxt);
0107     group.writeEntry("OutputPath",        outputDir);
0108     group.writeEntry("ConflictRule",      (int)conflictRule);
0109     group.writeEntry("OpenInFileBrowser", openInFileBrowser);
0110     group.writeEntry("ImageFormat",       (int)imageFormat);
0111 }
0112 
0113 QString AdvPrintSettings::outputName(Output out) const
0114 {
0115     QMap<Output, QString> outputs = outputNames();
0116 
0117     if (outputs.contains(out))
0118     {
0119         return outputs[out];
0120     }
0121 
0122     return QString();
0123 }
0124 
0125 QMap<AdvPrintSettings::Output, QString> AdvPrintSettings::outputNames()
0126 {
0127     QMap<Output, QString> out;
0128 
0129     out[FILES] = i18nc("Output: FILE", "Print to Image File");
0130 
0131 #ifndef Q_OS_WIN
0132 
0133     out[PDF]   = i18nc("Output: PDF",  "Print to PDF");
0134 
0135 #endif
0136 
0137     out[GIMP]  = i18nc("Output: GIMP", "Print with Gimp");
0138 
0139     return out;
0140 }
0141 
0142 QString AdvPrintSettings::format() const
0143 {
0144     if      (imageFormat == JPEG)
0145     {
0146         return QLatin1String("JPEG");
0147     }
0148     else if (imageFormat == TIFF)
0149     {
0150         return QLatin1String("TIF");
0151     }
0152 
0153     return QLatin1String("PNG");
0154 }
0155 
0156 QMap<AdvPrintSettings::ImageFormat, QString> AdvPrintSettings::imageFormatNames()
0157 {
0158     QMap<ImageFormat, QString> frms;
0159 
0160     frms[JPEG] = i18nc("Image format: JPEG", "JPEG");
0161     frms[PNG]  = i18nc("Image format: PNG",  "PNG");
0162     frms[TIFF] = i18nc("Image format: TIFF", "TIFF");
0163 
0164     return frms;
0165 }
0166 
0167 QMap<AdvPrintSettings::CaptionType, QString> AdvPrintSettings::captionTypeNames()
0168 {
0169     QMap<CaptionType, QString> types;
0170 
0171     types[NONE]     = i18nc("Caption type: NONE",      "No caption");
0172     types[FILENAME] = i18nc("Caption type: FILENAME",  "Image file names");
0173     types[DATETIME] = i18nc("Caption type: DATETIME",  "Exif date-time");
0174     types[COMMENT]  = i18nc("Caption type: COMMENT",   "Item comments");
0175     types[CUSTOM]   = i18nc("Caption type: CUSTOM",    "Custom format");
0176 
0177     return types;
0178 }
0179 
0180 QRect* AdvPrintSettings::getLayout(int photoIndex, int sizeIndex) const
0181 {
0182     AdvPrintPhotoSize* const s = photosizes.at(sizeIndex);
0183 
0184     // how many photos would actually be printed, including copies?
0185 
0186     int photoCount             = (photoIndex + 1);
0187 
0188     // how many pages?  Recall that the first layout item is the paper size
0189 
0190     int photosPerPage          = s->m_layouts.count() - 1;
0191     int remainder              = photoCount % photosPerPage;
0192     int retVal                 = (remainder == 0) ? photosPerPage : remainder;
0193 
0194     return s->m_layouts.at(retVal);
0195 }
0196 
0197 } // namespace DigikamGenericPrintCreatorPlugin