]> 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( 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 }
119
120 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
121 {
122         if (scene == NULL) {
123                 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
124                 scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
125                 if (scene == NULL) return;
126         }
127         scene->clear();
128         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
129         QDomDocument doc;
130         QString data = QString::fromUtf8(templateXml);
131         QString replacementText = QString::fromUtf8(templateText);
132         doc.setContent(data);
133         QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
134         int maxZValue = 0;
135         if ( titles.size() )
136         {
137
138                 QDomNodeList items = titles.item( 0 ).childNodes();
139                 for ( int i = 0; i < items.count(); i++ )
140                 {
141                         QGraphicsItem *gitem = NULL;
142                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
143                         if ( zValue > -1000 )
144                         {
145                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
146                                 {
147                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
148                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
149
150                                         QDomNode node = txtProperties.namedItem( "font-bold" );
151                                         if ( !node.isNull() )
152                                         {
153                                                 // Old: Bold/Not bold.
154                                                 font.setBold( node.nodeValue().toInt() );
155                                         }
156                                         else
157                                         {
158                                                 // New: Font weight (QFont::)
159                                                 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
160                                         }
161
162                                         //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
163                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
164                                         font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
165                                         // Older Kdenlive version did not store pixel size but point size
166                                         if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
167                                         {
168                                                 QFont f2;
169                                                 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
170                                                 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
171                                         }
172                                         else
173                                                 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
174                                         QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
175                                         QGraphicsTextItem *txt;
176                                         if ( !replacementText.isEmpty() )
177                                         {
178                                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
179                                                 text = text.replace( "%s", replacementText );
180                                                 txt = scene->addText( text, font );
181                                         }
182                                         else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
183                                         txt->setDefaultTextColor( col );
184                                         txt->setTextInteractionFlags( Qt::NoTextInteraction );
185                                         if ( txtProperties.namedItem( "alignment" ).isNull() == false )
186                                         {
187                                                 txt->setTextWidth( txt->boundingRect().width() );
188                                                 QTextCursor cur = txt->textCursor();
189                                                 QTextBlockFormat format = cur.blockFormat();
190                                                 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
191                                                 cur.select( QTextCursor::Document );
192                                                 cur.setBlockFormat( format );
193                                                 txt->setTextCursor( cur );
194                                                 cur.clearSelection();
195                                                 txt->setTextCursor( cur );
196                                         }
197
198                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
199                                         {
200                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
201                                         }
202                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
203                                         {
204                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
205                                         }
206
207                                         gitem = txt;
208                                 }
209                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
210                                 {
211                                         QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
212                                         QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
213                                         QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
214                                         double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
215                                         QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
216                                         gitem = rec;
217                                 }
218                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
219                                 {
220                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
221                                         QPixmap pix( url );
222                                         QGraphicsPixmapItem *rec = scene->addPixmap( pix );
223                                         rec->setData( Qt::UserRole, url );
224                                         gitem = rec;
225                                 }
226                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
227                                 {
228                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
229                                         //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
230                                         //m_scene->addItem(rec);
231                                         //rec->setData(Qt::UserRole, url);
232                                         //gitem = rec;
233                                 }
234                         }
235                         //pos and transform
236                         if ( gitem )
237                         {
238                                 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
239                                            items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
240                                 gitem->setPos( p );
241                                 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
242                                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
243                                 if ( zValue > maxZValue ) maxZValue = zValue;
244                                 gitem->setZValue( zValue );
245                                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
246                         }
247                         if ( items.item( i ).nodeName() == "background" )
248                         {
249                                 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
250                                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
251                                 QList<QGraphicsItem *> items = scene->items();
252                                 for ( int i = 0; i < items.size(); i++ )
253                                 {
254                                         if ( items.at( i )->zValue() == -1100 )
255                                         {
256                                                 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
257                                                 break;
258                                         }
259                                 }
260                         }
261                         else if ( items.item( i ).nodeName() == "startviewport" )
262                         {
263                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
264                                 
265                                 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
266                         }
267                         else if ( items.item( i ).nodeName() == "endviewport" )
268                         {
269                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
270                                 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
271                         }
272                 }
273         }
274         return;
275 }
276
277 QString colorToString( const QColor& c )
278 {
279         QString ret = "%1,%2,%3,%4";
280         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
281         return ret;
282 }
283
284 QString rectFToString( const QRectF& c )
285 {
286         QString ret = "%1,%2,%3,%4";
287         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
288         return ret;
289 }
290
291 QRectF stringToRect( const QString & s )
292 {
293
294         QStringList l = s.split( ',' );
295         if ( l.size() < 4 )
296                 return QRectF();
297         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
298 }
299
300 QColor stringToColor( const QString & s )
301 {
302         QStringList l = s.split( ',' );
303         if ( l.size() < 4 )
304                 return QColor();
305         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
306         ;
307 }
308 QTransform stringToTransform( const QString& s )
309 {
310         QStringList l = s.split( ',' );
311         if ( l.size() < 9 )
312                 return QTransform();
313         return QTransform(
314                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
315                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
316                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
317                );
318 }