X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Ftitledocument.cpp;h=e41abc2707e99f7761e75e89a07bb70697a35920;hb=04d9ee1d73082325557f73929600ffca12701cf3;hp=a402a195d2fd3d6a8a69f2c6b73e605b18e1c1e5;hpb=c24cb03154eece4a1213da57b6aa160100614f28;p=kdenlive diff --git a/src/titledocument.cpp b/src/titledocument.cpp index a402a195..e41abc27 100644 --- a/src/titledocument.cpp +++ b/src/titledocument.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -30,10 +31,30 @@ #include #include #include +#include +#include #include #include #include +#if QT_VERSION >= 0x040600 +#include +#include +#include +#endif + +QByteArray fileToByteArray(const QString& filename) +{ + QByteArray ret; + QFile file(filename); + if (file.open(QIODevice::ReadOnly)) + { + while (!file.atEnd()){ + ret.append(file.readLine()); + } + } + return ret; +} TitleDocument::TitleDocument() { @@ -47,7 +68,47 @@ void TitleDocument::setScene(QGraphicsScene* _scene, int width, int height) m_height = height; } -QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv) +int TitleDocument::base64ToUrl(QGraphicsItem* item, QDomElement& content, bool embed) +{ + if (embed) + { + if (!item->data(Qt::UserRole+1).toString().isEmpty()) + { + content.setAttribute("base64",item->data(Qt::UserRole+1).toString()); + } else if (!item->data(Qt::UserRole).toString().isEmpty() ) + { + content.setAttribute("base64",fileToByteArray( item->data(Qt::UserRole).toString() ).toBase64().data()); + } + content.removeAttribute("url"); + }else{ + // save for project files to disk + QString base64=item->data(Qt::UserRole+1).toString(); + if (!base64.isEmpty()){ + QString titlePath; + if (!m_projectPath.isEmpty()) + { + titlePath=m_projectPath; + }else{ + titlePath="/tmp/titles"; + } + QString filename=titlePath+QString( QCryptographicHash::hash(base64.toAscii(), QCryptographicHash::Md5).toHex().append(".titlepart")); + KStandardDirs::makeDir(titlePath); + QFile f(filename); + if (f.open(QIODevice::WriteOnly)){ + f.write(QByteArray::fromBase64(base64.toAscii()) ) ; + f.close(); + content.setAttribute("url",filename); + content.removeAttribute("base64"); + } + + } else { + return 1; + } + } + return 0; +} + +QDomDocument TitleDocument::xml(QGraphicsRectItem* startv, QGraphicsRectItem* endv, bool embed) { QDomDocument doc; @@ -66,10 +127,12 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt case 7: e.setAttribute("type", "QGraphicsPixmapItem"); content.setAttribute("url", item->data(Qt::UserRole).toString()); + base64ToUrl (item, content, embed ); break; case 13: e.setAttribute("type", "QGraphicsSvgItem"); content.setAttribute("url", item->data(Qt::UserRole).toString()); + base64ToUrl (item, content, embed ); break; case 3: e.setAttribute("type", "QGraphicsRectItem"); @@ -91,7 +154,19 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt content.setAttribute("font-pixel-size", font.pixelSize()); content.setAttribute("font-italic", font.italic()); content.setAttribute("font-underline", font.underline()); - content.setAttribute("font-color", colorToString(t->defaultTextColor())); + { + QTextCursor cursor(t->document()); + cursor.select(QTextCursor::Document); + QColor fontcolor = cursor.charFormat().foreground().color(); + content.setAttribute("font-color", colorToString(fontcolor)); + if (!t->data(101).isNull()) content.setAttribute("font-outline", t->data(101).toDouble()); + if (!t->data(102).isNull()) content.setAttribute("font-outline-color", colorToString(QColor(t->data(102).toString()))); + } + if (!t->data(100).isNull()) { + QStringList effectParams = t->data(100).toStringList(); + QString effectName = effectParams.takeFirst(); + content.setAttribute(effectName, effectParams.join(";")); + } // Only save when necessary. if (t->data(OriginXLeft).toInt() == AxisInverted) { @@ -107,11 +182,20 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt default: continue; } + + // position QDomElement pos = doc.createElement("position"); pos.setAttribute("x", item->pos().x()); pos.setAttribute("y", item->pos().y()); QTransform transform = item->transform(); QDomElement tr = doc.createElement("transform"); + if (!item->data(ZOOMFACTOR).isNull()) { + tr.setAttribute("zoom", QString::number(item->data(ZOOMFACTOR).toInt())); + } + if (!item->data(ROTATEFACTOR).isNull()) { + QList rotlist = item->data(ROTATEFACTOR).toList(); + tr.setAttribute("rotation", QString("%1,%2,%3").arg(rotlist[0].toDouble()).arg(rotlist[1].toDouble()).arg(rotlist[2].toDouble())); + } tr.appendChild(doc.createTextNode( QString("%1,%2,%3,%4,%5,%6,%7,%8,%9").arg( transform.m11()).arg(transform.m12()).arg(transform.m13()).arg(transform.m21()).arg(transform.m22()).arg(transform.m23()).arg(transform.m31()).arg(transform.m32()).arg(transform.m33()) @@ -120,26 +204,40 @@ QDomDocument TitleDocument::xml(QGraphicsPolygonItem* startv, QGraphicsPolygonIt e.setAttribute("z-index", item->zValue()); pos.appendChild(tr); +#if QT_VERSION >= 0x040600 + // effects + QGraphicsEffect *eff = item->graphicsEffect(); + if (eff) { + QGraphicsBlurEffect *blur = static_cast (eff); + QDomElement effect = doc.createElement("effect"); + if (blur) { + effect.setAttribute("type", "blur"); + effect.setAttribute("blurradius", blur->blurRadius()); + } else { + QGraphicsDropShadowEffect *shadow = static_cast (eff); + if (shadow) { + effect.setAttribute("type", "shadow"); + effect.setAttribute("blurradius", shadow->blurRadius()); + effect.setAttribute("xoffset", shadow->xOffset()); + effect.setAttribute("yoffset", shadow->yOffset()); + } + } + e.appendChild(effect); + } +#endif e.appendChild(pos); e.appendChild(content); - if (item->zValue() > -1100) main.appendChild(e); + if (item->zValue() > -1000) main.appendChild(e); } if (startv && endv) { QDomElement endp = doc.createElement("endviewport"); QDomElement startp = doc.createElement("startviewport"); - endp.setAttribute("x", endv->data(0).toString()); - endp.setAttribute("y", endv->data(1).toString()); - endp.setAttribute("size", endv->data(2).toString()); - endp.setAttribute("rect", rectFToString(endv->boundingRect())); - - startp.setAttribute("x", startv->data(0).toString()); - startp.setAttribute("y", startv->data(1).toString()); - startp.setAttribute("size", startv->data(2).toString()); - startp.setAttribute("rect", rectFToString(startv->boundingRect())); - - startp.setAttribute("z-index", startv->zValue()); - endp.setAttribute("z-index", endv->zValue()); + QRectF r(endv->pos().x(), endv->pos().y(), endv->rect().width(), endv->rect().height()); + endp.setAttribute("rect", rectFToString(r)); + QRectF r2(startv->pos().x(), startv->pos().y(), startv->rect().width(), startv->rect().height()); + startp.setAttribute("rect", rectFToString(r2)); + main.appendChild(startp); main.appendChild(endp); } @@ -169,12 +267,12 @@ QColor TitleDocument::getBackgroundColor() } -bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv, int out) +bool TitleDocument::saveDocument(const KUrl& url, QGraphicsRectItem* startv, QGraphicsRectItem* endv, int out, bool embed) { if (!m_scene) return false; - QDomDocument doc = xml(startv, endv); + QDomDocument doc = xml(startv, endv, embed); doc.documentElement().setAttribute("out", out); KTemporaryFile tmpfile; if (!tmpfile.open()) { @@ -192,8 +290,9 @@ bool TitleDocument::saveDocument(const KUrl& url, QGraphicsPolygonItem* startv, return KIO::NetAccess::upload(tmpfile.fileName(), url, 0); } -int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv, int *out) +int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsRectItem* startv, QGraphicsRectItem* endv, int *out, const QString& projectpath) { + m_projectPath=projectpath; QDomNodeList titles = doc.elementsByTagName("kdenlivetitle"); //TODO: Check if the opened title size is equal to project size, otherwise warn user and rescale if (doc.documentElement().hasAttribute("width") && doc.documentElement().hasAttribute("height")) { @@ -205,6 +304,16 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q m_width = doc_width; m_height = doc_height; } + } else { + // Document has no size info, it is likely an old version title, so ignore viewport data + QDomNodeList viewportlist = doc.documentElement().elementsByTagName("startviewport"); + if (!viewportlist.isEmpty()) { + doc.documentElement().removeChild(viewportlist.at(0)); + } + viewportlist = doc.documentElement().elementsByTagName("endviewport"); + if (!viewportlist.isEmpty()) { + doc.documentElement().removeChild(viewportlist.at(0)); + } } //TODO: get default title duration instead of hardcoded one if (doc.documentElement().hasAttribute("out")) @@ -246,7 +355,21 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q 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); + QTextCursor cursor(txt->document()); + cursor.select(QTextCursor::Document); + QTextCharFormat format; + if (txtProperties.namedItem("font-outline").nodeValue().toDouble() > 0.0) { + txt->setData(101, txtProperties.namedItem("font-outline").nodeValue().toDouble()); + txt->setData(102, stringToColor(txtProperties.namedItem("font-outline-color").nodeValue())); + format.setTextOutline( + QPen(QColor(stringToColor(txtProperties.namedItem("font-outline-color").nodeValue())), + txtProperties.namedItem("font-outline").nodeValue().toDouble(), + Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin) + ); + + } + format.setForeground(QBrush(col)); + cursor.mergeCharFormat(format); txt->setTextInteractionFlags(Qt::NoTextInteraction); if (txtProperties.namedItem("alignment").isNull() == false) { txt->setTextWidth(txt->boundingRect().width()); @@ -267,26 +390,56 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt()); } + // Effects + if (!txtProperties.namedItem("typewriter").isNull()) { + QStringList effData = QStringList() << "typewriter" << txtProperties.namedItem("typewriter").nodeValue(); + txt->setData(100, effData); + } + 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))); + QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin), 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); + QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue(); + QPixmap pix; + if (base64.isEmpty()){ + pix.load(url); + }else{ + pix.loadFromData(QByteArray::fromBase64(base64.toAscii())); + } QGraphicsPixmapItem *rec = m_scene->addPixmap(pix); rec->setData(Qt::UserRole, url); + if (!base64.isEmpty()){ + rec->setData(Qt::UserRole+1, base64); + } 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; + QString base64 = items.item(i).namedItem("content").attributes().namedItem("base64").nodeValue(); + QGraphicsSvgItem *rec = NULL; + if (base64.isEmpty()){ + rec = new QGraphicsSvgItem(url); + }else{ + rec = new QGraphicsSvgItem(); + QSvgRenderer *renderer= new QSvgRenderer(QByteArray::fromBase64(base64.toAscii()), rec ); + rec->setSharedRenderer(renderer); + //QString elem=rec->elementId(); + //QRectF bounds = renderer->boundsOnElement(elem); + } + if (rec){ + m_scene->addItem(rec); + rec->setData(Qt::UserRole, url); + if (!base64.isEmpty()){ + rec->setData(Qt::UserRole+1, base64); + } + gitem = rec; + } } } //pos and transform @@ -294,12 +447,36 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q 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())); + QDomElement trans = items.item(i).namedItem("position").firstChild().toElement(); + gitem->setTransform(stringToTransform(trans.firstChild().nodeValue())); + QString rotate = trans.attribute("rotation"); + if (!rotate.isEmpty()) gitem->setData(ROTATEFACTOR, stringToList(rotate)); + QString zoom = trans.attribute("zoom"); + if (!zoom.isEmpty()) gitem->setData(ZOOMFACTOR, zoom.toInt()); 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 QT_VERSION >= 0x040600 + // effects + QDomNode eff = items.item(i).namedItem("effect"); + if (!eff.isNull()) { + QDomElement e = eff.toElement(); + if (e.attribute("type") == "blur") { + QGraphicsBlurEffect *blur = new QGraphicsBlurEffect(); + blur->setBlurRadius(e.attribute("blurradius").toInt()); + gitem->setGraphicsEffect(blur); + } else if (e.attribute("type") == "shadow") { + QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(); + shadow->setBlurRadius(e.attribute("blurradius").toInt()); + shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt()); + gitem->setGraphicsEffect(shadow); + } + } +#endif } + if (items.item(i).nodeName() == "background") { kDebug() << items.item(i).attributes().namedItem("color").nodeValue(); QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue())); @@ -313,23 +490,14 @@ int TitleDocument::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* startv, Q } } else if (items.item(i).nodeName() == "startviewport" && startv) { QString rect = items.item(i).attributes().namedItem("rect").nodeValue(); - startv->setPolygon(stringToRect(rect)); - int x = items.item(i).attributes().namedItem("x").nodeValue().toInt(); - int y = items.item(i).attributes().namedItem("y").nodeValue().toInt(); - int size = items.item(i).attributes().namedItem("size").nodeValue().toInt(); - startv->setData(0, x); - startv->setData(1, y); - startv->setData(2, size); - //startv->setPos(p); + QRectF r = stringToRect(rect); + startv->setRect(0, 0, r.width(), r.height()); + startv->setPos(r.topLeft()); } else if (items.item(i).nodeName() == "endviewport" && endv) { QString rect = items.item(i).attributes().namedItem("rect").nodeValue(); - endv->setPolygon(stringToRect(rect)); - int x = items.item(i).attributes().namedItem("x").nodeValue().toInt(); - int y = items.item(i).attributes().namedItem("y").nodeValue().toInt(); - int size = items.item(i).attributes().namedItem("size").nodeValue().toInt(); - endv->setData(0, x); - endv->setData(1, y); - endv->setData(2, size); + QRectF r = stringToRect(rect); + endv->setRect(0, 0, r.width(), r.height()); + endv->setPos(r.topLeft()); } } } @@ -366,6 +534,7 @@ QColor TitleDocument::stringToColor(const QString & s) return QColor(); return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());; } + QTransform TitleDocument::stringToTransform(const QString& s) { QStringList l = s.split(','); @@ -378,6 +547,14 @@ QTransform TitleDocument::stringToTransform(const QString& s) ); } +QList TitleDocument::stringToList(const QString & s) +{ + QStringList l = s.split(','); + if (l.size() < 3) + return QList(); + return QList() << QVariant(l.at(0).toDouble()) << QVariant(l.at(1).toDouble()) << QVariant(l.at(2).toDouble()); +} + int TitleDocument::frameWidth() const { return m_width;