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., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#ifndef KOTEXTDOCUMENT_H
22
#define KOTEXTDOCUMENT_H
24
#include "KoRichText.h"
25
#include <koffice_export.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>;
35
class KoStyleCollection;
38
class KoTextZoomHandler;
39
class KoTextFormatCollection;
41
class KoTextFormatter;
45
class KOTEXT_EXPORT KoTextDocument : public QObject
49
friend class KoTextCursor;
50
friend class KoTextParag;
53
/** Identifiers for possible selections. */
56
InputMethodPreedit = 1,
57
HighlightSelection = 2, // used to highlight during search/replace
58
Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
59
// remove multiple lines with removeSelectedText()
62
//KoTextDocument( KoTextDocument *p );
63
//KoTextDocument( KoTextDocument *d, KoTextFormatCollection *f );
64
// see below for constructor
65
virtual ~KoTextDocument();
67
//KoTextDocument *parent() const { return par; }
69
void setText( const QString &text, const QString &context );
71
//QString text() const;
72
//QString text( int parag ) const;
73
//QString originalText() const;
78
//int widthUsed() const;
79
//int visibleWidth() const;
81
void setWidth( int w );
82
//int minimumWidth() const;
83
//virtual bool setMinimumWidth( int w, KoTextParag *parag );
86
int leftMargin() const;
87
void setLeftMargin( int lm );
88
int rightMargin() const;
89
void setRightMargin( int rm );
91
KoTextParag *firstParag() const;
92
KoTextParag *lastParag() const;
93
void setFirstParag( KoTextParag *p );
94
void setLastParag( KoTextParag *p );
98
//void setPreProcessor( KoTextPreProcessor *sh );
99
//KoTextPreProcessor *preProcessor() const;
101
void setFormatter( KoTextFormatterBase *f );
102
KoTextFormatterBase *formatter() const;
104
QColor selectionColor( int id ) const;
105
bool invertSelectionText( int id ) const;
106
void setSelectionColor( int id, const QColor &c );
107
void setInvertSelectionText( int id, bool b );
108
bool hasSelection( int id, bool visible = false ) const;
109
bool isSelectionSwapped( int id ); //// kotext
110
void setSelectionStart( int id, KoTextCursor *cursor );
111
bool setSelectionEnd( int id, KoTextCursor *cursor );
112
void selectAll( int id );
113
bool removeSelection( int id );
114
void selectionStart( int id, int ¶gId, int &index );
115
KoTextCursor selectionStartCursor( int id );
116
KoTextCursor selectionEndCursor( int id );
117
void selectionEnd( int id, int ¶gId, int &index );
118
void setFormat( int id, const KoTextFormat *f, int flags );
119
KoTextParag *selectionStart( int id );
120
KoTextParag *selectionEnd( int id );
121
int numSelections() const { return nSelections; }
122
void addSelection( int id );
124
QString selectedText( int id, bool withCustom = TRUE ) const;
125
//void copySelectedText( int id );
126
void removeSelectedText( int id, KoTextCursor *cursor );
128
KoTextParag *paragAt( int i ) const;
130
void addCommand( KoTextDocCommand *cmd );
131
KoTextCursor *undo( KoTextCursor *c = 0 );
132
KoTextCursor *redo( KoTextCursor *c = 0 );
133
KoTextDocCommandHistory *commands() const { return commandHistory; }
135
KoTextFormatCollection *formatCollection() const;
137
bool find( const QString &expr, bool cs, bool wo, bool forward, int *parag, int *index, KoTextCursor *cursor );
139
//void setTextFormat( Qt::TextFormat f );
140
//Qt::TextFormat textFormat() const;
142
bool inSelection( int selId, const QPoint &pos ) const;
144
void setUnderlineLinks( bool b ) { underlLinks = b; }
145
bool underlineLinks() const { return underlLinks; }
147
void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; }
148
QBrush *paper() const { return backBrush; }
150
//void doLayout( QPainter *p, int w );
151
#if 0 // see KoTextDocument
152
void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 );
153
void drawParag( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
154
QPixmap *&doubleBuffer, const QColorGroup &cg,
155
bool drawCursor, KoTextCursor *cursor, bool resetChanged = TRUE );
156
KoTextParag *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
157
bool onlyChanged = FALSE, bool drawCursor = FALSE, KoTextCursor *cursor = 0,
158
bool resetChanged = TRUE );
161
//void setDefaultFont( const QFont &f );
163
void registerCustomItem( KoTextCustomItem *i, KoTextParag *p );
164
void unregisterCustomItem( KoTextCustomItem *i, KoTextParag *p );
165
const QPtrList<KoTextCustomItem> & allCustomItems() const { return customItems; }
167
void setFlow( KoTextFlow *f );
169
KoTextFlow *flow() const { return flow_; }
170
bool isPageBreakEnabled() const { return m_pageBreakEnabled; }
171
void setPageBreakEnabled( bool b ) { m_pageBreakEnabled = b; }
173
void setWithoutDoubleBuffer( bool b ) { withoutDoubleBuffer = b; }
174
bool isWithoutDoubleBuffer() const { return withoutDoubleBuffer; } // added for KWTextDocument
176
void setUseFormatCollection( bool b ) { useFC = b; }
177
bool useFormatCollection() const { return useFC; }
179
#ifdef QTEXTTABLE_AVAILABLE
180
KoTextTableCell *tableCell() const { return tc; }
181
void setTableCell( KoTextTableCell *c ) { tc = c; }
184
void setPlainText( const QString &text );
185
//void setRichText( const QString &text, const QString &context );
186
//QString richText( KoTextParag *p = 0 ) const;
187
QString plainText() const;
189
//bool focusNextPrevChild( bool next );
191
int alignment() const;
192
void setAlignment( int a );
194
int *tabArray() const;
195
int tabStopWidth() const;
196
void setTabArray( int *a );
197
void setTabStops( int tw );
199
void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); }
200
int undoDepth() const { return commandHistory->undoDepth(); }
203
void clear( bool createEmptyParag = FALSE );
205
KoTextParag* loadList( const QDomElement& list, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
207
// For normal loading nextParagraph and pos are 0.
208
KoTextParag* loadOasisText( const QDomElement &bodyElem, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
210
QString copySelection( KoXmlWriter& writer, KoSavingContext& context, int selectionId );
212
void saveOasisContent( KoXmlWriter& writer, KoSavingContext& context ) const;
214
virtual KoTextParag *createParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = TRUE );
216
void setAddMargins( bool b ) { addMargs = b; }
217
int addMargins() const { return addMargs; }
219
void informParagraphDeleted( KoTextParag* parag );
222
//void minimumWidthChanged( int );
224
/** Emitted when a paragraph is deleted (kotext addition) */
225
void paragraphDeleted( KoTextParag* parag );
229
QPixmap *bufferPixmap( const QSize &s );
231
//// Beginning of kotext additions
235
* Construct a text document, i.e. a set of paragraphs
237
* @param zoomHandler The KoTextZoomHandler instance, to handle the zooming, as the name says :)
238
* We need one here because KoTextFormatter needs one for formatting, currently.
240
* @param fc a format collection for this document. Ownership is transferred to the document. ###
241
* @param formatter a text formatter for this document. If 0L, a KoTextFormatter is created.
242
* If not, ownership of the given one is transferred to the document.
243
* @param createInitialParag if true, an initial KoTextParag is created. Set to false if you reimplement createParag,
244
* since the constructor can't call the reimplementation. In that case, make sure to call
245
* clear(true) in your constructor; QRT doesn't support documents without paragraphs.
247
KoTextDocument( KoTextZoomHandler *zoomHandler,
248
KoTextFormatCollection *fc, KoTextFormatter *formatter = 0L,
249
bool createInitialParag = true );
251
/** Return the zoom handler associated with this document,
252
* used when formatting. Don't use for any other purpose, it might disappear. */
253
KoTextZoomHandler * formattingZoomHandler() const { return m_zoomHandler; }
256
* Return the zoom handler currently used for drawing.
257
* (This means, at a particular zoom level).
258
* Don't call this in a method that isn't called by drawWYSIWYG, it will be 0L !
259
* (a different one than zoomHandler(), in case it disappears one day,
260
* to have different zoom levels in different views)
262
KoTextZoomHandler * paintingZoomHandler() const { return m_zoomHandler; }
265
/** Visit all the parts of a selection.
266
* Returns true, unless canceled. See KoParagVisitor. */
267
bool visitSelection( int selectionId, KoParagVisitor *visitor, bool forward = true );
269
/** Visit all paragraphs of the document.
270
* Returns true, unless canceled. See KoParagVisitor. */
271
bool visitDocument( KoParagVisitor *visitor, bool forward = true );
273
/** Visit the document between those two point.
274
* Returns true, unless canceled. See KoParagVisitor. */
275
bool visitFromTo( KoTextParag *firstParag, int firstIndex, KoTextParag* lastParag, int lastIndex, KoParagVisitor* visitor, bool forw = true );
278
* Used by ~KoTextParag to know if it should die quickly
280
bool isDestroying() const { return m_bDestroying; }
283
* Flags for drawWYSIWYG and drawParagWYSIWYG
286
DrawMisspelledLine = 1,
287
DrawFormattingChars = 2,
289
DontDrawNoteVariable = 8,
290
TransparentBackground = 16
292
/** The main drawing method. Equivalent to KoTextDocument::draw, but reimplemented
294
KoTextParag *drawWYSIWYG( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
295
KoTextZoomHandler* zoomHandler, bool onlyChanged = FALSE,
296
bool drawCursor = FALSE, KoTextCursor *cursor = 0,
297
bool resetChanged = TRUE, uint drawingFlags = KoTextDocument::DrawSelections );
299
/** Draw a single paragraph (used by drawWYSIWYG and by KWTextFrameSet::drawCursor).
300
* Equivalent to KoTextDocument::draw, but modified for wysiwyg */
301
void drawParagWYSIWYG( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
302
QPixmap *&doubleBuffer, const QColorGroup &cg,
303
KoTextZoomHandler* zoomHandler,
304
bool drawCursor, KoTextCursor *cursor,
305
bool resetChanged = TRUE,
306
uint drawingFlags = KoTextDocument::DrawSelections );
308
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagString */
309
bool drawFormattingChars() const { return (m_drawingFlags & DrawFormattingChars); }
310
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagStringInternal */
311
bool drawingMissingSpellLine() const { return (m_drawingFlags & DrawMisspelledLine); }
313
/** Set by drawParagWYSIWYG, used by KoTextParag::drawParagStringInternal */
314
bool dontDrawingNoteVariable() const { return (m_drawingFlags & DontDrawNoteVariable); }
316
virtual KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const QMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const QValueList<KoParagLayout> & oldParagLayouts );
318
void emitNewCommand(KCommand *cmd) {
319
emit newCommand( cmd );
321
void emitRepaintChanged() {
322
emit repaintChanged();
326
* Emitted when a new command has been created and should be added to
327
* the main list of commands (usually in the KoDocument).
328
* KoTextObject connects (and forwards) that one.
330
void newCommand( KCommand *cmd );
332
* Tell the world that we'd like some repainting to happen.
333
* KoTextObject connects (and forwards) that one.
335
void repaintChanged();
338
void drawWithoutDoubleBuffer( QPainter *p, const QRect &rect, const QColorGroup &cg,
339
KoTextZoomHandler* zoomHandler, const QBrush *paper = 0 );
342
* Called by loadOasisText. This allows to extend the loading mechanism
343
* for special tags no handled by kotext (images, textboxes, tables, etc.)
344
* @return true if @p tag was handled.
346
virtual bool loadOasisBodyTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
347
KoTextParag* & /*lastParagraph*/, KoStyleCollection* /*styleColl*/,
348
KoTextParag* /*nextParagraph*/ ) {
353
* Called by KoTextParag::loadOasisSpan. This allows to extend the loading mechanism
354
* for special tags no handled by kotext (bookmarks, image, textbox, link, footnotes etc.)
355
* This method is here instead of in KoTextParag because it's easier to derive from
357
* @return true (and optionally @p textData and @p customItem) if @p tag was handled.
359
virtual bool loadSpanTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
360
KoTextParag* /*parag*/, uint /*pos*/,
361
QString& /*textData*/, KoTextCustomItem* & /*customItem*/ ) {
366
// The zoom handler used when formatting
367
// (due to the pixelx/pixelww stuff in KoTextFormatter)
368
KoTextZoomHandler * m_zoomHandler;
372
//// End of kotext additions
375
/*struct Q_EXPORT Focus {
381
int cx, cy; //, cw, vw;
382
KoTextParag *fParag, *lParag;
383
//KoTextPreProcessor *pProcessor;
384
QMap<int, QColor> selectionColors;
385
QMap<int, KoTextDocumentSelection> selections;
386
QMap<int, bool> selectionText;
387
KoTextDocCommandHistory *commandHistory;
388
KoTextFormatterBase *pFormatter;
389
KoTextFormatCollection *fCollection;
390
//Qt::TextFormat txtFormat;
391
//bool preferRichText : 1;
392
bool m_pageBreakEnabled : 1;
394
bool withoutDoubleBuffer : 1;
395
bool underlLinks : 1;
396
//bool nextDoubleBuffered : 1;
400
QPtrList<KoTextCustomItem> customItems;
403
//Focus focusIndicator;
407
//KoTextParag *minwParag;
413
inline int KoTextDocument::x() const
418
inline int KoTextDocument::y() const
423
inline int KoTextDocument::width() const
425
return flow_->width();
426
//return QMAX( cw, flow_->width() );
429
//inline int KoTextDocument::visibleWidth() const
434
inline KoTextParag *KoTextDocument::firstParag() const
439
inline KoTextParag *KoTextDocument::lastParag() const
444
inline void KoTextDocument::setFirstParag( KoTextParag *p )
449
inline void KoTextDocument::setLastParag( KoTextParag *p )
454
inline void KoTextDocument::setWidth( int w )
456
//cw = QMAX( w, minw );
457
flow_->setWidth( w );
461
//inline int KoTextDocument::minimumWidth() const
466
inline void KoTextDocument::setY( int y )
471
inline int KoTextDocument::leftMargin() const
476
inline void KoTextDocument::setLeftMargin( int lm )
481
inline int KoTextDocument::rightMargin() const
486
inline void KoTextDocument::setRightMargin( int rm )
491
/*inline KoTextPreProcessor *KoTextDocument::preProcessor() const
496
inline void KoTextDocument::setPreProcessor( KoTextPreProcessor * sh )
501
inline void KoTextDocument::setFormatter( KoTextFormatterBase *f )
507
inline KoTextFormatterBase *KoTextDocument::formatter() const
512
inline QColor KoTextDocument::selectionColor( int id ) const
514
return selectionColors[ id ];
517
inline bool KoTextDocument::invertSelectionText( int id ) const
519
return selectionText[ id ];
522
inline void KoTextDocument::setSelectionColor( int id, const QColor &c )
524
selectionColors[ id ] = c;
527
inline void KoTextDocument::setInvertSelectionText( int id, bool b )
529
selectionText[ id ] = b;
532
inline KoTextFormatCollection *KoTextDocument::formatCollection() const
537
inline int KoTextDocument::alignment() const
542
inline void KoTextDocument::setAlignment( int a )
547
inline int *KoTextDocument::tabArray() const
552
inline int KoTextDocument::tabStopWidth() const
557
inline void KoTextDocument::setTabArray( int *a )
562
inline void KoTextDocument::setTabStops( int tw )
567
/*inline QString KoTextDocument::originalText() const
574
inline void KoTextDocument::setFlow( KoTextFlow *f )
581
inline void KoTextDocument::takeFlow()
587
* Base class for "visitors". Visitors are a well-designed way to
588
* apply a given operation to all the paragraphs in a selection, or
589
* in a document. The visitor needs to inherit KoParagVisitor, and implement visit().
594
/** protected since this is an abstract base class */
596
virtual ~KoParagVisitor() {}
598
/** Visit the paragraph @p parag, from index @p start to index @p end */
599
virtual bool visit( KoTextParag *parag, int start, int end ) = 0;
606
/** A CustomItemsMap associates a custom item to an index
607
* Used in the undo/redo info for insert/delete text. */
608
class CustomItemsMap : public QMap<int, KoTextCustomItem *>
612
/** Insert all the items from the map, into the existing text */
613
void insertItems( const KoTextCursor & startCursor, int size );
615
/** Delete all the items from the map, adding their commands into macroCmd */
616
void deleteAll( KMacroCommand *macroCmd );