From 060fd6d143b3e17a925840fa5c783ad67f0d4fc2 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Tue, 26 Nov 2013 18:46:20 -0500 Subject: [PATCH] More methods for LabelModel. --- app/LabelModel.cpp | 310 +++++++++++++++++++++++++++++++++++++++++++++ app/LabelModel.h | 48 ++++++- 2 files changed, 355 insertions(+), 3 deletions(-) diff --git a/app/LabelModel.cpp b/app/LabelModel.cpp index 50c579e..889918f 100644 --- a/app/LabelModel.cpp +++ b/app/LabelModel.cpp @@ -360,5 +360,315 @@ namespace glabels rotateSelection( 90.0 ); } + + void LabelModel::flipSelectionHoriz() + { + foreach ( LabelModelItem *item, mItemList ) + { + if ( item->isSelected() ) + { + item->flipHoriz(); + } + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::flipSelectionVert() + { + foreach ( LabelModelItem *item, mItemList ) + { + if ( item->isSelected() ) + { + item->flipVert(); + } + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionLeft() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find left-most edge. */ + double x1_min = 7200; /* Start with a very large value: 7200pts = 100in */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + if ( r.x1() < x1_min ) x1_min = r.x1(); + } + + /* Now adjust the object positions to line up the left edges at left-most edge. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dx = x1_min - r.x1(); + item->setPositionRelative( dx, 0 ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionRight() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find right-most edge. */ + double x1_max = -7200; /* Start with a very large negative value: 7200pts = 100in */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + if ( r.x1() > x1_max ) x1_max = r.x1(); + } + + /* Now adjust the object positions to line up the right edges at right-most edge. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dx = x1_max - r.x1(); + item->setPositionRelative( dx, 0 ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionHCenter() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find average center of objects. */ + double xsum = 0; + int n = 0; + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + xsum += (r.x1() + r.x2()) / 2.0; + n++; + } + double xavg = xsum / n; + + /* Find item closest to average center of objects. */ + double xcenter = 7200; /* Start with very large value. */ + double dxmin = fabs( xavg - xcenter ); + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dx = fabs( xavg - (r.x1() + r.x2())/2.0 ); + if ( dx < dxmin ) + { + dxmin = dx; + xcenter = (r.x1() + r.x2()) / 2.0; + } + } + + /* Now adjust the object positions to line up with the center of this item. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dx = xcenter - (r.x1() + r.x2())/2.0; + item->setPositionRelative( dx, 0 ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionTop() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find top-most edge. */ + double y1_min = 7200; /* Start with a very large value: 7200pts = 100in */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + if ( r.y1() < y1_min ) y1_min = r.y1(); + } + + /* Now adjust the object positions to line up the top edges at top-most edge. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dy = y1_min - r.y1(); + item->setPositionRelative( 0, dy ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionBottom() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find bottom-most edge. */ + double y1_max = -7200; /* Start with a very large negative value: 7200pts = 100in */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + if ( r.y1() > y1_max ) y1_max = r.y1(); + } + + /* Now adjust the object positions to line up the bottom edges at bottom-most edge. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dy = y1_max - r.y1(); + item->setPositionRelative( 0, dy ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::alignSelectionVCenter() + { + if ( isSelectionEmpty() || isSelectionAtomic() ) + { + return; + } + + QList selectedList = getSelection(); + + /* Find average center of objects. */ + double ysum = 0; + int n = 0; + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + ysum += (r.y1() + r.y2()) / 2.0; + n++; + } + double yavg = ysum / n; + + /* Find item closest to average center of objects. */ + double ycenter = 7200; /* Start with very large value. */ + double dymin = fabs( yavg - ycenter ); + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dy = fabs( yavg - (r.y1() + r.y2())/2.0 ); + if ( dy < dymin ) + { + dymin = dy; + ycenter = (r.y1() + r.y2()) / 2.0; + } + } + + /* Now adjust the object positions to line up with the center of this item. */ + foreach ( LabelModelItem *item, selectedList ) + { + LabelRegion r = item->getExtent(); + double dy = ycenter - (r.y1() + r.y2())/2.0; + item->setPositionRelative( 0, dy ); + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::centerSelectionHoriz() + { + double xLabelCenter = w() / 2.0; + + foreach ( LabelModelItem *item, mItemList ) + { + if ( item->isSelected() ) + { + LabelRegion r = item->getExtent(); + double xItemCenter = (r.x1() + r.x2()) / 2.0; + double dx = xLabelCenter - xItemCenter; + item->setPositionRelative( dx, 0 ); + } + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::centerSelectionVert() + { + double yLabelCenter = h() / 2.0; + + foreach ( LabelModelItem *item, mItemList ) + { + if ( item->isSelected() ) + { + LabelRegion r = item->getExtent(); + double yItemCenter = (r.y1() + r.y2()) / 2.0; + double dy = yLabelCenter - yItemCenter; + item->setPositionRelative( 0, dy ); + } + } + + mModified = true; + + emit changed(); + } + + + void LabelModel::moveSelection( double dx, double dy ) + { + foreach ( LabelModelItem *item, mItemList ) + { + if ( item->isSelected() ) + { + item->setPositionRelative( dx, dy ); + } + } + + mModified = true; + + emit changed(); + } + } diff --git a/app/LabelModel.h b/app/LabelModel.h index 0bac480..aa8a7a7 100644 --- a/app/LabelModel.h +++ b/app/LabelModel.h @@ -24,6 +24,7 @@ #include #include +#include "libglabels/Template.h" #include "LabelModelItem.h" @@ -40,6 +41,7 @@ namespace glabels signals: void changed(); + void sizeChanged(); void selectionChanged(); void itemAdded( LabelModelItem *item ); void itemDeleted( LabelModelItem *item ); @@ -47,9 +49,36 @@ namespace glabels void itemToBottom( LabelModelItem *item ); public: - Q_PROPERTY( bool modified READ isModified ); bool isModified( void ) const { return mModified; } + const libglabels::Template *tmplate() const { return mTmplate; } + void setTmplate( const libglabels::Template *tmplate ) + { + if (mTmplate != tmplate) + { + mTmplate = tmplate; + mFrame = tmplate->frames().first(); + mModified = true; + emit changed(); + emit sizeChanged(); + } + } + + bool rotate() const { return mRotate; } + void setRotate( bool rotate ) + { + if (mRotate != rotate) + { + mRotate = rotate; + mModified = true; + emit changed(); + emit sizeChanged(); + } + } + + double w() const { return mRotate ? mFrame->h() : mFrame->w(); } + double h() const { return mRotate ? mFrame->w() : mFrame->h(); } + void addItem( LabelModelItem *item ); @@ -88,6 +117,17 @@ namespace glabels void rotateSelection( double thetaDegs ); void rotateSelectionLeft(); void rotateSelectionRight(); + void flipSelectionHoriz(); + void flipSelectionVert(); + void alignSelectionLeft(); + void alignSelectionRight(); + void alignSelectionHCenter(); + void alignSelectionTop(); + void alignSelectionBottom(); + void alignSelectionVCenter(); + void centerSelectionHoriz(); + void centerSelectionVert(); + void moveSelection( double dx, double dy ); private slots: @@ -99,8 +139,10 @@ namespace glabels QList mItemList; - bool mModified; - + bool mModified; + const libglabels::Template *mTmplate; + const libglabels::Frame *mFrame; + bool mRotate; }; }