Skip to content

Commit

Permalink
Refactor the Playlist QUndoCommands to separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddennedy committed Jan 27, 2015
1 parent f35fe71 commit b70d383
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 256 deletions.
151 changes: 151 additions & 0 deletions src/commands/playlistcommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2013-2015 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#include "playlistcommands.h"
#include "mltcontroller.h"
#include "mainwindow.h"

namespace Playlist
{

AppendCommand::AppendCommand(PlaylistModel& model, const QString& xml, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_xml(xml)
{
setText(QObject::tr("Append playlist item %1").arg(m_model.rowCount() + 1));
}

void AppendCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.append(producer);
}

void AppendCommand::undo()
{
m_model.remove(m_model.rowCount() - 1);
}

InsertCommand::InsertCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_xml(xml)
, m_row(row)
{
setText(QObject::tr("Insert playist item %1").arg(row + 1));
}

void InsertCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.insert(producer, m_row);
}

void InsertCommand::undo()
{
m_model.remove(m_row);
}

UpdateCommand::UpdateCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_newXml(xml)
, m_row(row)
{
setText(QObject::tr("Update playlist item %1").arg(row + 1));
m_oldXml = MLT.XML(m_model.playlist()->get_clip(m_row));
}

void UpdateCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_newXml.toUtf8().constData());
m_model.update(m_row, producer);
}

void UpdateCommand::undo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_oldXml.toUtf8().constData());
m_model.update(m_row, producer);
}

RemoveCommand::RemoveCommand(PlaylistModel& model, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_row(row)
{
m_xml = MLT.XML(m_model.playlist()->get_clip(m_row));
setText(QObject::tr("Remove playlist item %1").arg(row + 1));
}

void RemoveCommand::redo()
{
m_model.remove(m_row);
}

void RemoveCommand::undo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.insert(producer, m_row);
}

ClearCommand::ClearCommand(PlaylistModel& model, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
{
m_xml = MLT.XML(m_model.playlist());
setText(QObject::tr("Clear playlist"));
}

void ClearCommand::redo()
{
m_model.clear();
}

void ClearCommand::undo()
{
m_model.close();
Mlt::Producer* producer = new Mlt::Producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
if (producer->is_valid()) {
producer->set("resource", "<playlist>");
MAIN.open(producer);
MLT.pause();
MAIN.seekPlaylist(0);
}
}

MoveCommand::MoveCommand(PlaylistModel &model, int from, int to, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_from(from)
, m_to(to)
{
setText(QObject::tr("Move item from %1 to %2").arg(from + 1).arg(to + 1));
}

void MoveCommand::redo()
{
m_model.move(m_from, m_to);
}

void MoveCommand::undo()
{
m_model.move(m_to, m_from);
}

} // namespace Playlist
102 changes: 102 additions & 0 deletions src/commands/playlistcommands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2013-2015 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#ifndef PLAYLISTCOMMANDS_H
#define PLAYLISTCOMMANDS_H

#include "models/playlistmodel.h"
#include <QUndoCommand>
#include <QString>

namespace Playlist
{

class AppendCommand : public QUndoCommand
{
public:
AppendCommand(PlaylistModel& model, const QString& xml, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
QString m_xml;
};

class InsertCommand : public QUndoCommand
{
public:
InsertCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
QString m_xml;
int m_row;
};

class UpdateCommand : public QUndoCommand
{
public:
UpdateCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
QString m_newXml;
QString m_oldXml;
int m_row;
};

class RemoveCommand : public QUndoCommand
{
public:
RemoveCommand(PlaylistModel& model, int row, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
QString m_xml;
int m_row;
};

class MoveCommand : public QUndoCommand
{
public:
MoveCommand(PlaylistModel& model, int from, int to, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
int m_from;
int m_to;
};

class ClearCommand : public QUndoCommand
{
public:
ClearCommand(PlaylistModel& model, QUndoCommand * parent = 0);
void redo();
void undo();
private:
PlaylistModel& m_model;
QString m_xml;
};

}

#endif // PLAYLISTCOMMANDS_H
131 changes: 1 addition & 130 deletions src/docks/playlistdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "dialogs/durationdialog.h"
#include "mainwindow.h"
#include "settings.h"
#include <commands/playlistcommands.h>
#include <QMenu>
#include <QDebug>

Expand Down Expand Up @@ -420,136 +421,6 @@ void PlaylistDock::on_addButton_clicked()
on_actionAppendCut_triggered();
}

namespace Playlist
{

AppendCommand::AppendCommand(PlaylistModel& model, const QString& xml, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_xml(xml)
{
setText(QObject::tr("Append playlist item %1").arg(m_model.rowCount() + 1));
}

void AppendCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.append(producer);
}

void AppendCommand::undo()
{
m_model.remove(m_model.rowCount() - 1);
}

InsertCommand::InsertCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_xml(xml)
, m_row(row)
{
setText(QObject::tr("Insert playist item %1").arg(row + 1));
}

void InsertCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.insert(producer, m_row);
}

void InsertCommand::undo()
{
m_model.remove(m_row);
}

UpdateCommand::UpdateCommand(PlaylistModel& model, const QString& xml, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_newXml(xml)
, m_row(row)
{
setText(QObject::tr("Update playlist item %1").arg(row + 1));
m_oldXml = MLT.XML(m_model.playlist()->get_clip(m_row));
}

void UpdateCommand::redo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_newXml.toUtf8().constData());
m_model.update(m_row, producer);
}

void UpdateCommand::undo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_oldXml.toUtf8().constData());
m_model.update(m_row, producer);
}

RemoveCommand::RemoveCommand(PlaylistModel& model, int row, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_row(row)
{
m_xml = MLT.XML(m_model.playlist()->get_clip(m_row));
setText(QObject::tr("Remove playlist item %1").arg(row + 1));
}

void RemoveCommand::redo()
{
m_model.remove(m_row);
}

void RemoveCommand::undo()
{
Mlt::Producer producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
m_model.insert(producer, m_row);
}

ClearCommand::ClearCommand(PlaylistModel& model, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
{
m_xml = MLT.XML(m_model.playlist());
setText(QObject::tr("Clear playlist"));
}

void ClearCommand::redo()
{
m_model.clear();
}

void ClearCommand::undo()
{
m_model.close();
Mlt::Producer* producer = new Mlt::Producer(MLT.profile(), "xml-string", m_xml.toUtf8().constData());
if (producer->is_valid()) {
producer->set("resource", "<playlist>");
MAIN.open(producer);
MLT.pause();
MAIN.seekPlaylist(0);
}
}

MoveCommand::MoveCommand(PlaylistModel &model, int from, int to, QUndoCommand *parent)
: QUndoCommand(parent)
, m_model(model)
, m_from(from)
, m_to(to)
{
setText(QObject::tr("Move item from %1 to %2").arg(from + 1).arg(to + 1));
}

void MoveCommand::redo()
{
m_model.move(m_from, m_to);
}

void MoveCommand::undo()
{
m_model.move(m_to, m_from);
}

} // namespace Playlist

void PlaylistDock::on_actionThumbnailsHidden_triggered(bool checked)
{
if (checked) {
Expand Down

0 comments on commit b70d383

Please sign in to comment.