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