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