lib

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., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, 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 KoTextZoomHandler;
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:
00054     enum SelectionId {
00055     Standard = 0,
00056         InputMethodPreedit = 1,
00057         HighlightSelection = 2, // used to highlight during search/replace
00058     Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
00059     // remove multiple lines with removeSelectedText()
00060     };
00061 
00062     //KoTextDocument( KoTextDocument *p );
00063     //KoTextDocument( KoTextDocument *d, KoTextFormatCollection *f );
00064     // see below for constructor
00065     virtual ~KoTextDocument();
00066 
00067     //KoTextDocument *parent() const { return par; }
00068 
00069     void setText( const QString &text, const QString &context );
00070 
00071     //QString text() const;
00072     //QString text( int parag ) const;
00073     //QString originalText() const;
00074 
00075     int x() const;
00076     int y() const;
00077     int width() const;
00078     //int widthUsed() const;
00079     //int visibleWidth() const;
00080     int height() const;
00081     void setWidth( int w );
00082     //int minimumWidth() const;
00083     //virtual bool setMinimumWidth( int w, KoTextParag *parag );
00084 
00085     void setY( int y );
00086     int leftMargin() const;
00087     void setLeftMargin( int lm );
00088     int rightMargin() const;
00089     void setRightMargin( int rm );
00090 
00091     KoTextParag *firstParag() const;
00092     KoTextParag *lastParag() const;
00093     void setFirstParag( KoTextParag *p );
00094     void setLastParag( KoTextParag *p );
00095 
00096     void invalidate();
00097 
00098     //void setPreProcessor( KoTextPreProcessor *sh );
00099     //KoTextPreProcessor *preProcessor() const;
00100 
00101     void setFormatter( KoTextFormatterBase *f );
00102     KoTextFormatterBase *formatter() const;
00103 
00104     QColor selectionColor( int id ) const;
00105     bool invertSelectionText( int id ) const;
00106     void setSelectionColor( int id, const QColor &c );
00107     void setInvertSelectionText( int id, bool b );
00108     bool hasSelection( int id, bool visible = false ) const;
00109     bool isSelectionSwapped( int id ); 
00110     void setSelectionStart( int id, KoTextCursor *cursor );
00111     bool setSelectionEnd( int id, KoTextCursor *cursor );
00112     void selectAll( int id );
00113     bool removeSelection( int id );
00114     void selectionStart( int id, int &paragId, int &index );
00115     KoTextCursor selectionStartCursor( int id );
00116     KoTextCursor selectionEndCursor( int id );
00117     void selectionEnd( int id, int &paragId, int &index );
00118     void setFormat( int id, const KoTextFormat *f, int flags );
00119     KoTextParag *selectionStart( int id );
00120     KoTextParag *selectionEnd( int id );
00121     int numSelections() const { return nSelections; }
00122     void addSelection( int id );
00123 
00124     QString selectedText( int id, bool withCustom = TRUE ) const;
00125     //void copySelectedText( int id );
00126     void removeSelectedText( int id, KoTextCursor *cursor );
00127 
00128     KoTextParag *paragAt( int i ) const;
00129 
00130     void addCommand( KoTextDocCommand *cmd );
00131     KoTextCursor *undo( KoTextCursor *c = 0 );
00132     KoTextCursor *redo( KoTextCursor *c  = 0 );
00133     KoTextDocCommandHistory *commands() const { return commandHistory; }
00134 
00135     KoTextFormatCollection *formatCollection() const;
00136 
00137     bool find( const QString &expr, bool cs, bool wo, bool forward, int *parag, int *index, KoTextCursor *cursor );
00138 
00139     //void setTextFormat( Qt::TextFormat f );
00140     //Qt::TextFormat textFormat() const;
00141 
00142     bool inSelection( int selId, const QPoint &pos ) const;
00143 
00144     void setUnderlineLinks( bool b ) { underlLinks = b; }
00145     bool underlineLinks() const { return underlLinks; }
00146 
00147     void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; }
00148     QBrush *paper() const { return backBrush; }
00149 
00150     //void doLayout( QPainter *p, int w );
00151 #if 0 // see KoTextDocument
00152     void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 );
00153     void drawParag( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
00154             QPixmap *&doubleBuffer, const QColorGroup &cg,
00155             bool drawCursor, KoTextCursor *cursor, bool resetChanged = TRUE );
00156     KoTextParag *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
00157               bool onlyChanged = FALSE, bool drawCursor = FALSE, KoTextCursor *cursor = 0,
00158               bool resetChanged = TRUE );
00159 #endif
00160 
00161     //void setDefaultFont( const QFont &f );
00162 
00163     void registerCustomItem( KoTextCustomItem *i, KoTextParag *p );
00164     void unregisterCustomItem( KoTextCustomItem *i, KoTextParag *p );
00165     const QPtrList<KoTextCustomItem> & allCustomItems() const { return customItems; }
00166 
00167     void setFlow( KoTextFlow *f );
00168     void takeFlow();
00169     KoTextFlow *flow() const { return flow_; }
00170     bool isPageBreakEnabled() const { return m_pageBreakEnabled; }
00171     void setPageBreakEnabled( bool b ) { m_pageBreakEnabled = b; }
00172 
00173     void setWithoutDoubleBuffer( bool b ) { withoutDoubleBuffer = b; }
00174     bool isWithoutDoubleBuffer() const { return withoutDoubleBuffer; } // added for KWTextDocument
00175 
00176     void setUseFormatCollection( bool b ) { useFC = b; }
00177     bool useFormatCollection() const { return useFC; }
00178 
00179 #ifdef QTEXTTABLE_AVAILABLE
00180     KoTextTableCell *tableCell() const { return tc; }
00181     void setTableCell( KoTextTableCell *c ) { tc = c; }
00182 #endif
00183 
00184     void setPlainText( const QString &text );
00185     //void setRichText( const QString &text, const QString &context );
00186     //QString richText( KoTextParag *p = 0 ) const;
00187     QString plainText() const;
00188 
00189     //bool focusNextPrevChild( bool next );
00190 
00191     int alignment() const;
00192     void setAlignment( int a );
00193 
00194     int *tabArray() const;
00195     int tabStopWidth() const;
00196     void setTabArray( int *a );
00197     void setTabStops( int tw );
00198 
00199     void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); }
00200     int undoDepth() const { return commandHistory->undoDepth(); }
00201 
00202     int length() const;
00203     void clear( bool createEmptyParag = FALSE );
00204 
00205     KoTextParag* loadList( const QDomElement& list, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
00206 
00207     // For normal loading nextParagraph and pos are 0.
00208     KoTextParag* loadOasisText( const QDomElement &bodyElem, KoOasisContext& context, KoTextParag* lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph );
00209 
00210     QString copySelection( KoXmlWriter& writer, KoSavingContext& context, int selectionId );
00211 
00212     void saveOasisContent( KoXmlWriter& writer, KoSavingContext& context ) const;
00213 
00214     virtual KoTextParag *createParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = TRUE );
00215 
00216     // Whether margins are added or max'ed.
00217     int addMargins() const { return true; }
00218 
00219     void informParagraphDeleted( KoTextParag* parag );
00220 
00221 signals:
00222     //void minimumWidthChanged( int );
00223 
00225     void paragraphDeleted( KoTextParag* parag );
00226 
00227 private:
00228     void init();
00229     QPixmap *bufferPixmap( const QSize &s );
00230 
00232 
00233 public:
00247     KoTextDocument( KoTextZoomHandler *zoomHandler,
00248                     KoTextFormatCollection *fc, KoTextFormatter *formatter = 0L,
00249                     bool createInitialParag = true );
00250 
00253     KoTextZoomHandler * formattingZoomHandler() const { return m_zoomHandler; }
00254 
00262     KoTextZoomHandler * paintingZoomHandler() const { return m_zoomHandler; }
00263 
00264 
00267     bool visitSelection( int selectionId, KoParagVisitor *visitor, bool forward = true );
00268 
00271     bool visitDocument( KoParagVisitor *visitor, bool forward = true );
00272 
00275     bool visitFromTo( KoTextParag *firstParag, int firstIndex, KoTextParag* lastParag, int lastIndex, KoParagVisitor* visitor, bool forw = true );
00276 
00280     bool isDestroying() const { return m_bDestroying; }
00281 
00285     enum DrawingFlags {
00286         DrawMisspelledLine = 1,
00287         DrawFormattingChars = 2,
00288         DrawSelections = 4,
00289         DontDrawNoteVariable = 8,
00290         TransparentBackground = 16
00291     };
00294     KoTextParag *drawWYSIWYG( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg,
00295                               KoTextZoomHandler* zoomHandler, bool onlyChanged = FALSE,
00296                               bool drawCursor = FALSE, KoTextCursor *cursor = 0,
00297                               bool resetChanged = TRUE, uint drawingFlags = KoTextDocument::DrawSelections );
00298 
00301     void drawParagWYSIWYG( QPainter *p, KoTextParag *parag, int cx, int cy, int cw, int ch,
00302                            QPixmap *&doubleBuffer, const QColorGroup &cg,
00303                            KoTextZoomHandler* zoomHandler,
00304                            bool drawCursor, KoTextCursor *cursor,
00305                            bool resetChanged = TRUE,
00306                            uint drawingFlags = KoTextDocument::DrawSelections );
00307 
00309     bool drawFormattingChars() const { return (m_drawingFlags & DrawFormattingChars); }
00311     bool drawingMissingSpellLine() const { return (m_drawingFlags & DrawMisspelledLine); }
00312 
00314     bool dontDrawingNoteVariable() const { return (m_drawingFlags & DontDrawNoteVariable); }
00315 
00316     virtual KoTextDocCommand *deleteTextCommand( KoTextDocument *textdoc, int id, int index, const QMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const QValueList<KoParagLayout> & oldParagLayouts );
00317 
00318     void emitNewCommand(KCommand *cmd) {
00319         emit newCommand( cmd );
00320     }
00321     void emitRepaintChanged() {
00322         emit repaintChanged();
00323     }
00324 signals:
00330     void newCommand( KCommand *cmd );
00335     void repaintChanged();
00336 
00337 protected:
00338     void drawWithoutDoubleBuffer( QPainter *p, const QRect &rect, const QColorGroup &cg,
00339                                   KoTextZoomHandler* zoomHandler, const QBrush *paper = 0 );
00340 
00346     virtual bool loadOasisBodyTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
00347                                    KoTextParag* & /*lastParagraph*/, KoStyleCollection* /*styleColl*/,
00348                                    KoTextParag* /*nextParagraph*/ ) {
00349         return false;
00350     }
00351 
00359     virtual bool loadSpanTag( const QDomElement& /*tag*/, KoOasisContext& /*context*/,
00360                               KoTextParag* /*parag*/, uint /*pos*/,
00361                               QString& /*textData*/, KoTextCustomItem* & /*customItem*/ ) {
00362         return false;
00363     }
00364 
00365 private:
00366     // The zoom handler used when formatting
00367     // (due to the pixelx/pixelww stuff in KoTextFormatter)
00368     KoTextZoomHandler * m_zoomHandler;
00369     bool m_bDestroying;
00370     uint m_drawingFlags;
00371 
00373 
00374 private:
00375     /*struct Q_EXPORT Focus {
00376     KoTextParag *parag;
00377     int start, len;
00378     QString href;
00379     };*/
00380 
00381     int cx, cy; //, cw, vw;
00382     KoTextParag *fParag, *lParag;
00383     //KoTextPreProcessor *pProcessor;
00384     QMap<int, QColor> selectionColors;
00385     QMap<int, KoTextDocumentSelection> selections;
00386     QMap<int, bool> selectionText;
00387     KoTextDocCommandHistory *commandHistory;
00388     KoTextFormatterBase *pFormatter;
00389     KoTextFormatCollection *fCollection;
00390     //Qt::TextFormat txtFormat;
00391     //bool preferRichText : 1;
00392     bool m_pageBreakEnabled : 1;
00393     bool useFC : 1;
00394     bool withoutDoubleBuffer : 1;
00395     bool underlLinks : 1;
00396     //bool nextDoubleBuffered : 1;
00397     bool addMargs : 1;
00398     int nSelections;
00399     KoTextFlow *flow_;
00400     QPtrList<KoTextCustomItem> customItems;
00401     QBrush *backBrush;
00402     QPixmap *buf_pixmap;
00403     //Focus focusIndicator;
00404     //int minw;
00405     int leftmargin;
00406     int rightmargin;
00407     //KoTextParag *minwParag;
00408     int align;
00409     int *tArray;
00410     int tStopWidth;
00411 };
00412 
00413 inline int KoTextDocument::x() const
00414 {
00415     return cx;
00416 }
00417 
00418 inline int KoTextDocument::y() const
00419 {
00420     return cy;
00421 }
00422 
00423 inline int KoTextDocument::width() const
00424 {
00425     return flow_->width();
00426     //return QMAX( cw, flow_->width() );
00427 }
00428 
00429 //inline int KoTextDocument::visibleWidth() const
00430 //{
00431 //    return vw;
00432 //}
00433 
00434 inline KoTextParag *KoTextDocument::firstParag() const
00435 {
00436     return fParag;
00437 }
00438 
00439 inline KoTextParag *KoTextDocument::lastParag() const
00440 {
00441     return lParag;
00442 }
00443 
00444 inline void KoTextDocument::setFirstParag( KoTextParag *p )
00445 {
00446     fParag = p;
00447 }
00448 
00449 inline void KoTextDocument::setLastParag( KoTextParag *p )
00450 {
00451     lParag = p;
00452 }
00453 
00454 inline void KoTextDocument::setWidth( int w )
00455 {
00456     //cw = QMAX( w, minw );
00457     flow_->setWidth( w );
00458     //vw = w;
00459 }
00460 
00461 //inline int KoTextDocument::minimumWidth() const
00462 //{
00463 //    return minw;
00464 //}
00465 
00466 inline void KoTextDocument::setY( int y )
00467 {
00468     cy = y;
00469 }
00470 
00471 inline int KoTextDocument::leftMargin() const
00472 {
00473     return leftmargin;
00474 }
00475 
00476 inline void KoTextDocument::setLeftMargin( int lm )
00477 {
00478     leftmargin = lm;
00479 }
00480 
00481 inline int KoTextDocument::rightMargin() const
00482 {
00483     return rightmargin;
00484 }
00485 
00486 inline void KoTextDocument::setRightMargin( int rm )
00487 {
00488     rightmargin = rm;
00489 }
00490 
00491 /*inline KoTextPreProcessor *KoTextDocument::preProcessor() const
00492 {
00493     return pProcessor;
00494 }
00495 
00496 inline void KoTextDocument::setPreProcessor( KoTextPreProcessor * sh )
00497 {
00498     pProcessor = sh;
00499 }*/
00500 
00501 inline void KoTextDocument::setFormatter( KoTextFormatterBase *f )
00502 {
00503     delete pFormatter;
00504     pFormatter = f;
00505 }
00506 
00507 inline KoTextFormatterBase *KoTextDocument::formatter() const
00508 {
00509     return pFormatter;
00510 }
00511 
00512 inline QColor KoTextDocument::selectionColor( int id ) const
00513 {
00514     return selectionColors[ id ];
00515 }
00516 
00517 inline bool KoTextDocument::invertSelectionText( int id ) const
00518 {
00519     return selectionText[ id ];
00520 }
00521 
00522 inline void KoTextDocument::setSelectionColor( int id, const QColor &c )
00523 {
00524     selectionColors[ id ] = c;
00525 }
00526 
00527 inline void KoTextDocument::setInvertSelectionText( int id, bool b )
00528 {
00529     selectionText[ id ] = b;
00530 }
00531 
00532 inline KoTextFormatCollection *KoTextDocument::formatCollection() const
00533 {
00534     return fCollection;
00535 }
00536 
00537 inline int KoTextDocument::alignment() const
00538 {
00539     return align;
00540 }
00541 
00542 inline void KoTextDocument::setAlignment( int a )
00543 {
00544     align = a;
00545 }
00546 
00547 inline int *KoTextDocument::tabArray() const
00548 {
00549     return tArray;
00550 }
00551 
00552 inline int KoTextDocument::tabStopWidth() const
00553 {
00554     return tStopWidth;
00555 }
00556 
00557 inline void KoTextDocument::setTabArray( int *a )
00558 {
00559     tArray = a;
00560 }
00561 
00562 inline void KoTextDocument::setTabStops( int tw )
00563 {
00564     tStopWidth = tw;
00565 }
00566 
00567 /*inline QString KoTextDocument::originalText() const
00568 {
00569     if ( oTextValid )
00570     return oText;
00571     return text();
00572 }*/
00573 
00574 inline void KoTextDocument::setFlow( KoTextFlow *f )
00575 {
00576     if ( flow_ )
00577     delete flow_;
00578     flow_ = f;
00579 }
00580 
00581 inline void KoTextDocument::takeFlow()
00582 {
00583     flow_ = 0L;
00584 }
00585 
00591 class KoParagVisitor
00592 {
00593 protected:
00595     KoParagVisitor() {}
00596     virtual ~KoParagVisitor() {}
00597 public:
00599     virtual bool visit( KoTextParag *parag, int start, int end ) = 0;
00600 };
00601 
00602 class KCommand;
00603 class QDomElement;
00604 class KMacroCommand;
00605 
00608 class CustomItemsMap : public QMap<int, KoTextCustomItem *>
00609 {
00610 public:
00611 
00613     void insertItems( const KoTextCursor & startCursor, int size );
00614 
00616     void deleteAll( KMacroCommand *macroCmd );
00617 };
00618 
00619 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys