// fragmentact.cpp -- fragment actions // // Written by Frederic Bouvier, started May 2002. // // Copyright (C) 2002 Frederic Bouvier - fredb@users.sourceforge.net // // 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. // // $Id: fragmentact.cpp,v 1.9 2005/05/09 07:01:51 fredb Exp $ #ifdef _MSC_VER #pragma warning( disable : 4786 4503 ) #endif #include #include "fragmentact.hpp" #include "mainwin.hpp" #include "sdutil.hpp" FGSD_ActionID FGSD_MoveFragmentAction::_id; FGSD_ActionID FGSD_AddRemoveFragmentAction::_id; FGSD_MoveFragmentAction::FGSD_MoveFragmentAction( bool __up ) : _up( __up ) , _nb( 1 ) { } FGSD_MoveFragmentAction::FGSD_MoveFragmentAction( const char *__name, bool __up, size_t __nb ) : _up( __up ) , _name( __name ) , _nb( __nb ) { } bool FGSD_MoveFragmentAction::undo() { return _mainWindow->moveFragment( _name.c_str(), !_up, _nb ); } bool FGSD_MoveFragmentAction::redo( bool __step ) { bool ret = _mainWindow->moveFragment( _name.c_str(), _up, _nb ); if ( __step ) step( 1 ); return ret; } std::string FGSD_MoveFragmentAction::name() const { return _up ? "move-up" : "move-down"; } std::string FGSD_MoveFragmentAction::description() const { return "move a map fragment"; } bool FGSD_MoveFragmentAction::combine( FGSD_Action *__action ) { bool ret = false; FGSD_MoveFragmentAction *act = (FGSD_MoveFragmentAction *)__action->convertTo( FGSD_MoveFragmentAction::_id ); if ( act ) { act->_nb += _nb; ret = true; } return ret; } FGSD_ActionID FGSD_MoveFragmentAction::type() const { return _id; } void *FGSD_MoveFragmentAction::convertTo( FGSD_ActionID __id ) { return __id == _id ? (void *)this : 0; } FGSD_Action *FGSD_MoveFragmentAction::clone() const { return new FGSD_MoveFragmentAction( *this ); } bool FGSD_MoveFragmentAction::save( const char *__project, SGPropertyNode *__node ) { SGPropertyNode *node = __node->getChild( "family", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( "map-fragment" ); node = __node->getChild( "name", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( name().c_str() ); bool rel; std::string path = FGSD_Util::buildRelativePath( __project, _name.c_str(), rel ); if ( rel ) node = __node->getChild( "fragment-rel", 0, true ); else node = __node->getChild( "fragment", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( path.c_str() ); node = __node->getChild( "amount", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setIntValue( _nb ); return true; } bool FGSD_MoveFragmentAction::load( const char *__project, SGPropertyNode *__node ) { bool ret = false; bool rel = true; SGPropertyNode *node = __node->getChild( "fragment-rel" ); if ( !node ) { node = __node->getChild( "fragment" ); rel = false; } if ( node ) { _name = node->getStringValue(); if ( rel ) _name = FGSD_Util::buildAbsolutePath( __project, _name.c_str() ); node = __node->getChild( "amount" ); if ( node ) { _nb = node->getIntValue(); ret = true; } } return ret; } //=========================================================================== FGSD_AddRemoveFragmentAction::FGSD_AddRemoveFragmentAction( bool __add ) : _add( __add ) { } FGSD_AddRemoveFragmentAction::FGSD_AddRemoveFragmentAction( bool __add, const char *__name, size_t __nb ) : _add( __add ) , _name( __name ) , _nb( __nb ) { } bool FGSD_AddRemoveFragmentAction::undo() { bool ret = false; if ( _add ) { ret = _mainWindow->removeMapFragment( _name.c_str() ); } else { ret = _mainWindow->loadMapFragment( _name.c_str() ); if ( ret ) ret = _mainWindow->moveFragment( _name.c_str(), false, _nb ); } return ret; } bool FGSD_AddRemoveFragmentAction::redo( bool __step ) { bool ret = false; if ( _add ) { ret = _mainWindow->loadMapFragment( _name.c_str() ); } else { ret = _mainWindow->removeMapFragment( _name.c_str() ); } if ( __step ) step( _add ? 100 : 10 ); return ret; } std::string FGSD_AddRemoveFragmentAction::name() const { return _add ? "add" : "remove"; } std::string FGSD_AddRemoveFragmentAction::description() const { return _add ? "add a map fragment" : "remove a map fragment"; } FGSD_ActionID FGSD_AddRemoveFragmentAction::type() const { return _id; } void *FGSD_AddRemoveFragmentAction::convertTo( FGSD_ActionID __id ) { return __id == _id ? (void *)this : 0; } FGSD_Action *FGSD_AddRemoveFragmentAction::clone() const { return new FGSD_AddRemoveFragmentAction( *this ); } bool FGSD_AddRemoveFragmentAction::save( const char *__project, SGPropertyNode *__node ) { SGPropertyNode *node = __node->getChild( "family", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( "map-fragment" ); node = __node->getChild( "name", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( name().c_str() ); bool rel; std::string path = FGSD_Util::buildRelativePath( __project, _name.c_str(), rel ); if ( rel ) node = __node->getChild( "fragment-rel", 0, true ); else node = __node->getChild( "fragment", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( path.c_str() ); if ( !_add ) { node = __node->getChild( "z-order", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setIntValue( _nb ); } return true; } bool FGSD_AddRemoveFragmentAction::load( const char *__project, SGPropertyNode *__node ) { bool ret = false; bool rel = true; SGPropertyNode *node = __node->getChild( "fragment-rel" ); if ( !node ) { node = __node->getChild( "fragment" ); rel = false; } if ( node ) { _name = node->getStringValue(); if ( rel ) _name = FGSD_Util::buildAbsolutePath( __project, _name.c_str() ); if ( !_add ) { node = __node->getChild( "z-order" ); if ( node ) { _nb = node->getIntValue(); ret = true; } } else ret = true; } return ret; } size_t FGSD_AddRemoveFragmentAction::weight( SGPropertyNode *__node ) { return _add ? 100 : 10; }