Added RawText type for text and barcode objects.

This commit is contained in:
Jim Evins
2017-05-06 20:08:17 -04:00
parent 6d764bbf4d
commit 409ca1bf57
8 changed files with 198 additions and 39 deletions
+1
View File
@@ -79,6 +79,7 @@ set (glabels_sources
PropertiesView.cpp
Preview.cpp
PreviewOverlayItem.cpp
RawText.cpp
Region.cpp
SelectProductDialog.cpp
Settings.cpp
+7 -3
View File
@@ -123,7 +123,7 @@ namespace glabels
///
QString LabelModelBarcodeObject::bcData() const
{
return mBcData;
return mBcData.toString();
}
@@ -132,7 +132,7 @@ namespace glabels
///
void LabelModelBarcodeObject::setBcData( const QString& value )
{
if ( mBcData != value )
if ( mBcData.toString() != value )
{
mBcData = value;
update();
@@ -348,10 +348,14 @@ namespace glabels
{
QString text;
if ( mEditorBarcode->isEmpty() )
if ( mBcData.isEmpty() )
{
text = tr("No barcode data");
}
else if ( mBcData.hasPlaceHolders() )
{
text = mBcData.toString();
}
else
{
text = tr("Invalid barcode data");
+3 -1
View File
@@ -24,6 +24,8 @@
#include "LabelModelObject.h"
#include "RawText.h"
#include "glbarcode/Barcode.h"
@@ -134,7 +136,7 @@ namespace glabels
bool mBcTextFlag;
bool mBcChecksumFlag;
int mBcFormatDigits;
QString mBcData;
RawText mBcData;
ColorNode mBcColorNode;
glbarcode::Barcode* mEditorBarcode;
+5 -33
View File
@@ -121,7 +121,7 @@ namespace glabels
///
QString LabelModelTextObject::text() const
{
return mText;
return mText.toString();
}
@@ -130,7 +130,7 @@ namespace glabels
///
void LabelModelTextObject::setText( const QString& value )
{
if ( mText != value )
if ( mText.toString() != value )
{
mText = value;
update();
@@ -365,7 +365,7 @@ namespace glabels
QFontMetricsF fontMetrics( font );
double dy = fontMetrics.lineSpacing() * mTextLineSpacing;
QString displayText = mText.isEmpty() ? tr("Text") : mText;
QString displayText = mText.isEmpty() ? tr("Text") : mText.toString();
QTextDocument document( displayText );
// Do layouts
@@ -487,7 +487,7 @@ namespace glabels
QFontMetricsF fontMetrics( font );
double dy = fontMetrics.lineSpacing() * mTextLineSpacing;
QString displayText = mText.isEmpty() ? tr("Text") : mText;
QString displayText = mText.isEmpty() ? tr("Text") : mText.toString();
QTextDocument document( displayText );
qDeleteAll( mEditorLayouts );
@@ -597,7 +597,7 @@ namespace glabels
QFontMetricsF fontMetrics( font );
double dy = fontMetrics.lineSpacing() * mTextLineSpacing;
QTextDocument document( expandText( mText, record ) );
QTextDocument document( mText.expand( record ) );
QList<QTextLayout*> layouts;
@@ -664,32 +664,4 @@ namespace glabels
qDeleteAll( layouts );
}
///
/// Expand text by replacing fields with their values from the given record
///
QString LabelModelTextObject::expandText( QString text, merge::Record* record ) const
{
if ( record )
{
foreach ( QString key, record->keys() )
{
// Special case: remove line when it contains only an empty field.
// e.g. an optional ${ADDR2} line. To bypass this case, include
// whitespace at end of line.
if ( record->value(key).isEmpty() )
{
QStringList v = text.split( '\n' );
v.removeAll( "${"+key+"}" );
text = v.join( '\n' );
}
// Nominal case: simple replacement
text.replace( "${"+key+"}", record->value(key) );
}
}
return text;
}
} // namespace glabels
+2 -1
View File
@@ -23,6 +23,7 @@
#include "LabelModelObject.h"
#include "RawText.h"
#include <QTextLayout>
@@ -163,7 +164,7 @@ namespace glabels
// Private Members
///////////////////////////////////////////////////////////////
private:
QString mText;
RawText mText;
QString mFontFamily;
double mFontSize;
QFont::Weight mFontWeight;
+110
View File
@@ -0,0 +1,110 @@
/* RawText.cpp
*
* Copyright (C) 2017 Jim Evins <evins@snaught.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "RawText.h"
#include <QRegularExpression>
namespace glabels
{
///
/// Constructor from QString
///
RawText::RawText( const QString& string ) : mString(string)
{
}
///
/// Constructor from C string operator
///
RawText::RawText( const char* cString ) : mString(QString(cString))
{
}
///
/// Access as QString
///
QString RawText::toString() const
{
return mString;
}
///
/// Access as std::string
///
std::string RawText::toStdString() const
{
return mString.toStdString();
}
///
/// Expand all place holders
///
QString RawText::expand( merge::Record* record ) const
{
QString text = mString;
if ( record )
{
foreach ( QString key, record->keys() )
{
// Special case: remove line when it contains only an empty field.
// e.g. an optional ${ADDR2} line. To bypass this case, include
// whitespace at end of line.
if ( record->value(key).isEmpty() )
{
QStringList v = text.split( '\n' );
v.removeAll( "${"+key+"}" );
text = v.join( '\n' );
}
// Nominal case: simple replacement
text.replace( "${"+key+"}", record->value(key) );
}
}
return text;
}
///
/// Does raw text contain place holders?
///
bool RawText::hasPlaceHolders() const
{
QRegularExpression re("\\${\\w+}");
return mString.contains( re );
}
///
/// Is raw text empty?
///
bool RawText::isEmpty() const
{
return mString.isEmpty();
}
} // namespace glabels
+69
View File
@@ -0,0 +1,69 @@
/* RawText.h
*
* Copyright (C) 2017 Jim Evins <evins@snaught.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef RawText_h
#define RawText_h
#include "Merge/Record.h"
#include <QString>
namespace glabels
{
///
/// Raw Text Type
///
struct RawText
{
/////////////////////////////////
// Life Cycle
/////////////////////////////////
public:
RawText() = default;
RawText( const QString& string );
RawText( const char* cString );
/////////////////////////////////
// Misc. Methods
/////////////////////////////////
QString toString() const;
std::string toStdString() const;
QString expand( merge::Record* record ) const;
bool hasPlaceHolders() const;
bool isEmpty() const;
/////////////////////////////////
// Private Data
/////////////////////////////////
private:
QString mString;
};
}
#endif // RawText_h
+1 -1
View File
@@ -1122,7 +1122,7 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../glabels/LabelModelBarcodeObject.cpp" line="357"/>
<location filename="../glabels/LabelModelBarcodeObject.cpp" line="361"/>
<source>Invalid barcode data</source>
<translation type="unfinished"></translation>
</message>