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 #if QT_VERSION < 0x40500
139 transform = QTransform().scale( (double) width / originalWidth, (double) height / originalHeight );
141 transform = QTransform::fromScale ( (double) width / originalWidth, (double) height / originalHeight);
148 QDomNodeList items = titles.item( 0 ).childNodes();
149 for ( int i = 0; i < items.count(); i++ )
151 QGraphicsItem *gitem = NULL;
152 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
153 if ( zValue > -1000 )
155 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
157 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
158 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() );
172 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
173 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
174 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
175 // Older Kdenlive version did not store pixel size but point size
176 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
179 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
180 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
183 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
184 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
185 QGraphicsTextItem *txt;
186 if ( !replacementText.isEmpty() )
188 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
189 text = text.replace( "%s", replacementText );
190 txt = scene->addText( text, font );
192 else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
193 txt->setDefaultTextColor( col );
194 txt->setTextInteractionFlags( Qt::NoTextInteraction );
195 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
197 txt->setTextWidth( txt->boundingRect().width() );
198 QTextCursor cur = txt->textCursor();
199 QTextBlockFormat format = cur.blockFormat();
200 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
201 cur.select( QTextCursor::Document );
202 cur.setBlockFormat( format );
203 txt->setTextCursor( cur );
204 cur.clearSelection();
205 txt->setTextCursor( cur );
208 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
210 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
212 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
214 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
219 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
221 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
222 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
223 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
224 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
225 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
228 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
230 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
232 QGraphicsPixmapItem *rec = scene->addPixmap( pix );
233 rec->setData( Qt::UserRole, url );
236 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
238 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
239 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
240 //m_scene->addItem(rec);
241 //rec->setData(Qt::UserRole, url);
248 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
249 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
250 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
252 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
253 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
254 gitem->setZValue( zValue );
255 if ( transform != QTransform() ) gitem->setTransform( transform, true );
256 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
258 if ( items.item( i ).nodeName() == "background" )
260 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
261 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
262 QList<QGraphicsItem *> items = scene->items();
263 for ( int i = 0; i < items.size(); i++ )
265 if ( items.at( i )->zValue() == -1100 )
267 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
272 else if ( items.item( i ).nodeName() == "startviewport" )
274 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
275 if ( transform != QTransform() ) {
276 rect = rectTransform( rect, transform );
278 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
280 else if ( items.item( i ).nodeName() == "endviewport" )
282 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
283 if ( transform != QTransform() ) {
284 rect = rectTransform( rect, transform );
286 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
290 if ( mlt_properties_get( producer_props, "startrect") == mlt_properties_get( producer_props, "endrect") )
291 mlt_properties_set( producer_props, "endrect", "" );
295 QString rectTransform( QString s, QTransform t )
297 QStringList l = s.split( ',' );
298 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());
301 QString colorToString( const QColor& c )
303 QString ret = "%1,%2,%3,%4";
304 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
308 QString rectFToString( const QRectF& c )
310 QString ret = "%1,%2,%3,%4";
311 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
315 QRectF stringToRect( const QString & s )
318 QStringList l = s.split( ',' );
321 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
324 QColor stringToColor( const QString & s )
326 QStringList l = s.split( ',' );
329 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
332 QTransform stringToTransform( const QString& s )
334 QStringList l = s.split( ',' );
338 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
339 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
340 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()