diff --git a/glabels/View.cpp b/glabels/View.cpp index aa60043..8da9cc2 100644 --- a/glabels/View.cpp +++ b/glabels/View.cpp @@ -510,20 +510,40 @@ glabels::View::leaveEvent( QEvent* event ) void glabels::View::drawBgLayer( QPainter* painter ) { + /* + * Draw shadow + */ painter->save(); painter->setBrush( QBrush( shadowColor ) ); painter->setPen( Qt::NoPen ); + painter->translate( shadowOffsetPixels/mZoom, shadowOffsetPixels/mZoom ); - painter->drawPath( mModel->frame()->path( mModel->rotate() ) ); + + if ( mModel->rotate() ) + { + painter->rotate( -90 ); + painter->translate( -mModel->frame()->w(), 0 ); + } + painter->drawPath( mModel->frame()->path() ); painter->restore(); - + + + /* + * Draw label + */ painter->save(); painter->setBrush( QBrush( labelColor ) ); painter->setPen( QPen( labelOutlineColor ) ); - painter->drawPath( mModel->frame()->path( mModel->rotate() ) ); + + if ( mModel->rotate() ) + { + painter->rotate( -90 ); + painter->translate( -mModel->frame()->w(), 0 ); + } + painter->drawPath( mModel->frame()->path() ); painter->restore(); } @@ -537,8 +557,8 @@ glabels::View::drawGridLayer( QPainter* painter ) { if ( mGridVisible ) { - double w = mModel->w(); - double h = mModel->h(); + double w = mModel->frame()->w(); + double h = mModel->frame()->h(); double x0, y0; if ( dynamic_cast( mModel->frame() ) ) @@ -554,8 +574,13 @@ glabels::View::drawGridLayer( QPainter* painter ) } painter->save(); + if ( mModel->rotate() ) + { + painter->rotate( -90 ); + painter->translate( -mModel->frame()->w(), 0 ); + } - painter->setClipPath( mModel->frame()->path( mModel->rotate() ) ); + painter->setClipPath( mModel->frame()->path() ); painter->setPen( QPen( gridLineColor, gridLineWidthPixels/mZoom ) ); diff --git a/libglabels/Frame.h b/libglabels/Frame.h index 356ed14..ab7093f 100644 --- a/libglabels/Frame.h +++ b/libglabels/Frame.h @@ -25,7 +25,6 @@ #include #include #include -#include #include #include "Units.h" @@ -66,9 +65,8 @@ namespace libglabels virtual const QString sizeDescription( const Units* units ) const = 0; virtual bool isSimilarTo( Frame* other ) const = 0; - virtual const QPainterPath &path( bool isRotated = false ) const = 0; - virtual QGraphicsItem* createMarginGraphicsItem( double size, - const QPen& pen ) const = 0; + virtual const QPainterPath& path() const = 0; + virtual QPainterPath marginPath( double size ) const = 0; private: diff --git a/libglabels/FrameCd.cpp b/libglabels/FrameCd.cpp index 977d2ac..ccb2359 100644 --- a/libglabels/FrameCd.cpp +++ b/libglabels/FrameCd.cpp @@ -32,49 +32,22 @@ namespace libglabels FrameCd::FrameCd( double r1, double r2, double w, double h, double waste, QString id ) : mR1(r1), mR2(r2), mW(w), mH(h), mWaste(waste), Frame(id) { - // - // First, initialize the un-rotated path - // - { - // Outer path (may be clipped in the case business card type CD) - double theta1 = acos( w / (2*mR1) ) * 180/M_PI; - double theta2 = asin( h / (2*mR1) ) * 180/M_PI; + // Outer path (may be clipped in the case business card type CD) + double theta1 = acos( w / (2*mR1) ) * 180/M_PI; + double theta2 = asin( h / (2*mR1) ) * 180/M_PI; - mPath.arcMoveTo( 0, 0, 2*mR1, 2*mR1, theta1 ); - mPath.arcTo( 0, 0, 2*mR1, 2*mR1, theta1, theta2-theta1 ); - mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180-theta2, theta2-theta1 ); - mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180+theta1, theta2-theta1 ); - mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 360-theta2, theta2-theta1 ); - mPath.closeSubpath(); + mPath.arcMoveTo( 0, 0, 2*mR1, 2*mR1, theta1 ); + mPath.arcTo( 0, 0, 2*mR1, 2*mR1, theta1, theta2-theta1 ); + mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180-theta2, theta2-theta1 ); + mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180+theta1, theta2-theta1 ); + mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 360-theta2, theta2-theta1 ); + mPath.closeSubpath(); - // Inner path (hole) - mPath.addEllipse( mR1-mR2, mR1-mR2, 2*mR2, 2*mR2 ); + // Inner path (hole) + mPath.addEllipse( mR1-mR2, mR1-mR2, 2*mR2, 2*mR2 ); - // Translate to account for offset with clipped business card CDs - mPath.translate( w/2 - mR1, h/2 - mR1 ); - } - - // - // Next, initialize the rotated path - // - { - // Outer path (may be clipped in the case business card type CD) - double theta1 = acos( h / (2*mR1) ) * 180/M_PI; - double theta2 = asin( w / (2*mR1) ) * 180/M_PI; - - mRotatedPath.arcMoveTo( 0, 0, 2*mR1, 2*mR1, theta1 ); - mRotatedPath.arcTo( 0, 0, 2*mR1, 2*mR1, theta1, theta2-theta1 ); - mRotatedPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180-theta2, theta2-theta1 ); - mRotatedPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180+theta1, theta2-theta1 ); - mRotatedPath.arcTo( 0, 0, 2*mR1, 2*mR1, 360-theta2, theta2-theta1 ); - mRotatedPath.closeSubpath(); - - // Inner path (hole) - mRotatedPath.addEllipse( mR1-mR2, mR1-mR2, 2*mR2, 2*mR2 ); - - // Translate to account for offset with clipped business card CDs - mRotatedPath.translate( h/2 - mR1, w/2 - mR1 ); - } + // Translate to account for offset with clipped business card CDs + mPath.translate( w/2 - mR1, h/2 - mR1 ); } @@ -103,7 +76,7 @@ namespace libglabels } - const QString FrameCd::sizeDescription( const Units *units ) const + const QString FrameCd::sizeDescription( const Units* units ) const { if ( units->id() == "in" ) { @@ -124,7 +97,7 @@ namespace libglabels } - bool FrameCd::isSimilarTo( Frame *other ) const + bool FrameCd::isSimilarTo( Frame* other ) const { if ( FrameCd *otherCd = dynamic_cast(other) ) { @@ -140,13 +113,13 @@ namespace libglabels } - const QPainterPath& FrameCd::path( bool isRotated ) const + const QPainterPath& FrameCd::path() const { - return isRotated ? mRotatedPath : mPath; + return mPath; } - QGraphicsItem* FrameCd::createMarginGraphicsItem( double size, const QPen& pen ) const + QPainterPath FrameCd::marginPath( double size ) const { double r1 = mR1 - size; double r2 = mR2 + size; @@ -167,13 +140,10 @@ namespace libglabels // Inner path (hole) path.addEllipse( r1-r2, r1-r2, 2*r2, 2*r2 ); - // Translate to account for offset with clipped business card CDs (applies to element already drawn) + // Translate to account for offset with clipped business card CDs path.translate( mW/2 - r1, mH/2 - r1 ); - QGraphicsPathItem* item = new QGraphicsPathItem( path ); - item->setPen( pen ); - - return item; + return path; } } diff --git a/libglabels/FrameCd.h b/libglabels/FrameCd.h index 36daa58..9bbc580 100644 --- a/libglabels/FrameCd.h +++ b/libglabels/FrameCd.h @@ -43,11 +43,11 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units *units ) const; - bool isSimilarTo( Frame *other ) const; + const QString sizeDescription( const Units* units ) const; + bool isSimilarTo( Frame* other ) const; - const QPainterPath &path( bool isRotated ) const; - QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; + const QPainterPath& path() const; + QPainterPath marginPath( double size ) const; private: @@ -58,7 +58,6 @@ namespace libglabels double mWaste; QPainterPath mPath; - QPainterPath mRotatedPath; }; diff --git a/libglabels/FrameEllipse.cpp b/libglabels/FrameEllipse.cpp index 1c4ebbe..b7a46e7 100644 --- a/libglabels/FrameEllipse.cpp +++ b/libglabels/FrameEllipse.cpp @@ -33,7 +33,6 @@ namespace libglabels : mW(w), mH(h), mWaste(waste), Frame(id) { mPath.addEllipse( 0, 0, mW, mH ); - mRotatedPath.addEllipse( 0, 0, mH, mW ); } FrameEllipse::FrameEllipse( const FrameEllipse& other ) @@ -60,7 +59,7 @@ namespace libglabels } - const QString FrameEllipse::sizeDescription( const Units *units ) const + const QString FrameEllipse::sizeDescription( const Units* units ) const { if ( units->id() == "in" ) { @@ -96,21 +95,21 @@ namespace libglabels } - const QPainterPath& FrameEllipse::path( bool isRotated ) const + const QPainterPath& FrameEllipse::path() const { - return isRotated ? mRotatedPath : mPath; + return mPath; } - QGraphicsItem* FrameEllipse::createMarginGraphicsItem( double size, const QPen& pen ) const + QPainterPath FrameEllipse::marginPath( double size ) const { double w = mW - 2*size; double h = mH - 2*size; - QGraphicsEllipseItem* item = new QGraphicsEllipseItem( size, size, w, h ); - item->setPen( pen ); + QPainterPath path; + path.addEllipse( size, size, w, h ); - return item; + return path; } } diff --git a/libglabels/FrameEllipse.h b/libglabels/FrameEllipse.h index 27b7ca3..d70b31a 100644 --- a/libglabels/FrameEllipse.h +++ b/libglabels/FrameEllipse.h @@ -45,8 +45,8 @@ namespace libglabels const QString sizeDescription( const Units* units ) const; bool isSimilarTo( Frame* other ) const; - const QPainterPath& path( bool isRotated ) const; - QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; + const QPainterPath& path() const; + QPainterPath marginPath( double size ) const; private: @@ -55,7 +55,6 @@ namespace libglabels double mWaste; QPainterPath mPath; - QPainterPath mRotatedPath; }; diff --git a/libglabels/FrameRect.cpp b/libglabels/FrameRect.cpp index 46671ee..8b6c61e 100644 --- a/libglabels/FrameRect.cpp +++ b/libglabels/FrameRect.cpp @@ -33,7 +33,6 @@ namespace libglabels : mW(w), mH(h), mR(r), mXWaste(xWaste), mYWaste(yWaste), Frame(id) { mPath.addRoundedRect( 0, 0, mW, mH, mR, mR ); - mRotatedPath.addRoundedRect( 0, 0, mH, mW, mR, mR ); } @@ -62,7 +61,7 @@ namespace libglabels } - const QString FrameRect::sizeDescription( const Units *units ) const + const QString FrameRect::sizeDescription( const Units* units ) const { if ( units->id() == "in" ) { @@ -84,7 +83,7 @@ namespace libglabels } - bool FrameRect::isSimilarTo( Frame *other ) const + bool FrameRect::isSimilarTo( Frame* other ) const { if ( FrameRect *otherRect = dynamic_cast(other) ) { @@ -98,13 +97,13 @@ namespace libglabels } - const QPainterPath& FrameRect::path( bool isRotated ) const + const QPainterPath& FrameRect::path() const { - return isRotated ? mRotatedPath : mPath; + return mPath; } - QGraphicsItem* FrameRect::createMarginGraphicsItem( double size, const QPen& pen ) const + QPainterPath FrameRect::marginPath( double size ) const { double w = mW - 2*size; double h = mH - 2*size; @@ -113,10 +112,7 @@ namespace libglabels QPainterPath path; path.addRoundedRect( size, size, w, h, r, r ); - QGraphicsPathItem* item = new QGraphicsPathItem( path ); - item->setPen( pen ); - - return item; + return path; } diff --git a/libglabels/FrameRect.h b/libglabels/FrameRect.h index dc7cbd0..b9795f0 100644 --- a/libglabels/FrameRect.h +++ b/libglabels/FrameRect.h @@ -48,12 +48,12 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units *units ) const; + const QString sizeDescription( const Units* units ) const; - bool isSimilarTo( Frame *other ) const; + bool isSimilarTo( Frame* other ) const; - const QPainterPath& path( bool isRotated ) const; - QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; + const QPainterPath& path() const; + QPainterPath marginPath( double size ) const; private: @@ -64,7 +64,6 @@ namespace libglabels double mYWaste; QPainterPath mPath; - QPainterPath mRotatedPath; }; diff --git a/libglabels/FrameRound.cpp b/libglabels/FrameRound.cpp index e8470ff..0e59f7e 100644 --- a/libglabels/FrameRound.cpp +++ b/libglabels/FrameRound.cpp @@ -60,7 +60,7 @@ namespace libglabels } - const QString FrameRound::sizeDescription( const Units *units ) const + const QString FrameRound::sizeDescription( const Units* units ) const { if ( units->id() == "in" ) { @@ -81,7 +81,7 @@ namespace libglabels } - bool FrameRound::isSimilarTo( Frame *other ) const + bool FrameRound::isSimilarTo( Frame* other ) const { if ( FrameRound *otherRound = dynamic_cast(other) ) { @@ -94,20 +94,20 @@ namespace libglabels } - const QPainterPath& FrameRound::path( bool isRotated ) const + const QPainterPath& FrameRound::path() const { return mPath; } - QGraphicsItem* FrameRound::createMarginGraphicsItem( double size, const QPen& pen ) const + QPainterPath FrameRound::marginPath( double size ) const { double r = mR - size; - QGraphicsEllipseItem* item = new QGraphicsEllipseItem( mR-r, mR-r, 2*r, 2*r ); - item->setPen( pen ); + QPainterPath path; + path.addEllipse( size, size, 2*r, 2*r ); - return item; + return path; } } diff --git a/libglabels/FrameRound.h b/libglabels/FrameRound.h index 56b6abb..5d3090a 100644 --- a/libglabels/FrameRound.h +++ b/libglabels/FrameRound.h @@ -43,11 +43,11 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units *units ) const; - bool isSimilarTo( Frame *other ) const; + const QString sizeDescription( const Units* units ) const; + bool isSimilarTo( Frame* other ) const; - const QPainterPath &path( bool isRotated ) const; - QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; + const QPainterPath& path() const; + QPainterPath marginPath( double size ) const; private: diff --git a/libglabels/Markup.cpp b/libglabels/Markup.cpp index 7626e4a..72d6e35 100644 --- a/libglabels/Markup.cpp +++ b/libglabels/Markup.cpp @@ -24,27 +24,30 @@ namespace libglabels { - MarkupMargin::MarkupMargin( double size ) - : mSize(size) + const QPainterPath& Markup::path() const { + return mPath; + } + + + MarkupMargin::MarkupMargin( const Frame* frame, double size ) + : mFrame(frame), mSize(size) + { + mPath = frame->marginPath( size ); } Markup* MarkupMargin::dup() const { - return new MarkupMargin( mSize ); - } - - - QGraphicsItem* MarkupMargin::createGraphicsItem( const Frame* frame, const QPen& pen ) const - { - return frame->createMarginGraphicsItem( mSize, pen ); + return new MarkupMargin( mFrame, mSize ); } MarkupLine::MarkupLine( double x1, double y1, double x2, double y2 ) : mX1(x1), mY1(y1), mX2(x2), mY2(y2) { + mPath.moveTo( x1, y1 ); + mPath.lineTo( x2, y2 ); } @@ -54,18 +57,10 @@ namespace libglabels } - QGraphicsItem* MarkupLine::createGraphicsItem( const Frame* frame, const QPen& pen ) const - { - QGraphicsLineItem* item = new QGraphicsLineItem( mX1, mY1, mX2, mY2 ); - item->setPen( pen ); - - return item; - } - - MarkupRect::MarkupRect( double x1, double y1, double w, double h, double r ) : mX1(x1), mY1(y1), mW(w), mH(h), mR(r) { + mPath.addRoundedRect( x1, y1, w, h, r, r ); } @@ -75,21 +70,10 @@ namespace libglabels } - QGraphicsItem* MarkupRect::createGraphicsItem( const Frame* frame, const QPen& pen ) const - { - QPainterPath path; - path.addRoundedRect( mX1, mY1, mW, mH, mR, mR ); - - QGraphicsPathItem* item = new QGraphicsPathItem( path ); - item->setPen( pen ); - - return item; - } - - MarkupEllipse::MarkupEllipse( double x1, double y1, double w, double h ) : mX1(x1), mY1(y1), mW(w), mH(h) { + mPath.addEllipse( x1, y1, w, h ); } @@ -99,18 +83,10 @@ namespace libglabels } - QGraphicsItem* MarkupEllipse::createGraphicsItem( const Frame* frame, const QPen& pen ) const - { - QGraphicsEllipseItem* item = new QGraphicsEllipseItem( mX1, mY1, mW, mH ); - item->setPen( pen ); - - return item; - } - - MarkupCircle::MarkupCircle( double x0, double y0, double r ) : mX0(x0), mY0(y0), mR(r) { + mPath.addEllipse( x0-r, y0-r, 2*r, 2*r ); } Markup* MarkupCircle::dup() const @@ -118,13 +94,4 @@ namespace libglabels return new MarkupCircle( mX0, mY0, mR ); } - - QGraphicsItem* MarkupCircle::createGraphicsItem( const Frame* frame, const QPen& pen ) const - { - QGraphicsEllipseItem* item = new QGraphicsEllipseItem( mX0-mR, mY0-mR, 2*mR, 2*mR ); - item->setPen( pen ); - - return item; - } - } diff --git a/libglabels/Markup.h b/libglabels/Markup.h index 4f25819..d7c1e60 100644 --- a/libglabels/Markup.h +++ b/libglabels/Markup.h @@ -21,7 +21,6 @@ #ifndef libglabels_Markup_h #define libglabels_Markup_h -#include #include #include "Frame.h" @@ -34,22 +33,25 @@ namespace libglabels { public: virtual Markup* dup() const = 0; - virtual QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const = 0; + + const QPainterPath& path() const; + + protected: + QPainterPath mPath; }; class MarkupMargin : public Markup { public: - MarkupMargin( double size ); + MarkupMargin( const Frame* frame, double size ); double size() const; Markup* dup() const; - QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const; - private: + const Frame* mFrame; double mSize; }; @@ -66,8 +68,6 @@ namespace libglabels Markup* dup() const; - QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const; - private: double mX1; double mY1; @@ -89,8 +89,6 @@ namespace libglabels Markup* dup() const; - QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const; - private: double mX1; double mY1; @@ -112,8 +110,6 @@ namespace libglabels Markup* dup() const; - QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const; - private: double mX1; double mY1; @@ -133,8 +129,6 @@ namespace libglabels Markup* dup() const; - QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const; - private: double mX0; double mY0; diff --git a/libglabels/XmlTemplateParser.cpp b/libglabels/XmlTemplateParser.cpp index 0d705ea..c59714e 100644 --- a/libglabels/XmlTemplateParser.cpp +++ b/libglabels/XmlTemplateParser.cpp @@ -358,7 +358,7 @@ namespace libglabels { double size = XmlUtil::getLengthAttr( node, "size", 0 ); - frame->addMarkup( new MarkupMargin( size ) ); + frame->addMarkup( new MarkupMargin( frame, size ) ); }