diff --git a/glabels/LabelModel.cpp b/glabels/LabelModel.cpp index ca60e98..07222e1 100644 --- a/glabels/LabelModel.cpp +++ b/glabels/LabelModel.cpp @@ -1056,5 +1056,17 @@ namespace glabels emit modifiedChanged(); } + + /// + /// Draw label objects + /// + void LabelModel::draw( QPainter* painter, bool inEditor, MergeRecord* record ) const + { + foreach ( LabelModelObject* object, mObjectList ) + { + object->draw( painter, inEditor, record ); + } + } + } diff --git a/glabels/LabelModel.h b/glabels/LabelModel.h index 2559ce7..94a44b7 100644 --- a/glabels/LabelModel.h +++ b/glabels/LabelModel.h @@ -23,7 +23,9 @@ #include #include +#include +#include "MergeRecord.h" #include "libglabels/Template.h" @@ -166,6 +168,13 @@ namespace glabels void setSelectionFillColorNode( ColorNode fillColorNode ); + ///////////////////////////////// + // Drawing operations + ///////////////////////////////// + public: + void draw( QPainter* painter, bool inEditor = true, MergeRecord* record = 0 ) const; + + ///////////////////////////////// // Slots ///////////////////////////////// diff --git a/glabels/LabelModelBoxObject.cpp b/glabels/LabelModelBoxObject.cpp index f9d3541..af5883b 100644 --- a/glabels/LabelModelBoxObject.cpp +++ b/glabels/LabelModelBoxObject.cpp @@ -20,7 +20,6 @@ #include "LabelModelBoxObject.h" -#include #include #include @@ -139,48 +138,62 @@ namespace glabels /// - /// Create QGraphicsItem suitable for representing this object + /// Draw shadow of object /// - QGraphicsItem* LabelModelBoxObject::createGraphicsItem() + void LabelModelBoxObject::drawShadow( QPainter* painter, bool inEditor, MergeRecord* record ) const { - QGraphicsRectItem *rectItem = new QGraphicsRectItem( x0(), y0(), w(), h() ); + /// TODO expand colors based on record + + QColor lineColor = mLineColorNode.color(); + QColor fillColor = mFillColorNode.color(); + QColor shadowColor = mShadowColorNode.color(); - QBrush brush( fillColorNode().color() ); - rectItem->setBrush( brush ); + shadowColor.setAlphaF( mShadowOpacity ); - QPen pen( lineColorNode().color() ); - pen.setJoinStyle( Qt::MiterJoin ); - pen.setWidthF( lineWidth() ); - rectItem->setPen( pen ); + if ( fillColor.alpha() ) + { + painter->setPen( Qt::NoPen ); + painter->setBrush( shadowColor ); - updateGraphicsItemMatrix( rectItem ); - updateGraphicsItemShadow( rectItem ); + if ( lineColor.alpha() ) + { + /* Has FILL and OUTLINE: adjust size to account for line width. */ + painter->drawRect( -mLineWidth/2, -mLineWidth/2, mW+mLineWidth, mH+mLineWidth ); + } + else + { + /* Has FILL, but no OUTLINE. */ + painter->drawRect( 0, 0, mW, mH ); + } + } + else + { + if ( lineColor.alpha() ) + { + /* Has only OUTLINE. */ + painter->setPen( QPen( shadowColor, mLineWidth ) ); + painter->setBrush( Qt::NoBrush ); - return rectItem; + painter->drawRect( 0, 0, mW, mH ); + } + } + } - + /// - /// Update a QGraphicsItem to keep it in sync with this object + /// Draw object itself /// - void LabelModelBoxObject::updateGraphicsItem( QGraphicsItem* graphicsItem ) + void LabelModelBoxObject::drawObject( QPainter* painter, bool inEditor, MergeRecord* record ) const { - QGraphicsRectItem *rectItem = dynamic_cast(graphicsItem); - if ( rectItem ) - { - rectItem->setRect( x0(), y0(), w(), h() ); + /// TODO expand colors based on record + + QColor lineColor = mLineColorNode.color(); + QColor fillColor = mFillColorNode.color(); - QBrush brush( fillColorNode().color() ); - rectItem->setBrush( brush ); - - QPen pen( lineColorNode().color() ); - pen.setJoinStyle( Qt::MiterJoin ); - pen.setWidthF( lineWidth() ); - rectItem->setPen( pen ); - - updateGraphicsItemMatrix( rectItem ); - updateGraphicsItemShadow( rectItem ); - } + painter->setPen( QPen( lineColor, mLineWidth ) ); + painter->setBrush( fillColor ); + painter->drawRect( 0, 0, mW, mH ); } } diff --git a/glabels/LabelModelBoxObject.h b/glabels/LabelModelBoxObject.h index e17b4fb..e4117f8 100644 --- a/glabels/LabelModelBoxObject.h +++ b/glabels/LabelModelBoxObject.h @@ -77,11 +77,11 @@ namespace glabels /////////////////////////////////////////////////////////////// - // QGraphicsItem Method Implementations + // Drawing operations /////////////////////////////////////////////////////////////// - public: - virtual QGraphicsItem* createGraphicsItem(); - virtual void updateGraphicsItem( QGraphicsItem* graphicsItem ); + protected: + virtual void drawShadow( QPainter* painter, bool inEditor, MergeRecord* record ) const; + virtual void drawObject( QPainter* painter, bool inEditor, MergeRecord* record ) const; /////////////////////////////////////////////////////////////// diff --git a/glabels/LabelModelObject.cpp b/glabels/LabelModelObject.cpp index db461d6..a9f2ae1 100644 --- a/glabels/LabelModelObject.cpp +++ b/glabels/LabelModelObject.cpp @@ -20,10 +20,8 @@ #include "LabelModelObject.h" -#include #include #include -#include #include #include "ColorNode.h" @@ -903,30 +901,28 @@ namespace glabels /// - /// Update Representative Graphics Item with Object's Transformation Matrix + /// Draw object + shadow /// - void LabelModelObject::updateGraphicsItemMatrix( QGraphicsItem* graphicsItem ) + void LabelModelObject::draw( QPainter* painter, bool inEditor, MergeRecord* record ) const { - graphicsItem->setTransform( mMatrix ); + painter->save(); + painter->translate( mX0, mY0 ); + + if ( mShadowState ) + { + painter->save(); + painter->translate( mShadowX, mShadowY ); + painter->setTransform( mMatrix, true ); + drawShadow( painter, inEditor, record ); + painter->restore(); + } + + painter->setTransform( mMatrix, true ); + drawObject( painter, inEditor, record ); + + painter->restore(); } - /// - /// Update Representative Graphics Item with Object's Shadow Properties - /// - void LabelModelObject::updateGraphicsItemShadow( QGraphicsItem* graphicsItem ) - { - QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(); - - QColor color = mShadowColorNode.color(); - color.setAlphaF( mShadowOpacity ); - - shadowEffect->setColor( color ); - shadowEffect->setOffset( mShadowX, mShadowY ); - shadowEffect->setBlurRadius( 0 ); - - graphicsItem->setGraphicsEffect( shadowEffect ); - } - } diff --git a/glabels/LabelModelObject.h b/glabels/LabelModelObject.h index 8c88d0c..44ac9ef 100644 --- a/glabels/LabelModelObject.h +++ b/glabels/LabelModelObject.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "ColorNode.h" #include "TextNode.h" @@ -388,15 +389,32 @@ namespace glabels /////////////////////////////////////////////////////////////// - // QGraphicsItem methods + // Drawing operations /////////////////////////////////////////////////////////////// public: - virtual QGraphicsItem* createGraphicsItem() = 0; - virtual void updateGraphicsItem( QGraphicsItem* graphicsItem ) = 0; + void draw( QPainter* painter, bool inEditor, MergeRecord* record ) const; protected: - void updateGraphicsItemMatrix( QGraphicsItem* graphicsItem ); - void updateGraphicsItemShadow( QGraphicsItem* graphicsItem ); + virtual void drawShadow( QPainter* painter, bool inEditor, MergeRecord* record ) const = 0; + virtual void drawObject( QPainter* painter, bool inEditor, MergeRecord* record ) const = 0; + + + /////////////////////////////////////////////////////////////// + // Protected Members + /////////////////////////////////////////////////////////////// + protected: + bool mSelectedFlag; + + double mX0; + double mY0; + double mW; + double mH; + + bool mShadowState; + double mShadowX; + double mShadowY; + double mShadowOpacity; + ColorNode mShadowColorNode; /////////////////////////////////////////////////////////////// @@ -406,21 +424,8 @@ namespace glabels static int msNextId; int mId; - bool mSelectedFlag; - - double mX0; - double mY0; - double mW; - double mH; - QTransform mMatrix; - bool mShadowState; - double mShadowX; - double mShadowY; - double mShadowOpacity; - ColorNode mShadowColorNode; - }; } diff --git a/glabels/View.cpp b/glabels/View.cpp index b12f5c4..6650c96 100644 --- a/glabels/View.cpp +++ b/glabels/View.cpp @@ -288,6 +288,7 @@ glabels::View::paintEvent( QPaintEvent* event ) drawBgLayer( &painter ); drawGridLayer( &painter ); drawMarkupLayer( &painter ); + drawObjectsLayer( &painter ); } } @@ -628,3 +629,13 @@ glabels::View::drawMarkupLayer( QPainter* painter ) } +/// +/// Draw Objects Layer +/// +void +glabels::View::drawObjectsLayer( QPainter* painter ) +{ + mModel->draw( painter ); +} + +