From 409ca1bf570c24b59f140abe5b566b56810685a6 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 6 May 2017 20:08:17 -0400 Subject: [PATCH] Added RawText type for text and barcode objects. --- glabels/CMakeLists.txt | 1 + glabels/LabelModelBarcodeObject.cpp | 10 ++- glabels/LabelModelBarcodeObject.h | 4 +- glabels/LabelModelTextObject.cpp | 38 ++-------- glabels/LabelModelTextObject.h | 3 +- glabels/RawText.cpp | 110 ++++++++++++++++++++++++++++ glabels/RawText.h | 69 +++++++++++++++++ translations/glabels_C.ts | 2 +- 8 files changed, 198 insertions(+), 39 deletions(-) create mode 100644 glabels/RawText.cpp create mode 100644 glabels/RawText.h diff --git a/glabels/CMakeLists.txt b/glabels/CMakeLists.txt index 20259d1..6755657 100644 --- a/glabels/CMakeLists.txt +++ b/glabels/CMakeLists.txt @@ -79,6 +79,7 @@ set (glabels_sources PropertiesView.cpp Preview.cpp PreviewOverlayItem.cpp + RawText.cpp Region.cpp SelectProductDialog.cpp Settings.cpp diff --git a/glabels/LabelModelBarcodeObject.cpp b/glabels/LabelModelBarcodeObject.cpp index 16989d0..8235334 100644 --- a/glabels/LabelModelBarcodeObject.cpp +++ b/glabels/LabelModelBarcodeObject.cpp @@ -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"); diff --git a/glabels/LabelModelBarcodeObject.h b/glabels/LabelModelBarcodeObject.h index c073a7e..380e697 100644 --- a/glabels/LabelModelBarcodeObject.h +++ b/glabels/LabelModelBarcodeObject.h @@ -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; diff --git a/glabels/LabelModelTextObject.cpp b/glabels/LabelModelTextObject.cpp index 393e732..6d3b9af 100644 --- a/glabels/LabelModelTextObject.cpp +++ b/glabels/LabelModelTextObject.cpp @@ -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 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 diff --git a/glabels/LabelModelTextObject.h b/glabels/LabelModelTextObject.h index b0a5e40..0bce729 100644 --- a/glabels/LabelModelTextObject.h +++ b/glabels/LabelModelTextObject.h @@ -23,6 +23,7 @@ #include "LabelModelObject.h" +#include "RawText.h" #include @@ -163,7 +164,7 @@ namespace glabels // Private Members /////////////////////////////////////////////////////////////// private: - QString mText; + RawText mText; QString mFontFamily; double mFontSize; QFont::Weight mFontWeight; diff --git a/glabels/RawText.cpp b/glabels/RawText.cpp new file mode 100644 index 0000000..e93fd68 --- /dev/null +++ b/glabels/RawText.cpp @@ -0,0 +1,110 @@ +/* RawText.cpp + * + * Copyright (C) 2017 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 "RawText.h" + +#include + +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 diff --git a/glabels/RawText.h b/glabels/RawText.h new file mode 100644 index 0000000..e0b2f68 --- /dev/null +++ b/glabels/RawText.h @@ -0,0 +1,69 @@ +/* RawText.h + * + * Copyright (C) 2017 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 RawText_h +#define RawText_h + + +#include "Merge/Record.h" + +#include + + +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 diff --git a/translations/glabels_C.ts b/translations/glabels_C.ts index 696fb43..f557a96 100644 --- a/translations/glabels_C.ts +++ b/translations/glabels_C.ts @@ -1122,7 +1122,7 @@ - + Invalid barcode data