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