File indexing completed on 2025-01-05 04:00:11

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-02-06
0007  * Description : setup RAW decoding settings.
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 "setupraw.h"
0016 
0017 // Qt includes
0018 
0019 #include <QGridLayout>
0020 #include <QGroupBox>
0021 #include <QComboBox>
0022 #include <QRadioButton>
0023 #include <QVBoxLayout>
0024 #include <QTabWidget>
0025 #include <QIcon>
0026 #include <QPointer>
0027 #include <QStandardItemModel>
0028 
0029 // KDE includes
0030 
0031 #include <kconfiggroup.h>
0032 #include <ksharedconfig.h>
0033 #include <klocalizedstring.h>
0034 
0035 // Local includes
0036 
0037 #include "drawdecoding.h"
0038 #include "drawdecoderwidget.h"
0039 #include "dpluginloader.h"
0040 #include "dpluginrawimport.h"
0041 #include "dpluginaboutdlg.h"
0042 #include "setupeditor.h"
0043 
0044 namespace Digikam
0045 {
0046 
0047 class Q_DECL_HIDDEN SetupRaw::Private
0048 {
0049 public:
0050 
0051     explicit Private()
0052       : tab             (nullptr),
0053         behaviorPanel   (nullptr),
0054         settingsPanel   (nullptr),
0055         openSimple      (nullptr),
0056         openDefault     (nullptr),
0057         openTool        (nullptr),
0058         rawImportTool   (nullptr),
0059         toolAbout       (nullptr),
0060         rawSettings     (nullptr)
0061     {
0062     }
0063 
0064     static const QString  configGroupName;
0065     static const QString  configUseRawImportToolEntry;
0066     static const QString  configRawImportToolIidEntry;
0067     static const QString  nativeRawImportToolIid;
0068 
0069     QTabWidget*           tab;
0070 
0071     QWidget*              behaviorPanel;
0072     QWidget*              settingsPanel;
0073 
0074     QRadioButton*         openSimple;
0075     QRadioButton*         openDefault;
0076     QRadioButton*         openTool;
0077 
0078     QComboBox*            rawImportTool;
0079     QPushButton*          toolAbout;
0080 
0081     DRawDecoderWidget*    rawSettings;
0082 };
0083 
0084 const QString SetupRaw::Private::configGroupName(QLatin1String("ImageViewer Settings"));
0085 const QString SetupRaw::Private::configUseRawImportToolEntry(QLatin1String("UseRawImportTool"));
0086 const QString SetupRaw::Private::configRawImportToolIidEntry(QLatin1String("RawImportToolIid"));
0087 const QString SetupRaw::Private::nativeRawImportToolIid(QLatin1String("org.kde.digikam.plugin.rawimport.Native"));
0088 
0089 SetupRaw::SetupRaw(QTabWidget* const tab)
0090     : QObject(tab),
0091       d      (new Private)
0092 {
0093     d->tab = tab;
0094 
0095     // --------------------------------------------------------
0096 
0097     d->behaviorPanel                  = new QWidget;
0098     QVBoxLayout* const behaviorLayout = new QVBoxLayout;
0099 
0100     QLabel* const rawExplanation      = new QLabel;
0101     rawExplanation->setText(i18nc("@info",
0102                                   "A raw image file contains minimally processed data "
0103                                   "from the image sensor of a digital camera.\n"
0104                                   "Opening a raw file requires extensive data interpretation and processing."));
0105     rawExplanation->setWordWrap(true);
0106     QLabel* const rawIcon        = new QLabel;
0107     rawIcon->setPixmap(QIcon::fromTheme(QLatin1String("camera-photo")).pixmap(48));
0108     QHBoxLayout* const header    = new QHBoxLayout;
0109     header->addWidget(rawIcon);
0110     header->addWidget(rawExplanation);
0111     header->setStretchFactor(rawExplanation, 10);
0112     header->addStretch(1);
0113 
0114     QGroupBox* const behaviorBox = new QGroupBox;
0115     QGridLayout* const boxLayout = new QGridLayout;
0116 
0117     QLabel* const openIcon       = new QLabel;
0118     openIcon->setPixmap(QIcon::fromTheme(QLatin1String("document-open")).pixmap(32));
0119 
0120     QLabel* const openIntro      = new QLabel(i18nc("@label", "Open raw files in the image editor"));
0121 
0122     d->openSimple  = new QRadioButton(i18nc("@option:radio Open raw files...",
0123                                             "Fast and simple, as 8 bit image"));
0124     d->openDefault = new QRadioButton(i18nc("@option:radio Open raw files...",
0125                                             "Using the default settings, in 16 bit"));
0126     d->openTool    = new QRadioButton(i18nc("@option:radio Open raw files...",
0127                                             "Always open the Raw Import Tool to customize settings"));
0128 
0129     d->rawImportTool = new QComboBox;
0130 
0131     Q_FOREACH (DPlugin* const p, DPluginLoader::instance()->allPlugins())
0132     {
0133         DPluginRawImport* const raw = dynamic_cast<DPluginRawImport*>(p);
0134 
0135         if (raw)
0136         {
0137             QString iid  = raw->iid();
0138             QString name = raw->name();
0139 
0140             if      (iid == d->nativeRawImportToolIid)
0141             {
0142                 name += i18nc("@item: default raw plugin", " (default)");
0143                 d->rawImportTool->insertItem(0, name, iid);
0144             }
0145             else
0146             {
0147                 d->rawImportTool->addItem(name, iid);
0148 
0149                 if (raw->getRawProgram().isEmpty())
0150                 {
0151                     QStandardItemModel* const model =
0152                         qobject_cast<QStandardItemModel*>(d->rawImportTool->model());
0153 
0154                    if (model)
0155                    {
0156                         QStandardItem* const item = model->item(d->rawImportTool->count() - 1);
0157 
0158                         if (item)
0159                         {
0160                             item->setEnabled(false);
0161                         }
0162                    }
0163                 }
0164             }
0165         }
0166     }
0167 
0168     d->toolAbout = new QPushButton;
0169     d->toolAbout->setIcon(QIcon::fromTheme(QLatin1String("help-about")));
0170     d->toolAbout->setToolTip(i18nc("@info:tooltip", "About this Raw Import tool..."));
0171 
0172     // ----------------------------------------------
0173 
0174     boxLayout->addWidget(openIcon,         0, 0);
0175     boxLayout->addWidget(openIntro,        0, 1);
0176     boxLayout->addWidget(d->openSimple,    1, 0, 1, 4);
0177     boxLayout->addWidget(d->openDefault,   2, 0, 1, 4);
0178     boxLayout->addWidget(d->openTool,      3, 0, 1, 2);
0179     boxLayout->addWidget(d->rawImportTool, 3, 2, 1, 1);
0180     boxLayout->addWidget(d->toolAbout,     3, 3, 1, 1);
0181     boxLayout->setColumnStretch(2, 1);
0182     behaviorBox->setLayout(boxLayout);
0183 
0184     behaviorLayout->addLayout(header);
0185     behaviorLayout->addWidget(behaviorBox);
0186     behaviorLayout->addStretch();
0187     d->behaviorPanel->setLayout(behaviorLayout);
0188 
0189     // --------------------------------------------------------
0190 
0191     d->settingsPanel                  = new QWidget;
0192     QVBoxLayout* const settingsLayout = new QVBoxLayout;
0193 
0194     d->rawSettings                    = new DRawDecoderWidget(nullptr, 0 /* no advanced settings shown */);
0195     d->rawSettings->setItemIcon(0, QIcon::fromTheme(QLatin1String("image-x-adobe-dng")));
0196     d->rawSettings->setItemIcon(1, QIcon::fromTheme(QLatin1String("bordertool")));
0197     d->rawSettings->setItemIcon(2, QIcon::fromTheme(QLatin1String("lensdistortion")));
0198 
0199     settingsLayout->addWidget(d->rawSettings);
0200     d->settingsPanel->setLayout(settingsLayout);
0201 
0202     // --------------------------------------------------------
0203 
0204     d->tab->insertTab(SetupEditor::RAWBehavior,        d->behaviorPanel, i18nc("@title:tab", "RAW Behavior"));
0205     d->tab->insertTab(SetupEditor::RAWDefaultSettings, d->settingsPanel, i18nc("@title:tab", "RAW Default Settings"));
0206 
0207     // --------------------------------------------------------
0208 
0209     connect(d->openSimple, SIGNAL(toggled(bool)),
0210             this, SLOT(slotBehaviorChanged()));
0211 
0212     connect(d->openDefault, SIGNAL(toggled(bool)),
0213             this, SLOT(slotBehaviorChanged()));
0214 
0215     connect(d->openTool, SIGNAL(toggled(bool)),
0216             this, SLOT(slotBehaviorChanged()));
0217 
0218     connect(d->rawSettings, SIGNAL(signalSixteenBitsImageToggled(bool)),
0219             this, SLOT(slotSixteenBitsImageToggled(bool)));
0220 
0221     connect(d->toolAbout, SIGNAL(clicked()),
0222             this, SLOT(slotAboutRawImportPlugin()));
0223 
0224     // --------------------------------------------------------
0225 
0226     readSettings();
0227 }
0228 
0229 SetupRaw::~SetupRaw()
0230 {
0231     delete d;
0232 }
0233 
0234 void SetupRaw::slotSixteenBitsImageToggled(bool)
0235 {
0236     // Libraw provide a way to set brightness of image in 16 bits color depth.
0237     // We always set on this option. We drive brightness adjustment in digiKam Raw image loader.
0238 
0239     d->rawSettings->setEnabledBrightnessSettings(true);
0240 }
0241 
0242 void SetupRaw::slotBehaviorChanged()
0243 {
0244     d->rawImportTool->setEnabled(d->openTool->isChecked());
0245 
0246     DRawDecoderSettings settings = d->rawSettings->settings();
0247     settings.sixteenBitsImage    = !d->openSimple->isChecked();
0248     d->rawSettings->setSettings(settings);
0249 }
0250 
0251 void SetupRaw::applySettings()
0252 {
0253     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0254     KConfigGroup group        = config->group(d->configGroupName);
0255     group.writeEntry(d->configUseRawImportToolEntry, d->openTool->isChecked());
0256     group.writeEntry(d->configRawImportToolIidEntry, d->rawImportTool->itemData(d->rawImportTool->currentIndex()));
0257 
0258     d->rawSettings->writeSettings(group);
0259 
0260     config->sync();
0261 }
0262 
0263 void SetupRaw::readSettings()
0264 {
0265     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0266     KConfigGroup group        = config->group(d->configGroupName);
0267 
0268     d->rawSettings->readSettings(group);
0269 
0270     bool useTool = group.readEntry(d->configUseRawImportToolEntry, false);
0271 
0272     if (useTool)
0273     {
0274         d->openTool->setChecked(true);
0275     }
0276     else
0277     {
0278         if (d->rawSettings->settings().sixteenBitsImage)
0279         {
0280             d->openDefault->setChecked(true);
0281         }
0282         else
0283         {
0284             d->openSimple->setChecked(true);
0285         }
0286     }
0287 
0288     QString iid  = group.readEntry(d->configRawImportToolIidEntry, d->nativeRawImportToolIid);
0289     int rawIndex = d->rawImportTool->findData(iid);
0290 
0291     QStandardItemModel* const model =
0292         qobject_cast<QStandardItemModel*>(d->rawImportTool->model());
0293 
0294     if (model)
0295     {
0296         QStandardItem* const item = model->item(rawIndex);
0297 
0298         if (!item || !item->isEnabled())
0299         {
0300             rawIndex = d->rawImportTool->findData(d->nativeRawImportToolIid);
0301         }
0302     }
0303 
0304     d->rawImportTool->setCurrentIndex(rawIndex);
0305 }
0306 
0307 void SetupRaw::slotAboutRawImportPlugin()
0308 {
0309     QString iid = d->rawImportTool->itemData(d->rawImportTool->currentIndex()).toString();
0310 
0311     Q_FOREACH (DPlugin* const p, DPluginLoader::instance()->allPlugins())
0312     {
0313         DPluginRawImport* const raw = dynamic_cast<DPluginRawImport*>(p);
0314 
0315         if (raw)
0316         {
0317             if (raw->iid() == iid)
0318             {
0319                 QPointer<DPluginAboutDlg> dlg = new DPluginAboutDlg(p);
0320                 dlg->exec();
0321                 delete dlg;
0322             }
0323         }
0324     }
0325 }
0326 
0327 } // namespace Digikam
0328 
0329 #include "moc_setupraw.cpp"