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