Skip to content

Commit

Permalink
Allow user variables to be set from glabels-batch command line (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevins committed Oct 13, 2019
1 parent 1a900d0 commit 7a02c0b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
To Do List for gLabels 4.0 -- 2018-10-06
To Do List for gLabels 4.0 -- 2019-10-07
========================================

Expose user variables to glabels-batch
--------------------------------------
There should be a way to set initial values of user variables from the glabels-batch
command line. Possibly even create them on the fly.

Add programmable margin to text objects
---------------------------------------
The current built-in fixed margin seems to confuse people when dealing with
Expand Down
24 changes: 23 additions & 1 deletion glabels-batch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ int main( int argc, char **argv )
QCoreApplication::translate( "main", "Print crop marks." ) },

{{"r","reverse"},
QCoreApplication::translate( "main", "Print in reverse (mirror image)." ) }
QCoreApplication::translate( "main", "Print in reverse (mirror image)." ) },

{{"D","define"},
QCoreApplication::translate( "main", "Set user variable <var> to <value>" ),
QCoreApplication::translate( "main", "var>=<value" ) }
};


Expand All @@ -134,7 +138,23 @@ int main( int argc, char **argv )
QCoreApplication::translate( "main", "gLabels project file to print." ),
"file" );
parser.process( app );

//
// Parse variable definitions from command line, if any
//
QMap<QString,QString> variableDefinitions;

for ( QString definition : parser.values("define") )
{
QStringList parts = definition.split( '=' );
if ( parts.size() != 2 )
{
qWarning() << "Error: bad variable definition: " << definition;
return -1;
}

variableDefinitions[ parts[0] ] = parts[1];
}

//
// Initialize subsystems
Expand All @@ -152,6 +172,8 @@ int main( int argc, char **argv )
glabels::model::Model *model = glabels::model::XmlLabelParser::readFile( filename );
if ( model )
{
model->variables()->setVariables( variableDefinitions );

QPrinter printer( QPrinter::HighResolution );
printer.setColorMode( QPrinter::Color );
if ( parser.isSet("printer") )
Expand Down
6 changes: 6 additions & 0 deletions model/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ namespace glabels
}


void Variable::setInitialValue( const QString& value )
{
mInitialValue = value;
}


void Variable::resetValue()
{
switch (mType)
Expand Down
2 changes: 2 additions & 0 deletions model/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ namespace glabels
Increment increment() const;
QString stepSize() const;

void setInitialValue( const QString& value );

void resetValue();
void incrementValueOnItem();
void incrementValueOnCopy();
Expand Down
21 changes: 21 additions & 0 deletions model/Variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ namespace glabels
}


///
/// Set initial value of multiple variables
///
void Variables::setVariables( const QMap<QString,QString>& definitions )
{
for ( auto& name : definitions.keys() )
{
if ( hasVariable( name ) )
{
(*this)[name].setInitialValue( definitions[name] );
}
else
{
addVariable( Variable( Variable::Type::STRING,
name,
definitions[name] ) );
}
}
}


///
/// Reset variables to their initial values
///
Expand Down
2 changes: 2 additions & 0 deletions model/Variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace glabels
void deleteVariable( const QString& name );
void replaceVariable( const QString& name, const Variable& variable );

void setVariables( const QMap<QString,QString>& definitions );

void resetVariables();
void incrementVariablesOnItem();
void incrementVariablesOnCopy();
Expand Down

0 comments on commit 7a02c0b

Please sign in to comment.