Skip to content

Commit 7a02c0b

Browse files
committed
Allow user variables to be set from glabels-batch command line (#75)
1 parent 1a900d0 commit 7a02c0b

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

docs/TODO.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
To Do List for gLabels 4.0 -- 2018-10-06
1+
To Do List for gLabels 4.0 -- 2019-10-07
22
========================================
33

4+
Expose user variables to glabels-batch
5+
--------------------------------------
6+
There should be a way to set initial values of user variables from the glabels-batch
7+
command line. Possibly even create them on the fly.
8+
49
Add programmable margin to text objects
510
---------------------------------------
611
The current built-in fixed margin seems to confuse people when dealing with

glabels-batch/main.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ int main( int argc, char **argv )
121121
QCoreApplication::translate( "main", "Print crop marks." ) },
122122

123123
{{"r","reverse"},
124-
QCoreApplication::translate( "main", "Print in reverse (mirror image)." ) }
124+
QCoreApplication::translate( "main", "Print in reverse (mirror image)." ) },
125+
126+
{{"D","define"},
127+
QCoreApplication::translate( "main", "Set user variable <var> to <value>" ),
128+
QCoreApplication::translate( "main", "var>=<value" ) }
125129
};
126130

127131

@@ -134,7 +138,23 @@ int main( int argc, char **argv )
134138
QCoreApplication::translate( "main", "gLabels project file to print." ),
135139
"file" );
136140
parser.process( app );
141+
142+
//
143+
// Parse variable definitions from command line, if any
144+
//
145+
QMap<QString,QString> variableDefinitions;
137146

147+
for ( QString definition : parser.values("define") )
148+
{
149+
QStringList parts = definition.split( '=' );
150+
if ( parts.size() != 2 )
151+
{
152+
qWarning() << "Error: bad variable definition: " << definition;
153+
return -1;
154+
}
155+
156+
variableDefinitions[ parts[0] ] = parts[1];
157+
}
138158

139159
//
140160
// Initialize subsystems
@@ -152,6 +172,8 @@ int main( int argc, char **argv )
152172
glabels::model::Model *model = glabels::model::XmlLabelParser::readFile( filename );
153173
if ( model )
154174
{
175+
model->variables()->setVariables( variableDefinitions );
176+
155177
QPrinter printer( QPrinter::HighResolution );
156178
printer.setColorMode( QPrinter::Color );
157179
if ( parser.isSet("printer") )

model/Variable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ namespace glabels
8888
}
8989

9090

91+
void Variable::setInitialValue( const QString& value )
92+
{
93+
mInitialValue = value;
94+
}
95+
96+
9197
void Variable::resetValue()
9298
{
9399
switch (mType)

model/Variable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ namespace glabels
7171
Increment increment() const;
7272
QString stepSize() const;
7373

74+
void setInitialValue( const QString& value );
75+
7476
void resetValue();
7577
void incrementValueOnItem();
7678
void incrementValueOnCopy();

model/Variables.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ namespace glabels
8585
}
8686

8787

88+
///
89+
/// Set initial value of multiple variables
90+
///
91+
void Variables::setVariables( const QMap<QString,QString>& definitions )
92+
{
93+
for ( auto& name : definitions.keys() )
94+
{
95+
if ( hasVariable( name ) )
96+
{
97+
(*this)[name].setInitialValue( definitions[name] );
98+
}
99+
else
100+
{
101+
addVariable( Variable( Variable::Type::STRING,
102+
name,
103+
definitions[name] ) );
104+
}
105+
}
106+
}
107+
108+
88109
///
89110
/// Reset variables to their initial values
90111
///

model/Variables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ namespace glabels
6363
void deleteVariable( const QString& name );
6464
void replaceVariable( const QString& name, const Variable& variable );
6565

66+
void setVariables( const QMap<QString,QString>& definitions );
67+
6668
void resetVariables();
6769
void incrementVariablesOnItem();
6870
void incrementVariablesOnCopy();

0 commit comments

Comments
 (0)