Refactor frame and markup code to use paths.

- CD frames are currently broken.
This commit is contained in:
Jim Evins
2015-08-12 01:20:52 -04:00
parent 0ad6caeeca
commit 721746007c
13 changed files with 110 additions and 164 deletions
+30 -5
View File
@@ -510,20 +510,40 @@ glabels::View::leaveEvent( QEvent* event )
void void
glabels::View::drawBgLayer( QPainter* painter ) glabels::View::drawBgLayer( QPainter* painter )
{ {
/*
* Draw shadow
*/
painter->save(); painter->save();
painter->setBrush( QBrush( shadowColor ) ); painter->setBrush( QBrush( shadowColor ) );
painter->setPen( Qt::NoPen ); painter->setPen( Qt::NoPen );
painter->translate( shadowOffsetPixels/mZoom, shadowOffsetPixels/mZoom ); 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(); painter->restore();
/*
* Draw label
*/
painter->save(); painter->save();
painter->setBrush( QBrush( labelColor ) ); painter->setBrush( QBrush( labelColor ) );
painter->setPen( QPen( labelOutlineColor ) ); 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(); painter->restore();
} }
@@ -537,8 +557,8 @@ glabels::View::drawGridLayer( QPainter* painter )
{ {
if ( mGridVisible ) if ( mGridVisible )
{ {
double w = mModel->w(); double w = mModel->frame()->w();
double h = mModel->h(); double h = mModel->frame()->h();
double x0, y0; double x0, y0;
if ( dynamic_cast<const libglabels::FrameRect*>( mModel->frame() ) ) if ( dynamic_cast<const libglabels::FrameRect*>( mModel->frame() ) )
@@ -554,8 +574,13 @@ glabels::View::drawGridLayer( QPainter* painter )
} }
painter->save(); 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 ) ); painter->setPen( QPen( gridLineColor, gridLineWidthPixels/mZoom ) );
+2 -4
View File
@@ -25,7 +25,6 @@
#include <QString> #include <QString>
#include <QList> #include <QList>
#include <QPainterPath> #include <QPainterPath>
#include <QGraphicsItem>
#include <QVector> #include <QVector>
#include "Units.h" #include "Units.h"
@@ -66,9 +65,8 @@ namespace libglabels
virtual const QString sizeDescription( const Units* units ) const = 0; virtual const QString sizeDescription( const Units* units ) const = 0;
virtual bool isSimilarTo( Frame* other ) const = 0; virtual bool isSimilarTo( Frame* other ) const = 0;
virtual const QPainterPath &path( bool isRotated = false ) const = 0; virtual const QPainterPath& path() const = 0;
virtual QGraphicsItem* createMarginGraphicsItem( double size, virtual QPainterPath marginPath( double size ) const = 0;
const QPen& pen ) const = 0;
private: private:
+20 -50
View File
@@ -32,49 +32,22 @@ namespace libglabels
FrameCd::FrameCd( double r1, double r2, double w, double h, double waste, QString id ) 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) : mR1(r1), mR2(r2), mW(w), mH(h), mWaste(waste), Frame(id)
{ {
// // Outer path (may be clipped in the case business card type CD)
// First, initialize the un-rotated path 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.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, theta1, theta2-theta1 );
mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 180-theta2, 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, 180+theta1, theta2-theta1 );
mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 360-theta2, theta2-theta1 ); mPath.arcTo( 0, 0, 2*mR1, 2*mR1, 360-theta2, theta2-theta1 );
mPath.closeSubpath(); mPath.closeSubpath();
// Inner path (hole) // Inner path (hole)
mPath.addEllipse( mR1-mR2, mR1-mR2, 2*mR2, 2*mR2 ); mPath.addEllipse( mR1-mR2, mR1-mR2, 2*mR2, 2*mR2 );
// Translate to account for offset with clipped business card CDs // Translate to account for offset with clipped business card CDs
mPath.translate( w/2 - mR1, h/2 - mR1 ); 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 );
}
} }
@@ -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" ) 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<FrameCd*>(other) ) if ( FrameCd *otherCd = dynamic_cast<FrameCd*>(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 r1 = mR1 - size;
double r2 = mR2 + size; double r2 = mR2 + size;
@@ -167,13 +140,10 @@ namespace libglabels
// Inner path (hole) // Inner path (hole)
path.addEllipse( r1-r2, r1-r2, 2*r2, 2*r2 ); 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 ); path.translate( mW/2 - r1, mH/2 - r1 );
QGraphicsPathItem* item = new QGraphicsPathItem( path ); return path;
item->setPen( pen );
return item;
} }
} }
+4 -5
View File
@@ -43,11 +43,11 @@ namespace libglabels
double w() const; double w() const;
double h() 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; const QPainterPath& path() const;
QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; QPainterPath marginPath( double size ) const;
private: private:
@@ -58,7 +58,6 @@ namespace libglabels
double mWaste; double mWaste;
QPainterPath mPath; QPainterPath mPath;
QPainterPath mRotatedPath;
}; };
+7 -8
View File
@@ -33,7 +33,6 @@ namespace libglabels
: mW(w), mH(h), mWaste(waste), Frame(id) : mW(w), mH(h), mWaste(waste), Frame(id)
{ {
mPath.addEllipse( 0, 0, mW, mH ); mPath.addEllipse( 0, 0, mW, mH );
mRotatedPath.addEllipse( 0, 0, mH, mW );
} }
FrameEllipse::FrameEllipse( const FrameEllipse& other ) 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" ) 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 w = mW - 2*size;
double h = mH - 2*size; double h = mH - 2*size;
QGraphicsEllipseItem* item = new QGraphicsEllipseItem( size, size, w, h ); QPainterPath path;
item->setPen( pen ); path.addEllipse( size, size, w, h );
return item; return path;
} }
} }
+2 -3
View File
@@ -45,8 +45,8 @@ namespace libglabels
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; const QPainterPath& path() const;
QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; QPainterPath marginPath( double size ) const;
private: private:
@@ -55,7 +55,6 @@ namespace libglabels
double mWaste; double mWaste;
QPainterPath mPath; QPainterPath mPath;
QPainterPath mRotatedPath;
}; };
+6 -10
View File
@@ -33,7 +33,6 @@ namespace libglabels
: mW(w), mH(h), mR(r), mXWaste(xWaste), mYWaste(yWaste), Frame(id) : mW(w), mH(h), mR(r), mXWaste(xWaste), mYWaste(yWaste), Frame(id)
{ {
mPath.addRoundedRect( 0, 0, mW, mH, mR, mR ); 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" ) 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<FrameRect*>(other) ) if ( FrameRect *otherRect = dynamic_cast<FrameRect*>(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 w = mW - 2*size;
double h = mH - 2*size; double h = mH - 2*size;
@@ -113,10 +112,7 @@ namespace libglabels
QPainterPath path; QPainterPath path;
path.addRoundedRect( size, size, w, h, r, r ); path.addRoundedRect( size, size, w, h, r, r );
QGraphicsPathItem* item = new QGraphicsPathItem( path ); return path;
item->setPen( pen );
return item;
} }
+4 -5
View File
@@ -48,12 +48,12 @@ namespace libglabels
double w() const; double w() const;
double h() 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; const QPainterPath& path() const;
QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; QPainterPath marginPath( double size ) const;
private: private:
@@ -64,7 +64,6 @@ namespace libglabels
double mYWaste; double mYWaste;
QPainterPath mPath; QPainterPath mPath;
QPainterPath mRotatedPath;
}; };
+7 -7
View File
@@ -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" ) 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<FrameRound*>(other) ) if ( FrameRound *otherRound = dynamic_cast<FrameRound*>(other) )
{ {
@@ -94,20 +94,20 @@ namespace libglabels
} }
const QPainterPath& FrameRound::path( bool isRotated ) const const QPainterPath& FrameRound::path() const
{ {
return mPath; return mPath;
} }
QGraphicsItem* FrameRound::createMarginGraphicsItem( double size, const QPen& pen ) const QPainterPath FrameRound::marginPath( double size ) const
{ {
double r = mR - size; double r = mR - size;
QGraphicsEllipseItem* item = new QGraphicsEllipseItem( mR-r, mR-r, 2*r, 2*r ); QPainterPath path;
item->setPen( pen ); path.addEllipse( size, size, 2*r, 2*r );
return item; return path;
} }
} }
+4 -4
View File
@@ -43,11 +43,11 @@ namespace libglabels
double w() const; double w() const;
double h() 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; const QPainterPath& path() const;
QGraphicsItem* createMarginGraphicsItem( double size, const QPen& pen ) const; QPainterPath marginPath( double size ) const;
private: private:
+15 -48
View File
@@ -24,27 +24,30 @@
namespace libglabels namespace libglabels
{ {
MarkupMargin::MarkupMargin( double size ) const QPainterPath& Markup::path() const
: mSize(size)
{ {
return mPath;
}
MarkupMargin::MarkupMargin( const Frame* frame, double size )
: mFrame(frame), mSize(size)
{
mPath = frame->marginPath( size );
} }
Markup* MarkupMargin::dup() const Markup* MarkupMargin::dup() const
{ {
return new MarkupMargin( mSize ); return new MarkupMargin( mFrame, mSize );
}
QGraphicsItem* MarkupMargin::createGraphicsItem( const Frame* frame, const QPen& pen ) const
{
return frame->createMarginGraphicsItem( mSize, pen );
} }
MarkupLine::MarkupLine( double x1, double y1, double x2, double y2 ) MarkupLine::MarkupLine( double x1, double y1, double x2, double y2 )
: mX1(x1), mY1(y1), mX2(x2), mY2(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 ) MarkupRect::MarkupRect( double x1, double y1, double w, double h, double r )
: mX1(x1), mY1(y1), mW(w), mH(h), mR(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 ) MarkupEllipse::MarkupEllipse( double x1, double y1, double w, double h )
: mX1(x1), mY1(y1), mW(w), mH(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 ) MarkupCircle::MarkupCircle( double x0, double y0, double r )
: mX0(x0), mY0(y0), mR(r) : mX0(x0), mY0(y0), mR(r)
{ {
mPath.addEllipse( x0-r, y0-r, 2*r, 2*r );
} }
Markup* MarkupCircle::dup() const Markup* MarkupCircle::dup() const
@@ -118,13 +94,4 @@ namespace libglabels
return new MarkupCircle( mX0, mY0, mR ); 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;
}
} }
+7 -13
View File
@@ -21,7 +21,6 @@
#ifndef libglabels_Markup_h #ifndef libglabels_Markup_h
#define libglabels_Markup_h #define libglabels_Markup_h
#include <QGraphicsItem>
#include <QPainterPath> #include <QPainterPath>
#include "Frame.h" #include "Frame.h"
@@ -34,22 +33,25 @@ namespace libglabels
{ {
public: public:
virtual Markup* dup() const = 0; 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 class MarkupMargin : public Markup
{ {
public: public:
MarkupMargin( double size ); MarkupMargin( const Frame* frame, double size );
double size() const; double size() const;
Markup* dup() const; Markup* dup() const;
QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const;
private: private:
const Frame* mFrame;
double mSize; double mSize;
}; };
@@ -66,8 +68,6 @@ namespace libglabels
Markup* dup() const; Markup* dup() const;
QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const;
private: private:
double mX1; double mX1;
double mY1; double mY1;
@@ -89,8 +89,6 @@ namespace libglabels
Markup* dup() const; Markup* dup() const;
QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const;
private: private:
double mX1; double mX1;
double mY1; double mY1;
@@ -112,8 +110,6 @@ namespace libglabels
Markup* dup() const; Markup* dup() const;
QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const;
private: private:
double mX1; double mX1;
double mY1; double mY1;
@@ -133,8 +129,6 @@ namespace libglabels
Markup* dup() const; Markup* dup() const;
QGraphicsItem* createGraphicsItem( const Frame* frame, const QPen& pen ) const;
private: private:
double mX0; double mX0;
double mY0; double mY0;
+1 -1
View File
@@ -358,7 +358,7 @@ namespace libglabels
{ {
double size = XmlUtil::getLengthAttr( node, "size", 0 ); double size = XmlUtil::getLengthAttr( node, "size", 0 );
frame->addMarkup( new MarkupMargin( size ) ); frame->addMarkup( new MarkupMargin( frame, size ) );
} }