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>
41 static QApplication *app = NULL;
43 class ImageItem: public QGraphicsRectItem
54 virtual void paint( QPainter *painter,
55 const QStyleOptionGraphicsItem * /*option*/,
58 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
59 painter->drawImage(QPoint(), m_img);
64 QString rectTransform( QString s, QTransform t )
66 QStringList l = s.split( ',' );
67 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());
70 QString colorToString( const QColor& c )
72 QString ret = "%1,%2,%3,%4";
73 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
77 QString rectFToString( const QRectF& c )
79 QString ret = "%1,%2,%3,%4";
80 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
84 QRectF stringToRect( const QString & s )
87 QStringList l = s.split( ',' );
90 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
93 QColor stringToColor( const QString & s )
95 QStringList l = s.split( ',' );
98 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
101 QTransform stringToTransform( const QString& s )
103 QStringList l = s.split( ',' );
107 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
108 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
109 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
113 static void qscene_delete( void *data )
115 QGraphicsScene *scene = ( QGraphicsScene * )data;
121 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
124 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
126 QString data = QString::fromUtf8(templateXml);
127 QString replacementText = QString::fromUtf8(templateText);
128 doc.setContent(data);
129 QDomElement title = doc.documentElement();
131 // Check for invalid title
132 if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
134 QTransform transform;
135 if ( title.hasAttribute("width") ) {
136 int originalWidth = title.attribute("width").toInt();
137 mlt_properties_set_int( producer_props, "_original_width", originalWidth );
138 int originalHeight = title.attribute("height").toInt();
139 mlt_properties_set_int( producer_props, "_original_height", originalHeight );
140 scene->setSceneRect(0, 0, originalWidth, originalHeight);
143 QDomNodeList items = title.elementsByTagName("item");
144 for ( int i = 0; i < items.count(); i++ )
146 QGraphicsItem *gitem = NULL;
147 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
148 if ( zValue > -1000 )
150 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
152 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
153 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
154 QDomNode node = txtProperties.namedItem( "font-bold" );
155 if ( !node.isNull() )
157 // Old: Bold/Not bold.
158 font.setBold( node.nodeValue().toInt() );
162 // New: Font weight (QFont::)
163 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
165 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
166 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
167 // Older Kdenlive version did not store pixel size but point size
168 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
171 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
172 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
175 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
176 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
177 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
178 if ( !replacementText.isEmpty() )
180 text = text.replace( "%s", replacementText );
182 QGraphicsTextItem *txt = scene->addText(text, font);
183 txt->setDefaultTextColor( col );
184 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
186 txt->setTextWidth( txt->boundingRect().width() );
187 QTextCursor cur = txt->textCursor();
188 QTextBlockFormat format = cur.blockFormat();
189 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
190 cur.select( QTextCursor::Document );
191 cur.setBlockFormat( format );
192 txt->setTextCursor( cur );
193 cur.clearSelection();
194 txt->setTextCursor( cur );
196 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
198 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
200 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
202 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
206 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
208 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
209 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
210 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
211 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
212 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
215 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
217 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
219 ImageItem *rec = new ImageItem(img);
220 scene->addItem( rec );
223 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
225 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
226 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
234 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
235 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
237 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
238 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
239 gitem->setZValue( zValue );
243 QDomNode n = doc.documentElement().firstChildElement("background");
245 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
246 if (color.alpha() > 0) {
247 QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
248 rec->setZValue(-1100);
254 n = doc.documentElement().firstChildElement( "startviewport" );
257 startRect = n.attributes().namedItem( "rect" ).nodeValue();
259 n = doc.documentElement().firstChildElement( "endviewport" );
262 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
263 if (startRect != rect)
264 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
266 if (!startRect.isEmpty()) {
267 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
273 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
275 // Obtain the producer
276 mlt_producer producer = &self->parent;
277 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
279 // Obtain properties of frame
280 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
282 pthread_mutex_lock( &self->mutex );
284 // Check if user wants us to reload the image
285 if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
287 //mlt_cache_item_close( self->image_cache );
288 self->current_image = NULL;
289 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
290 mlt_properties_set_int( producer_props, "force_reload", 0 );
293 if (self->current_image == NULL) {
294 // restore QGraphicsScene
295 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
297 if ( force_refresh == 1 && scene )
300 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
307 argv[0] = (char*) "xxx";
312 app=new QApplication( argc,argv ); //, QApplication::Tty );
314 scene = new QGraphicsScene();
315 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
316 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
319 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
320 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
322 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
323 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
324 const QRectF source( 0, 0, width, height );
325 if (start.isNull()) {
326 start = QRectF( 0, 0, originalWidth, originalHeight );
329 //must be extracted from kdenlive title
330 QImage img( width, height, QImage::Format_ARGB32 );
334 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
335 //| QPainter::SmoothPixmapTransform );
338 scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
342 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
343 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
344 const QRectF r1( topleft, bottomRight );
345 scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
349 int size = width * height * 4;
350 uint8_t *pointer=img.bits();
351 QRgb* src = ( QRgb* ) pointer;
352 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
353 uint8_t *dst = self->current_image;
355 for ( int i = 0; i < width * height * 4; i += 4 )
358 *dst++=qGreen( *src );
359 *dst++=qBlue( *src );
360 *dst++=qAlpha( *src );
364 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
365 self->current_width = width;
366 self->current_height = height;
369 pthread_mutex_unlock( &self->mutex );
370 mlt_properties_set_int( properties, "width", self->current_width );
371 mlt_properties_set_int( properties, "height", self->current_height );