File indexing completed on 2025-03-09 03:50:51

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-11-13
0007  * Description : a tool to blend bracketed images.
0008  *
0009  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2015      by Benjamin Girault <benjamin dot girault at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "expoblendingpreprocesspage.h"
0017 
0018 // Qt includes
0019 
0020 #include <QDir>
0021 #include <QLabel>
0022 #include <QVBoxLayout>
0023 #include <QTimer>
0024 #include <QPixmap>
0025 #include <QPushButton>
0026 #include <QCheckBox>
0027 #include <QStyle>
0028 #include <QApplication>
0029 #include <QTextBrowser>
0030 
0031 // KDE includes
0032 
0033 #include <klocalizedstring.h>
0034 #include <ksharedconfig.h>
0035 #include <kconfiggroup.h>
0036 
0037 // Local includes
0038 
0039 #include "digikam_debug.h"
0040 #include "alignbinary.h"
0041 #include "expoblendingmanager.h"
0042 #include "expoblendingthread.h"
0043 #include "dlayoutbox.h"
0044 #include "dworkingpixmap.h"
0045 
0046 namespace DigikamGenericExpoBlendingPlugin
0047 {
0048 
0049 class Q_DECL_HIDDEN ExpoBlendingPreProcessPage::Private
0050 {
0051 public:
0052 
0053     explicit Private()
0054      :  progressCount (0),
0055         progressLabel (nullptr),
0056         progressTimer (nullptr),
0057         title         (nullptr),
0058         alignCheckBox (nullptr),
0059         detailsText   (nullptr),
0060         progressPix   (nullptr),
0061         mngr          (nullptr)
0062     {
0063     }
0064 
0065     int                  progressCount;
0066     QLabel*              progressLabel;
0067     QTimer*              progressTimer;
0068 
0069     QLabel*              title;
0070 
0071     QCheckBox*           alignCheckBox;
0072 
0073     QTextBrowser*        detailsText;
0074 
0075     DWorkingPixmap*      progressPix;
0076 
0077     ExpoBlendingManager* mngr;
0078 };
0079 
0080 ExpoBlendingPreProcessPage::ExpoBlendingPreProcessPage(ExpoBlendingManager* const mngr, QWizard* const dlg)
0081     : DWizardPage(dlg, QString::fromLatin1("<b>%1</b>").arg(i18nc("@title: window", "Pre-Processing Bracketed Images"))),
0082       d          (new Private)
0083 {
0084     d->mngr                 = mngr;
0085     d->progressTimer        = new QTimer(this);
0086     d->progressPix          = new DWorkingPixmap(this);
0087     DVBox* const vbox       = new DVBox(this);
0088     d->title                = new QLabel(vbox);
0089     d->title->setWordWrap(true);
0090     d->title->setOpenExternalLinks(true);
0091 
0092     KSharedConfigPtr config = KSharedConfig::openConfig();
0093     KConfigGroup group      = config->group(QLatin1String("ExpoBlending Settings"));
0094     d->alignCheckBox        = new QCheckBox(i18nc("@option: check", "Align bracketed images"), vbox);
0095     d->alignCheckBox->setChecked(group.readEntry("Auto Alignment", true));
0096 
0097     vbox->setStretchFactor(new QWidget(vbox), 2);
0098 
0099     d->detailsText          = new QTextBrowser(vbox);
0100     d->detailsText->hide();
0101 
0102     vbox->setStretchFactor(new QWidget(vbox), 2);
0103 
0104     d->progressLabel        = new QLabel(vbox);
0105     d->progressLabel->setAlignment(Qt::AlignCenter);
0106 
0107     vbox->setStretchFactor(new QWidget(vbox), 10);
0108 
0109     setPageWidget(vbox);
0110 
0111     resetTitle();
0112 
0113     QPixmap leftPix(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("digikam/data/assistant-preprocessing.png")));
0114     setLeftBottomPix(leftPix.scaledToWidth(128, Qt::SmoothTransformation));
0115 
0116     connect(d->mngr->thread(), SIGNAL(starting(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)),
0117             this, SLOT(slotExpoBlendingAction(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)));
0118 
0119     connect(d->progressTimer, SIGNAL(timeout()),
0120             this, SLOT(slotProgressTimerDone()));
0121 }
0122 
0123 ExpoBlendingPreProcessPage::~ExpoBlendingPreProcessPage()
0124 {
0125     KSharedConfigPtr config = KSharedConfig::openConfig();
0126     KConfigGroup group      = config->group(QLatin1String("ExpoBlending Settings"));
0127     group.writeEntry("Auto Alignment", d->alignCheckBox->isChecked());
0128     config->sync();
0129 
0130     delete d;
0131 }
0132 
0133 void ExpoBlendingPreProcessPage::resetTitle()
0134 {
0135     d->title->setText(QString::fromUtf8("<qt>"
0136                                         "<p>%1</p>"
0137                                         "<p>%2</p>"
0138                                         "<p>%3</p>"
0139                                         "<p>%4</p>"
0140                                         "</qt>")
0141                       .arg(i18nc("@info", "Now, we will pre-process bracketed images before fusing them."))
0142                       .arg(i18nc("@info", "Alignment must be performed if you have not used a tripod to take bracketed images. Alignment operations can take a while."))
0143                       .arg(i18nc("@info", "Pre-processing operations include Raw demosaicing. Raw images will be converted to 16-bit sRGB images with auto-gamma."))
0144                       .arg(i18nc("@info", "Press the \"Next\" button to start pre-processing.")));
0145 
0146     d->detailsText->hide();
0147     d->alignCheckBox->show();
0148 }
0149 
0150 void ExpoBlendingPreProcessPage::process()
0151 {
0152     d->title->setText(QString::fromUtf8("<qt>"
0153                                         "<p>%1</p>"
0154                                         "<p>%2</p>"
0155                                         "</qt>")
0156                                         .arg(i18nc("@info", "Pre-processing is in progress, please wait."))
0157                                         .arg(i18nc("@info", "This can take a while...")));
0158 
0159     d->alignCheckBox->hide();
0160     d->progressTimer->start(300);
0161 
0162     connect(d->mngr->thread(), SIGNAL(finished(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)),
0163             this, SLOT(slotExpoBlendingAction(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)));
0164 
0165     d->mngr->thread()->setPreProcessingSettings(d->alignCheckBox->isChecked());
0166     d->mngr->thread()->preProcessFiles(d->mngr->itemsList(), d->mngr->alignBinary().path());
0167 
0168     if (!d->mngr->thread()->isRunning())
0169     {
0170         d->mngr->thread()->start();
0171     }
0172 }
0173 
0174 void ExpoBlendingPreProcessPage::cancel()
0175 {
0176     disconnect(d->mngr->thread(), SIGNAL(finished(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)),
0177                this, SLOT(slotExpoBlendingAction(DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData)));
0178 
0179     d->mngr->thread()->cancel();
0180     d->progressTimer->stop();
0181     d->progressLabel->clear();
0182     resetTitle();
0183 }
0184 
0185 void ExpoBlendingPreProcessPage::slotProgressTimerDone()
0186 {
0187     d->progressLabel->setPixmap(d->progressPix->frameAt(d->progressCount));
0188 
0189     d->progressCount++;
0190 
0191     if (d->progressCount == 8)
0192     {
0193         d->progressCount = 0;
0194     }
0195 
0196     d->progressTimer->start(300);
0197 }
0198 
0199 void ExpoBlendingPreProcessPage::slotExpoBlendingAction(const DigikamGenericExpoBlendingPlugin::ExpoBlendingActionData& ad)
0200 {
0201     QString text;
0202 
0203     if (!ad.starting)           // Something is complete...
0204     {
0205         if (!ad.success)        // Something is failed...
0206         {
0207             switch (ad.action)
0208             {
0209                 case(EXPOBLENDING_PREPROCESSING):
0210                 {
0211                     d->title->setText(QString::fromUtf8("<qt>"
0212                                                         "<p>%1</p>"
0213                                                         "<p>%2</p>"
0214                                                         "<p>%3</p>"
0215                                                         "</qt>")
0216                                                         .arg(i18nc("@info", "Pre-processing has failed."))
0217                                                         .arg(i18nc("@info", "Please check your bracketed images stack..."))
0218                                                         .arg(i18nc("@info", "See processing messages below.")));
0219 
0220                     d->progressTimer->stop();
0221                     d->alignCheckBox->hide();
0222                     d->detailsText->show();
0223                     d->progressLabel->clear();
0224                     d->detailsText->setText(ad.message);
0225                     Q_EMIT signalPreProcessed(ExpoBlendingItemUrlsMap());
0226                     break;
0227                 }
0228 
0229                 default:
0230                 {
0231                     qCWarning(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Unknown action";
0232                     break;
0233                 }
0234             }
0235         }
0236         else                    // Something is done...
0237         {
0238             switch (ad.action)
0239             {
0240                 case(EXPOBLENDING_PREPROCESSING):
0241                 {
0242                     d->progressTimer->stop();
0243                     d->progressLabel->clear();
0244                     Q_EMIT signalPreProcessed(ad.preProcessedUrlsMap);
0245                     break;
0246                 }
0247 
0248                 default:
0249                 {
0250                     qCWarning(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Unknown action";
0251                     break;
0252                 }
0253             }
0254         }
0255     }
0256 }
0257 
0258 } // namespace DigikamGenericExpoBlendingPlugin
0259 
0260 #include "moc_expoblendingpreprocesspage.cpp"