2 * kdenlivetitle_wrapper.cpp -- kdenlivetitle wrapper
3 * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
4 * Copyright (c) 2009 Jean-Baptiste Mardelle <jb@kdenlive.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "kdenlivetitle_wrapper.h"
23 #include <QtGui/QImage>
24 #include <QtGui/QPainter>
25 #include <QtCore/QDebug>
26 #include <QtGui/QApplication>
27 #include <QtCore/QMutex>
28 #include <QtGui/QGraphicsScene>
29 #include <QtGui/QGraphicsTextItem>
30 #include <QtGui/QTextCursor>
31 #include <QtGui/QStyleOptionGraphicsItem>
33 static QApplication *app = NULL;
38 #include <framework/mlt_producer.h>
39 #include <framework/mlt_cache.h>
41 static QMutex g_mutex;
42 void refresh_kdenlivetitle( mlt_producer producer, uint8_t* buffer, int width, int height , double position, int force_refresh )
44 drawKdenliveTitle( producer, buffer, width, height, position, force_refresh );
47 static void qscene_delete( void *data )
49 QGraphicsScene *scene = ( QGraphicsScene * )data;
56 class ImageItem: public QGraphicsRectItem
67 virtual void paint( QPainter *painter,
68 const QStyleOptionGraphicsItem * /*option*/,
71 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
72 painter->drawImage(QPoint(), m_img);
77 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position, int force_refresh )
80 // restore cached image if any
81 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
83 if ( force_refresh == 0 )
85 uint8_t * cache = ( uint8_t * )mlt_properties_get_data( producer_props, "image", NULL );
92 // restore QGraphicsScene
93 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
95 if ( force_refresh == 1 && scene )
98 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
110 app=new QApplication( argc,argv ); //, QApplication::Tty );
112 scene = new QGraphicsScene( 0, 0, width, height, app );
113 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ), width, height );
114 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
118 //must be extracted from kdenlive title
119 QImage img( width, height, QImage::Format_ARGB32 );
123 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
124 //| QPainter::SmoothPixmapTransform );
126 const QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
127 const QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
128 const QRectF source( 0, 0, width, height );
132 scene->render( &p1, source, source );
134 scene->render( &p1, start, source );
137 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
138 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
139 const QRectF r1( topleft, bottomRight );
140 scene->render( &p1, r1, source );
144 uint8_t *pointer=img.bits();
145 QRgb* src = ( QRgb* ) pointer;
146 int size = width * height * 4;
147 for ( int i = 0; i < width * height * 4; i += 4 )
149 *buffer++=qRed( *src );
150 *buffer++=qGreen( *src );
151 *buffer++=qBlue( *src );
152 *buffer++=qAlpha( *src );
158 // No animation, cache image for further requests
159 uint8_t *cached_image = (uint8_t *)mlt_pool_alloc( size );
160 cached_image = buffer;
161 mlt_properties_set_data( producer_props, "image", cached_image, width * height * 4, NULL, NULL );
163 // Clear scene, we don't need it anymore
164 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
169 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText, int width, int height )
172 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
174 QString data = QString::fromUtf8(templateXml);
175 QString replacementText = QString::fromUtf8(templateText);
176 doc.setContent(data);
177 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
178 QTransform transform;
179 if ( doc.documentElement().hasAttribute("width") ) {
180 int originalWidth = doc.documentElement().attribute("width").toInt();
181 int originalHeight = doc.documentElement().attribute("height").toInt();
182 if (originalWidth != width || originalHeight != height) {
183 #if QT_VERSION < 0x40500
184 transform = QTransform().scale( (double) width / originalWidth, (double) height / originalHeight );
186 transform = QTransform::fromScale ( (double) width / originalWidth, (double) height / originalHeight);
193 QDomNodeList items = titles.item( 0 ).childNodes();
194 for ( int i = 0; i < items.count(); i++ )
196 QGraphicsItem *gitem = NULL;
197 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
198 if ( zValue > -1000 )
200 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
202 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
203 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
205 QDomNode node = txtProperties.namedItem( "font-bold" );
206 if ( !node.isNull() )
208 // Old: Bold/Not bold.
209 font.setBold( node.nodeValue().toInt() );
213 // New: Font weight (QFont::)
214 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
217 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
218 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
219 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
220 // Older Kdenlive version did not store pixel size but point size
221 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
224 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
225 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
228 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
229 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
230 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
231 if ( !replacementText.isEmpty() )
233 text = text.replace( "%s", replacementText );
235 QGraphicsTextItem *txt = new QGraphicsTextItem( text );
238 txt->setDefaultTextColor( col );
239 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
241 txt->setTextWidth( txt->boundingRect().width() );
242 QTextCursor cur = txt->textCursor();
243 QTextBlockFormat format = cur.blockFormat();
244 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
245 cur.select( QTextCursor::Document );
246 cur.setBlockFormat( format );
247 txt->setTextCursor( cur );
248 cur.clearSelection();
249 txt->setTextCursor( cur );
252 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
254 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
256 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
258 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
263 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
265 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
266 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
267 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
268 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
269 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
272 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
274 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
276 ImageItem *rec = new ImageItem(img);
277 scene->addItem( rec );
278 rec->setData( Qt::UserRole, url );
281 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
283 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
284 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
285 //m_scene->addItem(rec);
286 //rec->setData(Qt::UserRole, url);
293 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
294 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
295 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
297 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
298 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
299 gitem->setZValue( zValue );
300 if ( transform != QTransform() ) gitem->setTransform( transform, true );
301 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
303 if ( items.item( i ).nodeName() == "background" )
305 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
306 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
307 QList<QGraphicsItem *> items = scene->items();
308 for ( int i = 0; i < items.size(); i++ )
310 if ( items.at( i )->zValue() == -1100 )
312 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
317 else if ( items.item( i ).nodeName() == "startviewport" )
319 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
320 if ( transform != QTransform() ) {
321 rect = rectTransform( rect, transform );
323 mlt_properties_set( producer_props, "_startrect", rect.toUtf8().data() );
325 else if ( items.item( i ).nodeName() == "endviewport" )
327 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
328 if ( transform != QTransform() ) {
329 rect = rectTransform( rect, transform );
331 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
335 if ( !strcmp( mlt_properties_get( producer_props, "_startrect" ), mlt_properties_get( producer_props, "_endrect" ) ) )
336 mlt_properties_set( producer_props, "_endrect", "" );
340 QString rectTransform( QString s, QTransform t )
342 QStringList l = s.split( ',' );
343 return QString::number(l.at(0).toDouble() * t.m11()) + ',' + QString::number(l.at(1).toDouble() * t.m22()) + ',' + QString::number(l.at(2).toDouble() * t.m11()) + ',' + QString::number(l.at(3).toDouble() * t.m22());
346 QString colorToString( const QColor& c )
348 QString ret = "%1,%2,%3,%4";
349 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
353 QString rectFToString( const QRectF& c )
355 QString ret = "%1,%2,%3,%4";
356 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
360 QRectF stringToRect( const QString & s )
363 QStringList l = s.split( ',' );
366 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
369 QColor stringToColor( const QString & s )
371 QStringList l = s.split( ',' );
374 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
377 QTransform stringToTransform( const QString& s )
379 QStringList l = s.split( ',' );
383 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
384 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
385 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()