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