| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "AddWatermarkOperation.h" |
| 21 | |
| 22 | AddWatermarkOperation::AddWatermarkOperation(IImageAnnotator *imageAnnotator, const QSharedPointer<IConfig> &config) : |
| 23 | mImageAnnotator(imageAnnotator), |
| 24 | mConfig(config), |
| 25 | mMessageBoxService(new MessageBoxService) |
| 26 | { |
| 27 | |
| 28 | } |
| 29 | |
| 30 | AddWatermarkOperation::~AddWatermarkOperation() |
| 31 | { |
| 32 | delete mMessageBoxService; |
| 33 | } |
| 34 | |
| 35 | void AddWatermarkOperation::execute() |
| 36 | { |
| 37 | auto watermarkImage = mWatermarkImageLoader.load(); |
| 38 | if(watermarkImage.isNull()) { |
| 39 | NotifyAboutMissingWatermarkImage(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | auto availableSpace = mImageAnnotator->image().size(); |
| 44 | auto rotated = mConfig->rotateWatermarkEnabled(); |
| 45 | auto finishedWatermarkImage = mImagePreparer.prepare(watermarkImage, availableSpace, rotated); |
| 46 | auto position = getPositionForWatermark(finishedWatermarkImage, availableSpace); |
| 47 | mImageAnnotator->insertImageItem(position, finishedWatermarkImage); |
| 48 | } |
| 49 | |
| 50 | void AddWatermarkOperation::NotifyAboutMissingWatermarkImage() const |
| 51 | { |
| 52 | mMessageBoxService->ok(tr("Watermark Image Required"), tr("Please add a Watermark Image via Options > Settings > Annotator > Update")); |
| 53 | } |
| 54 | |
| 55 | QPointF AddWatermarkOperation::getPositionForWatermark(const QPixmap &image, const QSize &availableSpace) |
| 56 | { |
| 57 | auto availableWidth = availableSpace.width() - image.rect().width(); |
| 58 | auto availableHeight = availableSpace.height() - image.rect().height(); |
| 59 | |
| 60 | qreal x = availableWidth <= 0 ? 0 : rand() % availableWidth; |
| 61 | qreal y = availableHeight <= 0 ? 0 : rand() % availableHeight; |
| 62 | |
| 63 | return {x, y}; |
| 64 | } |