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