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 )
43 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
44 loadFromXml( producer, NULL, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
46 drawKdenliveTitle( producer, buffer, width, height, position );
49 static void qscene_delete( void *data )
51 QGraphicsScene *scene = ( QGraphicsScene * )data;
59 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position )
62 // restore QGraphicsScene
64 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
65 QGraphicsScene *scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
66 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
77 app=new QApplication( argc,argv );
79 scene = new QGraphicsScene(0, 0, (qreal) width, (qreal) height, app);
80 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
81 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qscene", scene, 0, ( mlt_destructor )qscene_delete );
86 //must be extracted from kdenlive title
87 QImage img( width, height, QImage::Format_ARGB32 );
91 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
92 //| QPainter::SmoothPixmapTransform );
94 QRectF m_start = stringToRect( QString( mlt_properties_get( producer_props, "startrect" ) ) );
95 QRectF m_end = stringToRect( QString( mlt_properties_get( producer_props, "endrect" ) ) );
97 if (m_start.isNull() && m_end.isNull()) {
101 QPointF topleft=m_start.topLeft()+( m_end.topLeft()-m_start.topLeft() )*position;
102 QPointF bottomRight=m_start.bottomRight()+( m_end.bottomRight()-m_start.bottomRight() )*position;
103 const QRectF r1( 0, 0, width, height );
104 const QRectF r2( topleft, bottomRight );
105 scene->render( &p1, r1, r2 );
108 uint8_t *pointer=img.bits();
109 QRgb* src = ( QRgb* ) pointer;
110 for ( int i = 0; i < width * height * 4; i += 4 )
112 *buffer++=qRed( *src );
113 *buffer++=qGreen( *src );
114 *buffer++=qBlue( *src );
115 *buffer++=qAlpha( *src );
120 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
123 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
124 scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
125 if (scene == NULL) return;
128 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
130 QString data = QString::fromUtf8(templateXml);
131 QString replacementText = QString::fromUtf8(templateText);
132 doc.setContent(data);
133 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
138 QDomNodeList items = titles.item( 0 ).childNodes();
139 for ( int i = 0; i < items.count(); i++ )
141 QGraphicsItem *gitem = NULL;
142 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
143 if ( zValue > -1000 )
145 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
147 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
148 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
150 QDomNode node = txtProperties.namedItem( "font-bold" );
151 if ( !node.isNull() )
153 // Old: Bold/Not bold.
154 font.setBold( node.nodeValue().toInt() );
158 // New: Font weight (QFont::)
159 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
162 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
163 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
164 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
165 // Older Kdenlive version did not store pixel size but point size
166 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
169 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
170 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
173 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
174 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
175 QGraphicsTextItem *txt;
176 if ( !replacementText.isEmpty() )
178 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
179 text = text.replace( "%s", replacementText );
180 txt = scene->addText( text, font );
182 else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
183 txt->setDefaultTextColor( col );
184 txt->setTextInteractionFlags( Qt::NoTextInteraction );
185 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
187 txt->setTextWidth( txt->boundingRect().width() );
188 QTextCursor cur = txt->textCursor();
189 QTextBlockFormat format = cur.blockFormat();
190 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
191 cur.select( QTextCursor::Document );
192 cur.setBlockFormat( format );
193 txt->setTextCursor( cur );
194 cur.clearSelection();
195 txt->setTextCursor( cur );
198 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
200 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
202 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
204 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
209 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
211 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
212 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
213 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
214 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
215 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
218 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
220 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
222 QGraphicsPixmapItem *rec = scene->addPixmap( pix );
223 rec->setData( Qt::UserRole, url );
226 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
228 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
229 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
230 //m_scene->addItem(rec);
231 //rec->setData(Qt::UserRole, url);
238 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
239 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
241 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
242 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
243 if ( zValue > maxZValue ) maxZValue = zValue;
244 gitem->setZValue( zValue );
245 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
247 if ( items.item( i ).nodeName() == "background" )
249 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
250 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
251 QList<QGraphicsItem *> items = scene->items();
252 for ( int i = 0; i < items.size(); i++ )
254 if ( items.at( i )->zValue() == -1100 )
256 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
261 else if ( items.item( i ).nodeName() == "startviewport" )
263 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
265 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
267 else if ( items.item( i ).nodeName() == "endviewport" )
269 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
270 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
277 QString colorToString( const QColor& c )
279 QString ret = "%1,%2,%3,%4";
280 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
284 QString rectFToString( const QRectF& c )
286 QString ret = "%1,%2,%3,%4";
287 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
291 QRectF stringToRect( const QString & s )
294 QStringList l = s.split( ',' );
297 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
300 QColor stringToColor( const QString & s )
302 QStringList l = s.split( ',' );
305 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
308 QTransform stringToTransform( const QString& s )
310 QStringList l = s.split( ',' );
314 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
315 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
316 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()