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