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 #if QT_VERSION >= 0x040600
43 #include <QtGui/QGraphicsEffect>
44 #include <QtGui/QGraphicsBlurEffect>
45 #include <QtGui/QGraphicsDropShadowEffect>
48 static QApplication *app = NULL;
50 class ImageItem: public QGraphicsItem
62 virtual QRectF boundingRect() const
64 return QRectF(0, 0, m_img.width(), m_img.height());
67 virtual void paint( QPainter *painter,
68 const QStyleOptionGraphicsItem * /*option*/,
71 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
72 painter->drawImage(QPoint(), m_img);
77 QString rectTransform( QString s, QTransform t )
79 QStringList l = s.split( ',' );
80 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());
83 QString colorToString( const QColor& c )
85 QString ret = "%1,%2,%3,%4";
86 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
90 QString rectFToString( const QRectF& c )
92 QString ret = "%1,%2,%3,%4";
93 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
97 QRectF stringToRect( const QString & s )
100 QStringList l = s.split( ',' );
103 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
106 QColor stringToColor( const QString & s )
108 QStringList l = s.split( ',' );
111 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
114 QTransform stringToTransform( const QString& s )
116 QStringList l = s.split( ',' );
120 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
121 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
122 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
126 static void qscene_delete( void *data )
128 QGraphicsScene *scene = ( QGraphicsScene * )data;
129 if (scene) delete scene;
134 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
137 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
139 QString data = QString::fromUtf8(templateXml);
140 QString replacementText = QString::fromUtf8(templateText);
141 doc.setContent(data);
142 QDomElement title = doc.documentElement();
144 // Check for invalid title
145 if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
149 if ( title.hasAttribute("width") ) {
150 originalWidth = title.attribute("width").toInt();
151 originalHeight = title.attribute("height").toInt();
152 scene->setSceneRect(0, 0, originalWidth, originalHeight);
155 originalWidth = scene->sceneRect().width();
156 originalHeight = scene->sceneRect().height();
158 if ( title.hasAttribute( "out" ) ) {
159 mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
162 mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
165 mlt_properties_set_int( producer_props, "_original_width", originalWidth );
166 mlt_properties_set_int( producer_props, "_original_height", originalHeight );
168 QDomNodeList items = title.elementsByTagName("item");
169 for ( int i = 0; i < items.count(); i++ )
171 QGraphicsItem *gitem = NULL;
172 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
173 if ( zValue > -1000 )
175 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
177 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
178 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
179 QDomNode node = txtProperties.namedItem( "font-bold" );
180 if ( !node.isNull() )
182 // Old: Bold/Not bold.
183 font.setBold( node.nodeValue().toInt() );
187 // New: Font weight (QFont::)
188 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
190 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
191 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
192 // Older Kdenlive version did not store pixel size but point size
193 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
196 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
197 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
200 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
201 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
202 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
203 if ( !replacementText.isEmpty() )
205 text = text.replace( "%s", replacementText );
207 QGraphicsTextItem *txt = scene->addText(text, font);
208 txt->setDefaultTextColor( col );
209 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
211 txt->setTextWidth( txt->boundingRect().width() );
212 QTextCursor cur = txt->textCursor();
213 QTextBlockFormat format = cur.blockFormat();
214 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
215 cur.select( QTextCursor::Document );
216 cur.setBlockFormat( format );
217 txt->setTextCursor( cur );
218 cur.clearSelection();
219 txt->setTextCursor( cur );
221 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
223 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
225 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
227 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
231 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
233 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
234 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
235 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
236 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
237 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
240 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
242 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
244 ImageItem *rec = new ImageItem(img);
245 scene->addItem( rec );
248 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
250 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
251 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
259 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
260 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
262 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
263 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
264 gitem->setZValue( zValue );
266 #if QT_VERSION >= 0x040600
268 QDomNode eff = items.item(i).namedItem("effect");
270 QDomElement e = eff.toElement();
271 if (e.attribute("type") == "blur") {
272 QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
273 blur->setBlurRadius(e.attribute("blurradius").toInt());
274 gitem->setGraphicsEffect(blur);
276 else if (e.attribute("type") == "shadow") {
277 QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
278 shadow->setBlurRadius(e.attribute("blurradius").toInt());
279 shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
280 gitem->setGraphicsEffect(shadow);
287 QDomNode n = title.firstChildElement("background");
289 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
290 if (color.alpha() > 0) {
291 QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
292 rec->setZValue(-1100);
298 n = title.firstChildElement( "startviewport" );
299 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
300 if (!n.isNull() && !n.toElement().hasAttribute("x"))
302 startRect = n.attributes().namedItem( "rect" ).nodeValue();
304 n = title.firstChildElement( "endviewport" );
305 // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
306 if (!n.isNull() && !n.toElement().hasAttribute("x"))
308 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
309 if (startRect != rect)
310 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
312 if (!startRect.isEmpty()) {
313 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
319 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
321 // Obtain the producer
322 mlt_producer producer = &self->parent;
323 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
325 // Obtain properties of frame
326 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
328 pthread_mutex_lock( &self->mutex );
330 // Check if user wants us to reload the image
331 if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
333 //mlt_cache_item_close( self->image_cache );
334 self->current_image = NULL;
335 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
336 mlt_properties_set_int( producer_props, "force_reload", 0 );
339 if (self->current_image == NULL) {
340 // restore QGraphicsScene
341 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
343 if ( force_refresh == 1 && scene )
346 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
353 argv[0] = (char*) "xxx";
355 // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER
356 // the QApplication is created, otherwise their will be NULL
363 if ( getenv("DISPLAY") == 0 )
365 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" );
366 pthread_mutex_unlock( &self->mutex );
371 app = new QApplication( argc, argv );
372 //fix to let the decimal point for every locale be: "."
373 setlocale(LC_NUMERIC,"POSIX");
375 scene = new QGraphicsScene();
376 scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
377 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
378 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
381 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
382 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
384 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
385 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
386 const QRectF source( 0, 0, width, height );
387 if (start.isNull()) {
388 start = QRectF( 0, 0, originalWidth, originalHeight );
391 //must be extracted from kdenlive title
392 QImage img( width, height, QImage::Format_ARGB32 );
396 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
397 //| QPainter::SmoothPixmapTransform );
398 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
402 scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
404 else if ( position > anim_out ) {
405 scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
408 double percentage = position / anim_out;
409 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
410 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
411 const QRectF r1( topleft, bottomRight );
412 scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
416 int size = width * height * 4;
417 uint8_t *pointer=img.bits();
418 QRgb* src = ( QRgb* ) pointer;
419 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
420 uint8_t *dst = self->current_image;
422 for ( int i = 0; i < width * height * 4; i += 4 )
425 *dst++=qGreen( *src );
426 *dst++=qBlue( *src );
427 *dst++=qAlpha( *src );
431 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
432 self->current_width = width;
433 self->current_height = height;
436 pthread_mutex_unlock( &self->mutex );
437 mlt_properties_set_int( properties, "width", self->current_width );
438 mlt_properties_set_int( properties, "height", self->current_height );