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"
21 #include <framework/mlt_producer.h>
23 #include <QtGui/QImage>
24 #include <QtGui/QPainter>
25 #include <QtCore/QDebug>
26 #include <QtGui/QApplication>
27 #include <QtCore/QFile>
28 #include <QtGui/QGraphicsScene>
29 #include <QtGui/QGraphicsTextItem>
30 #include <QtGui/QTextCursor>
32 static Title* titleclass = NULL;
33 static QApplication *app = NULL;
37 void init_qt( const char* c )
39 titleclass=new Title( c );
47 void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position, char *templatexml, char *templatetext, int force_refresh )
49 if (force_refresh) titleclass->loadFromXml( templatexml, QString( templatetext ) );
50 titleclass->drawKdenliveTitle( buffer, width, height, position, templatexml, templatetext );
55 Title::Title( const char *filename ):m_filename( filename ), m_scene( NULL )
67 void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, const char *templatexml, const char *templatetext )
69 if ( m_scene == NULL )
78 app=new QApplication( argc,argv );
80 m_scene = new QGraphicsScene(0, 0, (qreal) width, (qreal) height, app);
81 loadFromXml( templatexml, QString( templatetext ) );
83 //must be extracted from kdenlive title
84 QImage *img = new QImage( width,height,QImage::Format_ARGB32 );
88 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );//|QPainter::SmoothPixmapTransform );
89 if (m_start.isNull() && m_end.isNull()) {
90 m_scene->render( &p1 );
93 QPointF topleft=m_start.topLeft()+( m_end.topLeft()-m_start.topLeft() )*position;
94 QPointF bottomRight=m_start.bottomRight()+( m_end.bottomRight()-m_start.bottomRight() )*position;
95 const QRectF r1( 0, 0, width, height );
96 const QRectF r2( topleft, bottomRight );
97 m_scene->render( &p1, r1, r2 );
100 uint8_t *pointer=img->bits();
101 QRgb* src = ( QRgb* ) pointer;
102 for ( int i = 0; i < width * height * 4; i += 4 )
104 *buffer++=qRed( *src );
105 *buffer++=qGreen( *src );
106 *buffer++=qBlue( *src );
107 *buffer++=qAlpha( *src );
113 void Title::loadFromXml( const char *templateXml, const QString templateText )
115 if (m_scene == NULL) return;
118 QString data(templateXml);
119 doc.setContent(data);
120 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
125 QDomNodeList items = titles.item( 0 ).childNodes();
126 for ( int i = 0; i < items.count(); i++ )
128 QGraphicsItem *gitem = NULL;
129 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
130 if ( zValue > -1000 )
132 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
134 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
135 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
136 font.setBold( txtProperties.namedItem( "font-bold" ).nodeValue().toInt() );
137 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
138 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
139 // Older Kdenlive version did not store pixel size but point size
140 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
143 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
144 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
147 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
148 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
149 QGraphicsTextItem *txt;
150 if ( !templateText.isEmpty() )
152 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
153 text = text.replace( "%s", templateText );
154 txt = m_scene->addText( text, font );
156 else txt = m_scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
157 txt->setDefaultTextColor( col );
158 txt->setTextInteractionFlags( Qt::NoTextInteraction );
159 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
161 txt->setTextWidth( txt->boundingRect().width() );
162 QTextCursor cur = txt->textCursor();
163 QTextBlockFormat format = cur.blockFormat();
164 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
165 cur.select( QTextCursor::Document );
166 cur.setBlockFormat( format );
167 txt->setTextCursor( cur );
168 cur.clearSelection();
169 txt->setTextCursor( cur );
172 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
174 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
176 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
178 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
183 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
185 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
186 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
187 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
188 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
189 QGraphicsRectItem *rec = m_scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
192 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
194 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
196 QGraphicsPixmapItem *rec = m_scene->addPixmap( pix );
197 rec->setData( Qt::UserRole, url );
200 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
202 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
203 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
204 //m_scene->addItem(rec);
205 //rec->setData(Qt::UserRole, url);
212 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
213 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
215 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
216 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
217 if ( zValue > maxZValue ) maxZValue = zValue;
218 gitem->setZValue( zValue );
219 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
221 if ( items.item( i ).nodeName() == "background" )
223 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
224 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
225 QList<QGraphicsItem *> items = m_scene->items();
226 for ( int i = 0; i < items.size(); i++ )
228 if ( items.at( i )->zValue() == -1100 )
230 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
235 else if ( items.item( i ).nodeName() == "startviewport" )
237 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
238 m_start = stringToRect( rect );
240 else if ( items.item( i ).nodeName() == "endviewport" )
242 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
243 m_end = stringToRect( rect );
250 QString Title::colorToString( const QColor& c )
252 QString ret = "%1,%2,%3,%4";
253 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
257 QString Title::rectFToString( const QRectF& c )
259 QString ret = "%1,%2,%3,%4";
260 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
264 QRectF Title::stringToRect( const QString & s )
267 QStringList l = s.split( ',' );
270 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
273 QColor Title::stringToColor( const QString & s )
275 QStringList l = s.split( ',' );
278 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
281 QTransform Title::stringToTransform( const QString& s )
283 QStringList l = s.split( ',' );
287 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
288 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
289 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()