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 <QtGui/QImage>
21 #include <QtGui/QPainter>
22 #include <QtCore/QCoreApplication>
23 #include <QtGui/QApplication>
24 #include <QtCore/QFile>
25 #include <QtGui/QGraphicsScene>
26 #include <QtGui/QGraphicsTextItem>
27 #include <QtGui/QGraphicsPolygonItem>
28 #include <QtGui/QTextCursor>
29 #include "kdenlivetitle_wrapper.h"
30 #include <framework/mlt_producer.h>
32 static Title* titleclass;
33 static QApplication *app;
37 void init_qt( const char* c )
39 titleclass=new Title( QString( 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->reloadXml(templatexml, templatetext);
50 titleclass->drawKdenliveTitle( buffer, width, height, position, templatexml, templatetext );
54 Title::Title( const QString& filename ):m_filename( filename ), m_scene( NULL )
60 if (m_scene) delete m_scene;
64 void Title::reloadXml(char *templatexml, char *templatetext)
66 if (m_scene == NULL) return;
67 loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
70 void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, char *templatexml, char *templatetext )
72 if ( m_scene == NULL )
77 if ( ! QApplication::activeWindow() )
79 app=new QApplication( argc,argv );
80 m_scene = new QGraphicsScene(app);
81 loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
83 //must be extracted from kdenlive title
85 QImage *img=new QImage( width,height,QImage::Format_ARGB32 );
89 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );//|QPainter::SmoothPixmapTransform );
91 if (m_start.isNull() && m_end.isNull()) {
92 m_scene->render( &p1,QRect( 0, 0, width, height ) );
95 QPointF topleft=m_start.topLeft()+( m_end.topLeft()-m_start.topLeft() )*position;
96 QPointF bottomRight=m_start.bottomRight()+( m_end.bottomRight()-m_start.bottomRight() )*position;
97 m_scene->render( &p1,QRect( 0,0,width,height ),QRectF( topleft,bottomRight ) );
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 int Title::loadDocument( const QString& url, const QString templateXml, const QString templateText )
115 if (m_scene == NULL) return 0;
117 if ( !templateXml.isEmpty() )
119 doc.setContent( templateXml );
124 if ( file.open( QIODevice::ReadOnly ) )
126 doc.setContent( &file, false );
130 return loadFromXml( doc, templateText );
133 int Title::loadFromXml( QDomDocument doc, const QString templateText )
136 QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
141 QDomNodeList items = titles.item( 0 ).childNodes();
142 for ( int i = 0; i < items.count(); i++ )
144 QGraphicsItem *gitem = NULL;
145 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
146 if ( zValue > -1000 )
148 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
150 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
151 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
153 QDomNode node = txtProperties.namedItem( "font-bold" );
154 if ( !node.isNull() )
156 // Old: Bold/Not bold.
157 font.setBold( node.nodeValue().toInt() );
161 // New: Font weight (QFont::)
162 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
165 //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
166 font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
167 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
168 // Older Kdenlive version did not store pixel size but point size
169 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
172 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
173 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
176 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
177 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
178 QGraphicsTextItem *txt;
179 if ( !templateText.isEmpty() )
181 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
182 text = text.replace( "%s", templateText );
183 txt = m_scene->addText( text, font );
185 else txt = m_scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
186 txt->setDefaultTextColor( col );
187 txt->setTextInteractionFlags( Qt::NoTextInteraction );
188 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
190 txt->setTextWidth( txt->boundingRect().width() );
191 QTextCursor cur = txt->textCursor();
192 QTextBlockFormat format = cur.blockFormat();
193 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
194 cur.select( QTextCursor::Document );
195 cur.setBlockFormat( format );
196 txt->setTextCursor( cur );
197 cur.clearSelection();
198 txt->setTextCursor( cur );
201 if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
203 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
205 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
207 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
212 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
214 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
215 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
216 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
217 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
218 QGraphicsRectItem *rec = m_scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
221 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
223 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
225 QGraphicsPixmapItem *rec = m_scene->addPixmap( pix );
226 rec->setData( Qt::UserRole, url );
229 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
231 QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
232 //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
233 //m_scene->addItem(rec);
234 //rec->setData(Qt::UserRole, url);
241 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
242 items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
244 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
245 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
246 if ( zValue > maxZValue ) maxZValue = zValue;
247 gitem->setZValue( zValue );
248 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
250 if ( items.item( i ).nodeName() == "background" )
252 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
253 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
254 QList<QGraphicsItem *> items = m_scene->items();
255 for ( int i = 0; i < items.size(); i++ )
257 if ( items.at( i )->zValue() == -1100 )
259 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
264 else if ( items.item( i ).nodeName() == "startviewport" )
266 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
267 m_start = stringToRect( rect );
269 else if ( items.item( i ).nodeName() == "endviewport" )
271 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
272 m_end = stringToRect( rect );
279 QString Title::colorToString( const QColor& c )
281 QString ret = "%1,%2,%3,%4";
282 ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
286 QString Title::rectFToString( const QRectF& c )
288 QString ret = "%1,%2,%3,%4";
289 ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
293 QRectF Title::stringToRect( const QString & s )
296 QStringList l = s.split( ',' );
299 return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
302 QColor Title::stringToColor( const QString & s )
304 QStringList l = s.split( ',' );
307 return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
310 QTransform Title::stringToTransform( const QString& s )
312 QStringList l = s.split( ',' );
316 l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
317 l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
318 l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()