kdenlivedoc.cpp 10.7 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"
30 31
#include "profilesdialog.h"
#include "kdenlivesettings.h"
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
32

33
KdenliveDoc::KdenliveDoc(const KUrl &url, MltVideoProfile profile, QUndoGroup *undoGroup, 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_commandStack(new KUndoStack(undoGroup)), m_modified(false) {
34 35 36 37 38 39 40
    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();
41 42 43 44 45 46
            QDomNode infoXmlNode = m_document.elementsByTagName("kdenlive").at(0);
            if (!infoXmlNode.isNull()) {
                QDomElement infoXml = infoXmlNode.toElement();
                QString profilePath = infoXml.attribute("profile");
                if (!profilePath.isEmpty()) setProfilePath(profilePath);
            }
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 102 103 104 105 106 107 108
            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
109

110
    }
111
    m_scenelist = m_document.toString();
112 113
    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
114 115
}

116 117 118
KdenliveDoc::~KdenliveDoc() {
    delete m_commandStack;
    delete m_clipManager;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
119 120
}

121 122 123 124 125 126 127 128 129
QDomElement KdenliveDoc::documentInfoXml() {
    QDomDocument doc;
    QDomElement addedXml = doc.createElement("kdenlive");
    addedXml.setAttribute("version", "0.7");
    addedXml.setAttribute("profile", profilePath());
    return addedXml;
}


130 131
ClipManager *KdenliveDoc::clipManager() {
    return m_clipManager;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
132 133
}

134 135 136 137 138 139
QString KdenliveDoc::getDocumentStandard() {
    //WARNING: this way to tell the video standard is a bit hackish...
    if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) return "PAL";
    return "NTSC";
}

140
QString KdenliveDoc::profilePath() const {
141 142 143
    return m_profile.path;
}

144 145 146 147 148 149 150 151 152 153
void KdenliveDoc::setProfilePath(QString path) {
    KdenliveSettings::setCurrent_profile(path);
    m_profile = ProfilesDialog::getVideoProfile(path);
    m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
    m_width = m_profile.width;
    m_height = m_profile.height;
    if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
    else m_timecode.setFormat((int) m_fps);
}

154 155
void KdenliveDoc::setThumbsProgress(KUrl url, int progress) {
    emit thumbsProgress(url, progress);
156 157
}

158 159
KUndoStack *KdenliveDoc::commandStack() {
    return m_commandStack;
160 161
}

162 163
void KdenliveDoc::setRenderer(Render *render) {
    m_render = render;
164
    if (m_render) m_render->setSceneList(m_scenelist);
165 166
}

167 168
Render *KdenliveDoc::renderer() {
    return m_render;
169 170
}

171 172
void KdenliveDoc::updateClip(int id) {
    emit updateClipDisplay(id);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
173 174
}

175 176
int KdenliveDoc::getFramePos(QString duration) {
    return m_timecode.getFrameCount(duration, m_fps);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
177 178
}

179 180 181 182 183 184 185 186 187 188 189
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;
        }
190
    }
191
    return result;
192 193
}

194 195 196 197 198 199 200 201 202
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;
        }
203 204 205
    }
}

206 207 208 209 210 211 212 213 214 215
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;
        }
216
    }
217
    return result;
218 219 220
}


221
QDomDocument KdenliveDoc::generateSceneList() {
222 223 224 225 226 227
    QDomDocument doc;
    QDomElement westley = doc.createElement("westley");
    doc.appendChild(westley);
    QDomElement prod = doc.createElement("producer");
}

228
QDomDocument KdenliveDoc::toXml() const {
229
    return m_document;
230 231
}

232
Timecode KdenliveDoc::timecode() const {
233
    return m_timecode;
234 235
}

236 237
QDomNodeList KdenliveDoc::producersList() {
    return m_document.elementsByTagName("producer");
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
238 239
}

240 241
void KdenliveDoc::backupMltPlaylist() {
    if (m_render) m_scenelist = m_render->sceneList();
242 243
}

244
double KdenliveDoc::fps() const {
245
    return m_fps;
246 247
}

248
int KdenliveDoc::width() const {
249
    return m_width;
250 251
}

252
int KdenliveDoc::height() const {
253
    return m_height;
254 255
}

256
KUrl KdenliveDoc::url() const {
257
    return m_url;
258 259
}

260 261
void KdenliveDoc::setUrl(KUrl url) {
    m_url = url;
262 263 264 265 266 267
}

void KdenliveDoc::setModified(bool mod) {
    if (mod == m_modified) return;
    m_modified = mod;
    emit docModified(m_modified);
268 269
}

270 271 272 273 274 275 276
QString KdenliveDoc::description() const {
    if (m_url.isEmpty())
        return i18n("Untitled") + " / " + m_profile.description;
    else
        return m_url.fileName() + " / " + m_profile.description;
}

277 278 279 280 281
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
282 283
}

284 285 286
void KdenliveDoc::deleteProjectClip(const uint clipId) {
    emit deletTimelineClip(clipId);
    m_clipManager->slotDeleteClip(clipId);
287
    setModified(true);
288 289
}

290 291 292
void KdenliveDoc::deleteClip(const uint clipId) {
    emit signalDeleteProjectClip(clipId);
    m_clipManager->deleteClip(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
293 294
}

295 296 297
void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group) {
    kDebug() << "/////////  DOCUM, ADD CLP: " << url;
    m_clipManager->slotAddClipFile(url, group);
298
    setModified(true);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
299 300
}

301 302
DocClipBase *KdenliveDoc::getBaseClip(int clipId) {
    return m_clipManager->getClipById(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
303 304
}

305 306
void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group) {
    m_clipManager->slotAddColorClipFile(name, color, duration, group);
307
    setModified(true);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
308 309
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
310 311
#include "kdenlivedoc.moc"