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