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