kdenlivedoc.cpp 9.61 KB
Newer Older
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1
/***************************************************************************
2
 *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
3 4 5 6 7 8
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
9 10 11 12 13 14 15 16 17
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
18 19 20 21 22 23 24 25 26 27 28
 ***************************************************************************/

#include <KDebug>
#include <KStandardDirs>
#include <KMessageBox>
#include <KLocale>
#include <KFileDialog>
#include <KIO/NetAccess>


#include "kdenlivedoc.h"
29
#include "docclipbase.h"
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
30

31
KdenliveDoc::KdenliveDoc(const KUrl &url, MltVideoProfile profile, QWidget *parent): QObject(parent), m_render(NULL), m_url(url), m_profile(profile), m_fps((double)profile.frame_rate_num / profile.frame_rate_den), m_width(profile.width), m_height(profile.height), m_projectName(NULL), m_commandStack(new KUndoStack()) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    m_clipManager = new ClipManager(this);
    if (!url.isEmpty()) {
        QString tmpFile;
        if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
            QFile file(tmpFile);
            m_document.setContent(&file, false);
            file.close();
            m_projectName = url.fileName();
            KIO::NetAccess::removeTempFile(tmpFile);
        } else {
            KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
        }
    } else {
        // Creating new document
        QDomElement westley = m_document.createElement("westley");
        m_document.appendChild(westley);
        QDomElement doc = m_document.createElement("kdenlivedoc");
        doc.setAttribute("version", "0.6");
        westley.appendChild(doc);
        QDomElement props = m_document.createElement("properties");
        doc.setAttribute("width", m_width);
        doc.setAttribute("height", m_height);
        doc.setAttribute("projectfps", m_fps);
        doc.appendChild(props);


        /*QDomElement westley = m_document.createElement("westley");
        m_document.appendChild(westley);*/


        QDomElement tractor = m_document.createElement("tractor");
        QDomElement multitrack = m_document.createElement("multitrack");
        QDomElement playlist = m_document.createElement("playlist");
        QDomElement producer = m_document.createElement("producer");
        /*producer.setAttribute("mlt_service", "colour");
        producer.setAttribute("colour", "red");
        playlist.appendChild(producer);*/
        multitrack.appendChild(playlist);
        QDomElement playlist1 = m_document.createElement("playlist");
        playlist1.setAttribute("id", "playlist1");
        playlist1.setAttribute("hide", "video");
        multitrack.appendChild(playlist1);
        QDomElement playlist2 = m_document.createElement("playlist");
        playlist2.setAttribute("id", "playlist2");
        playlist2.setAttribute("hide", "video");
        multitrack.appendChild(playlist2);
        QDomElement playlist3 = m_document.createElement("playlist");
        multitrack.appendChild(playlist3);
        playlist3.setAttribute("id", "playlist3");
        QDomElement playlist4 = m_document.createElement("playlist");
        multitrack.appendChild(playlist4);
        playlist4.setAttribute("id", "playlist4");
        QDomElement playlist5 = m_document.createElement("playlist");
        multitrack.appendChild(playlist5);
        playlist5.setAttribute("id", "playlist5");
        tractor.appendChild(multitrack);

        for (uint i = 2; i < 6 ; i++) {
            QDomElement transition = m_document.createElement("transition");
            transition.setAttribute("in", "0");
            //TODO: Make audio mix last for all project duration
            transition.setAttribute("out", "15000");
            transition.setAttribute("a_track", QString::number(1));
            transition.setAttribute("b_track", QString::number(i));
            transition.setAttribute("mlt_service", "mix");
            transition.setAttribute("combine", "1");
            tractor.appendChild(transition);
        }

        doc.appendChild(tractor);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
102

103
    }
104 105
    if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
    else m_timecode.setFormat((int) m_fps);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
106 107
}

108 109 110
KdenliveDoc::~KdenliveDoc() {
    delete m_commandStack;
    delete m_clipManager;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
111 112
}

113 114
ClipManager *KdenliveDoc::clipManager() {
    return m_clipManager;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
115 116
}

117 118 119 120
QString KdenliveDoc::profilePath() {
    return m_profile.path;
}

121 122
void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
    emit thumbsProgress(url, progress);
123 124
}

125 126
KUndoStack *KdenliveDoc::commandStack() {
    return m_commandStack;
127 128
}

129 130 131
void KdenliveDoc::setRenderer(Render *render) {
    m_render = render;
    if (m_render) m_render->setSceneList(m_document);
132 133
}

134 135
Render *KdenliveDoc::renderer() {
    return m_render;
136 137
}

