Pointer cleanup (#242)
- Made greater use of smart pointers, eliminating many instances of manual memory management - Do not use pointers at all for many non-polymorphic classes - Assorted other code cleanup
This commit is contained in:
+54
-50
@@ -18,6 +18,7 @@
|
||||
* along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "Model.h"
|
||||
|
||||
#include "ModelObject.h"
|
||||
@@ -55,17 +56,9 @@ namespace glabels
|
||||
///
|
||||
Model::Model()
|
||||
{
|
||||
mVariables = new Variables();
|
||||
mMerge = new merge::None();
|
||||
mMerge.reset( new merge::None() );
|
||||
|
||||
connect( mVariables, SIGNAL(changed()), this, SLOT(onVariablesChanged()) );
|
||||
}
|
||||
|
||||
|
||||
Model::Model( merge::Merge* merge, Variables* variables )
|
||||
{
|
||||
mVariables = variables; // Shared
|
||||
mMerge = merge; // Shared
|
||||
connect( &mVariables, SIGNAL(changed()), this, SLOT(onVariablesChanged()) );
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +68,6 @@ namespace glabels
|
||||
Model::~Model()
|
||||
{
|
||||
qDeleteAll( mObjectList );
|
||||
// Final instance of mMerge and mVariables to be deleted by Model owner
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +76,7 @@ namespace glabels
|
||||
///
|
||||
Model* Model::save() const
|
||||
{
|
||||
auto* savedModel = new Model( mMerge, mVariables ); // mMerge and mVariables shared between models
|
||||
auto* savedModel = new Model(); // mMerge shared between models
|
||||
|
||||
if ( mFileName.isEmpty() && mUntitledInstance == 0 )
|
||||
{
|
||||
@@ -127,6 +119,10 @@ namespace glabels
|
||||
connect( object, SIGNAL(moved()), this, SLOT(onObjectMoved()) );
|
||||
}
|
||||
|
||||
mVariables.copy( savedModel->mVariables );
|
||||
|
||||
mMerge = savedModel->mMerge;
|
||||
|
||||
// Emit signals based on potential changes
|
||||
emit changed();
|
||||
emit selectionChanged();
|
||||
@@ -170,34 +166,34 @@ namespace glabels
|
||||
///
|
||||
/// Get template
|
||||
///
|
||||
const Template* Model::tmplate() const
|
||||
const Template& Model::tmplate() const
|
||||
{
|
||||
return &mTmplate;
|
||||
return mTmplate;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Get frame
|
||||
///
|
||||
const Frame* Model::frame() const
|
||||
const Frame* Model::frame( const QString& id ) const
|
||||
{
|
||||
return mTmplate.frames().constFirst();
|
||||
return mTmplate.frame( id );
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Set template
|
||||
///
|
||||
void Model::setTmplate( const Template* tmplate )
|
||||
void Model::setTmplate( const Template& tmplate )
|
||||
{
|
||||
mTmplate = *tmplate;
|
||||
mTmplate = tmplate;
|
||||
|
||||
setModified();
|
||||
|
||||
emit changed();
|
||||
emit sizeChanged();
|
||||
|
||||
Settings::addToRecentTemplateList( tmplate->name() );
|
||||
Settings::addToRecentTemplateList( tmplate.name() );
|
||||
}
|
||||
|
||||
|
||||
@@ -232,10 +228,9 @@ namespace glabels
|
||||
///
|
||||
Distance Model::w() const
|
||||
{
|
||||
auto& frames = mTmplate.frames();
|
||||
if ( !frames.isEmpty() )
|
||||
auto frame = mTmplate.frame();
|
||||
if ( frame )
|
||||
{
|
||||
auto* frame = mTmplate.frames().constFirst();
|
||||
return mRotate ? frame->h() : frame->w();
|
||||
}
|
||||
else
|
||||
@@ -250,10 +245,9 @@ namespace glabels
|
||||
///
|
||||
Distance Model::h() const
|
||||
{
|
||||
auto& frames = mTmplate.frames();
|
||||
if ( !frames.isEmpty() )
|
||||
auto frame = mTmplate.frame();
|
||||
if ( frame )
|
||||
{
|
||||
auto* frame = mTmplate.frames().constFirst();
|
||||
return mRotate ? frame->w() : frame->h();
|
||||
}
|
||||
else
|
||||
@@ -266,12 +260,10 @@ namespace glabels
|
||||
///
|
||||
/// Set height (if variable length)
|
||||
///
|
||||
void Model::setH( const Distance& h )
|
||||
void Model::setH( Distance h )
|
||||
{
|
||||
if ( auto* frame = mTmplate.frames().first() )
|
||||
if ( mTmplate.setH( h ) )
|
||||
{
|
||||
frame->setH( h );
|
||||
|
||||
setModified();
|
||||
|
||||
emit changed();
|
||||
@@ -350,7 +342,16 @@ namespace glabels
|
||||
///
|
||||
/// Get variables object
|
||||
///
|
||||
Variables* Model::variables() const
|
||||
Variables& Model::variables()
|
||||
{
|
||||
return mVariables;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Get const reference to variables object
|
||||
///
|
||||
const Variables& Model::constVariables() const
|
||||
{
|
||||
return mVariables;
|
||||
}
|
||||
@@ -361,7 +362,7 @@ namespace glabels
|
||||
///
|
||||
merge::Merge* Model::merge() const
|
||||
{
|
||||
return mMerge;
|
||||
return mMerge.get();
|
||||
}
|
||||
|
||||
|
||||
@@ -372,11 +373,10 @@ namespace glabels
|
||||
{
|
||||
if ( merge != mMerge )
|
||||
{
|
||||
delete mMerge;
|
||||
mMerge = merge;
|
||||
mMerge.reset( merge );
|
||||
|
||||
connect( mMerge, SIGNAL(sourceChanged()), this, SLOT(onMergeSourceChanged()) );
|
||||
connect( mMerge, SIGNAL(selectionChanged()), this, SLOT(onMergeSelectionChanged()) );
|
||||
connect( mMerge.get(), SIGNAL(sourceChanged()), this, SLOT(onMergeSourceChanged()) );
|
||||
connect( mMerge.get(), SIGNAL(selectionChanged()), this, SLOT(onMergeSelectionChanged()) );
|
||||
|
||||
setModified();
|
||||
|
||||
@@ -445,9 +445,9 @@ namespace glabels
|
||||
///
|
||||
/// Object at x,y
|
||||
///
|
||||
ModelObject* Model::objectAt( double scale,
|
||||
const Distance& x,
|
||||
const Distance& y ) const
|
||||
ModelObject* Model::objectAt( double scale,
|
||||
Distance x,
|
||||
Distance y ) const
|
||||
{
|
||||
/* Search object list in reverse order. I.e. from top to bottom. */
|
||||
QList<ModelObject*>::const_iterator it = mObjectList.end();
|
||||
@@ -468,20 +468,22 @@ namespace glabels
|
||||
///
|
||||
/// Handle at x,y
|
||||
///
|
||||
Handle* Model::handleAt( double scale,
|
||||
const Distance& x,
|
||||
const Distance& y ) const
|
||||
const Handle& Model::handleAt( double scale,
|
||||
Distance x,
|
||||
Distance y ) const
|
||||
{
|
||||
static Handle nullHandle;
|
||||
|
||||
foreach( ModelObject* object, mObjectList )
|
||||
{
|
||||
Handle* handle = object->handleAt( scale, x, y );
|
||||
if ( handle )
|
||||
auto& handle = object->handleAt( scale, x, y );
|
||||
if ( !handle.isNull() )
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return nullHandle;
|
||||
}
|
||||
|
||||
|
||||
@@ -1216,7 +1218,7 @@ namespace glabels
|
||||
///
|
||||
/// Move Selected Objects By dx,dy
|
||||
///
|
||||
void Model::moveSelection( const Distance& dx, const Distance& dy )
|
||||
void Model::moveSelection( Distance dx, Distance dy )
|
||||
{
|
||||
foreach ( ModelObject* object, mObjectList )
|
||||
{
|
||||
@@ -1387,7 +1389,7 @@ namespace glabels
|
||||
///
|
||||
/// Set Line Width Of Selected Objects
|
||||
///
|
||||
void Model::setSelectionLineWidth( const Distance& lineWidth )
|
||||
void Model::setSelectionLineWidth( Distance lineWidth )
|
||||
{
|
||||
foreach ( ModelObject* object, mObjectList )
|
||||
{
|
||||
@@ -1450,8 +1452,7 @@ namespace glabels
|
||||
{
|
||||
QClipboard *clipboard = QApplication::clipboard();
|
||||
|
||||
QByteArray buffer;
|
||||
XmlLabelCreator::serializeObjects( getSelection(), this, buffer );
|
||||
auto buffer = XmlLabelCreator::serializeObjects( getSelection(), this );
|
||||
|
||||
auto *mimeData = new QMimeData;
|
||||
mimeData->setData( MIME_TYPE, buffer );
|
||||
@@ -1607,11 +1608,14 @@ namespace glabels
|
||||
///
|
||||
/// Draw label objects
|
||||
///
|
||||
void Model::draw( QPainter* painter, bool inEditor, merge::Record* record, Variables* variables ) const
|
||||
void Model::draw( QPainter* painter,
|
||||
bool inEditor,
|
||||
const merge::Record& record,
|
||||
const Variables& variablesInstance ) const
|
||||
{
|
||||
foreach ( ModelObject* object, mObjectList )
|
||||
{
|
||||
object->draw( painter, inEditor, record, variables );
|
||||
object->draw( painter, inEditor, record, variablesInstance );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user