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 )
42 drawKdenliveTitle( producer, buffer, width, height, position, force_refresh );
45 static void qscene_delete( void *data )
47 QGraphicsScene *scene = ( QGraphicsScene * )data;
55 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position, int force_refresh )
58 // restore QGraphicsScene
60 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
61 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
63 if ( force_refresh == 1 && scene )
66 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
78 app=new QApplication( argc,argv );
80 scene = new QGraphicsScene( 0, 0, width, height, app );
81 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ), width, height );
82 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
87 //must be extracted from kdenlive title
88 QImage img( width, height, QImage::Format_ARGB32 );
92 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
93 //| QPainter::SmoothPixmapTransform );
95 const QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "startrect" ) ) );
96 const QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "endrect" ) ) );
97 const QRectF source( 0, 0, width, height );
101 scene->render( &p1, source, source );
103 scene->render( &p1, start, source );
106 QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
107 QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
108 const QRectF r1( topleft, bottomRight );
109 scene->render( &p1, r1, source );
112 uint8_t *pointer=img.bits();
113 QRgb* src = ( QRgb* ) pointer;
114 for ( int i = 0; i < width * height * 4; i += 4 )
116 *buffer++=qRed( *src );
117 *buffer++=qGreen( *src );
118 *buffer++=qBlue( *src );
119 *buffer++=qAlpha( *src );
124 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText, int width, int height )
127 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
129 QString data = QString::fromUtf8(templateXml);
130 QString replacementText = QString::fromUtf8(templateText);
131 doc.setContent(data);
132 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
133 QTransform transform;
134 if ( doc.documentElement().hasAttribute("width") ) {
135 int originalWidth = doc.documentElement().attribute("width").toInt();
136 int originalHeight = doc.documentElement().attribute("height").toInt();
137 if (originalWidth != width || originalHeight != height) {
138 transform = QTransform::fromScale ( (double) width / originalWidth, (double) height / originalHeight);
144 QDomNodeList items = titles.item( 0 ).childNodes();
145 for ( int i = 0; i < items.count(); i++ )
147 QGraphicsItem *gitem = NULL;
148 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
149 if ( zValue > -1000 )
151 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
153 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
154 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
156 QDomNode node = txtProperties.namedItem( "font-bold" );
157 if ( !node.isNull() )
159 // Old: Bold/Not bold.
160 font.setBold( node.nodeValue().toInt() );
164 // New: Font weight (QFont::)
165 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
168 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
169 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
170 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
171 // Older Kdenlive version did not store pixel size but point size
172 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
175 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
176 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
179 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
180 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
181 QGraphicsTextItem *txt;
182 if ( !replacementText.isEmpty() )
184 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
185 text = text.replace( "%s", replacementText );
186 txt = scene->addText( text, font );
188 else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
189 txt->setDefaultTextColor( col );
190 txt->setTextInteractionFlags( Qt::NoTextInteraction );
191 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
193 txt->setTextWidth( txt->boundingRect().width() );
194 QTextCursor cur = txt->textCursor();
195 QTextBlockFormat format = cur.blockFormat();
196 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
197 cur.select( QTextCursor::Document );
198 cur.setBlockFormat( format );
199 txt->setTextCursor( cur );
200 cur.clearSelection();
201 txt->setTextCursor( cur );
204 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
206 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
208 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
210 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
215 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
217 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
218 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
219 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
220 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
221 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
224 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
226 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
228 QGraphicsPixmapItem *rec = scene->addPixmap( pix );
229 rec->setData( Qt::UserRole, url );
232 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
234 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
235 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
236 //m_scene->addItem(rec);
237 //rec->setData(Qt::UserRole, url);
244 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
245 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
246 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
248 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
249 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
250 gitem->setZValue( zValue );
251 if ( transform != QTransform() ) gitem->setTransform( transform, true );
252 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
254 if ( items.item( i ).nodeName() == "background" )
256 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
257 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
258 QList<QGraphicsItem *> items = scene->items();
259 for ( int i = 0; i < items.size(); i++ )
261 if ( items.at( i )->zValue() == -1100 )
263 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
268 else if ( items.item( i ).nodeName() == "startviewport" )
270 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
271 if ( transform != QTransform() ) {
272 rect = rectTransform( rect, transform );
274 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
276 else if ( items.item( i ).nodeName() == "endviewport" )
278 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
279 if ( transform != QTransform() ) {
280 rect = rectTransform( rect, transform );
282 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
286 if ( mlt_properties_get( producer_props, "startrect") == mlt_properties_get( producer_props, "endrect") )
287 mlt_properties_set( producer_props, "endrect", "" );
291 QString rectTransform( QString s, QTransform t )
293 QStringList l = s.split( ',' );
294 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());
297 QString colorToString( const QColor& c )
299 QString ret = "%1,%2,%3,%4";
300 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
304 QString rectFToString( const QRectF& c )
306 QString ret = "%1,%2,%3,%4";
307 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
311 QRectF stringToRect( const QString & s )
314 QStringList l = s.split( ',' );
317 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
320 QColor stringToColor( const QString & s )
322 QStringList l = s.split( ',' );
325 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
328 QTransform stringToTransform( const QString& s )
330 QStringList l = s.split( ',' );
334 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
335 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
336 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()