diff --git a/model/ModelImageObject.cpp b/model/ModelImageObject.cpp index dc6e1fa..0eaa068 100644 --- a/model/ModelImageObject.cpp +++ b/model/ModelImageObject.cpp @@ -538,17 +538,26 @@ namespace glabels } else if ( mFilenameNode.isField() ) { - // Look for image file relative to project file 1st then CWD 2nd + // Look for image file relative to project file 1st then CWD 2nd then absolute 3rd auto* model = dynamic_cast( parent() ); - QDir::setSearchPaths( "images", {model->dirPath(), QDir::currentPath()} ); + QDir::setSearchPaths( "images", {model->dirPath(), QDir::currentPath(), QDir::rootPath()} ); QString filename = QString("images:") + mFilenameNode.text( record, variables ); - auto* image = new QImage( filename ); - if ( !image->isNull() ) + QImage* image; + QSvgRenderer* svgRenderer; + if ( readImageFile( filename, image, svgRenderer ) ) { - painter->drawImage( destRect, *image ); + if ( image ) + { + painter->drawImage( destRect, *image ); + delete image; + } + else + { + svgRenderer->render( painter, destRect ); + delete svgRenderer; + } } - delete image; } } @@ -584,63 +593,80 @@ namespace glabels if ( !mFilenameNode.isField() ) { QString filename = mFilenameNode.data(); - QFileInfo fileInfo( filename ); + if ( readImageFile( filename, mImage, mSvgRenderer ) ) + { + double aspectRatio = 0; + if ( mSvgRenderer ) + { + // Adjust size based on aspect ratio of SVG image + QRectF rect = mSvgRenderer->viewBoxF(); + aspectRatio = rect.width() ? rect.height() / rect.width() : 0; + } + else + { + // Adjust size based on aspect ratio of image + double imageW = mImage->width(); + double imageH = mImage->height(); + aspectRatio = imageW ? imageH / imageW : 0; + } + if ( aspectRatio ) + { + if ( mH > mW*aspectRatio ) + { + mH = mW*aspectRatio; + } + else + { + mW = mH/aspectRatio; + } + } + } + } + } + + + /// + /// Read an image or svg file + /// + bool ModelImageObject::readImageFile( const QString& fileName, QImage*& image, QSvgRenderer*& svgRenderer ) const + { + image = nullptr; + svgRenderer = nullptr; + + if ( !fileName.isEmpty() ) + { + QFileInfo fileInfo( fileName ); if ( fileInfo.isReadable() ) { - if ( (fileInfo.suffix() == "svg") || (fileInfo.suffix() == "SVG") ) + if ( fileInfo.suffix().toLower() == "svg" ) { - QFile file( filename ); + QFile file( fileName ); if ( file.open( QFile::ReadOnly ) ) { - mSvg = file.readAll(); + QByteArray svg = file.readAll(); file.close(); - mSvgRenderer = new QSvgRenderer( mSvg ); - if ( !mSvgRenderer->isValid() ) + svgRenderer = new QSvgRenderer( svg ); + if ( !svgRenderer->isValid() ) { - mSvgRenderer = nullptr; - } - else - { - // Adjust size based on aspect ratio of SVG image - QRectF rect = mSvgRenderer->viewBoxF(); - double aspectRatio = rect.height() / rect.width(); - if ( mH > mW*aspectRatio ) - { - mH = mW*aspectRatio; - } - else - { - mW = mH/aspectRatio; - } + delete svgRenderer; + svgRenderer = nullptr; } } } else { - mImage = new QImage( filename ); - if ( mImage->isNull() ) + image = new QImage( fileName ); + if ( image->isNull() ) { - mImage = nullptr; - } - else - { - // Adjust size based on aspect ratio of image - double imageW = mImage->width(); - double imageH = mImage->height(); - double aspectRatio = imageH / imageW; - if ( mH > mW*aspectRatio ) - { - mH = mW*aspectRatio; - } - else - { - mW = mH/aspectRatio; - } + delete image; + image = nullptr; } } } } + + return image != nullptr || svgRenderer != nullptr; } diff --git a/model/ModelImageObject.h b/model/ModelImageObject.h index 061a4c0..0f0fa15 100644 --- a/model/ModelImageObject.h +++ b/model/ModelImageObject.h @@ -152,7 +152,9 @@ namespace glabels // Private /////////////////////////////////////////////////////////////// void loadImage(); - + + bool readImageFile( const QString& fileName, QImage*& image, QSvgRenderer*& svgRenderer ) const; + QImage* createShadowImage( const QImage& image, const QColor& color ) const; diff --git a/model/Variable.cpp b/model/Variable.cpp index fc52e14..fa96462 100644 --- a/model/Variable.cpp +++ b/model/Variable.cpp @@ -26,6 +26,19 @@ namespace glabels namespace model { + Variable::Variable() + : mType(Type::STRING), + mIncrement(Increment::NEVER), + mStepSize("0"), + mIntegerValue(0), + mIntegerStep(0), + mFloatingPointValue(0), + mFloatingPointStep(0) + { + // empty + } + + Variable::Variable( Variable::Type type, const QString& name, const QString& initialValue, @@ -35,9 +48,13 @@ namespace glabels mName(name), mInitialValue(initialValue), mIncrement(increment), - mStepSize(stepSize) + mStepSize(stepSize), + mIntegerValue(0), + mIntegerStep(0), + mFloatingPointValue(0), + mFloatingPointStep(0) { - // empty + resetValue(); } diff --git a/model/Variable.h b/model/Variable.h index 3dfad13..c18f74f 100644 --- a/model/Variable.h +++ b/model/Variable.h @@ -54,7 +54,7 @@ namespace glabels public: - Variable() = default; + Variable(); Variable( Type type, const QString& name, diff --git a/model/unit_tests/CMakeLists.txt b/model/unit_tests/CMakeLists.txt index c896762..5989bd9 100644 --- a/model/unit_tests/CMakeLists.txt +++ b/model/unit_tests/CMakeLists.txt @@ -48,6 +48,14 @@ if (Qt5Test_FOUND) target_link_libraries (TestModel Model Qt5::Test) add_test (NAME Model COMMAND TestModel) + #======================================= + # Test ModelImageObject class + #======================================= + qt5_wrap_cpp (TestModelImageObject_moc_sources TestModelImageObject.h) + add_executable (TestModelImageObject TestModelImageObject.cpp ${TestModelImageObject_moc_sources}) + target_link_libraries (TestModelImageObject Model Qt5::Test) + add_test (NAME ModelImageObject COMMAND TestModelImageObject) + #======================================= # Test RawText class #======================================= @@ -64,4 +72,20 @@ if (Qt5Test_FOUND) target_link_libraries (TestTextNode Model Qt5::Test) add_test (NAME TextNode COMMAND TestTextNode) + #======================================= + # Test Variable class + #======================================= + qt5_wrap_cpp (TestVariable_moc_sources TestVariable.h) + add_executable (TestVariable TestVariable.cpp ${TestVariable_moc_sources}) + target_link_libraries (TestVariable Model Qt5::Test) + add_test (NAME Variable COMMAND TestVariable) + + #======================================= + # Test Variables class + #======================================= + qt5_wrap_cpp (TestVariables_moc_sources TestVariables.h) + add_executable (TestVariables TestVariables.cpp ${TestVariables_moc_sources}) + target_link_libraries (TestVariables Model Qt5::Test) + add_test (NAME Variables COMMAND TestVariables) + endif (Qt5Test_FOUND) diff --git a/model/unit_tests/TestColorNode.cpp b/model/unit_tests/TestColorNode.cpp index 7ce0485..251ac97 100644 --- a/model/unit_tests/TestColorNode.cpp +++ b/model/unit_tests/TestColorNode.cpp @@ -22,8 +22,6 @@ #include "model/ColorNode.h" -#include "merge/Record.h" - #include @@ -48,6 +46,7 @@ void TestColorNode::colorNode() QColor silver80 = QColor( 192, 192, 192, 128 ); Record record; + Variables vars; ColorNode colorNode; QVERIFY( !colorNode.isField() ); @@ -56,6 +55,8 @@ void TestColorNode::colorNode() QCOMPARE( colorNode.rgba(), rgbaBlackTransparent ); QCOMPARE( colorNode.color( nullptr, nullptr ), blackTransparent ); QCOMPARE( colorNode.color( &record, nullptr ), blackTransparent ); + QCOMPARE( colorNode.color( nullptr, &vars ), blackTransparent ); + QCOMPARE( colorNode.color( &record, &vars ), blackTransparent ); colorNode.setField( true ); QVERIFY( colorNode.isField() ); @@ -67,6 +68,8 @@ void TestColorNode::colorNode() QCOMPARE( colorNode.rgba(), rgbaWhite ); QCOMPARE( colorNode.color( nullptr, nullptr ), white ); QCOMPARE( colorNode.color( &record, nullptr ), white ); + QCOMPARE( colorNode.color( nullptr, &vars ), white ); + QCOMPARE( colorNode.color( &record, &vars ), white ); colorNode.setKey( "key1" ); QCOMPARE( colorNode.key(), QString( "key1" ) ); @@ -99,15 +102,15 @@ void TestColorNode::colorNode() colorNode.setColor( red ); QVERIFY( colorNode3 == colorNode ); + /// + /// Record + /// colorNode = ColorNode( QString( "key1" ) ); QVERIFY( colorNode.isField() ); // Defaults to true if given key only QCOMPARE( colorNode.key(), QString( "key1" ) ); QCOMPARE( colorNode.color(), blackTransparent ); - QCOMPARE( colorNode.color( &record, nullptr ), silver80 ); // Defaults to silver if given non-matching record/variables + QCOMPARE( colorNode.color( &record, &vars ), silver80 ); // Defaults to silver if given non-matching record/variables - /// - /// Record - /// record["key1"] = "white"; QCOMPARE( colorNode.color( &record, nullptr ), white ); @@ -127,4 +130,48 @@ void TestColorNode::colorNode() QCOMPARE( colorNode.color( &record, nullptr ), silver80 ); record["key2"] = "#8000FF00"; QCOMPARE( colorNode.color( &record, nullptr ), green80 ); + + /// + /// Variable + /// + colorNode = ColorNode( QString( "c1" ) ); + QVERIFY( colorNode.isField() ); // Defaults to true if given key only + QCOMPARE( colorNode.key(), QString( "c1" ) ); + QCOMPARE( colorNode.color(), blackTransparent ); + QCOMPARE( colorNode.color( &record, &vars ), silver80 ); // Defaults to silver if given non-matching record/variables + + { + Variable c1( Variable::Type::COLOR, "c1", "white", Variable::Increment::PER_ITEM ); + vars.addVariable( c1 ); + } + QCOMPARE( colorNode.color( nullptr, &vars ), white ); + vars.incrementVariablesOnItem(); + QCOMPARE( colorNode.color( nullptr, &vars ), white ); + + { + Variable c1( Variable::Type::COLOR, "c1", "red", Variable::Increment::PER_ITEM ); + vars.addVariable( c1 ); + } + QCOMPARE( colorNode.color( nullptr, &vars ), red ); + + { + Variable c1( Variable::Type::COLOR, "c1", "#8000FF00", Variable::Increment::PER_ITEM ); + vars.addVariable( c1 ); + } + QCOMPARE( colorNode.color( nullptr, &vars ), green80 ); + + colorNode.setKey( "c2" ); + QCOMPARE( colorNode.color( &record, nullptr ), silver80 ); + + { + Variable c2( Variable::Type::COLOR, "c2", "#8000FF00", Variable::Increment::PER_ITEM ); + vars.addVariable( c2 ); + } + QCOMPARE( colorNode.color( nullptr, &vars ), green80 ); + + /// + /// Record beats variable + /// + record["c2"] = "red"; + QCOMPARE( colorNode.color( &record, &vars ), red ); } diff --git a/model/unit_tests/TestModel.cpp b/model/unit_tests/TestModel.cpp index 31fc103..f52b4c4 100644 --- a/model/unit_tests/TestModel.cpp +++ b/model/unit_tests/TestModel.cpp @@ -318,6 +318,19 @@ void TestModel::saveRestore() QCOMPARE( model->merge(), merge ); QVERIFY( model->isModified() ); + // + // Add some variables + // + model->clearModified(); + QVERIFY( !model->isModified() ); + + Variable i( Variable::Type::INTEGER, "i", "2", Variable::Increment::PER_ITEM, "2" ); + Variable f( Variable::Type::FLOATING_POINT, "f", "6.54", Variable::Increment::PER_COPY, "0.12" ); + model->variables()->addVariable( i ); + QVERIFY( model->isModified() ); + model->variables()->addVariable( f ); + QVERIFY( model->isModified() ); + model->clearModified(); QVERIFY( !model->isModified() ); @@ -361,6 +374,7 @@ void TestModel::saveRestore() Model* saved = model->save(); QVERIFY( saved->isModified() ); QCOMPARE( saved->merge(), model->merge() ); // Shared + QCOMPARE( saved->variables(), model->variables() ); // Shared QCOMPARE( saved->isModified(), model->isModified() ); QCOMPARE( saved->shortName(), modelShortName ); QCOMPARE( saved->shortName(), model->shortName() ); @@ -410,6 +424,10 @@ void TestModel::saveRestore() Model* modified = model->save(); QCOMPARE( modified->merge(), merge2 ); // Shared + Variable c( Variable::Type::COLOR, "c", "blue", Variable::Increment::PER_PAGE ); + model->variables()->addVariable( c ); + QCOMPARE( model->variables(), saved->variables() ); // Shared. + // Verify differences QVERIFY( model->shortName() != modelShortName ); QVERIFY( model->shortName() != saved->shortName() ); @@ -442,6 +460,7 @@ void TestModel::saveRestore() QCOMPARE( model->merge(), merge2 ); // Unchanged QVERIFY( model->merge() != saved->merge() ); // NOTE saved->merge() now points to deleted object + QCOMPARE( model->variables(), saved->variables() ); // Unchanged // Unrestore model->restore( modified ); @@ -457,6 +476,7 @@ void TestModel::saveRestore() QVERIFY( model->objectList().at(0)->x0() != saved->objectList().at(0)->x0() ); QVERIFY( model->objectList().at(0)->y0() != saved->objectList().at(0)->y0() ); QCOMPARE( model->merge(), merge2 ); // Same + QCOMPARE( model->variables(), saved->variables() ); // Same QCOMPARE( model->shortName(), modified->shortName() ); QCOMPARE( model->fileName(), modified->fileName() ); @@ -469,6 +489,7 @@ void TestModel::saveRestore() QCOMPARE( model->objectList().at(0)->y0(), modified->objectList().at(0)->y0() ); delete model->merge(); // Final instance owned by us + delete model->variables(); // Instance owned by us delete model; delete saved; delete modified; diff --git a/model/unit_tests/TestModelImageObject.cpp b/model/unit_tests/TestModelImageObject.cpp new file mode 100644 index 0000000..d947d1c --- /dev/null +++ b/model/unit_tests/TestModelImageObject.cpp @@ -0,0 +1,150 @@ +/* TestModelImageObject.cpp + * + * Copyright (C) 2019 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 "TestModelImageObject.h" +#include "Test_Constants.h" + +#include "model/Model.h" +#include "model/ModelImageObject.h" +#include "model/Size.h" + +#include "merge/Factory.h" +#include "merge/TextCsvKeys.h" +#include "merge/Record.h" + +#include + + +QTEST_MAIN(TestModelImageObject) + +using namespace glabels::model; +using namespace glabels::merge; + + +void TestModelImageObject::initTestCase() +{ + Factory::init(); +} + + +void TestModelImageObject::readImageFile() +{ + QImage paintDevice( 10, 160, QImage::Format_RGB32 ); + paintDevice.fill( Qt::white ); + QPainter painter( &paintDevice ); + + Model model; + + ModelImageObject object; + object.setX0( 1 ); + object.setY0( 1 ); + object.setSize( 8, 8 ); + TextNode filenameNode( true, "image" ); + object.setFilenameNode( filenameNode ); + + model.addObject( object.clone() ); + + object.setY0( 11 ); + object.setSize( 8, 8 ); + TextNode filenameNode2( true, "var" ); + object.setFilenameNode( filenameNode2 ); + + model.addObject( object.clone() ); + + // Blue 8x8 square + QByteArray pngArray = QByteArray::fromBase64( glabels::test::blue_8x8_png ); + QImage png; + QVERIFY( png.loadFromData( pngArray, "PNG" ) ); + QTemporaryFile png1; png1.open(); png1.close(); png.save( png1.fileName(), "PNG" ); + QTemporaryFile png2; png2.open(); png2.close(); png.save( png2.fileName(), "PNG" ); + + // Red 8x8 square + QByteArray svg = glabels::test::red_8x8_svg; + QString svgTemplate = QDir::tempPath().append( QDir::separator() ).append( "TestModelImageObject_XXXXXX.svg" ); + QTemporaryFile svg1( svgTemplate ); svg1.open(); svg1.write( svg ); svg1.close(); + QTemporaryFile svg2( svgTemplate ); svg2.open(); svg2.write( svg ); svg2.close(); + + QFileInfo png1FileInfo( png1.fileName() ); + QFileInfo png2FileInfo( png2.fileName() ); + QFileInfo svg1FileInfo( svg1.fileName() ); + QFileInfo svg2FileInfo( svg2.fileName() ); + + QString modelFileName = png1FileInfo.dir().absolutePath().append( QDir::separator() ).append( "TestModelImageObject.glabels" ); + model.setFileName( modelFileName ); + + QTemporaryFile csv; + csv.open(); + csv.write( "id,image,type\n" ); + csv.write( "1," ); csv.write( png1FileInfo.fileName().toUtf8() ); csv.write( ",png\n" ); + csv.write( "2," ); csv.write( png1FileInfo.filePath().toUtf8() ); csv.write( ",png\n" ); + csv.write( "3," ); csv.write( svg1FileInfo.fileName().toUtf8() ); csv.write( ",svg\n" ); + csv.write( "4," ); csv.write( svg2FileInfo.filePath().toUtf8() ); csv.write( ",svg\n" ); + csv.write( "5," ); csv.write( svg2FileInfo.fileName().toUtf8() ); csv.write( ",svg\n" ); + csv.write( "6," ); csv.write( png2FileInfo.fileName().toUtf8() ); csv.write( ",png\n" ); + csv.write( "7," ); csv.write( svg1FileInfo.filePath().toUtf8() ); csv.write( ",svg\n" ); + csv.write( "8," ); csv.write( png2FileInfo.filePath().toUtf8() ); csv.write( ",png\n" ); + csv.close(); + + Merge* merge = Factory::createMerge( TextCsvKeys::id() ); + QVERIFY( merge ); + QCOMPARE( merge->id(), TextCsvKeys::id() ); + merge->setSource( csv.fileName() ); + model.setMerge( merge ); + + // Green 8x8 square + pngArray = QByteArray::fromBase64( glabels::test::green_8x8_png ); + QVERIFY( png.loadFromData( pngArray, "PNG" ) ); + QTemporaryFile png3; png3.open(); png3.close(); png.save( png3.fileName(), "PNG" ); + QFileInfo png3FileInfo( png3.fileName() ); + + Variable var( Variable::Type::STRING, "var", png3FileInfo.fileName(), Variable::Increment::PER_ITEM ); + model.variables()->addVariable( var ); + + const QList records = merge->selectedRecords(); + QCOMPARE( records.size(), 8 ); + int i, cnt; + for ( i = 0, cnt = records.size(); i < cnt; i++ ) + { + model.draw( &painter, false, records[i], model.variables() ); + painter.translate( 0, 20 ); + } + paintDevice.save( "/tmp/readimagefile.png", "PNG" ); + + QColor color, white = Qt::white; + for ( i = 0, cnt = records.size(); i < cnt; i++ ) + { + qDebug() << "record" << i; + color = records[i]->value( "type" ) == "png" ? Qt::blue : Qt::red; + QCOMPARE( white, paintDevice.pixelColor( 1, 0 + i * 20 ) ); + QCOMPARE( color, paintDevice.pixelColor( 1, 1 + i * 20 ) ); + QCOMPARE( color, paintDevice.pixelColor( 1, 8 + i * 20 ) ); + QCOMPARE( white, paintDevice.pixelColor( 1, 9 + i * 20 ) ); + + // Variable + color = Qt::green; + QCOMPARE( white, paintDevice.pixelColor( 1, 10 + i * 20 ) ); + QCOMPARE( color, paintDevice.pixelColor( 1, 11 + i * 20 ) ); + QCOMPARE( color, paintDevice.pixelColor( 1, 18 + i * 20 ) ); + QCOMPARE( white, paintDevice.pixelColor( 1, 19 + i * 20 ) ); + } + + delete model.merge(); + delete model.variables(); +} diff --git a/model/unit_tests/TestModelImageObject.h b/model/unit_tests/TestModelImageObject.h new file mode 100644 index 0000000..b508d1d --- /dev/null +++ b/model/unit_tests/TestModelImageObject.h @@ -0,0 +1,31 @@ +/* TestModelImageObject.h + * + * Copyright (C) 2019 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 TestModelImageObject : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + void readImageFile(); +}; diff --git a/model/unit_tests/TestTextNode.cpp b/model/unit_tests/TestTextNode.cpp index 397190b..19b2532 100644 --- a/model/unit_tests/TestTextNode.cpp +++ b/model/unit_tests/TestTextNode.cpp @@ -22,8 +22,6 @@ #include "model/TextNode.h" -#include "merge/Record.h" - #include @@ -36,6 +34,7 @@ using namespace glabels::merge; void TestTextNode::textNode() { Record record; + Variables vars; TextNode textNode; QVERIFY( !textNode.isField() ); @@ -44,6 +43,8 @@ void TestTextNode::textNode() QVERIFY( !(textNode != TextNode()) ); QCOMPARE( textNode.text( nullptr, nullptr ), QString( "" ) ); QCOMPARE( textNode.text( &record, nullptr ), QString( "" ) ); + QCOMPARE( textNode.text( nullptr, &vars ), QString( "" ) ); + QCOMPARE( textNode.text( &record, &vars ), QString( "" ) ); textNode.setField( true ); QVERIFY( textNode.isField() ); @@ -56,10 +57,14 @@ void TestTextNode::textNode() QCOMPARE( textNode.data(), QString( "data1" ) ); QCOMPARE( textNode.text( nullptr, nullptr ), QString( "data1" ) ); QCOMPARE( textNode.text( &record, nullptr ), QString( "data1" ) ); + QCOMPARE( textNode.text( nullptr, &vars ), QString( "data1" ) ); + QCOMPARE( textNode.text( &record, &vars ), QString( "data1" ) ); textNode.setField( true ); QCOMPARE( textNode.text( nullptr, nullptr ), QString( "" ) ); QCOMPARE( textNode.text( &record, nullptr ), QString( "" ) ); + QCOMPARE( textNode.text( nullptr, &vars ), QString( "" ) ); + QCOMPARE( textNode.text( &record, &vars ), QString( "" ) ); /// /// Constructors @@ -88,4 +93,32 @@ void TestTextNode::textNode() record["key1"] = "val1"; QCOMPARE( textNode.text( &record, nullptr ), QString( "val1" ) ); + + /// + /// Variable + /// + { + Variable key1( Variable::Type::STRING, "key1", "", Variable::Increment::PER_ITEM ); + vars.addVariable( key1 ); + } + QCOMPARE( textNode.text( nullptr, &vars ), QString( "" ) ); + + { + Variable key1( Variable::Type::STRING, "key1", "val1", Variable::Increment::PER_ITEM ); + vars.addVariable( key1 ); + } + QCOMPARE( textNode.text( nullptr, &vars ), QString( "val1" ) ); + + { + Variable key1( Variable::Type::INTEGER, "key1", "1", Variable::Increment::PER_ITEM, "1" ); + vars.addVariable( key1 ); + } + QCOMPARE( textNode.text( nullptr, &vars ), QString( "1" ) ); + vars.incrementVariablesOnItem(); + QCOMPARE( textNode.text( nullptr, &vars ), QString( "2" ) ); + + /// + /// Record beats variable + /// + QCOMPARE( textNode.text( &record, &vars ), QString( "val1" ) ); } diff --git a/model/unit_tests/TestVariable.cpp b/model/unit_tests/TestVariable.cpp new file mode 100644 index 0000000..85efadc --- /dev/null +++ b/model/unit_tests/TestVariable.cpp @@ -0,0 +1,300 @@ +/* TestVariable.cpp + * + * Copyright (C) 2019 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 "TestVariable.h" + +#include "model/Variable.h" + +#include + + +QTEST_MAIN(TestVariable) + +using namespace glabels::model; + + +void TestVariable::variable() +{ + { + Variable var; + + QCOMPARE( var.type(), Variable::Type::STRING ); + QCOMPARE( var.name(), QString() ); + QCOMPARE( var.initialValue(), QString() ); + QCOMPARE( var.increment(), Variable::Increment::NEVER ); + QCOMPARE( var.stepSize(), QString( "0" ) ); + QCOMPARE( var.value(), QString() ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::STRING ); + QCOMPARE( var.name(), QString() ); + QCOMPARE( var.initialValue(), QString() ); + QCOMPARE( var.increment(), Variable::Increment::NEVER ); + QCOMPARE( var.stepSize(), QString( "0" ) ); + QCOMPARE( var.value(), QString() ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString() ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString() ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString() ); + } + { + Variable var( Variable::Type::STRING, "s", "initial", Variable::Increment::PER_ITEM, "2" ); + + QCOMPARE( var.type(), Variable::Type::STRING ); + QCOMPARE( var.name(), QString( "s" ) ); + QCOMPARE( var.initialValue(), QString( "initial" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "initial" ) ); + + var.resetValue(); + QCOMPARE( var.name(), QString( "s" ) ); + QCOMPARE( var.initialValue(), QString( "initial" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "initial" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "initial" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "initial" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "initial" ) ); + + var.resetValue(); + QCOMPARE( var.name(), QString( "s" ) ); + QCOMPARE( var.initialValue(), QString( "initial" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "initial" ) ); + } + { + Variable var( Variable::Type::INTEGER, "i", "123", Variable::Increment::PER_ITEM, "1" ); + + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "123" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "1" ) ); + QCOMPARE( var.value(), QString( "123" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "123" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "1" ) ); + QCOMPARE( var.value(), QString( "123" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "124" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "124" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "124" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "125" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "125" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "125" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "123" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_ITEM ); + QCOMPARE( var.stepSize(), QString( "1" ) ); + QCOMPARE( var.value(), QString( "123" ) ); + } + { + Variable var( Variable::Type::INTEGER, "i", "1", Variable::Increment::PER_PAGE, "2" ); + + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "1" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "1" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "1" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "1" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "1" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "1" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "3" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "3" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "3" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "5" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::INTEGER ); + QCOMPARE( var.name(), QString( "i" ) ); + QCOMPARE( var.initialValue(), QString( "1" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "2" ) ); + QCOMPARE( var.value(), QString( "1" ) ); + } + { + Variable var( Variable::Type::FLOATING_POINT, "f", "1.2", Variable::Increment::PER_COPY, "0.2" ); + + QCOMPARE( var.type(), Variable::Type::FLOATING_POINT ); + QCOMPARE( var.name(), QString( "f" ) ); + QCOMPARE( var.initialValue(), QString( "1.2" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_COPY ); + QCOMPARE( var.stepSize(), QString( "0.2" ) ); + QCOMPARE( var.value(), QString( "1.2" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::FLOATING_POINT ); + QCOMPARE( var.name(), QString( "f" ) ); + QCOMPARE( var.initialValue(), QString( "1.2" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_COPY ); + QCOMPARE( var.stepSize(), QString( "0.2" ) ); + QCOMPARE( var.value(), QString( "1.2" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "1.2" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "1.4" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "1.4" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "1.4" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "1.6" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "1.6" ) ); + + var.resetValue(); + QCOMPARE( var.type(), Variable::Type::FLOATING_POINT ); + QCOMPARE( var.name(), QString( "f" ) ); + QCOMPARE( var.initialValue(), QString( "1.2" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_COPY ); + QCOMPARE( var.stepSize(), QString( "0.2" ) ); + QCOMPARE( var.value(), QString( "1.2" ) ); + } + { + Variable var( Variable::Type::COLOR, "c", "white", Variable::Increment::PER_PAGE ); + + QCOMPARE( var.type(), Variable::Type::COLOR ); + QCOMPARE( var.name(), QString( "c" ) ); + QCOMPARE( var.initialValue(), QString( "white" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "0" ) ); + QCOMPARE( var.value(), QString( "white" ) ); + + var.resetValue(); + QCOMPARE( var.name(), QString( "c" ) ); + QCOMPARE( var.initialValue(), QString( "white" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "0" ) ); + QCOMPARE( var.value(), QString( "white" ) ); + + var.incrementValueOnItem(); + QCOMPARE( var.value(), QString( "white" ) ); + + var.incrementValueOnCopy(); + QCOMPARE( var.value(), QString( "white" ) ); + + var.incrementValueOnPage(); + QCOMPARE( var.value(), QString( "white" ) ); + + var.resetValue(); + QCOMPARE( var.name(), QString( "c" ) ); + QCOMPARE( var.initialValue(), QString( "white" ) ); + QCOMPARE( var.increment(), Variable::Increment::PER_PAGE ); + QCOMPARE( var.stepSize(), QString( "0" ) ); + QCOMPARE( var.value(), QString( "white" ) ); + } +} + + +void TestVariable::statics() +{ + QCOMPARE( Variable::typeToI18nString( Variable::Type::STRING ), QString( "String" ) ); + QCOMPARE( Variable::typeToI18nString( Variable::Type::INTEGER ), QString( "Integer" ) ); + QCOMPARE( Variable::typeToI18nString( Variable::Type::FLOATING_POINT ), QString( "Floating Point" ) ); + QCOMPARE( Variable::typeToI18nString( Variable::Type::COLOR ), QString( "Color" ) ); + QCOMPARE( Variable::typeToI18nString( (Variable::Type)4 ), QString( "String" ) ); + + QCOMPARE( Variable::typeToIdString( Variable::Type::STRING ), QString( "string" ) ); + QCOMPARE( Variable::typeToIdString( Variable::Type::INTEGER ), QString( "integer" ) ); + QCOMPARE( Variable::typeToIdString( Variable::Type::FLOATING_POINT ), QString( "float" ) ); + QCOMPARE( Variable::typeToIdString( Variable::Type::COLOR ), QString( "color" ) ); + QCOMPARE( Variable::typeToIdString( (Variable::Type)4 ), QString( "string" ) ); + + QCOMPARE( Variable::idStringToType( "string" ), Variable::Type::STRING ); + QCOMPARE( Variable::idStringToType( "integer"), Variable::Type::INTEGER ); + QCOMPARE( Variable::idStringToType( "float" ), Variable::Type::FLOATING_POINT ); + QCOMPARE( Variable::idStringToType( "color" ), Variable::Type::COLOR ); + QCOMPARE( Variable::idStringToType( "non_existent" ), Variable::Type::STRING ); + + QCOMPARE( Variable::incrementToI18nString( Variable::Increment::NEVER ), QString( "Never" ) ); + QCOMPARE( Variable::incrementToI18nString( Variable::Increment::PER_ITEM ), QString( "Per item" ) ); + QCOMPARE( Variable::incrementToI18nString( Variable::Increment::PER_COPY ), QString( "Per copy" ) ); + QCOMPARE( Variable::incrementToI18nString( Variable::Increment::PER_PAGE ), QString( "Per page" ) ); + QCOMPARE( Variable::incrementToI18nString( (Variable::Increment)4 ), QString( "Never" ) ); + + QCOMPARE( Variable::incrementToIdString( Variable::Increment::NEVER ), QString( "never" ) ); + QCOMPARE( Variable::incrementToIdString( Variable::Increment::PER_ITEM ), QString( "per_item" ) ); + QCOMPARE( Variable::incrementToIdString( Variable::Increment::PER_COPY ), QString( "per_copy" ) ); + QCOMPARE( Variable::incrementToIdString( Variable::Increment::PER_PAGE ), QString( "per_page" ) ); + QCOMPARE( Variable::incrementToIdString( (Variable::Increment)4 ), QString( "never" ) ); + + QCOMPARE( Variable::idStringToIncrement( "never" ), Variable::Increment::NEVER ); + QCOMPARE( Variable::idStringToIncrement( "per_item" ), Variable::Increment::PER_ITEM ); + QCOMPARE( Variable::idStringToIncrement( "per_copy" ), Variable::Increment::PER_COPY ); + QCOMPARE( Variable::idStringToIncrement( "per_page" ), Variable::Increment::PER_PAGE ); + QCOMPARE( Variable::idStringToIncrement( "non_existent" ), Variable::Increment::NEVER ); +} diff --git a/model/unit_tests/TestVariable.h b/model/unit_tests/TestVariable.h new file mode 100644 index 0000000..9cad55d --- /dev/null +++ b/model/unit_tests/TestVariable.h @@ -0,0 +1,31 @@ +/* TestVariable.h + * + * Copyright (C) 2019 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 TestVariable : public QObject +{ + Q_OBJECT + +private slots: + void variable(); + void statics(); +}; diff --git a/model/unit_tests/TestVariables.cpp b/model/unit_tests/TestVariables.cpp new file mode 100644 index 0000000..c7f6ce4 --- /dev/null +++ b/model/unit_tests/TestVariables.cpp @@ -0,0 +1,157 @@ +/* TestVariables.cpp + * + * Copyright (C) 2019 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 "TestVariables.h" + +#include "model/Variables.h" + +#include + + +QTEST_MAIN(TestVariables) + +using namespace glabels::model; + + +void TestVariables::variables() +{ + Variables vars; + + Variable i( Variable::Type::INTEGER, "i", "3", Variable::Increment::PER_ITEM, "3" ); + QCOMPARE( i.value(), QString( "3" ) ); + + Variable i2( Variable::Type::INTEGER, "i2", "100", Variable::Increment::PER_COPY, "2" ); + QCOMPARE( i2.value(), QString( "100" ) ); + + Variable f( Variable::Type::FLOATING_POINT, "f", "0.0", Variable::Increment::PER_PAGE, "0.1" ); + QCOMPARE( f.value(), QString( "0" ) ); + + Variable s( Variable::Type::STRING, "s", "initial", Variable::Increment::PER_ITEM, "1" ); + QCOMPARE( s.value(), QString( "initial" ) ); + + Variable c( Variable::Type::COLOR, "c", "white", Variable::Increment::PER_ITEM, "01" ); + QCOMPARE( c.value(), QString( "white" ) ); + + QVERIFY( !vars.hasVariable( "i" ) ); + QVERIFY( !vars.hasVariable( "i2" ) ); + + // Add, delete + vars.addVariable( i ); + QVERIFY( vars.hasVariable( "i" ) ); + QCOMPARE( vars["i"].value(), i.value() ); + vars.deleteVariable( "i" ); + QVERIFY( !vars.hasVariable( "i" ) ); + QCOMPARE( vars["i"].value(), QString() ); + + // Add, replace + vars.addVariable( i ); + QVERIFY( vars.hasVariable( "i" ) ); + QCOMPARE( vars["i"].value(), i.value() ); + vars.replaceVariable( "i", i2 ); + QVERIFY( !vars.hasVariable( "i" ) ); + QVERIFY( vars.hasVariable( "i2" ) ); + QCOMPARE( vars["i2"].value(), i2.value() ); + QCOMPARE( vars["i"].value(), QString() ); + vars.deleteVariable( "i2" ); + QVERIFY( !vars.hasVariable( "i2" ) ); + + // Increment + vars.addVariable( i ); + vars.addVariable( i2 ); + vars.addVariable( f ); + vars.addVariable( s ); + vars.addVariable( c ); + + QVERIFY( vars.hasVariable( "i" ) ); // PER_ITEM + QVERIFY( vars.hasVariable( "i2" ) ); // PER_COPY + QVERIFY( vars.hasVariable( "f" ) ); // PER_PAGE + QVERIFY( vars.hasVariable( "s" ) ); + QVERIFY( vars.hasVariable( "c" ) ); + + QCOMPARE( vars["i"].value(), QString( "3" ) ); + QCOMPARE( vars["i2"].value(), QString( "100" ) ); // PRE_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.resetVariables(); + + QCOMPARE( vars["i"].value(), QString( "3" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "100" ) ); // PRE_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnItem(); + + QCOMPARE( vars["i"].value(), QString( "6" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "100" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnItem(); + + QCOMPARE( vars["i"].value(), QString( "9" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "100" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnCopy(); + + QCOMPARE( vars["i"].value(), QString( "9" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "102" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnCopy(); + + QCOMPARE( vars["i"].value(), QString( "9" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "104" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnPage(); + + QCOMPARE( vars["i"].value(), QString( "9" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "104" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0.1" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.incrementVariablesOnPage(); + + QCOMPARE( vars["i"].value(), QString( "9" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "104" ) ); // PER_COPY + QCOMPARE( vars["f"].value(), QString( "0.2" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); + + vars.resetVariables(); + + QCOMPARE( vars["i"].value(), QString( "3" ) ); // PER_ITEM + QCOMPARE( vars["i2"].value(), QString( "100" ) ); // PRE_COPY + QCOMPARE( vars["f"].value(), QString( "0" ) ); // PER_PAGE + QCOMPARE( vars["s"].value(), QString( "initial" ) ); + QCOMPARE( vars["c"].value(), QString( "white" ) ); +} diff --git a/model/unit_tests/TestVariables.h b/model/unit_tests/TestVariables.h new file mode 100644 index 0000000..93b56b4 --- /dev/null +++ b/model/unit_tests/TestVariables.h @@ -0,0 +1,30 @@ +/* TestVariables.h + * + * Copyright (C) 2019 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 TestVariables : public QObject +{ + Q_OBJECT + +private slots: + void variables(); +}; diff --git a/model/unit_tests/TestXmlLabel.cpp b/model/unit_tests/TestXmlLabel.cpp index db643ab..01a468d 100644 --- a/model/unit_tests/TestXmlLabel.cpp +++ b/model/unit_tests/TestXmlLabel.cpp @@ -19,12 +19,14 @@ */ #include "TestXmlLabel.h" +#include "Test_Constants.h" #include "model/XmlLabelCreator.h" #include "model/XmlLabelParser.h" #include "barcode/Backends.h" #include "model/ColorNode.h" +#include "model/FrameRect.h" #include "model/Model.h" #include "model/Size.h" @@ -40,19 +42,19 @@ QTEST_MAIN(TestXmlLabel) +using namespace glabels::model; +using namespace glabels::barcode; + void TestXmlLabel::initTestCase() { - using namespace glabels::barcode; Backends::init(); + Settings::init(); } void TestXmlLabel::serializeDeserialize() { - using namespace glabels::model; - using namespace glabels::barcode; - // // Empty object list // @@ -74,8 +76,9 @@ void TestXmlLabel::serializeDeserialize() 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 = ""; + QImage png; + QVERIFY( png.loadFromData( QByteArray::fromBase64( glabels::test::blue_8x8_png ), "PNG" ) ); + QByteArray svg = glabels::test::red_8x8_svg; Style bcStyle = Backends::defaultStyle(); objects << new ModelBoxObject( 0, 1, 10, 20, lock, 2, red, green, tMatrix, shadow, 1, 2, 0.7, black ); @@ -84,6 +87,7 @@ void TestXmlLabel::serializeDeserialize() 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 ); + QTest::ignoreMessage( QtWarningMsg, QRegularExpression( "^Embedded file \"[^\"]+image5.jpg\" missing\\. Trying actual file\\.$" ) ); 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, @@ -175,4 +179,64 @@ void TestXmlLabel::serializeDeserialize() outBuffer.clear(); XmlLabelCreator::serializeObjects( outObjects, model, outBuffer ); QCOMPARE( buffer, outBuffer ); + + /// + /// Write to file and read + /// + QString glabelsTemplate = QDir::tempPath().append( QDir::separator() ).append( "TestXmlLabel_XXXXXX.glabels" ); + QTemporaryFile glabels( glabelsTemplate ); + glabels.open(); glabels.close(); + + // Add template + Template tmplate( "Test Brand", "part", "desc", "testPaperId", 110, 410 ); + FrameRect* frame = new FrameRect( 120, 220, 5, 0, 0, "rect1" ); + tmplate.addFrame( frame ); + model->setTmplate( &tmplate ); // Copies + + // Add variables + Variables vars; + Variable s( Variable::Type::STRING, "s", "initial", Variable::Increment::NEVER ); + Variable c( Variable::Type::COLOR, "c", "red", Variable::Increment::PER_COPY ); + Variable i( Variable::Type::INTEGER, "i", "123", Variable::Increment::PER_ITEM, "1" ); + model->variables()->addVariable( s ); + model->variables()->addVariable( c ); + model->variables()->addVariable( i ); + QCOMPARE( model->variables()->size(), 3 ); + + model->setRotate( true ); + QVERIFY( model->rotate() ); + + XmlLabelCreator::writeFile( model, glabels.fileName() ); + + QCOMPARE( model->dir(), QFileInfo( glabels.fileName() ).dir() ); + + Model* readModel = XmlLabelParser::readFile( glabels.fileName() ); + QVERIFY( readModel ); + QCOMPARE( readModel->dir(), model->dir() ); + QCOMPARE( readModel->fileName(), model->fileName() ); + QCOMPARE( readModel->tmplate()->brand(), model->tmplate()->brand() ); + QCOMPARE( readModel->tmplate()->part(), model->tmplate()->part() ); + QCOMPARE( readModel->tmplate()->description(), model->tmplate()->description() ); + QCOMPARE( readModel->tmplate()->paperId(), model->tmplate()->paperId() ); + QCOMPARE( readModel->tmplate()->pageWidth().pt(), model->tmplate()->pageWidth().pt() ); + QCOMPARE( readModel->tmplate()->pageHeight().pt(), model->tmplate()->pageHeight().pt() ); + QCOMPARE( readModel->frame()->id(), model->frame()->id() ); + QCOMPARE( readModel->frame()->w().pt(), model->frame()->w().pt() ); + QCOMPARE( readModel->frame()->h().pt(), model->frame()->h().pt() ); + QCOMPARE( readModel->rotate(), model->rotate() ); + QCOMPARE( readModel->w(), model->w() ); + QCOMPARE( readModel->h(), model->h() ); + QCOMPARE( readModel->objectList(), model->objectList() ); + QCOMPARE( readModel->variables()->size(), model->variables()->size() ); + QCOMPARE( readModel->merge()->id(), model->merge()->id() ); + QCOMPARE( readModel->merge()->source(), model->merge()->source() ); + QCOMPARE( readModel->merge()->recordList(), model->merge()->recordList() ); + + delete readModel->merge(); + delete readModel->variables(); + delete readModel; + + delete model->merge(); + delete model->variables(); + delete model; } diff --git a/model/unit_tests/Test_Constants.h b/model/unit_tests/Test_Constants.h new file mode 100644 index 0000000..b9f063b --- /dev/null +++ b/model/unit_tests/Test_Constants.h @@ -0,0 +1,38 @@ +/* Test_Constants.h + * + * Copyright (C) 2019 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 . + */ + +#ifndef test_Constants_h +#define test_Constants_h + + +namespace glabels +{ + namespace test + { + + const char* blue_8x8_png = "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw1AUhU9TpVIqDhYRcchQnSyIijhqFYpQIdQKrTqYvPQPmjQkKS6OgmvBwZ/FqoOLs64OroIg+APi4uqk6CIl3pcUWsR44fE+zrvn8N59gNCoMM3qGgc03TbTyYSYza2KoVeEEMYAAhBkZhlzkpSCb33dUx/VXZxn+ff9Wb1q3mJAQCSeZYZpE28QT2/aBud94igrySrxOfGYSRckfuS64vEb56LLAs+Mmpn0PHGUWCx2sNLBrGRqxFPEMVXTKV/Ieqxy3uKsVWqsdU/+wkheX1nmOq1hJLGIJUgQoaCGMiqwEaddJ8VCms4TPv4h1y+RSyFXGYwcC6hCg+z6wf/g92ytwuSElxRJAN0vjvMxAoR2gWbdcb6PHad5AgSfgSu97a82gJlP0uttLXYE9G0DF9dtTdkDLneAwSdDNmVXCtISCgXg/Yy+KQf03wLhNW9urXOcPgAZmlXqBjg4BEaLlL3u8+6ezrn929Oa3w/Q2XJm1/XlIwAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+MHChYzAoNXJCYAAAAWSURBVBjTY2Rg+P+fAQ9gYiAAhocCABBdAg7zMxsKAAAAAElFTkSuQmCC"; + const char* green_8x8_png = "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kTtIw1AUhv+mikUqDlYQcchQnSz4Qhy1CkWoEGqFVh1MbvqCJg1Jiouj4Fpw8LFYdXBx1tXBVRAEHyAurk6KLlLiuUmhRYwXDvfjv/f/OfdcQKiXmWZ1jAGabpupRFzMZFfFrleE0E81jpDMLGNOkpLwXV/3CPD9Lsaz/O/9uXrUnMWAgEg8ywzTJt4gnt60Dc77xBFWlFXic+JRkxokfuS64vEb54LLAs+MmOnUPHGEWCy0sdLGrGhqxFPEUVXTKV/IeKxy3uKslaus2Sd/YTinryxznWoICSxiCRJEKKiihDJsxGjXSbGQovO4j3/Q9UvkUshVAiPHAirQILt+8D/4PVsrPznhJYXjQOeL43wMA127QKPmON/HjtM4AYLPwJXe8lfqwMwn6bWWFj0CereBi+uWpuwBlzvAwJMhm7IrBamEfB54P6NvygJ9t0D3mje35jlOH4A0zSp5AxwcAiMFyl73eXeofW7/3mnO7wdSvnKatbS90wAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB+MIFQovFXnldrAAAAAWSURBVBjTY2T4z/CfAQ9gYiAAhocCABFcAg5KXrI7AAAAAElFTkSuQmCC"; + const char* red_8x8_svg = ""; + + } +} + + +#endif // test_Constants_h