File indexing completed on 2025-01-19 03:51:10

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-02-06
0007  * Description : image editor printing interface.
0008  *
0009  * SPDX-FileCopyrightText: 2009      by Angelo Naselli <anaselli at linux dot it>
0010  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "printoptionspage.h"
0017 
0018 // Qt includes
0019 
0020 #include <QButtonGroup>
0021 #include <QGridLayout>
0022 #include <QToolButton>
0023 #include <QPushButton>
0024 #include <QApplication>
0025 #include <QStyle>
0026 #include <QHBoxLayout>
0027 #include <QMessageBox>
0028 #include <QSignalBlocker>
0029 
0030 // Local includes
0031 
0032 #include "digikam_debug.h"
0033 #include "printconfig.h"
0034 #include "iccsettings.h"
0035 #include "iccsettingscontainer.h"
0036 #include "iccmanager.h"
0037 #include "editorwindow.h"
0038 #include "dconfigdlgmngr.h"
0039 #include "ui_printoptionspage.h"
0040 
0041 namespace DigikamEditorPrintToolPlugin
0042 {
0043 
0044 static inline double unitToInches(PrintOptionsPage::Unit unit)
0045 {
0046     if      (unit == PrintOptionsPage::Inches)
0047     {
0048         return 1.0;
0049     }
0050     else if (unit == PrintOptionsPage::Centimeters)
0051     {
0052         return (1 / 2.54);
0053     }
0054     else   // Millimeters
0055     {
0056         return (1 / 25.4);
0057     }
0058 }
0059 
0060 // --------------------------------------------------------------------------
0061 
0062 class Q_DECL_HIDDEN PrintOptionsPage::Private : public Ui_PrintOptionsPage
0063 {
0064 public:
0065 
0066     explicit Private()
0067       : parent              (nullptr),
0068         configDialogManager (nullptr),
0069         colorManaged        (nullptr),
0070         cmPreferences       (nullptr),
0071         cmEnabled           (false)
0072     {
0073     }
0074 
0075     QWidget*        parent;
0076     QSize           imageSize;
0077     QButtonGroup    scaleGroup;
0078     QButtonGroup    positionGroup;
0079     DConfigDlgMngr* configDialogManager;
0080     IccProfile      outputProfile;
0081     QCheckBox*      colorManaged;
0082     QPushButton*    cmPreferences;
0083     bool            cmEnabled;
0084 
0085 public:
0086 
0087     void initColorManagement()
0088     {
0089         QHBoxLayout* const hlay = new QHBoxLayout(cmbox);
0090         colorManaged            = new QCheckBox(i18n("Use Color Management for Printing"), cmbox);
0091         colorManaged->setChecked(false);
0092         cmPreferences           = new QPushButton(i18n("Settings..."), cmbox);
0093         QWidget* const space    = new QWidget(cmbox);
0094         hlay->addWidget(colorManaged);
0095         hlay->addWidget(cmPreferences);
0096         hlay->addWidget(space);
0097         hlay->setStretchFactor(space, 10);
0098         hlay->setSpacing(0);
0099     }
0100 
0101     void initPositionFrame()
0102     {
0103         mPositionFrame->setStyleSheet(QLatin1String(
0104             "QFrame {"
0105             " background-color: palette(mid);"
0106             " border: 1px solid palette(dark);"
0107             "}"
0108             "QToolButton {"
0109             " border: none;"
0110             " background: palette(base);"
0111             "}"
0112             "QToolButton:hover {"
0113             " background: palette(alternate-base);"
0114             " border: 1px solid palette(highlight);"
0115             "}"
0116             "QToolButton:checked {"
0117             " background-color: palette(highlight);"
0118             "}"
0119         ));
0120 
0121         QGridLayout* const layout = new QGridLayout(mPositionFrame);
0122         layout->setContentsMargins(QMargins());
0123         layout->setSpacing(1);
0124 
0125         for (int row = 0 ; row < 3 ; ++row)
0126         {
0127             for (int col = 0 ; col < 3 ; ++col)
0128             {
0129                 QToolButton* const button = new QToolButton(mPositionFrame);
0130                 button->setFixedSize(40, 40);
0131                 button->setCheckable(true);
0132                 layout->addWidget(button, row, col);
0133 
0134                 Qt::Alignment alignment;
0135 
0136                 if      (row == 0)
0137                 {
0138                     alignment = Qt::AlignTop;
0139                 }
0140                 else if (row == 1)
0141                 {
0142                     alignment = Qt::AlignVCenter;
0143                 }
0144                 else
0145                 {
0146                     alignment = Qt::AlignBottom;
0147                 }
0148 
0149                 if      (col == 0)
0150                 {
0151                     alignment |= Qt::AlignLeft;
0152                 }
0153                 else if (col == 1)
0154                 {
0155                     alignment |= Qt::AlignHCenter;
0156                 }
0157                 else
0158                 {
0159                     alignment |= Qt::AlignRight;
0160                 }
0161 
0162                 positionGroup.addButton(button, int (alignment));
0163             }
0164         }
0165     }
0166 };
0167 
0168 PrintOptionsPage::PrintOptionsPage(QWidget* const parent,
0169                                    const QSize& imageSize)
0170     : QWidget(),
0171       d      (new Private)
0172 {
0173     d->setupUi(this);
0174     d->parent              = parent;
0175     d->imageSize           = imageSize;
0176     d->configDialogManager = new DConfigDlgMngr(this, PrintConfig::self());
0177 
0178     d->initPositionFrame();
0179 
0180     // Color management support
0181 
0182     d->initColorManagement();
0183 
0184     d->scaleGroup.addButton(d->mNoScale,     NoScale);
0185     d->scaleGroup.addButton(d->mScaleToPage, ScaleToPage);
0186     d->scaleGroup.addButton(d->mScaleTo,     ScaleToCustomSize);
0187 
0188     connect(d->kcfg_PrintWidth, SIGNAL(valueChanged(double)),
0189             this, SLOT(adjustHeightToRatio()));
0190 
0191     connect(d->kcfg_PrintHeight, SIGNAL(valueChanged(double)),
0192             this, SLOT(adjustWidthToRatio()));
0193 
0194     connect(d->kcfg_PrintKeepRatio, SIGNAL(toggled(bool)),
0195             this, SLOT(adjustHeightToRatio()));
0196 
0197     connect(d->colorManaged, SIGNAL(toggled(bool)),
0198             this, SLOT(slotAlertSettings(bool)));
0199 
0200     connect(d->cmPreferences, SIGNAL(clicked()),
0201             this, SLOT(slotSetupDlg()));
0202 }
0203 
0204 PrintOptionsPage::~PrintOptionsPage()
0205 {
0206     delete d;
0207 }
0208 
0209 bool PrintOptionsPage::colorManaged() const
0210 {
0211     return d->colorManaged->isChecked();
0212 }
0213 
0214 bool PrintOptionsPage::autoRotation() const
0215 {
0216     return d->kcfg_PrintAutoRotate->isChecked();
0217 }
0218 
0219 IccProfile PrintOptionsPage::outputProfile() const
0220 {
0221     return d->outputProfile;
0222 }
0223 
0224 Qt::Alignment PrintOptionsPage::alignment() const
0225 {
0226     int id = d->positionGroup.checkedId();
0227     qCWarning(DIGIKAM_DPLUGIN_EDITOR_LOG) << "alignment=" << id;
0228 
0229     return Qt::Alignment(id);
0230 }
0231 
0232 PrintOptionsPage::ScaleMode PrintOptionsPage::scaleMode() const
0233 {
0234     return PrintOptionsPage::ScaleMode(d->scaleGroup.checkedId());
0235 }
0236 
0237 bool PrintOptionsPage::enlargeSmallerImages() const
0238 {
0239     return d->kcfg_PrintEnlargeSmallerImages->isChecked();
0240 }
0241 
0242 PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const
0243 {
0244     return PrintOptionsPage::Unit(d->kcfg_PrintUnit->currentIndex());
0245 }
0246 
0247 double PrintOptionsPage::scaleWidth() const
0248 {
0249     return d->kcfg_PrintWidth->value() * unitToInches(scaleUnit());
0250 }
0251 
0252 double PrintOptionsPage::scaleHeight() const
0253 {
0254     return d->kcfg_PrintHeight->value() * unitToInches(scaleUnit());
0255 }
0256 
0257 void PrintOptionsPage::adjustWidthToRatio()
0258 {
0259     if (!d->kcfg_PrintKeepRatio->isChecked())
0260     {
0261         return;
0262     }
0263 
0264     double width = d->imageSize.width() * d->kcfg_PrintHeight->value() /
0265                    d->imageSize.height();
0266 
0267     const QSignalBlocker blocker(d->kcfg_PrintWidth);
0268     d->kcfg_PrintWidth->setValue(width ? width : 1.0);
0269 }
0270 
0271 void PrintOptionsPage::adjustHeightToRatio()
0272 {
0273     if (!d->kcfg_PrintKeepRatio->isChecked())
0274     {
0275         return;
0276     }
0277 
0278     double height = d->imageSize.height() * d->kcfg_PrintWidth->value() /
0279                     d->imageSize.width();
0280 
0281     const QSignalBlocker blocker(d->kcfg_PrintHeight);
0282     d->kcfg_PrintHeight->setValue(height ? height : 1.0);
0283 }
0284 
0285 void PrintOptionsPage::loadConfig()
0286 {
0287     QAbstractButton* button = d->positionGroup.button(PrintConfig::printPosition());
0288 
0289     if (button)
0290     {
0291         button->setChecked(true);
0292     }
0293     else
0294     {
0295         qCWarning(DIGIKAM_DPLUGIN_EDITOR_LOG) << "Unknown button for position group";
0296     }
0297 
0298     button = d->scaleGroup.button(PrintConfig::printScaleMode());
0299 
0300     if (button)
0301     {
0302         button->setChecked(true);
0303     }
0304     else
0305     {
0306         qCWarning(DIGIKAM_DPLUGIN_EDITOR_LOG) << "Unknown button for scale group";
0307     }
0308 
0309     d->configDialogManager->updateWidgets();
0310 
0311     if (d->kcfg_PrintKeepRatio->isChecked())
0312     {
0313         adjustHeightToRatio();
0314     }
0315 
0316     d->colorManaged->setChecked(PrintConfig::printColorManaged());
0317     ICCSettingsContainer settings = IccSettings::instance()->settings();
0318     d->outputProfile              = IccProfile(settings.defaultProofProfile);
0319     d->cmEnabled                  = settings.enableCM;
0320 }
0321 
0322 void PrintOptionsPage::saveConfig()
0323 {
0324     int position        = d->positionGroup.checkedId();
0325     PrintConfig::setPrintPosition(position);
0326 
0327     ScaleMode scaleMode = ScaleMode(d->scaleGroup.checkedId());
0328     PrintConfig::setPrintScaleMode(scaleMode);
0329 
0330     d->configDialogManager->updateSettings();
0331 
0332     PrintConfig::self()->save();
0333 }
0334 
0335 void PrintOptionsPage::slotAlertSettings(bool t)
0336 {
0337     if (t && !d->cmEnabled)
0338     {
0339         QString message = i18n("<p>Color Management is disabled.</p> "
0340                                "<p>You can enable it now by clicking "
0341                                "on the \"Settings\" button.</p>");
0342         QMessageBox::information(this, qApp->applicationName(), message);
0343         d->colorManaged->setChecked(false);
0344     }
0345 }
0346 
0347 void PrintOptionsPage::slotSetupDlg()
0348 {
0349     EditorWindow* const editor = dynamic_cast<EditorWindow*>(d->parent);
0350 
0351     if (editor)
0352     {
0353         editor->slotSetupICC();
0354     }
0355 }
0356 
0357 } // namespace DigiKam
0358 
0359 #include "moc_printoptionspage.cpp"