2019-12-13 09:51:29 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vwatermark.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 25 12, 2019
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentina project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2019 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2019-12-13 09:51:29 +01:00
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
#include "vwatermark.h"
|
|
|
|
|
2021-09-11 18:39:38 +02:00
|
|
|
#include "../ifc/xml/vwatermarkconverter.h"
|
2023-07-07 11:16:49 +02:00
|
|
|
#include "../vmisc/compatibility.h"
|
|
|
|
#include "../vmisc/projectversion.h"
|
2019-12-13 09:51:29 +01:00
|
|
|
|
2023-10-07 17:56:39 +02:00
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
2019-12-13 09:51:29 +01:00
|
|
|
const QString VWatermark::TagWatermark = QStringLiteral("watermark");
|
2023-07-07 11:16:49 +02:00
|
|
|
const QString VWatermark::TagText = QStringLiteral("text");
|
|
|
|
const QString VWatermark::TagImage = QStringLiteral("image");
|
2019-12-13 09:51:29 +01:00
|
|
|
|
2023-07-07 11:16:49 +02:00
|
|
|
const QString VWatermark::AttrOpacity = QStringLiteral("opacity");
|
|
|
|
const QString VWatermark::AttrRotation = QStringLiteral("rotation");
|
|
|
|
const QString VWatermark::AttrFont = QStringLiteral("font");
|
|
|
|
const QString VWatermark::AttrPath = QStringLiteral("path");
|
2019-12-13 09:51:29 +01:00
|
|
|
const QString VWatermark::AttrGrayscale = QStringLiteral("grayscale");
|
2023-07-07 11:16:49 +02:00
|
|
|
const QString VWatermark::AttrShow = QStringLiteral("show");
|
2019-12-13 09:51:29 +01:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto FileComment() -> QString
|
2019-12-13 09:51:29 +01:00
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
return u"Watermark created with Valentina v%1 (https://smart-pattern.com.ua/)."_s.arg(AppVersionStr());
|
2019-12-13 09:51:29 +01:00
|
|
|
}
|
2023-07-07 11:16:49 +02:00
|
|
|
} // namespace
|
2019-12-13 09:51:29 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VWatermark::VWatermark()
|
2023-07-07 11:16:49 +02:00
|
|
|
{
|
|
|
|
}
|
2019-12-13 09:51:29 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VWatermark::CreateEmptyWatermark()
|
|
|
|
{
|
|
|
|
this->clear();
|
|
|
|
QDomElement wElement = this->createElement(TagWatermark);
|
|
|
|
|
|
|
|
wElement.appendChild(createComment(FileComment()));
|
2021-09-11 18:39:38 +02:00
|
|
|
wElement.appendChild(CreateElementWithText(TagVersion, VWatermarkConverter::WatermarkMaxVerStr));
|
2019-12-13 09:51:29 +01:00
|
|
|
wElement.appendChild(createElement(TagText));
|
|
|
|
wElement.appendChild(createElement(TagImage));
|
|
|
|
|
|
|
|
appendChild(wElement);
|
2023-07-07 11:16:49 +02:00
|
|
|
insertBefore(
|
|
|
|
createProcessingInstruction(QStringLiteral("xml"), QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"")),
|
|
|
|
this->firstChild());
|
2019-12-13 09:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VWatermark::SaveDocument(const QString &fileName, QString &error) -> bool
|
2019-12-13 09:51:29 +01:00
|
|
|
{
|
|
|
|
// Update comment with Valentina version
|
|
|
|
QDomNode commentNode = documentElement().firstChild();
|
|
|
|
if (commentNode.isComment())
|
|
|
|
{
|
|
|
|
QDomComment comment = commentNode.toComment();
|
|
|
|
comment.setData(FileComment());
|
|
|
|
}
|
|
|
|
|
|
|
|
return VDomDocument::SaveDocument(fileName, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VWatermark::GetWatermark() const -> VWatermarkData
|
2019-12-13 09:51:29 +01:00
|
|
|
{
|
|
|
|
VWatermarkData data;
|
|
|
|
|
|
|
|
QDomNode root = documentElement();
|
|
|
|
if (not root.isNull() && root.isElement())
|
|
|
|
{
|
|
|
|
const QDomElement rootElement = root.toElement();
|
2019-12-29 16:13:30 +01:00
|
|
|
data.opacity = GetParametrInt(rootElement, AttrOpacity, QChar('2'));
|
2019-12-13 09:51:29 +01:00
|
|
|
|
|
|
|
QDomElement text = rootElement.firstChildElement(TagText);
|
|
|
|
if (not text.isNull())
|
|
|
|
{
|
2019-12-30 13:43:06 +01:00
|
|
|
data.showText = GetParametrBool(text, AttrShow, trueStr);
|
2019-12-13 09:51:29 +01:00
|
|
|
data.text = GetParametrEmptyString(text, AttrText);
|
2019-12-29 16:13:30 +01:00
|
|
|
data.textRotation = GetParametrInt(text, AttrRotation, QChar('0'));
|
2023-07-07 15:42:54 +02:00
|
|
|
data.font = FontFromString(GetParametrEmptyString(text, AttrFont));
|
2021-09-11 18:39:38 +02:00
|
|
|
QColor color(GetParametrString(text, AttrColor, QColor(Qt::black).name()));
|
|
|
|
if (not color.isValid())
|
|
|
|
{
|
|
|
|
color = Qt::black;
|
|
|
|
}
|
|
|
|
data.textColor = color;
|
2019-12-13 09:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement image = rootElement.firstChildElement(TagImage);
|
|
|
|
if (not image.isNull())
|
|
|
|
{
|
2019-12-30 13:43:06 +01:00
|
|
|
data.showImage = GetParametrBool(image, AttrShow, trueStr);
|
2019-12-13 09:51:29 +01:00
|
|
|
data.path = GetParametrEmptyString(image, AttrPath);
|
2019-12-29 16:13:30 +01:00
|
|
|
data.imageRotation = GetParametrInt(image, AttrRotation, QChar('0'));
|
2019-12-13 09:51:29 +01:00
|
|
|
data.grayscale = GetParametrBool(image, AttrGrayscale, falseStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VWatermark::SetWatermark(const VWatermarkData &data)
|
|
|
|
{
|
|
|
|
QDomNode root = documentElement();
|
|
|
|
if (not root.isNull() && root.isElement())
|
|
|
|
{
|
|
|
|
QDomElement rootElement = root.toElement();
|
|
|
|
SetAttribute(rootElement, AttrOpacity, data.opacity);
|
|
|
|
|
|
|
|
QDomElement text = rootElement.firstChildElement(TagText);
|
|
|
|
if (not text.isNull())
|
|
|
|
{
|
2019-12-30 13:43:06 +01:00
|
|
|
SetAttribute(text, AttrShow, data.showText);
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<QString>(text, AttrText, data.text,
|
2023-07-07 11:16:49 +02:00
|
|
|
[](const QString &text) noexcept { return text.isEmpty(); });
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<int>(text, AttrRotation, data.textRotation,
|
2023-07-07 11:16:49 +02:00
|
|
|
[](int textRotation) noexcept { return textRotation == 0; });
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<QString>(text, AttrFont, data.font.toString(),
|
2023-07-07 11:16:49 +02:00
|
|
|
[](const QString &fontString) noexcept { return fontString.isEmpty(); });
|
2021-09-11 18:39:38 +02:00
|
|
|
SetAttribute(text, AttrColor, data.textColor.name());
|
2019-12-13 09:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement image = rootElement.firstChildElement(TagImage);
|
|
|
|
if (not image.isNull())
|
|
|
|
{
|
2019-12-30 13:43:06 +01:00
|
|
|
SetAttribute(image, AttrShow, data.showImage);
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<QString>(image, AttrPath, data.path,
|
2023-07-07 11:16:49 +02:00
|
|
|
[](const QString &path) noexcept { return path.isEmpty(); });
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<int>(image, AttrRotation, data.imageRotation,
|
2023-07-07 11:16:49 +02:00
|
|
|
[](int imageRotation) noexcept { return imageRotation == 0; });
|
2021-05-25 20:31:02 +02:00
|
|
|
SetAttributeOrRemoveIf<bool>(image, AttrGrayscale, data.grayscale,
|
2023-07-07 11:16:49 +02:00
|
|
|
[](bool grayscale) noexcept { return not grayscale; });
|
2019-12-13 09:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|