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