valentina/src/libs/vformat/vwatermark.cpp

166 lines
6.9 KiB
C++
Raw Normal View History

/************************************************************************
**
** @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
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** 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"
using namespace Qt::Literals::StringLiterals;
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");
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");
const QString VWatermark::AttrGrayscale = QStringLiteral("grayscale");
2023-07-07 11:16:49 +02:00
const QString VWatermark::AttrShow = QStringLiteral("show");
namespace
{
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto FileComment() -> QString
{
return u"Watermark created with Valentina v%1 (https://smart-pattern.com.ua/)."_s.arg(AppVersionStr());
}
2023-07-07 11:16:49 +02:00
} // namespace
//---------------------------------------------------------------------------------------------------------------------
VWatermark::VWatermark()
2023-07-07 11:16:49 +02: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));
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());
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VWatermark::SaveDocument(const QString &fileName, QString &error) -> bool
{
// 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
{
VWatermarkData data;
QDomNode root = documentElement();
if (not root.isNull() && root.isElement())
{
const QDomElement rootElement = root.toElement();
data.opacity = GetParametrInt(rootElement, AttrOpacity, QChar('2'));
QDomElement text = rootElement.firstChildElement(TagText);
if (not text.isNull())
{
data.showText = GetParametrBool(text, AttrShow, trueStr);
data.text = GetParametrEmptyString(text, AttrText);
data.textRotation = GetParametrInt(text, AttrRotation, QChar('0'));
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;
}
QDomElement image = rootElement.firstChildElement(TagImage);
if (not image.isNull())
{
data.showImage = GetParametrBool(image, AttrShow, trueStr);
data.path = GetParametrEmptyString(image, AttrPath);
data.imageRotation = GetParametrInt(image, AttrRotation, QChar('0'));
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())
{
SetAttribute(text, AttrShow, data.showText);
SetAttributeOrRemoveIf<QString>(text, AttrText, data.text,
2023-07-07 11:16:49 +02:00
[](const QString &text) noexcept { return text.isEmpty(); });
SetAttributeOrRemoveIf<int>(text, AttrRotation, data.textRotation,
2023-07-07 11:16:49 +02:00
[](int textRotation) noexcept { return textRotation == 0; });
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());
}
QDomElement image = rootElement.firstChildElement(TagImage);
if (not image.isNull())
{
SetAttribute(image, AttrShow, data.showImage);
SetAttributeOrRemoveIf<QString>(image, AttrPath, data.path,
2023-07-07 11:16:49 +02:00
[](const QString &path) noexcept { return path.isEmpty(); });
SetAttributeOrRemoveIf<int>(image, AttrRotation, data.imageRotation,
2023-07-07 11:16:49 +02:00
[](int imageRotation) noexcept { return imageRotation == 0; });
SetAttributeOrRemoveIf<bool>(image, AttrGrayscale, data.grayscale,
2023-07-07 11:16:49 +02:00
[](bool grayscale) noexcept { return not grayscale; });
}
}
}