1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* This file is part of the KDE project
Copyright (C) 2001 David Faure <faure@kde.org>
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 "KoTextCommand.h"
#include "KoTextObject.h"
#include "KoTextParag.h"
#include "KoVariable.h"
#include <kdebug.h>
#include <klocale.h>
// This is automatically called by KCommandHistory's redo action when redo is activated
void KoTextCommand::execute()
{
m_textobj->redo();
}
// This is automatically called by KCommandHistory's undo action when undo is activated
void KoTextCommand::unexecute()
{
m_textobj->undo();
}
KoTextDeleteCommand::KoTextDeleteCommand(
KoTextDocument *d, int i, int idx, const TQMemArray<KoTextStringChar> &str,
const CustomItemsMap & customItemsMap,
const TQValueList<KoParagLayout> &oldParagLayouts )
: KoTextDocDeleteCommand( d, i, idx, str ),
m_oldParagLayouts( oldParagLayouts ),
m_customItemsMap( customItemsMap )
{
Q_ASSERT( id >= 0 );
}
KoTextCursor * KoTextDeleteCommand::execute( KoTextCursor *c )
{
KoTextParag *s = doc ? doc->paragAt( id ) : parag;
if ( !s ) {
kdWarning() << "can't locate parag at " << id << ", last parag: " << doc->lastParag()->paragId() << endl;
return 0;
}
cursor.setParag( s );
cursor.setIndex( index );
int len = text.size();
// Detach from custom items. They are already in the map, and we don't
// want them to be deleted
for ( int i = 0; i < len; ++i )
{
KoTextStringChar * ch = cursor.parag()->at( cursor.index() );
if ( ch->isCustom() )
{
ch->customItem()->setDeleted( true );
cursor.parag()->removeCustomItem(cursor.index());
}
cursor.gotoRight();
}
return KoTextDocDeleteCommand::execute(c);
}
KoTextCursor * KoTextDeleteCommand::unexecute( KoTextCursor *c )
{
// Let TQRichText re-create the text and formatting
KoTextCursor * cr = KoTextDocDeleteCommand::unexecute(c);
KoTextParag *s = doc ? doc->paragAt( id ) : parag;
if ( !s ) {
kdWarning() << "can't locate parag at " << id << ", last parag: " << (doc ? doc->lastParag()->paragId() : -1) << endl;
return 0;
}
cursor.setParag( s );
cursor.setIndex( index );
// Set any custom item that we had
m_customItemsMap.insertItems( cursor, text.size() );
// Now restore the parag layouts (i.e. libkotext specific stuff)
TQValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin();
kdDebug(32500) << "KoTextDeleteCommand::unexecute " << m_oldParagLayouts.count() << " parag layouts. First parag=" << s->paragId() << endl;
Q_ASSERT( id == s->paragId() );
KoTextParag *p = s;
while ( p ) {
if ( lit != m_oldParagLayouts.end() )
{
kdDebug(32500) << "KoTextDeleteCommand::unexecute applying paragtqlayout to parag " << p->paragId() << endl;
p->setParagLayout( *lit );
}
else
break;
//if ( s == cr->parag() )
// break;
p = p->next();
++lit;
}
return cr;
}
KoTextParagCommand::KoTextParagCommand( KoTextDocument *d, int fParag, int lParag,
const TQValueList<KoParagLayout> &oldParagLayouts,
KoParagLayout newParagLayout,
int flags,
TQStyleSheetItem::Margin margin )
: KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldParagLayouts( oldParagLayouts ),
m_newParagLayout( newParagLayout ), m_flags( flags ), m_margin( margin )
{
Q_ASSERT( fParag >= 0 );
Q_ASSERT( lParag >= 0 );
}
KoTextCursor * KoTextParagCommand::execute( KoTextCursor *c )
{
//kdDebug(32500) << "KoTextParagCommand::execute" << endl;
KoTextParag *p = doc->paragAt( firstParag );
if ( !p )
{
kdWarning() << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl;
return c;
}
while ( p ) {
if ( ( m_flags & KoParagLayout::Margins ) && m_margin != (TQStyleSheetItem::Margin)-1 ) // all
p->setMargin( static_cast<TQStyleSheetItem::Margin>(m_margin), m_newParagLayout.margins[m_margin] );
else
{
p->setParagLayout( m_newParagLayout, m_flags );
}
if ( p->paragId() == lastParag )
break;
p = p->next();
}
//kdDebug(32500) << "KoTextParagCommand::execute done" << endl;
// Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute...
c->setParag( p );
c->setIndex( p->length()-1 );
return c;
}
KoTextCursor * KoTextParagCommand::unexecute( KoTextCursor *c )
{
kdDebug(32500) << "KoTextParagCommand::unexecute" << endl;
KoTextParag *p = doc->paragAt( firstParag );
if ( !p )
{
kdDebug(32500) << "KoTextParagCommand::unexecute paragraph " << firstParag << "not found" << endl;
return c;
}
TQValueList<KoParagLayout>::Iterator lit = m_oldParagLayouts.begin();
while ( p ) {
if ( lit == m_oldParagLayouts.end() )
{
kdDebug(32500) << "KoTextParagCommand::unexecute m_oldParagLayouts not big enough!" << endl;
break;
}
if ( m_flags & KoParagLayout::Margins && m_margin != (TQStyleSheetItem::Margin)-1 ) // just one
p->setMargin( static_cast<TQStyleSheetItem::Margin>(m_margin), (*lit).margins[m_margin] );
else
{
p->setParagLayout( *lit, m_flags );
}
if ( p->paragId() == lastParag )
break;
p = p->next();
++lit;
}
// Set cursor to end of selection. Like in KoTextFormatCommand::[un]execute...
c->setParag( p );
c->setIndex( p->length()-1 );
return c;
}
//////////
KoParagFormatCommand::KoParagFormatCommand( KoTextDocument *d, int fParag, int lParag,
const TQValueList<KoTextFormat *> &oldFormats,
KoTextFormat * newFormat )
: KoTextDocCommand( d ), firstParag( fParag ), lastParag( lParag ), m_oldFormats( oldFormats ),
m_newFormat( newFormat )
{
TQValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
for ( ; lit != m_oldFormats.end() ; ++lit )
(*lit)->addRef();
}
KoParagFormatCommand::~KoParagFormatCommand()
{
TQValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
for ( ; lit != m_oldFormats.end() ; ++lit )
(*lit)->removeRef();
}
KoTextCursor * KoParagFormatCommand::execute( KoTextCursor *c )
{
KoTextParag *p = doc->paragAt( firstParag );
if ( !p )
{
kdDebug(32500) << "KoTextParagCommand::execute paragraph " << firstParag << "not found" << endl;
return c;
}
while ( p ) {
p->setFormat( m_newFormat );
p->tqinvalidate(0);
if ( p->paragId() == lastParag )
break;
p = p->next();
}
return c;
}
KoTextCursor * KoParagFormatCommand::unexecute( KoTextCursor *c )
{
kdDebug(32500) << "KoParagFormatCommand::unexecute" << endl;
KoTextParag *p = doc->paragAt( firstParag );
if ( !p )
{
kdDebug(32500) << "KoParagFormatCommand::unexecute paragraph " << firstParag << "not found" << endl;
return c;
}
TQValueList<KoTextFormat *>::Iterator lit = m_oldFormats.begin();
while ( p ) {
if ( lit == m_oldFormats.end() )
{
kdDebug(32500) << "KoParagFormatCommand::unexecute m_oldFormats not big enough!" << endl;
break;
}
p->setFormat( (*lit) );
if ( p->paragId() == lastParag )
break;
p = p->next();
++lit;
}
return c;
}
KoTextFormatCommand::KoTextFormatCommand(KoTextDocument *d, int sid, int sidx, int eid, int eidx, const TQMemArray<KoTextStringChar> &old, const KoTextFormat *f, int fl )
: KoTextDocFormatCommand(d, sid, sidx, eid, eidx, old, f, fl)
{
}
KoTextFormatCommand::~KoTextFormatCommand()
{
}
void KoTextFormatCommand::resizeCustomItems()
{
KoTextParag *sp = doc->paragAt( startId );
KoTextParag *ep = doc->paragAt( endId );
if ( !sp || !ep )
return;
KoTextCursor start( doc );
start.setParag( sp );
start.setIndex( startIndex );
KoTextCursor end( doc );
end.setParag( ep );
end.setIndex( endIndex );
doc->setSelectionStart( KoTextDocument::Temp, &start );
doc->setSelectionEnd( KoTextDocument::Temp, &end );
// TODO use the visitor pattern (some 'ResizeCustomItemVisitor')
if ( start.parag() == end.parag() )
{
TQString text = start.parag()->string()->toString().mid( start.index(), end.index() - start.index() );
for ( int i = start.index(); i < end.index(); ++i )
{
if( start.parag()->at(i)->isCustom())
{
start.parag()->at(i)->customItem()->resize();
}
}
}
else
{
int i;
TQString text = start.parag()->string()->toString().mid( start.index(), start.parag()->length() - 1 - start.index() );
for ( i = start.index(); i < start.parag()->length(); ++i )
if( start.parag()->at(i)->isCustom())
{
start.parag()->at(i)->customItem()->resize();
}
KoTextParag *p = start.parag()->next();
while ( p && p != end.parag() )
{
text = p->string()->toString().left( p->length() - 1 );
for ( i = 0; i < p->length(); ++i )
{
if( p->at(i)->isCustom())
{
p->at(i)->customItem()->resize();
}
}
p = p->next();
}
text = end.parag()->string()->toString().left( end.index() );
for ( i = 0; i < end.index(); ++i )
{
if( end.parag()->at(i)->isCustom())
{
end.parag()->at(i)->customItem()->resize();
}
}
}
}
KoTextCursor *KoTextFormatCommand::execute( KoTextCursor *c )
{
c = KoTextDocFormatCommand::execute( c );
resizeCustomItems();
return c;
}
KoTextCursor *KoTextFormatCommand::unexecute( KoTextCursor *c )
{
kdDebug(32500) << "KoTextFormatCommand::unexecute c:" << c << " index:" << c->index() << endl;
c = KoTextDocFormatCommand::unexecute( c );
kdDebug(32500) << "KoTextFormatCommand::unexecute after KoTextFormatCommand c:" << c << " index:" << c->index() << endl;
resizeCustomItems();
return c;
}
////
KoChangeVariableSubType::KoChangeVariableSubType(
short int _oldValue, short int _newValue,
KoVariable *var):
KCommand(),
m_newValue(_newValue),
m_oldValue(_oldValue),
m_var(var)
{
}
void KoChangeVariableSubType::execute()
{
Q_ASSERT(m_var);
m_var->setVariableSubType(m_newValue);
m_var->recalcAndRepaint();
}
void KoChangeVariableSubType::unexecute()
{
Q_ASSERT(m_var);
m_var->setVariableSubType(m_oldValue);
m_var->recalcAndRepaint();
}
TQString KoChangeVariableSubType::name() const
{
return i18n( "Change Variable Subtype" );
}
////
KoChangeVariableFormatProperties::KoChangeVariableFormatProperties(
const TQString &_oldValue, const TQString &_newValue,
KoVariable *var)
: KCommand(),
m_newValue(_newValue),
m_oldValue(_oldValue),
m_var(var)
{
}
void KoChangeVariableFormatProperties::execute()
{
Q_ASSERT(m_var);
// Wrong! m_var->variableFormat()->setFormatProperties( m_newValue );
KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection();
m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_newValue ) ) );
m_var->recalcAndRepaint();
}
void KoChangeVariableFormatProperties::unexecute()
{
Q_ASSERT(m_var);
// Wrong! m_var->variableFormat()->setFormatProperties( m_oldValue );
KoVariableFormatCollection* coll = m_var->variableColl()->formatCollection();
m_var->setVariableFormat( coll->format( m_var->variableFormat()->getKey( m_oldValue ) ) );
m_var->recalcAndRepaint();
}
TQString KoChangeVariableFormatProperties::name() const
{
return i18n( "Change Variable Format" );
}
|