| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2009-02-28 |
| 7 |
* Description : batch tool to add visible watermark. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2009-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2010 by Mikkel Baekhoej Christensen <mbc at baekhoej dot dk> |
| 11 |
* SPDX-FileCopyrightText: 2017 by Ahmed Fathi <ahmed dot fathi dot abdelmageed at gmail dot com> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 14 |
* |
| 15 |
* ============================================================ */ |
| 16 |
|
| 17 |
#include "watermark.h" |
| 18 |
|
| 19 |
// Qt includes |
| 20 |
|
| 21 |
#include <QFontMetrics> |
| 22 |
#include <QLabel> |
| 23 |
#include <QPainter> |
| 24 |
#include <QPen> |
| 25 |
#include <QPoint> |
| 26 |
#include <QRect> |
| 27 |
#include <QWidget> |
| 28 |
#include <QGridLayout> |
| 29 |
#include <QRadioButton> |
| 30 |
#include <QCheckBox> |
| 31 |
#include <QGroupBox> |
| 32 |
#include <QApplication> |
| 33 |
#include <QStyle> |
| 34 |
#include <QFontComboBox> |
| 35 |
#include <QComboBox> |
| 36 |
#include <QDoubleSpinBox> |
| 37 |
#include <QRandomGenerator> |
| 38 |
|
| 39 |
// KDE includes |
| 40 |
|
| 41 |
#include <klocalizedstring.h> |
| 42 |
|
| 43 |
// Local includes |
| 44 |
|
| 45 |
#include "dlayoutbox.h" |
| 46 |
#include "dnuminput.h" |
| 47 |
#include "dfileselector.h" |
| 48 |
#include "digikam_debug.h" |
| 49 |
#include "dimg.h" |
| 50 |
#include "blurfilter.h" |
| 51 |
#include "dfontproperties.h" |
| 52 |
#include "metaengine.h" |
| 53 |
#include "dcolorselector.h" |
| 54 |
#include "dtextedit.h" |
| 55 |
|
| 56 |
namespace DigikamBqmWatermarkPlugin |
| 57 |
{ |
| 58 |
|
| 59 |
class Q_DECL_HIDDEN WaterMark::Private |
| 60 |
{ |
| 61 |
|
| 62 |
public: |
| 63 |
|
| 64 |
enum WaterMarkPositon |
| 65 |
{ |
| 66 |
TopLeft = 0, |
| 67 |
TopRight, |
| 68 |
BottomLeft, |
| 69 |
BottomRight, |
| 70 |
Center, |
| 71 |
TopCenter, |
| 72 |
BottomCenter |
| 73 |
}; |
| 74 |
|
| 75 |
enum WaterMarkPlacementType |
| 76 |
{ |
| 77 |
SpecifiedLocation = 0, |
| 78 |
SystematicRepetition, |
| 79 |
RandomRepetition |
| 80 |
}; |
| 81 |
|
| 82 |
public: |
| 83 |
|
| 84 |
Private() = default; |
| 85 |
|
| 86 |
QGroupBox* textSettingsGroupBox = nullptr; |
| 87 |
QGroupBox* imageSettingsGroupBox = nullptr; |
| 88 |
QGroupBox* useAbsoluteImageSizeGroupBox = nullptr; |
| 89 |
QRadioButton* useImageRadioButton = nullptr; |
| 90 |
QCheckBox* ignoreWatermarkAspectCheckBox = nullptr; |
| 91 |
QCheckBox* addTransparencyToWatermarkImageCheckBox = nullptr; |
| 92 |
QCheckBox* useAbsoluteSizeCheckBox = nullptr; |
| 93 |
QCheckBox* useBackgroundCheckBox = nullptr; |
| 94 |
QCheckBox* denseRepetitionCheckBox = nullptr; |
| 95 |
QCheckBox* randomizeRotationCheckBox = nullptr; |
| 96 |
QRadioButton* useTextRadioButton = nullptr; |
| 97 |
|
| 98 |
DFileSelector* imageFileUrlRequester = nullptr; |
| 99 |
DTextEdit* textEdit = nullptr; |
| 100 |
|
| 101 |
QComboBox* placementPositionComboBox = nullptr; |
| 102 |
QComboBox* placementTypeComboBox = nullptr; |
| 103 |
QComboBox* rotationComboBox = nullptr; |
| 104 |
QDoubleSpinBox* sparsityFactorSpinBox = nullptr; |
| 105 |
DFontProperties* extendedFontChooserWidget = nullptr; |
| 106 |
|
| 107 |
DColorSelector* fontColorButton = nullptr; |
| 108 |
DColorSelector* backgroundColorButton = nullptr; |
| 109 |
|
| 110 |
DIntNumInput* textOpacity = nullptr; |
| 111 |
DIntNumInput* backgroundOpacity = nullptr; |
| 112 |
DIntNumInput* xMarginInput = nullptr; |
| 113 |
DIntNumInput* yMarginInput = nullptr; |
| 114 |
DIntNumInput* waterMarkSizePercent = nullptr; |
| 115 |
DIntNumInput* waterMarkOpacityPercent = nullptr; |
| 116 |
|
| 117 |
bool changeSettings = true; |
| 118 |
}; |
| 119 |
|
| 120 |
WaterMark::WaterMark(QObject* const parent) |
| 121 |
: BatchTool(QLatin1String("WaterMark"), DecorateTool, parent), |
| 122 |
d (new Private) |
| 123 |
{ |
| 124 |
} |
| 125 |
|
| 126 |
WaterMark::~WaterMark() |
| 127 |
{ |
| 128 |
delete d; |
| 129 |
} |
| 130 |
|
| 131 |
BatchTool* WaterMark::clone(QObject* const parent) const |
| 132 |
{ |
| 133 |
return new WaterMark(parent); |
| 134 |
} |
| 135 |
|
| 136 |
void WaterMark::registerSettingsWidget() |
| 137 |
{ |
| 138 |
const int spacing = layoutSpacing(); |
| 139 |
|
| 140 |
|
| 141 |
DVBox* const vbox = new DVBox; |
| 142 |
vbox->setContentsMargins(spacing, spacing, spacing, spacing); |
| 143 |
vbox->setSpacing(spacing); |
| 144 |
|
| 145 |
DHBox* const hbox = new DHBox(vbox); |
| 146 |
hbox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 147 |
hbox->setSpacing(spacing); |
| 148 |
|
| 149 |
d->useAbsoluteImageSizeGroupBox = new QGroupBox(vbox); |
| 150 |
QVBoxLayout* const useAbsoluteImageSizeGroupBoxLayout = new QVBoxLayout; |
| 151 |
useAbsoluteImageSizeGroupBoxLayout->setContentsMargins(spacing, spacing, spacing, spacing); |
| 152 |
useAbsoluteImageSizeGroupBoxLayout->addStretch(1); |
| 153 |
d->useAbsoluteImageSizeGroupBox->setLayout(useAbsoluteImageSizeGroupBoxLayout); |
| 154 |
|
| 155 |
DHBox* const useAbsoluteSizeHBox = new DHBox(); |
| 156 |
useAbsoluteSizeHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 157 |
useAbsoluteSizeHBox->setSpacing(spacing); |
| 158 |
d->useAbsoluteSizeCheckBox = new QCheckBox(useAbsoluteSizeHBox); |
| 159 |
d->useAbsoluteSizeCheckBox->setWhatsThis(i18n("Check this if you want the watermark to use the given size of " |
| 160 |
"the font or the image without any adjustment to the actual image.")); |
| 161 |
QLabel* const useAbsoluteSizeLabel = new QLabel(useAbsoluteSizeHBox); |
| 162 |
useAbsoluteSizeLabel->setText(i18n("Use Absolute Size")); |
| 163 |
d->useAbsoluteSizeCheckBox->setChecked(false); |
| 164 |
|
| 165 |
useAbsoluteImageSizeGroupBoxLayout->addWidget(useAbsoluteSizeHBox); |
| 166 |
|
| 167 |
QLabel* const watermarkTypeLabel = new QLabel(hbox); |
| 168 |
watermarkTypeLabel->setText(i18n("Watermark type: ")); |
| 169 |
|
| 170 |
d->useImageRadioButton = new QRadioButton(hbox); |
| 171 |
d->useImageRadioButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 172 |
QLabel* const useImageLabel = new QLabel(hbox); |
| 173 |
useImageLabel ->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 174 |
d->useTextRadioButton = new QRadioButton(hbox); |
| 175 |
d->useTextRadioButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 176 |
QLabel* const useTextLabel = new QLabel(hbox); |
| 177 |
useTextLabel ->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 178 |
useImageLabel->setText(i18n("Image")); |
| 179 |
useTextLabel->setText(i18n("Text")); |
| 180 |
|
| 181 |
useImageLabel->setAlignment(Qt::AlignLeft); |
| 182 |
useTextLabel->setAlignment(Qt::AlignLeft); |
| 183 |
d->useImageRadioButton->setChecked(true); |
| 184 |
|
| 185 |
// --- |
| 186 |
|
| 187 |
d->imageSettingsGroupBox = new QGroupBox(vbox); |
| 188 |
d->imageSettingsGroupBox->setTitle(i18n("Image settings")); |
| 189 |
QVBoxLayout* const imageSettingsGroupBoxLayout = new QVBoxLayout; |
| 190 |
imageSettingsGroupBoxLayout->setContentsMargins(spacing, spacing, spacing, spacing); |
| 191 |
imageSettingsGroupBoxLayout->addStretch(1); |
| 192 |
d->imageSettingsGroupBox->setLayout(imageSettingsGroupBoxLayout); |
| 193 |
|
| 194 |
DHBox* const imageSelectHBox = new DHBox(); |
| 195 |
QLabel* const label = new QLabel(imageSelectHBox); |
| 196 |
d->imageFileUrlRequester = new DFileSelector(imageSelectHBox); |
| 197 |
d->imageFileUrlRequester->lineEdit()->setPlaceholderText(i18n("Click to select watermark image.")); |
| 198 |
label->setText(i18n("Watermark image: ")); |
| 199 |
imageSettingsGroupBoxLayout->addWidget(imageSelectHBox); |
| 200 |
|
| 201 |
DHBox* const ignoreWatermarkAspectRatioHBox = new DHBox(); |
| 202 |
ignoreWatermarkAspectRatioHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 203 |
ignoreWatermarkAspectRatioHBox->setSpacing(spacing); |
| 204 |
|
| 205 |
d->ignoreWatermarkAspectCheckBox = new QCheckBox(ignoreWatermarkAspectRatioHBox); |
| 206 |
d->ignoreWatermarkAspectCheckBox->setWhatsThis(i18n("Check this if you want the watermark to ignore its own " |
| 207 |
"aspect ratio and use the image's aspect ratio instead.")); |
| 208 |
|
| 209 |
QLabel* const ignoreWatermarkAspectRatioLabel = new QLabel(ignoreWatermarkAspectRatioHBox); |
| 210 |
ignoreWatermarkAspectRatioLabel->setText(i18n("Ignore Watermark aspect Ratio")); |
| 211 |
d->ignoreWatermarkAspectCheckBox->setChecked(false); |
| 212 |
imageSettingsGroupBoxLayout->addWidget(ignoreWatermarkAspectRatioHBox); |
| 213 |
|
| 214 |
DHBox* const addTransparencyToWatermarkImageHBox = new DHBox(); |
| 215 |
addTransparencyToWatermarkImageHBox->setContentsMargins(QMargins()); |
| 216 |
addTransparencyToWatermarkImageHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 217 |
addTransparencyToWatermarkImageHBox->setSpacing(spacing); |
| 218 |
|
| 219 |
d->addTransparencyToWatermarkImageCheckBox = new QCheckBox(addTransparencyToWatermarkImageHBox); |
| 220 |
d->addTransparencyToWatermarkImageCheckBox->setWhatsThis(i18n("Check this if you want watermark to be transparent.")); |
| 221 |
d->addTransparencyToWatermarkImageCheckBox->setChecked(false); |
| 222 |
|
| 223 |
QLabel* const addTransparencyToWatermarkLabel = new QLabel(addTransparencyToWatermarkImageHBox); |
| 224 |
addTransparencyToWatermarkLabel->setText(i18n("Add transparency to watermark image")); |
| 225 |
imageSettingsGroupBoxLayout->addWidget(addTransparencyToWatermarkImageHBox); |
| 226 |
|
| 227 |
DHBox* const watermarkImageOpacityBox = new DHBox(); |
| 228 |
watermarkImageOpacityBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 229 |
watermarkImageOpacityBox->setSpacing(spacing); |
| 230 |
|
| 231 |
QLabel* const opacityLabel = new QLabel(watermarkImageOpacityBox); |
| 232 |
d->waterMarkOpacityPercent = new DIntNumInput(watermarkImageOpacityBox); |
| 233 |
d->waterMarkOpacityPercent->setRange(0, 100, 1); |
| 234 |
d->waterMarkOpacityPercent->setDefaultValue(100); |
| 235 |
d->waterMarkOpacityPercent->setWhatsThis(i18n("Opacity of watermark")); |
| 236 |
opacityLabel->setText(i18n("Opacity (%): ")); |
| 237 |
imageSettingsGroupBoxLayout->addWidget(watermarkImageOpacityBox); |
| 238 |
|
| 239 |
// --- |
| 240 |
|
| 241 |
d->textSettingsGroupBox = new QGroupBox(vbox); |
| 242 |
d->textSettingsGroupBox->setTitle(i18n("Text settings")); |
| 243 |
QVBoxLayout* const textSettingsGroupBoxLayout = new QVBoxLayout; |
| 244 |
textSettingsGroupBoxLayout->setContentsMargins(spacing, spacing, spacing, spacing); |
| 245 |
textSettingsGroupBoxLayout->addStretch(1); |
| 246 |
d->textSettingsGroupBox->setLayout(textSettingsGroupBoxLayout); |
| 247 |
|
| 248 |
QLabel* const textEditLabel = new QLabel(vbox); |
| 249 |
d->textEdit = new DTextEdit(vbox); |
| 250 |
d->textEdit->setPlaceholderText(i18n("Enter your watermark string here.")); |
| 251 |
textEditLabel->setText(i18n("Watermark text: ")); |
| 252 |
textSettingsGroupBoxLayout->addWidget(textEditLabel); |
| 253 |
textSettingsGroupBoxLayout->addWidget(d->textEdit); |
| 254 |
|
| 255 |
d->extendedFontChooserWidget = new DFontProperties(nullptr, DFontProperties::NoDisplayFlags); |
| 256 |
d->extendedFontChooserWidget->setSampleBoxVisible(true); |
| 257 |
d->extendedFontChooserWidget->enableColumn(0x04, false); |
| 258 |
d->extendedFontChooserWidget->setWhatsThis(i18n("Choose the font type and style. Size is auto calculated.")); |
| 259 |
textSettingsGroupBoxLayout->addWidget(d->extendedFontChooserWidget); |
| 260 |
|
| 261 |
DHBox* const fontColorBox = new DHBox(); |
| 262 |
QLabel* const label3 = new QLabel(fontColorBox); |
| 263 |
d->fontColorButton = new DColorSelector(fontColorBox); |
| 264 |
d->fontColorButton->setColor(Qt::black); |
| 265 |
d->fontColorButton->setWhatsThis(i18n("Set the font color to use here.")); |
| 266 |
label3->setText(i18n("Font color: ")); |
| 267 |
textSettingsGroupBoxLayout->addWidget(fontColorBox); |
| 268 |
|
| 269 |
DHBox* const fontOpacityBox = new DHBox(); |
| 270 |
QLabel* const textOpacityLabel = new QLabel(fontOpacityBox); |
| 271 |
textOpacityLabel->setText(i18n("Text opacity: ")); |
| 272 |
d->textOpacity = new DIntNumInput(fontOpacityBox); |
| 273 |
d->textOpacity->setRange(0, 100, 1); |
| 274 |
d->textOpacity->setDefaultValue(100); |
| 275 |
d->textOpacity->setWhatsThis(i18n("Set the opacity of the watermark text. " |
| 276 |
"100 is fully opaque, 0 is fully transparent.")); |
| 277 |
textSettingsGroupBoxLayout->addWidget(fontOpacityBox); |
| 278 |
|
| 279 |
DHBox* const useBackgroundHBox = new DHBox(); |
| 280 |
useBackgroundHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 281 |
useBackgroundHBox->setSpacing(spacing); |
| 282 |
|
| 283 |
d->useBackgroundCheckBox = new QCheckBox(useBackgroundHBox); |
| 284 |
d->useBackgroundCheckBox->setWhatsThis(i18n("Check this if you want a background fill behind the text.")); |
| 285 |
QLabel* const useBackgroundLabel = new QLabel(useBackgroundHBox); |
| 286 |
useBackgroundLabel->setText(i18n("Use background")); |
| 287 |
textSettingsGroupBoxLayout->addWidget(useBackgroundHBox); |
| 288 |
|
| 289 |
DHBox* const useBackgroundColorHBox = new DHBox(); |
| 290 |
QLabel* const backgroundColorLabel = new QLabel(useBackgroundColorHBox); |
| 291 |
d->backgroundColorButton = new DColorSelector(useBackgroundColorHBox); |
| 292 |
d->backgroundColorButton->setColor(QColor(0xCC, 0xCC, 0xCC)); |
| 293 |
d->backgroundColorButton->setWhatsThis(i18n("Choose the color of the watermark background.")); |
| 294 |
backgroundColorLabel ->setText(i18n("Background color: ")); |
| 295 |
textSettingsGroupBoxLayout->addWidget(useBackgroundColorHBox); |
| 296 |
|
| 297 |
DHBox* const useBackgroundOpacityHBox = new DHBox(); |
| 298 |
QLabel* const backgroundOpacityLabel = new QLabel(useBackgroundOpacityHBox); |
| 299 |
backgroundOpacityLabel->setText(i18n("Background opacity: ")); |
| 300 |
d->backgroundOpacity = new DIntNumInput(useBackgroundOpacityHBox); |
| 301 |
d->backgroundOpacity->setRange(0, 100, 1); |
| 302 |
d->backgroundOpacity->setDefaultValue(100); |
| 303 |
d->backgroundOpacity->setWhatsThis(i18n("Set the opacity of the watermark background. " |
| 304 |
"100 is fully opaque, 0 is fully transparent.")); |
| 305 |
textSettingsGroupBoxLayout->addWidget(useBackgroundOpacityHBox); |
| 306 |
|
| 307 |
d->imageSettingsGroupBox->setVisible(true); |
| 308 |
d->textSettingsGroupBox->setVisible(false); |
| 309 |
|
| 310 |
// --- |
| 311 |
|
| 312 |
DHBox* const placementTypeHBox = new DHBox(vbox); |
| 313 |
QLabel* const placementTypeLabel = new QLabel(placementTypeHBox); |
| 314 |
d->placementTypeComboBox = new QComboBox(placementTypeHBox); |
| 315 |
d->placementTypeComboBox->insertItem(Private::SpecifiedLocation, i18n("Specific Location")); |
| 316 |
d->placementTypeComboBox->insertItem(Private::SystematicRepetition, i18n("Systematic Repetition")); |
| 317 |
d->placementTypeComboBox->insertItem(Private::RandomRepetition, i18n("Random Repetition")); |
| 318 |
placementTypeLabel->setText(i18n("Placement Type: ")); |
| 319 |
|
| 320 |
DHBox* const placementHBox = new DHBox(vbox); |
| 321 |
placementHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 322 |
placementHBox->setSpacing(spacing); |
| 323 |
|
| 324 |
QLabel* const spaceLabel = new QLabel(placementHBox); |
| 325 |
d->denseRepetitionCheckBox = new QCheckBox(placementHBox); |
| 326 |
d->denseRepetitionCheckBox->setWhatsThis(i18n("When you choose to have the watermark repeated many times in the " |
| 327 |
"placement combo box, you can specify here whether the repetition.")); |
| 328 |
d->denseRepetitionCheckBox->setChecked(false); |
| 329 |
d->denseRepetitionCheckBox->setEnabled(false); |
| 330 |
spaceLabel->setText(QLatin1String(" ")); |
| 331 |
QLabel* const placementDensityLabel = new QLabel(placementHBox); |
| 332 |
placementDensityLabel->setText(i18n("Density of watermark repetition (Disabled in \"Specific Location\" mode)")); |
| 333 |
|
| 334 |
DHBox* const randomizeHBox = new DHBox(vbox); |
| 335 |
randomizeHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 336 |
randomizeHBox->setSpacing(spacing); |
| 337 |
|
| 338 |
QLabel* const spaceLabel2 = new QLabel(randomizeHBox); |
| 339 |
d->randomizeRotationCheckBox = new QCheckBox(randomizeHBox); |
| 340 |
d->randomizeRotationCheckBox->setWhatsThis(i18n("When you choose to have the watermark repeated randomly in " |
| 341 |
"the placement combo box, you can specify here to randomly " |
| 342 |
"rotate the watermark (0, 90, 180, 270 degrees).")); |
| 343 |
d->randomizeRotationCheckBox->setChecked(true); |
| 344 |
d->denseRepetitionCheckBox->setEnabled(false); |
| 345 |
spaceLabel2->setText(QLatin1String(" ")); |
| 346 |
QLabel* const randomizeRotation = new QLabel(randomizeHBox); |
| 347 |
randomizeRotation->setText(i18n("Randomize watermark orientation (Enabled in \"Random Repetition\" mode only)")); |
| 348 |
|
| 349 |
DHBox* const sparsityHBox = new DHBox(vbox); |
| 350 |
sparsityHBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 351 |
sparsityHBox->setSpacing(spacing); |
| 352 |
|
| 353 |
QLabel* const sparsityFactorLabel = new QLabel(sparsityHBox); |
| 354 |
d->sparsityFactorSpinBox = new QDoubleSpinBox(sparsityHBox); |
| 355 |
d->sparsityFactorSpinBox->setMinimum(0.0); |
| 356 |
d->sparsityFactorSpinBox->setValue(1); |
| 357 |
d->sparsityFactorSpinBox->setSingleStep(0.1); |
| 358 |
d->sparsityFactorSpinBox->setWhatsThis(i18n("Use this to get more control over the sparsity of watermark " |
| 359 |
"repetition. The higher the value the sparser the watermarks get. " |
| 360 |
"Use floating point values, typically between 1.0 and 3.0. It can " |
| 361 |
"also be less than 1.0")); |
| 362 |
sparsityFactorLabel->setText(i18n("Sparsity Factor: ")); |
| 363 |
|
| 364 |
DHBox* const placementPositionHBox = new DHBox(vbox); |
| 365 |
QLabel* const label4 = new QLabel(placementPositionHBox); |
| 366 |
d->placementPositionComboBox = new QComboBox(placementPositionHBox); |
| 367 |
d->placementPositionComboBox->insertItem(Private::TopLeft, i18nc("@item: placement position", "Top left")); |
| 368 |
d->placementPositionComboBox->insertItem(Private::TopRight, i18nc("@item: placement position", "Top right")); |
| 369 |
d->placementPositionComboBox->insertItem(Private::BottomLeft, i18nc("@item: placement position", "Bottom left")); |
| 370 |
d->placementPositionComboBox->insertItem(Private::BottomRight, i18nc("@item: placement position", "Bottom right")); |
| 371 |
d->placementPositionComboBox->insertItem(Private::Center, i18nc("@item: placement position", "Center")); |
| 372 |
d->placementPositionComboBox->insertItem(Private::TopCenter, i18nc("@item: placement position", "Top center")); |
| 373 |
d->placementPositionComboBox->insertItem(Private::BottomCenter, i18nc("@item: placement position", "Bottom center")); |
| 374 |
|
| 375 |
label4->setText(i18n("Placement Position: ")); |
| 376 |
|
| 377 |
DHBox* const placementRotationHBox = new DHBox(vbox); |
| 378 |
QLabel* const labelRotation = new QLabel(placementRotationHBox); |
| 379 |
d->rotationComboBox = new QComboBox(placementRotationHBox); |
| 380 |
d->rotationComboBox->insertItem(0, i18n("0 degrees")); |
| 381 |
d->rotationComboBox->insertItem(1, i18n("90 degrees CW")); |
| 382 |
d->rotationComboBox->insertItem(2, i18n("180 degrees")); |
| 383 |
d->rotationComboBox->insertItem(3, i18n("270 degrees CW")); |
| 384 |
labelRotation->setText(i18n("Rotation: ")); |
| 385 |
|
| 386 |
DHBox* const placementSizeHBox = new DHBox(vbox); |
| 387 |
QLabel* const label5 = new QLabel(placementSizeHBox); |
| 388 |
d->waterMarkSizePercent = new DIntNumInput(placementSizeHBox); |
| 389 |
d->waterMarkSizePercent->setRange(0, 100, 1); |
| 390 |
d->waterMarkSizePercent->setDefaultValue(30); |
| 391 |
d->waterMarkSizePercent->setWhatsThis(i18n("Size of watermark, as a percentage of the marked image.")); |
| 392 |
label5->setText(i18n("Size (%): ")); |
| 393 |
|
| 394 |
DHBox* const placementXMarginHBox = new DHBox(vbox); |
| 395 |
QLabel* const label6 = new QLabel(placementXMarginHBox); |
| 396 |
d->xMarginInput = new DIntNumInput(placementXMarginHBox); |
| 397 |
d->xMarginInput->setRange(0, 100, 1); |
| 398 |
d->xMarginInput->setDefaultValue(2); |
| 399 |
d->xMarginInput->setWhatsThis(i18n("Margin from edge in X direction, as a percentage of the marked image.")); |
| 400 |
label6->setText(i18n("X margin (%): ")); |
| 401 |
|
| 402 |
DHBox* const placementYMarginHBox = new DHBox(vbox); |
| 403 |
QLabel* const label7 = new QLabel(placementYMarginHBox); |
| 404 |
d->yMarginInput = new DIntNumInput(placementYMarginHBox); |
| 405 |
d->yMarginInput->setRange(0, 100, 1); |
| 406 |
d->yMarginInput->setDefaultValue(2); |
| 407 |
d->yMarginInput->setWhatsThis(i18n("Margin from edge in Y direction, as a percentage of the marked image.")); |
| 408 |
label7->setText(i18n("Y margin (%): ")); |
| 409 |
|
| 410 |
QLabel* const space = new QLabel(vbox); |
| 411 |
vbox->setStretchFactor(space, 10); |
| 412 |
|
| 413 |
m_settingsWidget = vbox; |
| 414 |
|
| 415 |
// ------------------------------------------------------------------------------------------------------ |
| 416 |
|
| 417 |
connect(d->useImageRadioButton, SIGNAL(toggled(bool)), |
| 418 |
this, SLOT(slotSettingsChanged())); |
| 419 |
|
| 420 |
connect(d->useTextRadioButton, SIGNAL(toggled(bool)), |
| 421 |
this, SLOT(slotSettingsChanged())); |
| 422 |
|
| 423 |
connect(d->imageFileUrlRequester->lineEdit(), SIGNAL(textChanged(QString)), |
| 424 |
this, SLOT(slotSettingsChanged())); |
| 425 |
|
| 426 |
connect(d->extendedFontChooserWidget, SIGNAL(fontSelected(QFont)), |
| 427 |
this, SLOT(slotSettingsChanged())); |
| 428 |
|
| 429 |
connect(d->fontColorButton, SIGNAL(signalColorSelected(QColor)), |
| 430 |
this, SLOT(slotSettingsChanged())); |
| 431 |
|
| 432 |
connect(d->textEdit, SIGNAL(textChanged()), |
| 433 |
this, SLOT(slotSettingsChanged())); |
| 434 |
|
| 435 |
connect(d->textOpacity, SIGNAL(valueChanged(int)), |
| 436 |
this, SLOT(slotSettingsChanged())); |
| 437 |
|
| 438 |
connect(d->useBackgroundCheckBox, SIGNAL(toggled(bool)), |
| 439 |
this, SLOT(slotSettingsChanged())); |
| 440 |
|
| 441 |
connect(d->ignoreWatermarkAspectCheckBox, SIGNAL(toggled(bool)), |
| 442 |
this, SLOT(slotSettingsChanged())); |
| 443 |
|
| 444 |
connect(d->addTransparencyToWatermarkImageCheckBox, SIGNAL(toggled(bool)), |
| 445 |
this, SLOT(slotSettingsChanged())); |
| 446 |
|
| 447 |
connect(d->useAbsoluteSizeCheckBox, SIGNAL(toggled(bool)), |
| 448 |
this, SLOT(slotSettingsChanged())); |
| 449 |
|
| 450 |
connect(d->backgroundColorButton, SIGNAL(signalColorSelected(QColor)), |
| 451 |
this, SLOT(slotSettingsChanged())); |
| 452 |
|
| 453 |
connect(d->backgroundOpacity, SIGNAL(valueChanged(int)), |
| 454 |
this, SLOT(slotSettingsChanged())); |
| 455 |
|
| 456 |
connect(d->placementTypeComboBox, SIGNAL(activated(int)), |
| 457 |
this, SLOT(slotSettingsChanged())); |
| 458 |
|
| 459 |
connect(d->placementPositionComboBox, SIGNAL(activated(int)), |
| 460 |
this, SLOT(slotSettingsChanged())); |
| 461 |
|
| 462 |
connect(d->denseRepetitionCheckBox, SIGNAL(toggled(bool)), |
| 463 |
this, SLOT(slotSettingsChanged())); |
| 464 |
|
| 465 |
connect(d->randomizeRotationCheckBox, SIGNAL(toggled(bool)), |
| 466 |
this, SLOT(slotSettingsChanged())); |
| 467 |
|
| 468 |
connect(d->sparsityFactorSpinBox, SIGNAL(valueChanged(double)), |
| 469 |
this, SLOT(slotSettingsChanged())); |
| 470 |
|
| 471 |
connect(d->rotationComboBox, SIGNAL(activated(int)), |
| 472 |
this, SLOT(slotSettingsChanged())); |
| 473 |
|
| 474 |
connect(d->waterMarkSizePercent, SIGNAL(valueChanged(int)), |
| 475 |
this, SLOT(slotSettingsChanged())); |
| 476 |
|
| 477 |
connect(d->yMarginInput, SIGNAL(valueChanged(int)), |
| 478 |
this, SLOT(slotSettingsChanged())); |
| 479 |
|
| 480 |
connect(d->xMarginInput, SIGNAL(valueChanged(int)), |
| 481 |
this, SLOT(slotSettingsChanged())); |
| 482 |
|
| 483 |
connect(d->waterMarkOpacityPercent, SIGNAL(valueChanged(int)), |
| 484 |
this, SLOT(slotSettingsChanged())); |
| 485 |
|
| 486 |
BatchTool::registerSettingsWidget(); |
| 487 |
} |
| 488 |
|
| 489 |
BatchToolSettings WaterMark::defaultSettings() |
| 490 |
{ |
| 491 |
BatchToolSettings settings; |
| 492 |
|
| 493 |
settings.insert(QLatin1String("Use image"), true); |
| 494 |
settings.insert(QLatin1String("Watermark image"), QString()); |
| 495 |
settings.insert(QLatin1String("Text"), QString()); |
| 496 |
settings.insert(QLatin1String("Font"), QFont()); |
| 497 |
settings.insert(QLatin1String("Color"), QColor(Qt::black)); |
| 498 |
settings.insert(QLatin1String("Text opacity"), 100); |
| 499 |
settings.insert(QLatin1String("Use background"), true); |
| 500 |
settings.insert(QLatin1String("Background color"), QColor(0xCC, 0xCC, 0xCC)); |
| 501 |
settings.insert(QLatin1String("Background opacity"), 0xCC); |
| 502 |
settings.insert(QLatin1String("PlacementType"), Private::SpecifiedLocation); // specified location for the watermark |
| 503 |
settings.insert(QLatin1String("Dense Repetition"), false); |
| 504 |
settings.insert(QLatin1String("Randomize Rotation"), true); |
| 505 |
settings.insert(QLatin1String("Sparsity Factor"), 1.0); |
| 506 |
settings.insert(QLatin1String("Placement"), Private::BottomRight); |
| 507 |
settings.insert(QLatin1String("Rotation"), 0); |
| 508 |
settings.insert(QLatin1String("Watermark size"), 25); |
| 509 |
settings.insert(QLatin1String("X margin"), 2); |
| 510 |
settings.insert(QLatin1String("Y margin"), 2); |
| 511 |
settings.insert(QLatin1String("Opacity"), 100); |
| 512 |
|
| 513 |
return settings; |
| 514 |
} |
| 515 |
|
| 516 |
void WaterMark::slotAssignSettings2Widget() |
| 517 |
{ |
| 518 |
d->changeSettings = false; |
| 519 |
d->useImageRadioButton->setChecked(settings().value(QLatin1String("Use image")).toBool()); |
| 520 |
d->useTextRadioButton->setChecked(!settings().value(QLatin1String("Use image")).toBool()); |
| 521 |
d->imageFileUrlRequester->setFileDlgPath(settings().value(QLatin1String("Watermark image")).toString()); |
| 522 |
|
| 523 |
QString txt = settings().value(QLatin1String("Text")).toString(); |
| 524 |
|
| 525 |
if (d->textEdit->text() != txt) |
| 526 |
{ |
| 527 |
d->textEdit->setText(txt); |
| 528 |
} |
| 529 |
|
| 530 |
d->extendedFontChooserWidget->setFont(qvariant_cast<QFont>(settings().value(QLatin1String("Font")))); |
| 531 |
d->fontColorButton->setColor(settings().value(QLatin1String("Color")).toString()); |
| 532 |
d->textOpacity->setValue(settings().value(QLatin1String("Text opacity")).toInt()); |
| 533 |
d->useBackgroundCheckBox->setChecked(settings().value(QLatin1String("Use background")).toBool()); |
| 534 |
d->backgroundColorButton->setColor(settings().value(QLatin1String("Background color")).toString()); |
| 535 |
d->backgroundOpacity->setValue(settings().value(QLatin1String("Background opacity")).toInt()); |
| 536 |
d->placementPositionComboBox->setCurrentIndex(settings().value(QLatin1String("PlacementType")).toInt()); |
| 537 |
d->denseRepetitionCheckBox->setChecked(settings().value(QLatin1String("Dense Repetition")).toBool()); |
| 538 |
d->randomizeRotationCheckBox->setChecked(settings().value(QLatin1String("Randomize Rotation")).toBool()); |
| 539 |
d->sparsityFactorSpinBox->setValue(settings().value(QLatin1String("Sparsity Factor")).toDouble()); |
| 540 |
d->placementPositionComboBox->setCurrentIndex(settings().value(QLatin1String("Placement")).toInt()); |
| 541 |
d->rotationComboBox->setCurrentIndex(settings().value(QLatin1String("Rotation")).toInt()); |
| 542 |
d->waterMarkSizePercent->setValue(settings().value(QLatin1String("Watermark size")).toInt()); |
| 543 |
d->xMarginInput->setValue(settings().value(QLatin1String("X margin")).toInt()); |
| 544 |
d->yMarginInput->setValue(settings().value(QLatin1String("Y margin")).toInt()); |
| 545 |
d->waterMarkOpacityPercent->setValue(settings().value(QLatin1String("Opacity")).toInt()); |
| 546 |
d->changeSettings = true; |
| 547 |
} |
| 548 |
|
| 549 |
void WaterMark::slotSettingsChanged() |
| 550 |
{ |
| 551 |
if (d->useImageRadioButton->isChecked()) |
| 552 |
{ |
| 553 |
d->textSettingsGroupBox->setVisible(false); |
| 554 |
d->imageSettingsGroupBox->setVisible(true); |
| 555 |
} |
| 556 |
else if (d->useTextRadioButton->isChecked()) |
| 557 |
{ |
| 558 |
d->imageSettingsGroupBox->setVisible(false); |
| 559 |
d->textSettingsGroupBox->setVisible(true); |
| 560 |
} |
| 561 |
|
| 562 |
d->waterMarkSizePercent->setEnabled(!d->useAbsoluteSizeCheckBox->isChecked()); |
| 563 |
d->extendedFontChooserWidget->enableColumn(0x04, d->useAbsoluteSizeCheckBox->isChecked()); |
| 564 |
d->placementPositionComboBox->setEnabled(((int)d->placementTypeComboBox->currentIndex() == Private::SpecifiedLocation)); |
| 565 |
d->denseRepetitionCheckBox->setEnabled(((int)d->placementTypeComboBox->currentIndex() != Private::SpecifiedLocation)); |
| 566 |
d->sparsityFactorSpinBox->setEnabled(((int)d->placementTypeComboBox->currentIndex() != Private::SpecifiedLocation)); |
| 567 |
d->randomizeRotationCheckBox->setEnabled(((int)d->placementTypeComboBox->currentIndex() == Private::RandomRepetition)); |
| 568 |
d->rotationComboBox->setEnabled(!(d->randomizeRotationCheckBox->isEnabled() && d->randomizeRotationCheckBox->isChecked())); |
| 569 |
d->waterMarkOpacityPercent->setEnabled(d->addTransparencyToWatermarkImageCheckBox->isChecked()); |
| 570 |
|
| 571 |
if (d->changeSettings) |
| 572 |
{ |
| 573 |
BatchToolSettings settings; |
| 574 |
settings.insert(QLatin1String("Use image"), d->useImageRadioButton->isChecked()); |
| 575 |
settings.insert(QLatin1String("Watermark image"), d->imageFileUrlRequester->fileDlgPath()); |
| 576 |
settings.insert(QLatin1String("Text"), d->textEdit->text()); |
| 577 |
settings.insert(QLatin1String("Font"), d->extendedFontChooserWidget->font()); |
| 578 |
settings.insert(QLatin1String("Color"), d->fontColorButton->color()); |
| 579 |
settings.insert(QLatin1String("Text opacity"), d->textOpacity->value()); |
| 580 |
settings.insert(QLatin1String("Use background"), d->useBackgroundCheckBox->isChecked()); |
| 581 |
settings.insert(QLatin1String("Ignore Watermark Aspect Ratio"), d->ignoreWatermarkAspectCheckBox->isChecked()); |
| 582 |
settings.insert(QLatin1String("Use Absolute Size"), d->useAbsoluteSizeCheckBox->isChecked()); |
| 583 |
settings.insert(QLatin1String("Background color"), d->backgroundColorButton->color()); |
| 584 |
settings.insert(QLatin1String("Background opacity"), d->backgroundOpacity->value()); |
| 585 |
settings.insert(QLatin1String("Dense Repetition"), d->denseRepetitionCheckBox->isChecked()); |
| 586 |
settings.insert(QLatin1String("Randomize Rotation"), d->randomizeRotationCheckBox->isChecked()); |
| 587 |
settings.insert(QLatin1String("Sparsity Factor"), (double)d->sparsityFactorSpinBox->value()); |
| 588 |
settings.insert(QLatin1String("PlacementType"), (int)d->placementTypeComboBox->currentIndex()); |
| 589 |
settings.insert(QLatin1String("Placement"), (int)d->placementPositionComboBox->currentIndex()); |
| 590 |
settings.insert(QLatin1String("Rotation"), (int)d->rotationComboBox->currentIndex()); |
| 591 |
settings.insert(QLatin1String("Watermark size"), (int)d->waterMarkSizePercent->value()); |
| 592 |
settings.insert(QLatin1String("X margin"), (int)d->xMarginInput->value()); |
| 593 |
settings.insert(QLatin1String("Y margin"), (int)d->yMarginInput->value()); |
| 594 |
settings.insert(QLatin1String("Opacity"), (int)d->waterMarkOpacityPercent->value()); |
| 595 |
settings.insert(QLatin1String("Add transparency"), d->addTransparencyToWatermarkImageCheckBox->isChecked()); |
| 596 |
BatchTool::slotSettingsChanged(settings); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
bool WaterMark::toolOperations() |
| 601 |
{ |
| 602 |
if (!loadToDImg()) |
| 603 |
{ |
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
QString fileName = settings().value(QLatin1String("Watermark image")).toString(); |
| 608 |
int placementPosition = settings().value(QLatin1String("Placement")).toInt(); |
| 609 |
int placementType = settings().value(QLatin1String("PlacementType")).toInt(); |
| 610 |
bool denseRepetition = settings().value(QLatin1String("Dense Repetition")).toBool(); |
| 611 |
bool randomizeRotation = settings().value(QLatin1String("Randomize Rotation")).toBool(); |
| 612 |
double userSparsityFactor = settings().value(QLatin1String("Sparsity Factor")).toDouble(); |
| 613 |
int size = settings().value(QLatin1String("Watermark size")).toInt(); |
| 614 |
int xMargin = settings().value(QLatin1String("X margin")).toInt(); |
| 615 |
int yMargin = settings().value(QLatin1String("Y margin")).toInt(); |
| 616 |
bool useImage = settings().value(QLatin1String("Use image")).toBool(); |
| 617 |
QString text = settings().value(QLatin1String("Text")).toString(); |
| 618 |
QFont font = qvariant_cast<QFont>(settings().value(QLatin1String("Font"))); |
| 619 |
|
| 620 |
QColor fontColor = settings().value(QLatin1String("Color")).toString(); |
| 621 |
int textOpacity = settings().value(QLatin1String("Text opacity")).toInt(); |
| 622 |
bool useBackground = settings().value(QLatin1String("Use background")).toBool(); |
| 623 |
QColor backgroundColor = settings().value(QLatin1String("Background color")).toString(); |
| 624 |
int backgroundOpacity = settings().value(QLatin1String("Background opacity")).toInt(); |
| 625 |
Qt::AspectRatioMode watermarkAspectRatioMode = settings().value(QLatin1String("Ignore Watermark Aspect Ratio")).toBool() ? |
| 626 |
Qt::IgnoreAspectRatio : Qt::KeepAspectRatio; |
| 627 |
bool useAbsoluteSize = settings().value(QLatin1String("Use Absolute Size")).toBool(); |
| 628 |
|
| 629 |
DImg watermarkImage; |
| 630 |
DColorComposer* const composer = DColorComposer::getComposer(DColorComposer::PorterDuffNone); |
| 631 |
int marginW = lround(image().width() * (xMargin / 100.0)); |
| 632 |
int marginH = lround(image().height() * (yMargin / 100.0)); |
| 633 |
int rotationIndex = settings().value(QLatin1String("Rotation")).toInt(); |
| 634 |
double opacity = settings().value(QLatin1String("Opacity")).toInt()/100.0; |
| 635 |
bool addTransparency = settings().value(QLatin1String("Add transparency")).toBool(); |
| 636 |
|
| 637 |
DImg::ANGLE rotationAngle = (rotationIndex == 1) ? DImg::ANGLE::ROT90 : |
| 638 |
(rotationIndex == 2) ? DImg::ANGLE::ROT180 : |
| 639 |
(rotationIndex == 3) ? DImg::ANGLE::ROT270 : |
| 640 |
DImg::ANGLE::ROTNONE; |
| 641 |
|
| 642 |
// rotate and/or flip the image depending on the exif information to allow for the expected watermark placement. |
| 643 |
// note that this operation is reversed after proper watermark generation to leave everything as it was. |
| 644 |
|
| 645 |
image().exifRotate(inputUrl().toLocalFile()); |
| 646 |
|
| 647 |
float ratio = (float)image().height() / image().width(); |
| 648 |
|
| 649 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 650 |
{ |
| 651 |
size = size * ratio; |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
// For Images whose height are much larger than their width, this helps keep |
| 656 |
// the watermark size reasonable |
| 657 |
|
| 658 |
if (ratio > 1.5) |
| 659 |
{ |
| 660 |
int tempSize = size * ratio; |
| 661 |
|
| 662 |
if (tempSize < 35) |
| 663 |
{ |
| 664 |
tempSize *= 1.5; |
| 665 |
} |
| 666 |
|
| 667 |
size = (tempSize < 100) ? tempSize : 100; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
if (useImage) |
| 672 |
{ |
| 673 |
watermarkImage = DImg(fileName); |
| 674 |
|
| 675 |
if (watermarkImage.isNull()) |
| 676 |
{ |
| 677 |
return false; |
| 678 |
} |
| 679 |
|
| 680 |
if (!useAbsoluteSize) |
| 681 |
{ |
| 682 |
DImg tempImage = watermarkImage.smoothScale(image().width() * size / 100, |
| 683 |
image().height() * size / 100, |
| 684 |
watermarkAspectRatioMode); |
| 685 |
watermarkImage = tempImage; |
| 686 |
} |
| 687 |
|
| 688 |
if (addTransparency) |
| 689 |
{ |
| 690 |
if (!watermarkImage.hasAlpha()) |
| 691 |
{ |
| 692 |
return false; |
| 693 |
} |
| 694 |
|
| 695 |
for (uint i = 0 ; i < watermarkImage.width() ; ++i) |
| 696 |
{ |
| 697 |
for (uint j = 0 ; j < watermarkImage.height() ; ++j) |
| 698 |
{ |
| 699 |
DColor color = watermarkImage.getPixelColor(i, j); |
| 700 |
color.setAlpha((int)(opacity*color.alpha())); |
| 701 |
watermarkImage.setPixelColor(i, j, color); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
int alignMode; |
| 709 |
const int radius = 10; |
| 710 |
|
| 711 |
if (text.isEmpty()) |
| 712 |
{ |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
int fontSize = queryFontSize(text, font, size); |
| 717 |
|
| 718 |
if (fontSize == 0) |
| 719 |
{ |
| 720 |
return false; |
| 721 |
} |
| 722 |
|
| 723 |
switch (placementPosition) |
| 724 |
{ |
| 725 |
case Private::TopLeft: |
| 726 |
{ |
| 727 |
alignMode = Qt::AlignLeft; |
| 728 |
break; |
| 729 |
} |
| 730 |
|
| 731 |
case Private::TopRight: |
| 732 |
{ |
| 733 |
alignMode = Qt::AlignRight; |
| 734 |
break; |
| 735 |
} |
| 736 |
|
| 737 |
case Private::BottomLeft: |
| 738 |
{ |
| 739 |
alignMode = Qt::AlignLeft; |
| 740 |
break; |
| 741 |
} |
| 742 |
|
| 743 |
case Private::Center: |
| 744 |
case Private::TopCenter: |
| 745 |
case Private::BottomCenter: |
| 746 |
{ |
| 747 |
alignMode = Qt::AlignCenter; |
| 748 |
break; |
| 749 |
} |
| 750 |
|
| 751 |
default: // BottomRight |
| 752 |
{ |
| 753 |
alignMode = Qt::AlignRight; |
| 754 |
break; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
if (!useAbsoluteSize) |
| 759 |
{ |
| 760 |
font.setPointSizeF(fontSize); |
| 761 |
} |
| 762 |
|
| 763 |
QFontMetrics fontMt(font); |
| 764 |
QRect fontRect = fontMt.boundingRect(radius, radius, image().width(), image().height(), 0, text); |
| 765 |
|
| 766 |
// Add a transparent layer. |
| 767 |
|
| 768 |
QRect backgroundRect(fontRect.x() - radius, fontRect.y() - radius, |
| 769 |
fontRect.width() + 2 * radius, fontRect.height() + 2 * radius); |
| 770 |
DImg backgroundLayer(backgroundRect.width(), backgroundRect.height(), image().sixteenBit(), true); |
| 771 |
DColor transparent(QColor(0, 0, 0)); |
| 772 |
transparent.setAlpha(0); |
| 773 |
|
| 774 |
if (image().sixteenBit()) |
| 775 |
{ |
| 776 |
transparent.convertToSixteenBit(); |
| 777 |
} |
| 778 |
|
| 779 |
backgroundLayer.fill(transparent); |
| 780 |
|
| 781 |
DImg grayTransLayer(fontRect.width(), fontRect.height(), image().sixteenBit(), true); |
| 782 |
|
| 783 |
if (useBackground) |
| 784 |
{ |
| 785 |
DColor grayTrans(backgroundColor); |
| 786 |
grayTrans.setAlpha(backgroundOpacity * 255 / 100); |
| 787 |
|
| 788 |
if (image().sixteenBit()) |
| 789 |
{ |
| 790 |
grayTrans.convertToSixteenBit(); |
| 791 |
} |
| 792 |
|
| 793 |
grayTransLayer.fill(grayTrans); |
| 794 |
backgroundLayer.bitBlendImage(composer, &grayTransLayer, 0, 0, |
| 795 |
grayTransLayer.width(), grayTransLayer.height(), |
| 796 |
radius, radius); |
| 797 |
} |
| 798 |
|
| 799 |
BlurFilter blur(&backgroundLayer, nullptr, radius); |
| 800 |
blur.startFilterDirectly(); |
| 801 |
backgroundLayer.putImageData(blur.getTargetImage().bits()); |
| 802 |
|
| 803 |
// Draw text |
| 804 |
|
| 805 |
QImage img = backgroundLayer.copyQImage(fontRect); |
| 806 |
QPainter p(&img); |
| 807 |
fontColor.setAlpha(textOpacity * 255 / 100); |
| 808 |
p.setPen(QPen(fontColor, 1)); |
| 809 |
p.setFont(font); |
| 810 |
p.save(); |
| 811 |
p.drawText(0, 0, fontRect.width(), fontRect.height(), alignMode, text); |
| 812 |
p.restore(); |
| 813 |
p.end(); |
| 814 |
|
| 815 |
watermarkImage = DImg(img); |
| 816 |
} |
| 817 |
|
| 818 |
watermarkImage.convertToDepthOfImage(&image()); |
| 819 |
|
| 820 |
QRect watermarkRect(0, 0, watermarkImage.width(), watermarkImage.height()); |
| 821 |
|
| 822 |
int xAdditionalValue = 0; |
| 823 |
int yAdditionalValue = 0; |
| 824 |
|
| 825 |
watermarkImage.rotate(rotationAngle); |
| 826 |
|
| 827 |
if (placementType == Private::SpecifiedLocation) |
| 828 |
{ |
| 829 |
switch (placementPosition) |
| 830 |
{ |
| 831 |
case Private::TopLeft: |
| 832 |
{ |
| 833 |
watermarkRect.moveTopLeft(QPoint(marginW, marginH)); |
| 834 |
break; |
| 835 |
} |
| 836 |
|
| 837 |
case Private::TopRight: |
| 838 |
{ |
| 839 |
if ((rotationAngle == DImg::ANGLE::ROT270) || (rotationAngle == DImg::ANGLE::ROT90)) |
| 840 |
{ |
| 841 |
xAdditionalValue += watermarkRect.width() - watermarkRect.height(); |
| 842 |
} |
| 843 |
|
| 844 |
watermarkRect.moveTopRight(QPoint(image().width() + xAdditionalValue - marginW - 1, |
| 845 |
marginH)); |
| 846 |
break; |
| 847 |
} |
| 848 |
|
| 849 |
case Private::BottomLeft: |
| 850 |
{ |
| 851 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 852 |
{ |
| 853 |
yAdditionalValue += watermarkRect.height() - watermarkRect.width(); |
| 854 |
} |
| 855 |
|
| 856 |
watermarkRect.moveBottomLeft(QPoint(marginW, |
| 857 |
image().height() + yAdditionalValue - marginH - 1)); |
| 858 |
break; |
| 859 |
} |
| 860 |
|
| 861 |
case Private::Center: |
| 862 |
{ |
| 863 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 864 |
{ |
| 865 |
xAdditionalValue += (watermarkRect.width() - watermarkRect.height()) / 2; |
| 866 |
yAdditionalValue += (watermarkRect.height() - watermarkRect.width()) / 2; |
| 867 |
} |
| 868 |
|
| 869 |
watermarkRect.moveCenter(QPoint((int)(image().width() / 2 + xAdditionalValue), |
| 870 |
(int)(image().height() / 2 + yAdditionalValue))); |
| 871 |
break; |
| 872 |
} |
| 873 |
|
| 874 |
case Private::TopCenter: |
| 875 |
{ |
| 876 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 877 |
{ |
| 878 |
xAdditionalValue += (watermarkRect.width() - watermarkRect.height()) / 2; |
| 879 |
} |
| 880 |
|
| 881 |
watermarkRect.moveCenter(QPoint((int)(image().width() / 2 + xAdditionalValue), |
| 882 |
(watermarkRect.height() / 2) + marginH)); |
| 883 |
break; |
| 884 |
} |
| 885 |
|
| 886 |
case Private::BottomCenter: |
| 887 |
{ |
| 888 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 889 |
{ |
| 890 |
xAdditionalValue += (watermarkRect.width() - watermarkRect.height()) / 2; |
| 891 |
yAdditionalValue += watermarkRect.height() - watermarkRect.width(); |
| 892 |
} |
| 893 |
|
| 894 |
watermarkRect.moveCenter(QPoint((int)(image().width() / 2 + xAdditionalValue), |
| 895 |
image().height() + yAdditionalValue - |
| 896 |
(watermarkRect.height() / 2) - marginH - 1)); |
| 897 |
break; |
| 898 |
} |
| 899 |
|
| 900 |
default: // BottomRight |
| 901 |
{ |
| 902 |
if ((rotationAngle == DImg::ANGLE::ROT90) || (rotationAngle == DImg::ANGLE::ROT270)) |
| 903 |
{ |
| 904 |
xAdditionalValue += watermarkRect.width() - watermarkRect.height(); |
| 905 |
yAdditionalValue += watermarkRect.height() - watermarkRect.width(); |
| 906 |
} |
| 907 |
|
| 908 |
watermarkRect.moveBottomRight(QPoint(image().width() + xAdditionalValue - marginW - 1, |
| 909 |
image().height() + yAdditionalValue - marginH - 1)); |
| 910 |
break; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
image().bitBlendImage(composer, |
| 915 |
&watermarkImage, 0, 0, watermarkImage.width(), watermarkImage.height(), |
| 916 |
watermarkRect.left(), watermarkRect.top()); |
| 917 |
} |
| 918 |
else |
| 919 |
{ |
| 920 |
const float DENSE_SPACING_FACTOR = 1.2F; |
| 921 |
const float SPARSE_SPACING_FACTOR = 1.8F; |
| 922 |
float widthRatio = (float)watermarkRect.width() / image().width(); |
| 923 |
float heightRatio = (float)watermarkRect.height() / image().height(); |
| 924 |
float spacingFactor = (denseRepetition) ? DENSE_SPACING_FACTOR : SPARSE_SPACING_FACTOR; |
| 925 |
spacingFactor *= userSparsityFactor; |
| 926 |
|
| 927 |
if (placementType == Private::SystematicRepetition) |
| 928 |
{ |
| 929 |
if ((rotationAngle == DImg::ANGLE::ROT270) || (rotationAngle == DImg::ANGLE::ROT90)) |
| 930 |
{ |
| 931 |
widthRatio = (float)watermarkRect.height() / image().width(); |
| 932 |
heightRatio = (float)watermarkRect.width() / image().height(); |
| 933 |
} |
| 934 |
|
| 935 |
for (uint i = 0 ; i < image().width() ; i += spacingFactor * widthRatio * image().width()) |
| 936 |
{ |
| 937 |
for (uint j = 0 ; j < image().height() ; j += spacingFactor * heightRatio * image().height()) |
| 938 |
{ |
| 939 |
image().bitBlendImage(composer, &watermarkImage, 0, 0, watermarkImage.width(), watermarkImage.height(), i, j); |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
else if (placementType == Private::RandomRepetition) |
| 944 |
{ |
| 945 |
widthRatio = (widthRatio > heightRatio) ? widthRatio : heightRatio; |
| 946 |
heightRatio = widthRatio; |
| 947 |
|
| 948 |
auto randomGenerator = QRandomGenerator::global(); |
| 949 |
|
| 950 |
for (uint i = 0 ; i < image().width() ; i += spacingFactor * widthRatio * image().width()) |
| 951 |
{ |
| 952 |
for (uint j = 0 ; j < image().height() ; j += spacingFactor * heightRatio * image().height()) |
| 953 |
{ |
| 954 |
int number = (denseRepetition) ? 2 : 3; |
| 955 |
|
| 956 |
if (randomGenerator->bounded(number) == 0) |
| 957 |
{ |
| 958 |
if (randomizeRotation) |
| 959 |
{ |
| 960 |
int x = randomGenerator->bounded(4); |
| 961 |
watermarkImage.rotate((DImg::ANGLE)x); |
| 962 |
} |
| 963 |
|
| 964 |
image().bitBlendImage(composer, &watermarkImage, 0, 0, watermarkImage.width(), watermarkImage.height(), i, j); |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
// TODO: Create watermark filter, move code there, implement FilterAction |
| 972 |
|
| 973 |
delete composer; |
| 974 |
image().reverseExifRotate(inputUrl().toLocalFile()); |
| 975 |
|
| 976 |
return (savefromDImg()); |
| 977 |
} |
| 978 |
|
| 979 |
int WaterMark::queryFontSize(const QString& text, const QFont& font, int length) const |
| 980 |
{ |
| 981 |
// Find font size using relative length compared to image width. |
| 982 |
|
| 983 |
QFont fnt = font; |
| 984 |
QRect fontRect; |
| 985 |
|
| 986 |
for (int i = 1 ; i <= 1000 ; ++i) |
| 987 |
{ |
| 988 |
fnt.setPointSizeF(i); |
| 989 |
QFontMetrics fontMt(fnt); |
| 990 |
fontRect = fontMt.boundingRect(0, 0, image().width(), image().height(), 0, text); |
| 991 |
|
| 992 |
if (fontRect.width() > lround((image().width() * length) / 100.0)) |
| 993 |
{ |
| 994 |
return (i - 1); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
return 0; |
| 999 |
} |
| 1000 |
|
| 1001 |
} // namespace DigikamBqmWatermarkPlugin |
| 1002 |
|
| 1003 |
#include "moc_watermark.cpp" |