Skip to content

Commit

Permalink
Save/restore file paths as relative to project file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevins committed Aug 19, 2019
1 parent ec3d900 commit 6a789c9
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 132 deletions.
8 changes: 2 additions & 6 deletions glabels/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ namespace glabels
model::Model *model = model::XmlLabelParser::readFile( fileName );
if ( model )
{
model->setFileName( fileName );

// Either apply to current window or open a new one
if ( window->isEmpty() )
{
Expand Down Expand Up @@ -156,7 +154,6 @@ namespace glabels
}

model::XmlLabelCreator::writeFile( window->model(), window->model()->fileName() );
window->model()->clearModified();

// Save CWD
mCwd = QFileInfo( window->model()->fileName() ).absolutePath();
Expand All @@ -170,7 +167,8 @@ namespace glabels
///
bool File::saveAs( MainWindow *window )
{
// Either use the saved CWD from a previous open/save or grab it from the path of the current file
// Either use the saved CWD from a previous open/save or grab it from the path
// of the current file.
QString cwd = mCwd;
if ( window->model() && !window->model()->fileName().isEmpty() )
{
Expand Down Expand Up @@ -210,8 +208,6 @@ namespace glabels
}

model::XmlLabelCreator::writeFile( window->model(), fileName );
window->model()->setFileName( fileName );
window->model()->clearModified();

// Save CWD
mCwd = QFileInfo( fileName ).absolutePath();
Expand Down
6 changes: 3 additions & 3 deletions glabels/MergeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace glabels
mUndoRedoModel = undoRedoModel;

// Initialize CWD
mCwd = mModel->dir();
mCwd = mModel->dirPath();

onMergeChanged();
connect( mModel, SIGNAL(mergeChanged()), this, SLOT(onMergeChanged()) );
Expand Down Expand Up @@ -93,7 +93,7 @@ namespace glabels

case merge::Factory::FILE:
locationLabel->setEnabled( true );
fn = QDir(mModel->dir()).relativeFilePath( mModel->merge()->source() );
fn = mModel->dir().relativeFilePath( mModel->merge()->source() );
locationLineEdit->setText( fn );
locationBrowseButton->setVisible( true );
break;
Expand Down Expand Up @@ -124,7 +124,7 @@ namespace glabels
///
void MergeView::onMergeSourceChanged()
{
QString fn = QDir(mModel->dir()).relativeFilePath( mModel->merge()->source() );
QString fn = mModel->dir().relativeFilePath( mModel->merge()->source() );
locationLineEdit->setText( fn );

recordsTable->clear();
Expand Down
2 changes: 1 addition & 1 deletion glabels/ObjectEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace glabels
}
else
{
QString fn = QDir(mModel->dir()).relativeFilePath( filenameNode.data() );
QString fn = mModel->dir().relativeFilePath( filenameNode.data() );
imageFilenameLineEdit->setText( fn );
}

Expand Down
25 changes: 20 additions & 5 deletions model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include <QApplication>
#include <QClipboard>
#include <QDir>
#include <QFileInfo>
#include <QMimeData>
#include <QtDebug>
Expand Down Expand Up @@ -282,9 +281,25 @@ namespace glabels


///
/// Get directory.
/// Get directory as a QDir.
///
QString Model::dir() const
QDir Model::dir() const
{
if ( mFileName.isEmpty() )
{
return QDir::current();
}
else
{
return QFileInfo( mFileName ).absoluteDir();
}
}


///
/// Get directory as a path.
///
QString Model::dirPath() const
{
if ( mFileName.isEmpty() )
{
Expand Down Expand Up @@ -1400,7 +1415,7 @@ namespace glabels
QClipboard *clipboard = QApplication::clipboard();

QByteArray buffer;
XmlLabelCreator::serializeObjects( getSelection(), buffer );
XmlLabelCreator::serializeObjects( getSelection(), this, buffer );

auto *mimeData = new QMimeData;
mimeData->setData( MIME_TYPE, buffer );
Expand Down Expand Up @@ -1456,7 +1471,7 @@ namespace glabels
{
// Native objects
QByteArray buffer = mimeData->data( MIME_TYPE );
QList <ModelObject*> objects = XmlLabelParser::deserializeObjects( buffer );
QList <ModelObject*> objects = XmlLabelParser::deserializeObjects( buffer, this );

unselectAll();
foreach ( ModelObject* object, objects )
Expand Down
4 changes: 3 additions & 1 deletion model/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "merge/Merge.h"
#include "merge/Record.h"

#include <QDir>
#include <QList>
#include <QObject>
#include <QPainter>
Expand Down Expand Up @@ -91,7 +92,8 @@ namespace glabels
void setModified();
void clearModified();

QString dir() const;
QDir dir() const;
QString dirPath() const;
QString shortName();
const QString& fileName() const;
void setFileName( const QString &fileName );
Expand Down
4 changes: 2 additions & 2 deletions model/ModelImageObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ namespace glabels
{
// Look for image file relative to project file 1st then CWD 2nd
auto* model = dynamic_cast<Model*>( parent() );
QDir::setSearchPaths( "images", {model->dir(), QDir::currentPath()} );
QDir::setSearchPaths( "images", {model->dirPath(), QDir::currentPath()} );
QString filename = QString("images:") + mFilenameNode.text( record, variables );

auto* image = new QImage( filename );
Expand Down Expand Up @@ -537,7 +537,7 @@ namespace glabels
{
// Look for image file relative to project file 1st then CWD 2nd
auto* model = dynamic_cast<Model*>( parent() );
QDir::setSearchPaths( "images", {model->dir(), QDir::currentPath()} );
QDir::setSearchPaths( "images", {model->dirPath(), QDir::currentPath()} );
QString filename = QString("images:") + mFilenameNode.text( record, variables );

auto* image = new QImage( filename );
Expand Down
98 changes: 66 additions & 32 deletions model/XmlLabelCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "XmlTemplateCreator.h"
#include "XmlUtil.h"

#include "merge/Factory.h"
#include "merge/None.h"

#include <QByteArray>
Expand All @@ -49,38 +50,41 @@ namespace glabels
{

void
XmlLabelCreator::writeFile( const Model* label, const QString& fileName )
XmlLabelCreator::writeFile( Model* model, const QString& fileName )
{
QDomDocument doc;

createDoc( doc, label );
QByteArray buffer = doc.toByteArray( 2 );

QFile file( fileName );

if ( !file.open( QFile::WriteOnly | QFile::Text) )
{
qWarning() << "Error: Cannot write file " << fileName
<< ": " << file.errorString();
return;
}

model->setFileName( fileName );
model->clearModified();

QDomDocument doc;
createDoc( doc, model );

QByteArray buffer = doc.toByteArray( 2 );
file.write( buffer.data(), buffer.size() );
}


void
XmlLabelCreator::writeBuffer( const Model* label, QByteArray& buffer )
XmlLabelCreator::writeBuffer( const Model* model, QByteArray& buffer )
{
QDomDocument doc;

createDoc( doc, label );
createDoc( doc, model );
buffer = doc.toByteArray( 2 );
}


void
XmlLabelCreator::serializeObjects( const QList<ModelObject*>& objects,
QByteArray& buffer )
const Model* model,
QByteArray& buffer )
{
QDomDocument doc;

Expand All @@ -91,15 +95,15 @@ namespace glabels
doc.appendChild( root );
XmlUtil::setStringAttr( root, "version", "4.0" );

createDataNode( root, objects );
createObjectsNode( root, objects, false );
createDataNode( root, model, objects );
createObjectsNode( root, model, objects, false );

buffer = doc.toByteArray( 2 );
}


void
XmlLabelCreator::createDoc( QDomDocument& doc, const Model* label )
XmlLabelCreator::createDoc( QDomDocument& doc, const Model* model )
{
QDomNode xmlNode( doc.createProcessingInstruction( "xml", "version=\"1.0\"" ) );
doc.appendChild( xmlNode );
Expand All @@ -108,26 +112,29 @@ namespace glabels
doc.appendChild( root );
XmlUtil::setStringAttr( root, "version", "4.0" );

XmlTemplateCreator().createTemplateNode( root, label->tmplate() );
XmlTemplateCreator().createTemplateNode( root, model->tmplate() );

createObjectsNode( root, label->objectList(), label->rotate() );
createObjectsNode( root, model, model->objectList(), model->rotate() );

if ( label->merge() && !dynamic_cast<merge::None*>(label->merge()) )
if ( model->merge() && !dynamic_cast<merge::None*>(model->merge()) )
{
createMergeNode( root, label );
createMergeNode( root, model );
}

if ( label->variables()->size() != 0 )
if ( model->variables()->size() != 0 )
{
createVariablesNode( root, label );
createVariablesNode( root, model );
}

createDataNode( root, label->objectList() );
createDataNode( root, model, model->objectList() );
}


void
XmlLabelCreator::createObjectsNode( QDomElement &parent, const QList<ModelObject*>& objects, bool rotate )
XmlLabelCreator::createObjectsNode( QDomElement& parent,
const Model* model,
const QList<ModelObject*>& objects,
bool rotate )
{
QDomDocument doc = parent.ownerDocument();
QDomElement node = doc.createElement( "Objects" );
Expand All @@ -152,7 +159,7 @@ namespace glabels
}
else if ( auto* imageObject = dynamic_cast<ModelImageObject*>(object) )
{
createObjectImageNode( node, imageObject );
createObjectImageNode( node, model, imageObject );
}
else if ( auto* barcodeObject = dynamic_cast<ModelBarcodeObject*>(object) )
{
Expand Down Expand Up @@ -250,7 +257,9 @@ namespace glabels


void
XmlLabelCreator::createObjectImageNode( QDomElement &parent, const ModelImageObject* object )
XmlLabelCreator::createObjectImageNode( QDomElement& parent,
const Model* model,
const ModelImageObject* object )
{
QDomDocument doc = parent.ownerDocument();
QDomElement node = doc.createElement( "Object-image" );
Expand All @@ -269,7 +278,8 @@ namespace glabels
}
else
{
XmlUtil::setStringAttr( node, "src", object->filenameNode().data() );
QString fn = model->dir().relativeFilePath( object->filenameNode().data() );
XmlUtil::setStringAttr( node, "src", fn );
}

/* affine attrs */
Expand Down Expand Up @@ -461,25 +471,45 @@ namespace glabels


void
XmlLabelCreator::createMergeNode( QDomElement &parent, const Model* label )
XmlLabelCreator::createMergeNode( QDomElement &parent, const Model* model )
{
QDomDocument doc = parent.ownerDocument();
QDomElement node = doc.createElement( "Merge" );
parent.appendChild( node );

XmlUtil::setStringAttr( node, "type", label->merge()->id() );
XmlUtil::setStringAttr( node, "src", label->merge()->source() );
QString id = model->merge()->id();
QString src = model->merge()->source();

XmlUtil::setStringAttr( node, "type", id );

switch ( merge::Factory::idToType( id ) )
{
case merge::Factory::NONE:
case merge::Factory::FIXED:
break;

case merge::Factory::FILE:
{
QString fn = model->dir().relativeFilePath( src );
XmlUtil::setStringAttr( node, "src", fn );
}
break;

default:
qWarning() << "XmlLabelCreator::createMergeNode(): Should not be reached!";
break;
}
}


void
XmlLabelCreator::createVariablesNode( QDomElement &parent, const Model* label )
XmlLabelCreator::createVariablesNode( QDomElement &parent, const Model* model )
{
QDomDocument doc = parent.ownerDocument();
QDomElement node = doc.createElement( "Variables" );
parent.appendChild( node );

for ( const auto& v : *label->variables() )
for ( const auto& v : *model->variables() )
{
createVariableNode( node, v );
}
Expand Down Expand Up @@ -512,7 +542,9 @@ namespace glabels


void
XmlLabelCreator::createDataNode( QDomElement &parent, const QList<ModelObject*>& objects )
XmlLabelCreator::createDataNode( QDomElement& parent,
const Model* model,
const QList<ModelObject*>& objects )
{
QDomDocument doc = parent.ownerDocument();
QDomElement node = doc.createElement( "Data" );
Expand All @@ -522,12 +554,14 @@ namespace glabels

foreach ( QString name, data.imageNames() )
{
createPngFileNode( node, name, data.getImage( name ) );
QString fn = model->dir().relativeFilePath( name );
createPngFileNode( node, fn, data.getImage( name ) );
}

foreach ( QString name, data.svgNames() )
{
createSvgFileNode( node, name, data.getSvg( name ) );
QString fn = model->dir().relativeFilePath( name );
createSvgFileNode( node, fn, data.getSvg( name ) );
}
}

Expand Down
Loading

0 comments on commit 6a789c9

Please sign in to comment.