lib

KoTextParag.h

00001 #ifndef KOTEXTPARAG_H
00002 #define KOTEXTPARAG_H
00003 
00004 /* This file is part of the KDE project
00005    Copyright (C) 2001-2005 David Faure <faure@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021 */
00022 
00023 // -*- c++ -*-
00024 
00025 #include "KoParagLayout.h"
00026 
00027 #include "KoTextFormat.h"
00028 #include "KoRichText.h" // for KoTextString
00029 class KoTextFormatterBase;
00030 class KoTextParagLineStart;
00031 class KoTextString;
00032 class KoTextDocument;
00033 class KoParagCounter;
00034 class KoParagStyle;
00035 class KoTextCustomItem;
00036 class KoOasisContext;
00037 class KoSavingContext;
00038 class KoStyleCollection;
00039 
00040 struct KoTextParagSelection
00041 {
00042     int start, end;
00043 };
00044 
00045 #if defined(Q_TEMPLATEDLL)
00046 // MOC_SKIP_BEGIN
00047 template class QMap<int, KoTextParagSelection>;
00048 template class QMap<int, KoTextParagLineStart*>;
00049 // MOC_SKIP_END
00050 #endif
00051 
00052 class KOTEXT_EXPORT KoTextParag
00053 {
00054     friend class KoTextDocument;
00055     friend class KoTextCursor;
00056 
00057 public:
00058     KoTextParag( KoTextDocument *d, KoTextParag *pr = 0, KoTextParag *nx = 0, bool updateIds = TRUE );
00059     virtual ~KoTextParag();
00060 
00061     KoTextString *string() const;
00062     KoTextStringChar *at( int i ) const;
00063     int leftGap() const;
00064     int length() const;
00065 
00066     // Abstraction over the trailing-space thing, so that it can be removed later
00067     int lastCharPos() const { return str->length()-2; }
00068 
00069     void setFormat( KoTextFormat *fm );
00070     KoTextFormat *paragFormat() const;
00071 
00072     KoTextDocument *document() const;
00073 
00074     QRect rect() const;
00075     void setRect( const QRect& rect ) { r = rect; }
00076     void setHeight( int h ) { r.setHeight( h ); }
00077     void setWidth( int w ) { r.setWidth( w ); }
00078     void show();
00079     void hide();
00080     bool isVisible() const { return visible; }
00081 
00082     KoTextParag *prev() const;
00083     KoTextParag *next() const;
00084     void setPrev( KoTextParag *s );
00085     void setNext( KoTextParag *s );
00086 
00087     void insert( int index, const QString &s );
00088     void append( const QString &s, bool reallyAtEnd = FALSE );
00089     void truncate( int index );
00090     void remove( int index, int len );
00091 
00092     void move( int &dy );
00093     void format( int start = -1, bool doMove = TRUE );
00094 
00096     void invalidate( int chr /*ignored*/ = 0 );
00098     bool isValid() const;
00099 
00101     bool hasChanged() const;
00102     void setChanged( bool b, bool recursive = FALSE );
00103     short int lineChanged(); // first line that has been changed.
00104     void setLineChanged( short int line );
00105 
00106     int lineHeightOfChar( int i, int *bl = 0, int *y = 0 ) const;
00107     KoTextStringChar *lineStartOfChar( int i, int *index = 0, int *line = 0 ) const;
00108     int lines() const;
00109     KoTextStringChar *lineStartOfLine( int line, int *index = 0 ) const;
00110     int lineY( int l ) const;
00111     int lineBaseLine( int l ) const;
00112     int lineHeight( int l ) const;
00113     void lineInfo( int l, int &y, int &h, int &bl ) const;
00114 
00115     void setSelection( int id, int start, int end );
00116     void removeSelection( int id );
00117     int selectionStart( int id ) const;
00118     int selectionEnd( int id ) const;
00119     bool hasSelection( int id ) const;
00120     bool hasAnySelection() const;
00121     bool fullSelected( int id ) const;
00122 
00123     //void setEndState( int s );
00124     //int endState() const;
00125 
00126     void setParagId( int i );
00127     int paragId() const;
00128 
00129     QMap<int, KoTextParagLineStart*> &lineStartList();
00130 
00131     void setFormat( int index, int len, const KoTextFormat *f, bool useCollection = TRUE, int flags = -1 );
00132 
00133     void setAlignment( uint a );
00134     void setAlignmentDirect( uint a ) { align = a; }
00135     uint alignment() const;
00136 
00137     virtual void paint( QPainter &painter, const QColorGroup &cg, KoTextCursor *cursor, bool drawSelections,
00138                        int clipx, int clipy, int clipw, int cliph ); // kotextparag.cc
00139 
00140 
00141     int topMargin() const;
00142     int bottomMargin() const;
00143     int leftMargin() const;
00144     int firstLineMargin() const;
00145     int rightMargin() const;
00146     int lineSpacing( int line ) const;
00147     int calculateLineSpacing( int line, int start, int last ) const;
00148 
00149     void registerFloatingItem( KoTextCustomItem *i );
00150     void unregisterFloatingItem( KoTextCustomItem *i );
00151 
00152     void setFullWidth( bool b ) { fullWidth = b; }
00153     bool isFullWidth() const { return fullWidth; }
00154 
00155     int customItems() const;
00156 
00157     void setDocumentRect( const QRect &r );
00158     int documentWidth() const;
00159     //int documentVisibleWidth() const;
00160     int documentX() const;
00161     int documentY() const;
00162     KoTextFormatCollection *formatCollection() const;
00163     //void setFormatter( KoTextFormatterBase *f );
00164     KoTextFormatterBase *formatter() const;
00165     //int minimumWidth() const;
00166     int widthUsed() const;
00167 
00168     int nextTabDefault( int i, int x );
00169     int nextTab( int i, int x, int availableWidth );
00170     int *tabArray() const;
00171     void setTabArray( int *a );
00172     void setTabStops( int tw );
00173 
00176     void setNewLinesAllowed( bool b );
00178     bool isNewLinesAllowed() const;
00179 
00180     virtual void join( KoTextParag *s );
00181     virtual void copyParagData( KoTextParag *parag );
00182 
00183     //void setBreakable( bool b ) { breakable = b; }
00184     //bool isBreakable() const { return breakable; }
00185 
00186     void setMovedDown( bool b ) { movedDown = b; }
00187     bool wasMovedDown() const { return movedDown; }
00188 
00189     void setDirection( QChar::Direction d );
00190     QChar::Direction direction() const;
00191 
00193     void setPartOfTableOfContents( bool b ) { m_toc = b; }
00194     bool partOfTableOfContents() const { return m_toc; }
00195 
00196     // For KoTextFormatter only
00197     void insertLineStart( int index, KoTextParagLineStart *ls );
00198 
00199 protected:
00200     void drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg );
00201     void drawCursorDefault( QPainter &painter, KoTextCursor *cursor, int curx, int cury, int curh, const QColorGroup &cg );
00202     void drawCursor( QPainter &painter, KoTextCursor *cursor, int curx, int cury, int curh, const QColorGroup &cg );
00203 
00209 public:
00210     KoTextDocument * textDocument() const { return document(); }
00211 
00212     KoTextFormat * paragraphFormat() const
00213     { return static_cast<KoTextFormat *>( paragFormat() ); }
00214 
00217     virtual void setParagLayout( const KoParagLayout &layout, int flags = KoParagLayout::All,
00218                                  int marginIndex = -1 );
00219 
00220     const KoParagLayout & paragLayout() { return m_layout; }
00221 
00222     // Margins
00223     double margin( QStyleSheetItem::Margin m ) { return m_layout.margins[m]; }
00224     const double * margins() const { return m_layout.margins; }
00225     void setMargin( QStyleSheetItem::Margin m, double _i );
00226     void setMargins( const double * _i );
00227 
00229     double kwLineSpacing() const { return m_layout.lineSpacingValue(); }
00230 
00231     void setLineSpacing( double _i );
00232 
00233     KoParagLayout::SpacingType kwLineSpacingType() const { return m_layout.lineSpacingType; }
00234 
00235     void setLineSpacingType( KoParagLayout::SpacingType _type );
00236 
00237 
00239     void setAlign( int align );
00241     int resolveAlignment() const;
00242 
00245     int breakableTopMargin() const;
00246 
00247     // Borders
00248     KoBorder leftBorder() const { return m_layout.leftBorder; }
00249     KoBorder rightBorder() const { return m_layout.rightBorder; }
00250     KoBorder topBorder() const { return m_layout.topBorder; }
00251     KoBorder bottomBorder() const { return m_layout.bottomBorder; }
00252     bool hasBorder() const { return m_layout.hasBorder(); }
00253     bool joinBorder() const { return m_layout.joinBorder; }
00254 
00255     void setLeftBorder( const KoBorder & _brd ) { m_layout.leftBorder = _brd; }
00256     void setRightBorder( const KoBorder & _brd ) { m_layout.rightBorder = _brd; }
00257     void setTopBorder( const KoBorder & _brd );
00258     void setBottomBorder( const KoBorder & _brd );
00259     void setJoinBorder( bool join );
00260 
00261     // Paragraph background
00262     QColor backgroundColor() { return m_layout.backgroundColor; }
00263     void setBackgroundColor( const QColor& color);
00264 
00265     // Counters are used to implement list and heading numbering/bullets.
00266     void setCounter( const KoParagCounter & counter );
00267     void setNoCounter();
00268     void setCounter( const KoParagCounter * pCounter )
00269     {
00270         if ( pCounter ) setCounter( *pCounter );
00271         else setNoCounter();
00272     }
00273     KoParagCounter *counter();
00274 
00278     int counterWidth() const;
00279 
00281     KoParagStyle *style() const { return m_layout.style; }
00283     void setStyle( KoParagStyle *style ) { m_layout.style = style; }
00285     void applyStyle( KoParagStyle *style );
00286 
00288     const KoTabulatorList& tabList() const { return m_layout.tabList(); }
00290     void setTabList( const KoTabulatorList &tabList );
00291 
00293     int shadowX( KoTextZoomHandler *zh ) const;
00295     int shadowY( KoTextZoomHandler *zh ) const;
00297     double shadowDistanceY() const;
00298 
00300     void setCustomItem( int index, KoTextCustomItem * custom, KoTextFormat * currentFormat );
00302     void removeCustomItem( int index );
00303 
00306     int findCustomItem( const KoTextCustomItem * custom ) const;
00307 
00309     QMap<int, int>& tabCache() { return m_tabCache; }
00310 
00312     QRect pixelRect( KoTextZoomHandler* zh ) const;
00313 
00317      static void drawFontEffects( QPainter * p, KoTextFormat *format, KoTextZoomHandler *zh, QFont font, const QColor & color, int startX, int baseLine, int bw, int y, int h, QChar firstChar );
00318 
00320     QString toString( int from = 0, int length = 0xffffffff) const;
00321 
00323     void fixParagWidth( bool viewFormattingChars );
00324 
00326     virtual void loadOasis( const QDomElement& e, KoOasisContext& context, KoStyleCollection *styleCollection, uint& pos );
00330     virtual void saveOasis( KoXmlWriter& writer, KoSavingContext& context,
00331                             int from, int to, bool saveAnchorsFramesets = false ) const;
00332 
00333     void loadOasisSpan( const QDomElement& parent, KoOasisContext& context, uint& pos );
00334 
00335     void applyListStyle( KoOasisContext& context, int restartNumbering, bool orderedList, bool heading, int level );
00336 
00337 #ifndef NDEBUG
00338     void printRTDebug( int );
00339 #endif
00340 
00341 protected:
00342     void invalidateCounters();
00343     bool lineHyphenated( int l ) const;
00344 
00345     void paintLines( QPainter &painter, const QColorGroup &cg, KoTextCursor *cursor, bool drawSelections,
00346                      int clipx, int clipy, int clipw, int cliph );
00347 
00348     void drawParagString( QPainter &painter, const QString &str, int start, int len, int startX,
00349                           int lastY, int baseLine, int bw, int h, bool drawSelections,
00350                           KoTextFormat *lastFormat, const QMemArray<int> &selectionStarts,
00351                           const QMemArray<int> &selectionEnds, const QColorGroup &cg, bool rightToLeft, int line );
00352     void drawParagStringInternal( QPainter &painter, const QString &s, int start, int len, int startX,
00353                                   int lastY, int baseLine, int bw, int h, bool drawSelections,
00354                                   KoTextFormat *lastFormat, const QMemArray<int> &selectionStarts,
00355                                   const QMemArray<int> &selectionEnds, const QColorGroup &cg, bool rightToLeft, int line, KoTextZoomHandler* zh, bool drawingShadow );
00356 
00358     enum { FormattingSpace = 1, FormattingBreak = 2, FormattingEndParag = 4, FormattingTabs = 8,
00359            AllFormattingChars = FormattingSpace | FormattingBreak | FormattingEndParag | FormattingTabs };
00360 
00365     virtual void drawFormattingChars( QPainter &painter, int start, int len,
00366                                       int lastY_pix, int baseLine_pix, int h_pix, // in pixels
00367                                       bool drawSelections,
00368                                       KoTextFormat *format, const QMemArray<int> &selectionStarts,
00369                                       const QMemArray<int> &selectionEnds, const QColorGroup &cg,
00370                                       bool rightToLeft, int line, KoTextZoomHandler* zh,
00371                                       int whichFormattingChars );
00372 
00373 protected:
00374     KoParagLayout m_layout;
00375     QMap<int, int> m_tabCache;
00376 
00377 private:
00378     KoParagLayout loadParagLayout( KoOasisContext& context, KoStyleCollection *styleCollection, bool findStyle );
00379 
00380 
00381 
00383 private:
00384     QMap<int, KoTextParagSelection> &selections() const;
00385     QPtrList<KoTextCustomItem> &floatingItems() const;
00387     int heightForLineSpacing( int startChar, int lastChar ) const;
00388 
00389     QMap<int, KoTextParagLineStart*> lineStarts;
00390     QRect r;
00391     KoTextParag *p, *n;
00392     KoTextDocument *doc;
00393     bool m_invalid : 1;
00394     bool changed : 1;
00395     bool fullWidth : 1;
00396     bool newLinesAllowed : 1;
00397     bool visible : 1;
00398     bool movedDown : 1;
00399     bool m_toc : 1;
00400     uint align : 4;
00401     short int m_lineChanged;
00402     int id;
00403     int m_wused;
00404     KoTextString *str;
00405     QMap<int, KoTextParagSelection> *mSelections;
00406     QPtrList<KoTextCustomItem> *mFloatingItems;
00407     KoTextFormat *defFormat; // is this really used?
00408     int *tArray;
00409 
00410     // Those things are used by QRT for the case of a paragraph without document
00411     // We don't use this currently, and it's not worth making EVERY parag bigger
00412     // just for a special case that's rarely used. Better have lightweight KoTextDocument
00413     // replacement (with common base class), if we ever want efficient single-parag docs...
00414     //int tabStopWidth;
00415     //QRect docRect;
00416     //KoTextFormatterBase *pFormatter;
00417     //KoTextDocCommandHistory *commandHistory;
00418 };
00419 
00420 inline int KoTextParag::length() const
00421 {
00422     return str->length();
00423 }
00424 
00425 inline QRect KoTextParag::rect() const
00426 {
00427     return r;
00428 }
00429 
00430 inline KoTextStringChar *KoTextParag::at( int i ) const
00431 {
00432     return &str->at( i );
00433 }
00434 
00435 inline bool KoTextParag::isValid() const
00436 {
00437     return !m_invalid;
00438 }
00439 
00440 inline bool KoTextParag::hasChanged() const
00441 {
00442     return changed;
00443 }
00444 
00445 inline short int KoTextParag::lineChanged()
00446 {
00447     return m_lineChanged;
00448 }
00449 
00450 inline void KoTextParag::append( const QString &s, bool reallyAtEnd )
00451 {
00452     if ( reallyAtEnd )
00453     insert( str->length(), s );
00454     else
00455     insert( QMAX( str->length() - 1, 0 ), s );
00456 }
00457 
00458 inline KoTextParag *KoTextParag::prev() const
00459 {
00460     return p;
00461 }
00462 
00463 inline KoTextParag *KoTextParag::next() const
00464 {
00465     return n;
00466 }
00467 
00468 inline bool KoTextParag::hasAnySelection() const
00469 {
00470     return mSelections ? !selections().isEmpty() : FALSE;
00471 }
00472 
00473 /*inline void KoTextParag::setEndState( int s )
00474 {
00475     if ( s == state )
00476     return;
00477     state = s;
00478 }
00479 
00480 inline int KoTextParag::endState() const
00481 {
00482     return state;
00483 }*/
00484 
00485 inline void KoTextParag::setParagId( int i )
00486 {
00487     id = i;
00488 }
00489 
00490 inline int KoTextParag::paragId() const
00491 {
00492     //if ( id == -1 )
00493     //  kdWarning() << "invalid parag id!!!!!!!! (" << (void*)this << ")" << endl;
00494     return id;
00495 }
00496 
00497 inline QMap<int, KoTextParagLineStart*> &KoTextParag::lineStartList()
00498 {
00499     return lineStarts;
00500 }
00501 
00502 inline KoTextString *KoTextParag::string() const
00503 {
00504     return str;
00505 }
00506 
00507 inline KoTextDocument *KoTextParag::document() const
00508 {
00509     return doc;
00510 }
00511 
00512 inline void KoTextParag::setAlignment( uint a )
00513 {
00514     if ( a == align )
00515     return;
00516     align = a;
00517     invalidate( 0 );
00518 }
00519 
00520 /*inline void KoTextParag::setListStyle( QStyleSheetItem::ListStyle ls )
00521 {
00522     lstyle = ls;
00523     invalidate( 0 );
00524 }
00525 
00526 inline QStyleSheetItem::ListStyle KoTextParag::listStyle() const
00527 {
00528     return lstyle;
00529 }*/
00530 
00531 inline KoTextFormat *KoTextParag::paragFormat() const
00532 {
00533     return defFormat;
00534 }
00535 
00536 inline void KoTextParag::registerFloatingItem( KoTextCustomItem *i )
00537 {
00538     floatingItems().append( i );
00539 }
00540 
00541 inline void KoTextParag::unregisterFloatingItem( KoTextCustomItem *i )
00542 {
00543     floatingItems().removeRef( i );
00544 }
00545 
00546 /*inline void KoTextParag::addCustomItem()
00547 {
00548     numCustomItems++;
00549 }
00550 
00551 inline void KoTextParag::removeCustomItem()
00552 {
00553     numCustomItems--;
00554 }*/
00555 
00556 inline int KoTextParag::customItems() const
00557 {
00558     return mFloatingItems ? mFloatingItems->count() : 0;
00559     // was numCustomItems, but no need for a separate count
00560 }
00561 
00562 inline void KoTextParag::setNewLinesAllowed( bool b )
00563 {
00564     newLinesAllowed = b;
00565 }
00566 
00567 inline bool KoTextParag::isNewLinesAllowed() const
00568 {
00569     return newLinesAllowed;
00570 }
00571 
00572 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys