diff --git a/glabels/LabelEditor.cpp b/glabels/LabelEditor.cpp index ef7fe05..c3a4c15 100644 --- a/glabels/LabelEditor.cpp +++ b/glabels/LabelEditor.cpp @@ -469,6 +469,10 @@ namespace glabels mResizeObject = handle->owner(); mResizeHandle = handle; mResizeHonorAspect = event->modifiers() & Qt::ControlModifier; + if ( mResizeObject->lockAspectRatio() ) + { + mResizeHonorAspect = !mResizeHonorAspect; + } mState = ArrowResize; } diff --git a/glabels/ObjectEditor.cpp b/glabels/ObjectEditor.cpp index ecaa8f9..0053bc2 100644 --- a/glabels/ObjectEditor.cpp +++ b/glabels/ObjectEditor.cpp @@ -188,6 +188,7 @@ namespace glabels sizeWSpin->setValue( mObject->w().inUnits(mUnits) ); sizeHSpin->setValue( mObject->h().inUnits(mUnits) ); + sizeAspectCheck->setChecked( mObject->lockAspectRatio() ); model::Size originalSize = mObject->naturalSize(); QString originalSizeString = QString( "%1: %2 x %3 %4" ) @@ -655,6 +656,7 @@ namespace glabels if ( sizeAspectCheck->isChecked() ) { + mObject->setLockAspectRatio( true ); if ( fabs(spinW - mObject->w()) > fabs(spinH - mObject->h()) ) { mObject->setWHonorAspect( spinW ); @@ -668,6 +670,7 @@ namespace glabels } else { + mObject->setLockAspectRatio( false ); mObject->setSize( spinW, spinH ); } diff --git a/glabels/ui/ObjectEditor.ui b/glabels/ui/ObjectEditor.ui index 3e0589e..7a2b631 100644 --- a/glabels/ui/ObjectEditor.ui +++ b/glabels/ui/ObjectEditor.ui @@ -1202,16 +1202,7 @@ 15 - - 0 - - - 0 - - - 0 - - + 0 @@ -2190,6 +2181,22 @@ + + sizeAspectCheck + toggled(bool) + ObjectEditor + onRectSizeControlsChanged() + + + 231 + 289 + + + 199 + 319 + + + onChanged() diff --git a/model/ModelBarcodeObject.cpp b/model/ModelBarcodeObject.cpp index 315632e..1cdd5e2 100644 --- a/model/ModelBarcodeObject.cpp +++ b/model/ModelBarcodeObject.cpp @@ -89,13 +89,14 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const barcode::Style& bcStyle, bool bcTextFlag, bool bcChecksumFlag, QString bcData, const ColorNode& bcColorNode, const QMatrix& matrix ) - : ModelObject( x0, y0, w, h, matrix ) + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix ) { mOutline = new Outline( this ); diff --git a/model/ModelBarcodeObject.h b/model/ModelBarcodeObject.h index 4931ddf..ef5918d 100644 --- a/model/ModelBarcodeObject.h +++ b/model/ModelBarcodeObject.h @@ -51,6 +51,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const barcode::Style& bcStyle, bool bcTextFlag, bool bcChecksumFlag, diff --git a/model/ModelBoxObject.cpp b/model/ModelBoxObject.cpp index 94e81d5..c6b9a8a 100644 --- a/model/ModelBoxObject.cpp +++ b/model/ModelBoxObject.cpp @@ -54,6 +54,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, @@ -63,7 +64,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelShapeObject( x0, y0, w, h, + : ModelShapeObject( x0, y0, w, h, lockAspectRatio, lineWidth, lineColorNode, fillColorNode, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) diff --git a/model/ModelBoxObject.h b/model/ModelBoxObject.h index 8705c99..610fc3c 100644 --- a/model/ModelBoxObject.h +++ b/model/ModelBoxObject.h @@ -47,6 +47,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, diff --git a/model/ModelEllipseObject.cpp b/model/ModelEllipseObject.cpp index c3ca82e..783ab96 100644 --- a/model/ModelEllipseObject.cpp +++ b/model/ModelEllipseObject.cpp @@ -54,6 +54,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, @@ -63,7 +64,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelShapeObject( x0, y0, w, h, + : ModelShapeObject( x0, y0, w, h, lockAspectRatio, lineWidth, lineColorNode, fillColorNode, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) diff --git a/model/ModelEllipseObject.h b/model/ModelEllipseObject.h index e26e9ba..e690b61 100644 --- a/model/ModelEllipseObject.h +++ b/model/ModelEllipseObject.h @@ -47,6 +47,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, diff --git a/model/ModelImageObject.cpp b/model/ModelImageObject.cpp index 5848625..d0c5c52 100644 --- a/model/ModelImageObject.cpp +++ b/model/ModelImageObject.cpp @@ -70,6 +70,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const TextNode& filenameNode, const QMatrix& matrix, bool shadowState, @@ -77,7 +78,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, w, h, + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { @@ -111,6 +112,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& filename, const QImage& image, const QMatrix& matrix, @@ -119,7 +121,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, w, h, + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { @@ -152,6 +154,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& filename, const QByteArray& svg, const QMatrix& matrix, @@ -160,7 +163,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, w, h, + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { diff --git a/model/ModelImageObject.h b/model/ModelImageObject.h index ba87078..eecf2b0 100644 --- a/model/ModelImageObject.h +++ b/model/ModelImageObject.h @@ -49,6 +49,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const TextNode& filenameNode, const QMatrix& matrix = QMatrix(), bool shadowState = false, @@ -61,6 +62,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& filename, const QImage& image, const QMatrix& matrix = QMatrix(), @@ -74,6 +76,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& filename, const QByteArray& svg, const QMatrix& matrix = QMatrix(), diff --git a/model/ModelLineObject.cpp b/model/ModelLineObject.cpp index 5d7b5e1..2ff38bf 100644 --- a/model/ModelLineObject.cpp +++ b/model/ModelLineObject.cpp @@ -68,7 +68,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, dx, dy, + : ModelObject( x0, y0, dx, dy, false /*lockAspectRatio*/, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { diff --git a/model/ModelObject.cpp b/model/ModelObject.cpp index 637352c..9268be0 100644 --- a/model/ModelObject.cpp +++ b/model/ModelObject.cpp @@ -51,6 +51,7 @@ namespace glabels mY0 = 0; mW = 0; mH = 0; + mLockAspectRatio = false; mMatrix = QMatrix(); mShadowState = false; @@ -72,6 +73,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QMatrix& matrix, bool shadowState, const Distance& shadowX, @@ -85,6 +87,7 @@ namespace glabels mY0 = y0; mW = w; mH = h; + mLockAspectRatio = lockAspectRatio; mMatrix = matrix; mShadowState = shadowState; @@ -112,6 +115,7 @@ namespace glabels mY0 = object->mY0; mW = object->mW; mH = object->mH; + mLockAspectRatio = object->mLockAspectRatio; mShadowState = object->mShadowState; mShadowX = object->mShadowX; @@ -272,6 +276,28 @@ namespace glabels } + /// + /// Lock Aspect Ratio Property Getter + /// + bool ModelObject::lockAspectRatio() const + { + return mLockAspectRatio; + } + + + /// + /// Lock Aspect Ratio Property Setter + /// + void ModelObject::setLockAspectRatio( bool value ) + { + if ( mLockAspectRatio != value ) + { + mLockAspectRatio = value; + emit changed(); + } + } + + /// /// Matrix Property Getter /// diff --git a/model/ModelObject.h b/model/ModelObject.h index 6af51e8..4e9de0c 100644 --- a/model/ModelObject.h +++ b/model/ModelObject.h @@ -64,6 +64,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio = false, const QMatrix& matrix = QMatrix(), bool shadowState = false, const Distance& shadowX = 0, @@ -136,6 +137,13 @@ namespace glabels void setH( const Distance& value ); + // + // Lock Aspect Ratio Property + // + bool lockAspectRatio() const; + void setLockAspectRatio( bool value ); + + // // Transformation Matrix Property // @@ -424,6 +432,7 @@ namespace glabels Distance mY0; Distance mW; Distance mH; + bool mLockAspectRatio; bool mShadowState; Distance mShadowX; diff --git a/model/ModelShapeObject.cpp b/model/ModelShapeObject.cpp index 989f5af..afca487 100644 --- a/model/ModelShapeObject.cpp +++ b/model/ModelShapeObject.cpp @@ -58,6 +58,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, @@ -67,7 +68,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, w, h, + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { diff --git a/model/ModelShapeObject.h b/model/ModelShapeObject.h index b33c5a3..888ac03 100644 --- a/model/ModelShapeObject.h +++ b/model/ModelShapeObject.h @@ -47,6 +47,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const Distance& lineWidth, const ColorNode& lineColorNode, const ColorNode& fillColorNode, diff --git a/model/ModelTextObject.cpp b/model/ModelTextObject.cpp index 434d48b..ba43f05 100644 --- a/model/ModelTextObject.cpp +++ b/model/ModelTextObject.cpp @@ -82,6 +82,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& text, const QString& fontFamily, double fontSize, @@ -100,7 +101,7 @@ namespace glabels const Distance& shadowY, double shadowOpacity, const ColorNode& shadowColorNode ) - : ModelObject( x0, y0, w, h, + : ModelObject( x0, y0, w, h, lockAspectRatio, matrix, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ) { diff --git a/model/ModelTextObject.h b/model/ModelTextObject.h index 1ab62c6..6cabd49 100644 --- a/model/ModelTextObject.h +++ b/model/ModelTextObject.h @@ -50,6 +50,7 @@ namespace glabels const Distance& y0, const Distance& w, const Distance& h, + bool lockAspectRatio, const QString& text, const QString& fontFamily, double fontSize, diff --git a/model/XmlLabelCreator.cpp b/model/XmlLabelCreator.cpp index 87b3114..12bf317 100644 --- a/model/XmlLabelCreator.cpp +++ b/model/XmlLabelCreator.cpp @@ -384,6 +384,7 @@ namespace glabels { XmlUtil::setLengthAttr( node, "w", object->w() ); XmlUtil::setLengthAttr( node, "h", object->h() ); + XmlUtil::setBoolAttr( node, "lock_aspect_ratio", object->lockAspectRatio() ); } @@ -433,24 +434,21 @@ namespace glabels void XmlLabelCreator::createShadowAttrs( QDomElement &node, const ModelObject* object ) { - if ( object->shadow() ) + XmlUtil::setBoolAttr( node, "shadow", object->shadow() ); + + XmlUtil::setLengthAttr( node, "shadow_x", object->shadowX() ); + XmlUtil::setLengthAttr( node, "shadow_y", object->shadowY() ); + + if ( object->fillColorNode().isField() ) { - XmlUtil::setBoolAttr( node, "shadow", object->shadow() ); - - XmlUtil::setLengthAttr( node, "shadow_x", object->shadowX() ); - XmlUtil::setLengthAttr( node, "shadow_y", object->shadowY() ); - - if ( object->fillColorNode().isField() ) - { - XmlUtil::setStringAttr( node, "shadow_color_field", object->shadowColorNode().key() ); - } - else - { - XmlUtil::setUIntAttr( node, "shadow_color", object->shadowColorNode().rgba() ); - } - - XmlUtil::setDoubleAttr( node, "shadow_opacity", object->shadowOpacity() ); + XmlUtil::setStringAttr( node, "shadow_color_field", object->shadowColorNode().key() ); } + else + { + XmlUtil::setUIntAttr( node, "shadow_color", object->shadowColorNode().rgba() ); + } + + XmlUtil::setDoubleAttr( node, "shadow_opacity", object->shadowOpacity() ); } diff --git a/model/XmlLabelParser.cpp b/model/XmlLabelParser.cpp index 779149c..9690a08 100644 --- a/model/XmlLabelParser.cpp +++ b/model/XmlLabelParser.cpp @@ -354,6 +354,7 @@ namespace glabels /* size attrs */ Distance w = XmlUtil::getLengthAttr( node, "w", 0 ); Distance h = XmlUtil::getLengthAttr( node, "h", 0 ); + bool lockAspectRatio = XmlUtil::getBoolAttr( node, "lock_aspect_ratio", false ); /* line attrs */ Distance lineWidth = XmlUtil::getLengthAttr( node, "line_width", 1.0 ); @@ -389,7 +390,7 @@ namespace glabels color = XmlUtil::getUIntAttr( node, "shadow_color", 0 ); ColorNode shadowColorNode( field_flag, color, key ); - return new ModelBoxObject( x0, y0, w, h, + return new ModelBoxObject( x0, y0, w, h, lockAspectRatio, lineWidth, lineColorNode, fillColorNode, QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), @@ -407,6 +408,7 @@ namespace glabels /* size attrs */ Distance w = XmlUtil::getLengthAttr( node, "w", 0 ); Distance h = XmlUtil::getLengthAttr( node, "h", 0 ); + bool lockAspectRatio = XmlUtil::getBoolAttr( node, "lock_aspect_ratio", false ); /* line attrs */ Distance lineWidth = XmlUtil::getLengthAttr( node, "line_width", 1.0 ); @@ -442,7 +444,7 @@ namespace glabels color = XmlUtil::getUIntAttr( node, "shadow_color", 0 ); ColorNode shadowColorNode( field_flag, color, key ); - return new ModelEllipseObject( x0, y0, w, h, + return new ModelEllipseObject( x0, y0, w, h, lockAspectRatio, lineWidth, lineColorNode, fillColorNode, QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), @@ -506,6 +508,7 @@ namespace glabels /* size attrs */ Distance w = XmlUtil::getLengthAttr( node, "w", 0 ); Distance h = XmlUtil::getLengthAttr( node, "h", 0 ); + bool lockAspectRatio = XmlUtil::getBoolAttr( node, "lock_aspect_ratio", false ); /* file attrs */ QString key = XmlUtil::getStringAttr( node, "src_field", "" ); @@ -535,7 +538,7 @@ namespace glabels if ( filenameNode.isField() ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, lockAspectRatio, filenameNode, QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); @@ -544,22 +547,25 @@ namespace glabels { if ( data.hasImage( filename ) ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, lockAspectRatio, filename, data.getImage( filename ), QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); } else if ( data.hasSvg( filename ) ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, lockAspectRatio, filename, data.getSvg( filename ), QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); } else { - qWarning() << "Embedded file" << filename << "missing. Trying actual file."; - return new ModelImageObject( x0, y0, w, h, + if ( !filename.isEmpty() ) + { + qWarning() << "Embedded file" << filename << "missing. Trying actual file."; + } + return new ModelImageObject( x0, y0, w, h, lockAspectRatio, filenameNode, QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ), shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); @@ -578,6 +584,7 @@ namespace glabels /* size attrs */ Distance w = XmlUtil::getLengthAttr( node, "w", 0 ); Distance h = XmlUtil::getLengthAttr( node, "h", 0 ); + bool lockAspectRatio = XmlUtil::getBoolAttr( node, "lock_aspect_ratio", false ); /* barcode attrs */ barcode::Style bcStyle = barcode::Backends::style( XmlUtil::getStringAttr( node, "backend", "" ), @@ -601,7 +608,7 @@ namespace glabels a[4] = XmlUtil::getDoubleAttr( node, "a4", 0.0 ); a[5] = XmlUtil::getDoubleAttr( node, "a5", 0.0 ); - return new ModelBarcodeObject( x0, y0, w, h, + return new ModelBarcodeObject( x0, y0, w, h, lockAspectRatio, bcStyle, bcTextFlag, bcChecksumFlag, bcData, bcColorNode, QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ) ); } @@ -617,6 +624,7 @@ namespace glabels /* size attrs */ Distance w = XmlUtil::getLengthAttr( node, "w", 0 ); Distance h = XmlUtil::getLengthAttr( node, "h", 0 ); + bool lockAspectRatio = XmlUtil::getBoolAttr( node, "lock_aspect_ratio", false ); /* color attr */ QString key = XmlUtil::getStringAttr( node, "color_field", "" ); @@ -682,7 +690,7 @@ namespace glabels } QString text = document.toPlainText(); - return new ModelTextObject( x0, y0, w, h, + return new ModelTextObject( x0, y0, w, h, lockAspectRatio, text, fontFamily, fontSize, fontWeight, fontItalicFlag, fontUnderlineFlag, textColorNode, textHAlign, textVAlign, textWrapMode, textLineSpacing, diff --git a/model/XmlLabelParser_3.cpp b/model/XmlLabelParser_3.cpp index e2a442f..dca802a 100644 --- a/model/XmlLabelParser_3.cpp +++ b/model/XmlLabelParser_3.cpp @@ -229,7 +229,7 @@ namespace glabels color = XmlUtil::getUIntAttr( node, "shadow_color", 0 ); const ColorNode shadowColorNode( field_flag, color, key ); - return new ModelBoxObject( x0, y0, w, h, + return new ModelBoxObject( x0, y0, w, h, false /*lockAspectRatio*/, lineWidth, lineColorNode, fillColorNode, affineTransformation, @@ -276,7 +276,7 @@ namespace glabels color = XmlUtil::getUIntAttr( node, "shadow_color", 0 ); const ColorNode shadowColorNode( field_flag, color, key ); - return new ModelEllipseObject( x0, y0, w, h, + return new ModelEllipseObject( x0, y0, w, h, false /*lockAspectRatio*/, lineWidth, lineColorNode, fillColorNode, affineTransformation, @@ -357,27 +357,27 @@ namespace glabels if ( filenameNode.isField() ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, false /*lockAspectRatio*/, filenameNode, affineTransformation, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); } if ( data.hasImage( filename ) ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, false /*lockAspectRatio*/, filename, data.getImage( filename ), affineTransformation, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); } if ( data.hasSvg( filename ) ) { - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, false /*lockAspectRatio*/, filename, data.getSvg( filename ), affineTransformation, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); } qWarning() << "Embedded file" << filename << "missing. Trying actual file."; - return new ModelImageObject( x0, y0, w, h, + return new ModelImageObject( x0, y0, w, h, false /*lockAspectRatio*/, filenameNode, affineTransformation, shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode ); @@ -419,7 +419,7 @@ namespace glabels /* affine attrs */ const auto affineTransformation = parseAffineTransformation(node); - return new ModelBarcodeObject( x0, y0, w, h, + return new ModelBarcodeObject( x0, y0, w, h, false /*lockAspectRatio*/, bcStyle, bcTextFlag, bcChecksumFlag, bcData, bcColorNode, affineTransformation ); } @@ -554,7 +554,7 @@ namespace glabels } const QString text = document.toPlainText(); - auto textNode = new ModelTextObject( x0, y0, w, h, text, + auto textNode = new ModelTextObject( x0, y0, w, h, false /*lockAspectRatio*/, text, fontFamily, fontSize, fontWeight, fontItalicFlag, false, textColorNode, textHAlign, textVAlign, textWrapMode, textLineSpacing, textAutoShrink, diff --git a/model/unit_tests/CMakeLists.txt b/model/unit_tests/CMakeLists.txt index 5edddd8..d05896c 100644 --- a/model/unit_tests/CMakeLists.txt +++ b/model/unit_tests/CMakeLists.txt @@ -16,4 +16,12 @@ if (Qt5Test_FOUND) target_link_libraries (TestXmlUtil Model Qt5::Test) add_test (NAME XmlUtil COMMAND TestXmlUtil) + #======================================= + # Test XmlLabelCreator/Parser classes + #======================================= + qt5_wrap_cpp (TestXmlLabel_moc_sources TestXmlLabel.h) + add_executable (TestXmlLabel TestXmlLabel.cpp ${TestXmlLabel_moc_sources}) + target_link_libraries (TestXmlLabel Model Qt5::Test) + add_test (NAME XmlLabel COMMAND TestXmlLabel) + endif (Qt5Test_FOUND) diff --git a/model/unit_tests/TestXmlLabel.cpp b/model/unit_tests/TestXmlLabel.cpp new file mode 100644 index 0000000..cc4a510 --- /dev/null +++ b/model/unit_tests/TestXmlLabel.cpp @@ -0,0 +1,162 @@ +/* TestXmlLabel.cpp + * + * Copyright (C) 2018 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt 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. + * + * gLabels-qt 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 gLabels-qt. If not, see . + */ + +#include "TestXmlLabel.h" + +#include "model/XmlLabelCreator.h" +#include "model/XmlLabelParser.h" + +#include "barcode/Backends.h" +#include "model/ColorNode.h" +#include "model/Size.h" + +#include "model/ModelBarcodeObject.h" +#include "model/ModelBoxObject.h" +#include "model/ModelEllipseObject.h" +#include "model/ModelLineObject.h" +#include "model/ModelImageObject.h" +#include "model/ModelTextObject.h" + +#include + + +QTEST_MAIN(TestXmlLabel) + + +void TestXmlLabel::initTestCase() +{ + using namespace glabels::barcode; + Backends::init(); +} + + +void TestXmlLabel::serializeDeserialize() +{ + using namespace glabels::model; + using namespace glabels::barcode; + + // + // Empty object list + // + QList objects, outObjects; + QByteArray buffer, outBuffer; + + QCOMPARE( objects.count(), 0 ); + XmlLabelCreator::serializeObjects( objects, buffer ); + outObjects = XmlLabelParser::deserializeObjects( buffer ); + QCOMPARE( objects.count(), outObjects.count() ); + QCOMPARE( objects, outObjects ); + XmlLabelCreator::serializeObjects( outObjects, outBuffer ); + QCOMPARE( buffer, outBuffer ); + + // + // All objects list + // + bool lock = true, noLock = false, shadow = true, noShadow = false; + ColorNode black( Qt::black ), white( Qt::white ), red( Qt::red ), green( Qt::green ), blue( Qt::blue ); + QMatrix tMatrix( 1, 0, 0, 1, 50.0, 50.0 ), sMatrix( 0.5, 0, 0, 1.0, 0, 0 ); + QImage png( QFINDTESTDATA( "../../../glabels/images/glabels-logo.png" ) ); + QByteArray svg = ""; + Style bcStyle = Backends::defaultStyle(); + + objects << new ModelBoxObject( 0, 1, 10, 20, lock, 2, red, green, tMatrix, shadow, 1, 2, 0.7, black ); + objects << new ModelEllipseObject( 1, 2, 30, 40, noLock, 3, black, white, sMatrix, shadow, 2, 3, 0.8, blue ); + objects << new ModelImageObject( 2, 3, 50, 50, lock, TextNode( false, "" ), tMatrix, noShadow, 3, 4, 0.9, white ); + objects << new ModelImageObject( 3, 4, 60, 70, noLock, "image2.png", png, sMatrix, shadow, 6, 4, 0.9, black ); + objects << new ModelImageObject( 4, 5, 70, 80, lock, "image3.svg", svg ); + objects << new ModelImageObject( 5, 6, 80, 90, noLock, TextNode( true, "${key}" ), tMatrix, shadow ); + objects << new ModelImageObject( 6, 7, 90, 100, lock, TextNode( false, "image5.jpg" ) ); // Gives warning that embedded file missing + objects << new ModelLineObject( 7, 8, 100, 110, 4, green, sMatrix, shadow, 5, 5, 0.5, red ); + objects << new ModelTextObject( 8, 9, 110, 120, lock, "text", "Serif", 12, QFont::Bold, true, true, red, + Qt::AlignHCenter, Qt::AlignBottom, QTextOption::NoWrap, 1.3, false, sMatrix, shadow, 5, 5, 0.5, red ); + objects << new ModelBarcodeObject( 9, 10, 50, 50, noLock, bcStyle, true, true, QString("1234"), black, tMatrix ); + QCOMPARE( objects.count(), 10 ); + + buffer.clear(); + XmlLabelCreator::serializeObjects( objects, buffer ); + outObjects = XmlLabelParser::deserializeObjects( buffer ); + QCOMPARE( objects.count(), outObjects.count() ); + + for ( int i = 0; i < objects.count(); i++ ) + { + qDebug() << "object" << i; + QVERIFY( objects.at(i)->id() != outObjects.at(i)->id() ); // Ids are generated and unique + QCOMPARE( objects.at(i)->x0(), outObjects.at(i)->x0() ); + QCOMPARE( objects.at(i)->x0().pt(), (double)i ); + QCOMPARE( objects.at(i)->y0(), outObjects.at(i)->y0() ); + QCOMPARE( objects.at(i)->y0().pt(), (double)(i + 1) ); + QCOMPARE( objects.at(i)->w().pt(), outObjects.at(i)->w().pt() ); // Use `pt()` so invoke `qFuzzyCompare(double, double)` otherwise get rounding difference for Barcode + QCOMPARE( objects.at(i)->h().pt(), outObjects.at(i)->h().pt() ); // Fuzzy + QCOMPARE( objects.at(i)->lockAspectRatio(), outObjects.at(i)->lockAspectRatio() ); + QCOMPARE( objects.at(i)->lockAspectRatio(), (bool)((i + 1) % 2) ); + QCOMPARE( objects.at(i)->matrix(), outObjects.at(i)->matrix() ); + QCOMPARE( objects.at(i)->shadow(), outObjects.at(i)->shadow() ); + QCOMPARE( objects.at(i)->shadowX(), outObjects.at(i)->shadowX() ); + QCOMPARE( objects.at(i)->shadowY(), outObjects.at(i)->shadowY() ); + QCOMPARE( objects.at(i)->shadowOpacity(), outObjects.at(i)->shadowOpacity() ); + QVERIFY( objects.at(i)->shadowColorNode() == outObjects.at(i)->shadowColorNode() ); + QCOMPARE( objects.at(i)->naturalSize().w().pt(), outObjects.at(i)->naturalSize().w().pt() ); // Fuzzy + QCOMPARE( objects.at(i)->naturalSize().h().pt(), outObjects.at(i)->naturalSize().h().pt() ); // Fuzzy + + QCOMPARE( objects.at(i)->text(), outObjects.at(i)->text() ); + QCOMPARE( objects.at(i)->fontFamily(), outObjects.at(i)->fontFamily() ); + QCOMPARE( objects.at(i)->fontSize(), outObjects.at(i)->fontSize() ); + QCOMPARE( objects.at(i)->fontWeight(), outObjects.at(i)->fontWeight() ); + QCOMPARE( objects.at(i)->fontItalicFlag(), outObjects.at(i)->fontItalicFlag() ); + QCOMPARE( objects.at(i)->fontUnderlineFlag(), outObjects.at(i)->fontUnderlineFlag() ); + QVERIFY( objects.at(i)->textColorNode() == outObjects.at(i)->textColorNode() ); + QCOMPARE( objects.at(i)->textHAlign(), outObjects.at(i)->textHAlign() ); + QCOMPARE( objects.at(i)->textVAlign(), outObjects.at(i)->textVAlign() ); + QCOMPARE( objects.at(i)->textWrapMode(), outObjects.at(i)->textWrapMode() ); + QCOMPARE( objects.at(i)->textLineSpacing(), outObjects.at(i)->textLineSpacing() ); + QCOMPARE( objects.at(i)->textAutoShrink(), outObjects.at(i)->textAutoShrink() ); + + QVERIFY( objects.at(i)->filenameNode() == outObjects.at(i)->filenameNode() ); + if ( objects.at(i)->image() ) + { + QCOMPARE( *(objects.at(i)->image()), *(outObjects.at(i)->image()) ); + } + else + { + QCOMPARE( objects.at(i)->image(), outObjects.at(i)->image() ); + } + QCOMPARE( objects.at(i)->svg(), outObjects.at(i)->svg() ); + + QCOMPARE( objects.at(i)->lineWidth(), outObjects.at(i)->lineWidth() ); + QVERIFY( objects.at(i)->lineColorNode() == outObjects.at(i)->lineColorNode() ); + QVERIFY( objects.at(i)->fillColorNode() == outObjects.at(i)->fillColorNode() ); + + QCOMPARE( objects.at(i)->bcData(), outObjects.at(i)->bcData() ); + QCOMPARE( objects.at(i)->bcTextFlag(), outObjects.at(i)->bcTextFlag() ); + QCOMPARE( objects.at(i)->bcChecksumFlag(), outObjects.at(i)->bcChecksumFlag() ); + QVERIFY( objects.at(i)->bcColorNode() == outObjects.at(i)->bcColorNode() ); + QVERIFY( !( objects.at(i)->bcStyle() != outObjects.at(i)->bcStyle() ) ); // Only != operator + QCOMPARE( objects.at(i)->bcFormatDigits(), outObjects.at(i)->bcFormatDigits() ); + + QCOMPARE( objects.at(i)->canText(), outObjects.at(i)->canText() ); + QCOMPARE( objects.at(i)->canFill(), outObjects.at(i)->canFill() ); + QCOMPARE( objects.at(i)->canLineColor(), outObjects.at(i)->canLineColor() ); + QCOMPARE( objects.at(i)->canLineWidth(), outObjects.at(i)->canLineWidth() ); + } + + outBuffer.clear(); + XmlLabelCreator::serializeObjects( outObjects, outBuffer ); + QCOMPARE( buffer, outBuffer ); +} diff --git a/model/unit_tests/TestXmlLabel.h b/model/unit_tests/TestXmlLabel.h new file mode 100644 index 0000000..0483f54 --- /dev/null +++ b/model/unit_tests/TestXmlLabel.h @@ -0,0 +1,33 @@ +/* TestXmlLabel.h + * + * Copyright (C) 2018 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt 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. + * + * gLabels-qt 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 gLabels-qt. If not, see . + */ + +#include + + +class TestXmlLabel : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + void serializeDeserialize(); +}; + + diff --git a/templates/glabels-4.0.dtd b/templates/glabels-4.0.dtd index 5834460..1ade6a9 100644 --- a/templates/glabels-4.0.dtd +++ b/templates/glabels-4.0.dtd @@ -323,8 +323,9 @@ >