karbon

vreplacingcmd.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20
21#include "vreplacingcmd.h"
22#include "vselection.h"
23#include "vdocument.h"
24
26 : VCommand( doc, name )
27{
28 // Set members.
29 m_oldObjects = doc ? document()->selection()->clone() : 0L;
30 m_newObjects = 0L;
31}
32
33VReplacingCmd::~VReplacingCmd()
34{
35 delete( m_oldObjects );
36 delete( m_newObjects );
37}
38
39void
41{
42 // Did we have at least once a success? Otherwise we don't get inserted
43 // into the command history.
44 bool successful = false;
45
46
47 // Create new shapes if they don't exist yet.
48 if( !m_newObjects )
49 {
50 m_newObjects = new VSelection();
51
52 // Pointer to temporary object.
53 VObject* newObject;
54
55 VObjectListIterator itr( m_oldObjects->objects() );
56 VObjectList rejects;
57
58 for( ; itr.current(); ++itr )
59 {
60 // Clone object and visit the clone.
61 newObject = itr.current()->clone();
62
63 // Success.
64 if( visit( *newObject ) )
65 {
66 successful = true;
67
68 // Insert new shape right before old shape.
69 itr.current()->parent()->insertInfrontOf(
70 newObject, itr.current() );
71
72 // Add new shape to list of new objects.
73 m_newObjects->append( newObject );
74 }
75 // No success.
76 else
77 {
78 rejects.append( itr.current() );
79 // Delete temporary object.
80 delete( newObject );
81 }
82 }
83 VObjectListIterator jtr( rejects );
84 for( ; jtr.current(); ++jtr )
85 {
86 // Don't consider this object in the future anymore.
87 m_oldObjects->take( *jtr.current() );
88 }
89 }
90
91 // Nothing to do.
92 if( m_newObjects->objects().count() == 0 )
93 return;
94
95
96 VObjectListIterator itr( m_oldObjects->objects() );
97
98 // Hide old objects.
99 for( ; itr.current(); ++itr )
100 {
101 document()->selection()->take( *itr.current() );
102 itr.current()->setState( VObject::deleted );
103 }
104
105 // Show new objects.
106 for( itr = m_newObjects->objects(); itr.current(); ++itr )
107 {
108 itr.current()->setState( VObject::normal );
109 document()->selection()->append( itr.current() );
110 }
111
112
113 // Tell command history wether we had success at least once.
114 setSuccess( successful );
115}
116
117void
119{
120 // Nothing to do.
121 if( m_newObjects->objects().count() == 0 )
122 return;
123
124
125 VObjectListIterator itr( m_oldObjects->objects() );
126
127 // Show old objects.
128 for( ; itr.current(); ++itr )
129 {
130 itr.current()->setState( VObject::normal );
131 document()->selection()->append( itr.current() );
132 }
133
134 // Hide new objects.
135 for( itr = m_newObjects->objects(); itr.current(); ++itr )
136 {
137 document()->selection()->take( *itr.current() );
138 itr.current()->setState( VObject::deleted );
139 }
140
141
142 // Reset success for command history.
143 setSuccess( false );
144}
145
VDocument * document() const
Returns the document the command works on.
Definition vcommand.h:121
TQString name() const
Returns the name of the command.
Definition vcommand.h:91
VCommand(VDocument *doc, const TQString &name, const TQString &icon="14_action")
Constructs a new command.
Definition vcommand.h:51
All non-visual, static doc info is in here.
Definition vdocument.h:53
VSelection * selection() const
Returns a pointer to the selection.
Definition vdocument.h:238
The base class for all karbon objects.
Definition vobject.h:50
virtual VObject * clone() const =0
Create an exact copy of this object.
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ normal
visible, not active
Definition vobject.h:54
virtual void execute()
Executes the command.
VReplacingCmd(VDocument *doc, const TQString &name)
Make it "abstract".
virtual void unexecute()
Unexecutes the command.
VSelection manages a set of selected vobjects.
Definition vselection.h:62
void append()
Adds all objects to the selection.
virtual VSelection * clone() const
Create an exact copy of this object.
void take(VObject &object)
Removes the reference to the object, not the object itself.
virtual bool visit(VObject &object)
General visit method.
Definition vvisitor.cpp:31
void setSuccess(bool success=true)
Set the success property.
Definition vvisitor.h:142