From d87dbfde0a55796cff1b306e32d7e9babbb5f4af Mon Sep 17 00:00:00 2001 From: renbin Date: Mon, 15 Jan 2024 14:12:16 +0800 Subject: [PATCH] feat: Merge permission/watermark into master MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合并权限控制和水印需求到主线. 涉及提交 807a3b3a84d6c76e60d8fa216229dbdfd04ea732 到 31bae041cdaad3045fe8d15f33b4d0d9cc8751fd * 新增权限控制功能, 允许配置功能开闭; * 新增水印功能, 允许通过配置定制阅读和打印水印; * 水印接口特殊, DTKWidget主线和定制线的接口不同. Log: 回合看图权限控制和水印需求 Task: https://pms.uniontech.com/story-view-27785.html Task: https://pms.uniontech.com/story-view-30217.html Influence: Print Permission --- libimageviewer/service/permissionconfig.cpp | 142 ++++++++++++++------ libimageviewer/service/permissionconfig.h | 45 ++++++- libimageviewer/widgets/printhelper.cpp | 113 ++++------------ libimageviewer/widgets/printhelper.h | 9 +- tests/test_permissionconfig.cpp | 137 ++++++++++++++++--- 5 files changed, 295 insertions(+), 151 deletions(-) diff --git a/libimageviewer/service/permissionconfig.cpp b/libimageviewer/service/permissionconfig.cpp index 8aa0fdb2..c9aa4308 100644 --- a/libimageviewer/service/permissionconfig.cpp +++ b/libimageviewer/service/permissionconfig.cpp @@ -283,9 +283,8 @@ bool PermissionConfig::eventFilter(QObject *watched, QEvent *event) return false; } - auto checkFunc = [&](const char *prop, qreal value, qreal limit){ - if (QByteArray(prop) != propEvent->propertyName() - || (value <= limit)) { + auto checkFunc = [&](const char *prop, qreal value, qreal limit) { + if (QByteArray(prop) != propEvent->propertyName() || (value <= limit)) { return; } @@ -327,6 +326,14 @@ QString PermissionConfig::targetImage() const return targetImagePath; } +/** + @return 返回适配的数据,以便于外部进行打印水印的计算 + */ +PermissionConfig::AdapterWaterMarkData PermissionConfig::adapterPrintWaterMarkData() const +{ + return printAdapterWaterMark; +} + #ifdef DTKWIDGET_CLASS_DWaterMarkHelper /** @@ -548,30 +555,33 @@ void PermissionConfig::initReadWaterMark(const QJsonObject ¶m) } #ifdef DTKWIDGET_CLASS_DWaterMarkHelper - readWaterMark.type = WaterMarkType::Text; - readWaterMark.font.setFamily(param.value("font").toString()); - readWaterMark.font.setPointSize(param.value("fontSize").toInt()); + readAdapterWaterMark.type = AdapterWaterMarkData::Text; + readAdapterWaterMark.font.setFamily(param.value("font").toString()); + readAdapterWaterMark.font.setPointSize(param.value("fontSize").toInt()); QString colorName = param.value("color").toString(); if (!colorName.startsWith('#')) { colorName.prepend('#'); } - readWaterMark.color.setNamedColor(colorName); - readWaterMark.opacity = param.value("opacity").toDouble() / 255; - readWaterMark.layout = param.value("layout").toInt() ? WaterMarkLayout::Tiled : WaterMarkLayout::Center; - readWaterMark.rotation = param.value("angle").toDouble(); - readWaterMark.lineSpacing = param.value("rowSpacing").toInt(); - readWaterMark.spacing = param.value("columnSpacing").toInt(); - readWaterMark.text = param.value("text").toString(); + readAdapterWaterMark.color.setNamedColor(colorName); + readAdapterWaterMark.opacity = param.value("opacity").toDouble() / 255; + readAdapterWaterMark.layout = param.value("layout").toInt() ? AdapterWaterMarkData::Tiled : AdapterWaterMarkData::Center; + readAdapterWaterMark.rotation = param.value("angle").toDouble(); + readAdapterWaterMark.lineSpacing = param.value("rowSpacing").toInt(); + readAdapterWaterMark.spacing = param.value("columnSpacing").toInt(); + readAdapterWaterMark.text = param.value("text").toString(); qreal deviceRatio = qApp->devicePixelRatio(); if (ignoreDevicePixelRatio && !qFuzzyCompare(1.0, deviceRatio) && deviceRatio > 0) { - readWaterMark.font.setPointSizeF(readWaterMark.font.pointSizeF() / deviceRatio); - readWaterMark.lineSpacing /= deviceRatio; - readWaterMark.spacing /= deviceRatio; + readAdapterWaterMark.font.setPointSizeF(readAdapterWaterMark.font.pointSizeF() / deviceRatio); + readAdapterWaterMark.lineSpacing /= deviceRatio; + readAdapterWaterMark.spacing /= deviceRatio; } authFlags.setFlag(EnableReadWaterMark, true); + + // 转换为公共接口类型 + readWaterMark = convertAdapterWaterMarkData(readAdapterWaterMark); #endif // DTKWIDGET_CLASS_DWaterMarkHelper } @@ -586,43 +596,86 @@ void PermissionConfig::initPrintWaterMark(const QJsonObject ¶m) } #ifdef DTKWIDGET_CLASS_DWaterMarkHelper - printWaterMark.type = WaterMarkType::Text; - printWaterMark.font.setFamily(param.value("font").toString()); - printWaterMark.font.setPointSize(param.value("fontSize").toInt()); + printAdapterWaterMark.type = AdapterWaterMarkData::Text; + printAdapterWaterMark.font.setFamily(param.value("font").toString()); + printAdapterWaterMark.font.setPointSize(param.value("fontSize").toInt()); QString colorName = param.value("color").toString(); if (!colorName.startsWith('#')) { colorName.prepend('#'); } - printWaterMark.color.setNamedColor(colorName); - printWaterMark.opacity = param.value("opacity").toDouble() / 255; - printWaterMark.layout = param.value("layout").toInt() ? WaterMarkLayout::Tiled : WaterMarkLayout::Center; - printWaterMark.rotation = param.value("angle").toDouble(); - printWaterMark.lineSpacing = param.value("rowSpacing").toInt(); - printWaterMark.spacing = param.value("columnSpacing").toInt(); - printWaterMark.text = param.value("text").toString(); + printAdapterWaterMark.color.setNamedColor(colorName); + printAdapterWaterMark.opacity = param.value("opacity").toDouble() / 255; + printAdapterWaterMark.layout = param.value("layout").toInt() ? AdapterWaterMarkData::Tiled : AdapterWaterMarkData::Center; + printAdapterWaterMark.rotation = param.value("angle").toDouble(); + printAdapterWaterMark.lineSpacing = param.value("rowSpacing").toInt(); + printAdapterWaterMark.spacing = param.value("columnSpacing").toInt(); + printAdapterWaterMark.text = param.value("text").toString(); authFlags.setFlag(EnablePrintWaterMark, true); // 计算 DTK 打印水印的间距转换系数,限制提升到 10000.0 - if (!printWaterMark.text.isEmpty()) { - QFontMetrics fm(printWaterMark.font); - QSize textSize = fm.size(Qt::TextSingleLine, printWaterMark.text); + if (!printAdapterWaterMark.text.isEmpty()) { + QFontMetrics fm(printAdapterWaterMark.font); + QSize textSize = fm.size(Qt::TextSingleLine, printAdapterWaterMark.text); if (textSize.height() > 0) { - printRowSpacing = (qreal(printWaterMark.lineSpacing + textSize.height()) / textSize.height()) - 1.0; + printRowSpacing = (qreal(printAdapterWaterMark.lineSpacing + textSize.height()) / textSize.height()) - 1.0; printRowSpacing = qBound(0.0, printRowSpacing, 10000.0); } if (textSize.width() > 0) { - printColumnSpacing = (qreal(printWaterMark.spacing + textSize.width()) / textSize.width()) - 1.0; + printColumnSpacing = (qreal(printAdapterWaterMark.spacing + textSize.width()) / textSize.width()) - 1.0; printColumnSpacing = qBound(0.0, printColumnSpacing, 10000.0); } qInfo() << QString("Print config spacing ratio row: %1 column: %2").arg(printRowSpacing).arg(printColumnSpacing); } + // 转换为公共接口类型 + printWaterMark = convertAdapterWaterMarkData(printAdapterWaterMark); #endif // DTKWIDGET_CLASS_DWaterMarkHelper } +#ifdef DTKWIDGET_CLASS_DWaterMarkHelper +/** + @brief 将内部使用的数据 `adptData` 转换为可被外部使用的公共接口类型 `WaterMarkData` , + `WaterMarkData` 在主线和定制分支的接口结构不同,需要转换以兼容不同版本 + */ +WaterMarkData PermissionConfig::convertAdapterWaterMarkData(const PermissionConfig::AdapterWaterMarkData &adptData) const +{ + WaterMarkData data; +#ifdef WATERMARK_5_4_42 + data.type = AdapterWaterMarkData::Text == adptData.type ? WaterMarkType::Text : WaterMarkType::Image; + data.layout = AdapterWaterMarkData::Center == adptData.layout ? WaterMarkLayout::Center : WaterMarkLayout::Tiled; + data.scaleFactor = adptData.scaleFactor; + data.spacing = adptData.spacing; + data.lineSpacing = adptData.lineSpacing; + data.text = adptData.text; + data.font = adptData.font; + data.color = adptData.color; + data.rotation = adptData.rotation; + data.opacity = adptData.opacity; + data.image = adptData.image; + data.grayScale = adptData.grayScale; + +#else + data.setType(AdapterWaterMarkData::Text == adptData.type ? WaterMarkData::Text : WaterMarkData::Image); + data.setLayout(AdapterWaterMarkData::Center == adptData.layout ? WaterMarkData::Center : WaterMarkData::Tiled); + data.setScaleFactor(adptData.scaleFactor); + data.setSpacing(adptData.spacing); + data.setLineSpacing(adptData.lineSpacing); + data.setText(adptData.text); + data.setFont(adptData.font); + data.setColor(adptData.color); + data.setRotation(adptData.rotation); + data.setOpacity(adptData.opacity); + data.setImage(adptData.image); + data.setGrayScale(adptData.grayScale); +#endif // WATERMARK_5_4_42 + + return data; +} +#endif // DTKWIDGET_CLASS_DWaterMarkHelper + /** @brief 检测当前系统环境中是否存在打印水印插件 通过路径下是否存在指定的插件文件决定,使用DPKG判断不一定准确, @@ -686,32 +739,35 @@ bool PermissionConfig::initWaterMarkPluginEnvironment() { QJsonObject envData; #ifdef DTKWIDGET_CLASS_DWaterMarkHelper - envData.insert("angle", static_cast(printWaterMark.rotation)); - envData.insert("transparency", static_cast(printWaterMark.opacity * 100)); - QFontMetrics fm(printWaterMark.font); - QSize textSize = fm.size(Qt::TextSingleLine, printWaterMark.text); + envData.insert("angle", static_cast(printAdapterWaterMark.rotation)); + envData.insert("transparency", static_cast(printAdapterWaterMark.opacity * 100)); + QFontMetrics fm(printAdapterWaterMark.font); + QSize textSize = fm.size(Qt::TextSingleLine, printAdapterWaterMark.text); if (textSize.height() > 0) { - envData.insert("rowSpacing", qMax(0.0, (qreal(printWaterMark.lineSpacing + textSize.height()) / textSize.height()) - 1.0)); + envData.insert("rowSpacing", + qMax(0.0, (qreal(printAdapterWaterMark.lineSpacing + textSize.height()) / textSize.height()) - 1.0)); } if (textSize.width() > 0) { - envData.insert("columnSpacing", qMax(0.0, (qreal(printWaterMark.spacing + textSize.width()) / textSize.width()) - 1.0)); + envData.insert("columnSpacing", + qMax(0.0, (qreal(printAdapterWaterMark.spacing + textSize.width()) / textSize.width()) - 1.0)); } envData.insert("layout", - static_cast(printWaterMark.layout == WaterMarkLayout::Center ? DPrintPreviewWatermarkInfo::Center : - DPrintPreviewWatermarkInfo::Tiled)); + static_cast(printAdapterWaterMark.layout == AdapterWaterMarkData::Center ? + DPrintPreviewWatermarkInfo::Center : + DPrintPreviewWatermarkInfo::Tiled)); // 仅文本水印 envData.insert("watermarkType", static_cast(DPrintPreviewWatermarkInfo::TextWatermark)); envData.insert("textType", static_cast(DPrintPreviewWatermarkInfo::Custom)); - envData.insert("customText", printWaterMark.text); - envData.insert("textColor", printWaterMark.color.name()); + envData.insert("customText", printAdapterWaterMark.text); + envData.insert("textColor", printAdapterWaterMark.color.name()); // 兼容插件传参格式 QJsonArray fontList; - fontList.append(printWaterMark.font.family()); + fontList.append(printAdapterWaterMark.font.family()); envData.insert("fontList", fontList); static const qreal sc_defaultFontSize = 65.0; // 字体使用缩放滑块处理 10%~200%, 默认字体大小为65 - envData.insert("size", int(printWaterMark.font.pointSizeF() / sc_defaultFontSize * 100)); + envData.insert("size", int(printAdapterWaterMark.font.pointSizeF() / sc_defaultFontSize * 100)); #endif QJsonDocument doc; diff --git a/libimageviewer/service/permissionconfig.h b/libimageviewer/service/permissionconfig.h index 6a50e22f..6cae576b 100644 --- a/libimageviewer/service/permissionconfig.h +++ b/libimageviewer/service/permissionconfig.h @@ -15,6 +15,13 @@ #include #include +// DTKWidget 主线和定制线的水印接口不同,通过版本进行区分 +// 主线水印接口在 5.6.9 之后引入. +// 因此,判断定制线:存在水印接口,版本不低于 5.4.42.7 且低于 5.5 +#if DTK_VERSION_CHECK(5, 4, 42, 7) <= DTK_VERSION && DTK_VERSION < DTK_VERSION_CHECK(5, 5, 0, 0) +#define WATERMARK_5_4_42 +#endif // VERSION CHECK + DWIDGET_USE_NAMESPACE #endif // DTKWIDGET_CLASS_DWaterMarkHelper @@ -76,6 +83,27 @@ class PermissionConfig : public QObject Q_SIGNAL void currentImagePathChanged(const QString &fileName, bool isTargetImage); QString targetImage() const; + // 用于兼容主线/定制线的通用水印定义,适配不同接口 + struct AdapterWaterMarkData + { + enum AWaterMarkType { None, Text, Image }; // 水印类型 + enum AWaterMarkLayout { Center, Tiled }; // 水印布局 + + AWaterMarkType type = None; // 水印类型 + AWaterMarkLayout layout = Center; // 水印布局 + qreal scaleFactor = 1.0; // 整体大小缩放系数 + int spacing = 0; // 间距 + int lineSpacing = 0; // 行间距 + QString text; // 文本水印内容 + QFont font; // 文本水印字体 + QColor color; // 文本水印颜色 + qreal rotation = 0; // 水印旋转角度(0~360) + qreal opacity = 1; // 水印透明度(0~1) + QImage image; // 图片水印中的图片 + bool grayScale = true; // 是否灰度化图片 + }; + AdapterWaterMarkData adapterPrintWaterMarkData() const; + // 阅读/打印水印 #ifdef DTKWIDGET_CLASS_DWaterMarkHelper WaterMarkData readWaterMarkData() const; @@ -96,6 +124,10 @@ class PermissionConfig : public QObject void detectWaterMarkPluginExists(); bool initWaterMarkPluginEnvironment(); +#ifdef DTKWIDGET_CLASS_DWaterMarkHelper + WaterMarkData convertAdapterWaterMarkData(const AdapterWaterMarkData &adptData) const; +#endif // DTKWIDGET_CLASS_DWaterMarkHelper + bool checkAuthInvalid(const QString &fileName = QString()) const; void reduceOnePrintCount(); @@ -113,16 +145,21 @@ class PermissionConfig : public QObject Status status = NotOpen; // 被控制权限图片的状态 Authorises authFlags = NoAuth; - bool ignoreDevicePixelRatio = false; // 过滤设备显示比率,按照原生像素计算 - bool useWaterMarkPlugin = false; // 是否使用水印插件 + bool ignoreDevicePixelRatio = false; // 过滤设备显示比率,按照原生像素计算 + bool useWaterMarkPlugin = false; // 是否使用水印插件 + + AdapterWaterMarkData readAdapterWaterMark; // 水印数据,不区分DTK版本 + AdapterWaterMarkData printAdapterWaterMark; #ifdef DTKWIDGET_CLASS_DWaterMarkHelper - WaterMarkData readWaterMark; + WaterMarkData readWaterMark; // 水印数据,根据不同DTK版本支持程度不同 WaterMarkData printWaterMark; #endif // DTKWIDGET_CLASS_DWaterMarkHelper - bool breakPrintSpacingLimit = false; // 打破打印间距限制 + bool breakPrintSpacingLimit = false; // 打破打印间距限制 qreal printRowSpacing = 0.0; qreal printColumnSpacing = 0.0; + + Q_DISABLE_COPY(PermissionConfig) }; #endif // PERMISSIONCONFIG_H diff --git a/libimageviewer/widgets/printhelper.cpp b/libimageviewer/widgets/printhelper.cpp index bec3a670..52f706a4 100755 --- a/libimageviewer/widgets/printhelper.cpp +++ b/libimageviewer/widgets/printhelper.cpp @@ -24,7 +24,6 @@ #include "unionimage/unionimage.h" #endif - PrintHelper *PrintHelper::m_Printer = nullptr; PrintHelper *PrintHelper::getIntance() @@ -46,58 +45,6 @@ PrintHelper::~PrintHelper() m_re->deleteLater(); } -//暂时没有使用配置文件的快捷键,现在是根据代码中的快捷键 -/* -static QAction *hookToolBarActionIcons(QToolBar *bar, QAction **pageSetupAction = nullptr) -{ - QAction *last_action = nullptr; - - for (QAction *action : bar->actions()) { - const QString &text = action->text(); - - if (text.isEmpty()) - continue; - - // 防止被lupdate扫描出来 - const char *context = "QPrintPreviewDialog"; - const char *print = "Print"; - - const QMap map { - {QCoreApplication::translate(context, "Next page"), QStringLiteral("go-next")}, - {QCoreApplication::translate(context, "Previous page"), QStringLiteral("go-previous")}, - {QCoreApplication::translate(context, "First page"), QStringLiteral("go-first")}, - {QCoreApplication::translate(context, "Last page"), QStringLiteral("go-last")}, - {QCoreApplication::translate(context, "Fit width"), QStringLiteral("fit-width")}, - {QCoreApplication::translate(context, "Fit page"), QStringLiteral("fit-page")}, - {QCoreApplication::translate(context, "Zoom in"), QStringLiteral("zoom-in")}, - {QCoreApplication::translate(context, "Zoom out"), QStringLiteral("zoom-out")}, - {QCoreApplication::translate(context, "Portrait"), QStringLiteral("layout-portrait")}, - {QCoreApplication::translate(context, "Landscape"), QStringLiteral("layout-landscape")}, - {QCoreApplication::translate(context, "Show single page"), QStringLiteral("view-page-one")}, - {QCoreApplication::translate(context, "Show facing pages"), QStringLiteral("view-page-sided")}, - {QCoreApplication::translate(context, "Show overview of all pages"), QStringLiteral("view-page-multi")}, - {QCoreApplication::translate(context, print), QStringLiteral("print")}, - {QCoreApplication::translate(context, "Page setup"), QStringLiteral("page-setup")} - }; - - - const QString &icon_name = map.value(action->text()); - - if (icon_name.isEmpty()) - continue; - - if (pageSetupAction && icon_name == "page-setup") { - *pageSetupAction = action; - } - - QIcon icon(QStringLiteral(":/qt-project.org/dialogs/assets/images/qprintpreviewdialog/images/%1-24.svg").arg(icon_name)); -// action->setIcon(icon); - last_action = action; - } - - return last_action; -} -*/ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) { if (!PermissionConfig::instance()->isPrintable()) { @@ -108,7 +55,7 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) m_re->m_imgs.clear(); m_re->m_paths = paths; - QStringList tempExsitPaths;//保存存在的图片路径 + QStringList tempExsitPaths; // 保存存在的图片路径 for (const QString &path : paths) { QString errMsg; QImageReader imgReadreder(path); @@ -118,7 +65,7 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) m_re->m_imgs << imgReadreder.read(); } } else { - //QImage不应该多次赋值,所以换到这里来,修复style问题 + // QImage不应该多次赋值,所以换到这里来,修复style问题 QImage img; LibUnionImage_NameSpace::loadStaticImageFromFile(path, img, errMsg); if (!img.isNull()) { @@ -128,15 +75,14 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) tempExsitPaths << paths; } - //看图采用同步,因为只有一张图片,传入父指针 - DPrintPreviewDialog printDialog2(parent); -#if (DTK_VERSION_MAJOR > 5 \ - || (DTK_VERSION_MAJOR >=5 && DTK_VERSION_MINOR > 4) \ - || (DTK_VERSION_MAJOR >= 5 && DTK_VERSION_MINOR >= 4 && DTK_VERSION_PATCH >= 10))//5.4.4暂时没有合入 - //增加运行时版本判断 + // 看图采用同步,因为只有一张图片,传入父指针 + DPrintPreviewDialog printDialog2(parent); +#if (DTK_VERSION_MAJOR > 5 || (DTK_VERSION_MAJOR >= 5 && DTK_VERSION_MINOR > 4) || \ + (DTK_VERSION_MAJOR >= 5 && DTK_VERSION_MINOR >= 4 && DTK_VERSION_PATCH >= 10)) // 5.4.4暂时没有合入 + // 增加运行时版本判断 if (DApplication::runtimeDtkVersion() >= DTK_VERSION_CHECK(5, 4, 10, 0)) { if (!tempExsitPaths.isEmpty()) { - //直接传递为路径,不会有问题 + // 直接传递为路径,不会有问题 printDialog2.setDocName(QFileInfo(tempExsitPaths.at(0)).absoluteFilePath()); } } @@ -150,7 +96,8 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) // 定制分支,水印功能不依赖DTK版本,更新打印水印设置 if (PermissionConfig::instance()->hasPrintWaterMark()) { - auto data = PermissionConfig::instance()->printWaterMarkData(); + // 使用适配的水印配置 + auto data = PermissionConfig::instance()->adapterPrintWaterMarkData(); DPrintPreviewSettingInfo *baseInfo = printDialog2.createDialogSettingInfo(DPrintPreviewWatermarkInfo::PS_Watermark); if (baseInfo) { @@ -158,6 +105,7 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) if (info) { // 打印水印实现方式同阅读水印略有不同,调整参数以使得效果一致。 info->opened = true; + info->angle = static_cast(data.rotation); info->transparency = static_cast(data.opacity * 100); QFontMetrics fm(data.font); @@ -168,8 +116,12 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) if (textSize.width() > 0) { info->columnSpacing = qMax(0.0, (qreal(data.spacing + textSize.width()) / textSize.width()) - 1.0); } - info->layout = data.layout == WaterMarkLayout::Center ? DPrintPreviewWatermarkInfo::Center : DPrintPreviewWatermarkInfo::Tiled; - info->currentWatermarkType = (data.type == WaterMarkType::Text) ? DPrintPreviewWatermarkInfo::TextWatermark : DPrintPreviewWatermarkInfo::ImageWatermark; + info->layout = (data.layout == PermissionConfig::AdapterWaterMarkData::Center) ? + DPrintPreviewWatermarkInfo::Center : + DPrintPreviewWatermarkInfo::Tiled; + info->currentWatermarkType = (data.type == PermissionConfig::AdapterWaterMarkData::Text) ? + DPrintPreviewWatermarkInfo::TextWatermark : + DPrintPreviewWatermarkInfo::ImageWatermark; info->textType = DPrintPreviewWatermarkInfo::Custom; info->customText = data.text; info->textColor = data.color; @@ -177,6 +129,7 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) static const qreal sc_defaultFontSize = 65.0; // 字体使用缩放滑块处理 10%~200%, 默认字体大小为65 info->size = int(data.font.pointSizeF() / sc_defaultFontSize * 100); + printDialog2.updateDialogSettingInfo(info); } else { qWarning() << qPrintable("Can't convert DPrintPreviewDialog watermark info.") << baseInfo->type(); @@ -191,16 +144,14 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) // WaterMarkFrame 和 WaterMarkContentFrame 是DTK窗口中水印设置控件的 objectName auto widgetList = printDialog2.findChildren("WaterMarkFrame"); widgetList.append(printDialog2.findChildren("WaterMarkContentFrame")); - for (auto wid: widgetList) { + for (auto wid : widgetList) { wid->setVisible(true); wid->setEnabled(false); } - } #endif - connect(&printDialog2, SIGNAL(paintRequested(DPrinter *)), - m_re, SLOT(paintRequestSync(DPrinter *))); + connect(&printDialog2, SIGNAL(paintRequested(DPrinter *)), m_re, SLOT(paintRequestSync(DPrinter *))); #ifndef USE_TEST int ret = printDialog2.exec(); @@ -209,7 +160,7 @@ void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent) int ret = QDialog::Accepted; #endif - if (QDialog::Accepted == ret) { + if (QDialog::Accepted == ret) { if (!tempExsitPaths.isEmpty()) { PermissionConfig::instance()->triggerPrint(tempExsitPaths.first()); } @@ -224,40 +175,34 @@ RequestedSlot::RequestedSlot(QObject *parent) Q_UNUSED(parent) } -RequestedSlot::~RequestedSlot() -{ - -} +RequestedSlot::~RequestedSlot() {} void RequestedSlot::paintRequestSync(DPrinter *_printer) { - //由于之前再度修改了打印的逻辑,导致了相同图片不在被显示,多余多页tiff来说不合理 + // 由于之前再度修改了打印的逻辑,导致了相同图片不在被显示,多余多页tiff来说不合理 QPainter painter(_printer); int indexNum = 0; for (QImage img : m_imgs) { if (!img.isNull()) { painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::SmoothPixmapTransform); - QRect wRect = _printer->pageRect(); - //修复bug98129,打印不完全问题,ratio应该是适应宽或者高,不应该直接适应宽 + QRect wRect = _printer->pageRect(); + // 修复bug98129,打印不完全问题,ratio应该是适应宽或者高,不应该直接适应宽 qreal ratio = 0.0; qDebug() << wRect; ratio = wRect.width() * 1.0 / img.width(); if (qreal(wRect.height() - img.height() * ratio) > 0) { - painter.drawImage(QRectF(0, abs(qreal(wRect.height() - img.height() * ratio)) / 2, - wRect.width(), img.height() * ratio), img); + painter.drawImage( + QRectF(0, abs(qreal(wRect.height() - img.height() * ratio)) / 2, wRect.width(), img.height() * ratio), img); } else { ratio = wRect.height() * 1.0 / img.height(); - painter.drawImage(QRectF(qreal(wRect.width() - img.width() * ratio) / 2, 0, - img.width() * ratio, wRect.height()), img); + painter.drawImage(QRectF(qreal(wRect.width() - img.width() * ratio) / 2, 0, img.width() * ratio, wRect.height()), + img); } - - } indexNum++; if (indexNum != m_imgs.size()) { _printer->newPage(); - } } painter.end(); diff --git a/libimageviewer/widgets/printhelper.h b/libimageviewer/widgets/printhelper.h index e27a238c..35812fae 100755 --- a/libimageviewer/widgets/printhelper.h +++ b/libimageviewer/widgets/printhelper.h @@ -10,14 +10,17 @@ #include #include DWIDGET_USE_NAMESPACE + //重构printhelper,因为dtk更新 //绘制图片处理类 class RequestedSlot : public QObject { Q_OBJECT + public: explicit RequestedSlot(QObject *parent = nullptr); ~RequestedSlot(); + private slots: void paintRequestSync(DPrinter *_printer); @@ -25,6 +28,7 @@ private slots: QStringList m_paths; QList m_imgs; }; + class PrintHelper : public QObject { Q_OBJECT @@ -33,14 +37,13 @@ class PrintHelper : public QObject static PrintHelper *getIntance(); ~PrintHelper(); - // static QSize adjustSize(PrintOptionsPage* optionsPage, QImage img, int resolution, const QSize & viewportSize); - // static QPoint adjustPosition(PrintOptionsPage* optionsPage, const QSize& imageSize, const QSize & viewportSize); void showPrintDialog(const QStringList &paths, QWidget *parent = nullptr); RequestedSlot *m_re = nullptr; + private: explicit PrintHelper(QObject *parent = nullptr); static PrintHelper *m_Printer; }; -#endif // PRINTHELPER_H +#endif // PRINTHELPER_H diff --git a/tests/test_permissionconfig.cpp b/tests/test_permissionconfig.cpp index 0aa370e8..2a54ca3e 100644 --- a/tests/test_permissionconfig.cpp +++ b/tests/test_permissionconfig.cpp @@ -48,14 +48,14 @@ TEST_F(UT_PermissionConfig, parseConfigOption_NormalParam_Pass) QString configParam; QStringList imageList; QString tempPath = QApplication::applicationDirPath() + "/svg.svg"; - QStringList tmpList {tempPath}; - QStringList arguments {qApp->arguments().first(), "--config=test", tempPath}; + QStringList tmpList{tempPath}; + QStringList arguments{qApp->arguments().first(), "--config=test", tempPath}; EXPECT_TRUE(PermissionConfig::instance()->parseConfigOption(arguments, configParam, imageList)); EXPECT_EQ(configParam, QString("test")); EXPECT_EQ(imageList, tmpList); - QStringList arguments2 {qApp->arguments().first(), "--config", "test", tempPath}; + QStringList arguments2{qApp->arguments().first(), "--config", "test", tempPath}; EXPECT_TRUE(PermissionConfig::instance()->parseConfigOption(arguments2, configParam, imageList)); EXPECT_EQ(configParam, QString("test")); } @@ -65,13 +65,13 @@ TEST_F(UT_PermissionConfig, parseConfigOption_NormalParam_Fail) QString configParam; QStringList imageList; QString tempPath = QApplication::applicationDirPath() + "/svg.svg"; - QStringList arguments {qApp->arguments().first(), "config", "test", tempPath}; + QStringList arguments{qApp->arguments().first(), "config", "test", tempPath}; EXPECT_FALSE(PermissionConfig::instance()->parseConfigOption({}, configParam, imageList)); EXPECT_TRUE(configParam.isEmpty()); EXPECT_TRUE(imageList.isEmpty()); - QStringList tmpList {"config", "test", tempPath}; + QStringList tmpList{"config", "test", tempPath}; EXPECT_FALSE(PermissionConfig::instance()->parseConfigOption(arguments, configParam, imageList)); EXPECT_TRUE(configParam.isEmpty()); EXPECT_EQ(imageList, tmpList); @@ -84,15 +84,14 @@ TEST_F(UT_PermissionConfig, initFromArguments_NormalParam_Pass) QString tempPath = QApplication::applicationDirPath() + "/svg.svg"; QStringList args; - args << qApp->arguments().first() - << QString("--config=%1").arg(permission_config()) - << tempPath; + args << qApp->arguments().first() << QString("--config=%1").arg(permission_config()) << tempPath; PermissionConfig::instance()->initFromArguments(args); EXPECT_TRUE(PermissionConfig::instance()->isValid()); - PermissionConfig::Authorises auth = PermissionConfig::Authorises( - PermissionConfig::EnableCopy | PermissionConfig::EnableEdit | PermissionConfig::EnableSwitch | PermissionConfig::EnableWallpaper); + PermissionConfig::Authorises auth = + PermissionConfig::Authorises(PermissionConfig::EnableCopy | PermissionConfig::EnableEdit | + PermissionConfig::EnableSwitch | PermissionConfig::EnableWallpaper); EXPECT_EQ(PermissionConfig::instance()->authFlags, auth); EXPECT_EQ(PermissionConfig::instance()->targetImagePath, tempPath); @@ -167,7 +166,8 @@ TEST_F(UT_PermissionConfig, print_Count_Pass) EXPECT_TRUE(PermissionConfig::instance()->isUnlimitPrint()); } -TEST_F(UT_PermissionConfig, watarMark_JsonData_Pass) +#ifdef WATERMARK_5_4_42 +TEST_F(UT_PermissionConfig, watarMark_JsonData_Pass_5_4_42) { // Default value auto markData = PermissionConfig::instance()->readWaterMarkData(); @@ -176,7 +176,7 @@ TEST_F(UT_PermissionConfig, watarMark_JsonData_Pass) EXPECT_EQ(markData.layout, WaterMarkLayout::Center); EXPECT_TRUE(markData.text.isEmpty()); - QJsonObject param {{"text", "test"}, {"opacity", 0}, {"layout", 1}}; + QJsonObject param{{"text", "test"}, {"opacity", 0}, {"layout", 1}}; PermissionConfig::instance()->initReadWaterMark(param); markData = PermissionConfig::instance()->readWaterMarkData(); @@ -190,14 +190,40 @@ TEST_F(UT_PermissionConfig, watarMark_JsonData_Pass) EXPECT_EQ(markData.type, WaterMarkType::Text); } +#else + +TEST_F(UT_PermissionConfig, watarMark_JsonData_Pass) +{ + // Default value + auto markData = PermissionConfig::instance()->readWaterMarkData(); + EXPECT_EQ(markData.type(), WaterMarkData::WaterMarkType::None); + EXPECT_EQ(markData.opacity(), 1); + EXPECT_EQ(markData.layout(), WaterMarkData::WaterMarkLayout::Center); + EXPECT_TRUE(markData.text().isEmpty()); + + QJsonObject param{{"text", "test"}, {"opacity", 0}, {"layout", 1}}; + + PermissionConfig::instance()->initReadWaterMark(param); + markData = PermissionConfig::instance()->readWaterMarkData(); + EXPECT_EQ(markData.type(), WaterMarkData::WaterMarkType::Text); + EXPECT_EQ(markData.opacity(), 0); + EXPECT_EQ(markData.layout(), WaterMarkData::WaterMarkLayout::Tiled); + EXPECT_EQ(markData.text(), QString("test")); + + PermissionConfig::instance()->initPrintWaterMark(param); + markData = PermissionConfig::instance()->printWaterMarkData(); + EXPECT_EQ(markData.type(), WaterMarkData::WaterMarkType::Text); +} +#endif // WATERMARK_5_4_42 + TEST_F(UT_PermissionConfig, triggerAction_SignalNotify_Pass) { QJsonObject notifyData; - QObject::connect(PermissionConfig::instance(), &PermissionConfig::authoriseNotify, [&](const QJsonObject ¬ify){ + QObject::connect(PermissionConfig::instance(), &PermissionConfig::authoriseNotify, [&](const QJsonObject ¬ify) { notifyData = notify; }); - auto TriggerFunction = [&](PermissionConfig::TidType tid, const QString &operate){ + auto TriggerFunction = [&](PermissionConfig::TidType tid, const QString &operate) { // PermissionConfig::targetImagePath is empty. PermissionConfig::instance()->triggerAction(tid, ""); // ReportMode::ReportAndBroadcast = 0b11 @@ -255,11 +281,17 @@ TEST_F(UT_PermissionConfig, initWaterMarkPluginEnvironment_CheckPlugin_Pass) EXPECT_FALSE(envData.isEmpty()); } -TEST_F(UT_PermissionConfig, initWaterMarkPluginEnvironment_CheckValue_Pass) +TEST_F(UT_PermissionConfig, initWaterMarkPluginEnvironment_CheckValue) { static const qreal sc_defaultFontSize = 65.0; // 验证打印水印设置值 - WaterMarkData &watermark = PermissionConfig::instance()->printWaterMark; +#ifdef WATERMARK_5_4_42 + WaterMarkData &watermark = PermissionConfig::instance()->printWaterMark; + watermark.layout = WaterMarkLayout::Center; +#else + PermissionConfig::AdapterWaterMarkData &watermark = PermissionConfig::instance()->printAdapterWaterMark; + watermark.layout = PermissionConfig::AdapterWaterMarkData::Center; +#endif // WATERMARK_5_4_42 watermark.rotation = 45; watermark.opacity = 0.3; watermark.font.setPointSize(30); @@ -267,7 +299,6 @@ TEST_F(UT_PermissionConfig, initWaterMarkPluginEnvironment_CheckValue_Pass) watermark.lineSpacing = 0; watermark.spacing = 0; watermark.color = "#FF00FF"; - watermark.layout = WaterMarkLayout::Center; // 强制设置环境变量 PermissionConfig::instance()->initWaterMarkPluginEnvironment(); @@ -383,4 +414,76 @@ TEST_F(UT_PermissionConfig, installFilterPrintDialog_FilterData_Ignore) EXPECT_TRUE(qFuzzyCompare(widget->property("_d_print_waterMarkColumnSpacing").toReal(), 1.0)); } +#ifdef WATERMARK_5_4_42 + +TEST_F(UT_PermissionConfig, convertAdapterWaterMarkData_Equal_Pass) +{ + WaterMarkData data; + data.type = WaterMarkData::Text; + data.layout = WaterMarkData::Tiled; + data.text = "123Test"; + QFont f; + f.setPointSize(15); + data.font = f; + QColor color(Qt::red); + data.color = color; + data.lineSpacing = 10; + data.rotation = 10; + + PermissionConfig::AdapterWaterMarkData adptData; + adptData.type = PermissionConfig::AdapterWaterMarkData::Text; + adptData.layout = PermissionConfig::AdapterWaterMarkData::Tiled; + adptData.text = "123Test"; + adptData.font = f; + adptData.color = color; + adptData.lineSpacing = 10; + adptData.rotation = 10; + + auto cvtData = PermissionConfig::instance()->convertAdapterWaterMarkData(adptData); + EXPECT_EQ(data.type, cvtData.type); + EXPECT_EQ(data.layout, cvtData.layout); + EXPECT_EQ(data.text, cvtData.text); + EXPECT_EQ(data.font, cvtData.font); + EXPECT_EQ(data.color, cvtData.color); + EXPECT_EQ(data.lineSpacing, cvtData.lineSpacing); + EXPECT_EQ(data.rotation, cvtData.rotation); +} + +#else + +TEST_F(UT_PermissionConfig, convertAdapterWaterMarkData_Equal_Pass) +{ + WaterMarkData data; + data.setType(WaterMarkData::Text); + data.setLayout(WaterMarkData::Tiled); + data.setText("123Test"); + QFont f; + f.setPointSize(15); + data.setFont(f); + QColor color(Qt::red); + data.setColor(color); + data.setLineSpacing(10); + data.setRotation(10); + + PermissionConfig::AdapterWaterMarkData adptData; + adptData.type = PermissionConfig::AdapterWaterMarkData::Text; + adptData.layout = PermissionConfig::AdapterWaterMarkData::Tiled; + adptData.text = "123Test"; + adptData.font = f; + adptData.color = color; + adptData.lineSpacing = 10; + adptData.rotation = 10; + + auto cvtData = PermissionConfig::instance()->convertAdapterWaterMarkData(adptData); + EXPECT_EQ(data.type(), cvtData.type()); + EXPECT_EQ(data.layout(), cvtData.layout()); + EXPECT_EQ(data.text(), cvtData.text()); + EXPECT_EQ(data.font(), cvtData.font()); + EXPECT_EQ(data.color(), cvtData.color()); + EXPECT_EQ(data.lineSpacing(), cvtData.lineSpacing()); + EXPECT_EQ(data.rotation(), cvtData.rotation()); +} + +#endif // WATERMARK_5_4_42 + #endif