From 6d764bbf4d82d8ab8f71e8b723cc8b09e74753f8 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Thu, 4 May 2017 21:58:29 -0400 Subject: [PATCH] Render barcode placeholder for invalid data in label editor. --- glabels/LabelEditor.cpp | 3 ++ glabels/LabelModelBarcodeObject.cpp | 70 ++++++++++++++++++----------- translations/glabels_C.ts | 23 +++++++--- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/glabels/LabelEditor.cpp b/glabels/LabelEditor.cpp index 999a1fc..9cea76e 100644 --- a/glabels/LabelEditor.cpp +++ b/glabels/LabelEditor.cpp @@ -735,6 +735,9 @@ namespace glabels case Text: mCreateObject->setSize( 72, 36 ); break; + case Barcode: + mCreateObject->setSize( 72, 36 ); + break; case Line: mCreateObject->setSize( 72, 0 ); break; diff --git a/glabels/LabelModelBarcodeObject.cpp b/glabels/LabelModelBarcodeObject.cpp index 3648715..16989d0 100644 --- a/glabels/LabelModelBarcodeObject.cpp +++ b/glabels/LabelModelBarcodeObject.cpp @@ -42,6 +42,8 @@ namespace glabels // namespace { + const QColor emptyFillColor = QColor( 128, 128, 128, 128 ); + const Distance pad = Distance::pt(4); } @@ -65,7 +67,7 @@ namespace glabels mBcTextFlag = mBcStyle.canText(); mBcChecksumFlag = mBcStyle.canChecksum(); mBcFormatDigits = mBcStyle.preferedN(); - mBcData = mBcStyle.defaultDigits(); + mBcData = ""; mBcColorNode = ColorNode( Qt::black ); update(); // Initialize cached editor layouts @@ -261,22 +263,7 @@ namespace glabels bool inEditor, merge::Record* record ) const { - QColor bcColor = mBcColorNode.color( record ); - - if ( bcColor.alpha() ) - { - QColor shadowColor = mShadowColorNode.color( record ); - shadowColor.setAlphaF( mShadowOpacity ); - - if ( inEditor ) - { - drawBcInEditor( painter, shadowColor ); - } - else - { - drawBc( painter, shadowColor, record ); - } - } + // Barcodes don't support shadows. } @@ -333,11 +320,14 @@ namespace glabels mEditorBarcode->build( mBcData.toStdString(), mW.pt(), mH.pt() ); - mW = Distance::pt( mEditorBarcode->width() ); - mH = Distance::pt( mEditorBarcode->height() ); + if ( mEditorBarcode->isDataValid() ) + { + mW = Distance::pt( mEditorBarcode->width() ); + mH = Distance::pt( mEditorBarcode->height() ); + } QPainterPath path; - path.addRect( 0, 0, mEditorBarcode->width(), mEditorBarcode->height() ); + path.addRect( 0, 0, mW.pt(), mH.pt() ); mHoverPath = path; } @@ -354,13 +344,43 @@ namespace glabels glbarcode::QtRenderer renderer(painter); mEditorBarcode->render( renderer ); } - else if ( mEditorBarcode->isEmpty() ) - { - // FIXME: display "Empty" - } else { - // FIXME: display "Invalid data" + QString text; + + if ( mEditorBarcode->isEmpty() ) + { + text = tr("No barcode data"); + } + else + { + text = tr("Invalid barcode data"); + } + + painter->setPen( Qt::NoPen ); + painter->setBrush( QBrush( emptyFillColor ) ); + painter->drawRect( QRectF( 0, 0, mW.pt(), mH.pt() ) ); + + QFont font( "Sans" ); + font.setPointSizeF( 6 ); + + QFontMetricsF fm(font); + QRectF textRect = fm.boundingRect( text ); + + double wPts = (mW - 2*pad).pt(); + double hPts = (mH - 2*pad).pt(); + if ( (wPts < textRect.width()) || (hPts < textRect.height()) ) + { + double scaleX = wPts / textRect.width(); + double scaleY = hPts / textRect.height(); + font.setPointSizeF( 6 * std::min( scaleX, scaleY ) ); + } + + painter->setFont( font ); + painter->setPen( QPen( color ) ); + painter->drawText( QRectF( 0, 0, mW.pt(), mH.pt() ), + Qt::AlignCenter, + text ); } } diff --git a/translations/glabels_C.ts b/translations/glabels_C.ts index a8815a7..696fb43 100644 --- a/translations/glabels_C.ts +++ b/translations/glabels_C.ts @@ -1093,15 +1093,15 @@ glabels::LabelEditor - - - - + + + + Move - + Delete @@ -1114,6 +1114,19 @@ + + glabels::LabelModelBarcodeObject + + + No barcode data + + + + + Invalid barcode data + + + glabels::LabelModelTextObject