138 139
void KdenliveDoc::updateClip(int id) {
    emit updateClipDisplay(id);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
140 141
}

142 143
int KdenliveDoc::getFramePos(QString duration) {
    return m_timecode.getFrameCount(duration, m_fps);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
144 145
}

146 147 148 149 150 151 152 153 154 155 156
QString KdenliveDoc::producerName(int id) {
    QString result = "unnamed";
    QDomNodeList prods = producersList();
    int ct = prods.count();
    for (int i = 0; i <  ct ; i++) {
        QDomElement e = prods.item(i).toElement();
        if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
            result = e.attribute("name");
            if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
            break;
        }
157
    }
158
    return result;
159 160
}

161 162 163 164 165 166 167 168 169
void KdenliveDoc::setProducerDuration(int id, int duration) {
    QDomNodeList prods = producersList();
    int ct = prods.count();
    for (int i = 0; i <  ct ; i++) {
        QDomElement e = prods.item(i).toElement();
        if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
            e.setAttribute("duration", QString::number(duration));
            break;
        }
170 171 172
    }
}

173 174 175 176 177 178 179 180 181 182
int KdenliveDoc::getProducerDuration(int id) {
    int result = 0;
    QDomNodeList prods = producersList();
    int ct = prods.count();
    for (int i = 0; i <  ct ; i++) {
        QDomElement e = prods.item(i).toElement();
        if (e.attribute("id") != "black" && e.attribute("id").toInt() == id) {
            result = e.attribute("duration").toInt();
            break;
        }
183
    }
184
    return result;
185 186 187
}


188
QDomDocument KdenliveDoc::generateSceneList() {
189 190 191 192 193 194
    QDomDocument doc;
    QDomElement westley = doc.createElement("westley");
    doc.appendChild(westley);
    QDomElement prod = doc.createElement("producer");
}

195 196
QDomDocument KdenliveDoc::toXml() {
    return m_document;
197 198
}

199 200
Timecode KdenliveDoc::timecode() {
    return m_timecode;
201 202
}

203 204
QString KdenliveDoc::documentName() {
    return m_projectName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
205 206
}

207 208
QDomNodeList KdenliveDoc::producersList() {
    return m_document.elementsByTagName("producer");
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
void KdenliveDoc::setProducers(QDomElement doc) {
    QDomNode kdenlivedocument = m_document.elementsByTagName("kdenlivedoc").item(0);

    QDomNodeList list = m_document.elementsByTagName("producer");
    int ct = list.count();
    kDebug() << "DELETING CHILD PRODUCERS: " << ct;
    for (int i = ct; i > 0; i--) {
        kdenlivedocument.removeChild(list.item(i));
    }

    QDomNode n = doc.firstChild();
    ct = 0;
    while (!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if (!e.isNull() && e.tagName() == "producer") {
            kdenlivedocument.appendChild(m_document.importNode(e, true));
            ct++;
        }
        n = n.nextSibling();
    }
    kDebug() << "ADDING CHILD PRODS: " << ct << "\n";
    //kDebug()<<m_document.toString();
233 234
}

235 236
double KdenliveDoc::fps() {
    return m_fps;
237 238
}

239 240
int KdenliveDoc::width() {
    return m_width;
241 242
}

243 244
int KdenliveDoc::height() {
    return m_height;
245 246
}

247 248
KUrl KdenliveDoc::url() {
    return m_url;
249 250
}

251 252 253 254 255
void KdenliveDoc::addClip(const QDomElement &elem, const int clipId) {
    kDebug() << "/////////  DOCUM, CREATING NEW CLIP, ID:" << clipId;
    DocClipBase *clip = new DocClipBase(m_clipManager, elem, clipId);
    m_clipManager->addClip(clip);
    emit addProjectClip(clip);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
256 257
}

258 259 260
void KdenliveDoc::deleteProjectClip(const uint clipId) {
    emit deletTimelineClip(clipId);
    m_clipManager->slotDeleteClip(clipId);
261 262
}

263 264 265
void KdenliveDoc::deleteClip(const uint clipId) {
    emit signalDeleteProjectClip(clipId);
    m_clipManager->deleteClip(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
266 267
}

268 269 270
void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group) {
    kDebug() << "/////////  DOCUM, ADD CLP: " << url;
    m_clipManager->slotAddClipFile(url, group);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
271 272
}

273 274
DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
    return m_clipManager->getClipById(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
275 276
}

277 278
void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group) {
    m_clipManager->slotAddColorClipFile(name, color, duration, group);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
279 280
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
281 282
#include "kdenlivedoc.moc"