vgroupcmd.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, The Karbon Developers
3 Copyright (C) 2002, The Karbon Developers
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tdelocale.h>
22
23#include "vgroup.h"
24#include "vgroupcmd.h"
25#include "vselection.h"
26#include "vdocument.h"
27#include "vlayer.h"
28
29VGroupCmd::VGroupCmd( VDocument *doc )
30 : VCommand( doc, i18n( "Group Objects" ), "14_group" )
31{
32 m_selection = document()->selection()->clone();
33
34 m_group = 0L;
35}
36
37VGroupCmd::~VGroupCmd()
38{
39 delete( m_selection );
40}
41
42void
43VGroupCmd::execute()
44{
45 m_group = new VGroup( document()->activeLayer() );
46
47 VObjectListIterator itr( m_selection->objects() );
48 for ( ; itr.current() ; ++itr )
49 {
50 // remove from corresponding parent
51 VGroup *parent = dynamic_cast<VGroup*>( itr.current()->parent() );
52 if( parent )
53 parent->take( *itr.current() );
54
55 m_group->append( itr.current() );
56 }
57
58 document()->append( m_group );
59 document()->selection()->clear();
60 document()->selection()->append( m_group );
61
62 setSuccess( true );
63}
64
65void
66VGroupCmd::unexecute()
67{
68 if( ! m_group )
69 return;
70
71 document()->selection()->clear();
72
73 VObjectListIterator itr( m_group->objects() );
74 for ( ; itr.current() ; ++itr )
75 {
76 // TODO : remove from corresponding VLayer
77 document()->selection()->append( itr.current() );
78 }
79
80 VGroup* parent;
81 if( ( parent = dynamic_cast<VGroup*>( m_group->parent() ) ) )
82 {
83 // unregister from parent:
84 parent->take( *m_group );
85
86 // inform all objects in this group about their new parent
87 VObjectListIterator itr = m_selection->objects();
88
89 for ( ; itr.current() ; ++itr )
90 {
91 parent->append( itr.current() );
92 }
93
94 m_group->clear();
95 m_group->setState( VObject::deleted );
96 }
97
98 setSuccess( false );
99}
100
The base class for all karbon commands.
Definition vcommand.h:42
All non-visual, static doc info is in here.
Definition vdocument.h:53
Base class for all sort of VObject conglomerats.
Definition vgroup.h:35
void take(const VObject &object)
Removes the reference to child object, not the object itself.
Definition vgroup.cpp:364
void append(VObject *object)
Appends a new child object.
Definition vgroup.cpp:372
@ deleted
deleted, nearly dead
Definition vobject.h:58