lib Library API Documentation

kotextdocument.h

00001 // -*- c++ -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2001 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #ifndef KOTEXTDOCUMENT_H
00022 #define KOTEXTDOCUMENT_H
00023 
00024 #include "korichtext.h"
00025 #include <koffice_export.h>
00026 #if defined(Q_TEMPLATEDLL)
00027 // MOC_SKIP_BEGIN
00028 template class Q_EXPORT QMap<int, QColor>;
00029 template class Q_EXPORT QMap<int, bool>;
00030 template class Q_EXPORT QMap<int, KoTextDocumentSelection>;
00031 template class Q_EXPORT QPtrList<KoTextDocument>;
00032 // MOC_SKIP_END
00033 #endif
00034 
00035 class KoStyleCollection;
00036 class KoXmlWriter;
00037 class KoGenStyles;
00038 class KoZoomHandler;
00039 class KoTextFormatCollection;
00040 class KoParagVisitor;
00041 class KoTextFormatter;
00042 class KoTextParag;
00043 class CustomItemsMap;
00044 
00045 class KOTEXT_EXPORT KoTextDocument : public QObject
00046 {
00047     Q_OBJECT
00048 
00049     friend class KoTextCursor;
00050     friend class KoTextParag;
00051 
00052 public:
00053     enum SelectionIds {
00054     Standard = 0,
00055         InputMethodPreedit = 1,
00056         // 2 is taken by kotextobject (HighlightSelection)
00057     Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
00058     // remove multiple lines with removeSelectedText()
00059     };
00060 
00061     //KoTextDocument( KoTextDocument *p );
00062     //KoTextDocument( KoTextDocument *d, KoTextFormatCollection *f );
00063     // see below for constructor
00064     virtual ~KoTextDocument();
00065 
00066     //KoTextDocument *parent() const { return par; }
00067 
00068     void setText( const QString &text, const QString &context );
00069 
00070     //QString text() const;
00071     //QString text( int parag ) const;
00072     //QString originalText() const;
00073 
00074     int x() const;
00075     int y() const;
00076     int width() const;
00077     //int widthUsed() const;
00078     //int visibleWidth() const;
00079     int height() const;
00080     void setWidth( int w );
00081     //int minimumWidth() const;
00082     //virtual bool setMinimumWidth( int w, KoTextParag *parag );
00083 
00084     void setY( int y );
00085     int leftMargin() const;
00086     void setLeftMargin( int lm );
00087     int rightMargin() const;
00088     void setRightMargin( int rm );
00089 
00090     KoTextParag *firstParag() const;
00091     KoTextParag *lastParag() const;
00092     void setFirstParag( KoTextParag *p );
00093     void setLastParag( KoTextParag *p );
00094 
00095     void invalidate();
00096 
00097     //void setPreProcessor( KoTextPreProcessor *sh );
00098     //KoTextPreProcessor *preProcessor() const;
00099 
00100     void setFormatter( KoTextFormatterBase *f );
00101     KoTextFormatterBase *formatter() const;
00102 
00103     QColor selectionColor( int id ) const;
00104     bool invertSelectionText( int id ) const;
00105     void setSelectionColor( int id, const QColor &c );
00106     void setInvertSelectionText( int id, bool b );
00107     bool hasSelection( int id, bool visible = false ) const;
00108     bool isSelectionSwapped( int id ); 
00109     void setSelectionStart( int id, KoTextCursor *cursor );
00110     bool setSelectionEnd( int id, KoTextCursor *cursor );
00111     void selectAll( int id );
00112     bool removeSelection( int id );
00113     void selectionStart( int id, int &paragId, int &index );
00114     KoTextCursor selectionStartCursor( int id );
00115     KoTextCursor selectionEndCursor( int id );
00116     void selectionEnd( int id, int &paragId, int &index );
00117     void setFormat( int id, const KoTextFormat *f, int flags );
00118     KoTextParag *selectionStart( int id );
00119     KoTextParag *selectionEnd( int id );
00120     int numSelections() const { return nSelections; }
00121     void addSelection( int id );
00122 
00123     QString selectedText( int id, bool withCustom = TRUE ) const;
00124     //void copySelectedText( int id );
00125     void removeSelectedText( int id, KoTextCursor *cursor );
00126 
00127     KoTextParag *paragAt( int i ) const;
00128 
00129     void addCommand( KoTextDocCommand *cmd );
00130     KoTextCursor *undo( KoTextCursor *c = 0 );
00131     KoTextCursor *redo( KoTextCursor *c  = 0 );
00132     KoTextDocCommandHistory *commands() const { return commandHistory; }
00133 
00134     KoTextFormatCollection *formatCollection() const;
00135 
00136     bool find( const QString &expr, bool cs, bool wo, bool forward, int *parag, int *index, KoTextCursor *cursor );
00137 
00138     //void setTextFormat( Qt::TextFormat f );
00139     //Qt::TextFormat textFormat() const;
00140 
00141     bool inSelection( int selId, const QPoint &pos ) const;
00142 
00143     void setUnderlineLinks( bool b ) { underlLinks = b; }
00144     bool underlineLinks() const { return underlLinks; }
00145 
00146     void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; }
00147     QBrush *paper() const { return backBrush; }
00148 
00149     //void doLayout( QPainter *p, int w );
00150 #if 0 // see KoTextDocument
00151     void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 );
00152     void drawParag( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
00153             QPixmap *&doubleBuffer, const QColorGroup &cg,
00154             bool drawCursor, KoTextCursor *cursor, bool resetChanged = TRUE );
00155     KoTextParag *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
00156               bool onlyChanged = FALSE, bool drawCursor = FALSE, KoTextCursor *cursor = 0,
00157               bool resetChanged = TRUE );
00158 #endif
00159 
00160     //void setDefaultFont( const QFont &f );
00161 
00162     void registerCustomItem( KoTextCustomItem *i, KoTextParag *p );
00163     void unregisterCustomItem( KoTextCustomItem *i, KoTextParag *p );
00164     const QPtrList<KoTextCustomItem> & allCustomItems() const { return customItems; }
00165 
00166     void setFlow( KoTextFlow *f );
00167     void takeFlow();
00168     KoTextFlow *flow() const { return flow_; }
00169     bool isPageBreakEnabled() const { return m_pageBreakEnabled; }
00170     void setPageBreakEnabled( bool b ) { m_pageBreakEnabled = b; }
00171 
00172     void setWithoutDoubleBuffer( bool b ) { withoutDoubleBuffer = b; }
00173     bool isWithoutDoubleBuffer() const { return withoutDoubleBuffer; } // added for KWTextDocument
00174 
00175     void setUseFormatCollection( bool b ) { useFC = b; }
00176     bool useFormatCollection() const { return useFC; }
00177 
00178 #ifdef QTEXTTABLE_AVAILABLE
00179     KoTextTableCell *tableCell() const { return tc; }
00180     void setTableCell( KoTextTableCell *c ) { tc = c; }
00181 #endif
00182 
00183     void setPlainText( const QString &text );
00184     //void setRichText( const QString &text, const QString &context );
00185     //QString richText( KoTextParag *p = 0 ) const;
00186     QString plainText() const;
00187 
00188     //bool focusNextPrevChild( bool next );
00189 
00190     int alignment() const;
00191     void setAlignment( int a );
00192 
00193     int *tabArray() const;
00194     int tabStopWidth() const;
00195     void setTabArray( int *a );
00196     void setTabStops( int tw );
00197 
00198     void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); }
00199     int undoDepth() const { return commandHistory->undoDepth(); }
00200 
00201     int length() const;
00202     void clear( bool createEmptyParag = FALSE );
00203 
00204     KoTextParag* loadList( const QDomElement& list, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
00205 
00206     // For normal loading nextParagraph and pos are 0.
00207     KoTextParag* loadOasisText( const QDomElement &bodyElem, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
00208 
00209     QString copySelection( KoXmlWriter& writer, KoSavingContext& context, int selectionId );
00210 
00211     void saveOasisContent( KoXmlWriter& writer, KoSavingContext& context ) const;
00212 
00213     virtual KoTextParag *createParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = TRUE );
00214 
00215     void setAddMargins( bool b ) { addMargs = b; }
00216     int addMargins() const { return addMargs; }
00217 
00218     void informParagraphDeleted( KoTextParag* parag );
00219 
00220 signals:
00221     //void minimumWidthChanged( int );
00222 
00224     void paragraphDeleted( KoTextParag* parag );
00225 
00226 private:
00227     void init();
00228     QPixmap *bufferPixmap( const QSize &s );
00229 
00231 
00232 public:
00246     KoTextDocument( KoZoomHandler *zoomHandler,
00247                     KoTextFormatCollection *fc, KoTextFormatter *formatter = 0L,
00248                     bool createInitialParag = true );
00249 
00252     KoZoomHandler * formattingZoomHandler() const { return m_zoomHandler; }
00253 
00261     KoZoomHandler * paintingZoomHandler() const { return m_zoomHandler; }
00262 
00263 
00266     bool visitSelection( int selectionId, KoParagVisitor *visitor, bool forward = true );
00267 
00270     bool visitDocument( KoParagVisitor *visitor, bool forward = true );
00271 
00274     bool visitFromTo( KoTextParag *firstParag, int firstIndex, KoTextParag* lastParag, int lastIndex, KoParagVisitor* visitor, bool forw = true );
00275 
00279     bool isDestroying() const { return m_bDestroying; }
00280 
00284     enum DrawingFlags {
00285         DrawMisspelledLine = 1,
00286         DrawFormattingChars = 2,
00287         DrawSelections = 4,
00288         DontDrawNoteVariable = 8
00289     };
00292     KoTextParag *drawWYSIWYG( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
00293                               KoZoomHandler* zoomHandler, bool onlyChanged = FALSE,
00294                               bool drawCursor = FALSE, KoTextCursor *cursor = 0,
00295                               bool resetChanged = TRUE, uint drawingFlags = KoTextDocument::DrawSelections );
00296 
00299     void drawParagWYSIWYG( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
00300                            QPixmap *&doubleBuffer, const QColorGroup &cg,
00301                            KoZoomHandler* zoomHandler,
00302                            bool drawCursor, KoTextCursor *cursor,
00303                            bool resetChanged = TRUE,
00304                            uint drawingFlags = KoTextDocument::DrawSelections );
00305 
00307     bool drawFormattingChars() const { return (m_drawingFlags & DrawFormattingChars); }
00309     bool drawingMissingSpellLine() const { return (m_drawingFlags & DrawMisspelledLine); }
00310 
00312     bool dontDrawingNoteVariable() const { return (m_drawingFlags & DontDrawNoteVariable); }
00313 
00314     virtual KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const QMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const QValueList<KoParagLayout> & oldParagLayouts );
00315 
00316     void emitNewCommand(KCommand *cmd) {
00317         emit newCommand( cmd );
00318     }
00319     void emitRepaintChanged() {
00320         emit repaintChanged();
00321     }
00322 signals:
00328     void newCommand( KCommand *cmd );
00333     void repaintChanged();
00334 
00335 protected:
00336     void drawWithoutDoubleBuffer( QPainter *p, const QRect &rect, const QColorGroup &cg,
00337                                   KoZoomHandler* zoomHandler, const QBrush *paper = 0 );
00338 
00344     virtual bool loadOasisBodyTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
00345                                    KoTextParag* & /*lastParagraph*/, KoStyleCollection* /*styleColl*/,
00346                                    KoTextParag* /*nextParagraph*/ ) {
00347         return false;
00348     }
00349 
00357     virtual bool loadSpanTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
00358                               KoTextParag* /*parag*/, uint /*pos*/,
00359                               QString& /*textData*/, KoTextCustomItem* & /*customItem*/ ) {
00360         return false;
00361     }
00362 
00363 private:
00364     // The zoom handler used when formatting
00365     // (due to the pixelx/pixelww stuff in KoTextFormatter)
00366     KoZoomHandler * m_zoomHandler;
00367     bool m_bDestroying;
00368     uint m_drawingFlags;
00369 
00371 
00372 private:
00373     /*struct Q_EXPORT Focus {
00374     KoTextParag *parag;
00375     int start, len;
00376     QString href;
00377     };*/
00378 
00379     int cx, cy; //, cw, vw;
00380     KoTextParag *fParag, *lParag;
00381     //KoTextPreProcessor *pProcessor;
00382     QMap<int, QColor> selectionColors;
00383     QMap<int, KoTextDocumentSelection> selections;
00384     QMap<int, bool> selectionText;
00385     KoTextDocCommandHistory *commandHistory;
00386     KoTextFormatterBase *pFormatter;
00387     KoTextFormatCollection *fCollection;
00388     //Qt::TextFormat txtFormat;
00389     //bool preferRichText : 1;
00390     bool m_pageBreakEnabled : 1;
00391     bool useFC : 1;
00392     bool withoutDoubleBuffer : 1;
00393     bool underlLinks : 1;
00394     //bool nextDoubleBuffered : 1;
00395     bool addMargs : 1;
00396     int nSelections;
00397     KoTextFlow *flow_;
00398     QPtrList<KoTextCustomItem> customItems;
00399     QBrush *backBrush;
00400     QPixmap *buf_pixmap;
00401     //Focus focusIndicator;
00402     //int minw;
00403     int leftmargin;
00404     int rightmargin;
00405     //KoTextParag *minwParag;
00406     int align;
00407     int *tArray;
00408     int tStopWidth;
00409 };
00410 
00411 inline int KoTextDocument::x() const
00412 {
00413     return cx;
00414 }
00415 
00416 inline int KoTextDocument::y() const
00417 {
00418     return cy;
00419 }
00420 
00421 inline int KoTextDocument::width() const
00422 {
00423     return flow_->width();
00424     //return QMAX( cw, flow_->width() );
00425 }
00426 
00427 //inline int KoTextDocument::visibleWidth() const
00428 //{
00429 //    return vw;
00430 //}
00431 
00432 inline KoTextParag *KoTextDocument::firstParag() const
00433 {
00434     return fParag;
00435 }
00436 
00437 inline KoTextParag *KoTextDocument::lastParag() const
00438 {
00439     return lParag;
00440 }
00441 
00442 inline void KoTextDocument::setFirstParag( KoTextParag *p )
00443 {
00444     fParag = p;
00445 }
00446 
00447 inline void KoTextDocument::setLastParag( KoTextParag *p )
00448 {
00449     lParag = p;
00450 }
00451 
00452 inline void KoTextDocument::setWidth( int w )
00453 {
00454     //cw = QMAX( w, minw );
00455     flow_->setWidth( w );
00456     //vw = w;
00457 }
00458 
00459 //inline int KoTextDocument::minimumWidth() const
00460 //{
00461 //    return minw;
00462 //}
00463 
00464 inline void KoTextDocument::setY( int y )
00465 {
00466     cy = y;
00467 }
00468 
00469 inline int KoTextDocument::leftMargin() const
00470 {
00471     return leftmargin;
00472 }
00473 
00474 inline void KoTextDocument::setLeftMargin( int lm )
00475 {
00476     leftmargin = lm;
00477 }
00478 
00479 inline int KoTextDocument::rightMargin() const
00480 {
00481     return rightmargin;
00482 }
00483 
00484 inline void KoTextDocument::setRightMargin( int rm )
00485 {
00486     rightmargin = rm;
00487 }
00488 
00489 /*inline KoTextPreProcessor *KoTextDocument::preProcessor() const
00490 {
00491     return pProcessor;
00492 }
00493 
00494 inline void KoTextDocument::setPreProcessor( KoTextPreProcessor * sh )
00495 {
00496     pProcessor = sh;
00497 }*/
00498 
00499 inline void KoTextDocument::setFormatter( KoTextFormatterBase *f )
00500 {
00501     delete pFormatter;
00502     pFormatter = f;
00503 }
00504 
00505 inline KoTextFormatterBase *KoTextDocument::formatter() const
00506 {
00507     return pFormatter;
00508 }
00509 
00510 inline QColor KoTextDocument::selectionColor( int id ) const
00511 {
00512     return selectionColors[ id ];
00513 }
00514 
00515 inline bool KoTextDocument::invertSelectionText( int id ) const
00516 {
00517     return selectionText[ id ];
00518 }
00519 
00520 inline void KoTextDocument::setSelectionColor( int id, const QColor &c )
00521 {
00522     selectionColors[ id ] = c;
00523 }
00524 
00525 inline void KoTextDocument::setInvertSelectionText( int id, bool b )
00526 {
00527     selectionText[ id ] = b;
00528 }
00529 
00530 inline KoTextFormatCollection *KoTextDocument::formatCollection() const
00531 {
00532     return fCollection;
00533 }
00534 
00535 inline int KoTextDocument::alignment() const
00536 {
00537     return align;
00538 }
00539 
00540 inline void KoTextDocument::setAlignment( int a )
00541 {
00542     align = a;
00543 }
00544 
00545 inline int *KoTextDocument::tabArray() const
00546 {
00547     return tArray;
00548 }
00549 
00550 inline int KoTextDocument::tabStopWidth() const
00551 {
00552     return tStopWidth;
00553 }
00554 
00555 inline void KoTextDocument::setTabArray( int *a )
00556 {
00557     tArray = a;
00558 }
00559 
00560 inline void KoTextDocument::setTabStops( int tw )
00561 {
00562     tStopWidth = tw;
00563 }
00564 
00565 /*inline QString KoTextDocument::originalText() const
00566 {
00567     if ( oTextValid )
00568     return oText;
00569     return text();
00570 }*/
00571 
00572 inline void KoTextDocument::setFlow( KoTextFlow *f )
00573 {
00574     if ( flow_ )
00575     delete flow_;
00576     flow_ = f;
00577 }
00578 
00579 inline void KoTextDocument::takeFlow()
00580 {
00581     flow_ = 0L;
00582 }
00583 
00589 class KoParagVisitor
00590 {
00591 protected:
00593     KoParagVisitor() {}
00594     virtual ~KoParagVisitor() {}
00595 public:
00597     virtual bool visit( KoTextParag *parag, int start, int end ) = 0;
00598 };
00599 
00600 class KCommand;
00601 class QDomElement;
00602 class KMacroCommand;
00603 
00606 class CustomItemsMap : public QMap<int, KoTextCustomItem *>
00607 {
00608 public:
00609 
00611     void insertItems( const KoTextCursor & startCursor, int size );
00612 
00614     void deleteAll( KMacroCommand *macroCmd );
00615 };
00616 
00617 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:09 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003