Implemented variable substitution in simple print jobs.

This commit is contained in:
Jim Evins
2019-03-24 17:49:41 -04:00
parent dedbe07312
commit 37f0a8890d
34 changed files with 524 additions and 193 deletions
+29 -13
View File
@@ -29,8 +29,9 @@ namespace
{
// All variable types. (must be in sorted order)
const QVector<glabels::model::Variable::Type> allTypes = {
glabels::model::Variable::Type::NUMERIC,
glabels::model::Variable::Type::STRING
glabels::model::Variable::Type::STRING,
glabels::model::Variable::Type::INTEGER,
glabels::model::Variable::Type::FLOATING_POINT
};
// All variable increments. (must be in sorted order)
@@ -76,7 +77,7 @@ namespace glabels
{
typeCombo->setCurrentIndex( static_cast<int>(variable.type()) );
nameEdit->setText( variable.name() );
valueEdit->setText( variable.value() );
valueEdit->setText( variable.initialValue() );
incrementCombo->setCurrentIndex( static_cast<int>(variable.increment()) );
stepSizeEdit->setText( variable.stepSize() );
@@ -150,8 +151,20 @@ namespace glabels
auto type = static_cast<model::Variable::Type>(typeCombo->currentIndex());
auto increment = static_cast<model::Variable::Increment>(incrementCombo->currentIndex());
if ( type == model::Variable::Type::NUMERIC )
switch (type)
{
case model::Variable::Type::INTEGER:
valueEdit->setValidator( new QIntValidator() );
stepSizeEdit->setValidator( new QIntValidator() );
if ( increment == model::Variable::Increment::NEVER )
{
stepSizeEdit->setText( "0" );
}
break;
case model::Variable::Type::FLOATING_POINT:
valueEdit->setValidator( new QDoubleValidator() );
stepSizeEdit->setValidator( new QDoubleValidator() );
@@ -159,21 +172,24 @@ namespace glabels
{
stepSizeEdit->setText( "0" );
}
}
else
{
break;
default:
valueEdit->setValidator( nullptr );
stepSizeEdit->setValidator( nullptr );
incrementCombo->setCurrentIndex( static_cast<int>(model::Variable::Increment::NEVER) );
stepSizeEdit->setText( "" );
break;
}
incrementLabel->setEnabled( type == model::Variable::Type::NUMERIC );
incrementCombo->setEnabled( type == model::Variable::Type::NUMERIC );
stepSizeLabel->setEnabled( (type == model::Variable::Type::NUMERIC) &&
(increment != model::Variable::Increment::NEVER) );
stepSizeEdit->setEnabled( (type == model::Variable::Type::NUMERIC) &&
(increment != model::Variable::Increment::NEVER) );
bool isNumeric = ( type == model::Variable::Type::INTEGER ) ||
( type == model::Variable::Type::FLOATING_POINT );
incrementLabel->setEnabled( isNumeric );
incrementCombo->setEnabled( isNumeric );
stepSizeLabel->setEnabled( isNumeric && (increment != model::Variable::Increment::NEVER) );
stepSizeEdit->setEnabled( isNumeric && (increment != model::Variable::Increment::NEVER) );
validateCurrentInputs();
}
+1 -1
View File
@@ -1157,7 +1157,7 @@ namespace glabels
void
LabelEditor::drawObjectsLayer( QPainter* painter )
{
mModel->draw( painter );
mModel->draw( painter, true, nullptr, nullptr );
}
+3 -3
View File
@@ -63,7 +63,7 @@ namespace glabels
typeHeaderItem->setFlags( typeHeaderItem->flags() ^ Qt::ItemIsEditable );
table->setHorizontalHeaderItem( I_COL_TYPE, typeHeaderItem );
auto* valueHeaderItem = new QTableWidgetItem( tr("Value") );
auto* valueHeaderItem = new QTableWidgetItem( tr("Initial Value") );
valueHeaderItem->setFlags( valueHeaderItem->flags() ^ Qt::ItemIsEditable );
table->setHorizontalHeaderItem( I_COL_VALUE, valueHeaderItem );
@@ -122,7 +122,7 @@ namespace glabels
{
EditVariableDialog dialog( this );
model::Variable v( model::Variable::Type::NUMERIC,
model::Variable v( model::Variable::Type::INTEGER,
"x",
"0",
model::Variable::Increment::NEVER,
@@ -216,7 +216,7 @@ namespace glabels
nameItem->setFlags( nameItem->flags() ^ Qt::ItemIsEditable );
table->setItem( iRow, I_COL_NAME, nameItem );
auto* valueItem = new QTableWidgetItem( v.value() );
auto* valueItem = new QTableWidgetItem( v.initialValue() );
valueItem->setFlags( valueItem->flags() ^ Qt::ItemIsEditable );
table->setItem( iRow, I_COL_VALUE, valueItem );
+3 -3
View File
@@ -19,7 +19,7 @@
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Variable Type:</string>
<string>Variable type:</string>
</property>
</widget>
</item>
@@ -39,7 +39,7 @@
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Value:</string>
<string>Initial value:</string>
</property>
</widget>
</item>
@@ -61,7 +61,7 @@
<item>
<widget class="QLabel" name="stepSizeLabel">
<property name="text">
<string>Step Size:</string>
<string>Step size:</string>
</property>
</widget>
</item>
+2 -2
View File
@@ -1476,11 +1476,11 @@ namespace glabels
///
/// Draw label objects
///
void Model::draw( QPainter* painter, bool inEditor, merge::Record* record ) const
void Model::draw( QPainter* painter, bool inEditor, merge::Record* record, Variables* variables ) const
{
foreach ( ModelObject* object, mObjectList )
{
object->draw( painter, inEditor, record );
object->draw( painter, inEditor, record, variables );
}
}
+4 -1
View File
@@ -208,7 +208,10 @@ namespace glabels
// Drawing operations
/////////////////////////////////
public:
void draw( QPainter* painter, bool inEditor = true, merge::Record* record = nullptr ) const;
void draw( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const;
/////////////////////////////////
+8 -5
View File
@@ -311,7 +311,8 @@ namespace glabels
///
void ModelBarcodeObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
// Barcodes don't support shadows.
}
@@ -322,7 +323,8 @@ namespace glabels
///
void ModelBarcodeObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
QColor bcColor = mBcColorNode.color( record );
@@ -332,7 +334,7 @@ namespace glabels
}
else
{
drawBc( painter, bcColor, record );
drawBc( painter, bcColor, record, variables );
}
}
@@ -450,7 +452,8 @@ namespace glabels
void
ModelBarcodeObject::drawBc( QPainter* painter,
const QColor& color,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
painter->setPen( QPen( color ) );
@@ -458,7 +461,7 @@ namespace glabels
bc->setChecksum(mBcChecksumFlag);
bc->setShowText(mBcTextFlag);
bc->build( mBcData.expand( record ).toStdString(), mW.pt(), mH.pt() );
bc->build( mBcData.expand( record, variables ).toStdString(), mW.pt(), mH.pt() );
glbarcode::QtRenderer renderer(painter);
bc->render( renderer );
+16 -3
View File
@@ -126,8 +126,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
@@ -139,7 +147,12 @@ namespace glabels
void update();
void drawBcInEditor( QPainter* painter, const QColor& color ) const;
void drawBc( QPainter* painter, const QColor& color, merge::Record* record ) const;
void drawBc( QPainter* painter,
const QColor& color,
merge::Record* record,
Variables* variables ) const;
void drawPlaceHolder( QPainter* painter, const QColor& color, const QString& text ) const;
+8 -2
View File
@@ -103,7 +103,10 @@ namespace glabels
///
/// Draw shadow of object
///
void ModelBoxObject::drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelBoxObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
QColor fillColor = mFillColorNode.color( record );
@@ -148,7 +151,10 @@ namespace glabels
///
/// Draw object itself
///
void ModelBoxObject::drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelBoxObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
QColor fillColor = mFillColorNode.color( record );
+10 -2
View File
@@ -72,8 +72,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
};
+8 -2
View File
@@ -103,7 +103,10 @@ namespace glabels
///
/// Draw shadow of object
///
void ModelEllipseObject::drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelEllipseObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
QColor fillColor = mFillColorNode.color( record );
@@ -148,7 +151,10 @@ namespace glabels
///
/// Draw object itself
///
void ModelEllipseObject::drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelEllipseObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
QColor fillColor = mFillColorNode.color( record );
+10 -2
View File
@@ -72,8 +72,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
};
+8 -2
View File
@@ -395,7 +395,10 @@ namespace glabels
///
/// Draw shadow of object
///
void ModelImageObject::drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelImageObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QRectF destRect( 0, 0, mW.pt(), mH.pt() );
@@ -424,7 +427,10 @@ namespace glabels
///
/// Draw object itself
///
void ModelImageObject::drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelImageObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QRectF destRect( 0, 0, mW.pt(), mH.pt() );
+10 -2
View File
@@ -132,8 +132,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
+8 -2
View File
@@ -186,7 +186,10 @@ namespace glabels
///
/// Draw shadow of object
///
void ModelLineObject::drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelLineObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
QColor shadowColor = mShadowColorNode.color( record );
@@ -204,7 +207,10 @@ namespace glabels
///
/// Draw object itself
///
void ModelLineObject::drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelLineObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
QColor lineColor = mLineColorNode.color( record );
+10 -2
View File
@@ -97,8 +97,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
+6 -3
View File
@@ -1200,7 +1200,10 @@ namespace glabels
///
/// Draw object + shadow
///
void ModelObject::draw( QPainter* painter, bool inEditor, merge::Record* record ) const
void ModelObject::draw( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const
{
painter->save();
painter->translate( mX0.pt(), mY0.pt() );
@@ -1210,12 +1213,12 @@ namespace glabels
painter->save();
painter->translate( mShadowX.pt(), mShadowY.pt() );
painter->setMatrix( mMatrix, true );
drawShadow( painter, inEditor, record );
drawShadow( painter, inEditor, record, variables );
painter->restore();
}
painter->setMatrix( mMatrix, true );
drawObject( painter, inEditor, record );
drawObject( painter, inEditor, record, variables );
painter->restore();
}
+16 -3
View File
@@ -27,6 +27,7 @@
#include "Handles.h"
#include "Outline.h"
#include "TextNode.h"
#include "Variables.h"
#include "barcode/Style.h"
#include "merge/Record.h"
@@ -403,12 +404,24 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
public:
void draw( QPainter* painter, bool inEditor, merge::Record* record ) const;
void draw( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const;
void drawSelectionHighlight( QPainter* painter, double scale ) const;
protected:
virtual void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const = 0;
virtual void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const = 0;
virtual void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const = 0;
virtual void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const = 0;
virtual QPainterPath hoverPath( double scale ) const = 0;
virtual void sizeUpdated();
+12 -9
View File
@@ -518,7 +518,8 @@ namespace glabels
///
void ModelTextObject::drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
QColor textColor = mTextColorNode.color( record );
@@ -533,7 +534,7 @@ namespace glabels
}
else
{
drawText( painter, shadowColor, record );
drawText( painter, shadowColor, record, variables );
}
}
}
@@ -544,7 +545,8 @@ namespace glabels
///
void ModelTextObject::drawObject( QPainter* painter,
bool inEditor,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
QColor textColor = mTextColorNode.color( record );
@@ -554,7 +556,7 @@ namespace glabels
}
else
{
drawText( painter, textColor, record );
drawText( painter, textColor, record, variables );
}
}
@@ -696,7 +698,8 @@ namespace glabels
void
ModelTextObject::drawText( QPainter* painter,
const QColor& color,
merge::Record* record ) const
merge::Record* record,
Variables* variables ) const
{
painter->save();
@@ -704,7 +707,7 @@ namespace glabels
QFont font;
font.setFamily( mFontFamily );
font.setPointSizeF( mTextAutoShrink ? autoShrinkFontSize( record ) : mFontSize );
font.setPointSizeF( mTextAutoShrink ? autoShrinkFontSize( record, variables ) : mFontSize );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
@@ -716,7 +719,7 @@ namespace glabels
QFontMetricsF fontMetrics( font );
double dy = fontMetrics.lineSpacing() * mTextLineSpacing;
QTextDocument document( mText.expand( record ) );
QTextDocument document( mText.expand( record, variables ) );
QList<QTextLayout*> layouts;
@@ -790,7 +793,7 @@ namespace glabels
/// Determine auto shrink font size
///
double
ModelTextObject::autoShrinkFontSize( merge::Record* record ) const
ModelTextObject::autoShrinkFontSize( merge::Record* record, Variables* variables ) const
{
QFont font;
font.setFamily( mFontFamily );
@@ -802,7 +805,7 @@ namespace glabels
textOption.setAlignment( mTextHAlign );
textOption.setWrapMode( mTextWrapMode );
QTextDocument document( mText.expand( record ) );
QTextDocument document( mText.expand( record, variables ) );
double candidateSize = mFontSize;
while ( candidateSize > 1.0 )
+21 -6
View File
@@ -185,8 +185,16 @@ namespace glabels
// Drawing operations
///////////////////////////////////////////////////////////////
protected:
void drawShadow( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const override;
void drawShadow( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
void drawObject( QPainter* painter,
bool inEditor,
merge::Record* record,
Variables* variables ) const override;
QPainterPath hoverPath( double scale ) const override;
@@ -196,10 +204,17 @@ namespace glabels
private:
void sizeUpdated() override;
void update();
void drawTextInEditor( QPainter* painter, const QColor& color ) const;
void drawText( QPainter* painter, const QColor&color, merge::Record* record ) const;
QString expandText( QString text, merge::Record* record ) const;
double autoShrinkFontSize( merge::Record* record ) const;
void drawTextInEditor( QPainter* painter,
const QColor& color ) const;
void drawText( QPainter* painter,
const QColor& color,
merge::Record* record,
Variables* variables ) const;
double autoShrinkFontSize( merge::Record* record,
Variables* variables ) const;
///////////////////////////////////////////////////////////////
+36 -26
View File
@@ -47,7 +47,7 @@ namespace glabels
PageRenderer::PageRenderer( const Model* model )
: mModel(nullptr), mMerge(nullptr), mNCopies(0), mStartLabel(0), mLastLabel(0),
: mModel(nullptr), mMerge(nullptr), mVariables(nullptr), mNCopies(0), mStartLabel(0), mLastLabel(0),
mPrintOutlines(false), mPrintCropMarks(false), mPrintReverse(false),
mIPage(0), mIsMerge(false), mNPages(0), mNLabelsPerPage(0)
{
@@ -65,6 +65,7 @@ namespace glabels
connect( mModel, SIGNAL(changed()), this, SLOT(onModelChanged()) );
onModelChanged();
mVariables = mModel->variables();
}
@@ -249,37 +250,44 @@ namespace glabels
void PageRenderer::printSimplePage( QPainter* painter, int iPage ) const
{
int iStart = 0;
int iEnd = mNLabelsPerPage;
if ( iPage == 0 )
{
iStart = mStartLabel;
}
if ( (mLastLabel / mNLabelsPerPage) == iPage )
{
iEnd = mLastLabel % mNLabelsPerPage;
}
printCropMarks( painter );
for ( int i = iStart; i < iEnd; i++ )
int iCopy = 0;
int iLabel = mStartLabel;
int iCurrentPage = 0;
mVariables->resetVariables();
while ( (iCopy < mNCopies) && (iCurrentPage <= iPage) )
{
painter->save();
if ( iCurrentPage == iPage )
{
int i = iLabel % mNLabelsPerPage;
painter->save();
painter->translate( mOrigins[i].x().pt(), mOrigins[i].y().pt() );
painter->translate( mOrigins[i].x().pt(), mOrigins[i].y().pt() );
painter->save();
painter->save();
clipLabel( painter );
printLabel( painter, nullptr );
clipLabel( painter );
printLabel( painter, nullptr, mVariables );
painter->restore(); // From before clip
painter->restore(); // From before clip
printOutline( painter );
printOutline( painter );
painter->restore(); // From before translation
painter->restore(); // From before translation
}
iCopy++;
iLabel++;
iCurrentPage = iLabel / mNLabelsPerPage;
mVariables->incrementVariablesOnCopy();
if ( (iLabel % mNLabelsPerPage) == 0 /* starting a new page */ )
{
mVariables->incrementVariablesOnPage();
}
}
}
@@ -317,7 +325,7 @@ namespace glabels
painter->save();
clipLabel( painter );
printLabel( painter, records[iRecord] );
printLabel( painter, records[iRecord], mVariables );
painter->restore(); // From before clip
@@ -411,7 +419,9 @@ namespace glabels
}
void PageRenderer::printLabel( QPainter* painter, merge::Record* record ) const
void PageRenderer::printLabel( QPainter* painter,
merge::Record* record,
Variables* variables ) const
{
painter->save();
@@ -427,7 +437,7 @@ namespace glabels
painter->scale( -1, 1 );
}
mModel->draw( painter, false, record );
mModel->draw( painter, false, record, variables );
painter->restore();
}
+3 -1
View File
@@ -23,6 +23,7 @@
#include "Point.h"
#include "Variables.h"
#include "merge/Merge.h"
#include "merge/Record.h"
@@ -100,7 +101,7 @@ namespace glabels
void printCropMarks( QPainter* painter ) const;
void printOutline( QPainter* painter ) const;
void clipLabel( QPainter* painter ) const;
void printLabel( QPainter* painter, merge::Record* record ) const;
void printLabel( QPainter* painter, merge::Record* record, Variables* variables ) const;
/////////////////////////////////
@@ -109,6 +110,7 @@ namespace glabels
private:
const Model* mModel;
const merge::Merge* mMerge;
Variables* mVariables;
int mNCopies;
int mStartLabel;
+2 -2
View File
@@ -66,7 +66,7 @@ namespace glabels
///
/// Expand all place holders
///
QString RawText::expand( merge::Record* record ) const
QString RawText::expand( merge::Record* record, Variables* variables ) const
{
QString text;
@@ -74,7 +74,7 @@ namespace glabels
{
if ( token.isField )
{
text += token.field.evaluate( record );
text += token.field.evaluate( record, variables );
}
else
{
+1 -1
View File
@@ -52,7 +52,7 @@ namespace glabels
/////////////////////////////////
QString toString() const;
std::string toStdString() const;
QString expand( merge::Record* record ) const;
QString expand( merge::Record* record, Variables* variables ) const;
bool hasPlaceHolders() const;
bool isEmpty() const;
+15 -3
View File
@@ -42,21 +42,33 @@ namespace glabels
}
QString SubstitutionField::evaluate( const merge::Record* record ) const
QString SubstitutionField::evaluate( const merge::Record* record,
const Variables* variables ) const
{
QString value = mDefaultValue;
if ( record && record->contains(mFieldName) && !record->value(mFieldName).isEmpty() )
bool haveRecordField = record &&
record->contains(mFieldName) &&
!record->value(mFieldName).isEmpty();
bool haveVariable = variables &&
variables->contains(mFieldName) &&
!(*variables)[mFieldName].value().isEmpty();
if ( haveRecordField )
{
value = record->value(mFieldName);
}
else if ( haveVariable )
{
value = (*variables)[mFieldName].value();
}
if ( !mFormatType.isNull() )
{
value = formatValue( value );
}
if ( record && record->contains(mFieldName) && !record->value(mFieldName).isEmpty() && mNewLine )
if ( mNewLine && (haveRecordField || haveVariable) )
{
value = "\n" + value;
}
+2 -1
View File
@@ -21,6 +21,7 @@
#ifndef model_SubstitutionField_h
#define model_SubstitutionField_h
#include "Variables.h"
#include "merge/Record.h"
@@ -39,7 +40,7 @@ namespace glabels
SubstitutionField();
SubstitutionField( const QString& string );
QString evaluate( const merge::Record* record ) const;
QString evaluate( const merge::Record* record, const Variables* variables ) const;
QString fieldName() const;
QString defaultValue() const;
+120 -19
View File
@@ -28,12 +28,12 @@ namespace glabels
Variable::Variable( Variable::Type type,
const QString& name,
const QString& value,
const QString& initialValue,
Variable::Increment increment,
const QString& stepSize )
: mType(type),
mName(name),
mValue(value),
mInitialValue(initialValue),
mIncrement(increment),
mStepSize(stepSize)
{
@@ -53,9 +53,9 @@ namespace glabels
}
QString Variable::value() const
QString Variable::initialValue() const
{
return mValue;
return mInitialValue;
}
@@ -70,15 +70,110 @@ namespace glabels
return mStepSize;
}
void Variable::resetValue()
{
switch (mType)
{
case Type::STRING:
// do nothing
break;
case Type::INTEGER:
mIntegerValue = mInitialValue.toLongLong();
mIntegerStep = mStepSize.toLongLong();
break;
case Type::FLOATING_POINT:
mFloatingPointValue = mInitialValue.toDouble();
mFloatingPointStep = mStepSize.toDouble();
break;
}
}
void Variable::incrementValueOnCopy()
{
if ( mIncrement == Increment::PER_COPY )
{
switch (mType)
{
case Type::STRING:
// do nothing
break;
case Type::INTEGER:
mIntegerValue += mIntegerStep;
break;
case Type::FLOATING_POINT:
mFloatingPointValue += mFloatingPointStep;
break;
}
}
}
void Variable::incrementValueOnMerge()
{
if ( mIncrement == Increment::PER_MERGE_RECORD )
{
switch (mType)
{
case Type::STRING:
// do nothing
break;
case Type::INTEGER:
mIntegerValue += mIntegerStep;
break;
case Type::FLOATING_POINT:
mFloatingPointValue += mFloatingPointStep;
break;
}
}
}
void Variable::incrementValueOnPage()
{
if ( mIncrement == Increment::PER_PAGE )
{
switch (mType)
{
case Type::STRING:
// do nothing
break;
case Type::INTEGER:
mIntegerValue += mIntegerStep;
break;
case Type::FLOATING_POINT:
mFloatingPointValue += mFloatingPointStep;
break;
}
}
}
QString Variable::value() const
{
switch (mType)
{
case Type::STRING:
return mInitialValue;
case Type::INTEGER:
return QString::number( mIntegerValue );
case Type::FLOATING_POINT:
return QString::number( mFloatingPointValue, 'g', 15 );
}
}
QString Variable::typeToI18nString( Type type )
{
switch (type)
{
case Type::NUMERIC:
return tr("Numeric");
case Type::STRING:
return tr("String");
case Type::INTEGER:
return tr("Integer");
case Type::FLOATING_POINT:
return tr("Floating Point");
}
}
@@ -87,24 +182,30 @@ namespace glabels
{
switch (type)
{
case Type::NUMERIC:
return "numeric";
case Type::STRING:
return "string";
case Type::INTEGER:
return "integer";
case Type::FLOATING_POINT:
return "float";
}
}
Variable::Type Variable::idStringToType( const QString& string )
Variable::Type Variable::idStringToType( const QString& id )
{
if ( string == "numeric" )
{
return Type::NUMERIC;
}
else if ( string == "string" )
if ( id == "string" )
{
return Type::STRING;
}
else if ( id == "integer" )
{
return Type::INTEGER;
}
else if ( id == "float" )
{
return Type::FLOATING_POINT;
}
else
{
return Type::STRING; // Default
@@ -144,21 +245,21 @@ namespace glabels
}
Variable::Increment Variable::idStringToIncrement( const QString& string )
Variable::Increment Variable::idStringToIncrement( const QString& id )
{
if ( string == "never" )
if ( id == "never" )
{
return Increment::NEVER;
}
else if ( string == "per_copy" )
else if ( id == "per_copy" )
{
return Increment::PER_COPY;
}
else if ( string == "per_merge_record" )
else if ( id == "per_merge_record" )
{
return Increment::PER_MERGE_RECORD;
}
else if ( string == "per_page" )
else if ( id == "per_page" )
{
return Increment::PER_PAGE;
}
+16 -5
View File
@@ -38,8 +38,9 @@ namespace glabels
public:
enum class Type
{
NUMERIC,
STRING
STRING,
INTEGER,
FLOATING_POINT
};
enum class Increment
@@ -56,7 +57,7 @@ namespace glabels
Variable( Type type,
const QString& name,
const QString& value,
const QString& initialValue,
Increment increment = Increment::NEVER,
const QString& stepSize = "0" );
@@ -65,10 +66,15 @@ namespace glabels
Type type() const;
QString name() const;
QString value() const;
QString initialValue() const;
Increment increment() const;
QString stepSize() const;
void resetValue();
void incrementValueOnCopy();
void incrementValueOnMerge();
void incrementValueOnPage();
QString value() const;
static QString typeToI18nString( Type type );
static QString typeToIdString( Type type );
@@ -82,10 +88,15 @@ namespace glabels
private:
Type mType;
QString mName;
QString mValue;
QString mInitialValue;
Increment mIncrement;
QString mStepSize;
long long mIntegerValue;
long long mIntegerStep;
double mFloatingPointValue;
double mFloatingPointStep;
};
}
+48
View File
@@ -85,6 +85,54 @@ namespace glabels
}
///
/// Reset variables to their initial values
///
void Variables::resetVariables()
{
for ( auto& v : *this )
{
v.resetValue();
}
}
///
/// Increment variables on copy
///
void Variables::incrementVariablesOnCopy()
{
for ( auto& v : *this )
{
v.incrementValueOnCopy();
}
}
///
/// Increment variables on merge record
///
void Variables::incrementVariablesOnMerge()
{
for ( auto& v : *this )
{
v.incrementValueOnMerge();
}
}
///
/// Increment variables on page
///
void Variables::incrementVariablesOnPage()
{
for ( auto& v : *this )
{
v.incrementValueOnPage();
}
}
} // namespace model
} // namespace glabels
+5
View File
@@ -63,6 +63,11 @@ namespace glabels
void deleteVariable( const QString& name );
void replaceVariable( const QString& name, const Variable& variable );
void resetVariables();
void incrementVariablesOnCopy();
void incrementVariablesOnMerge();
void incrementVariablesOnPage();
/////////////////////////////////
// Signals
+1 -1
View File
@@ -495,7 +495,7 @@ namespace glabels
XmlUtil::setStringAttr( node, "type", Variable::typeToIdString( v.type() ) );
XmlUtil::setStringAttr( node, "name", v.name() );
XmlUtil::setStringAttr( node, "value", v.value() );
XmlUtil::setStringAttr( node, "initialValue", v.initialValue() );
XmlUtil::setStringAttr( node, "increment", Variable::incrementToIdString( v.increment() ) );
XmlUtil::setStringAttr( node, "stepSize", v.stepSize() );
}
+2 -2
View File
@@ -747,14 +747,14 @@ namespace glabels
{
QString typeString = XmlUtil::getStringAttr( node, "type", "string" );
QString name = XmlUtil::getStringAttr( node, "name", "unknown" );
QString value = XmlUtil::getStringAttr( node, "value", "0" );
QString initialValue = XmlUtil::getStringAttr( node, "initialValue", "0" );
QString incrementString = XmlUtil::getStringAttr( node, "increment", "never" );
QString stepSize = XmlUtil::getStringAttr( node, "stepSize", "0" );
auto type = Variable::idStringToType( typeString );
auto increment = Variable::idStringToIncrement( incrementString );
Variable v( type, name, value, increment, stepSize );
Variable v( type, name, initialValue, increment, stepSize );
label->variables()->addVariable( v );
}
+59 -47
View File
@@ -139,6 +139,8 @@ void TestSubstitutionField::simpleEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField f1( "${1}" );
model::SubstitutionField f2( "${2}" );
model::SubstitutionField f3( "${3}" );
@@ -150,10 +152,10 @@ void TestSubstitutionField::simpleEvaluation()
record1[ "3" ] = "Opqrstu";
record1[ "4" ] = "Vwxyz!@";
QCOMPARE( f1.evaluate( &record1 ), QString( "Abcdefg" ) );
QCOMPARE( f2.evaluate( &record1 ), QString( "Hijklmn" ) );
QCOMPARE( f3.evaluate( &record1 ), QString( "Opqrstu" ) );
QCOMPARE( f4.evaluate( &record1 ), QString( "Vwxyz!@" ) );
QCOMPARE( f1.evaluate( &record1, &variables ), QString( "Abcdefg" ) );
QCOMPARE( f2.evaluate( &record1, &variables ), QString( "Hijklmn" ) );
QCOMPARE( f3.evaluate( &record1, &variables ), QString( "Opqrstu" ) );
QCOMPARE( f4.evaluate( &record1, &variables ), QString( "Vwxyz!@" ) );
merge::Record record2;
record2[ "1" ] = "1234567";
@@ -161,10 +163,10 @@ void TestSubstitutionField::simpleEvaluation()
record2[ "3" ] = "8901234";
record2[ "4" ] = "#$%^&*";
QCOMPARE( f1.evaluate( &record2 ), QString( "1234567" ) );
QCOMPARE( f2.evaluate( &record2 ), QString( "FooBar" ) );
QCOMPARE( f3.evaluate( &record2 ), QString( "8901234" ) );
QCOMPARE( f4.evaluate( &record2 ), QString( "#$%^&*" ) );
QCOMPARE( f1.evaluate( &record2, &variables ), QString( "1234567" ) );
QCOMPARE( f2.evaluate( &record2, &variables ), QString( "FooBar" ) );
QCOMPARE( f3.evaluate( &record2, &variables ), QString( "8901234" ) );
QCOMPARE( f4.evaluate( &record2, &variables ), QString( "#$%^&*" ) );
}
@@ -172,6 +174,8 @@ void TestSubstitutionField::defaultValueEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField f1( "${1:=foo1}" );
model::SubstitutionField f2( "${2:=foo2}" );
model::SubstitutionField f3( "${3:=foo3}" );
@@ -183,17 +187,17 @@ void TestSubstitutionField::defaultValueEvaluation()
record1[ "3" ] = "Opqrstu";
record1[ "4" ] = "Vwxyz!@";
QCOMPARE( f1.evaluate( &record1 ), QString( "Abcdefg" ) );
QCOMPARE( f2.evaluate( &record1 ), QString( "Hijklmn" ) );
QCOMPARE( f3.evaluate( &record1 ), QString( "Opqrstu" ) );
QCOMPARE( f4.evaluate( &record1 ), QString( "Vwxyz!@" ) );
QCOMPARE( f1.evaluate( &record1, &variables ), QString( "Abcdefg" ) );
QCOMPARE( f2.evaluate( &record1, &variables ), QString( "Hijklmn" ) );
QCOMPARE( f3.evaluate( &record1, &variables ), QString( "Opqrstu" ) );
QCOMPARE( f4.evaluate( &record1, &variables ), QString( "Vwxyz!@" ) );
merge::Record record2; // All fields empty
QCOMPARE( f1.evaluate( &record2 ), QString( "foo1" ) );
QCOMPARE( f2.evaluate( &record2 ), QString( "foo2" ) );
QCOMPARE( f3.evaluate( &record2 ), QString( "foo3" ) );
QCOMPARE( f4.evaluate( &record2 ), QString( "foo4" ) );
QCOMPARE( f1.evaluate( &record2, &variables ), QString( "foo1" ) );
QCOMPARE( f2.evaluate( &record2, &variables ), QString( "foo2" ) );
QCOMPARE( f3.evaluate( &record2, &variables ), QString( "foo3" ) );
QCOMPARE( f4.evaluate( &record2, &variables ), QString( "foo4" ) );
merge::Record record3;
record3[ "1" ] = "xyzzy";
@@ -201,10 +205,10 @@ void TestSubstitutionField::defaultValueEvaluation()
// Field "3" empty
record3[ "4" ] = "plugh";
QCOMPARE( f1.evaluate( &record3 ), QString( "xyzzy" ) );
QCOMPARE( f2.evaluate( &record3 ), QString( "foo2" ) );
QCOMPARE( f3.evaluate( &record3 ), QString( "foo3" ) );
QCOMPARE( f4.evaluate( &record3 ), QString( "plugh" ) );
QCOMPARE( f1.evaluate( &record3, &variables ), QString( "xyzzy" ) );
QCOMPARE( f2.evaluate( &record3, &variables ), QString( "foo2" ) );
QCOMPARE( f3.evaluate( &record3, &variables ), QString( "foo3" ) );
QCOMPARE( f4.evaluate( &record3, &variables ), QString( "plugh" ) );
}
@@ -212,6 +216,8 @@ void TestSubstitutionField::formattedStringEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField f1( "${1:%10s}" );
model::SubstitutionField f2( "${2:%10s}" );
model::SubstitutionField f3( "${3:%10s}" );
@@ -233,15 +239,15 @@ void TestSubstitutionField::formattedStringEvaluation()
record1[ "7" ] = "-100";
record1[ "8" ] = "3.14";
QCOMPARE( f1.evaluate( &record1 ), QString( " 0" ) );
QCOMPARE( f2.evaluate( &record1 ), QString( " 1" ) );
QCOMPARE( f3.evaluate( &record1 ), QString( " -1" ) );
QCOMPARE( f4.evaluate( &record1 ), QString( " 3.14" ) );
QCOMPARE( f1.evaluate( &record1, &variables ), QString( " 0" ) );
QCOMPARE( f2.evaluate( &record1, &variables ), QString( " 1" ) );
QCOMPARE( f3.evaluate( &record1, &variables ), QString( " -1" ) );
QCOMPARE( f4.evaluate( &record1, &variables ), QString( " 3.14" ) );
QCOMPARE( f5.evaluate( &record1 ), QString( "0 " ) );
QCOMPARE( f6.evaluate( &record1 ), QString( "100 " ) );
QCOMPARE( f7.evaluate( &record1 ), QString( "-100 " ) );
QCOMPARE( f8.evaluate( &record1 ), QString( "3.14 " ) );
QCOMPARE( f5.evaluate( &record1, &variables ), QString( "0 " ) );
QCOMPARE( f6.evaluate( &record1, &variables ), QString( "100 " ) );
QCOMPARE( f7.evaluate( &record1, &variables ), QString( "-100 " ) );
QCOMPARE( f8.evaluate( &record1, &variables ), QString( "3.14 " ) );
}
@@ -249,6 +255,8 @@ void TestSubstitutionField::formattedFloatEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField f1( "${1:%+5.2f}" );
model::SubstitutionField f2( "${2:%+5.2f}" );
model::SubstitutionField f3( "${3:%+5.2f}" );
@@ -270,15 +278,15 @@ void TestSubstitutionField::formattedFloatEvaluation()
record1[ "7" ] = "-100";
record1[ "8" ] = "3.14";
QCOMPARE( f1.evaluate( &record1 ), QString( "+0.00" ) );
QCOMPARE( f2.evaluate( &record1 ), QString( "+1.00" ) );
QCOMPARE( f3.evaluate( &record1 ), QString( "-1.00" ) );
QCOMPARE( f4.evaluate( &record1 ), QString( "+3.14" ) );
QCOMPARE( f1.evaluate( &record1, &variables ), QString( "+0.00" ) );
QCOMPARE( f2.evaluate( &record1, &variables ), QString( "+1.00" ) );
QCOMPARE( f3.evaluate( &record1, &variables ), QString( "-1.00" ) );
QCOMPARE( f4.evaluate( &record1, &variables ), QString( "+3.14" ) );
QCOMPARE( f5.evaluate( &record1 ), QString( "+0.00e+00" ) );
QCOMPARE( f6.evaluate( &record1 ), QString( "+1.00e+02" ) );
QCOMPARE( f7.evaluate( &record1 ), QString( "-1.00e+02" ) );
QCOMPARE( f8.evaluate( &record1 ), QString( "+3.14e+00" ) );
QCOMPARE( f5.evaluate( &record1, &variables ), QString( "+0.00e+00" ) );
QCOMPARE( f6.evaluate( &record1, &variables ), QString( "+1.00e+02" ) );
QCOMPARE( f7.evaluate( &record1, &variables ), QString( "-1.00e+02" ) );
QCOMPARE( f8.evaluate( &record1, &variables ), QString( "+3.14e+00" ) );
}
@@ -286,6 +294,8 @@ void TestSubstitutionField::formattedIntEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField f1( "${1:%08d}" );
model::SubstitutionField f2( "${2:%08d}" );
model::SubstitutionField f3( "${3:%08d}" );
@@ -307,15 +317,15 @@ void TestSubstitutionField::formattedIntEvaluation()
record1[ "7" ] = "-1";
record1[ "8" ] = "314";
QCOMPARE( f1.evaluate( &record1 ), QString( "00000000" ) );
QCOMPARE( f2.evaluate( &record1 ), QString( "00000001" ) );
QCOMPARE( f3.evaluate( &record1 ), QString( "-0000001" ) );
QCOMPARE( f4.evaluate( &record1 ), QString( "00000000" ) ); // Invalid integer value
QCOMPARE( f1.evaluate( &record1, &variables ), QString( "00000000" ) );
QCOMPARE( f2.evaluate( &record1, &variables ), QString( "00000001" ) );
QCOMPARE( f3.evaluate( &record1, &variables ), QString( "-0000001" ) );
QCOMPARE( f4.evaluate( &record1, &variables ), QString( "00000000" ) ); // Invalid integer value
QCOMPARE( f5.evaluate( &record1 ), QString( "00000064" ) ); // 100(decimal) == 64(hex)
QCOMPARE( f6.evaluate( &record1 ), QString( "00000100" ) );
QCOMPARE( f7.evaluate( &record1 ), QString( "00000000" ) ); // Invalid unsigned integer
QCOMPARE( f8.evaluate( &record1 ), QString( "0000013a" ) ); // 314(decimal) == 13a(hex)
QCOMPARE( f5.evaluate( &record1, &variables ), QString( "00000064" ) ); // 100(decimal) == 64(hex)
QCOMPARE( f6.evaluate( &record1, &variables ), QString( "00000100" ) );
QCOMPARE( f7.evaluate( &record1, &variables ), QString( "00000000" ) ); // Invalid unsigned integer
QCOMPARE( f8.evaluate( &record1, &variables ), QString( "0000013a" ) ); // 314(decimal) == 13a(hex)
}
@@ -323,6 +333,8 @@ void TestSubstitutionField::newLineEvaluation()
{
using namespace glabels;
model::Variables variables;
model::SubstitutionField addr2( "${ADDR2:n}" );
QCOMPARE( addr2.fieldName(), QString( "ADDR2" ) );
QCOMPARE( addr2.newLine(), true );
@@ -336,7 +348,7 @@ void TestSubstitutionField::newLineEvaluation()
merge::Record record3;
// ADDR2 not defined
QCOMPARE( addr2.evaluate( &record1 ), QString( "\nApt. 5B" ) ); // Prepends a newline
QCOMPARE( addr2.evaluate( &record2 ), QString( "" ) ); // Evaluates empty
QCOMPARE( addr2.evaluate( &record3 ), QString( "" ) ); // Evaluates empty
QCOMPARE( addr2.evaluate( &record1, &variables ), QString( "\nApt. 5B" ) ); // Prepends a newline
QCOMPARE( addr2.evaluate( &record2, &variables ), QString( "" ) ); // Evaluates empty
QCOMPARE( addr2.evaluate( &record3, &variables ), QString( "" ) ); // Evaluates empty
}
+21 -17
View File
@@ -184,24 +184,24 @@
<source>Dialog</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Variable Type:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Name:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Value:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Increment:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Step Size:</source>
<source>Variable type:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Initial value:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Step size:</source>
<translation type="unfinished"></translation>
</message>
</context>
@@ -1060,10 +1060,6 @@
</context>
<context>
<name>Variable</name>
<message>
<source>Numeric</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>String</source>
<translation type="unfinished"></translation>
@@ -1084,6 +1080,14 @@
<source>Per page</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Integer</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Floating Point</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>VariablesView</name>
@@ -2141,10 +2145,6 @@
<source>Type</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Value</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Increment</source>
<translation type="unfinished"></translation>
@@ -2153,6 +2153,10 @@
<source>Step Size</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Initial Value</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>glabels::barcode::Backends</name>