File indexing completed on 2025-01-19 03:50:57
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2022-09-23 0007 * Description : assign labels metadata batch tool. 0008 * 0009 * SPDX-FileCopyrightText: 2021-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 "assignlabels.h" 0016 0017 // Qt includes 0018 0019 #include <QApplication> 0020 #include <QWidget> 0021 #include <QLabel> 0022 #include <QStyle> 0023 #include <QLayout> 0024 #include <QCheckBox> 0025 0026 // KDE includes 0027 0028 #include <klocalizedstring.h> 0029 0030 // Local includes 0031 0032 #include "digikam_debug.h" 0033 #include "digikam_globals.h" 0034 #include "dimg.h" 0035 #include "dinfointerface.h" 0036 #include "dmetadata.h" 0037 #include "dpluginbqm.h" 0038 #include "ratingwidget.h" 0039 #include "colorlabelwidget.h" 0040 #include "picklabelwidget.h" 0041 #include "dfileoperations.h" 0042 #include "dlayoutbox.h" 0043 0044 namespace DigikamBqmAssignLabelsPlugin 0045 { 0046 0047 class Q_DECL_HIDDEN AssignLabels::Private 0048 { 0049 public: 0050 0051 explicit Private() 0052 : setRating (nullptr), 0053 ratingWidget (nullptr), 0054 setColor (nullptr), 0055 colorLabelSelector(nullptr), 0056 setPick (nullptr), 0057 pickLabelSelector (nullptr), 0058 changeSettings (true) 0059 { 0060 } 0061 0062 QCheckBox* setRating; 0063 RatingWidget* ratingWidget; 0064 0065 QCheckBox* setColor; 0066 ColorLabelSelector* colorLabelSelector; 0067 0068 QCheckBox* setPick; 0069 PickLabelSelector* pickLabelSelector; 0070 0071 bool changeSettings; 0072 }; 0073 0074 AssignLabels::AssignLabels(QObject* const parent) 0075 : BatchTool(QLatin1String("AssignLabels"), MetadataTool, parent), 0076 d (new Private) 0077 { 0078 } 0079 0080 AssignLabels::~AssignLabels() 0081 { 0082 delete d; 0083 } 0084 0085 BatchTool* AssignLabels::clone(QObject* const parent) const 0086 { 0087 return new AssignLabels(parent); 0088 } 0089 0090 void AssignLabels::registerSettingsWidget() 0091 { 0092 const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing), 0093 QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)); 0094 0095 DVBox* const vbox = new DVBox; 0096 0097 DHBox* const pickBox = new DHBox(vbox); 0098 d->setPick = new QCheckBox(i18n("Pick Label:"), pickBox); 0099 d->pickLabelSelector = new PickLabelSelector(pickBox); 0100 pickBox->layout()->setAlignment(d->pickLabelSelector, Qt::AlignVCenter | Qt::AlignRight); 0101 0102 DHBox* const colorBox = new DHBox(vbox); 0103 d->setColor = new QCheckBox(i18n("Color Label:"), colorBox); 0104 d->colorLabelSelector = new ColorLabelSelector(colorBox); 0105 colorBox->layout()->setAlignment(d->colorLabelSelector, Qt::AlignVCenter | Qt::AlignRight); 0106 0107 DHBox* const rateBox = new DHBox(vbox); 0108 d->setRating = new QCheckBox(i18n("Rating:"), rateBox); 0109 d->ratingWidget = new RatingWidget(rateBox); 0110 rateBox->layout()->setAlignment(d->ratingWidget, Qt::AlignVCenter | Qt::AlignRight); 0111 0112 QWidget* const space = new QWidget(vbox); 0113 vbox->setStretchFactor(space, 10); 0114 vbox->setContentsMargins(spacing, spacing, spacing, spacing); 0115 0116 m_settingsWidget = vbox; 0117 0118 connect(d->setPick, SIGNAL(toggled(bool)), 0119 this, SLOT(slotSettingsChanged())); 0120 0121 connect(d->pickLabelSelector, SIGNAL(signalPickLabelChanged(int)), 0122 this, SLOT(slotSettingsChanged())); 0123 0124 connect(d->setColor, SIGNAL(toggled(bool)), 0125 this, SLOT(slotSettingsChanged())); 0126 0127 connect(d->colorLabelSelector, SIGNAL(signalColorLabelChanged(int)), 0128 this, SLOT(slotSettingsChanged())); 0129 0130 connect(d->setRating, SIGNAL(toggled(bool)), 0131 this, SLOT(slotSettingsChanged())); 0132 0133 connect(d->ratingWidget, SIGNAL(signalRatingChanged(int)), 0134 this, SLOT(slotSettingsChanged())); 0135 0136 BatchTool::registerSettingsWidget(); 0137 } 0138 0139 BatchToolSettings AssignLabels::defaultSettings() 0140 { 0141 BatchToolSettings settings; 0142 0143 settings.insert(QLatin1String("SetPick"), false); 0144 settings.insert(QLatin1String("PickLabel"), (int)NoPickLabel); 0145 settings.insert(QLatin1String("SetColor"), false); 0146 settings.insert(QLatin1String("ColorLabel"), (int)NoColorLabel); 0147 settings.insert(QLatin1String("SetRating"), false); 0148 settings.insert(QLatin1String("RatingValue"), (int)NoRating); 0149 0150 return settings; 0151 } 0152 0153 void AssignLabels::slotAssignSettings2Widget() 0154 { 0155 d->changeSettings = false; 0156 0157 bool setPick = settings()[QLatin1String("SetPick")].toBool(); 0158 PickLabel pick = (PickLabel)settings()[QLatin1String("PickLabel")].toInt(); 0159 bool setColor = settings()[QLatin1String("SetColor")].toBool(); 0160 ColorLabel color = (ColorLabel)settings()[QLatin1String("ColorLabel")].toInt(); 0161 bool setRating = settings()[QLatin1String("SetRating")].toBool(); 0162 int rating = settings()[QLatin1String("RatingValue")].toInt(); 0163 0164 d->setPick->setChecked(setPick); 0165 d->pickLabelSelector->setPickLabel(pick); 0166 d->setColor->setChecked(setColor); 0167 d->colorLabelSelector->setColorLabel(color); 0168 d->setRating->setChecked(setRating); 0169 d->ratingWidget->setRating(rating); 0170 0171 d->changeSettings = true; 0172 } 0173 0174 void AssignLabels::slotSettingsChanged() 0175 { 0176 if (d->changeSettings) 0177 { 0178 BatchToolSettings settings; 0179 0180 settings.insert(QLatin1String("SetPick"), d->setPick->isChecked()); 0181 settings.insert(QLatin1String("PickLabel"), (int)d->pickLabelSelector->pickLabel()); 0182 settings.insert(QLatin1String("SetColor"), d->setColor->isChecked()); 0183 settings.insert(QLatin1String("ColorLabel"), (int)d->colorLabelSelector->colorLabel()); 0184 settings.insert(QLatin1String("SetRating"), d->setRating->isChecked()); 0185 settings.insert(QLatin1String("RatingValue"), d->ratingWidget->rating()); 0186 0187 BatchTool::slotSettingsChanged(settings); 0188 } 0189 } 0190 0191 bool AssignLabels::toolOperations() 0192 { 0193 bool ret = true; 0194 QScopedPointer<DMetadata> meta(new DMetadata); 0195 0196 if (image().isNull()) 0197 { 0198 if (!meta->load(inputUrl().toLocalFile())) 0199 { 0200 return false; 0201 } 0202 } 0203 else 0204 { 0205 meta->setData(image().getMetadata()); 0206 } 0207 0208 bool setPick = settings()[QLatin1String("SetPick")].toBool(); 0209 PickLabel pick = (PickLabel)settings()[QLatin1String("PickLabel")].toInt(); 0210 bool setColor = settings()[QLatin1String("SetColor")].toBool(); 0211 ColorLabel color = (ColorLabel)settings()[QLatin1String("ColorLabel")].toInt(); 0212 bool setRating = settings()[QLatin1String("SetRating")].toBool(); 0213 int rating = settings()[QLatin1String("RatingValue")].toInt(); 0214 0215 if (setPick) 0216 { 0217 meta->setItemPickLabel(pick); 0218 qCDebug(DIGIKAM_DPLUGIN_BQM_LOG) << "Assign Picklabel:" << pick; 0219 } 0220 0221 if (setColor) 0222 { 0223 meta->setItemColorLabel(color); 0224 qCDebug(DIGIKAM_DPLUGIN_BQM_LOG) << "Assign Colorlabel:" << color; 0225 } 0226 0227 if (setRating) 0228 { 0229 meta->setItemRating(rating); 0230 qCDebug(DIGIKAM_DPLUGIN_BQM_LOG) << "Assign Rating:" << rating; 0231 } 0232 0233 if (image().isNull()) 0234 { 0235 QFile::remove(outputUrl().toLocalFile()); 0236 ret &= DFileOperations::copyFile(inputUrl().toLocalFile(), outputUrl().toLocalFile()); 0237 0238 if (ret) 0239 { 0240 ret &= meta->save(outputUrl().toLocalFile()); 0241 qCDebug(DIGIKAM_DPLUGIN_BQM_LOG) << "Save metadata to file:" << ret; 0242 } 0243 } 0244 else 0245 { 0246 qCDebug(DIGIKAM_DPLUGIN_BQM_LOG) << "Save metadata to image"; 0247 image().setMetadata(meta->data()); 0248 ret &= savefromDImg(); 0249 } 0250 0251 return ret; 0252 } 0253 0254 } // namespace DigikamBqmAssignLabelsPlugin 0255 0256 #include "moc_assignlabels.cpp"