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