]> git.sesse.net Git - mlt/blobdiff - src/modules/qimage/kdenlivetitle_wrapper.cpp
Add feature to dynamically replace text in a block
[mlt] / src / modules / qimage / kdenlivetitle_wrapper.cpp
index baf325279323f648a4f914032fc591f2efba396c..0e7d4f41a338a87e223fad7e2a2e7eaf8e4e60fc 100644 (file)
 #include <QtGui/QTextCursor>
 #include "kdenlivetitle_wrapper.h"
 #include <framework/mlt_producer.h>
-extern "C" {
-    void init_qt (const char* c){
-        titleclass=new Title(QString(c));
-    }
-    void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position){
-       titleclass->drawKdenliveTitle(buffer,width,height,position);
-       int i=0;
-       uint8_t* pointer;
-       //rotate bytes for correct order in mlt
-       for (i=0;i<width*height*4;i+=4){
-            pointer=buffer+i;
-            uint8_t a=pointer[0],r=pointer[1],g=pointer[2],b=pointer[3];
-            pointer[0]=g;//g
-            pointer[1]=r;//r
-            pointer[2]=b;//b
-            pointer[3]=a;//a
-       }
-    }
-}
-Title::Title(const QString& filename){
-    int argc=0;
-    char* argv[1];
-    argv[0]="xxx"; 
-    if (! QApplication::activeWindow())
-        app=new QApplication(argc,argv);
-    //must be extracted from kdenlive title
-    start =new QGraphicsPolygonItem(QPolygonF(QRectF(100, 100, 600, 600)));;
-    end=new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 300, 300)));;
-    m_scene=new QGraphicsScene;
-    loadDocument(filename,start,end);
+
+static Title* titleclass;
+static QApplication *app;
+
+extern "C"
+{
+       void init_qt( const char* c )
+       {
+               titleclass=new Title( QString( c ) );
+       }
+       void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position, char *templatexml, char *templatetext  )
+       {
+               titleclass->drawKdenliveTitle( buffer, width, height, position, templatexml, templatetext );
+       }
 }
-void Title::drawKdenliveTitle(uint8_t * buffer ,int width,int height,double position){
-    //qDebug() << width << height;
-    QImage img((uchar*)buffer,width,height,width*4,QImage::Format_ARGB32);
-    img.fill(Qt::transparent);
-    //img.fill(66);
-    QRectF rstart=start->boundingRect();
-    QRectF rend=end->boundingRect();
-    QPointF topleft=rstart.topLeft()+(rend.topLeft()-rstart.topLeft())*position;
-    QPointF bottomRight=rstart.bottomRight()+(rend.bottomRight()-rstart.bottomRight())*position;
-    QPainter p1;
-    p1.begin(&img);
-    p1.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform );
-    m_scene->render(&p1,QRect(0,0,width,height),QRectF(topleft,bottomRight));
-    p1.end();
-    //qDebug() << img.hasAlphaChannel();
-    //img.save("test.png");
+Title::Title( const QString& filename ):m_filename( filename ),m_scene( NULL )
+{
+       //must be extracted from kdenlive title
+       start =new QGraphicsPolygonItem( QPolygonF( QRectF( 100, 100, 600, 600 ) ) );
+       ;
+       end=new QGraphicsPolygonItem( QPolygonF( QRectF( 0, 0, 300, 300 ) ) );
+       ;
 }
-int Title::loadDocument(const QString& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
+void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, char *templatexml, char *templatetext )
 {
-    QDomDocument doc;
-    if (!m_scene)
-        return -1;
+       if ( !m_scene )
+       {
+               int argc=0;
+               char* argv[1];
+               argv[0]="xxx";
+               if ( ! QApplication::activeWindow() )
+                       //if (!app)
+                       app=new QApplication( argc,argv );
+               m_scene=new QGraphicsScene;
+               loadDocument( m_filename, start, end, QString(templatexml), QString(templatetext) );
+       }
+       //must be extracted from kdenlive title
 
-        QFile file(url);
-        if (file.open(QIODevice::ReadOnly)) {
-            doc.setContent(&file, false);
-            file.close();
-        } 
-        return loadFromXml(doc, startv, endv);
+       QImage *img=new QImage( width,height,QImage::Format_ARGB32 );
+       img->fill( 0 );
+       QRectF rstart=start->boundingRect();
+       QRectF rend=end->boundingRect();
+       QPointF topleft=rstart.topLeft()+( rend.topLeft()-rstart.topLeft() )*position;
+       QPointF bottomRight=rstart.bottomRight()+( rend.bottomRight()-rstart.bottomRight() )*position;
+       QPainter p1;
+       p1.begin( img );
+       p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );//|QPainter::SmoothPixmapTransform );
+       m_scene->render( &p1,QRect( 0,0,width,height ),QRectF( topleft,bottomRight ) );
+       p1.end();
+       uint8_t *pointer=img->bits();
+       QRgb* src = ( QRgb* ) pointer;
+       for ( int i=0;i<width*height*4;i+=4 )
+       {
+               *buffer++=qRed( *src );
+               *buffer++=qGreen( *src );
+               *buffer++=qBlue( *src );
+               *buffer++=qAlpha( *src );
+               src++;
+       }
+       delete img;
+}
+int Title::loadDocument( const QString& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv, const QString templateXml, const QString templateText )
+{
+       QDomDocument doc;
+       if ( !m_scene )
+               return -1;
+       if (!templateXml.isEmpty()) {
+               doc.setContent(templateXml);
+       }
+       else {
+               QFile file( url );
+               if ( file.open( QIODevice::ReadOnly ) )
+               {
+                       doc.setContent( &file, false );
+                       file.close();
+               }
+       }
+       return loadFromXml( doc, startv, endv, templateText );
 }
-int Title::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
+
+int Title::loadFromXml( QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv, const QString templateText )
 {
-    QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
-    int maxZValue = 0;
-    if (titles.size()) {
+       QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
+       int maxZValue = 0;
+       if ( titles.size() )
+       {
 
-        QDomNodeList items = titles.item(0).childNodes();
-        for (int i = 0; i < items.count(); i++) {
-            QGraphicsItem *gitem = NULL;
-            int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
-            if (zValue > -1000) {
-                if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
-                    QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
-                    QFont font(txtProperties.namedItem("font").nodeValue());
-                    font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
-                    font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
-                    font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
-                    // Older Kdenlive version did not store pixel size but point size
-                    if (txtProperties.namedItem("font-pixel-size").isNull()) {
-                        QFont f2;
-                        f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
-                        font.setPixelSize(QFontInfo(f2).pixelSize());
-                    } else
-                        font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
-                    QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
-                    QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
-                    txt->setDefaultTextColor(col);
-                    txt->setTextInteractionFlags(Qt::NoTextInteraction);
-                    if (txtProperties.namedItem("alignment").isNull() == false) {
-                        txt->setTextWidth(txt->boundingRect().width());
-                        QTextCursor cur = txt->textCursor();
-                        QTextBlockFormat format = cur.blockFormat();
-                        format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
-                        cur.select(QTextCursor::Document);
-                        cur.setBlockFormat(format);
-                        txt->setTextCursor(cur);
-                        cur.clearSelection();
-                        txt->setTextCursor(cur);
-                    }
+               QDomNodeList items = titles.item( 0 ).childNodes();
+               for ( int i = 0; i < items.count(); i++ )
+               {
+                       QGraphicsItem *gitem = NULL;
+                       int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
+                       if ( zValue > -1000 )
+                       {
+                               if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
+                               {
+                                       QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
+                                       QFont font( txtProperties.namedItem( "font" ).nodeValue() );
+                                       font.setBold( txtProperties.namedItem( "font-bold" ).nodeValue().toInt() );
+                                       font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
+                                       font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
+                                       // Older Kdenlive version did not store pixel size but point size
+                                       if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
+                                       {
+                                               QFont f2;
+                                               f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
+                                               font.setPixelSize( QFontInfo( f2 ).pixelSize() );
+                                       }
+                                       else
+                                               font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
+                                       QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
+                                       QGraphicsTextItem *txt;
+                                       if (!templateText.isEmpty()) {
+                                               QString text = items.item(i).namedItem("content").firstChild().nodeValue();
+                                               text = text.replace("%s", templateText);
+                                               txt = m_scene->addText(text, font);
+                                       }
+                                       else txt = m_scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
+                                       txt->setDefaultTextColor( col );
+                                       txt->setTextInteractionFlags( Qt::NoTextInteraction );
+                                       if ( txtProperties.namedItem( "alignment" ).isNull() == false )
+                                       {
+                                               txt->setTextWidth( txt->boundingRect().width() );
+                                               QTextCursor cur = txt->textCursor();
+                                               QTextBlockFormat format = cur.blockFormat();
+                                               format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
+                                               cur.select( QTextCursor::Document );
+                                               cur.setBlockFormat( format );
+                                               txt->setTextCursor( cur );
+                                               cur.clearSelection();
+                                               txt->setTextCursor( cur );
+                                       }
 
-                    if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
-                        //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
-                    }
-                    if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
-                        //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
-                    }
+                                       if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
+                                       {
+                                               //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
+                                       }
+                                       if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
+                                       {
+                                               //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
+                                       }
 
-                    gitem = txt;
-                } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
-                    QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
-                    QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
-                    QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
-                    double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
-                    QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
-                    gitem = rec;
-                } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
-                    QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
-                    QPixmap pix(url);
-                    QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
-                    rec->setData(Qt::UserRole, url);
-                    gitem = rec;
-                } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
-                    QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
-                    //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
-                    //m_scene->addItem(rec);
-                    //rec->setData(Qt::UserRole, url);
-                    //gitem = rec;
-                }
-            }
-            //pos and transform
-            if (gitem) {
-                QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
-                          items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
-                gitem->setPos(p);
-                gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
-                int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
-                if (zValue > maxZValue) maxZValue = zValue;
-                gitem->setZValue(zValue);
-                gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
-            }
-            if (items.item(i).nodeName() == "background") {
-                QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
-                //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
-                QList<QGraphicsItem *> items = m_scene->items();
-                for (int i = 0; i < items.size(); i++) {
-                    if (items.at(i)->zValue() == -1100) {
-                        ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
-                        break;
-                    }
-                }
-            } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
-                    QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
-                    double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
-                    QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
-                    kDebug() << width << rect;
-                    startv->setPolygon(rect);
-                    startv->setPos(p);
-                } else if (items.item(i).nodeName() == "endviewport" && endv) {
-                    QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
-                    double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
-                    QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
-                    kDebug() << width << rect;
-                    endv->setPolygon(rect);
-                    endv->setPos(p);
-                }*/
-        }
-    }
-    return maxZValue;
+                                       gitem = txt;
+                               }
+                               else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
+                               {
+                                       QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
+                                       QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
+                                       QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
+                                       double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
+                                       QGraphicsRectItem *rec = m_scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
+                                       gitem = rec;
+                               }
+                               else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
+                               {
+                                       QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
+                                       QPixmap pix( url );
+                                       QGraphicsPixmapItem *rec = m_scene->addPixmap( pix );
+                                       rec->setData( Qt::UserRole, url );
+                                       gitem = rec;
+                               }
+                               else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
+                               {
+                                       QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
+                                       //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
+                                       //m_scene->addItem(rec);
+                                       //rec->setData(Qt::UserRole, url);
+                                       //gitem = rec;
+                               }
+                       }
+                       //pos and transform
+                       if ( gitem )
+                       {
+                               QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
+                                          items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
+                               gitem->setPos( p );
+                               gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
+                               int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
+                               if ( zValue > maxZValue ) maxZValue = zValue;
+                               gitem->setZValue( zValue );
+                               //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+                       }
+                       if ( items.item( i ).nodeName() == "background" )
+                       {
+                               QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
+                               //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
+                               QList<QGraphicsItem *> items = m_scene->items();
+                               for ( int i = 0; i < items.size(); i++ )
+                               {
+                                       if ( items.at( i )->zValue() == -1100 )
+                                       {
+                                               (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
+                                               break;
+                                       }
+                               }
+                       }
+                       else if ( items.item( i ).nodeName() == "startviewport" && startv )
+                       {
+                               QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
+                               startv->setPolygon( stringToRect( rect ) );
+                       }
+                       else if ( items.item( i ).nodeName() == "endviewport" && endv )
+                       {
+                               QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
+                               endv->setPolygon( stringToRect( rect ) );
+                       }
+               }
+       }
+       return maxZValue;
 }
 
