]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
* Fix memleaks
[mlt] / src / modules / qimage / kdenlivetitle_wrapper.cpp
1 /*
2  * kdenlivetitle_wrapper.cpp -- kdenlivetitle wrapper
3  * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <QtGui/QImage>
21 #include <QtGui/QPainter>
22 #include <QtCore/QCoreApplication>
23 #include <QtGui/QApplication>
24 #include <QtCore/QDebug>
25 #include <QtCore/QFile>
26 #include <QtGui/QGraphicsScene>
27 #include <QtGui/QGraphicsTextItem>
28 #include <QtGui/QGraphicsPolygonItem>
29 #include <QtGui/QTextCursor>
30 #include "kdenlivetitle_wrapper.h"
31 #include <framework/mlt_producer.h>
32
33 static Title* titleclass;
34 static QApplication *app;
35
36 extern "C"
37 {
38         void init_qt( const char* c )
39         {
40                 titleclass=new Title( QString( c ) );
41         }
42         void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position, char *templatexml, char *templatetext, int force_refresh )
43         {
44                 if (force_refresh) titleclass->reloadXml(templatexml, templatetext);
45                 titleclass->drawKdenliveTitle( buffer, width, height, position, templatexml, templatetext );
46         }
47 }
48
49 Title::Title( const QString& filename ):m_filename( filename ), sceneLoaded( false )
50 {
51         //must be extracted from kdenlive title
52         /*m_start( QPolygonF( QRectF( 100, 100, 600, 600 ) ) );
53         m_end( QPolygonF( QRectF( 0, 0, 300, 300 ) ) );*/
54 }
55
56 void Title::reloadXml(char *templatexml, char *templatetext)
57 {
58         loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
59 }
60
61 void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, char *templatexml, char *templatetext )
62 {
63         if ( !sceneLoaded )
64         {
65                 int argc=0;
66                 char* argv[1];
67                 argv[0]="xxx";
68                 if ( ! QApplication::activeWindow() )
69                         //if (!app)
70                         app=new QApplication( argc,argv );
71                 loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
72         }
73         //must be extracted from kdenlive title
74
75         QImage *img=new QImage( width,height,QImage::Format_ARGB32 );
76         img->fill( 0 );
77         QPainter p1;
78         p1.begin( img );
79         p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );//|QPainter::SmoothPixmapTransform );
80         
81         if (m_start.polygon().isEmpty() && m_end.polygon().isEmpty()) {
82             m_scene.render( &p1,QRect( 0, 0, width, height ) );
83         }
84         else {
85             QRectF rstart=m_start.boundingRect();
86             QRectF rend=m_end.boundingRect();
87             QPointF topleft=rstart.topLeft()+( rend.topLeft()-rstart.topLeft() )*position;
88             QPointF bottomRight=rstart.bottomRight()+( rend.bottomRight()-rstart.bottomRight() )*position;
89             m_scene.render( &p1,QRect( 0,0,width,height ),QRectF( topleft,bottomRight ) );
90         }
91         p1.end();
92         uint8_t *pointer=img->bits();
93         QRgb* src = ( QRgb* ) pointer;
94         for ( int i=0;i<width*height*4;i+=4 )
95         {
96                 *buffer++=qRed( *src );
97                 *buffer++=qGreen( *src );
98                 *buffer++=qBlue( *src );
99                 *buffer++=qAlpha( *src );
100                 src++;
101         }
102         delete img;
103 }
104
105 int Title::loadDocument( const QString& url, const QString templateXml, const QString templateText )
106 {
107         QDomDocument doc;
108         sceneLoaded = true;
109         if ( !templateXml.isEmpty() )
110         {
111                 doc.setContent( templateXml );
112         }
113         else
114         {
115                 QFile file( url );
116                 if ( file.open( QIODevice::ReadOnly ) )
117                 {
118                         doc.setContent( &file, false );
119                         file.close();
120                 }
121         }
122         return loadFromXml( doc, templateText );
123 }
124
125 int Title::loadFromXml( QDomDocument doc, const QString templateText )
126 {
127         m_scene.clear();
128         QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
129         int maxZValue = 0;
130         if ( titles.size() )
131         {
132
133                 QDomNodeList items = titles.item( 0 ).childNodes();
134                 for ( int i = 0; i < items.count(); i++ )
135                 {
136                         QGraphicsItem *gitem = NULL;
137                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
138                         if ( zValue > -1000 )
139                         {
140                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
141                                 {
142                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
143                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
144                                         font.setBold( txtProperties.namedItem( "font-bold" ).nodeValue().toInt() );
145                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
146                                         font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
147                                         // Older Kdenlive version did not store pixel size but point size
148                                         if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
149                                         {
150                                                 QFont f2;
151                                                 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
152                                                 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
153                                         }
154                                         else
155                                                 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
156                                         QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
157                                         QGraphicsTextItem *txt;
158                                         if ( !templateText.isEmpty() )
159                                         {
160                                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
161                                                 text = text.replace( "%s", templateText );
162                                                 txt = m_scene.addText( text, font );
163                                         }
164                                         else txt = m_scene.addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
165                                         txt->setDefaultTextColor( col );
166                                         txt->setTextInteractionFlags( Qt::NoTextInteraction );
167                                         if ( txtProperties.namedItem( "alignment" ).isNull() == false )
168                                         {
169                                                 txt->setTextWidth( txt->boundingRect().width() );
170                                                 QTextCursor cur = txt->textCursor();
171                                                 QTextBlockFormat format = cur.blockFormat();
172                                                 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
173                                                 cur.select( QTextCursor::Document );
174                                                 cur.setBlockFormat( format );
175                                                 txt->setTextCursor( cur );
176                                                 cur.clearSelection();
177                                                 txt->setTextCursor( cur );
178                                         }
179
180                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
181                                         {
182                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
183                                         }
184                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
185                                         {
186                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
187                                         }
188
189                                         gitem = txt;
190                                 }
191                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
192                                 {
193                                         QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
194                                         QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
195                                         QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
196                                         double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
197                                         QGraphicsRectItem *rec = m_scene.addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
198                                         gitem = rec;
199                                 }
200                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
201                                 {
202                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
203                                         QPixmap pix( url );
204                                         QGraphicsPixmapItem *rec = m_scene.addPixmap( pix );
205                                         rec->setData( Qt::UserRole, url );
206                                         gitem = rec;
207                                 }
208                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
209                                 {
210                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
211                                         //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
212                                         //m_scene.addItem(rec);
213                                         //rec->setData(Qt::UserRole, url);
214                                         //gitem = rec;
215                                 }
216                         }
217                         //pos and transform
218                         if ( gitem )
219                         {
220                                 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
221                                            items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
222                                 gitem->setPos( p );
223                                 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
224                                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
225                                 if ( zValue > maxZValue ) maxZValue = zValue;
226                                 gitem->setZValue( zValue );
227                                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
228                         }
229                         if ( items.item( i ).nodeName() == "background" )
230                         {
231                                 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
232                                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
233                                 QList<QGraphicsItem *> items = m_scene.items();
234                                 for ( int i = 0; i < items.size(); i++ )
235                                 {
236                                         if ( items.at( i )->zValue() == -1100 )
237                                         {
238                                                 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
239                                                 break;
240                                         }
241                                 }
242                         }
243                         else if ( items.item( i ).nodeName() == "startviewport" )
244                         {
245                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
246                                 m_start.setPolygon( stringToRect( rect ) );
247                         }
248                         else if ( items.item( i ).nodeName() == "endviewport" )
249                         {
250                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
251                                 m_end.setPolygon( stringToRect( rect ) );
252                         }
253                 }
254         }
255         return maxZValue;
256 }
257
258 QString Title::colorToString( const QColor& c )
259 {
260         QString ret = "%1,%2,%3,%4";
261         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
262         return ret;
263 }
264
265 QString Title::rectFToString( const QRectF& c )
266 {
267         QString ret = "%1,%2,%3,%4";
268         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
269         return ret;
270 }
271
272 QRectF Title::stringToRect( const QString & s )
273 {
274
275         QStringList l = s.split( ',' );
276         if ( l.size() < 4 )
277                 return QRectF();
278         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
279 }
280
281 QColor Title::stringToColor( const QString & s )
282 {
283         QStringList l = s.split( ',' );
284         if ( l.size() < 4 )
285                 return QColor();
286         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
287         ;
288 }
289 QTransform Title::stringToTransform( const QString& s )
290 {
291         QStringList l = s.split( ',' );
292         if ( l.size() < 9 )
293                 return QTransform();
294         return QTransform(
295                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
296                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
297                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
298                );
299 }