]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
don't use cache, just normal properties to store scene
[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 "kdenlivetitle_wrapper.h"
21
22 #include <QtGui/QImage>
23 #include <QtGui/QPainter>
24 #include <QtCore/QDebug>
25 #include <QtGui/QApplication>
26 #include <QtCore/QMutex>
27 #include <QtGui/QGraphicsScene>
28 #include <QtGui/QGraphicsTextItem>
29 #include <QtGui/QTextCursor>
30
31 static QApplication *app = NULL;
32
33 extern "C"
34 {
35   
36 #include <framework/mlt_producer.h>
37 #include <framework/mlt_cache.h>
38
39         static QMutex g_mutex;
40         void refresh_kdenlivetitle( mlt_producer producer, uint8_t* buffer, int width, int height , double position, int force_refresh )
41         {
42                 drawKdenliveTitle( producer, buffer, width, height, position, force_refresh );
43         }
44
45         static void qscene_delete( void *data )
46         {
47                 QGraphicsScene *scene = ( QGraphicsScene * )data;
48                 delete scene;
49                 scene = NULL;
50         }
51 }
52
53
54
55 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position, int force_refresh )
56 {
57   
58         // restore QGraphicsScene
59         g_mutex.lock();
60         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
61         QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
62
63         if ( force_refresh == 1 && scene )
64         {
65                 scene = NULL;
66                 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
67         }
68  
69         if ( scene == NULL )
70         {
71                 int argc = 1;
72                 char* argv[1];
73                 argv[0] = "xxx";
74                 if (qApp) {
75                     app = qApp;
76                 }
77                 else {
78                     app=new QApplication( argc,argv );
79                 }
80                 scene = new QGraphicsScene( 0, 0, width, height, app );
81                 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ), width, height );
82                 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
83         }
84
85         g_mutex.unlock();
86         
87         //must be extracted from kdenlive title
88         QImage img( 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 );
93         //| QPainter::SmoothPixmapTransform );
94
95         const QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "startrect" ) ) );
96         const QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "endrect" ) ) );
97         const QRectF source( 0, 0, width, height );
98
99         if (end.isNull()) {
100                 if (start.isNull())
101                         scene->render( &p1, source, source );
102                 else
103                         scene->render( &p1, start, source );
104         }
105         else {
106             QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
107             QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
108             const QRectF r1( topleft, bottomRight );
109             scene->render( &p1, r1, source );
110         }
111         p1.end();
112         uint8_t *pointer=img.bits();
113         QRgb* src = ( QRgb* ) pointer;
114         for ( int i = 0; i < width * height * 4; i += 4 )
115         {
116                 *buffer++=qRed( *src );
117                 *buffer++=qGreen( *src );
118                 *buffer++=qBlue( *src );
119                 *buffer++=qAlpha( *src );
120                 src++;
121         }
122 }
123
124 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText, int width, int height  )
125 {
126         scene->clear();
127         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
128         QDomDocument doc;
129         QString data = QString::fromUtf8(templateXml);
130         QString replacementText = QString::fromUtf8(templateText);
131         doc.setContent(data);
132         QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
133         QTransform transform;
134         if ( doc.documentElement().hasAttribute("width") ) {
135             int originalWidth = doc.documentElement().attribute("width").toInt();
136             int originalHeight = doc.documentElement().attribute("height").toInt();
137             if (originalWidth != width || originalHeight != height) {
138                     transform = QTransform::fromScale ( (double) width / originalWidth, (double) height / originalHeight);
139             }
140         }
141         if ( titles.size() )
142         {
143
144                 QDomNodeList items = titles.item( 0 ).childNodes();
145                 for ( int i = 0; i < items.count(); i++ )
146                 {
147                         QGraphicsItem *gitem = NULL;
148                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
149                         if ( zValue > -1000 )
150                         {
151                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
152                                 {
153                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
154                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
155
156                                         QDomNode node = txtProperties.namedItem( "font-bold" );
157                                         if ( !node.isNull() )
158                                         {
159                                                 // Old: Bold/Not bold.
160                                                 font.setBold( node.nodeValue().toInt() );
161                                         }
162                                         else
163                                         {
164                                                 // New: Font weight (QFont::)
165                                                 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
166                                         }
167
168                                         //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
169                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
170                                         font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
171                                         // Older Kdenlive version did not store pixel size but point size
172                                         if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
173                                         {
174                                                 QFont f2;
175                                                 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
176                                                 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
177                                         }
178                                         else
179                                                 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
180                                         QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
181                                         QGraphicsTextItem *txt;
182                                         if ( !replacementText.isEmpty() )
183                                         {
184                                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
185                                                 text = text.replace( "%s", replacementText );
186                                                 txt = scene->addText( text, font );
187                                         }
188                                         else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
189                                         txt->setDefaultTextColor( col );
190                                         txt->setTextInteractionFlags( Qt::NoTextInteraction );
191                                         if ( txtProperties.namedItem( "alignment" ).isNull() == false )
192                                         {
193                                                 txt->setTextWidth( txt->boundingRect().width() );
194                                                 QTextCursor cur = txt->textCursor();
195                                                 QTextBlockFormat format = cur.blockFormat();
196                                                 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
197                                                 cur.select( QTextCursor::Document );
198                                                 cur.setBlockFormat( format );
199                                                 txt->setTextCursor( cur );
200                                                 cur.clearSelection();
201                                                 txt->setTextCursor( cur );
202                                         }
203
204                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
205                                         {
206                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
207                                         }
208                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
209                                         {
210                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
211                                         }
212
213                                         gitem = txt;
214                                 }
215                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
216                                 {
217                                         QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
218                                         QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
219                                         QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
220                                         double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
221                                         QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
222                                         gitem = rec;
223                                 }
224                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
225                                 {
226                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
227                                         QPixmap pix( url );
228                                         QGraphicsPixmapItem *rec = scene->addPixmap( pix );
229                                         rec->setData( Qt::UserRole, url );
230                                         gitem = rec;
231                                 }
232                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
233                                 {
234                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
235                                         //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
236                                         //m_scene->addItem(rec);
237                                         //rec->setData(Qt::UserRole, url);
238                                         //gitem = rec;
239                                 }
240                         }
241                         //pos and transform
242                         if ( gitem )
243                         {
244                                 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
245                                            items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
246                                 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
247                                 gitem->setPos( p );
248                                 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
249                                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
250                                 gitem->setZValue( zValue );
251                                 if ( transform != QTransform() ) gitem->setTransform( transform, true );
252                                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
253                         }
254                         if ( items.item( i ).nodeName() == "background" )
255                         {
256                                 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
257                                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
258                                 QList<QGraphicsItem *> items = scene->items();
259                                 for ( int i = 0; i < items.size(); i++ )
260                                 {
261                                         if ( items.at( i )->zValue() == -1100 )
262                                         {
263                                                 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
264                                                 break;
265                                         }
266                                 }
267                         }
268                         else if ( items.item( i ).nodeName() == "startviewport" )
269                         {
270                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
271                                 if ( transform != QTransform() ) {
272                                     rect = rectTransform( rect, transform );
273                                 }
274                                 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
275                         }
276                         else if ( items.item( i ).nodeName() == "endviewport" )
277                         {
278                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
279                                 if ( transform != QTransform() ) {
280                                     rect = rectTransform( rect, transform );
281                                 }
282                                 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
283                         }
284                 }
285         }
286         if ( mlt_properties_get( producer_props, "startrect") == mlt_properties_get( producer_props, "endrect") )
287                 mlt_properties_set( producer_props, "endrect", "" );
288         return;
289 }
290
291 QString rectTransform( QString s, QTransform t )
292 {
293         QStringList l = s.split( ',' );
294         return QString::number(l.at(0).toDouble() * t.m11()) + ',' + QString::number(l.at(1).toDouble() * t.m22()) + ',' + QString::number(l.at(2).toDouble() * t.m11()) + ',' + QString::number(l.at(3).toDouble() * t.m22());
295 }
296
297 QString colorToString( const QColor& c )
298 {
299         QString ret = "%1,%2,%3,%4";
300         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
301         return ret;
302 }
303
304 QString rectFToString( const QRectF& c )
305 {
306         QString ret = "%1,%2,%3,%4";
307         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
308         return ret;
309 }
310
311 QRectF stringToRect( const QString & s )
312 {
313
314         QStringList l = s.split( ',' );
315         if ( l.size() < 4 )
316                 return QRectF();
317         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
318 }
319
320 QColor stringToColor( const QString & s )
321 {
322         QStringList l = s.split( ',' );
323         if ( l.size() < 4 )
324                 return QColor();
325         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
326         ;
327 }
328 QTransform stringToTransform( const QString& s )
329 {
330         QStringList l = s.split( ',' );
331         if ( l.size() < 9 )
332                 return QTransform();
333         return QTransform(
334                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
335                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
336                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
337                );
338 }