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