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
+2 -4
View File
@@ -25,7 +25,6 @@
#include <QString>
#include <QList>
#include <QPainterPath>
#include <QGraphicsItem>
#include <QVector>
#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:
+20 -50
View File
@@ -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<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 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;
}
}
+4 -5
View File
@@ -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;
};
+7 -8
View File
@@ -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;
}
}
+2 -3
View File
@@ -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;
};
+6 -10
View File
@@ -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<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 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;
}
+4 -5
View File
@@ -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;
};
+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" )
{
@@ -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) )
{
@@ -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;
}
}
+4 -4
View File
@@ -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:
+15 -48
View File
@@ -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;
}
}
+7 -13
View File
@@ -21,7 +21,6 @@
#ifndef libglabels_Markup_h
#define libglabels_Markup_h
#include <QGraphicsItem>
#include <QPainterPath>
#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;
+1 -1
View File
@@ -358,7 +358,7 @@ namespace libglabels
{
double size = XmlUtil::getLengthAttr( node, "size", 0 );
frame->addMarkup( new MarkupMargin( size ) );
frame->addMarkup( new MarkupMargin( frame, size ) );
}