]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
don't block under qt-application (kdenlive)
[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 extern "C" {
34     void init_qt (const char* c){
35         titleclass=new Title(QString(c));
36     }
37     void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position){
38        titleclass->drawKdenliveTitle(buffer,width,height,position);
39        int i=0;
40        uint8_t* pointer;
41        //rotate bytes for correct order in mlt
42        for (i=0;i<width*height*4;i+=4){
43             pointer=buffer+i;
44             uint8_t a=pointer[0],r=pointer[1],g=pointer[2],b=pointer[3];
45             pointer[0]=g;//g
46             pointer[1]=r;//r
47             pointer[2]=b;//b
48             pointer[3]=a;//a
49        }
50     }
51 }
52 Title::Title(const QString& filename){
53     int argc=0;
54     char* argv[1];
55     argv[0]="xxx"; 
56     if (! QApplication::activeWindow())
57         app=new QApplication(argc,argv);
58     //must be extracted from kdenlive title
59     start =new QGraphicsPolygonItem(QPolygonF(QRectF(100, 100, 600, 600)));;
60     end=new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 300, 300)));;
61     m_scene=new QGraphicsScene;
62     loadDocument(filename,start,end);
63 }
64 void Title::drawKdenliveTitle(uint8_t * buffer ,int width,int height,double position){
65     //qDebug() << width << height;
66     QImage img((uchar*)buffer,width,height,width*4,QImage::Format_ARGB32);
67     img.fill(Qt::transparent);
68     //img.fill(66);
69     QRectF rstart=start->boundingRect();
70     QRectF rend=end->boundingRect();
71     QPointF topleft=rstart.topLeft()+(rend.topLeft()-rstart.topLeft())*position;
72     QPointF bottomRight=rstart.bottomRight()+(rend.bottomRight()-rstart.bottomRight())*position;
73     QPainter p1;
74     p1.begin(&img);
75     p1.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform );
76     m_scene->render(&p1,QRect(0,0,width,height),QRectF(topleft,bottomRight));
77     p1.end();
78     //qDebug() << img.hasAlphaChannel();
79     //img.save("test.png");
80 }
81 int Title::loadDocument(const QString& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
82 {
83     QDomDocument doc;
84     if (!m_scene)
85         return -1;
86
87         QFile file(url);
88         if (file.open(QIODevice::ReadOnly)) {
89             doc.setContent(&file, false);
90             file.close();
91         } 
92         return loadFromXml(doc, startv, endv);
93 }
94 int Title::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
95 {
96     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
97     int maxZValue = 0;
98     if (titles.size()) {
99
100         QDomNodeList items = titles.item(0).childNodes();
101         for (int i = 0; i < items.count(); i++) {
102             QGraphicsItem *gitem = NULL;
103             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
104             if (zValue > -1000) {
105                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
106                     QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
107                     QFont font(txtProperties.namedItem("font").nodeValue());
108                     font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
109                     font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
110                     font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
111                     // Older Kdenlive version did not store pixel size but point size
112                     if (txtProperties.namedItem("font-pixel-size").isNull()) {
113                         QFont f2;
114                         f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
115                         font.setPixelSize(QFontInfo(f2).pixelSize());
116                     } else
117                         font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
118                     QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
119                     QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
120                     txt->setDefaultTextColor(col);
121                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
122                     if (txtProperties.namedItem("alignment").isNull() == false) {
123                         txt->setTextWidth(txt->boundingRect().width());
124                         QTextCursor cur = txt->textCursor();
125                         QTextBlockFormat format = cur.blockFormat();
126                         format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
127                         cur.select(QTextCursor::Document);
128                         cur.setBlockFormat(format);
129                         txt->setTextCursor(cur);
130                         cur.clearSelection();
131                         txt->setTextCursor(cur);
132                     }
133
134                     if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
135                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
136                     }
137                     if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
138                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
139                     }
140
141                     gitem = txt;
142                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
143                     QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
144                     QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
145                     QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
146                     double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
147                     QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
148                     gitem = rec;
149                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
150                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
151                     QPixmap pix(url);
152                     QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
153                     rec->setData(Qt::UserRole, url);
154                     gitem = rec;
155                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
156                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
157                     //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
158                     //m_scene->addItem(rec);
159                     //rec->setData(Qt::UserRole, url);
160                     //gitem = rec;
161                 }
162             }
163             //pos and transform
164             if (gitem) {
165                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
166                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
167                 gitem->setPos(p);
168                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
169                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
170                 if (zValue > maxZValue) maxZValue = zValue;
171                 gitem->setZValue(zValue);
172                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
173             }
174             if (items.item(i).nodeName() == "background") {
175                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
176                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
177                 QList<QGraphicsItem *> items = m_scene->items();
178                 for (int i = 0; i < items.size(); i++) {
179                     if (items.at(i)->zValue() == -1100) {
180                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
181                         break;
182                     }
183                 }
184             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
185                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
186                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
187                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
188                     kDebug() << width << rect;
189                     startv->setPolygon(rect);
190                     startv->setPos(p);
191                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
192                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
193                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
194                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
195                     kDebug() << width << rect;
196                     endv->setPolygon(rect);
197                     endv->setPos(p);
198                 }*/
199         }
200     }
201     return maxZValue;
202 }
203
204 QString Title::colorToString(const QColor& c)
205 {
206     QString ret = "%1,%2,%3,%4";
207     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
208     return ret;
209 }
210
211 QString Title::rectFToString(const QRectF& c)
212 {
213     QString ret = "%1,%2,%3,%4";
214     ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
215     return ret;
216 }
217
218 QRectF Title::stringToRect(const QString & s)
219 {
220
221     QStringList l = s.split(',');
222     if (l.size() < 4)
223         return QRectF();
224     return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
225 }
226
227 QColor Title::stringToColor(const QString & s)
228 {
229     QStringList l = s.split(',');
230     if (l.size() < 4)
231         return QColor();
232     return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
233 }
234 QTransform Title::stringToTransform(const QString& s)
235 {
236     QStringList l = s.split(',');
237     if (l.size() < 9)
238         return QTransform();
239     return QTransform(
240                l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
241                l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
242                l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
243            );
244 }