1 /*
2  * QGuidoItemContainerFactory.cpp
3  *
4  * Created by Christophe Daudin on 12/05/09.
5  * Copyright 2009 Grame. All rights reserved.
6  *
7  * GNU Lesser General Public License Usage
8  * Alternatively, this file may be used under the terms of the GNU Lesser
9  * General Public License version 2.1 as published by the Free Software
10  * Foundation and appearing in the file LICENSE.LGPL included in the
11  * packaging of this file.  Please review the following information to
12  * ensure the GNU Lesser General Public License version 2.1 requirements
13  * will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
14  *
15  *
16  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19 #include "QGuidoItemContainerFactory.h"
20 
21 #include "QGuidoItemContainer.h"
22 #include "QSimpleItemContainer.h"
23 #include <assert.h>
24 
25 #include <QImageReader>
26 
27 #define DOM_GMN_CONTAINER_ITEM_DISPLAY_TYPE	"displayType"
28 
29 //------------------------------------------------------------------------------------------
buildGuidoItemContainer(QGraphicsItem * parent)30 QGuidoItemContainer * QGuidoItemContainerFactory::buildGuidoItemContainer( QGraphicsItem * parent )
31 {
32 	return new QGuidoItemContainer(parent);
33 }
34 
35 //------------------------------------------------------------------------------------------
buildGuidoItemContainer(const QGuidoItemContainer * other,QGraphicsItem * parent)36 QGuidoItemContainer * QGuidoItemContainerFactory::buildGuidoItemContainer( const QGuidoItemContainer * other , QGraphicsItem * parent )
37 {
38 	return new QGuidoItemContainer(other,parent);
39 }
40 
41 //------------------------------------------------------------------------------------------
buildSimpleTextItemContainer(QGraphicsItem * parent)42 QLanguageItem * QGuidoItemContainerFactory::buildSimpleTextItemContainer( QGraphicsItem * parent )
43 {
44 	QGraphicsSimpleTextItem * item = new QGraphicsSimpleTextItem();
45 	QGraphicsSimpleTextItemAdapter * adapter = new QGraphicsSimpleTextItemAdapter(item);
46 	return new QSimpleItemContainer( item , adapter , parent );
47 }
48 
49 //------------------------------------------------------------------------------------------
buildLanguageItem(QGraphicsItem * parent)50 QLanguageItem * QGuidoItemContainerFactory::buildLanguageItem( QGraphicsItem * parent )
51 {
52 	return buildGuidoItemContainer( parent );
53 }
54 
55 //------------------------------------------------------------------------------------------
buildLanguageItem(const QLanguageItem * other,QGraphicsItem * parent)56 QLanguageItem * QGuidoItemContainerFactory::buildLanguageItem( const QLanguageItem * other , QGraphicsItem * parent )
57 {
58 	if( dynamic_cast<const QGuidoItemContainer*> (other) )
59 		return buildGuidoItemContainer( (QGuidoItemContainer*)other , parent );
60 	else if ( dynamic_cast<const QSimpleItemContainer*> (other) )
61 	{
62 		const QSimpleItemContainer* simpleItemContainer = (QSimpleItemContainer*)other;
63 		QGraphicsItem * item = 0;
64 		QItemAdapter * adapter = 0;
65 		if ( dynamic_cast<const QGraphicsPixmapItemAdapter*> (simpleItemContainer->adapter()) )
66 		{
67 			return QPixmapItemContainer::buildPixmapItemContainer( simpleItemContainer , parent );
68 		}
69 		else if ( dynamic_cast<const QGraphicsSimpleTextItemAdapter*> (simpleItemContainer->adapter()) )
70 		{
71 			item = new QGraphicsSimpleTextItem();
72 			adapter = new QGraphicsSimpleTextItemAdapter((QGraphicsSimpleTextItem*)item);
73 		}
74 		if ( item && adapter )
75 			return new QSimpleItemContainer( item , adapter , simpleItemContainer , parent );
76 	}
77 
78 	assert(0);
79 	return 0;
80 }
81 
82 //------------------------------------------------------------------------------------------
buildLanguageItem(const QMimeData * mimeData,QGraphicsItem * parent)83 QLanguageItem * QGuidoItemContainerFactory::buildLanguageItem( const QMimeData * mimeData , QGraphicsItem * parent )
84 {
85 	if ( QGuidoItemContainer::recognizes(mimeData) )
86 	{
87 		return new QGuidoItemContainer(mimeData, parent );
88 	}
89 	else
90 	{
91 		QGraphicsItem * item = 0;
92 		QItemAdapter * adapter = 0;
93 		if ( QGraphicsPixmapItemAdapter::recognizes(mimeData) )
94 		{
95 			return QPixmapItemContainer::buildPixmapItemContainer( mimeData , parent );
96 		}
97 		else if ( QGraphicsSimpleTextItemAdapter::recognizes(mimeData) )
98 		{
99 			item = new QGraphicsSimpleTextItem();
100 			adapter = new QGraphicsSimpleTextItemAdapter((QGraphicsSimpleTextItem*)item);
101 		}
102 		if ( item && adapter )
103 			return new QSimpleItemContainer( item , adapter , mimeData , parent );
104 		assert(0);
105 		return 0;
106 	}
107 }
108 
109 //------------------------------------------------------------------------------------------
buildLanguageItem(const QDomElement * domElement,QGraphicsItem * parent)110 QLanguageItem * QGuidoItemContainerFactory::buildLanguageItem( const QDomElement * domElement , QGraphicsItem * parent )
111 {
112 	if ( QGuidoItemContainer::recognizes( domElement ) )
113 	{
114 		return new QGuidoItemContainer(domElement, parent );
115 	}
116 	else
117 	{
118 		QGraphicsItem * item = 0;
119 		QItemAdapter * adapter = 0;
120 		if ( QGraphicsPixmapItemAdapter::recognizes(domElement) )
121 		{
122 			return QPixmapItemContainer::buildPixmapItemContainer( domElement , parent );
123 		}
124 		else if ( QGraphicsSimpleTextItemAdapter::recognizes(domElement) )
125 		{
126 			item = new QGraphicsSimpleTextItem();
127 			adapter = new QGraphicsSimpleTextItemAdapter((QGraphicsSimpleTextItem*)item);
128 		}
129 		if ( item && adapter )
130 			return new QSimpleItemContainer( item , adapter , domElement , parent );
131 
132 		assert(0);
133 		return 0;
134 	}
135 }
136 
137 //------------------------------------------------------------------------------------------
buildLanguageItemFromFile(const QString & fileName,QGraphicsItem * parent)138 QLanguageItem * QGuidoItemContainerFactory::buildLanguageItemFromFile( const QString& fileName , QGraphicsItem * parent )
139 {
140 	QLanguageItem * languageItem;
141 	// 1. Try to build a QGraphicsPixmapItem container
142 	if ( QImageReader( fileName ).canRead() )
143 	{
144 		languageItem = QPixmapItemContainer::buildPixmapItemContainer( parent );
145 		languageItem->loadFile( fileName );
146 		languageItem->unlinkFile();	// QPixmapItemContainer are never file-linked.
147 		return languageItem;
148 	}
149 	else
150 	{
151 		// 2.	The file is not a valid image file
152 		//		=> Try to build a QGuidoItemContainer
153 		languageItem = buildGuidoItemContainer( parent );
154 		languageItem->loadFile( fileName );
155 		if ( languageItem->isValid() )
156 			return languageItem;
157 		else
158 		{
159 			delete languageItem;
160 			// 3.	The file is not a valid Guido file
161 			//		=> add  the contents of the file as a QGraphicsSimpleTextItem
162 			QGraphicsSimpleTextItem * i = new QGraphicsSimpleTextItem();
163 			languageItem = new QSimpleItemContainer( i , new QGraphicsSimpleTextItemAdapter(i) , parent );
164 			languageItem->loadFile( fileName );
165 			return languageItem;
166 		}
167 	}
168 }
169