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 <QtSvg/QGraphicsSvgItem>
31 #include <QtGui/QTextCursor>
32 #include <QtGui/QStyleOptionGraphicsItem>
34 #include <QtCore/QString>
36 #include <QtXml/QDomElement>
37 #include <QtCore/QRectF>
38 #include <QtGui/QColor>
39 #include <QtGui/QWidget>
40 #include <framework/mlt_log.h>
42 static QApplication *app = NULL;
44 class ImageItem: public QGraphicsRectItem
55 virtual void paint( QPainter *painter,
56 const QStyleOptionGraphicsItem * /*option*/,
59 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
60 painter->drawImage(QPoint(), m_img);
65 QString rectTransform( QString s, QTransform t )
67 QStringList l = s.split( ',' );
68 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());
71 QString colorToString( const QColor& c )
73 QString ret = "%1,%2,%3,%4";
74 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
78 QString rectFToString( const QRectF& c )
80 QString ret = "%1,%2,%3,%4";
81 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
85 QRectF stringToRect( const QString & s )
88 QStringList l = s.split( ',' );
91 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
94 QColor stringToColor( const QString & s )
96 QStringList l = s.split( ',' );
99 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
102 QTransform stringToTransform( const QString& s )
104 QStringList l = s.split( ',' );
108 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
109 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
110 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
114 static void qscene_delete( void *data )
116 QGraphicsScene *scene = ( QGraphicsScene * )data;
117 if (scene) delete scene;
122 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
125 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
127 QString data = QString::fromUtf8(templateXml);
128 QString replacementText = QString::fromUtf8(templateText);
129 doc.setContent(data);
130 QDomElement title = doc.documentElement();
132 // Check for invalid title
133 if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
137 if ( title.hasAttribute("width") ) {
138 originalWidth = title.attribute("width").toInt();
139 originalHeight = title.attribute("height").toInt();
140 scene->setSceneRect(0, 0, originalWidth, originalHeight);
143 originalWidth = scene->sceneRect().width();
144 originalHeight = scene->sceneRect().height();
146 mlt_properties_set_int( producer_props, "_original_width", originalWidth );
147 mlt_properties_set_int( producer_props, "_original_height", originalHeight );
149 QDomNodeList items = title.elementsByTagName("item");
150 for ( int i = 0; i < items.count(); i++ )
152 QGraphicsItem *gitem = NULL;
153 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
154 if ( zValue > -1000 )
156 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
158 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
159 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
160 QDomNode node = txtProperties.namedItem( "font-bold" );
161 if ( !node.isNull() )
163 // Old: Bold/Not bold.
164 font.setBold( node.nodeValue().toInt() );
168 // New: Font weight (QFont::)
169 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
171 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
172 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
173 // Older Kdenlive version did not store pixel size but point size
174 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
177 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
178 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
181 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
182 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
183 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
184 if ( !replacementText.isEmpty() )
186 text = text.replace( "%s", replacementText );
188 QGraphicsTextItem *txt = scene->addText(text, font);
189 txt->setDefaultTextColor( col );
190 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
192 txt->setTextWidth( txt->boundingRect().width() );
193 QTextCursor cur = txt->textCursor();
194 QTextBlockFormat format = cur.blockFormat();
195 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
196 cur.select( QTextCursor::Document );
197 cur.setBlockFormat( format );
198 txt->setTextCursor( cur );
199 cur.clearSelection();
200 txt->setTextCursor( cur );
202 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
204 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
206 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
208 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
212 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
214 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
215 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
216 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
217 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
218 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
221 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
223 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
225 ImageItem *rec = new ImageItem(img);
226 scene->addItem( rec );
229 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
231 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
232 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
240 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
241 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
243 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
244 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
245 gitem->setZValue( zValue );
249 QDomNode n = title.firstChildElement("background");
251 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
252 if (color.alpha() > 0) {
253 QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
254 rec->setZValue(-1100);
260 n = title.firstChildElement( "startviewport" );
261 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
262 if (!n.isNull() && !n.toElement().hasAttribute("x"))
264 startRect = n.attributes().namedItem( "rect" ).nodeValue();
266 n = title.firstChildElement( "endviewport" );
267 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
268 if (!n.isNull() && !n.toElement().hasAttribute("x"))
270 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
271 if (startRect != rect)
272 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
274 if (!startRect.isEmpty()) {
275 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
281 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
283 // Obtain the producer
284 mlt_producer producer = &self->parent;
285 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
287 // Obtain properties of frame
288 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
290 pthread_mutex_lock( &self->mutex );
292 // Check if user wants us to reload the image
293 if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
295 //mlt_cache_item_close( self->image_cache );
296 self->current_image = NULL;
297 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
298 mlt_properties_set_int( producer_props, "force_reload", 0 );
301 if (self->current_image == NULL) {
302 // restore QGraphicsScene
303 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
305 if ( force_refresh == 1 && scene )
308 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
315 argv[0] = (char*) "xxx";
317 // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER
318 // the QApplication is created, otherwise their will be NULL
325 if ( getenv("DISPLAY") == 0 )
327 mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
328 pthread_mutex_unlock( &self->mutex );
333 app = new QApplication( argc, argv );
335 scene = new QGraphicsScene();
336 scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
337 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
338 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
341 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
342 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
344 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
345 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
346 const QRectF source( 0, 0, width, height );
347 if (start.isNull()) {
348 start = QRectF( 0, 0, originalWidth, originalHeight );
351 //must be extracted from kdenlive title
352 QImage img( width, height, QImage::Format_ARGB32 );
356 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
357 //| QPainter::SmoothPixmapTransform );
360 scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
364 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
365 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
366 const QRectF r1( topleft, bottomRight );
367 scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
371 int size = width * height * 4;
372 uint8_t *pointer=img.bits();
373 QRgb* src = ( QRgb* ) pointer;
374 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
375 uint8_t *dst = self->current_image;
377 for ( int i = 0; i < width * height * 4; i += 4 )
380 *dst++=qGreen( *src );
381 *dst++=qBlue( *src );
382 *dst++=qAlpha( *src );
386 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
387 self->current_width = width;
388 self->current_height = height;
391 pthread_mutex_unlock( &self->mutex );
392 mlt_properties_set_int( properties, "width", self->current_width );
393 mlt_properties_set_int( properties, "height", self->current_height );