-QString Title::colorToString(const QColor& c)
+QString Title::colorToString( const QColor& c )
 {
-    QString ret = "%1,%2,%3,%4";
-    ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
-    return ret;
+       QString ret = "%1,%2,%3,%4";
+       ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
+       return ret;
 }
 
-QString Title::rectFToString(const QRectF& c)
+QString Title::rectFToString( const QRectF& c )
 {
-    QString ret = "%1,%2,%3,%4";
-    ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
-    return ret;
+       QString ret = "%1,%2,%3,%4";
+       ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
+       return ret;
 }
 
-QRectF Title::stringToRect(const QString & s)
+QRectF Title::stringToRect( const QString & s )
 {
 
-    QStringList l = s.split(',');
-    if (l.size() < 4)
-        return QRectF();
-    return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
+       QStringList l = s.split( ',' );
+       if ( l.size() < 4 )
+               return QRectF();
+       return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
 }
 
-QColor Title::stringToColor(const QString & s)
+QColor Title::stringToColor( const QString & s )
 {
-    QStringList l = s.split(',');
-    if (l.size() < 4)
-        return QColor();
-    return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
+       QStringList l = s.split( ',' );
+       if ( l.size() < 4 )
+               return QColor();
+       return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
+       ;
 }
-QTransform Title::stringToTransform(const QString& s)
+QTransform Title::stringToTransform( const QString& s )
 {
-    QStringList l = s.split(',');
-    if (l.size() < 9)
-        return QTransform();
-    return QTransform(
-               l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
-               l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
-               l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
-           );
+       QStringList l = s.split( ',' );
+       if ( l.size() < 9 )
+               return QTransform();
+       return QTransform(
+                  l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
+                  l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
+                  l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
+              );
 }