/* LabelModelBarcodeObject.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 "LabelModelBarcodeObject.h" #include "BarcodeBackends.h" #include "Size.h" #include "glbarcode/Factory.h" #include "glbarcode/QtRenderer.h" #include #include #include #include #include #include namespace glabels { // // Private // namespace { } /// /// Constructor /// LabelModelBarcodeObject::LabelModelBarcodeObject() { mOutline = new Outline( this ); mHandles << new HandleNorthWest( this ); mHandles << new HandleNorth( this ); mHandles << new HandleNorthEast( this ); mHandles << new HandleEast( this ); mHandles << new HandleSouthEast( this ); mHandles << new HandleSouth( this ); mHandles << new HandleSouthWest( this ); mHandles << new HandleWest( this ); mBcStyle = BarcodeBackends::lookupStyleFromId( "code39" ); mBcTextFlag = mBcStyle.canText(); mBcChecksumFlag = mBcStyle.canChecksum(); mBcFormatDigits = mBcStyle.preferedN(); mBcDataNode = TextNode( false, mBcStyle.defaultDigits() ); mBcColorNode = ColorNode( Qt::black ); update(); // Initialize cached editor layouts } /// /// Copy constructor /// LabelModelBarcodeObject::LabelModelBarcodeObject( const LabelModelBarcodeObject* object ) : LabelModelObject(object) { mBcStyle = object->mBcStyle; mBcTextFlag = object->mBcTextFlag; mBcChecksumFlag = object->mBcChecksumFlag; mBcFormatDigits = object->mBcFormatDigits; mBcDataNode = object->mBcDataNode; mBcColorNode = object->mBcColorNode; mEditorBarcode = nullptr; update(); // Initialize cached editor layouts } /// /// Destructor /// LabelModelBarcodeObject::~LabelModelBarcodeObject() { delete mOutline; foreach( Handle* handle, mHandles ) { delete handle; } mHandles.clear(); delete mEditorBarcode; } /// /// Clone /// LabelModelBarcodeObject* LabelModelBarcodeObject::clone() const { return new LabelModelBarcodeObject( this ); } /// /// bcDataNode Property Getter /// TextNode LabelModelBarcodeObject::bcDataNode() const { return mBcDataNode; } /// /// bcDataNode Property Setter /// void LabelModelBarcodeObject::setBcDataNode( const TextNode& value ) { if ( mBcDataNode != value ) { mBcDataNode = value; update(); emit changed(); } } /// /// bcTextFlag Property Getter /// bool LabelModelBarcodeObject::bcTextFlag() const { return mBcTextFlag; } /// /// bcTextFlag Property Setter /// void LabelModelBarcodeObject::setBcTextFlag( bool value ) { if ( mBcTextFlag != value ) { mBcTextFlag = value; update(); emit changed(); } } /// /// bcChecksumFlag Property Getter /// bool LabelModelBarcodeObject::bcChecksumFlag() const { return mBcChecksumFlag; } /// /// bcChecksumFlag Property Setter /// void LabelModelBarcodeObject::setBcChecksumFlag( bool value ) { if ( mBcChecksumFlag != value ) { mBcChecksumFlag = value; update(); emit changed(); } } /// /// Barcode Color Node Property Getter /// ColorNode LabelModelBarcodeObject::bcColorNode() const { return mBcColorNode; } /// /// Barcode Color Node Property Setter /// void LabelModelBarcodeObject::setBcColorNode( const ColorNode& value ) { if ( mBcColorNode != value ) { mBcColorNode = value; update(); emit changed(); } } /// /// Barcode Style Property Getter /// BarcodeStyle LabelModelBarcodeObject::bcStyle() const { return mBcStyle; } /// /// Barcode Style Property Setter /// void LabelModelBarcodeObject::setBcStyle( const BarcodeStyle& value ) { if ( mBcStyle != value ) { mBcStyle = value; update(); emit changed(); } } /// /// Barcode Format Digits Property Getter /// int LabelModelBarcodeObject::bcFormatDigits() const { return mBcFormatDigits; } /// /// Barcode Format Digits Property Setter /// void LabelModelBarcodeObject::setBcFormatDigits( int value ) { if ( mBcFormatDigits != value ) { mBcFormatDigits = value; update(); emit changed(); } } /// /// Draw shadow of object /// void LabelModelBarcodeObject::drawShadow( QPainter* painter, 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 ); } } } /// /// Draw object itself /// void LabelModelBarcodeObject::drawObject( QPainter* painter, bool inEditor, merge::Record* record ) const { QColor bcColor = mBcColorNode.color( record ); if ( inEditor ) { drawBcInEditor( painter, bcColor ); } else { drawBc( painter, bcColor, record ); } } /// /// Path to test for hover condition /// QPainterPath LabelModelBarcodeObject::hoverPath( double scale ) const { return mHoverPath; } /// /// Size updated /// void LabelModelBarcodeObject::sizeUpdated() { update(); } /// /// Update cached information for editor view /// void LabelModelBarcodeObject::update() { if ( mEditorBarcode ) { delete mEditorBarcode; } mEditorBarcode = glbarcode::Factory::createBarcode( mBcStyle.id().toStdString() ); mEditorBarcode->setChecksum(mBcChecksumFlag); mEditorBarcode->setShowText(mBcTextFlag); if ( mBcDataNode.isField() ) { mEditorBarcode->build( mBcStyle.defaultDigits().toStdString(), mW.pt(), mH.pt() ); } else { mEditorBarcode->build( mBcDataNode.data().toStdString(), mW.pt(), mH.pt() ); } mW = Distance::pt( mEditorBarcode->width() ); mH = Distance::pt( mEditorBarcode->height() ); QPainterPath path; path.addRect( 0, 0, mEditorBarcode->width(), mEditorBarcode->height() ); mHoverPath = path; } /// /// Draw text in editor from cached information /// void LabelModelBarcodeObject::drawBcInEditor( QPainter* painter, const QColor& color ) const { painter->setPen( QPen( color ) ); if ( mEditorBarcode->isDataValid() ) { glbarcode::QtRenderer renderer(painter); mEditorBarcode->render( renderer ); } else if ( mEditorBarcode->isEmpty() ) { // FIXME: display "Empty" } else { // FIXME: display "Invalid data" } } /// /// Draw text in final printout or preview /// void LabelModelBarcodeObject::drawBc( QPainter* painter, const QColor& color, merge::Record* record ) const { painter->setPen( QPen( color ) ); glbarcode::Barcode* bc = glbarcode::Factory::createBarcode( mBcStyle.id().toStdString() ); bc->setChecksum(mBcChecksumFlag); bc->setShowText(mBcTextFlag); bc->build( mBcDataNode.text(record).toStdString(), mW.pt(), mH.pt() ); glbarcode::QtRenderer renderer(painter); bc->render( renderer ); } } // namespace glabels