2 * kdenlivetitle_wrapper.cpp -- kdenlivetitle wrapper
3 * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "kdenlivetitle_wrapper.h"
22 #include <QtGui/QImage>
23 #include <QtGui/QPainter>
24 #include <QtCore/QDebug>
25 #include <QtGui/QApplication>
26 #include <QtCore/QMutex>
27 #include <QtGui/QGraphicsScene>
28 #include <QtGui/QGraphicsTextItem>
29 #include <QtGui/QTextCursor>
31 static QApplication *app = NULL;
36 #include <framework/mlt_producer.h>
37 #include <framework/mlt_cache.h>
39 static QMutex g_mutex;
40 void refresh_kdenlivetitle( mlt_producer producer, uint8_t* buffer, int width, int height , double position, int force_refresh )
43 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
44 loadFromXml( producer, NULL, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
46 drawKdenliveTitle( producer, buffer, width, height, position );
49 static void qscene_delete( void *data )
51 QGraphicsScene *scene = ( QGraphicsScene * )data;
59 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position )
62 // restore QGraphicsScene
64 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
65 QGraphicsScene *scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
66 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
77 app=new QApplication( argc,argv );
79 scene = new QGraphicsScene(0, 0, (qreal) width, (qreal) height, app);
80 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
81 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qscene", scene, 0, ( mlt_destructor )qscene_delete );
86 //must be extracted from kdenlive title
87 QImage *img = new QImage( width,height,QImage::Format_ARGB32 );
91 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
92 //| QPainter::SmoothPixmapTransform );
94 QRectF m_start = stringToRect( QString( mlt_properties_get( producer_props, "startrect" ) ) );
95 QRectF m_end = stringToRect( QString( mlt_properties_get( producer_props, "endrect" ) ) );
97 if (m_start.isNull() && m_end.isNull()) {
101 QPointF topleft=m_start.topLeft()+( m_end.topLeft()-m_start.topLeft() )*position;
102 QPointF bottomRight=m_start.bottomRight()+( m_end.bottomRight()-m_start.bottomRight() )*position;
103 const QRectF r1( 0, 0, width, height );
104 const QRectF r2( topleft, bottomRight );
105 scene->render( &p1, r1, r2 );
108 uint8_t *pointer=img->bits();
109 QRgb* src = ( QRgb* ) pointer;
110 for ( int i = 0; i < width * height * 4; i += 4 )
112 *buffer++=qRed( *src );
113 *buffer++=qGreen( *src );
114 *buffer++=qBlue( *src );
115 *buffer++=qAlpha( *src );
121 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
124 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
125 scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
126 if (scene == NULL) return;
129 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
131 QString data(templateXml);
132 QString replacementText(templateText);
133 doc.setContent(data);
134 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
139 QDomNodeList items = titles.item( 0 ).childNodes();
140 for ( int i = 0; i < items.count(); i++ )
142 QGraphicsItem *gitem = NULL;
143 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
144 if ( zValue > -1000 )
146 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
148 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
149 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
151 QDomNode node = txtProperties.namedItem( "font-bold" );
152 if ( !node.isNull() )
154 // Old: Bold/Not bold.
155 font.setBold( node.nodeValue().toInt() );
159 // New: Font weight (QFont::)
160 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
163 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
164 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
165 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
166 // Older Kdenlive version did not store pixel size but point size
167 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
170 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
171 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
174 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
175 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
176 QGraphicsTextItem *txt;
177 if ( !replacementText.isEmpty() )
179 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
180 text = text.replace( "%s", replacementText );
181 txt = scene->addText( text, font );
183 else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
184 txt->setDefaultTextColor( col );
185 txt->setTextInteractionFlags( Qt::NoTextInteraction );
186 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
188 txt->setTextWidth( txt->boundingRect().width() );
189 QTextCursor cur = txt->textCursor();
190 QTextBlockFormat format = cur.blockFormat();
191 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
192 cur.select( QTextCursor::Document );
193 cur.setBlockFormat( format );
194 txt->setTextCursor( cur );
195 cur.clearSelection();
196 txt->setTextCursor( cur );
199 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
201 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
203 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
205 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
210 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
212 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
213 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
214 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
215 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
216 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
219 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
221 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
223 QGraphicsPixmapItem *rec = scene->addPixmap( pix );
224 rec->setData( Qt::UserRole, url );
227 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
229 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
230 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
231 //m_scene->addItem(rec);
232 //rec->setData(Qt::UserRole, url);
239 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
240 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
242 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
243 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
244 if ( zValue > maxZValue ) maxZValue = zValue;
245 gitem->setZValue( zValue );
246 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
248 if ( items.item( i ).nodeName() == "background" )
250 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
251 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
252 QList<QGraphicsItem *> items = scene->items();
253 for ( int i = 0; i < items.size(); i++ )
255 if ( items.at( i )->zValue() == -1100 )
257 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
262 else if ( items.item( i ).nodeName() == "startviewport" )
264 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
266 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
268 else if ( items.item( i ).nodeName() == "endviewport" )
270 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
271 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
278 QString colorToString( const QColor& c )
280 QString ret = "%1,%2,%3,%4";
281 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
285 QString rectFToString( const QRectF& c )
287 QString ret = "%1,%2,%3,%4";
288 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
292 QRectF stringToRect( const QString & s )
295 QStringList l = s.split( ',' );
298 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
301 QColor stringToColor( const QString & s )
303 QStringList l = s.split( ',' );
306 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
309 QTransform stringToTransform( const QString& s )
311 QStringList l = s.split( ',' );
315 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
316 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
317 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()