vzordercmd.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, 2003 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#include <tdelocale.h>
21
22#include "vzordercmd.h"
23#include "vselection.h"
24#include "vdocument.h"
25#include "vlayer.h"
26
27VZOrderCmd::VZOrderCmd( VDocument *doc, VOrder state )
28 : VCommand( doc, i18n( "Order Selection" ) ), m_state( state )
29{
30 m_selection = document()->selection()->clone();
31}
32
33VZOrderCmd::VZOrderCmd( VDocument *doc, VObject *obj, VOrder state )
34 : VCommand( doc, i18n( "Order Selection" ) ), m_state( state )
35{
36 m_selection = new VSelection();
37 m_selection->append( obj );
38}
39
40VZOrderCmd::~VZOrderCmd()
41{
42 delete( m_selection );
43}
44
45void
47{
48 if( m_state == sendToBack )
49 {
50 VObjectListIterator itr( document()->selection()->objects() );
51 for ( itr.toLast() ; itr.current() ; --itr )
52 {
53 // remove from old layer
54 VObjectList objects;
55 VLayerListIterator litr( document()->layers() );
56
57 for ( ; litr.current(); ++litr )
58 {
59 objects = litr.current()->objects();
60 VObjectListIterator itr2( objects );
61 for ( ; itr2.current(); ++itr2 )
62 if( itr2.current() == itr.current() )
63 {
64 litr.current()->sendToBack( *itr2.current() );
65 itr2.current()->setState( VObject::selected );
66 }
67 }
68 }
69 }
70 else if( m_state == bringToFront )
71 {
72 VObjectListIterator itr( document()->selection()->objects() );
73 for ( ; itr.current() ; ++itr )
74 {
75 // remove from old layer
76 VObjectList objects;
77 VLayerListIterator litr( document()->layers() );
78
79 for ( ; litr.current(); ++litr )
80 {
81 objects = litr.current()->objects();
82 VObjectListIterator itr2( objects );
83 for ( ; itr2.current(); ++itr2 )
84 if( itr2.current() == itr.current() )
85 {
86 litr.current()->bringToFront( *itr2.current() );
87 itr2.current()->setState( VObject::selected );
88 }
89 }
90 }
91 }
92 else if( m_state == up || m_state == down )
93 {
94 VSelection selection = *m_selection;
95 // TODO : this doesn't work for objects inside groups!
96 VLayerListIterator litr( document()->layers() );
97 while( !selection.objects().isEmpty() && litr.current() )
98 {
99 for ( ; litr.current(); ++litr )
100 {
101 if( litr.current()->state() == VObject::deleted )
102 continue;
103 VObjectList objects = litr.current()->objects();
104 VObjectList todo;
105 VObjectListIterator objectItr( objects );
106 // find all selected VObjects that are in the current layer
107 for ( ; objectItr.current(); ++objectItr )
108 {
109 VObjectListIterator selectionItr( selection.objects() );
110 for ( ; selectionItr.current() ; ++selectionItr )
111 {
112 if( objectItr.current() == selectionItr.current() )
113 {
114 if( m_state == up )
115 todo.prepend( objectItr.current() );
116 else
117 todo.append( objectItr.current() );
118 }
119 }
120 }
121
122 kdDebug(38000) << "todo.count() : " << todo.count() << endl;
123
124 // we have found the affected vobjects in this vlayer
125 VObjectListIterator todoItr( todo );
126 for ( ; todoItr.current(); ++todoItr )
127 {
128 if( m_state == up )
129 litr.current()->upwards( *todoItr.current() );
130 else
131 litr.current()->downwards( *todoItr.current() );
132 // remove from selection
133 selection.take( *todoItr.current() );
134 // make sure object stays selected
135 todoItr.current()->setState( VObject::selected );
136 }
137 }
138 }
139 }
140 setSuccess( true );
141}
142
143void
145{
146 if( m_state == sendToBack )
147 {
148 m_state = bringToFront;
149 execute();
150 m_state = sendToBack;
151 }
152 else if( m_state == bringToFront )
153 {
154 m_state = sendToBack;
155 execute();
156 m_state = bringToFront;
157 }
158 else if( m_state == up )
159 {
160 m_state = down;
161 execute();
162 m_state = up;
163 }
164 else if( m_state == down )
165 {
166 m_state = up;
167 execute();
168 m_state = down;
169 }
170 setSuccess( false );
171}
172
The base class for all karbon commands.
Definition vcommand.h:42
VDocument * document() const
Returns the document the command works on.
Definition vcommand.h:121
All non-visual, static doc info is in here.
Definition vdocument.h:53
The base class for all karbon objects.
Definition vobject.h:50
@ selected
visible, active and can be manipulated by tools
Definition vobject.h:61
@ deleted
deleted, nearly dead
Definition vobject.h:58
VSelection manages a set of selected vobjects.
Definition vselection.h:62
const VObjectList & objects() const
Read only access to the selected objects.
Definition vselection.h:136
void take(VObject &object)
Removes the reference to the object, not the object itself.
void setSuccess(bool success=true)
Set the success property.
Definition vvisitor.h:142
virtual void unexecute()
Unexecutes the command.
virtual void execute()
Executes the command.