Commit bc0a9c2e authored by Martin Pfeiffer's avatar Martin Pfeiffer
Browse files

implementation of the commands

svn path=/trunk/koffice/; revision=599883
parent 3425ab3f
/* This file is part of the KDE project
Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
2006 Martin Pfeiffer <hubipete@gmx.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "FormulaCommand.h"
#include "FormulaCursor.h"
#include <klocale.h>
namespace KFormula {
FormulaCommandAdd::FormulaCommandAdd( FormulaCursor* cursor, QList<BasicElement*> added )
: KCommand()
{
m_ownerElement = cursor()->ownerElement();
m_positionInElement = cursor()->position();
m_addedElements = added;
}
void FormulaCommandAdd::execute()
{
FormulaCursor* cursor = new FormulaCursor( m_ownerElement );
cursor->setPosition( m_positionInElement );
foreach( BasicElement* tmp, m_addedElements )
m_ownerElement->insertChild( cursor, tmp );
delete cursor;
}
void FormulaCommandAdd::unexecute()
{
foreach( BasicElement* tmp, m_addedElements )
m_ownerElement->removeElement( tmp );
}
QString FormulaCommandAdd::name() const
{
return i18n( "Add Elements" );
}
FormulaCommandRemove::FormulaCommandRemove( FormulaCursor* cursor,
QList<BasicElement*> removed )
: KCommand()
{
m_ownerElement = cursor()->ownerElement();
m_positionInElement = cursor()->position();
m_removedElements = removed;
}
void FormulaCommandRemove::execute()
{
foreach( BasicElement* tmp, m_removedElements )
m_ownerElement->removeElement( tmp );
}
void FormulaCommandRemove::unexecute()
{
FormulaCursor* cursor = new FormulaCursor( m_ownerElement );
cursor->setPosition( m_positionInElement );
foreach( BasicElement* tmp, m_removedElements )
m_ownerElement->insertChild( cursor, tmp );
delete cursor;
}
QString FormulaCommandRemove::name() const
{
return i18n( "Remove Elements" );
}
FormulaCommandReplace::FormulaCommandReplace( FormulaCursor* cursor,
QList<BasicElement*> replaced,QList<BasicElement*> replacing )
: KCommand()
{
m_ownerElement = cursor()->ownerElement();
m_positionInElement = cursor()->position();
m_replacedElements = replaced;
m_replacingElements = replacing;
}
void FormulaCommandReplace::execute()
{
foreach( BasicElement* tmp, m_replacedElements )
m_ownerElement->removeElement( tmp );
FormulaCursor* cursor = new FormulaCursor( m_ownerElement );
cursor->setPosition( m_positionInElement );
foreach( BasicElement* tmp, m_replacingElements )
m_ownerElement->insertChild( cursor, tmp );
delete cursor;
}
void FormulaCommandReplace::unexecute()
{
foreach( BasicElement* tmp, m_replacingElements )
m_ownerElement->removeElement( tmp );
FormulaCursor* cursor = new FormulaCursor( m_ownerElement );
cursor->setPosition( m_positionInElement );
foreach( BasicElement* tmp, m_replacedElements )
m_ownerElement->insertChild( cursor, tmp );
delete cursor;
}
QString FormulaCommandReplace::name() const
{
return i18n( "Replace Element" );
}
FormulaCommandAttribute::FormulaCommandAttribute( FormulaCursor* cursor,
QHash<QString,QString> attributes )
: KCommand()
{
m_ownerElement = cursor->ownerElement();
m_attributes = attributes;
m_oldAttributes = m_ownerElement->attributes();
QHashIterator<QString, QString> i( m_oldAttributes );
while( i.hasNext() )
{
i.next();
if( !m_attributes.contains( i.key() ) )
m_attributes.insert( i.key(), i.value() );
}
}
void FormulaCommandAttribute::execute()
{
m_ownerElement->setAttributes( m_attributes );
}
void FormulaCommandAttribute::unexecute()
{
m_ownerElement->setAttributes( m_oldAttributes );
}
QString FormulaCommandAttribute::name() const
{
return i18n( "Attribute Changed" );
}
} //namespace KFormula
......@@ -43,10 +43,10 @@ class FormulaCommandAdd : public KCommand {
public:
/**
* The constructor
* @param owner The BasicElement which owns the added elements
* @param elements The list of elements that has been added
* @param cursor The FormulaCursor where the elements will be added
* @param added The list of elements that has been added
*/
FormulaCommandAdd( BasicElement* owner, QList<BasicElement*> elements );
FormulaCommandAdd( FormulaCursor* cursor, QList<BasicElement*> added );
/// Execute the command
void execute();
......@@ -60,6 +60,9 @@ public:
private:
/// The BasicElement that owns the newly added elements
BasicElement* m_ownerElement;
/// The position inside m_ownerElement
int m_positionInElement;
/// The list of added elements
QList<BasicElement*> m_addedElements;
......@@ -80,10 +83,10 @@ class FormulaCommandRemove : public KCommand {
public:
/**
* The constructor
* @param owner The BasicElement which owned the removed elements
* @param cursor The FormulaCursor where the elements will be removed
* @param elements The list of removed elements
*/
FormulaCommandRemove( BasicElement* owner, QList<BasicElement*> elements );
FormulaCommandRemove( FormulaCursor* cursor, QList<BasicElement*> elements );
/// Execute the command
void execute();
......@@ -97,7 +100,10 @@ public:
private:
/// The BasicElement that owned the removed elements
BasicElement* m_ownerElement;
/// The position inside m_ownerElement
int m_positionInElement;
/// The list of removed elements
QList<BasicElement*> m_removedElements;
};
......@@ -117,12 +123,12 @@ class FormulaCommandReplace : public KCommand {
public:
/**
* The constructor
* @param owner The BasicElement which owns the replaced elements
* @param cursor The FormulaCursor where the elements will be replaced
* @param replaced The list of elements that have been replaced
* @param replacing The list of elements that has replaced the old elements
*/
FormulaCommandReplace( BasicElement* owner, QList<BasicElement*> replaced,
QList<BasicElement*> replacing );
FormulaCommandReplace( FormulaCursor* cursor, QList<BasicElement*> replaced,
QList<BasicElement*> replacing );
/// Execute the command
void execute();
......@@ -137,6 +143,9 @@ private:
/// The BasicElement that owned the replaced elements
BasicElement* m_ownerElement;
/// The position inside m_ownerElement
int m_positionInElement;
/// The list of the new elements
QList<BasicElement*> m_replacingElements;
......@@ -161,7 +170,7 @@ public:
* @param owner The BasicElement which owns the changed attributes
* @param attributes The list of the old attributes
*/
FormulaCommandAttribute( BasicElement* owner, QHash<QString,QString> attributes );
FormulaCommandAttribute( FormulaCursor* cursor, QHash<QString,QString> attributes );
/// Execute the command
void execute();
......@@ -176,8 +185,11 @@ private:
/// The BasicElement whose attributes have been changed
BasicElement* m_ownerElement;
/// All attributes that are set newly
QHash<QString,QString> m_attributes;
/// All attributes the element had before
QHash<QString,QString> m_oldAttributes;
QHash<QString,QString> m_oldAttributes;
};
} //namespace KFormula
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment