2010-02-09 09:44:24 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Paul Davis
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-03-01 19:00:00 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-02-09 09:44:24 -05:00
|
|
|
#include "pbd/stateful_diff_command.h"
|
2010-03-01 19:00:00 -05:00
|
|
|
#include "pbd/property_list.h"
|
2010-03-02 14:12:01 -05:00
|
|
|
#include "pbd/demangle.h"
|
2010-02-09 17:28:46 -05:00
|
|
|
#include "i18n.h"
|
2010-02-09 09:44:24 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
|
|
|
|
* since the last time that clear_history was called on it.
|
|
|
|
* @param s Stateful object.
|
|
|
|
*/
|
|
|
|
|
2010-06-23 16:14:07 -04:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s)
|
|
|
|
: _object (s)
|
2010-03-31 21:07:24 -04:00
|
|
|
, _undo (new PropertyList)
|
|
|
|
, _redo (new PropertyList)
|
2010-02-09 09:44:24 -05:00
|
|
|
{
|
2010-06-23 16:14:07 -04:00
|
|
|
s->diff (*_undo, *_redo, this);
|
|
|
|
|
|
|
|
/* if the stateful object that this command refers to goes away,
|
|
|
|
be sure to notify owners of this command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
2010-06-23 16:14:07 -04:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s, XMLNode const & n)
|
2010-02-09 17:28:46 -05:00
|
|
|
: _object (s)
|
2010-03-31 21:07:24 -04:00
|
|
|
, _undo (0)
|
|
|
|
, _redo (0)
|
2010-02-09 17:28:46 -05:00
|
|
|
{
|
2010-03-01 19:00:00 -05:00
|
|
|
const XMLNodeList& children (n.children());
|
|
|
|
|
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
|
|
|
if ((*i)->name() == X_("Undo")) {
|
2010-03-31 21:07:24 -04:00
|
|
|
_undo = s->property_factory (**i);
|
2010-03-01 19:00:00 -05:00
|
|
|
} else if ((*i)->name() == X_("Do")) {
|
2010-03-31 21:07:24 -04:00
|
|
|
_redo = s->property_factory (**i);
|
2010-03-01 19:00:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 21:07:24 -04:00
|
|
|
assert (_undo != 0);
|
|
|
|
assert (_redo != 0);
|
2010-06-23 16:14:07 -04:00
|
|
|
|
|
|
|
/* if the stateful object that this command refers to goes away,
|
|
|
|
be sure to notify owners of this command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
|
2010-02-09 17:28:46 -05:00
|
|
|
}
|
|
|
|
|
2010-02-09 09:44:24 -05:00
|
|
|
StatefulDiffCommand::~StatefulDiffCommand ()
|
|
|
|
{
|
2010-06-23 16:14:07 -04:00
|
|
|
drop_references ();
|
|
|
|
|
2010-03-31 21:07:24 -04:00
|
|
|
delete _undo;
|
|
|
|
delete _redo;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::operator() ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-04-01 20:21:08 -04:00
|
|
|
s->set_properties (*_redo);
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::undo ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-03-01 19:00:00 -05:00
|
|
|
std::cerr << "Undoing a stateful diff command\n";
|
2010-04-01 20:21:08 -04:00
|
|
|
s->set_properties (*_undo);
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
StatefulDiffCommand::get_state ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
/* XXX should we throw? */
|
|
|
|
return * new XMLNode("");
|
|
|
|
}
|
|
|
|
|
2010-02-09 17:28:46 -05:00
|
|
|
XMLNode* node = new XMLNode (X_("StatefulDiffCommand"));
|
|
|
|
|
2010-02-11 18:10:29 -05:00
|
|
|
node->add_property ("obj-id", s->id().to_s());
|
2010-03-02 14:12:01 -05:00
|
|
|
node->add_property ("type-name", demangled_name (*s.get()));
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-03-31 21:07:24 -04:00
|
|
|
XMLNode* undo = new XMLNode (X_("Undo"));
|
|
|
|
XMLNode* redo = new XMLNode (X_("Do"));
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-03-31 21:07:24 -04:00
|
|
|
_undo->add_history_state (undo);
|
|
|
|
_redo->add_history_state (redo);
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-03-31 21:07:24 -04:00
|
|
|
node->add_child_nocopy (*undo);
|
|
|
|
node->add_child_nocopy (*redo);
|
2010-02-09 17:28:46 -05:00
|
|
|
|
|
|
|
return *node;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|