Serialize/deserialize for copy/paste.
This commit is contained in:
@@ -32,9 +32,9 @@ namespace glabels
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DataCache::DataCache( const LabelModel* model )
|
DataCache::DataCache( const QList<LabelModelObject*>& objects )
|
||||||
{
|
{
|
||||||
foreach( LabelModelObject* object, model->objectList() )
|
foreach( LabelModelObject* object, objects )
|
||||||
{
|
{
|
||||||
if ( LabelModelImageObject* imageObject = dynamic_cast<LabelModelImageObject*>(object) )
|
if ( LabelModelImageObject* imageObject = dynamic_cast<LabelModelImageObject*>(object) )
|
||||||
{
|
{
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ namespace glabels
|
|||||||
public:
|
public:
|
||||||
DataCache();
|
DataCache();
|
||||||
|
|
||||||
DataCache( const LabelModel* model );
|
DataCache( const QList<LabelModelObject*>& objects );
|
||||||
|
|
||||||
bool hasImage( const QString& name ) const;
|
bool hasImage( const QString& name ) const;
|
||||||
QImage getImage( const QString& name ) const;
|
QImage getImage( const QString& name ) const;
|
||||||
|
|||||||
+15
-20
@@ -88,8 +88,10 @@ namespace glabels
|
|||||||
|
|
||||||
QDomElement root = doc.createElement( "Glabels-objects" );
|
QDomElement root = doc.createElement( "Glabels-objects" );
|
||||||
doc.appendChild( root );
|
doc.appendChild( root );
|
||||||
|
XmlUtil::setStringAttr( root, "version", "4.0" );
|
||||||
|
|
||||||
addObjectsToNode( root, objects );
|
createDataNode( root, objects );
|
||||||
|
createObjectsNode( root, objects, false );
|
||||||
|
|
||||||
buffer = doc.toByteArray( 2 );
|
buffer = doc.toByteArray( 2 );
|
||||||
}
|
}
|
||||||
@@ -107,60 +109,53 @@ namespace glabels
|
|||||||
|
|
||||||
XmlTemplateCreator().createTemplateNode( root, label->tmplate() );
|
XmlTemplateCreator().createTemplateNode( root, label->tmplate() );
|
||||||
|
|
||||||
createObjectsNode( root, label );
|
createObjectsNode( root, label->objectList(), label->rotate() );
|
||||||
|
|
||||||
if ( label->merge() && !dynamic_cast<merge::None*>(label->merge()) )
|
if ( label->merge() && !dynamic_cast<merge::None*>(label->merge()) )
|
||||||
{
|
{
|
||||||
createMergeNode( root, label );
|
createMergeNode( root, label );
|
||||||
}
|
}
|
||||||
|
|
||||||
createDataNode( root, label );
|
createDataNode( root, label->objectList() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
XmlLabelCreator::createObjectsNode( QDomElement &parent, const LabelModel* label )
|
XmlLabelCreator::createObjectsNode( QDomElement &parent, const QList<LabelModelObject*>& objects, bool rotate )
|
||||||
{
|
{
|
||||||
QDomDocument doc = parent.ownerDocument();
|
QDomDocument doc = parent.ownerDocument();
|
||||||
QDomElement node = doc.createElement( "Objects" );
|
QDomElement node = doc.createElement( "Objects" );
|
||||||
parent.appendChild( node );
|
parent.appendChild( node );
|
||||||
|
|
||||||
XmlUtil::setStringAttr( node, "id", "0" );
|
XmlUtil::setStringAttr( node, "id", "0" );
|
||||||
XmlUtil::setBoolAttr( node, "rotate", label->rotate() );
|
XmlUtil::setBoolAttr( node, "rotate", rotate );
|
||||||
|
|
||||||
addObjectsToNode( node, label->objectList() );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
XmlLabelCreator::addObjectsToNode( QDomElement &parent, const QList<LabelModelObject*>& objects )
|
|
||||||
{
|
|
||||||
foreach ( LabelModelObject* object, objects )
|
foreach ( LabelModelObject* object, objects )
|
||||||
{
|
{
|
||||||
if ( LabelModelBoxObject* boxObject = dynamic_cast<LabelModelBoxObject*>(object) )
|
if ( LabelModelBoxObject* boxObject = dynamic_cast<LabelModelBoxObject*>(object) )
|
||||||
{
|
{
|
||||||
createObjectBoxNode( parent, boxObject );
|
createObjectBoxNode( node, boxObject );
|
||||||
}
|
}
|
||||||
else if ( LabelModelEllipseObject* ellipseObject = dynamic_cast<LabelModelEllipseObject*>(object) )
|
else if ( LabelModelEllipseObject* ellipseObject = dynamic_cast<LabelModelEllipseObject*>(object) )
|
||||||
{
|
{
|
||||||
createObjectEllipseNode( parent, ellipseObject );
|
createObjectEllipseNode( node, ellipseObject );
|
||||||
}
|
}
|
||||||
else if ( LabelModelLineObject* lineObject = dynamic_cast<LabelModelLineObject*>(object) )
|
else if ( LabelModelLineObject* lineObject = dynamic_cast<LabelModelLineObject*>(object) )
|
||||||
{
|
{
|
||||||
createObjectLineNode( parent, lineObject );
|
createObjectLineNode( node, lineObject );
|
||||||
}
|
}
|
||||||
else if ( LabelModelImageObject* imageObject = dynamic_cast<LabelModelImageObject*>(object) )
|
else if ( LabelModelImageObject* imageObject = dynamic_cast<LabelModelImageObject*>(object) )
|
||||||
{
|
{
|
||||||
createObjectImageNode( parent, imageObject );
|
createObjectImageNode( node, imageObject );
|
||||||
}
|
}
|
||||||
else if ( LabelModelTextObject* textObject = dynamic_cast<LabelModelTextObject*>(object) )
|
else if ( LabelModelTextObject* textObject = dynamic_cast<LabelModelTextObject*>(object) )
|
||||||
{
|
{
|
||||||
createObjectTextNode( parent, textObject );
|
createObjectTextNode( node, textObject );
|
||||||
}
|
}
|
||||||
// TODO: other object types
|
// TODO: other object types
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Q_ASSERT_X( false, "XmlLabelCreator::addObjectsToNode", "Invalid object type." );
|
Q_ASSERT_X( false, "XmlLabelCreator::createObjectsNode", "Invalid object type." );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -444,13 +439,13 @@ namespace glabels
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
XmlLabelCreator::createDataNode( QDomElement &parent, const LabelModel* label )
|
XmlLabelCreator::createDataNode( QDomElement &parent, const QList<LabelModelObject*>& objects )
|
||||||
{
|
{
|
||||||
QDomDocument doc = parent.ownerDocument();
|
QDomDocument doc = parent.ownerDocument();
|
||||||
QDomElement node = doc.createElement( "Data" );
|
QDomElement node = doc.createElement( "Data" );
|
||||||
parent.appendChild( node );
|
parent.appendChild( node );
|
||||||
|
|
||||||
DataCache data( label );
|
DataCache data( objects );
|
||||||
|
|
||||||
foreach ( QString name, data.imageNames() )
|
foreach ( QString name, data.imageNames() )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,8 +55,7 @@ namespace glabels
|
|||||||
private:
|
private:
|
||||||
static void createDoc( QDomDocument& doc, const LabelModel* label );
|
static void createDoc( QDomDocument& doc, const LabelModel* label );
|
||||||
static void createRootNode( const LabelModel* label );
|
static void createRootNode( const LabelModel* label );
|
||||||
static void createObjectsNode( QDomElement &parent, const LabelModel* label );
|
static void createObjectsNode( QDomElement &parent, const QList<LabelModelObject*>& objects, bool rotate );
|
||||||
static void addObjectsToNode( QDomElement &parent, const QList<LabelModelObject*>& objects );
|
|
||||||
static void createObjectBoxNode( QDomElement &parent, const LabelModelBoxObject* object );
|
static void createObjectBoxNode( QDomElement &parent, const LabelModelBoxObject* object );
|
||||||
static void createObjectEllipseNode( QDomElement &parent, const LabelModelEllipseObject* object );
|
static void createObjectEllipseNode( QDomElement &parent, const LabelModelEllipseObject* object );
|
||||||
static void createObjectLineNode( QDomElement &parent, const LabelModelLineObject* object );
|
static void createObjectLineNode( QDomElement &parent, const LabelModelLineObject* object );
|
||||||
@@ -67,7 +66,7 @@ namespace glabels
|
|||||||
static void createAffineAttrs( QDomElement &node, const LabelModelObject* object );
|
static void createAffineAttrs( QDomElement &node, const LabelModelObject* object );
|
||||||
static void createShadowAttrs( QDomElement &node, const LabelModelObject* object );
|
static void createShadowAttrs( QDomElement &node, const LabelModelObject* object );
|
||||||
static void createMergeNode( QDomElement &parent, const LabelModel* label );
|
static void createMergeNode( QDomElement &parent, const LabelModel* label );
|
||||||
static void createDataNode( QDomElement &parent, const LabelModel* label );
|
static void createDataNode( QDomElement &parent, const QList<LabelModelObject*>& objects );
|
||||||
static void createPngFileNode( QDomElement &parent, const QString& name, const QImage& image );
|
static void createPngFileNode( QDomElement &parent, const QString& name, const QImage& image );
|
||||||
static void createSvgFileNode( QDomElement &parent, const LabelModel* label, const QString& name );
|
static void createSvgFileNode( QDomElement &parent, const LabelModel* label, const QString& name );
|
||||||
|
|
||||||
|
|||||||
+25
-15
@@ -152,7 +152,25 @@ namespace glabels
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
return parseObjects( root, DataCache() );
|
/* Pass 1, extract data nodes to pre-load cache. */
|
||||||
|
DataCache data;
|
||||||
|
for ( QDomNode child = root.firstChild(); !child.isNull(); child = child.nextSibling() )
|
||||||
|
{
|
||||||
|
if ( child.toElement().tagName() == "Data" )
|
||||||
|
{
|
||||||
|
parseDataNode( child.toElement(), data );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Pass 2, now extract objects. */
|
||||||
|
for ( QDomNode child = root.firstChild(); !child.isNull(); child = child.nextSibling() )
|
||||||
|
{
|
||||||
|
if ( child.toElement().tagName() == "Objects" )
|
||||||
|
{
|
||||||
|
list = parseObjectsNode( child.toElement(), data );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -244,7 +262,11 @@ namespace glabels
|
|||||||
}
|
}
|
||||||
else if ( tagName == "Objects" )
|
else if ( tagName == "Objects" )
|
||||||
{
|
{
|
||||||
parseObjectsNode( child.toElement(), data, label );
|
QList<LabelModelObject*> list = parseObjectsNode( child.toElement(), data );
|
||||||
|
foreach ( LabelModelObject* object, list )
|
||||||
|
{
|
||||||
|
label->addObject( object );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if ( tagName == "Merge" )
|
else if ( tagName == "Merge" )
|
||||||
{
|
{
|
||||||
@@ -266,7 +288,7 @@ namespace glabels
|
|||||||
|
|
||||||
|
|
||||||
QList<LabelModelObject*>
|
QList<LabelModelObject*>
|
||||||
XmlLabelParser::parseObjects( const QDomElement &node, const DataCache& data )
|
XmlLabelParser::parseObjectsNode( const QDomElement &node, const DataCache& data )
|
||||||
{
|
{
|
||||||
QList<LabelModelObject*> list;
|
QList<LabelModelObject*> list;
|
||||||
|
|
||||||
@@ -310,18 +332,6 @@ namespace glabels
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
XmlLabelParser::parseObjectsNode( const QDomElement &node, const DataCache& data, LabelModel* label )
|
|
||||||
{
|
|
||||||
QList<LabelModelObject*> list = parseObjects( node, data );
|
|
||||||
|
|
||||||
foreach ( LabelModelObject* object, list )
|
|
||||||
{
|
|
||||||
label->addObject( object );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LabelModelBoxObject*
|
LabelModelBoxObject*
|
||||||
XmlLabelParser::parseObjectBoxNode( const QDomElement &node )
|
XmlLabelParser::parseObjectBoxNode( const QDomElement &node )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -56,8 +56,7 @@ namespace glabels
|
|||||||
private:
|
private:
|
||||||
static void gunzip( const QByteArray& gzippedData, QByteArray& data );
|
static void gunzip( const QByteArray& gzippedData, QByteArray& data );
|
||||||
static LabelModel* parseRootNode( const QDomElement &node );
|
static LabelModel* parseRootNode( const QDomElement &node );
|
||||||
static QList<LabelModelObject*> parseObjects( const QDomElement &node, const DataCache& data );
|
static QList<LabelModelObject*> parseObjectsNode( const QDomElement &node, const DataCache& data );
|
||||||
static void parseObjectsNode( const QDomElement &node, const DataCache& data, LabelModel* label );
|
|
||||||
static LabelModelBoxObject* parseObjectBoxNode( const QDomElement &node );
|
static LabelModelBoxObject* parseObjectBoxNode( const QDomElement &node );
|
||||||
static LabelModelEllipseObject* parseObjectEllipseNode( const QDomElement &node );
|
static LabelModelEllipseObject* parseObjectEllipseNode( const QDomElement &node );
|
||||||
static LabelModelLineObject* parseObjectLineNode( const QDomElement &node );
|
static LabelModelLineObject* parseObjectLineNode( const QDomElement &node );
|
||||||
|
|||||||
Reference in New Issue
Block a user