2
/* This file is part of the KDE project
3
Copyright (C) 2001 David Faure <faure@kde.org>
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.
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.
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., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
21
#ifndef KOTEXTDOCUMENT_H
22
#define KOTEXTDOCUMENT_H
24
#include "korichtext.h"
26
#if defined(Q_TEMPLATEDLL)
28
template class Q_EXPORT QMap<int, QColor>;
29
template class Q_EXPORT QMap<int, bool>;
30
template class Q_EXPORT QMap<int, KoTextDocumentSelection>;
31
template class Q_EXPORT QPtrList<KoTextDocument>;
36
class KoTextFormatCollection;
38
class KoTextFormatter;
42
class Q_EXPORT KoTextDocument : public QObject
46
friend class KoTextCursor;
47
friend class KoTextParag;
52
Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
53
// remove multiple lines with removeSelectedText()
56
//KoTextDocument( KoTextDocument *p );
57
//KoTextDocument( KoTextDocument *d, KoTextFormatCollection *f );
58
// see below for constructor
59
virtual ~KoTextDocument();
61
//KoTextDocument *parent() const { return par; }
63
void setText( const QString &text, const QString &context );
66
QString text( int parag ) const;
67
//QString originalText() const;
72
//int widthUsed() const;
73
//int visibleWidth() const;
75
void setWidth( int w );
76
//int minimumWidth() const;
77
//virtual bool setMinimumWidth( int w, KoTextParag *parag );
80
int leftMargin() const;
81
void setLeftMargin( int lm );
82
int rightMargin() const;
83
void setRightMargin( int rm );
85
KoTextParag *firstParag() const;
86
KoTextParag *lastParag() const;
87
void setFirstParag( KoTextParag *p );
88
void setLastParag( KoTextParag *p );
92
//void setPreProcessor( KoTextPreProcessor *sh );
93
//KoTextPreProcessor *preProcessor() const;
95
void setFormatter( KoTextFormatterBase *f );
96
KoTextFormatterBase *formatter() const;
98
void setIndent( KoTextIndent *i );
99
KoTextIndent *indent() const;
101
QColor selectionColor( int id ) const;
102
bool invertSelectionText( int id ) const;
103
void setSelectionColor( int id, const QColor &c );
104
void setInvertSelectionText( int id, bool b );
105
bool hasSelection( int id, bool visible = false ) const;
106
bool isSelectionSwapped( int id ); //// kotext
107
void setSelectionStart( int id, KoTextCursor *cursor );
108
bool setSelectionEnd( int id, KoTextCursor *cursor );
109
void selectAll( int id );
110
bool removeSelection( int id );
111
void selectionStart( int id, int ¶gId, int &index );
112
KoTextCursor selectionStartCursor( int id );
113
KoTextCursor selectionEndCursor( int id );
114
void selectionEnd( int id, int ¶gId, int &index );
115
void setFormat( int id, const KoTextFormat *f, int flags );
116
KoTextParag *selectionStart( int id );
117
KoTextParag *selectionEnd( int id );
118
int numSelections() const { return nSelections; }
119
void addSelection( int id );
121
QString selectedText( int id, bool withCustom = TRUE ) const;
122
//void copySelectedText( int id );
123
void removeSelectedText( int id, KoTextCursor *cursor );
124
void indentSelection( int id );
126
KoTextParag *paragAt( int i ) const;
128
void addCommand( KoTextDocCommand *cmd );
129
KoTextCursor *undo( KoTextCursor *c = 0 );
130
KoTextCursor *redo( KoTextCursor *c = 0 );
131
KoTextDocCommandHistory *commands() const { return commandHistory; }
133
KoTextFormatCollection *formatCollection() const;
135
bool find( const QString &expr, bool cs, bool wo, bool forward, int *parag, int *index, KoTextCursor *cursor );
137
//void setTextFormat( Qt::TextFormat f );
138
//Qt::TextFormat textFormat() const;
140
bool inSelection( int selId, const QPoint &pos ) const;
142
void setUnderlineLinks( bool b ) { underlLinks = b; }
143
bool underlineLinks() const { return underlLinks; }
145
void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; }
146
QBrush *paper() const { return backBrush; }
148
//void doLayout( QPainter *p, int w );
149
#if 0 // see KoTextDocument
150
void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 );
151
void drawParag( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
152
QPixmap *&doubleBuffer, const QColorGroup &cg,
153
bool drawCursor, KoTextCursor *cursor, bool resetChanged = TRUE );
154
KoTextParag *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
155
bool onlyChanged = FALSE, bool drawCursor = FALSE, KoTextCursor *cursor = 0,
156
bool resetChanged = TRUE );
159
//void setDefaultFont( const QFont &f );
161
void registerCustomItem( KoTextCustomItem *i, KoTextParag *p );
162
void unregisterCustomItem( KoTextCustomItem *i, KoTextParag *p );
163
const QPtrList<KoTextCustomItem> & allCustomItems() const { return customItems; }
165
void setFlow( KoTextFlow *f );
167
KoTextFlow *flow() const { return flow_; }
168
bool isPageBreakEnabled() const { return m_pageBreakEnabled; }
169
void setPageBreakEnabled( bool b ) { m_pageBreakEnabled = b; }
171
void setWithoutDoubleBuffer( bool b ) { withoutDoubleBuffer = b; }
172
bool isWithoutDoubleBuffer() const { return withoutDoubleBuffer; } // added for KWTextDocument
174
void setUseFormatCollection( bool b ) { useFC = b; }
175
bool useFormatCollection() const { return useFC; }
177
#ifdef QTEXTTABLE_AVAILABLE
178
KoTextTableCell *tableCell() const { return tc; }
179
void setTableCell( KoTextTableCell *c ) { tc = c; }
182
void setPlainText( const QString &text );
183
//void setRichText( const QString &text, const QString &context );
184
QString richText( KoTextParag *p = 0 ) const;
185
QString plainText( KoTextParag *p = 0 ) const;
187
//bool focusNextPrevChild( bool next );
189
int alignment() const;
190
void setAlignment( int a );
192
int *tabArray() const;
193
int tabStopWidth() const;
194
void setTabArray( int *a );
195
void setTabStops( int tw );
197
void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); }
198
int undoDepth() const { return commandHistory->undoDepth(); }
201
void clear( bool createEmptyParag = FALSE );
203
virtual KoTextParag *createParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = TRUE );
204
void insertChild( QObject *o ) { QObject::insertChild( o ); }
205
void removeChild( QObject *o ) { QObject::removeChild( o ); }
206
//void insertChild( KoTextDocument *d ) { childList.append( d ); }
207
//void removeChild( KoTextDocument *d ) { childList.removeRef( d ); }
208
//QPtrList<KoTextDocument> children() const { return childList; }
210
void setAddMargins( bool b ) { addMargs = b; }
211
int addMargins() const { return addMargs; }
213
//bool hasFocusParagraph() const;
214
//QString focusHref() const;
216
//void invalidateOriginalText() { oTextValid = FALSE; }
217
void informParagraphDeleted( KoTextParag* parag );
220
//void minimumWidthChanged( int );
222
/** Emitted when a paragraph is deleted (kotext addition) */
223
void paragraphDeleted( KoTextParag* parag );
227
QPixmap *bufferPixmap( const QSize &s );
229
//// Beginning of kotext additions
233
* Construct a text document, i.e. a set of paragraphs
235
* @param zoomHandler The KoZoomHandler instance, to handle the zooming, as the name says :)
236
* We need one here because KoTextFormatter needs one for formatting, currently.
238
* @param fc a format collection for this document. Ownership is transferred to the document. ###
239
* @param formatter a text formatter for this document. If 0L, a KoTextFormatter is created.
240
* If not, ownership of the given one is transferred to the document.
241
* @param createInitialParag if true, an initial KoTextParag is created. Set to false if you reimplement createParag,
242
* since the constructor can't call the reimplementation. In that case, make sure to call
243
* clear(true) in your constructor; QRT doesn't support documents without paragraphs.
245
KoTextDocument( KoZoomHandler *zoomHandler,
246
KoTextFormatCollection *fc, KoTextFormatter *formatter = 0L,
247
bool createInitialParag = true );
249
/** Return the zoom handler associated with this document,
250
* used when formatting. Don't use for any other purpose, it might disappear. */
251
KoZoomHandler * formattingZoomHandler() const { return m_zoomHandler; }
254
* Return the zoom handler currently used for drawing.
255
* (This means, at a particular zoom level).
256
* Don't call this in a method that isn't called by drawWYSIWYG, it will be 0L !
257
* (a different one than zoomHandler(), in case it disappears one day,
258
* to have different zoom levels in different views)
260
KoZoomHandler * paintingZoomHandler() const { return m_zoomHandler; }
263
/** Visit all the parts of a selection.
264
* Returns true, unless canceled. See KoParagVisitor. */
265
bool visitSelection( int selectionId, KoParagVisitor *visitor, bool forward = true );
267
/** Visit all paragraphs of the document.
268
* Returns true, unless canceled. See KoParagVisitor. */
269
bool visitDocument( KoParagVisitor *visitor, bool forward = true );
271
/** Visit the document between those two point.
272
* Returns true, unless canceled. See KoParagVisitor. */
273
bool visitFromTo( KoTextParag *firstParag, int firstIndex, KoTextParag* lastParag, int lastIndex, KoParagVisitor* visitor, bool forw = true );
276
* Used by ~KoTextParag to know if it should die quickly
278
bool isDestroying() const { return m_bDestroying; }
281
* Flags for drawWYSIWYG and drawParagWYSIWYG
284
DrawMisspelledLine = 1,
285
DrawFormattingChars = 2,
287
DontDrawNoteVariable = 8
289
/** The main drawing method. Equivalent to KoTextDocument::draw, but reimplemented
291
KoTextParag *drawWYSIWYG( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
292
KoZoomHandler* zoomHandler, bool onlyChanged = FALSE,
293
bool drawCursor = FALSE, KoTextCursor *cursor = 0,
294
bool resetChanged = TRUE, uint drawingFlags = KoTextDocument::DrawSelections );
296
/** Draw a single paragraph (used by drawWYSIWYG and by KWTextFrameSet::drawCursor).
297
* Equivalent to KoTextDocument::draw, but modified for wysiwyg */
298
void drawParagWYSIWYG( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
299
QPixmap *&doubleBuffer, const QColorGroup &cg,
300
KoZoomHandler* zoomHandler,
301
bool drawCursor, KoTextCursor *cursor,
302
bool resetChanged = TRUE,
303
uint drawingFlags = KoTextDocument::DrawSelections );
305
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagString */
306
bool drawFormattingChars() const { return (m_drawingFlags & DrawFormattingChars); }
307
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagStringInternal */
308
bool drawingMissingSpellLine() const { return (m_drawingFlags & DrawMisspelledLine); }
310
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagStringInternal */
311
bool dontDrawingNoteVariable() const { return (m_drawingFlags & DontDrawNoteVariable); }
313
virtual KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const QMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const QValueList<KoParagLayout> & oldParagLayouts );
315
void emitNewCommand(KCommand *cmd) {
316
emit newCommand( cmd );
318
void emitRepaintChanged() {
319
emit repaintChanged();
323
* Emitted when a new command has been created and should be added to
324
* the main list of commands (usually in the KoDocument).
325
* KoTextObject connects (and forwards) that one.
327
void newCommand( KCommand *cmd );
329
* Tell the world that we'd like some repainting to happen.
330
* KoTextObject connects (and forwards) that one.
332
void repaintChanged();
335
void drawWithoutDoubleBuffer( QPainter *p, const QRect &rect, const QColorGroup &cg,
336
KoZoomHandler* zoomHandler, const QBrush *paper = 0 );
339
// The zoom handler used when formatting
340
// (due to the pixelx/pixelww stuff in KoTextFormatter)
341
KoZoomHandler * m_zoomHandler;
345
//// End of kotext additions
348
/*struct Q_EXPORT Focus {
354
int cx, cy; //, cw, vw;
355
KoTextParag *fParag, *lParag;
356
//KoTextPreProcessor *pProcessor;
357
QMap<int, QColor> selectionColors;
358
QMap<int, KoTextDocumentSelection> selections;
359
QMap<int, bool> selectionText;
360
KoTextDocCommandHistory *commandHistory;
361
KoTextFormatterBase *pFormatter;
362
KoTextIndent *indenter;
363
KoTextFormatCollection *fCollection;
364
//Qt::TextFormat txtFormat;
365
//bool preferRichText : 1;
366
bool m_pageBreakEnabled : 1;
368
bool withoutDoubleBuffer : 1;
369
bool underlLinks : 1;
370
//bool nextDoubleBuffered : 1;
372
//bool oTextValid : 1;
375
QPtrList<KoTextCustomItem> customItems;
376
//KoTextDocument *par;
377
#ifdef QTEXTTABLE_AVAILABLE
380
KoTextCursor *tmpCursor;
383
//Focus focusIndicator;
387
//KoTextParag *minwParag;
396
inline int KoTextDocument::x() const
401
inline int KoTextDocument::y() const
406
inline int KoTextDocument::width() const
408
return flow_->width();
409
//return QMAX( cw, flow_->width() );
412
//inline int KoTextDocument::visibleWidth() const
417
inline KoTextParag *KoTextDocument::firstParag() const
422
inline KoTextParag *KoTextDocument::lastParag() const
427
inline void KoTextDocument::setFirstParag( KoTextParag *p )
432
inline void KoTextDocument::setLastParag( KoTextParag *p )
437
inline void KoTextDocument::setWidth( int w )
439
//cw = QMAX( w, minw );
440
flow_->setWidth( w );
444
//inline int KoTextDocument::minimumWidth() const
449
inline void KoTextDocument::setY( int y )
454
inline int KoTextDocument::leftMargin() const
459
inline void KoTextDocument::setLeftMargin( int lm )
464
inline int KoTextDocument::rightMargin() const
469
inline void KoTextDocument::setRightMargin( int rm )
474
/*inline KoTextPreProcessor *KoTextDocument::preProcessor() const
479
inline void KoTextDocument::setPreProcessor( KoTextPreProcessor * sh )
484
inline void KoTextDocument::setFormatter( KoTextFormatterBase *f )
490
inline KoTextFormatterBase *KoTextDocument::formatter() const
495
inline void KoTextDocument::setIndent( KoTextIndent *i )
500
inline KoTextIndent *KoTextDocument::indent() const
505
inline QColor KoTextDocument::selectionColor( int id ) const
507
return selectionColors[ id ];
510
inline bool KoTextDocument::invertSelectionText( int id ) const
512
return selectionText[ id ];
515
inline void KoTextDocument::setSelectionColor( int id, const QColor &c )
517
selectionColors[ id ] = c;
520
inline void KoTextDocument::setInvertSelectionText( int id, bool b )
522
selectionText[ id ] = b;
525
inline KoTextFormatCollection *KoTextDocument::formatCollection() const
530
inline int KoTextDocument::alignment() const
535
inline void KoTextDocument::setAlignment( int a )
540
inline int *KoTextDocument::tabArray() const
545
inline int KoTextDocument::tabStopWidth() const
550
inline void KoTextDocument::setTabArray( int *a )
555
inline void KoTextDocument::setTabStops( int tw )
560
/*inline QString KoTextDocument::originalText() const
567
inline void KoTextDocument::setFlow( KoTextFlow *f )
574
inline void KoTextDocument::takeFlow()
580
* Base class for "visitors". Visitors are a well-designed way to
581
* apply a given operation to all the paragraphs in a selection, or
582
* in a document. The visitor needs to inherit KoParagVisitor, and implement visit().
587
/** protected since this is an abstract base class */
589
virtual ~KoParagVisitor() {}
591
/** Visit the paragraph @p parag, from index @p start to index @p end */
592
virtual bool visit( KoTextParag *parag, int start, int end ) = 0;
599
/** A CustomItemsMap associates a custom item to an index
600
* Used in the undo/redo info for insert/delete text. */
601
class CustomItemsMap : public QMap<int, KoTextCustomItem *>
605
/** Insert all the items from the map, into the existing text */
606
void insertItems( const KoTextCursor & startCursor, int size );
608
/** Delete all the items from the map, adding their commands into macroCmd */
609
void deleteAll( KMacroCommand *macroCmd );