-rw-r--r-- | noncore/apps/opie-write/qcleanuphandler.h | 12 | ||||
-rw-r--r-- | noncore/apps/opie-write/qcomplextext.cpp | 3 | ||||
-rw-r--r-- | noncore/apps/opie-write/qrichtext.cpp | 4311 | ||||
-rw-r--r-- | noncore/apps/opie-write/qrichtext_p.cpp | 303 | ||||
-rw-r--r-- | noncore/apps/opie-write/qrichtext_p.h | 576 | ||||
-rw-r--r-- | noncore/apps/opie-write/qstylesheet.cpp | 1230 | ||||
-rw-r--r-- | noncore/apps/opie-write/qstylesheet.h | 16 | ||||
-rw-r--r-- | noncore/apps/opie-write/qt3namespace.h | 1 | ||||
-rw-r--r-- | noncore/apps/opie-write/qtextedit.cpp | 1495 | ||||
-rw-r--r-- | noncore/apps/opie-write/qtextedit.h | 30 |
10 files changed, 3833 insertions, 4144 deletions
diff --git a/noncore/apps/opie-write/qcleanuphandler.h b/noncore/apps/opie-write/qcleanuphandler.h index 5c5bf16..2d6eb7c 100644 --- a/noncore/apps/opie-write/qcleanuphandler.h +++ b/noncore/apps/opie-write/qcleanuphandler.h @@ -40,13 +40,9 @@ #include <qlist.h> #endif // QT_H template<class Type> -#ifdef Q_NO_TEMPLATE_EXPORT class QCleanupHandler -#else -class Q_EXPORT QCleanupHandler -#endif { public: QCleanupHandler() : cleanupObjects( 0 ) {} ~QCleanupHandler() { clear(); } @@ -87,13 +83,9 @@ private: QPtrList<Type*> *cleanupObjects; }; template<class Type> -#ifdef Q_NO_TEMPLATE_EXPORT class QSingleCleanupHandler -#else -class Q_EXPORT QSingleCleanupHandler -#endif { public: QSingleCleanupHandler() : object( 0 ) {} ~QSingleCleanupHandler() { @@ -111,13 +103,9 @@ private: Type **object; }; template<class Type> -#ifdef Q_NO_TEMPLATE_EXPORT class QSharedCleanupHandler -#else -class Q_EXPORT QSharedCleanupHandler -#endif { public: QSharedCleanupHandler() : object( 0 ) {} ~QSharedCleanupHandler() { diff --git a/noncore/apps/opie-write/qcomplextext.cpp b/noncore/apps/opie-write/qcomplextext.cpp index 0fa6c2e..e8b94da 100644 --- a/noncore/apps/opie-write/qcomplextext.cpp +++ b/noncore/apps/opie-write/qcomplextext.cpp @@ -65,11 +65,8 @@ QBidiContext::~QBidiContext() if( parent && parent->deref() ) delete parent; } -static QChar *shapeBuffer = 0; -static int shapeBufSize = 0; - /* Arabic shaping obeys a number of rules according to the joining classes (see Unicode book, section on arabic). diff --git a/noncore/apps/opie-write/qrichtext.cpp b/noncore/apps/opie-write/qrichtext.cpp index 7901000..3b044c3 100644 --- a/noncore/apps/opie-write/qrichtext.cpp +++ b/noncore/apps/opie-write/qrichtext.cpp @@ -40,16 +40,13 @@ #include "qstringlist.h" #include "qfont.h" #include "qtextstream.h" #include "qfile.h" -#include "qregexp.h" #include "qapplication.h" -#include "qclipboard.h" #include "qmap.h" #include "qfileinfo.h" #include "qstylesheet.h" #include "qmime.h" -#include "qregexp.h" #include "qimage.h" #include "qdragobject.h" #include "qpaintdevicemetrics.h" #include "qpainter.h" @@ -63,27 +60,21 @@ #include <stdlib.h> using namespace Qt3; -//#define PARSER_DEBUG -//#define DEBUG_COLLECTION// ---> also in qrichtext_p.h -//#define DEBUG_TABLE_RENDERING +static QTextCursor* richTextExportStart = 0; +static QTextCursor* richTextExportEnd = 0; static QTextFormatCollection *qFormatCollection = 0; -const int QStyleSheetItem_WhiteSpaceNoCompression = 3; // ### belongs in QStyleSheetItem, fix 3.1 -const int QStyleSheetItem_WhiteSpaceNormalWithNewlines = 4; // ### belongs in QStyleSheetItem, fix 3.1 - const int border_tolerance = 2; -#if defined(PARSER_DEBUG) -static QString debug_indent; -#endif - #ifdef Q_WS_WIN #include "qt_windows.h" #endif +#define QChar_linesep QChar(0x2028U) + static inline bool is_printer( QPainter *p ) { if ( !p || !p->device() ) return FALSE; @@ -176,20 +167,18 @@ bool QTextCommandHistory::isRedoAvailable() // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ QTextDeleteCommand::QTextDeleteCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, - const QValueList< QPtrVector<QStyleSheetItem> > &os, - const QValueList<QStyleSheetItem::ListStyle> &ols, - const QMemArray<int> &oas) - : QTextCommand( d ), id( i ), index( idx ), parag( 0 ), text( str ), oldStyles( os ), oldListStyles( ols ), oldAligns( oas ) + const QByteArray& oldStyleInfo ) + : QTextCommand( d ), id( i ), index( idx ), parag( 0 ), text( str ), styleInformation( oldStyleInfo ) { for ( int j = 0; j < (int)text.size(); ++j ) { if ( text[ j ].format() ) text[ j ].format()->addRef(); } } -QTextDeleteCommand::QTextDeleteCommand( QTextParag *p, int idx, const QMemArray<QTextStringChar> &str ) +QTextDeleteCommand::QTextDeleteCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ) : QTextCommand( 0 ), id( -1 ), index( idx ), parag( p ), text( str ) { for ( int i = 0; i < (int)text.size(); ++i ) { if ( text[ i ].format() ) @@ -207,24 +196,24 @@ QTextDeleteCommand::~QTextDeleteCommand() } QTextCursor *QTextDeleteCommand::execute( QTextCursor *c ) { - QTextParag *s = doc ? doc->paragAt( id ) : parag; + QTextParagraph *s = doc ? doc->paragAt( id ) : parag; if ( !s ) { - qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParag()->paragId() ); + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); return 0; } - cursor.setParag( s ); + cursor.setParagraph( s ); cursor.setIndex( index ); int len = text.size(); if ( c ) *c = cursor; if ( doc ) { - doc->setSelectionStart( QTextDocument::Temp, &cursor ); + doc->setSelectionStart( QTextDocument::Temp, cursor ); for ( int i = 0; i < len; ++i ) cursor.gotoNextLetter(); - doc->setSelectionEnd( QTextDocument::Temp, &cursor ); + doc->setSelectionEnd( QTextDocument::Temp, cursor ); doc->removeSelectedText( QTextDocument::Temp, &cursor ); if ( c ) *c = cursor; } else { @@ -235,58 +224,42 @@ QTextCursor *QTextDeleteCommand::execute( QTextCursor *c ) } QTextCursor *QTextDeleteCommand::unexecute( QTextCursor *c ) { - QTextParag *s = doc ? doc->paragAt( id ) : parag; + QTextParagraph *s = doc ? doc->paragAt( id ) : parag; if ( !s ) { - qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParag()->paragId() ); + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); return 0; } - cursor.setParag( s ); + cursor.setParagraph( s ); cursor.setIndex( index ); QString str = QTextString::toString( text ); cursor.insert( str, TRUE, &text ); - cursor.setParag( s ); + cursor.setParagraph( s ); cursor.setIndex( index ); if ( c ) { - c->setParag( s ); + c->setParagraph( s ); c->setIndex( index ); for ( int i = 0; i < (int)text.size(); ++i ) c->gotoNextLetter(); } - QValueList< QPtrVector<QStyleSheetItem> >::Iterator it = oldStyles.begin(); - QValueList<QStyleSheetItem::ListStyle>::Iterator lit = oldListStyles.begin(); - int i = 0; - QTextParag *p = s; - bool end = FALSE; - while ( p ) { - if ( it != oldStyles.end() ) - p->setStyleSheetItems( *it ); - else - end = TRUE; - if ( lit != oldListStyles.end() ) - p->setListStyle( *lit ); - else - end = TRUE; - if ( i < (int)oldAligns.size() ) - p->setAlignment( oldAligns.at( i ) ); - else - end = TRUE; - if ( end ) - break; - p = p->next(); - ++it; - ++lit; - ++i; + if ( !styleInformation.isEmpty() ) { + QDataStream styleStream( styleInformation, IO_ReadOnly ); + int num; + styleStream >> num; + QTextParagraph *p = s; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); + p = p->next(); + } } - - s = cursor.parag(); + s = cursor.paragraph(); while ( s ) { s->format(); s->setChanged( TRUE ); - if ( s == c->parag() ) + if ( s == c->paragraph() ) break; s = s->next(); } @@ -314,22 +287,22 @@ QTextFormatCommand::~QTextFormatCommand() } QTextCursor *QTextFormatCommand::execute( QTextCursor *c ) { - QTextParag *sp = doc->paragAt( startId ); - QTextParag *ep = doc->paragAt( endId ); + QTextParagraph *sp = doc->paragAt( startId ); + QTextParagraph *ep = doc->paragAt( endId ); if ( !sp || !ep ) return c; QTextCursor start( doc ); - start.setParag( sp ); + start.setParagraph( sp ); start.setIndex( startIndex ); QTextCursor end( doc ); - end.setParag( ep ); + end.setParagraph( ep ); end.setIndex( endIndex ); - doc->setSelectionStart( QTextDocument::Temp, &start ); - doc->setSelectionEnd( QTextDocument::Temp, &end ); + doc->setSelectionStart( QTextDocument::Temp, start ); + doc->setSelectionEnd( QTextDocument::Temp, end ); doc->setFormat( QTextDocument::Temp, format, flags ); doc->removeSelection( QTextDocument::Temp ); if ( endIndex == ep->length() ) end.gotoLeft(); @@ -338,10 +311,10 @@ QTextCursor *QTextFormatCommand::execute( QTextCursor *c ) } QTextCursor *QTextFormatCommand::unexecute( QTextCursor *c ) { - QTextParag *sp = doc->paragAt( startId ); - QTextParag *ep = doc->paragAt( endId ); + QTextParagraph *sp = doc->paragAt( startId ); + QTextParagraph *ep = doc->paragAt( endId ); if ( !sp || !ep ) return 0; int idx = startIndex; @@ -372,137 +345,97 @@ QTextCursor *QTextFormatCommand::unexecute( QTextCursor *c ) } } QTextCursor end( doc ); - end.setParag( ep ); + end.setParagraph( ep ); end.setIndex( endIndex ); if ( endIndex == ep->length() ) end.gotoLeft(); *c = end; return c; } -QTextAlignmentCommand::QTextAlignmentCommand( QTextDocument *d, int fParag, int lParag, int na, const QMemArray<int> &oa ) - : QTextCommand( d ), firstParag( fParag ), lastParag( lParag ), newAlign( na ), oldAligns( oa ) +QTextStyleCommand::QTextStyleCommand( QTextDocument *d, int fParag, int lParag, const QByteArray& beforeChange ) + : QTextCommand( d ), firstParag( fParag ), lastParag( lParag ), before( beforeChange ) { + after = readStyleInformation( d, fParag, lParag ); } -QTextCursor *QTextAlignmentCommand::execute( QTextCursor *c ) + +QByteArray QTextStyleCommand::readStyleInformation( QTextDocument* doc, int fParag, int lParag ) { - QTextParag *p = doc->paragAt( firstParag ); + QByteArray style; + QTextParagraph *p = doc->paragAt( fParag ); if ( !p ) - return c; - while ( p ) { - p->setAlignment( newAlign ); - if ( p->paragId() == lastParag ) - break; + return style; + QDataStream styleStream( style, IO_WriteOnly ); + int num = lParag - fParag + 1; + styleStream << num; + while ( num -- && p ) { + p->writeStyleInformation( styleStream ); p = p->next(); } - return c; + return style; } -QTextCursor *QTextAlignmentCommand::unexecute( QTextCursor *c ) +void QTextStyleCommand::writeStyleInformation( QTextDocument* doc, int fParag, const QByteArray& style ) { - QTextParag *p = doc->paragAt( firstParag ); + QTextParagraph *p = doc->paragAt( fParag ); if ( !p ) - return c; - int i = 0; - while ( p ) { - if ( i < (int)oldAligns.size() ) - p->setAlignment( oldAligns.at( i ) ); - if ( p->paragId() == lastParag ) - break; + return; + QDataStream styleStream( style, IO_ReadOnly ); + int num; + styleStream >> num; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); p = p->next(); - ++i; } - return c; -} - -QTextParagTypeCommand::QTextParagTypeCommand( QTextDocument *d, int fParag, int lParag, bool l, - QStyleSheetItem::ListStyle s, const QValueList< QPtrVector<QStyleSheetItem> > &os, - const QValueList<QStyleSheetItem::ListStyle> &ols ) - : QTextCommand( d ), firstParag( fParag ), lastParag( lParag ), list( l ), listStyle( s ), oldStyles( os ), oldListStyles( ols ) -{ } -QTextCursor *QTextParagTypeCommand::execute( QTextCursor *c ) +QTextCursor *QTextStyleCommand::execute( QTextCursor *c ) { - QTextParag *p = doc->paragAt( firstParag ); - if ( !p ) - return c; - while ( p ) { - p->setList( list, (int)listStyle ); - if ( p->paragId() == lastParag ) - break; - p = p->next(); - } + writeStyleInformation( doc, firstParag, after ); return c; } -QTextCursor *QTextParagTypeCommand::unexecute( QTextCursor *c ) +QTextCursor *QTextStyleCommand::unexecute( QTextCursor *c ) { - QTextParag *p = doc->paragAt( firstParag ); - if ( !p ) - return c; - QValueList< QPtrVector<QStyleSheetItem> >::Iterator it = oldStyles.begin(); - QValueList<QStyleSheetItem::ListStyle>::Iterator lit = oldListStyles.begin(); - while ( p ) { - if ( it != oldStyles.end() ) - p->setStyleSheetItems( *it ); - if ( lit != oldListStyles.end() ) - p->setListStyle( *lit ); - if ( p->paragId() == lastParag ) - break; - p = p->next(); - ++it; - ++lit; - } + writeStyleInformation( doc, firstParag, before ); return c; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ QTextCursor::QTextCursor( QTextDocument *d ) - : doc( d ), ox( 0 ), oy( 0 ) -{ - nested = FALSE; - idx = 0; - string = doc ? doc->firstParag() : 0; - tmpIndex = -1; - valid = TRUE; -} - -QTextCursor::QTextCursor() + : idx( 0 ), tmpIndex( -1 ), ox( 0 ), oy( 0 ), + valid( TRUE ) { + para = d ? d->firstParagraph() : 0; } QTextCursor::QTextCursor( const QTextCursor &c ) { - doc = c.doc; ox = c.ox; oy = c.oy; - nested = c.nested; idx = c.idx; - string = c.string; + para = c.para; tmpIndex = c.tmpIndex; indices = c.indices; - parags = c.parags; + paras = c.paras; xOffsets = c.xOffsets; yOffsets = c.yOffsets; valid = c.valid; } QTextCursor &QTextCursor::operator=( const QTextCursor &c ) { - doc = c.doc; ox = c.ox; oy = c.oy; - nested = c.nested; idx = c.idx; - string = c.string; + para = c.para; tmpIndex = c.tmpIndex; indices = c.indices; - parags = c.parags; + paras = c.paras; xOffsets = c.xOffsets; yOffsets = c.yOffsets; valid = c.valid; @@ -510,61 +443,54 @@ QTextCursor &QTextCursor::operator=( const QTextCursor &c ) } bool QTextCursor::operator==( const QTextCursor &c ) const { - return doc == c.doc && string == c.string && idx == c.idx; + return para == c.para && idx == c.idx; } int QTextCursor::totalOffsetX() const { - if ( !nested ) - return 0; - QValueStack<int>::ConstIterator xit = xOffsets.begin(); int xoff = ox; - for ( ; xit != xOffsets.end(); ++xit ) + for ( QValueStack<int>::ConstIterator xit = xOffsets.begin(); xit != xOffsets.end(); ++xit ) xoff += *xit; return xoff; } int QTextCursor::totalOffsetY() const { - if ( !nested ) - return 0; - QValueStack<int>::ConstIterator yit = yOffsets.begin(); int yoff = oy; - for ( ; yit != yOffsets.end(); ++yit ) + for ( QValueStack<int>::ConstIterator yit = yOffsets.begin(); yit != yOffsets.end(); ++yit ) yoff += *yit; return yoff; } void QTextCursor::gotoIntoNested( const QPoint &globalPos ) { - if ( !doc ) + if ( !para ) return; push(); ox = 0; int bl, y; - string->lineHeightOfChar( idx, &bl, &y ); - oy = y + string->rect().y(); - nested = TRUE; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); QPoint p( globalPos.x() - offsetX(), globalPos.y() - offsetY() ); - Q_ASSERT( string->at( idx )->isCustom() ); - ox = string->at( idx )->x; - string->at( idx )->customItem()->enterAt( this, doc, string, idx, ox, oy, p ); + Q_ASSERT( para->at( idx )->isCustom() ); + ox = para->at( idx )->x; + + QTextDocument* doc = document(); + para->at( idx )->customItem()->enterAt( this, doc, para, idx, ox, oy, p ); } void QTextCursor::invalidateNested() { - if ( nested ) { - QValueStack<QTextParag*>::Iterator it = parags.begin(); - QValueStack<int>::Iterator it2 = indices.begin(); - for ( ; it != parags.end(); ++it, ++it2 ) { - if ( *it == string ) - continue; - (*it)->invalidate( 0 ); - if ( (*it)->at( *it2 )->isCustom() ) - (*it)->at( *it2 )->customItem()->invalidate(); - } + QValueStack<QTextParagraph*>::Iterator it = paras.begin(); + QValueStack<int>::Iterator it2 = indices.begin(); + for ( ; it != paras.end(); ++it, ++it2 ) { + if ( *it == para ) + continue; + (*it)->invalidate( 0 ); + if ( (*it)->at( *it2 )->isCustom() ) + (*it)->at( *it2 )->customItem()->invalidate(); } } void QTextCursor::insert( const QString &str, bool checkNewLine, QMemArray<QTextStringChar> *formatting ) @@ -572,67 +498,65 @@ void QTextCursor::insert( const QString &str, bool checkNewLine, QMemArray<QText tmpIndex = -1; bool justInsert = TRUE; QString s( str ); #if defined(Q_WS_WIN) - if ( checkNewLine ) - s = s.replace( QRegExp( "\\r" ), "" ); + if ( checkNewLine ) { + int i = 0; + while ( ( i = s.find( '\r', i ) ) != -1 ) + s.remove( i ,1 ); + } #endif if ( checkNewLine ) justInsert = s.find( '\n' ) == -1; - if ( justInsert ) { - string->insert( idx, s ); + if ( justInsert ) { // we ignore new lines and insert all in the current para at the current index + para->insert( idx, s.unicode(), s.length() ); if ( formatting ) { for ( int i = 0; i < (int)s.length(); ++i ) { if ( formatting->at( i ).format() ) { formatting->at( i ).format()->addRef(); - string->string()->setFormat( idx + i, formatting->at( i ).format(), TRUE ); + para->string()->setFormat( idx + i, formatting->at( i ).format(), TRUE ); } } } idx += s.length(); - } else { - QStringList lst = QStringList::split( '\n', s, TRUE ); - QStringList::Iterator it = lst.begin(); - int y = string->rect().y() + string->rect().height(); + } else { // we split at new lines + int start = -1; + int end; + int y = para->rect().y() + para->rect().height(); int lastIndex = 0; - QTextFormat *lastFormat = 0; - for ( ; it != lst.end(); ) { - if ( it != lst.begin() ) { - splitAndInsertEmptyParag( FALSE, TRUE ); - string->setEndState( -1 ); - string->prev()->format( -1, FALSE ); - if ( lastFormat && formatting && string->prev() ) { - lastFormat->addRef(); - string->prev()->string()->setFormat( string->prev()->length() - 1, lastFormat, TRUE ); - } - } - lastFormat = 0; - QString s = *it; - ++it; - if ( !s.isEmpty() ) - string->insert( idx, s ); + do { + end = s.find( '\n', start + 1 ); // find line break + if ( end == -1 ) // didn't find one, so end of line is end of string + end = s.length(); + int len = (start == -1 ? end : end - start - 1); + if ( len > 0 ) // insert the line + para->insert( idx, s.unicode() + start + 1, len ); else - string->invalidate( 0 ); - if ( formatting ) { - int len = s.length(); + para->invalidate( 0 ); + if ( formatting ) { // set formats to the chars of the line for ( int i = 0; i < len; ++i ) { if ( formatting->at( i + lastIndex ).format() ) { formatting->at( i + lastIndex ).format()->addRef(); - string->string()->setFormat( i + idx, formatting->at( i + lastIndex ).format(), TRUE ); + para->string()->setFormat( i + idx, formatting->at( i + lastIndex ).format(), TRUE ); } } - if ( it != lst.end() ) - lastFormat = formatting->at( len + lastIndex ).format(); - ++len; lastIndex += len; } + start = end; // next start is at the end of this line + idx += len; // increase the index of the cursor to the end of the inserted text + if ( s[end] == '\n' ) { // if at the end was a line break, break the line + splitAndInsertEmptyParagraph( FALSE, TRUE ); + para->setEndState( -1 ); + para->prev()->format( -1, FALSE ); + lastIndex++; + } - idx += s.length(); - } - string->format( -1, FALSE ); - int dy = string->rect().y() + string->rect().height() - y; - QTextParag *p = string; - p->setParagId( p->prev()->paragId() + 1 ); + } while ( end < (int)s.length() ); + + para->format( -1, FALSE ); + int dy = para->rect().y() + para->rect().height() - y; + QTextParagraph *p = para; + p->setParagId( p->prev() ? p->prev()->paragId() + 1 : 0 ); p = p->next(); while ( p ) { p->setParagId( p->prev()->paragId() + 1 ); p->move( dy ); @@ -641,19 +565,19 @@ void QTextCursor::insert( const QString &str, bool checkNewLine, QMemArray<QText p = p->next(); } } - int h = string->rect().height(); - string->format( -1, TRUE ); - if ( h != string->rect().height() ) + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) invalidateNested(); - else if ( doc && doc->parent() ) - doc->nextDoubleBuffered = TRUE; + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; } void QTextCursor::gotoLeft() { - if ( string->string()->isRightToLeft() ) + if ( para->string()->isRightToLeft() ) gotoNextLetter(); else gotoPreviousLetter(); } @@ -663,76 +587,65 @@ void QTextCursor::gotoPreviousLetter() tmpIndex = -1; if ( idx > 0 ) { idx--; - } else if ( string->prev() ) { - QTextParag *s = string->prev(); - while ( s && !s->isVisible() ) - s = s->prev(); - if ( s ) { - string = s; - idx = string->length() - 1; - } - } else { - if ( nested ) { + const QTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) + processNesting( EnterEnd ); + } else if ( para->prev() ) { + para = para->prev(); + while ( !para->isVisible() && para->prev() ) + para = para->prev(); + idx = para->length() - 1; + } else if ( nestedDepth() ) { + pop(); + processNesting( Prev ); + if ( idx == -1 ) { pop(); - processNesting( Prev ); - if ( idx == -1 ) { - pop(); - if ( idx > 0 ) { - idx--; - } else if ( string->prev() ) { - string = string->prev(); - idx = string->length() - 1; - } + if ( idx > 0 ) { + idx--; + } else if ( para->prev() ) { + para = para->prev(); + idx = para->length() - 1; } } } - - const QTextStringChar *tsc = string->at( idx ); - if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) { - processNesting( EnterEnd ); - } } void QTextCursor::push() { indices.push( idx ); - parags.push( string ); + paras.push( para ); xOffsets.push( ox ); yOffsets.push( oy ); - nestedStack.push( nested ); } void QTextCursor::pop() { - if ( !doc ) + if ( indices.isEmpty() ) return; idx = indices.pop(); - string = parags.pop(); + para = paras.pop(); ox = xOffsets.pop(); oy = yOffsets.pop(); - if ( doc->parent() ) - doc = doc->parent(); - nested = nestedStack.pop(); } void QTextCursor::restoreState() { while ( !indices.isEmpty() ) pop(); } -bool QTextCursor::place( const QPoint &p, QTextParag *s, bool link ) +bool QTextCursor::place( const QPoint &p, QTextParagraph *s, bool link ) { QPoint pos( p ); QRect r; - QTextParag *str = s; + QTextParagraph *str = s; if ( pos.y() < s->rect().y() ) pos.setY( s->rect().y() ); while ( s ) { r = s->rect(); - r.setWidth( doc ? doc->width() : QWIDGETSIZE_MAX ); + r.setWidth( document() ? document()->width() : QWIDGETSIZE_MAX ); if ( s->isVisible() ) str = s; if ( pos.y() >= r.y() && pos.y() <= r.y() + r.height() || !s->next() ) break; @@ -743,9 +656,9 @@ bool QTextCursor::place( const QPoint &p, QTextParag *s, bool link ) return FALSE; s = str; - setParag( s, FALSE ); + setParagraph( s ); int y = s->rect().y(); int lines = s->lines(); QTextStringChar *chr = 0; int index = 0; @@ -757,9 +670,9 @@ bool QTextCursor::place( const QPoint &p, QTextParag *s, bool link ) cy = s->lineY( i ); ch = s->lineHeight( i ); if ( !chr ) return FALSE; - if ( pos.y() >= y + cy && pos.y() <= y + cy + ch ) + if ( pos.y() <= y + cy + ch ) break; } int nextLine; if ( i < lines - 1 ) @@ -797,61 +710,61 @@ bool QTextCursor::place( const QPoint &p, QTextParag *s, bool link ) } } i++; } - setIndex( curpos, FALSE ); + setIndex( curpos ); - if ( inCustom && doc && parag()->at( curpos )->isCustom() && parag()->at( curpos )->customItem()->isNested() ) { - QTextDocument *oldDoc = doc; + if ( inCustom && para->document() && para->at( curpos )->isCustom() && para->at( curpos )->customItem()->isNested() ) { + QTextDocument *oldDoc = para->document(); gotoIntoNested( pos ); - if ( oldDoc == doc ) + if ( oldDoc == para->document() ) return TRUE; QPoint p( pos.x() - offsetX(), pos.y() - offsetY() ); - if ( !place( p, document()->firstParag(), link ) ) + if ( !place( p, document()->firstParagraph(), link ) ) pop(); } return TRUE; } void QTextCursor::processNesting( Operation op ) { - if ( !doc ) + if ( !para->document() ) return; + QTextDocument* doc = para->document(); push(); - ox = string->at( idx )->x; + ox = para->at( idx )->x; int bl, y; - string->lineHeightOfChar( idx, &bl, &y ); - oy = y + string->rect().y(); - nested = TRUE; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); bool ok = FALSE; switch ( op ) { case EnterBegin: - ok = string->at( idx )->customItem()->enter( this, doc, string, idx, ox, oy ); + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy ); break; case EnterEnd: - ok = string->at( idx )->customItem()->enter( this, doc, string, idx, ox, oy, TRUE ); + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy, TRUE ); break; case Next: - ok = string->at( idx )->customItem()->next( this, doc, string, idx, ox, oy ); + ok = para->at( idx )->customItem()->next( this, doc, para, idx, ox, oy ); break; case Prev: - ok = string->at( idx )->customItem()->prev( this, doc, string, idx, ox, oy ); + ok = para->at( idx )->customItem()->prev( this, doc, para, idx, ox, oy ); break; case Down: - ok = string->at( idx )->customItem()->down( this, doc, string, idx, ox, oy ); + ok = para->at( idx )->customItem()->down( this, doc, para, idx, ox, oy ); break; case Up: - ok = string->at( idx )->customItem()->up( this, doc, string, idx, ox, oy ); + ok = para->at( idx )->customItem()->up( this, doc, para, idx, ox, oy ); break; } if ( !ok ) pop(); } void QTextCursor::gotoRight() { - if ( string->string()->isRightToLeft() ) + if ( para->string()->isRightToLeft() ) gotoPreviousLetter(); else gotoNextLetter(); } @@ -859,36 +772,31 @@ void QTextCursor::gotoRight() void QTextCursor::gotoNextLetter() { tmpIndex = -1; - const QTextStringChar *tsc = string->at( idx ); + const QTextStringChar *tsc = para->at( idx ); if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) { processNesting( EnterBegin ); return; } - if ( idx < string->length() - 1 ) { + if ( idx < para->length() - 1 ) { idx++; - } else if ( string->next() ) { - QTextParag *s = string->next(); - while ( s && !s->isVisible() ) - s = s->next(); - if ( s ) { - string = s; - idx = 0; - } - } else { - if ( nested ) { + } else if ( para->next() ) { + para = para->next(); + while ( !para->isVisible() && para->next() ) + para = para->next(); + idx = 0; + } else if ( nestedDepth() ) { + pop(); + processNesting( Next ); + if ( idx == -1 ) { pop(); - processNesting( Next ); - if ( idx == -1 ) { - pop(); - if ( idx < string->length() - 1 ) { - idx++; - } else if ( string->next() ) { - string = string->next(); - idx = 0; - } + if ( idx < para->length() - 1 ) { + idx++; + } else if ( para->next() ) { + para = para->next(); + idx = 0; } } } } @@ -896,45 +804,45 @@ void QTextCursor::gotoNextLetter() void QTextCursor::gotoUp() { int indexOfLineStart; int line; - QTextStringChar *c = string->lineStartOfChar( idx, &indexOfLineStart, &line ); + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); if ( !c ) return; tmpIndex = QMAX( tmpIndex, idx - indexOfLineStart ); if ( indexOfLineStart == 0 ) { - if ( !string->prev() ) { - if ( !nested ) + if ( !para->prev() ) { + if ( !nestedDepth() ) return; pop(); processNesting( Up ); if ( idx == -1 ) { pop(); - if ( !string->prev() ) + if ( !para->prev() ) return; idx = tmpIndex = 0; } else { tmpIndex = -1; return; } } - QTextParag *s = string->prev(); - while ( s && !s->isVisible() ) - s = s->prev(); - if ( s ) - string = s; - int lastLine = string->lines() - 1; - if ( !string->lineStartOfLine( lastLine, &indexOfLineStart ) ) + QTextParagraph *p = para->prev(); + while ( p && !p->isVisible() ) + p = p->prev(); + if ( p ) + para = p; + int lastLine = para->lines() - 1; + if ( !para->lineStartOfLine( lastLine, &indexOfLineStart ) ) return; - if ( indexOfLineStart + tmpIndex < string->length() ) + if ( indexOfLineStart + tmpIndex < para->length() ) idx = indexOfLineStart + tmpIndex; else - idx = string->length() - 1; + idx = para->length() - 1; } else { --line; int oldIndexOfLineStart = indexOfLineStart; - if ( !string->lineStartOfLine( line, &indexOfLineStart ) ) + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) return; if ( indexOfLineStart + tmpIndex < oldIndexOfLineStart ) idx = indexOfLineStart + tmpIndex; else @@ -945,53 +853,53 @@ void QTextCursor::gotoUp() void QTextCursor::gotoDown() { int indexOfLineStart; int line; - QTextStringChar *c = string->lineStartOfChar( idx, &indexOfLineStart, &line ); + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); if ( !c ) return; tmpIndex = QMAX( tmpIndex, idx - indexOfLineStart ); - if ( line == string->lines() - 1 ) { - if ( !string->next() ) { - if ( !nested ) + if ( line == para->lines() - 1 ) { + if ( !para->next() ) { + if ( !nestedDepth() ) return; pop(); processNesting( Down ); if ( idx == -1 ) { pop(); - if ( !string->next() ) + if ( !para->next() ) return; idx = tmpIndex = 0; } else { tmpIndex = -1; return; } } - QTextParag *s = string->next(); + QTextParagraph *s = para->next(); while ( s && !s->isVisible() ) s = s->next(); if ( s ) - string = s; - if ( !string->lineStartOfLine( 0, &indexOfLineStart ) ) + para = s; + if ( !para->lineStartOfLine( 0, &indexOfLineStart ) ) return; int end; - if ( string->lines() == 1 ) - end = string->length(); + if ( para->lines() == 1 ) + end = para->length(); else - string->lineStartOfLine( 1, &end ); + para->lineStartOfLine( 1, &end ); if ( indexOfLineStart + tmpIndex < end ) idx = indexOfLineStart + tmpIndex; else idx = end - 1; } else { ++line; int end; - if ( line == string->lines() - 1 ) - end = string->length(); + if ( line == para->lines() - 1 ) + end = para->length(); else - string->lineStartOfLine( line + 1, &end ); - if ( !string->lineStartOfLine( line, &indexOfLineStart ) ) + para->lineStartOfLine( line + 1, &end ); + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) return; if ( indexOfLineStart + tmpIndex < end ) idx = indexOfLineStart + tmpIndex; else @@ -1003,16 +911,16 @@ void QTextCursor::gotoLineEnd() { tmpIndex = -1; int indexOfLineStart; int line; - QTextStringChar *c = string->lineStartOfChar( idx, &indexOfLineStart, &line ); + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); if ( !c ) return; - if ( line == string->lines() - 1 ) { - idx = string->length() - 1; + if ( line == para->lines() - 1 ) { + idx = para->length() - 1; } else { - c = string->lineStartOfLine( ++line, &indexOfLineStart ); + c = para->lineStartOfLine( ++line, &indexOfLineStart ); indexOfLineStart--; idx = indexOfLineStart; } } @@ -1021,90 +929,63 @@ void QTextCursor::gotoLineStart() { tmpIndex = -1; int indexOfLineStart; int line; - QTextStringChar *c = string->lineStartOfChar( idx, &indexOfLineStart, &line ); + QTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); if ( !c ) return; idx = indexOfLineStart; } void QTextCursor::gotoHome() { - tmpIndex = -1; - if ( doc ) - string = doc->firstParag(); - idx = 0; + if ( topParagraph()->document() ) + gotoPosition( topParagraph()->document()->firstParagraph() ); + else + gotoLineStart(); } void QTextCursor::gotoEnd() { - if ( doc && !doc->lastParag()->isValid() ) - return; - - tmpIndex = -1; - if ( doc ) - string = doc->lastParag(); - idx = string->length() - 1; + if ( topParagraph()->document() && topParagraph()->document()->lastParagraph()->isValid() ) + gotoPosition( topParagraph()->document()->lastParagraph(), + topParagraph()->document()->lastParagraph()->length() - 1); + else + gotoLineEnd(); } void QTextCursor::gotoPageUp( int visibleHeight ) { - tmpIndex = -1; - QTextParag *s = string; - int h = visibleHeight; - int y = s->rect().y(); - while ( s ) { - if ( y - s->rect().y() >= h ) - break; - s = s->prev(); - } - - if ( !s && doc ) - s = doc->firstParag(); - - string = s; - idx = 0; + int targetY = globalY() - visibleHeight; + QTextParagraph* old; int index; + do { + old = para; index = idx; + gotoUp(); + } while ( (old != para || index != idx) && globalY() > targetY ); } void QTextCursor::gotoPageDown( int visibleHeight ) { - tmpIndex = -1; - QTextParag *s = string; - int h = visibleHeight; - int y = s->rect().y(); - while ( s ) { - if ( s->rect().y() - y >= h ) - break; - s = s->next(); - } - - if ( !s && doc ) { - s = doc->lastParag(); - string = s; - idx = string->length() - 1; - return; - } - - if ( !s->isValid() ) - return; - - string = s; - idx = 0; + int targetY = globalY() + visibleHeight; + QTextParagraph* old; int index; + do { + old = para; index = idx; + gotoDown(); + } while ( (old != para || index != idx) && globalY() < targetY ); } void QTextCursor::gotoWordRight() { - if ( string->string()->isRightToLeft() ) + if ( para->string()->isRightToLeft() ) gotoPreviousWord(); else gotoNextWord(); } void QTextCursor::gotoWordLeft() { - if ( string->string()->isRightToLeft() ) + if ( para->string()->isRightToLeft() ) gotoNextWord(); else gotoPreviousWord(); } @@ -1112,9 +993,9 @@ void QTextCursor::gotoWordLeft() void QTextCursor::gotoPreviousWord() { gotoPreviousLetter(); tmpIndex = -1; - QTextString *s = string->string(); + QTextString *s = para->string(); bool allowSame = FALSE; if ( idx == ((int)s->length()-1) ) return; for ( int i = idx; i >= 0; --i ) { @@ -1134,9 +1015,9 @@ void QTextCursor::gotoPreviousWord() void QTextCursor::gotoNextWord() { tmpIndex = -1; - QTextString *s = string->string(); + QTextString *s = para->string(); bool allowSame = FALSE; for ( int i = idx; i < (int)s->length(); ++i ) { if ( ! (s->at( i ).c.isSpace() || s->at( i ).c == '\t' || s->at( i ).c == '.' || s->at( i ).c == ',' || s->at( i ).c == ':' || s->at( i ).c == ';') ) { @@ -1152,14 +1033,14 @@ void QTextCursor::gotoNextWord() } if ( idx < ((int)s->length()-1) ) { gotoLineEnd(); - } else if ( string->next() ) { - QTextParag *s = string->next(); - while ( s && !s->isVisible() ) - s = s->next(); + } else if ( para->next() ) { + QTextParagraph *p = para->next(); + while ( p && !p->isVisible() ) + p = p->next(); if ( s ) { - string = s; + para = p; idx = 0; } } else { gotoLineEnd(); @@ -1172,77 +1053,81 @@ bool QTextCursor::atParagStart() } bool QTextCursor::atParagEnd() { - return idx == string->length() - 1; + return idx == para->length() - 1; } -void QTextCursor::splitAndInsertEmptyParag( bool ind, bool updateIds ) +void QTextCursor::splitAndInsertEmptyParagraph( bool ind, bool updateIds ) { - if ( !doc ) + if ( !para->document() ) return; tmpIndex = -1; QTextFormat *f = 0; - if ( doc->useFormatCollection() ) { - f = string->at( idx )->format(); - if ( idx == string->length() - 1 && idx > 0 ) - f = string->at( idx - 1 )->format(); + if ( para->document()->useFormatCollection() ) { + f = para->at( idx )->format(); + if ( idx == para->length() - 1 && idx > 0 ) + f = para->at( idx - 1 )->format(); if ( f->isMisspelled() ) { f->removeRef(); - f = doc->formatCollection()->format( f->font(), f->color() ); + f = para->document()->formatCollection()->format( f->font(), f->color() ); } } if ( atParagEnd() ) { - QTextParag *n = string->next(); - QTextParag *s = doc->createParag( doc, string, n, updateIds ); + QTextParagraph *n = para->next(); + QTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); if ( f ) s->setFormat( 0, 1, f, TRUE ); - s->copyParagData( string ); + s->copyParagData( para ); if ( ind ) { int oi, ni; s->indent( &oi, &ni ); - string = s; + para = s; idx = ni; } else { - string = s; + para = s; idx = 0; } } else if ( atParagStart() ) { - QTextParag *p = string->prev(); - QTextParag *s = doc->createParag( doc, p, string, updateIds ); + QTextParagraph *p = para->prev(); + QTextParagraph *s = para->document()->createParagraph( para->document(), p, para, updateIds ); if ( f ) s->setFormat( 0, 1, f, TRUE ); - s->copyParagData( string ); + s->copyParagData( para ); if ( ind ) { s->indent(); s->format(); indent(); - string->format(); + para->format(); } } else { - QString str = string->string()->toString().mid( idx, 0xFFFFFF ); - QTextParag *n = string->next(); - QTextParag *s = doc->createParag( doc, string, n, updateIds ); - s->copyParagData( string ); + QString str = para->string()->toString().mid( idx, 0xFFFFFF ); + QTextParagraph *n = para->next(); + QTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); + s->copyParagData( para ); s->remove( 0, 1 ); s->append( str, TRUE ); for ( uint i = 0; i < str.length(); ++i ) { - s->setFormat( i, 1, string->at( idx + i )->format(), TRUE ); - if ( string->at( idx + i )->isCustom() ) { - QTextCustomItem * item = string->at( idx + i )->customItem(); + QTextStringChar* tsc = para->at( idx + i ); + s->setFormat( i, 1, tsc->format(), TRUE ); + if ( tsc->isCustom() ) { + QTextCustomItem * item = tsc->customItem(); s->at( i )->setCustomItem( item ); - string->at( idx + i )->loseCustomItem(); + tsc->loseCustomItem(); } + if ( tsc->isAnchor() ) + s->at( i )->setAnchor( tsc->anchorName(), + tsc->anchorHref() ); } - string->truncate( idx ); + para->truncate( idx ); if ( ind ) { int oi, ni; s->indent( &oi, &ni ); - string = s; + para = s; idx = ni; } else { - string = s; + para = s; idx = 0; } } @@ -1252,60 +1137,28 @@ void QTextCursor::splitAndInsertEmptyParag( bool ind, bool updateIds ) bool QTextCursor::remove() { tmpIndex = -1; if ( !atParagEnd() ) { - string->remove( idx, 1 ); - int h = string->rect().height(); - string->format( -1, TRUE ); - if ( h != string->rect().height() ) + para->remove( idx, 1 ); + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) invalidateNested(); - else if ( doc && doc->parent() ) - doc->nextDoubleBuffered = TRUE; + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; return FALSE; - } else if ( string->next() ) { - if ( string->length() == 1 ) { - string->next()->setPrev( string->prev() ); - if ( string->prev() ) - string->prev()->setNext( string->next() ); - QTextParag *p = string->next(); - delete string; - string = p; - string->invalidate( 0 ); - QTextParag *s = string; - while ( s ) { - s->id = s->p ? s->p->id + 1 : 0; - s->state = -1; - s->needPreProcess = TRUE; - s->changed = TRUE; - s = s->n; - } - string->format(); - } else { - string->join( string->next() ); - } + } else if ( para->next() ) { + para->join( para->next() ); invalidateNested(); return TRUE; } return FALSE; } -void QTextCursor::killLine() -{ - if ( atParagEnd() ) - return; - string->remove( idx, string->length() - idx - 1 ); - int h = string->rect().height(); - string->format( -1, TRUE ); - if ( h != string->rect().height() ) - invalidateNested(); - else if ( doc && doc->parent() ) - doc->nextDoubleBuffered = TRUE; -} - void QTextCursor::indent() { int oi = 0, ni = 0; - string->indent( &oi, &ni ); + para->indent( &oi, &ni ); if ( oi == ni ) return; if ( idx >= oi ) @@ -1313,39 +1166,26 @@ void QTextCursor::indent() else idx = ni; } -void QTextCursor::setDocument( QTextDocument *d ) -{ - doc = d; - string = d->firstParag(); - idx = 0; - nested = FALSE; - restoreState(); - tmpIndex = -1; -} - // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ QTextDocument::QTextDocument( QTextDocument *p ) - : par( p ), parParag( 0 ), tc( 0 ), tArray( 0 ), tStopWidth( 0 ) + : par( p ), parentPar( 0 ), tc( 0 ), tArray( 0 ), tStopWidth( 0 ) { fCollection = new QTextFormatCollection; init(); } QTextDocument::QTextDocument( QTextDocument *p, QTextFormatCollection *f ) - : par( p ), parParag( 0 ), tc( 0 ), tArray( 0 ), tStopWidth( 0 ) + : par( p ), parentPar( 0 ), tc( 0 ), tArray( 0 ), tStopWidth( 0 ) { fCollection = f; init(); } void QTextDocument::init() { -#if defined(PARSER_DEBUG) - qDebug( debug_indent + "new QTextDocument (%p)", this ); -#endif oTextValid = TRUE; mightHaveCustomItems = FALSE; if ( par ) par->insertChild( this ); @@ -1362,14 +1202,12 @@ void QTextDocument::init() wused = 0; minwParag = curParag = 0; align = AlignAuto; nSelections = 1; - addMargs = FALSE; - sheet_ = QStyleSheet::defaultSheet(); + setStyleSheet( QStyleSheet::defaultSheet() ); factory_ = QMimeSourceFactory::defaultFactory(); contxt = QString::null; - fCollection->setStyleSheet( sheet_ ); underlLinks = par ? par->underlLinks : TRUE; backBrush = 0; buf_pixmap = 0; @@ -1379,10 +1217,9 @@ void QTextDocument::init() withoutDoubleBuffer = par->withoutDoubleBuffer; else withoutDoubleBuffer = FALSE; - lParag = fParag = createParag( this, 0, 0 ); - tmpCursor = 0; + lParag = fParag = createParagraph( this, 0, 0 ); cx = 0; cy = 2; if ( par ) @@ -1392,8 +1229,10 @@ void QTextDocument::init() flow_ = new QTextFlow; flow_->setWidth( cw ); leftmargin = rightmargin = 4; + scaleFontsFactor = 1; + selectionColors[ Standard ] = QApplication::palette().color( QPalette::Active, QColorGroup::Highlight ); selectionText[ Standard ] = TRUE; commandHistory = new QTextCommandHistory( 100 ); @@ -1422,16 +1261,18 @@ void QTextDocument::clear( bool createEmptyParag ) { if ( flow_ ) flow_->clear(); while ( fParag ) { - QTextParag *p = fParag->next(); + QTextParagraph *p = fParag->next(); delete fParag; fParag = p; } fParag = lParag = 0; if ( createEmptyParag ) - fParag = lParag = createParag( this ); + fParag = lParag = createParagraph( this ); selections.clear(); + oText = QString::null; + oTextValid = TRUE; } int QTextDocument::widthUsed() const { @@ -1448,14 +1289,14 @@ int QTextDocument::height() const } -QTextParag *QTextDocument::createParag( QTextDocument *d, QTextParag *pr, QTextParag *nx, bool updateIds ) +QTextParagraph *QTextDocument::createParagraph( QTextDocument *d, QTextParagraph *pr, QTextParagraph *nx, bool updateIds ) { - return new QTextParag( d, pr, nx, updateIds ); + return new QTextParagraph( d, pr, nx, updateIds ); } -bool QTextDocument::setMinimumWidth( int needed, int used, QTextParag *p ) +bool QTextDocument::setMinimumWidth( int needed, int used, QTextParagraph *p ) { if ( needed == -1 ) { minw = 0; wused = 0; @@ -1484,9 +1325,9 @@ void QTextDocument::setPlainText( const QString &text ) int lastNl = 0; int nl = text.find( '\n' ); if ( nl == -1 ) { - lParag = createParag( this, lParag, 0 ); + lParag = createParagraph( this, lParag, 0 ); if ( !fParag ) fParag = lParag; QString s = text; if ( !s.isEmpty() ) { @@ -1495,9 +1336,9 @@ void QTextDocument::setPlainText( const QString &text ) lParag->append( s ); } } else { for (;;) { - lParag = createParag( this, lParag, 0 ); + lParag = createParagraph( this, lParag, 0 ); if ( !fParag ) fParag = lParag; QString s = text.mid( lastNl, nl - lastNl ); if ( !s.isEmpty() ) { @@ -1513,9 +1354,9 @@ void QTextDocument::setPlainText( const QString &text ) nl = 0xffffff; } } if ( !lParag ) - lParag = fParag = createParag( this, 0, 0 ); + lParag = fParag = createParagraph( this, 0, 0 ); } struct Q_EXPORT QTextDocumentTag { QTextDocumentTag(){} @@ -1558,77 +1399,79 @@ struct Q_EXPORT QTextDocumentTag { bool operator==( const QTextDocumentTag& ) const { return FALSE; } #endif }; -#define NEWPAR do{ if ( !hasNewPar ) curpar = createParag( this, curpar ); \ - if ( curpar->isBr ) curpar->isBr = FALSE; \ + +#define NEWPAR do{ if ( !hasNewPar) { \ + if ( !textEditMode && curpar && curpar->length()>1 && curpar->at( curpar->length()-2)->c == QChar_linesep ) \ + curpar->remove( curpar->length()-2, 1 ); \ + curpar = createParagraph( this, curpar, curpar->next() ); styles.append( vec ); vec = 0;} \ hasNewPar = TRUE; \ - curpar->setAlignment( curtag.alignment ); \ - curpar->setDirection( (QChar::Direction)curtag.direction ); \ + curpar->rtext = TRUE; \ + curpar->align = curtag.alignment; \ + curpar->lstyle = curtag.liststyle; \ + curpar->litem = ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ); \ + curpar->str->setDirection( (QChar::Direction)curtag.direction ); \ space = TRUE; \ - QPtrVector<QStyleSheetItem> vec( (uint)tags.count() + 1); \ + delete vec; vec = new QPtrVector<QStyleSheetItem>( (uint)tags.count() + 1); \ int i = 0; \ for ( QValueStack<QTextDocumentTag>::Iterator it = tags.begin(); it != tags.end(); ++it ) \ - vec.insert( i++, (*it).style ); \ - vec.insert( i, curtag.style ); \ - curpar->setStyleSheetItems( vec ); }while(FALSE) + vec->insert( i++, (*it).style ); \ + vec->insert( i, curtag.style ); \ + }while(FALSE) void QTextDocument::setRichText( const QString &text, const QString &context ) { - setTextFormat( Qt::RichText ); if ( !context.isEmpty() ) setContext( context ); clear(); - fParag = lParag = createParag( this ); + fParag = lParag = createParagraph( this ); + oTextValid = TRUE; + oText = text; setRichTextInternal( text ); + fParag->rtext = TRUE; } -static QStyleSheetItem::ListStyle chooseListStyle( const QStyleSheetItem *nstyle, - const QMap<QString, QString> &attr, - QStyleSheetItem::ListStyle curListStyle ) -{ - if ( nstyle->name() == "ol" || nstyle->name() == "ul" ) { - curListStyle = nstyle->listStyle(); - QMap<QString, QString>::ConstIterator it = attr.find( "type" ); - if ( it != attr.end() ) { - QString sl = *it; - if ( sl == "1" ) { - curListStyle = QStyleSheetItem::ListDecimal; - } else if ( sl == "a" ) { - curListStyle = QStyleSheetItem::ListLowerAlpha; - } else if ( sl == "A" ) { - curListStyle = QStyleSheetItem::ListUpperAlpha; - } else { - sl = sl.lower(); - if ( sl == "square" ) - curListStyle = QStyleSheetItem::ListSquare; - else if ( sl == "disc" ) - curListStyle = QStyleSheetItem::ListDisc; - else if ( sl == "circle" ) - curListStyle = QStyleSheetItem::ListCircle; - } - } - } - return curListStyle; -} - -void QTextDocument::setRichTextInternal( const QString &text ) +void QTextDocument::setRichTextInternal( const QString &text, QTextCursor* cursor ) { - oTextValid = TRUE; - oText = text; - QTextParag* curpar = lParag; + QTextParagraph* curpar = lParag; int pos = 0; QValueStack<QTextDocumentTag> tags; QTextDocumentTag initag( "", sheet_->item(""), *formatCollection()->defaultFormat() ); QTextDocumentTag curtag = initag; bool space = TRUE; + bool canMergeLi = FALSE; + + bool textEditMode = FALSE; const QChar* doc = text.unicode(); int length = text.length(); bool hasNewPar = curpar->length() <= 1; - QString lastClose; QString anchorName; + + // style sheet handling for margin and line spacing calculation below + QTextParagraph* stylesPar = curpar; + QPtrVector<QStyleSheetItem>* vec = 0; + QPtrList< QPtrVector<QStyleSheetItem> > styles; + styles.setAutoDelete( TRUE ); + + if ( cursor ) { + cursor->splitAndInsertEmptyParagraph(); + QTextCursor tmp = *cursor; + tmp.gotoPreviousLetter(); + stylesPar = curpar = tmp.paragraph(); + hasNewPar = TRUE; + textEditMode = TRUE; + } else { + NEWPAR; + } + + // set rtext spacing to FALSE for the initial paragraph. + curpar->rtext = FALSE; + + QString wellKnownTags = "br hr wsp table qt body meta title"; + while ( pos < length ) { if ( hasPrefix(doc, length, pos, '<' ) ){ if ( !hasPrefix( doc, length, pos+1, QChar('/') ) ) { // open tag @@ -1637,35 +1480,10 @@ void QTextDocument::setRichTextInternal( const QString &text ) QString tagname = parseOpenTag(doc, length, pos, attr, emptyTag); if ( tagname.isEmpty() ) continue; // nothing we could do with this, probably parse error - if ( tagname == "title" ) { - QString title; - while ( pos < length ) { - if ( hasPrefix( doc, length, pos, QChar('<') ) && hasPrefix( doc, length, pos+1, QChar('/') ) && - parseCloseTag( doc, length, pos ) == "title" ) - break; - title += doc[ pos ]; - ++pos; - } - attribs.replace( "title", title ); - } - const QStyleSheetItem* nstyle = sheet_->item(tagname); - if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { -// if ( tagname == "br" ) { -// // our standard br emty-tag handling breaks -// // inside list items, we would get another -// // list item in this case. As workaround, fake -// // a new paragraph instead -// tagname = "p"; -// nstyle = sheet_->item( tagname ); -// } - if ( nstyle ) - hasNewPar = FALSE; // we want empty paragraphs in this case - } - if ( nstyle ) { // we might have to close some 'forgotten' tags while ( !nstyle->allowedInContext( curtag.style ) ) { QString msg; @@ -1676,69 +1494,87 @@ void QTextDocument::setRichTextInternal( const QString &text ) break; curtag = tags.pop(); } - // special handling for p. We do not want to nest there for HTML compatibility - if ( nstyle->displayMode() == QStyleSheetItem::DisplayBlock ) { + /* special handling for p and li for HTML + compatibility. We do not want to embed blocks in + p, and we do not want new blocks inside non-empty + lis. Plus we want to merge empty lis sometimes. */ + if( nstyle->displayMode() == QStyleSheetItem::DisplayListItem ) { + canMergeLi = TRUE; + } else if ( nstyle->displayMode() == QStyleSheetItem::DisplayBlock ) { while ( curtag.style->name() == "p" ) { if ( tags.isEmpty() ) break; curtag = tags.pop(); } - } + if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + // we are in a li and a new block comes along + if ( nstyle->name() == "ul" || nstyle->name() == "ol" ) + hasNewPar = FALSE; // we want an empty li (like most browsers) + if ( !hasNewPar ) { + /* do not add new blocks inside + non-empty lis */ + while ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + } else if ( canMergeLi ) { + /* we have an empty li and a block + comes along, merge them */ + nstyle = curtag.style; + } + canMergeLi = FALSE; + } + } } QTextCustomItem* custom = 0; - // some well-known empty tags - if ( tagname == "br" ) { - emptyTag = TRUE; - hasNewPar = FALSE; - if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { - // when linebreaking a list item, we do not - // actually want a new list item but just a - // new line. Fake this by pushing a paragraph - // onto the stack - tags.push( curtag ); - curtag.name = tagname; - curtag.style = nstyle; - } - NEWPAR; - curpar->isBr = TRUE; - curpar->setAlignment( curtag.alignment ); - } else if ( tagname == "hr" ) { - emptyTag = TRUE; - custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); - NEWPAR; - } else if ( tagname == "table" ) { - QTextFormat format = curtag.format.makeTextFormat( nstyle, attr ); - curpar->setAlignment( curtag.alignment ); - custom = parseTable( attr, format, doc, length, pos, curpar ); - (void)eatSpace( doc, length, pos ); - emptyTag = TRUE; - } else if ( tagname == "qt" ) { - for ( QMap<QString, QString>::Iterator it = attr.begin(); it != attr.end(); ++it ) { - if ( it.key() == "bgcolor" ) { - QBrush *b = new QBrush( QColor( *it ) ); + + // some well-known tags, some have a nstyle, some not + if ( wellKnownTags.find( tagname ) != -1 ) { + if ( tagname == "br" ) { + emptyTag = space = TRUE; + int index = QMAX( curpar->length(),1) - 1; + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->append( QChar_linesep ); + curpar->setFormat( index, 1, &format ); + } else if ( tagname == "hr" ) { + emptyTag = space = TRUE; + custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); + NEWPAR; + } else if ( tagname == "table" ) { + emptyTag = space = TRUE; + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->setAlignment( curtag.alignment ); + custom = parseTable( attr, format, doc, length, pos, curpar ); + } else if ( tagname == "qt" || tagname == "body" ) { + if ( attr.contains( "bgcolor" ) ) { + QBrush *b = new QBrush( QColor( attr["bgcolor"] ) ); setPaper( b ); - } else if ( it.key() == "background" ) { + } + if ( attr.contains( "background" ) ) { QImage img; - const QMimeSource* m = factory_->data( *it, contxt ); + QString bg = attr["background"]; + const QMimeSource* m = factory_->data( bg, contxt ); if ( !m ) { - qWarning("QRichText: no mimesource for %s", (*it).latin1() ); + qWarning("QRichText: no mimesource for %s", bg.latin1() ); } else { if ( !QImageDrag::decode( m, img ) ) { - qWarning("QTextImage: cannot decode %s", (*it).latin1() ); + qWarning("QTextImage: cannot decode %s", bg.latin1() ); } } if ( !img.isNull() ) { QPixmap pm; pm.convertFromImage( img ); QBrush *b = new QBrush( QColor(), pm ); setPaper( b ); } - } else if ( it.key() == "text" ) { - QColor c( *it ); + } + if ( attr.contains( "text" ) ) { + QColor c( attr["text"] ); if ( formatCollection()->defaultFormat()->color() != c ) { QDict<QTextFormat> formats = formatCollection()->dict(); QDictIterator<QTextFormat> it( formats ); while ( it.current() ) { @@ -1751,26 +1587,53 @@ void QTextDocument::setRichTextInternal( const QString &text ) } formatCollection()->defaultFormat()->setColor( c ); curtag.format.setColor( c ); } - } else if ( it.key() == "link" ) { - linkColor = QColor( *it ); - } else if ( it.key() == "title" ) { - attribs.replace( it.key(), *it ); } + if ( attr.contains( "link" ) ) + linkColor = QColor( attr["link"] ); + if ( attr.contains( "title" ) ) + attribs.replace( "title", attr["title"] ); + + if ( textEditMode ) { + if ( attr.contains("style" ) ) { + QString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + QString style = QTextDocument::section( a, ";", s, s ); + if ( style.startsWith("font-size:" ) && QTextDocument::endsWith(style, "pt") ) { + scaleFontsFactor = double( formatCollection()->defaultFormat()->fn.pointSize() ) / + style.mid( 10, style.length() - 12 ).toInt(); + } + } + } + nstyle = 0; // ignore body in textEditMode + } + // end qt- and body-tag handling + } else if ( tagname == "meta" ) { + if ( attr["name"] == "qrichtext" && attr["content"] == "1" ) + textEditMode = TRUE; + } else if ( tagname == "title" ) { + QString title; + while ( pos < length ) { + if ( hasPrefix( doc, length, pos, QChar('<') ) && hasPrefix( doc, length, pos+1, QChar('/') ) && + parseCloseTag( doc, length, pos ) == "title" ) + break; + title += doc[ pos ]; + ++pos; + } + attribs.replace( "title", title ); } - } else { + } // end of well-known tag handling + + if ( !custom ) // try generic custom item custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); - } if ( !nstyle && !custom ) // we have no clue what this tag could be, ignore it continue; if ( custom ) { - int index = curpar->length() - 1; - if ( index < 0 ) - index = 0; - QTextFormat format = curtag.format.makeTextFormat( nstyle, attr ); + int index = QMAX( curpar->length(),1) - 1; + QTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); curpar->append( QChar('*') ); curpar->setFormat( index, 1, &format ); curpar->at( index )->setCustomItem( custom ); if ( !curtag.anchorHref.isEmpty() ) @@ -1781,34 +1644,33 @@ void QTextDocument::setRichTextInternal( const QString &text ) } registerCustomItem( custom, curpar ); hasNewPar = FALSE; } else if ( !emptyTag ) { - // ignore whitespace for inline elements if there was already one - if ( nstyle->whiteSpaceMode() == QStyleSheetItem::WhiteSpaceNormal - && ( space || nstyle->displayMode() != QStyleSheetItem::DisplayInline ) ) - eatSpace( doc, length, pos ); - - // if we do nesting, push curtag on the stack, - // otherwise reinint curag. - if ( nstyle != curtag.style || nstyle->selfNesting() ) { + /* if we do nesting, push curtag on the stack, + otherwise reinint curag. */ + if ( curtag.style->name() != tagname || nstyle->selfNesting() ) { tags.push( curtag ); } else { if ( !tags.isEmpty() ) curtag = tags.top(); else curtag = initag; } - const QStyleSheetItem* ostyle = curtag.style; - curtag.name = tagname; curtag.style = nstyle; curtag.name = tagname; curtag.style = nstyle; - if ( nstyle->whiteSpaceMode() != QStyleSheetItem::WhiteSpaceNormal ) + if ( int(nstyle->whiteSpaceMode()) != QStyleSheetItem::Undefined ) curtag.wsm = nstyle->whiteSpaceMode(); - curtag.liststyle = chooseListStyle( nstyle, attr, curtag.liststyle ); - curtag.format = curtag.format.makeTextFormat( nstyle, attr ); + + /* ignore whitespace for inline elements if there + was already one*/ + if ( !textEditMode && curtag.wsm == QStyleSheetItem::WhiteSpaceNormal + && ( space || nstyle->displayMode() != QStyleSheetItem::DisplayInline ) ) + eatSpace( doc, length, pos ); + + curtag.format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); if ( nstyle->isAnchor() ) { if ( !anchorName.isEmpty() ) anchorName += "#" + attr["name"]; else @@ -1818,70 +1680,103 @@ void QTextDocument::setRichTextInternal( const QString &text ) if ( nstyle->alignment() != QStyleSheetItem::Undefined ) curtag.alignment = nstyle->alignment(); - if ( ostyle->displayMode() == QStyleSheetItem::DisplayListItem && - curpar->length() <= 1 - && nstyle->displayMode() == QStyleSheetItem::DisplayBlock ) { - // do not do anything, we reuse the paragraph we have - } else if ( nstyle->displayMode() != QStyleSheetItem::DisplayInline && nstyle->displayMode() != QStyleSheetItem::DisplayNone ) { + if ( (int) nstyle->listStyle() != QStyleSheetItem::Undefined ) + curtag.liststyle = nstyle->listStyle(); + + if ( nstyle->displayMode() == QStyleSheetItem::DisplayBlock + || nstyle->displayMode() == QStyleSheetItem::DisplayListItem ) { + + if ( nstyle->name() == "ol" || nstyle->name() == "ul" || nstyle->name() == "li") { + QString type = attr["type"]; + if ( !type.isEmpty() ) { + if ( type == "1" ) { + curtag.liststyle = QStyleSheetItem::ListDecimal; + } else if ( type == "a" ) { + curtag.liststyle = QStyleSheetItem::ListLowerAlpha; + } else if ( type == "A" ) { + curtag.liststyle = QStyleSheetItem::ListUpperAlpha; + } else { + type = type.lower(); + if ( type == "square" ) + curtag.liststyle = QStyleSheetItem::ListSquare; + else if ( type == "disc" ) + curtag.liststyle = QStyleSheetItem::ListDisc; + else if ( type == "circle" ) + curtag.liststyle = QStyleSheetItem::ListCircle; + } + } + } + + + /* Internally we treat ordered and bullet + lists the same for margin calculations. In + order to have fast pointer compares in the + xMargin() functions we restrict ourselves to + <ol>. Once we calculate the margins in the + parser rathern than later, the unelegance of + this approach goes awy + */ + if ( nstyle->name() == "ul" ) + curtag.style = sheet_->item( "ol" ); + + if ( attr.contains( "align" ) ) { + QString align = attr["align"]; + if ( align == "center" ) + curtag.alignment = Qt::AlignCenter; + else if ( align == "right" ) + curtag.alignment = Qt::AlignRight; + else if ( align == "justify" ) + curtag.alignment = Qt3::AlignJustify; + } + if ( attr.contains( "dir" ) ) { + QString dir = attr["dir"]; + if ( dir == "rtl" ) + curtag.direction = QChar::DirR; + else if ( dir == "ltr" ) + curtag.direction = QChar::DirL; + } + NEWPAR; - } - if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { - curpar->setListStyle( curtag.liststyle ); - if ( attr.find( "value" ) != attr.end() ) - curpar->setListValue( (*attr.find( "value" )).toInt() ); - } + if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { + if ( attr.contains( "value " ) ) + curpar->setListValue( attr["value"].toInt() ); + } - if ( nstyle->displayMode() != QStyleSheetItem::DisplayInline ) - curpar->setFormat( &curtag.format ); - - if ( attr.contains( "align" ) && - ( curtag.name == "p" || - curtag.name == "div" || - curtag.name == "li" || - curtag.name[ 0 ] == 'h' ) ) { - QString align = attr["align"]; - if ( align == "center" ) - curtag.alignment = Qt::AlignCenter; - else if ( align == "right" ) - curtag.alignment = Qt::AlignRight; - else if ( align == "justify" ) - curtag.alignment = Qt3::AlignJustify; - } - if ( attr.contains( "dir" ) && - ( curtag.name == "p" || - curtag.name == "div" || - curtag.name == "li" || - curtag.name[ 0 ] == 'h' ) ) { - QString dir = attr["dir"]; - if ( dir == "rtl" ) - curtag.direction = QChar::DirR; - else if ( dir == "ltr" ) - curtag.direction = QChar::DirL; - } - if ( nstyle->displayMode() != QStyleSheetItem::DisplayInline ) { - curpar->setAlignment( curtag.alignment ); - curpar->setDirection( (QChar::Direction)curtag.direction ); + if ( attr.contains( "style" ) ) { + QString a = attr["style"]; + bool ok = TRUE; + for ( int s = 0; ok && s < a.contains(';')+1; s++ ) { + QString style = QTextDocument::section( a, ";", s, s ); + if ( style.startsWith("margin-top:" ) && QTextDocument::endsWith(style, "px") ) + curpar->utm = 1+style.mid(11, style.length() - 13).toInt(&ok); + else if ( style.startsWith("margin-bottom:" ) && QTextDocument::endsWith(style, "px") ) + curpar->ubm = 1+style.mid(14, style.length() - 16).toInt(&ok); + else if ( style.startsWith("margin-left:" ) && QTextDocument::endsWith(style, "px") ) + curpar->ulm = 1+style.mid(12, style.length() - 14).toInt(&ok); + else if ( style.startsWith("margin-right:" ) && QTextDocument::endsWith(style, "px") ) + curpar->urm = 1+style.mid(13, style.length() - 15).toInt(&ok); + else if ( style.startsWith("text-indent:" ) && QTextDocument::endsWith(style, "px") ) + curpar->uflm = 1+style.mid(12, style.length() - 14).toInt(&ok); + } + if ( !ok ) // be pressmistic + curpar->utm = curpar->ubm = curpar->urm = curpar->ulm = 0; + } } } } else { QString tagname = parseCloseTag( doc, length, pos ); - lastClose = tagname; if ( tagname.isEmpty() ) continue; // nothing we could do with this, probably parse error if ( !sheet_->item( tagname ) ) // ignore unknown tags continue; - // we close a block item. Since the text may continue, we need to have a new paragraph - bool needNewPar = curtag.style->displayMode() == QStyleSheetItem::DisplayBlock; + bool needNewPar = curtag.style->displayMode() == QStyleSheetItem::DisplayBlock + || curtag.style->displayMode() == QStyleSheetItem::DisplayListItem; - if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { - needNewPar = TRUE; - hasNewPar = FALSE; // we want empty paragraphs in this case - } // html slopiness: handle unbalanched tag closing while ( curtag.name != tagname ) { QString msg; @@ -1900,32 +1795,54 @@ void QTextDocument::setRichTextInternal( const QString &text ) else curtag = initag; if ( needNewPar ) { - if ( curtag.style->displayMode() == QStyleSheetItem::DisplayListItem ) { - tags.push( curtag ); - curtag.name = "p"; - curtag.style = sheet_->item( curtag.name ); // a list item continues, use p for that - } + if ( textEditMode && tagname == "p" ) // preserve empty paragraphs + hasNewPar = FALSE; NEWPAR; } } } else { // normal contents QString s; QChar c; while ( pos < length && !hasPrefix(doc, length, pos, QChar('<') ) ){ - QStyleSheetItem::WhiteSpaceMode wsm = curtag.wsm; - if ( s.length() > 4096 ) - wsm = (QStyleSheetItem::WhiteSpaceMode)QStyleSheetItem_WhiteSpaceNormalWithNewlines; - - c = parseChar( doc, length, pos, wsm ); + if ( textEditMode ) { + // text edit mode: we handle all white space but ignore newlines + c = parseChar( doc, length, pos, QStyleSheetItem::WhiteSpacePre ); + if ( c == QChar_linesep ) + break; + } else { + int l = pos; + c = parseChar( doc, length, pos, curtag.wsm ); + + // in white space pre mode: treat any space as non breakable + if ( c == ' ' && curtag.wsm == QStyleSheetItem::WhiteSpacePre ) + c = QChar::nbsp; + + if ( c == ' ' || c == QChar_linesep ) { + /* avoid overlong paragraphs by forcing a new + paragraph after 4096 characters. This case can + occur when loading undiscovered plain text + documents in rich text mode. Instead of hanging + forever, we do the trick. + */ + if ( curtag.wsm == QStyleSheetItem::WhiteSpaceNormal && s.length() > 4096 ) do { + if ( doc[l] == '\n' ) { + hasNewPar = FALSE; // for a new paragraph ... + NEWPAR; + hasNewPar = FALSE; // ... and make it non-reusable + c = '\n'; // make sure we break below + break; + } + } while ( ++l < pos ); + } + } - if ( c == '\n' ) // happens only in whitespacepre-mode or with WhiteSpaceNormalWithNewlines. - break; // we want a new line in this case + if ( c == '\n' ) + break; // break on newlines, pre delievers a QChar_linesep - bool c_isSpace = c.isSpace() && c.unicode() != 0x00a0U && - curtag.wsm != QStyleSheetItem_WhiteSpaceNoCompression; + bool c_isSpace = c.isSpace() && c.unicode() != 0x00a0U && !textEditMode; if ( curtag.wsm == QStyleSheetItem::WhiteSpaceNormal && c_isSpace && space ) continue; if ( c == '\r' ) @@ -1934,11 +1851,9 @@ void QTextDocument::setRichTextInternal( const QString &text ) s += c; } if ( !s.isEmpty() && curtag.style->displayMode() != QStyleSheetItem::DisplayNone ) { hasNewPar = FALSE; - int index = curpar->length() - 1; - if ( index < 0 ) - index = 0; + int index = QMAX( curpar->length(),1) - 1; curpar->append( s ); QTextFormat* f = formatCollection()->format( &curtag.format ); curpar->setFormat( index, s.length(), f, FALSE ); // do not use collection because we have done that already f->ref += s.length() -1; // that what friends are for... @@ -1950,27 +1865,181 @@ void QTextDocument::setRichTextInternal( const QString &text ) curpar->at(index)->setAnchor( anchorName, curpar->at(index)->anchorHref() ); anchorName = QString::null; } } - if ( c == '\n' ) { // happens in WhiteSpacePre mode - hasNewPar = FALSE; - tags.push( curtag ); - NEWPAR; - curtag = tags.pop(); - } } } - - if ( hasNewPar && curpar != fParag ) { + if ( hasNewPar && curpar != fParag && !cursor ) { // cleanup unused last paragraphs curpar = curpar->p; delete curpar->n; } - if ( !anchorName.isEmpty() ) { curpar->at(curpar->length() - 1)->setAnchor( anchorName, curpar->at( curpar->length() - 1 )->anchorHref() ); anchorName = QString::null; } + + + setRichTextMarginsInternal( styles, stylesPar ); + + if ( cursor ) { + cursor->gotoPreviousLetter(); + cursor->remove(); + } + +} + +void QTextDocument::setRichTextMarginsInternal( QPtrList< QPtrVector<QStyleSheetItem> >& styles, QTextParagraph* stylesPar ) +{ + // margin and line spacing calculation + QPtrVector<QStyleSheetItem>* prevStyle = 0; + QPtrVector<QStyleSheetItem>* curStyle = styles.first(); + QPtrVector<QStyleSheetItem>* nextStyle = styles.next(); + while ( stylesPar ) { + if ( !curStyle ) { + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + continue; + } + + int i, mar; + QStyleSheetItem* mainStyle = curStyle->size() ? (*curStyle)[curStyle->size()-1] : 0; + if ( mainStyle && mainStyle->displayMode() == QStyleSheetItem::DisplayListItem ) + stylesPar->setListItem( TRUE ); + int numLists = 0; + for ( i = 0; i < (int)curStyle->size(); ++i ) { + if ( (*curStyle)[ i ]->displayMode() == QStyleSheetItem::DisplayBlock + && int((*curStyle)[ i ]->listStyle()) != QStyleSheetItem::Undefined ) + numLists++; + } + stylesPar->ldepth = numLists; + if ( stylesPar->next() && nextStyle ) { + // also set the depth of the next paragraph, required for the margin calculation + numLists = 0; + for ( i = 0; i < (int)nextStyle->size(); ++i ) { + if ( (*nextStyle)[ i ]->displayMode() == QStyleSheetItem::DisplayBlock + && int((*nextStyle)[ i ]->listStyle()) != QStyleSheetItem::Undefined ) + numLists++; + } + stylesPar->next()->ldepth = numLists; + } + + // do the top margin + QStyleSheetItem* item = mainStyle; + int m; + if (stylesPar->utm > 0 ) { + m = stylesPar->utm-1; + stylesPar->utm = 0; + } else { + m = QMAX(0, item->margin( QStyleSheetItem::MarginTop ) ); + if ( item->displayMode() == QStyleSheetItem::DisplayListItem + && stylesPar->ldepth ) + m /= stylesPar->ldepth; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( prevStyle && i < (int) prevStyle->size() && + ( item->displayMode() == QStyleSheetItem::DisplayBlock && + (*prevStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( int(item->listStyle()) != QStyleSheetItem::Undefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = QMAX( 0, item->margin( QStyleSheetItem::MarginTop ) ); + m = QMAX( m, mar ); + } + stylesPar->utm = m - stylesPar->topMargin(); + + // do the bottom margin + item = mainStyle; + if (stylesPar->ubm > 0 ) { + m = stylesPar->ubm-1; + stylesPar->ubm = 0; + } else { + m = QMAX(0, item->margin( QStyleSheetItem::MarginBottom ) ); + if ( item->displayMode() == QStyleSheetItem::DisplayListItem + && stylesPar->ldepth ) + m /= stylesPar->ldepth; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( nextStyle && i < (int) nextStyle->size() && + ( item->displayMode() == QStyleSheetItem::DisplayBlock && + (*nextStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( int(item->listStyle()) != QStyleSheetItem::Undefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = QMAX(0, item->margin( QStyleSheetItem::MarginBottom ) ); + m = QMAX( m, mar ); + } + stylesPar->ubm = m - stylesPar->bottomMargin(); + + // do the left margin, simplyfied + item = mainStyle; + if (stylesPar->ulm > 0 ) { + m = stylesPar->ulm-1; + stylesPar->ulm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginLeft ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += QMAX( 0, item->margin( QStyleSheetItem::MarginLeft ) ); + } + stylesPar->ulm = m - stylesPar->leftMargin(); + + // do the right margin, simplyfied + item = mainStyle; + if (stylesPar->urm > 0 ) { + m = stylesPar->urm-1; + stylesPar->urm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginRight ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += QMAX( 0, item->margin( QStyleSheetItem::MarginRight ) ); + } + stylesPar->urm = m - stylesPar->rightMargin(); + + // do the first line margin, which really should be called text-indent + item = mainStyle; + if (stylesPar->uflm > 0 ) { + m = stylesPar->uflm-1; + stylesPar->uflm = 0; + } else { + m = QMAX( 0, item->margin( QStyleSheetItem::MarginFirstLine ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + mar = QMAX( 0, item->margin( QStyleSheetItem::MarginFirstLine ) ); + m = QMAX( m, mar ); + } + stylesPar->uflm =m - stylesPar->firstLineMargin(); + + // do the bogus line "spacing", which really is just an extra margin + item = mainStyle; + for ( i = (int)curStyle->size() - 1 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( item->lineSpacing() != QStyleSheetItem::Undefined ) { + stylesPar->ulinespacing = item->lineSpacing(); + if ( formatCollection() && + stylesPar->ulinespacing < formatCollection()->defaultFormat()->height() ) + stylesPar->ulinespacing += formatCollection()->defaultFormat()->height(); + break; + } + } + + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + } } void QTextDocument::setText( const QString &text, const QString &context ) { @@ -1982,161 +2051,220 @@ void QTextDocument::setText( const QString &text, const QString &context ) else setPlainText( text ); } -QString QTextDocument::plainText( QTextParag *p ) const +QString QTextDocument::plainText() const { - if ( !p ) { - QString buffer; - QString s; - QTextParag *p = fParag; - while ( p ) { - if ( !p->mightHaveCustomItems ) { - s = p->string()->toString(); - } else { - for ( int i = 0; i < p->length() - 1; ++i ) { - if ( p->at( i )->isCustom() ) { - if ( p->at( i )->customItem()->isNested() ) { - s += "\n"; - QTextTable *t = (QTextTable*)p->at( i )->customItem(); - QPtrList<QTextTableCell> cells = t->tableCells(); - for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) - s += c->richText()->plainText() + "\n"; - s += "\n"; - } - } else { - s += p->at( i )->c; + QString buffer; + QString s; + QTextParagraph *p = fParag; + while ( p ) { + if ( !p->mightHaveCustomItems ) { + s = p->string()->toString(); + } else { + for ( int i = 0; i < p->length() - 1; ++i ) { + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; } + } else { + s += p->at( i )->c; } } - s.remove( s.length() - 1, 1 ); - if ( p->next() ) - s += "\n"; - buffer += s; - p = p->next(); } - return buffer; - } else { - return p->string()->toString(); + s.remove( s.length() - 1, 1 ); + if ( p->next() ) + s += "\n"; + buffer += s; + p = p->next(); } + return buffer; } -static QString align_to_string( const QString &tag, int a ) +static QString align_to_string( int a ) { - if ( tag == "p" || tag == "li" || ( tag[0] == 'h' && tag[1].isDigit() ) ) { - if ( a & Qt::AlignRight ) - return " align=\"right\""; - if ( a & Qt::AlignCenter ) - return " align=\"center\""; - if ( a & Qt3::AlignJustify ) - return " align=\"justify\""; - } - return ""; + if ( a & Qt::AlignRight ) + return " align=\"right\""; + if ( a & Qt::AlignHCenter ) + return " align=\"center\""; + if ( a & Qt3::AlignJustify ) + return " align=\"justify\""; + return QString::null; } -static QString direction_to_string( const QString &tag, int d ) +static QString direction_to_string( int d ) { - if ( d != QChar::DirON && - ( tag == "p" || tag == "div" || tag == "li" || ( tag[0] == 'h' && tag[1].isDigit() ) ) ) + if ( d != QChar::DirON ) return ( d == QChar::DirL? " dir=\"ltr\"" : " dir=\"rtl\"" ); - return ""; + return QString::null; } -QString QTextDocument::richText( QTextParag *p ) const +static QString list_value_to_string( int v ) { - QString s,n; - if ( !p ) { - p = fParag; - QPtrVector<QStyleSheetItem> lastItems, items; - while ( p ) { - items = p->styleSheetItems(); - if ( items.size() ) { - QStyleSheetItem *item = items[ items.size() - 1 ]; - items.resize( items.size() - 1 ); - if ( items.size() > lastItems.size() ) { - for ( int i = lastItems.size(); i < (int)items.size(); ++i ) { - n = items[i]->name(); - if ( n.isEmpty() || n == "li" ) - continue; - s += "<" + n + align_to_string( n, p->alignment() ) + ">"; - } - } else { - QString end; - for ( int i = items.size(); i < (int)lastItems.size(); ++i ) { - n = lastItems[i]->name(); - if ( n.isEmpty() || n == "li" || n == "br" ) - continue; - end.prepend( "</" + lastItems[ i ]->name() + ">" ); - } - s += end; - } - lastItems = items; - n = item->name(); - if ( n == "li" && p->listValue() != -1 ) { - s += "<li value=\"" + QString::number( p->listValue() ) + "\">"; - } else { - QString ps = p->richText(); - if ( ps.isEmpty() ) - s += "<br>"; // empty paragraph - else if ( !n.isEmpty() ) { - s += "<" + n + align_to_string( n, p->alignment() ) - + direction_to_string( n, p->direction() ) + ">" + ps; - if ( n != "li" && n != "br") - s += "</" + n + ">"; - } else - s += ps; - } - } else { - QString end; - for ( int i = 0; i < (int)lastItems.size(); ++i ) { - QString n = lastItems[i]->name(); - if ( n.isEmpty() || n == "li" || n == "br" ) - continue; - end.prepend( "</" + n + ">" ); - } - s += end; - QString ps = p->richText(); - if ( ps.isEmpty() ) - s += "<br>"; // empty paragraph - else - s += "<p" + align_to_string( "p", p->alignment() ) + direction_to_string( "p", p->direction() ) - + ">" + ps + "</p>"; - lastItems = items; + if ( v != -1 ) + return " listvalue=\"" + QString::number( v ) + "\""; + return QString::null; +} + +static QString list_style_to_string( int v ) +{ + switch( v ) { + case QStyleSheetItem::ListDecimal: return "\"1\""; + case QStyleSheetItem::ListLowerAlpha: return "\"a\""; + case QStyleSheetItem::ListUpperAlpha: return "\"A\""; + case QStyleSheetItem::ListDisc: return "\"disc\""; + case QStyleSheetItem::ListSquare: return "\"square\""; + case QStyleSheetItem::ListCircle: return "\"circle\""; + default: + return QString::null; + } +} + +static inline bool list_is_ordered( int v ) +{ + return v == QStyleSheetItem::ListDecimal || + v == QStyleSheetItem::ListLowerAlpha || + v == QStyleSheetItem::ListUpperAlpha; +} + + +static QString margin_to_string( QStyleSheetItem* style, int t, int b, int l, int r, int fl ) +{ + QString s; + if ( l > 0 ) + s += QString(!!s?";":"") + "margin-left:" + QString::number(l+QMAX(0,style->margin(QStyleSheetItem::MarginLeft))) + "px"; + if ( r > 0 ) + s += QString(!!s?";":"") + "margin-right:" + QString::number(r+QMAX(0,style->margin(QStyleSheetItem::MarginRight))) + "px"; + if ( t > 0 ) + s += QString(!!s?";":"") + "margin-top:" + QString::number(t+QMAX(0,style->margin(QStyleSheetItem::MarginTop))) + "px"; + if ( b > 0 ) + s += QString(!!s?";":"") + "margin-bottom:" + QString::number(b+QMAX(0,style->margin(QStyleSheetItem::MarginBottom))) + "px"; + if ( fl > 0 ) + s += QString(!!s?";":"") + "text-indent:" + QString::number(fl+QMAX(0,style->margin(QStyleSheetItem::MarginFirstLine))) + "px"; + if ( !!s ) + return " style=\"" + s + "\""; + return QString::null; +} + +QString QTextDocument::richText() const +{ + QString s = ""; + if ( !par ) { + s += "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:" ; + s += QString::number( formatCollection()->defaultFormat()->font().pointSize() ); + s += "pt;font-family:"; + s += formatCollection()->defaultFormat()->font().family(); + s +="\">"; + } + QTextParagraph* p = fParag; + + QStyleSheetItem* item_p = styleSheet()->item("p"); + QStyleSheetItem* item_ul = styleSheet()->item("ul"); + QStyleSheetItem* item_ol = styleSheet()->item("ol"); + QStyleSheetItem* item_li = styleSheet()->item("li"); + if ( !item_p || !item_ul || !item_ol || !item_li ) { + qWarning( "QTextEdit: cannot export HTML due to insufficient stylesheet (lack of p, ul, ol, or li)" ); + return QString::null; + } + int pastListDepth = 0; + int listDepth = 0; + int futureListDepth = 0; + QMemArray<int> listStyles(10); + + while ( p ) { + listDepth = p->listDepth(); + if ( listDepth < pastListDepth ) { + for ( int i = listDepth+1; i <= pastListDepth; i++ ) + s += list_is_ordered( listStyles[i] ) ? "</ol>" : "</ul>"; + s += '\n'; + } else if ( listDepth > pastListDepth ) { + s += '\n'; + listStyles.resize( QMAX( (int)listStyles.size(), listDepth+1 ) ); + QString list_type; + listStyles[listDepth] = p->listStyle(); + if ( !list_is_ordered( p->listStyle() ) || item_ol->listStyle() != p->listStyle() ) + list_type = " type=" + list_style_to_string( p->listStyle() ); + for ( int i = pastListDepth; i < listDepth; i++ ) { + s += list_is_ordered( p->listStyle() ) ? "<ol" : "<ul" ; + s += list_type + ">"; } - if ( ( p = p->next() ) ) - s += '\n'; - } - } else { - s = p->richText(); + } else { + s += '\n'; + } + + QString ps = p->richText(); + + // for the bottom margin we need to know whether we are at the end of a list + futureListDepth = 0; + if ( listDepth > 0 && p->next() ) + futureListDepth = p->next()->listDepth(); + + if ( richTextExportStart && richTextExportStart->paragraph() ==p && + richTextExportStart->index() == 0 ) + s += "<selstart/>"; + + if ( p->isListItem() ) { + s += "<li"; + if ( p->listStyle() != listStyles[listDepth] ) + s += " type=" + list_style_to_string( p->listStyle() ); + s +=align_to_string( p->alignment() ); + s += margin_to_string( item_li, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s += list_value_to_string( p->listValue() ); + s += direction_to_string( p->direction() ); + s +=">"; + s += ps; + s += "</li>"; + } else { + // normal paragraph item + s += "<p"; + s += align_to_string( p->alignment() ); + s += margin_to_string( item_p, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s +=direction_to_string( p->direction() ); + s += ">"; + s += ps; + s += "</p>"; + } + pastListDepth = listDepth; + p = p->next(); + } + while ( listDepth > 0 ) { + s += list_is_ordered( listStyles[listDepth] ) ? "</ol>" : "</ul>"; + listDepth--; } + if ( !par ) + s += "\n</body></html>\n"; + return s; } QString QTextDocument::text() const { - if ( plainText().simplifyWhiteSpace().isEmpty() ) - return QString(""); if ( txtFormat == Qt::AutoText && preferRichText || txtFormat == Qt::RichText ) return richText(); - return plainText( 0 ); + return plainText(); } QString QTextDocument::text( int parag ) const { - QTextParag *p = paragAt( parag ); + QTextParagraph *p = paragAt( parag ); if ( !p ) return QString::null; if ( txtFormat == Qt::AutoText && preferRichText || txtFormat == Qt::RichText ) - return richText( p ); + return p->richText(); else - return plainText( p ); + return p->string()->toString(); } void QTextDocument::invalidate() { - QTextParag *s = fParag; + QTextParagraph *s = fParag; while ( s ) { s->invalidate( 0 ); s = s->next(); } @@ -2147,9 +2275,9 @@ void QTextDocument::selectionStart( int id, int ¶gId, int &index ) QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); if ( it == selections.end() ) return; QTextDocumentSelection &sel = *it; - paragId = !sel.swapped ? sel.startCursor.parag()->paragId() : sel.endCursor.parag()->paragId(); + paragId = !sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); index = !sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); } QTextCursor QTextDocument::selectionStartCursor( int id) @@ -2179,34 +2307,12 @@ void QTextDocument::selectionEnd( int id, int ¶gId, int &index ) QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); if ( it == selections.end() ) return; QTextDocumentSelection &sel = *it; - paragId = sel.swapped ? sel.startCursor.parag()->paragId() : sel.endCursor.parag()->paragId(); + paragId = sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); index = sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); } -QTextParag *QTextDocument::selectionStart( int id ) -{ - QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); - if ( it == selections.end() ) - return 0; - QTextDocumentSelection &sel = *it; - if ( sel.startCursor.parag()->paragId() < sel.endCursor.parag()->paragId() ) - return sel.startCursor.parag(); - return sel.endCursor.parag(); -} - -QTextParag *QTextDocument::selectionEnd( int id ) -{ - QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); - if ( it == selections.end() ) - return 0; - QTextDocumentSelection &sel = *it; - if ( sel.startCursor.parag()->paragId() > sel.endCursor.parag()->paragId() ) - return sel.startCursor.parag(); - return sel.endCursor.parag(); -} - void QTextDocument::addSelection( int id ) { nSelections = QMAX( nSelections, id + 1 ); } @@ -2219,40 +2325,40 @@ static void setSelectionEndHelper( int id, QTextDocumentSelection &sel, QTextCur c1 = end; c2 = start; } - c1.parag()->removeSelection( id ); - c2.parag()->removeSelection( id ); - if ( c1.parag() != c2.parag() ) { - c1.parag()->setSelection( id, c1.index(), c1.parag()->length() - 1 ); - c2.parag()->setSelection( id, 0, c2.index() ); + c1.paragraph()->removeSelection( id ); + c2.paragraph()->removeSelection( id ); + if ( c1.paragraph() != c2.paragraph() ) { + c1.paragraph()->setSelection( id, c1.index(), c1.paragraph()->length() - 1 ); + c2.paragraph()->setSelection( id, 0, c2.index() ); } else { - c1.parag()->setSelection( id, QMIN( c1.index(), c2.index() ), QMAX( c1.index(), c2.index() ) ); + c1.paragraph()->setSelection( id, QMIN( c1.index(), c2.index() ), QMAX( c1.index(), c2.index() ) ); } sel.startCursor = start; sel.endCursor = end; - if ( sel.startCursor.parag() == sel.endCursor.parag() ) + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) sel.swapped = sel.startCursor.index() > sel.endCursor.index(); } -bool QTextDocument::setSelectionEnd( int id, QTextCursor *cursor ) +bool QTextDocument::setSelectionEnd( int id, const QTextCursor &cursor ) { QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); if ( it == selections.end() ) return FALSE; QTextDocumentSelection &sel = *it; QTextCursor start = sel.startCursor; - QTextCursor end = *cursor; + QTextCursor end = cursor; if ( start == end ) { removeSelection( id ); setSelectionStart( id, cursor ); return TRUE; } - if ( sel.endCursor.parag() == end.parag() ) { + if ( sel.endCursor.paragraph() == end.paragraph() ) { setSelectionEndHelper( id, sel, start, end ); return TRUE; } @@ -2261,12 +2367,11 @@ bool QTextDocument::setSelectionEnd( int id, QTextCursor *cursor ) QTextCursor tmp = sel.startCursor; if ( sel.swapped ) tmp = sel.endCursor; tmp.restoreState(); - QTextCursor tmp2 = *cursor; + QTextCursor tmp2 = cursor; tmp2.restoreState(); - c.setParag( tmp.parag()->paragId() < tmp2.parag()->paragId() ? tmp.parag() : tmp2.parag() ); - QTextCursor old; + c.setParagraph( tmp.paragraph()->paragId() < tmp2.paragraph()->paragId() ? tmp.paragraph() : tmp2.paragraph() ); bool hadStart = FALSE; bool hadEnd = FALSE; bool hadStartParag = FALSE; bool hadEndParag = FALSE; @@ -2278,30 +2383,30 @@ bool QTextDocument::setSelectionEnd( int id, QTextCursor *cursor ) if ( c == start ) hadStart = TRUE; if ( c == end ) hadEnd = TRUE; - if ( c.parag() == start.parag() ) + if ( c.paragraph() == start.paragraph() ) hadStartParag = TRUE; - if ( c.parag() == end.parag() ) + if ( c.paragraph() == end.paragraph() ) hadEndParag = TRUE; if ( c == sel.startCursor ) hadOldStart = TRUE; if ( c == sel.endCursor ) hadOldEnd = TRUE; if ( !sel.swapped && ( hadEnd && !hadStart || - hadEnd && hadStart && start.parag() == end.parag() && start.index() > end.index() ) ) + hadEnd && hadStart && start.paragraph() == end.paragraph() && start.index() > end.index() ) ) sel.swapped = TRUE; if ( c == end && hadStartParag || c == start && hadEndParag ) { QTextCursor tmp = c; tmp.restoreState(); - if ( tmp.parag() != c.parag() ) { - int sstart = tmp.parag()->selectionStart( id ); - tmp.parag()->removeSelection( id ); - tmp.parag()->setSelection( id, sstart, tmp.index() ); + if ( tmp.paragraph() != c.paragraph() ) { + int sstart = tmp.paragraph()->selectionStart( id ); + tmp.paragraph()->removeSelection( id ); + tmp.paragraph()->setSelection( id, sstart, tmp.index() ); } } if ( inSelection && @@ -2309,41 +2414,53 @@ bool QTextDocument::setSelectionEnd( int id, QTextCursor *cursor ) leftSelection = TRUE; else if ( !leftSelection && !inSelection && ( hadStart || hadEnd ) ) inSelection = TRUE; - bool noSelectionAnymore = hadOldStart && hadOldEnd && leftSelection && !inSelection && !c.parag()->hasSelection( id ) && c.atParagEnd(); - c.parag()->removeSelection( id ); + bool noSelectionAnymore = hadOldStart && hadOldEnd && leftSelection && !inSelection && !c.paragraph()->hasSelection( id ) && c.atParagEnd(); + c.paragraph()->removeSelection( id ); if ( inSelection ) { - if ( c.parag() == start.parag() && start.parag() == end.parag() ) { - c.parag()->setSelection( id, QMIN( start.index(), end.index() ), QMAX( start.index(), end.index() ) ); - } else if ( c.parag() == start.parag() && !hadEndParag ) { - c.parag()->setSelection( id, start.index(), c.parag()->length() - 1 ); - } else if ( c.parag() == end.parag() && !hadStartParag ) { - c.parag()->setSelection( id, end.index(), c.parag()->length() - 1 ); - } else if ( c.parag() == end.parag() && hadEndParag ) { - c.parag()->setSelection( id, 0, end.index() ); - } else if ( c.parag() == start.parag() && hadStartParag ) { - c.parag()->setSelection( id, 0, start.index() ); + if ( c.paragraph() == start.paragraph() && start.paragraph() == end.paragraph() ) { + c.paragraph()->setSelection( id, QMIN( start.index(), end.index() ), QMAX( start.index(), end.index() ) ); + } else if ( c.paragraph() == start.paragraph() && !hadEndParag ) { + c.paragraph()->setSelection( id, start.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && !hadStartParag ) { + c.paragraph()->setSelection( id, end.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && hadEndParag ) { + c.paragraph()->setSelection( id, 0, end.index() ); + } else if ( c.paragraph() == start.paragraph() && hadStartParag ) { + c.paragraph()->setSelection( id, 0, start.index() ); } else { - c.parag()->setSelection( id, 0, c.parag()->length() - 1 ); + c.paragraph()->setSelection( id, 0, c.paragraph()->length() - 1 ); } } if ( leftSelection ) inSelection = FALSE; - old = c; - c.gotoNextLetter(); - if ( old == c || noSelectionAnymore ) + if ( noSelectionAnymore ) break; + // *ugle*hack optimization + QTextParagraph *p = c.paragraph(); + if ( p->mightHaveCustomItems || p == start.paragraph() || p == end.paragraph() || p == lastParagraph() ) { + c.gotoNextLetter(); + if ( p == lastParagraph() && c.atParagEnd() ) + break; + } else { + if ( p->document()->parent() ) + do { + c.gotoNextLetter(); + } while ( c.paragraph() == p ); + else + c.setParagraph( p->next() ); + } } if ( !sel.swapped ) - sel.startCursor.parag()->setSelection( id, sel.startCursor.index(), sel.startCursor.parag()->length() - 1 ); + sel.startCursor.paragraph()->setSelection( id, sel.startCursor.index(), sel.startCursor.paragraph()->length() - 1 ); sel.startCursor = start; sel.endCursor = end; - if ( sel.startCursor.parag() == sel.endCursor.parag() ) + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) sel.swapped = sel.startCursor.index() > sel.endCursor.index(); setSelectionEndHelper( id, sel, start, end ); @@ -2357,108 +2474,51 @@ void QTextDocument::selectAll( int id ) QTextDocumentSelection sel; sel.swapped = FALSE; QTextCursor c( this ); - c.setParag( fParag ); + c.setParagraph( fParag ); c.setIndex( 0 ); sel.startCursor = c; - c.setParag( lParag ); + c.setParagraph( lParag ); c.setIndex( lParag->length() - 1 ); sel.endCursor = c; - QTextParag *p = fParag; + selections.insert( id, sel ); + + QTextParagraph *p = fParag; while ( p ) { p->setSelection( id, 0, p->length() - 1 ); - for ( int i = 0; i < (int)p->length(); ++i ) { - if ( p->at( i )->isCustom() && p->at( i )->customItem()->isNested() ) { - QTextTable *t = (QTextTable*)p->at( i )->customItem(); - QPtrList<QTextTableCell> tableCells = t->tableCells(); - for ( QTextTableCell *c = tableCells.first(); c; c = tableCells.next() ) - c->richText()->selectAll( id ); - } - } p = p->next(); } - selections.insert( id, sel ); + for ( QTextDocument *d = childList.first(); d; d = childList.next() ) + d->selectAll( id ); } bool QTextDocument::removeSelection( int id ) { - QMap<int, QTextDocumentSelection>::Iterator it = selections.find( id ); - if ( it == selections.end() ) + if ( !selections.contains( id ) ) return FALSE; - QTextDocumentSelection &sel = *it; - - if ( sel.startCursor == sel.endCursor ) { - selections.remove( id ); - return TRUE; - } - - if ( !mightHaveCustomItems ) { - QTextCursor start = sel.startCursor; - QTextCursor end = sel.endCursor; - if ( sel.swapped ) { - start = sel.endCursor; - end = sel.startCursor; - } + QTextDocumentSelection &sel = selections[ id ]; - for ( QTextParag *p = start.parag(); p; p = p->next() ) { + QTextCursor start = sel.swapped ? sel.endCursor : sel.startCursor; + QTextCursor end = sel.swapped ? sel.startCursor : sel.endCursor; + QTextParagraph* p = 0; + while ( start != end ) { + if ( p != start.paragraph() ) { + p = start.paragraph(); p->removeSelection( id ); - if ( p == end.parag() ) - break; } - - selections.remove( id ); - return TRUE; - } - - QTextCursor c( this ); - QTextCursor tmp = sel.startCursor; - if ( sel.swapped ) - tmp = sel.endCursor; - tmp.restoreState(); - c.setParag( tmp.parag() ); - QTextCursor old; - bool hadStart = FALSE; - bool hadEnd = FALSE; - QTextParag *lastParag = 0; - bool leftSelection = FALSE; - bool inSelection = FALSE; - sel.swapped = FALSE; - for ( ;; ) { - if ( c.parag() == sel.startCursor.parag() ) - hadStart = TRUE; - if ( c.parag() == sel.endCursor.parag() ) - hadEnd = TRUE; - - if ( inSelection && - ( c == sel.endCursor && hadStart || c == sel.startCursor && hadEnd ) ) - leftSelection = TRUE; - else if ( !leftSelection && !inSelection && ( c.parag() == sel.startCursor.parag() || c.parag() == sel.endCursor.parag() ) ) - inSelection = TRUE; - - bool noSelectionAnymore = leftSelection && !inSelection && !c.parag()->hasSelection( id ) && c.atParagEnd(); - - if ( lastParag != c.parag() ) - c.parag()->removeSelection( id ); - - old = c; - lastParag = c.parag(); - c.gotoNextLetter(); - if ( old == c || noSelectionAnymore ) - break; + start.gotoNextLetter(); } - selections.remove( id ); return TRUE; } -QString QTextDocument::selectedText( int id, bool withCustom ) const +QString QTextDocument::selectedText( int id, bool asRichText ) const { - // ######## TODO: look at textFormat() and return rich text or plain text (like the text() method!) QMap<int, QTextDocumentSelection>::ConstIterator it = selections.find( id ); if ( it == selections.end() ) return QString::null; @@ -2480,37 +2540,49 @@ QString QTextDocument::selectedText( int id, bool withCustom ) const Meaning if you select text in two table cells, you will get the entire table. This is still far better than the 3.0.2, where you always got the entire table. - ### Fix this properly for 3.0.4. + ### Fix this properly when refactoring */ while ( c2.nestedDepth() > c1.nestedDepth() ) c2.oneUp(); while ( c1.nestedDepth() > c2.nestedDepth() ) c1.oneUp(); while ( c1.nestedDepth() && c2.nestedDepth() && - c1.parag()->document() != c2.parag()->document() ) { + c1.paragraph()->document() != c2.paragraph()->document() ) { c1.oneUp(); c2.oneUp(); } - // do not trust sel_swapped with tables. Fix this properly for 3.0.4 as well - if ( c1.parag()->paragId() > c2.parag()->paragId() || - (c1.parag() == c2.parag() && c1.index() > c2.index() ) ) { + // do not trust sel_swapped with tables. Fix this properly when refactoring as well + if ( c1.paragraph()->paragId() > c2.paragraph()->paragId() || + (c1.paragraph() == c2.paragraph() && c1.index() > c2.index() ) ) { QTextCursor tmp = c1; c2 = c1; c1 = tmp; } // end selection 3.0.3 improvement + if ( asRichText && !parent() ) { + richTextExportStart = &c1; + richTextExportEnd = &c2; - if ( c1.parag() == c2.parag() ) { - QString s; - QTextParag *p = c1.parag(); + QString sel = richText(); + int from = sel.find( "<selstart/>" ); + int to = sel.findRev( "<selend/>" ); + if ( from >= 0 && from <= to ) + sel = sel.mid( from, to - from ); + richTextExportStart = richTextExportEnd = 0; + return sel; + } + + QString s; + if ( c1.paragraph() == c2.paragraph() ) { + QTextParagraph *p = c1.paragraph(); int end = c2.index(); if ( p->at( QMAX( 0, end - 1 ) )->isCustom() ) ++end; - if ( !withCustom || !p->mightHaveCustomItems ) { + if ( !p->mightHaveCustomItems ) { s += p->string()->toString().mid( c1.index(), end - c1.index() ); } else { for ( int i = c1.index(); i < end; ++i ) { if ( p->at( i )->isCustom() ) { @@ -2526,43 +2598,49 @@ QString QTextDocument::selectedText( int id, bool withCustom ) const s += p->at( i )->c; } } } - return s; - } - - QString s; - QTextParag *p = c1.parag(); - int start = c1.index(); - while ( p ) { - int end = p == c2.parag() ? c2.index() : p->length() - 1; - if ( p == c2.parag() && p->at( QMAX( 0, end - 1 ) )->isCustom() ) - ++end; - if ( !withCustom || !p->mightHaveCustomItems ) { - s += p->string()->toString().mid( start, end - start ); - if ( p != c2.parag() ) - s += "\n"; - } else { - for ( int i = start; i < end; ++i ) { - if ( p->at( i )->isCustom() ) { - if ( p->at( i )->customItem()->isNested() ) { - s += "\n"; - QTextTable *t = (QTextTable*)p->at( i )->customItem(); - QPtrList<QTextTableCell> cells = t->tableCells(); - for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) - s += c->richText()->plainText() + "\n"; - s += "\n"; + } else { + QTextParagraph *p = c1.paragraph(); + int start = c1.index(); + while ( p ) { + int end = p == c2.paragraph() ? c2.index() : p->length() - 1; + if ( p == c2.paragraph() && p->at( QMAX( 0, end - 1 ) )->isCustom() ) + ++end; + if ( !p->mightHaveCustomItems ) { + s += p->string()->toString().mid( start, end - start ); + if ( p != c2.paragraph() ) + s += "\n"; + } else { + for ( int i = start; i < end; ++i ) { + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + QTextTable *t = (QTextTable*)p->at( i )->customItem(); + QPtrList<QTextTableCell> cells = t->tableCells(); + for ( QTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else { + s += p->at( i )->c; } - } else { - s += p->at( i )->c; } } + start = 0; + if ( p == c2.paragraph() ) + break; + p = p->next(); } - start = 0; - if ( p == c2.parag() ) - break; - p = p->next(); } + // ### workaround for plain text export until we get proper + // mime types: turn unicode line seperators into the more + // widely understood \n. Makes copy and pasting code snipplets + // from within Assistent possible + QChar* uc = (QChar*) s.unicode(); + for ( uint ii = 0; ii < s.length(); ii++ ) + if ( uc[(int)ii] == QChar_linesep ) + uc[(int)ii] = QChar('\n'); return s; } void QTextDocument::setFormat( int id, QTextFormat *f, int flags ) @@ -2582,30 +2660,20 @@ void QTextDocument::setFormat( int id, QTextFormat *f, int flags ) c2.restoreState(); c1.restoreState(); - if ( c1.parag() == c2.parag() ) { - c1.parag()->setFormat( c1.index(), c2.index() - c1.index(), f, TRUE, flags ); + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->setFormat( c1.index(), c2.index() - c1.index(), f, TRUE, flags ); return; } - c1.parag()->setFormat( c1.index(), c1.parag()->length() - c1.index(), f, TRUE, flags ); - QTextParag *p = c1.parag()->next(); - while ( p && p != c2.parag() ) { + c1.paragraph()->setFormat( c1.index(), c1.paragraph()->length() - c1.index(), f, TRUE, flags ); + QTextParagraph *p = c1.paragraph()->next(); + while ( p && p != c2.paragraph() ) { p->setFormat( 0, p->length(), f, TRUE, flags ); p = p->next(); } - c2.parag()->setFormat( 0, c2.index(), f, TRUE, flags ); -} - -void QTextDocument::copySelectedText( int id ) -{ -#ifndef QT_NO_CLIPBOARD - if ( !hasSelection( id ) ) - return; - - QApplication::clipboard()->setText( selectedText( id ) ); -#endif + c2.paragraph()->setFormat( 0, c2.index(), f, TRUE, flags ); } void QTextDocument::removeSelectedText( int id, QTextCursor *cursor ) { @@ -2631,43 +2699,43 @@ void QTextDocument::removeSelectedText( int id, QTextCursor *cursor ) *cursor = c1; removeSelection( id ); - if ( c1.parag() == c2.parag() ) { - c1.parag()->remove( c1.index(), c2.index() - c1.index() ); + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->remove( c1.index(), c2.index() - c1.index() ); return; } - if ( c1.parag() == fParag && c1.index() == 0 && - c2.parag() == lParag && c2.index() == lParag->length() - 1 ) + if ( c1.paragraph() == fParag && c1.index() == 0 && + c2.paragraph() == lParag && c2.index() == lParag->length() - 1 ) cursor->setValid( FALSE ); bool didGoLeft = FALSE; - if ( c1.index() == 0 && c1.parag() != fParag ) { + if ( c1.index() == 0 && c1.paragraph() != fParag ) { cursor->gotoPreviousLetter(); if ( cursor->isValid() ) didGoLeft = TRUE; } - c1.parag()->remove( c1.index(), c1.parag()->length() - 1 - c1.index() ); - QTextParag *p = c1.parag()->next(); + c1.paragraph()->remove( c1.index(), c1.paragraph()->length() - 1 - c1.index() ); + QTextParagraph *p = c1.paragraph()->next(); int dy = 0; - QTextParag *tmp; - while ( p && p != c2.parag() ) { + QTextParagraph *tmp; + while ( p && p != c2.paragraph() ) { tmp = p->next(); dy -= p->rect().height(); delete p; p = tmp; } - c2.parag()->remove( 0, c2.index() ); + c2.paragraph()->remove( 0, c2.index() ); while ( p ) { p->move( dy ); p->invalidate( 0 ); p->setEndState( -1 ); p = p->next(); } - c1.parag()->join( c2.parag() ); + c1.paragraph()->join( c2.paragraph() ); if ( didGoLeft ) cursor->gotoNextLetter(); } @@ -2678,16 +2746,16 @@ void QTextDocument::indentSelection( int id ) if ( it == selections.end() ) return; QTextDocumentSelection sel = *it; - QTextParag *startParag = sel.startCursor.parag(); - QTextParag *endParag = sel.endCursor.parag(); - if ( sel.endCursor.parag()->paragId() < sel.startCursor.parag()->paragId() ) { - endParag = sel.startCursor.parag(); - startParag = sel.endCursor.parag(); + QTextParagraph *startParag = sel.startCursor.paragraph(); + QTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); } - QTextParag *p = startParag; + QTextParagraph *p = startParag; while ( p && p != endParag ) { p->indent(); p = p->next(); } @@ -2707,86 +2775,71 @@ QTextCursor *QTextDocument::redo( QTextCursor *c ) { return commandHistory->redo( c ); } -bool QTextDocument::find( const QString &expr, bool cs, bool wo, bool forward, - int *parag, int *index, QTextCursor *cursor ) +bool QTextDocument::find( QTextCursor& cursor, const QString &e, bool cs, bool wo, bool forward ) { - QTextParag *p = forward ? fParag : lParag; - if ( parag ) - p = paragAt( *parag ); - else if ( cursor ) - p = cursor->parag(); - bool first = TRUE; - - while ( p ) { - QString s = p->string()->toString(); - s.remove( s.length() - 1, 1 ); // get rid of trailing space - int start = forward ? 0 : s.length() - 1; - if ( first && index ) - start = *index; - else if ( first ) - start = cursor->index(); - if ( !forward && first ) { - start -= expr.length() + 1; - if ( start < 0 ) { - first = FALSE; - p = p->prev(); - continue; - } + removeSelection( Standard ); + QTextParagraph *p = 0; + QString expr = e; + // if we search for 'word only' than we have to be sure that + // the expression contains no space or punct character at the + // beginning or in the end. Otherwise we would run into a + // endlessloop. + if ( wo ) { + for ( ;; ) { + if ( expr[ 0 ].isSpace() || expr[ 0 ].isPunct() ) + expr = expr.right( expr.length() - 1 ); + else + break; } - first = FALSE; - for ( ;; ) { - int res = forward ? s.find( expr, start, cs ) : s.findRev( expr, start, cs ); - if ( res == -1 ) + if ( expr.at( expr.length() - 1 ).isSpace() || expr.at( expr.length() - 1 ).isPunct() ) + expr = expr.left( expr.length() - 1 ); + else break; - - bool ok = TRUE; - if ( wo ) { + } + } + for (;;) { + if ( p != cursor.paragraph() ) { + p = cursor.paragraph(); + QString s = cursor.paragraph()->string()->toString(); + int start = cursor.index(); + for ( ;; ) { + int res = forward ? s.find( expr, start, cs ) : s.findRev( expr, start, cs ); int end = res + expr.length(); - if ( ( res == 0 || s[ res - 1 ].isSpace() || s[ res - 1 ].isPunct() ) && - ( end == (int)s.length() || s[ end ].isSpace() || s[ end ].isPunct() ) ) - ok = TRUE; - else - ok = FALSE; - } - if ( ok ) { - cursor->setParag( p ); - cursor->setIndex( res ); - setSelectionStart( Standard, cursor ); - cursor->setIndex( res + expr.length() ); - setSelectionEnd( Standard, cursor ); - if ( parag ) - *parag = p->paragId(); - if ( index ) - *index = res; - return TRUE; - } - if ( forward ) { - start = res + 1; - } else { - if ( res == 0 ) + if ( res == -1 || ( !forward && start < end ) ) break; - start = res - 1; + if ( !wo || ( ( res == 0 || s[ res - 1 ].isSpace() || s[ res - 1 ].isPunct() ) && + ( end == (int)s.length() || s[ end ].isSpace() || s[ end ].isPunct() ) ) ) { + removeSelection( Standard ); + cursor.setIndex( forward ? end : res ); + setSelectionStart( Standard, cursor ); + cursor.setIndex( forward ? res : end ); + setSelectionEnd( Standard, cursor ); + return TRUE; + } + start = res + (forward ? 1 : -1); } } - p = forward ? p->next() : p->prev(); + if ( forward ) { + if ( cursor.paragraph() == lastParagraph() && cursor.atParagEnd () ) + break; + cursor.gotoNextLetter(); + } else { + if ( cursor.paragraph() == firstParagraph() && cursor.atParagStart() ) + break; + cursor.gotoPreviousLetter(); + } } - return FALSE; } void QTextDocument::setTextFormat( Qt::TextFormat f ) { txtFormat = f; - if ( txtFormat == Qt::RichText && fParag && fParag == lParag && fParag->length() <= 1 ) { - QPtrVector<QStyleSheetItem> v = fParag->styleSheetItems(); - v.resize( v.size() + 1 ); - v.insert( v.size() - 1, styleSheet()->item( "p" ) ); - fParag->setStyleSheetItems( v ); - } - + if ( fParag == lParag && fParag->length() <= 1 ) + fParag->rtext = ( f == Qt::RichText ); } Qt::TextFormat QTextDocument::textFormat() const { @@ -2799,19 +2852,19 @@ bool QTextDocument::inSelection( int selId, const QPoint &pos ) const if ( it == selections.end() ) return FALSE; QTextDocumentSelection sel = *it; - QTextParag *startParag = sel.startCursor.parag(); - QTextParag *endParag = sel.endCursor.parag(); - if ( sel.startCursor.parag() == sel.endCursor.parag() && - sel.startCursor.parag()->selectionStart( selId ) == sel.endCursor.parag()->selectionEnd( selId ) ) + QTextParagraph *startParag = sel.startCursor.paragraph(); + QTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() && + sel.startCursor.paragraph()->selectionStart( selId ) == sel.endCursor.paragraph()->selectionEnd( selId ) ) return FALSE; - if ( sel.endCursor.parag()->paragId() < sel.startCursor.parag()->paragId() ) { - endParag = sel.startCursor.parag(); - startParag = sel.endCursor.parag(); + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); } - QTextParag *p = startParag; + QTextParagraph *p = startParag; while ( p ) { if ( p->rect().contains( pos ) ) { bool inSel = FALSE; int selStart = p->selectionStart( selId ); @@ -2854,9 +2907,9 @@ void QTextDocument::doLayout( QPainter *p, int w ) QTextFormat::setPainter( p ); flow_->setWidth( w ); cw = w; vw = w; - QTextParag *parag = fParag; + QTextParagraph *parag = fParag; while ( parag ) { parag->invalidate( 0 ); if ( p ) parag->adjustToPainter( p ); @@ -2867,49 +2920,30 @@ void QTextDocument::doLayout( QPainter *p, int w ) } QPixmap *QTextDocument::bufferPixmap( const QSize &s ) { - if ( !buf_pixmap ) { - int w = QABS( s.width() ); - int h = QABS( s.height() ); - buf_pixmap = new QPixmap( w, h ); - } else { - if ( buf_pixmap->width() < s.width() || - buf_pixmap->height() < s.height() ) { - buf_pixmap->resize( QMAX( s.width(), buf_pixmap->width() ), - QMAX( s.height(), buf_pixmap->height() ) ); - } - } - + if ( !buf_pixmap ) + buf_pixmap = new QPixmap( s.expandedTo( QSize(1,1) ) ); + else if ( buf_pixmap->size() != s ) + buf_pixmap->resize( s.expandedTo( buf_pixmap->size() ) ); return buf_pixmap; } void QTextDocument::draw( QPainter *p, const QRect &rect, const QColorGroup &cg, const QBrush *paper ) { - if ( !firstParag() ) + if ( !firstParagraph() ) return; if ( paper ) { p->setBrushOrigin( 0, 0 ); p->fillRect( rect, *paper ); } - if ( formatCollection()->defaultFormat()->color() != cg.text() ) { - QDict<QTextFormat> formats = formatCollection()->dict(); - QDictIterator<QTextFormat> it( formats ); - while ( it.current() ) { - if ( it.current() == formatCollection()->defaultFormat() ) { - ++it; - continue; - } - it.current()->setColor( cg.text() ); - ++it; - } - formatCollection()->defaultFormat()->setColor( cg.text() ); - } + if ( formatCollection()->defaultFormat()->color() != cg.text() ) + setDefaultFormat( formatCollection()->defaultFormat()->font(), cg.text() ); - QTextParag *parag = firstParag(); + QTextParagraph *parag = firstParagraph(); while ( parag ) { if ( !parag->isValid() ) parag->format(); int y = parag->rect().y(); @@ -2931,9 +2965,9 @@ void QTextDocument::draw( QPainter *p, const QRect &rect, const QColorGroup &cg, flow()->drawFloatingItems( p, rect.x(), rect.y(), rect.width(), rect.height(), cg, FALSE ); } } -void QTextDocument::drawParag( QPainter *p, QTextParag *parag, int cx, int cy, int cw, int ch, +void QTextDocument::drawParagraph( QPainter *p, QTextParagraph *parag, int cx, int cy, int cw, int ch, QPixmap *&doubleBuffer, const QColorGroup &cg, bool drawCursor, QTextCursor *cursor, bool resetChanged ) { QPainter *painter = 0; @@ -2964,25 +2998,13 @@ void QTextDocument::drawParag( QPainter *p, QTextParag *parag, int cx, int cy, i } painter->setBrushOrigin( -ir.x(), -ir.y() ); - if ( useDoubleBuffer || is_printer( painter ) ) { - if ( !parag->backgroundColor() ) - painter->fillRect( QRect( 0, 0, ir.width(), ir.height() ), - cg.brush( QColorGroup::Base ) ); - else - painter->fillRect( QRect( 0, 0, ir.width(), ir.height() ), - *parag->backgroundColor() ); - } else { - if ( cursor && cursor->parag() == parag ) { - if ( !parag->backgroundColor() ) - painter->fillRect( QRect( parag->at( cursor->index() )->x, 0, 2, ir.height() ), - cg.brush( QColorGroup::Base ) ); - else - painter->fillRect( QRect( parag->at( cursor->index() )->x, 0, 2, ir.height() ), - *parag->backgroundColor() ); - } - } + if ( useDoubleBuffer || is_printer( painter ) ) + painter->fillRect( QRect( 0, 0, ir.width(), ir.height() ), parag->backgroundBrush( cg ) ); + else if ( cursor && cursor->paragraph() == parag ) + painter->fillRect( QRect( parag->at( cursor->index() )->x, 0, 2, ir.height() ), + parag->backgroundBrush( cg ) ); painter->translate( -( ir.x() - parag->rect().x() ), -( ir.y() - parag->rect().y() ) ); parag->paint( *painter, cg, drawCursor ? cursor : 0, TRUE, cx, cy, cw, ch ); @@ -2994,19 +3016,21 @@ void QTextDocument::drawParag( QPainter *p, QTextParag *parag, int cx, int cy, i } else { painter->translate( -ir.x(), -ir.y() ); } - if ( parag->rect().x() + parag->rect().width() < parag->document()->x() + parag->document()->width() ) { - p->fillRect( parag->rect().x() + parag->rect().width(), parag->rect().y(), - ( parag->document()->x() + parag->document()->width() ) - - ( parag->rect().x() + parag->rect().width() ), - parag->rect().height(), cg.brush( QColorGroup::Base ) ); + if ( useDoubleBuffer ) { + if ( parag->rect().x() + parag->rect().width() < parag->document()->x() + parag->document()->width() ) { + p->fillRect( parag->rect().x() + parag->rect().width(), parag->rect().y(), + ( parag->document()->x() + parag->document()->width() ) - + ( parag->rect().x() + parag->rect().width() ), + parag->rect().height(), cg.brush( QColorGroup::Base ) ); + } } parag->document()->nextDoubleBuffered = FALSE; } -QTextParag *QTextDocument::draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, +QTextParagraph *QTextDocument::draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, bool onlyChanged, bool drawCursor, QTextCursor *cursor, bool resetChanged ) { if ( withoutDoubleBuffer || par && par->withoutDoubleBuffer ) { withoutDoubleBuffer = TRUE; @@ -3015,66 +3039,55 @@ QTextParag *QTextDocument::draw( QPainter *p, int cx, int cy, int cw, int ch, co return 0; } withoutDoubleBuffer = FALSE; - if ( !firstParag() ) + if ( !firstParagraph() ) return 0; - if ( drawCursor && cursor ) - tmpCursor = cursor; if ( cx < 0 && cy < 0 ) { cx = 0; cy = 0; cw = width(); ch = height(); } - QTextParag *lastFormatted = 0; - QTextParag *parag = firstParag(); + QTextParagraph *lastFormatted = 0; + QTextParagraph *parag = firstParagraph(); QPixmap *doubleBuffer = 0; QPainter painter; + bool fullWidthSelection = FALSE; while ( parag ) { lastFormatted = parag; if ( !parag->isValid() ) parag->format(); - if ( !parag->rect().intersects( QRect( cx, cy, cw, ch ) ) ) { - QRect pr( parag->rect() ); + QRect pr = parag->rect(); + if ( fullWidthSelection ) pr.setWidth( parag->document()->width() ); - if ( pr.intersects( QRect( cx, cy, cw, ch ) ) ) - p->fillRect( pr.intersect( QRect( cx, cy, cw, ch ) ), cg.brush( QColorGroup::Base ) ); - if ( parag->rect().y() > cy + ch ) { - tmpCursor = 0; - goto floating; - } - parag = parag->next(); - continue; - } - - if ( !parag->hasChanged() && onlyChanged ) { + if ( pr.y() > cy + ch ) + goto floating; + if ( !pr.intersects( QRect( cx, cy, cw, ch ) ) || ( onlyChanged && !parag->hasChanged() ) ) { parag = parag->next(); continue; } - drawParag( p, parag, cx, cy, cw, ch, doubleBuffer, cg, drawCursor, cursor, resetChanged ); + drawParagraph( p, parag, cx, cy, cw, ch, doubleBuffer, cg, drawCursor, cursor, resetChanged ); parag = parag->next(); } - parag = lastParag(); + parag = lastParagraph(); floating: if ( parag->rect().y() + parag->rect().height() < parag->document()->height() ) { - if ( !parag->document()->parent() ) { // !useDoubleBuffer + if ( !parag->document()->parent() ) { p->fillRect( 0, parag->rect().y() + parag->rect().height(), parag->document()->width(), parag->document()->height() - ( parag->rect().y() + parag->rect().height() ), cg.brush( QColorGroup::Base ) ); } if ( !flow()->isEmpty() ) { QRect cr( cx, cy, cw, ch ); -// cr = cr.intersect( QRect( 0, parag->rect().y() + parag->rect().height(), parag->document()->width(), -// parag->document()->height() - ( parag->rect().y() + parag->rect().height() ) ) ); flow()->drawFloatingItems( p, cr.x(), cr.y(), cr.width(), cr.height(), cg, FALSE ); } } @@ -3082,24 +3095,37 @@ QTextParag *QTextDocument::draw( QPainter *p, int cx, int cy, int cw, int ch, co delete buf_pixmap; buf_pixmap = 0; } - tmpCursor = 0; return lastFormatted; } -void QTextDocument::setDefaultFont( const QFont &f ) +/* + #### this function only sets the default font size in the format collection + */ +void QTextDocument::setDefaultFormat( const QFont &font, const QColor &color ) { - int s = f.pointSize(); - bool usePixels = FALSE; - if ( s == -1 ) { - s = f.pixelSize(); - usePixels = TRUE; + bool reformat = font != fCollection->defaultFormat()->font(); + for ( QTextDocument *d = childList.first(); d; d = childList.next() ) + d->setDefaultFormat( font, color ); + fCollection->updateDefaultFormat( font, color, sheet_ ); + + if ( !reformat ) + return; + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; + + // invalidate paragraphs and custom items + QTextParagraph *p = fParag; + while ( p ) { + p->invalidate( 0 ); + for ( int i = 0; i < p->length() - 1; ++i ) + if ( p->at( i )->isCustom() ) + p->at( i )->customItem()->invalidate(); + p = p->next(); } - updateFontSizes( s, usePixels ); } -void QTextDocument::registerCustomItem( QTextCustomItem *i, QTextParag *p ) +void QTextDocument::registerCustomItem( QTextCustomItem *i, QTextParagraph *p ) { if ( i && i->placement() != QTextCustomItem::PlaceInline ) { flow_->registerFloatingItem( i ); p->registerFloatingItem( i ); @@ -3107,9 +3133,9 @@ void QTextDocument::registerCustomItem( QTextCustomItem *i, QTextParag *p ) } p->mightHaveCustomItems = mightHaveCustomItems = TRUE; } -void QTextDocument::unregisterCustomItem( QTextCustomItem *i, QTextParag *p ) +void QTextDocument::unregisterCustomItem( QTextCustomItem *i, QTextParagraph *p ) { flow_->unregisterFloatingItem( i ); p->unregisterFloatingItem( i ); i->setParagraph( 0 ); @@ -3142,9 +3168,9 @@ bool QTextDocument::focusNextPrevChild( bool next ) } focusIndicator.href = QString::null; if ( next ) { - QTextParag *p = focusIndicator.parag; + QTextParagraph *p = focusIndicator.parag; int index = focusIndicator.start + focusIndicator.len; while ( p ) { for ( int i = index; i < p->length(); ++i ) { if ( p->at( i )->isAnchor() ) { @@ -3201,9 +3227,9 @@ bool QTextDocument::focusNextPrevChild( bool next ) index = 0; p = p->next(); } } else { - QTextParag *p = focusIndicator.parag; + QTextParagraph *p = focusIndicator.parag; int index = focusIndicator.start - 1; if ( focusIndicator.len == 0 && index < focusIndicator.parag->length() - 1 ) index++; while ( p ) { @@ -3281,9 +3307,9 @@ bool QTextDocument::focusNextPrevChild( bool next ) int QTextDocument::length() const { int l = 0; - QTextParag *p = fParag; + QTextParagraph *p = fParag; while ( p ) { l += p->length() - 1; // don't count trailing space p = p->next(); } @@ -3384,22 +3410,27 @@ QTextString::QTextString( const QTextString &s ) } void QTextString::insert( int index, const QString &s, QTextFormat *f ) { + insert( index, s.unicode(), s.length(), f ); +} + +void QTextString::insert( int index, const QChar *unicode, int len, QTextFormat *f ) +{ int os = data.size(); - data.resize( data.size() + s.length() ); + data.resize( data.size() + len ); if ( index < os ) { - memmove( data.data() + index + s.length(), data.data() + index, + memmove( data.data() + index + len, data.data() + index, sizeof( QTextStringChar ) * ( os - index ) ); } - for ( int i = 0; i < (int)s.length(); ++i ) { + for ( int i = 0; i < len; ++i ) { data[ (int)index + i ].x = 0; data[ (int)index + i ].lineStart = 0; data[ (int)index + i ].d.format = 0; data[ (int)index + i ].type = QTextStringChar::Regular; data[ (int)index + i ].rightToLeft = 0; data[ (int)index + i ].startOfRun = 0; - data[ (int)index + i ].c = s[ i ]; + data[ (int)index + i ].c = unicode[i]; data[ (int)index + i ].setFormat( f ); } bidiDirty = TRUE; } @@ -3408,9 +3439,9 @@ QTextString::~QTextString() { clear(); } -void QTextString::insert( int index, QTextStringChar *c ) +void QTextString::insert( int index, QTextStringChar *c, bool doAddRefFormat ) { int os = data.size(); data.resize( data.size() + 1 ); if ( index < os ) { @@ -3422,8 +3453,10 @@ void QTextString::insert( int index, QTextStringChar *c ) data[ (int)index ].lineStart = 0; data[ (int)index ].rightToLeft = 0; data[ (int)index ].d.format = 0; data[ (int)index ].type = QTextStringChar::Regular; + if ( doAddRefFormat && c->format() ) + c->format()->addRef(); data[ (int)index ].setFormat( c->format() ); bidiDirty = TRUE; } @@ -3544,48 +3577,43 @@ void QTextDocument::setStyleSheet( QStyleSheet *s ) { if ( !s ) return; sheet_ = s; - fCollection->setStyleSheet( s ); - updateStyles(); -} - -void QTextDocument::updateStyles() -{ - invalidate(); - if ( par ) - underlLinks = par->underlLinks; - fCollection->updateStyles(); - for ( QTextDocument *d = childList.first(); d; d = childList.next() ) - d->updateStyles(); -} - -void QTextDocument::updateFontSizes( int base, bool usePixels ) -{ - for ( QTextDocument *d = childList.first(); d; d = childList.next() ) - d->updateFontSizes( base, usePixels ); - invalidate(); - fCollection->updateFontSizes( base, usePixels ); + list_tm = list_bm = par_tm = par_bm = 12; + list_lm = 40; + li_tm = li_bm = 0; + QStyleSheetItem* item = s->item( "ol" ); + if ( item ) { + list_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + list_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + list_lm = QMAX(0,item->margin( QStyleSheetItem::MarginLeft )); + } + if ( (item = s->item( "li" ) ) ) { + li_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + li_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + } + if ( (item = s->item( "p" ) ) ) { + par_tm = QMAX(0,item->margin( QStyleSheetItem::MarginTop )); + par_bm = QMAX(0,item->margin( QStyleSheetItem::MarginBottom )); + } } -void QTextDocument::updateFontAttributes( const QFont &f, const QFont &old ) -{ +void QTextDocument::setUnderlineLinks( bool b ) { + underlLinks = b; for ( QTextDocument *d = childList.first(); d; d = childList.next() ) - d->updateFontAttributes( f, old ); - invalidate(); - fCollection->updateFontAttributes( f, old ); + d->setUnderlineLinks( b ); } void QTextStringChar::setFormat( QTextFormat *f ) { if ( type == Regular ) { - d.format = f; + d.format = f; } else { - if ( !d.custom ) { - d.custom = new CustomData; - d.custom->custom = 0; - } - d.custom->format = f; + if ( !d.custom ) { + d.custom = new CustomData; + d.custom->custom = 0; + } + d.custom->format = f; } } void QTextStringChar::setCustomItem( QTextCustomItem *i ) @@ -3650,9 +3678,9 @@ void QTextStringChar::setAnchor( const QString& name, const QString& href ) int QTextString::width( int idx ) const { int w = 0; QTextStringChar *c = &at( idx ); - if ( c->c.unicode() == 0xad ) + if ( c->c.unicode() == 0xad || c->c.unicode() == 0x2028 ) return 0; if( c->isCustom() ) { if( c->customItem()->placement() == QTextCustomItem::PlaceInline ) w = c->customItem()->width; @@ -3698,89 +3726,62 @@ QMemArray<QTextStringChar> QTextString::subString( int start, int len ) const } return a; } -QTextStringChar *QTextStringChar::clone() const -{ - QTextStringChar *chr = new QTextStringChar; - chr->c = c; - chr->x = 0; - chr->lineStart = 0; - chr->rightToLeft = 0; - chr->d.format = 0; - chr->type = QTextStringChar::Regular; - chr->setFormat( format() ); - if ( chr->format() ) - chr->format()->addRef(); - return chr; -} - // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -QTextParag::QTextParag( QTextDocument *d, QTextParag *pr, QTextParag *nx, bool updateIds ) - : invalid( 0 ), p( pr ), n( nx ), docOrPseudo( d ), align( 0 ),mSelections( 0 ), - mStyleSheetItemsVec( 0 ), mFloatingItems( 0 ), listS( QStyleSheetItem::ListDisc ), - numSubParag( -1 ), tm( -1 ), bm( -1 ), lm( -1 ), rm( -1 ), flm( -1 ), - tArray(0), tabStopWidth(0), eData( 0 ) -{ - listS = QStyleSheetItem::ListDisc; - if ( ! (hasdoc = docOrPseudo != 0 ) ) - docOrPseudo = new QTextParagPseudoDocument; +QTextParagraph::QTextParagraph( QTextDocument *d, QTextParagraph *pr, QTextParagraph *nx, bool updateIds ) + : invalid( 0 ), p( pr ), n( nx ), docOrPseudo( d ), + changed(FALSE), firstFormat(TRUE), firstPProcess(TRUE), needPreProcess(FALSE), fullWidth(TRUE), + lastInFrame(FALSE), visible(TRUE), breakable(TRUE), movedDown(FALSE), + mightHaveCustomItems(FALSE), hasdoc( d != 0 ), litem(FALSE), rtext(FALSE), + align( 0 ),mSelections( 0 ), + mFloatingItems( 0 ), lstyle( QStyleSheetItem::ListDisc ), + utm( 0 ), ubm( 0 ), ulm( 0 ), urm( 0 ), uflm( 0 ), ulinespacing( 0 ), + tArray(0), tabStopWidth(0), eData( 0 ), ldepth( 0 ) +{ + lstyle = QStyleSheetItem::ListDisc; + if ( !hasdoc ) + docOrPseudo = new QTextParagraphPseudoDocument; bgcol = 0; - breakable = TRUE; - isBr = FALSE; - movedDown = FALSE; - mightHaveCustomItems = FALSE; - visible = TRUE; list_val = -1; - newLinesAllowed = FALSE; - lastInFrame = FALSE; - defFormat = formatCollection()->defaultFormat(); + QTextFormat* defFormat = formatCollection()->defaultFormat(); if ( !hasdoc ) { tabStopWidth = defFormat->width( 'x' ) * 8; pseudoDocument()->commandHistory = new QTextCommandHistory( 100 ); } -#if defined(PARSER_DEBUG) - qDebug( debug_indent + "new QTextParag" ); -#endif - fullWidth = TRUE; if ( p ) p->n = this; if ( n ) n->p = this; if ( !p && hasdoc ) - document()->setFirstParag( this ); + document()->setFirstParagraph( this ); if ( !n && hasdoc ) - document()->setLastParag( this ); + document()->setLastParagraph( this ); - changed = FALSE; - firstFormat = TRUE; state = -1; - needPreProcess = FALSE; if ( p ) id = p->id + 1; else id = 0; if ( n && updateIds ) { - QTextParag *s = n; + QTextParagraph *s = n; while ( s ) { s->id = s->p->id + 1; - s->numSubParag = -1; - s->lm = s->rm = s->tm = s->bm = -1, s->flm = -1; + s->invalidateStyleCache(); s = s->n; } } - firstPProcess = TRUE; str = new QTextString(); str->insert( 0, " ", formatCollection()->defaultFormat() ); } -QTextParag::~QTextParag() +QTextParagraph::~QTextParagraph() { delete str; if ( hasdoc ) { register QTextDocument *doc = document(); @@ -3795,38 +3796,36 @@ QTextParag::~QTextParag() } if ( tArray ) delete [] tArray; delete eData; - QMap<int, QTextParagLineStart*>::Iterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::Iterator it = lineStarts.begin(); for ( ; it != lineStarts.end(); ++it ) delete *it; if ( mSelections ) delete mSelections; if ( mFloatingItems ) delete mFloatingItems; - if ( mStyleSheetItemsVec ) - delete mStyleSheetItemsVec; if ( p ) p->setNext( n ); if ( n ) n->setPrev( p ); } -void QTextParag::setNext( QTextParag *s ) +void QTextParagraph::setNext( QTextParagraph *s ) { n = s; if ( !n && hasdoc ) - document()->setLastParag( this ); + document()->setLastParagraph( this ); } -void QTextParag::setPrev( QTextParag *s ) +void QTextParagraph::setPrev( QTextParagraph *s ) { p = s; if ( !p && hasdoc ) - document()->setFirstParag( this ); + document()->setFirstParagraph( this ); } -void QTextParag::invalidate( int chr ) +void QTextParagraph::invalidate( int chr ) { if ( invalid < 0 ) invalid = chr; else @@ -3834,30 +3833,42 @@ void QTextParag::invalidate( int chr ) if ( mFloatingItems ) { for ( QTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) i->ypos = -1; } - lm = rm = bm = tm = flm = -1; + invalidateStyleCache(); } -void QTextParag::insert( int index, const QString &s ) +void QTextParagraph::invalidateStyleCache() +{ + if ( list_val < 0 ) + list_val = -1; +} + + +void QTextParagraph::insert( int index, const QString &s ) +{ + insert( index, s.unicode(), s.length() ); +} + +void QTextParagraph::insert( int index, const QChar *unicode, int len ) { if ( hasdoc && !document()->useFormatCollection() && document()->preProcessor() ) - str->insert( index, s, + str->insert( index, unicode, len, document()->preProcessor()->format( QTextPreProcessor::Standard ) ); else - str->insert( index, s, formatCollection()->defaultFormat() ); + str->insert( index, unicode, len, formatCollection()->defaultFormat() ); invalidate( index ); needPreProcess = TRUE; } -void QTextParag::truncate( int index ) +void QTextParagraph::truncate( int index ) { str->truncate( index ); insert( length(), " " ); needPreProcess = TRUE; } -void QTextParag::remove( int index, int len ) +void QTextParagraph::remove( int index, int len ) { if ( index + len - str->length() > 0 ) return; for ( int i = index; i < index + len; ++i ) { @@ -3870,16 +3881,16 @@ void QTextParag::remove( int index, int len ) invalidate( 0 ); needPreProcess = TRUE; } -void QTextParag::join( QTextParag *s ) +void QTextParagraph::join( QTextParagraph *s ) { int oh = r.height() + s->r.height(); n = s->n; if ( n ) n->p = this; else if ( hasdoc ) - document()->setLastParag( this ); + document()->setLastParagraph( this ); int start = str->length(); if ( length() > 0 && at( length() - 1 )->c == ' ' ) { remove( length() - 1, 1 ); @@ -3895,8 +3906,12 @@ void QTextParag::join( QTextParag *s ) if ( s->str->at( i ).isCustom() ) { QTextCustomItem * item = s->str->at( i ).customItem(); str->at( i + start ).setCustomItem( item ); s->str->at( i ).loseCustomItem(); + if ( hasdoc ) { + document()->unregisterCustomItem( item, s ); + document()->registerCustomItem( item, this ); + } } if ( s->str->at( i ).isAnchor() ) { str->at( i + start ).setAnchor( s->str->at( i ).anchorName(), s->str->at( i ).anchorHref() ); @@ -3913,22 +3928,24 @@ void QTextParag::join( QTextParag *s ) invalidate( 0 ); r.setHeight( oh ); needPreProcess = TRUE; if ( n ) { - QTextParag *s = n; + QTextParagraph *s = n; + s->invalidate( 0 ); while ( s ) { s->id = s->p->id + 1; s->state = -1; s->needPreProcess = TRUE; s->changed = TRUE; + s->invalidateStyleCache(); s = s->n; } } format(); state = -1; } -void QTextParag::move( int &dy ) +void QTextParagraph::move( int &dy ) { if ( dy == 0 ) return; changed = TRUE; @@ -3950,9 +3967,9 @@ void QTextParag::move( int &dy ) } } } -void QTextParag::format( int start, bool doMove ) +void QTextParagraph::format( int start, bool doMove ) { if ( !str || str->length() == 0 || !formatter() ) return; @@ -3965,54 +3982,54 @@ void QTextParag::format( int start, bool doMove ) if ( invalid == -1 ) return; r.moveTopLeft( QPoint( documentX(), p ? p->r.y() + p->r.height() : documentY() ) ); - r.setWidth( documentWidth() ); if ( p ) p->lastInFrame = FALSE; movedDown = FALSE; bool formattedAgain = FALSE; formatAgain: + r.setWidth( documentWidth() ); if ( hasdoc && mFloatingItems ) { for ( QTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) { i->ypos = r.y(); if ( i->placement() == QTextCustomItem::PlaceRight ) { i->xpos = r.x() + r.width() - i->width; } } } - QMap<int, QTextParagLineStart*> oldLineStarts = lineStarts; + QMap<int, QTextLineStart*> oldLineStarts = lineStarts; lineStarts.clear(); int y = formatter()->format( document(), this, start, oldLineStarts ); r.setWidth( QMAX( r.width(), formatter()->minimumWidth() ) ); - QMap<int, QTextParagLineStart*>::Iterator it = oldLineStarts.begin(); + QMap<int, QTextLineStart*>::Iterator it = oldLineStarts.begin(); for ( ; it != oldLineStarts.end(); ++it ) delete *it; QTextStringChar *c = 0; - // do not do this on mac, as the paragraph - // with has to be the full document width on mac as the selections - // always extend completely to the right. This is a bit unefficient, - // as this results in a bigger double buffer than needed but ok for - // now. - if ( lineStarts.count() == 1 ) { //&& ( !doc || document()->flow()->isEmpty() ) ) { + // do not do this on mac, as the paragraph + // with has to be the full document width on mac as the selections + // always extend completely to the right. This is a bit unefficient, + // as this results in a bigger double buffer than needed but ok for + // now. + if ( lineStarts.count() == 1 ) { if ( !string()->isBidi() ) { c = &str->at( str->length() - 1 ); r.setWidth( c->x + str->width( str->length() - 1 ) ); } else { r.setWidth( lineStarts[0]->w ); } } - if ( newLinesAllowed ) { + if ( !hasdoc ) { // qt_format_text bounding rect handling it = lineStarts.begin(); int usedw = 0; for ( ; it != lineStarts.end(); ++it ) usedw = QMAX( usedw, (*it)->w ); @@ -4053,9 +4070,9 @@ void QTextParag::format( int start, bool doMove ) } if ( n && doMove && n->invalid == -1 && r.y() + r.height() != n->r.y() ) { int dy = ( r.y() + r.height() ) - n->r.y(); - QTextParag *s = n; + QTextParagraph *s = n; bool makeInvalid = p && p->lastInFrame; while ( s && dy ) { if ( !s->isFullWidth() ) makeInvalid = TRUE; @@ -4073,14 +4090,14 @@ void QTextParag::format( int start, bool doMove ) invalid = -1; //##### string()->setTextChanged( FALSE ); } -int QTextParag::lineHeightOfChar( int i, int *bl, int *y ) const +int QTextParagraph::lineHeightOfChar( int i, int *bl, int *y ) const { if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.end(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.end(); --it; for ( ;; ) { if ( i >= it.key() ) { if ( bl ) @@ -4093,19 +4110,19 @@ int QTextParag::lineHeightOfChar( int i, int *bl, int *y ) const break; --it; } - qWarning( "QTextParag::lineHeightOfChar: couldn't find lh for %d", i ); + qWarning( "QTextParagraph::lineHeightOfChar: couldn't find lh for %d", i ); return 15; } -QTextStringChar *QTextParag::lineStartOfChar( int i, int *index, int *line ) const +QTextStringChar *QTextParagraph::lineStartOfChar( int i, int *index, int *line ) const { if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); int l = (int)lineStarts.count() - 1; - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.end(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.end(); --it; for ( ;; ) { if ( i >= it.key() ) { if ( index ) @@ -4119,53 +4136,53 @@ QTextStringChar *QTextParag::lineStartOfChar( int i, int *index, int *line ) con --it; --l; } - qWarning( "QTextParag::lineStartOfChar: couldn't find %d", i ); + qWarning( "QTextParagraph::lineStartOfChar: couldn't find %d", i ); return 0; } -int QTextParag::lines() const +int QTextParagraph::lines() const { if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); return (int)lineStarts.count(); } -QTextStringChar *QTextParag::lineStartOfLine( int line, int *index ) const +QTextStringChar *QTextParagraph::lineStartOfLine( int line, int *index ) const { if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); if ( line >= 0 && line < (int)lineStarts.count() ) { - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while ( line-- > 0 ) ++it; int i = it.key(); if ( index ) *index = i; return &str->at( i ); } - qWarning( "QTextParag::lineStartOfLine: couldn't find %d", line ); + qWarning( "QTextParagraph::lineStartOfLine: couldn't find %d", line ); return 0; } -int QTextParag::leftGap() const +int QTextParagraph::leftGap() const { if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); int line = 0; int x = str->at(0).x; /* set x to x of first char */ if ( str->isBidi() ) { - for ( int i = 1; i < str->length(); ++i ) + for ( int i = 1; i < str->length()-1; ++i ) x = QMIN(x, str->at(i).x); return x; } - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while (line < (int)lineStarts.count()) { int i = it.key(); /* char index */ x = QMIN(x, str->at(i).x); ++it; @@ -4173,9 +4190,9 @@ int QTextParag::leftGap() const } return x; } -void QTextParag::setFormat( int index, int len, QTextFormat *f, bool useCollection, int flags ) +void QTextParagraph::setFormat( int index, int len, QTextFormat *f, bool useCollection, int flags ) { if ( !f ) return; if ( index < 0 ) @@ -4211,11 +4228,11 @@ void QTextParag::setFormat( int index, int len, QTextFormat *f, bool useCollecti } } } -void QTextParag::indent( int *oldIndent, int *newIndent ) +void QTextParagraph::indent( int *oldIndent, int *newIndent ) { - if ( !hasdoc || !document()->indent() || style() && style()->displayMode() != QStyleSheetItem::DisplayBlock ) { + if ( !hasdoc || !document()->indent() || isListItem() ) { if ( oldIndent ) *oldIndent = 0; if ( newIndent ) *newIndent = 0; @@ -4225,256 +4242,183 @@ void QTextParag::indent( int *oldIndent, int *newIndent ) } document()->indent()->indent( document(), this, oldIndent, newIndent ); } -void QTextParag::paint( QPainter &painter, const QColorGroup &cg, QTextCursor *cursor, bool drawSelections, +void QTextParagraph::paint( QPainter &painter, const QColorGroup &cg, QTextCursor *cursor, bool drawSelections, int clipx, int clipy, int clipw, int cliph ) { if ( !visible ) return; - QTextStringChar *chr = at( 0 ); - int i = 0; - int h = 0; - int baseLine = 0, lastBaseLine = 0; - QTextStringChar *formatChar = 0; - int lastY = -1; - int startX = 0; - int bw = 0; - int cy = 0; - int curx = -1, cury = 0, curh = 0; - bool lastDirection = chr->rightToLeft; - const int full_sel_width = (hasdoc ? document()->width() : r.width()); -#if 0 // seems we don't need that anymore - int tw = 0; -#endif - + QTextStringChar *chr = 0; + int i, y, h, baseLine, xstart, xend; + i = y =h = baseLine = 0; + QRect cursorRect; + drawSelections &= ( mSelections != 0 ); + // macintosh full-width selection style + bool fullWidthStyle = FALSE; + int fullSelectionWidth = 0; + if ( drawSelections && fullWidthStyle ) + fullSelectionWidth = (hasdoc ? document()->width() : r.width()); + QString qstr = str->toString(); - // ### workaround so that \n are not drawn, actually this should be - // fixed in QFont somewhere (under Windows you get ugly boxes + // ### workaround so that \n are not drawn, actually this should + // be fixed in QFont somewhere (under Windows you get ugly boxes // otherwise) QChar* uc = (QChar*) qstr.unicode(); - for ( uint ii = 0; ii < qstr.length(); ii++ ) { - if ( uc[(int)ii]== '\n' ) + for ( uint ii = 0; ii < qstr.length(); ii++ ) + if ( uc[(int)ii]== '\n' || uc[(int)ii] == QChar_linesep || uc[(int)ii] == '\t' ) uc[(int)ii] = 0x20; - } - - - const int nSels = hasdoc ? document()->numSelections() : 1; - QMemArray<int> selectionStarts( nSels ); - QMemArray<int> selectionEnds( nSels ); - if ( drawSelections ) { - bool hasASelection = FALSE; - for ( i = 0; i < nSels; ++i ) { - if ( !hasSelection( i ) ) { - selectionStarts[ i ] = -1; - selectionEnds[ i ] = -1; - } else { - hasASelection = TRUE; - selectionStarts[ i ] = selectionStart( i ); - int end = selectionEnd( i ); - if ( end == length() - 1 && n && n->hasSelection( i ) ) - end++; - selectionEnds[ i ] = end; - } - } - if ( !hasASelection ) - drawSelections = FALSE; - } int line = -1; - int cw; - bool didListLabel = FALSE; int paintStart = 0; - int paintEnd = -1; - int lasth = 0; + int selection = -1; for ( i = 0; i < length(); i++ ) { chr = at( i ); -#if 0 // seems we don't need that anymore - if ( !str->isBidi() && is_printer( &painter ) ) { // ### fix our broken ps-printer - if ( !chr->lineStart ) - chr->x = QMAX( chr->x, tw ); - else - tw = 0; + + // we flush at end of document + bool flush = i== length()-1; + bool selectionStateChanged = FALSE; + if ( !flush ) { + QTextStringChar *nextchr = at( i+1 ); + // we flush at end of line + flush |= nextchr->lineStart; + // we flush on format changes + flush |= ( nextchr->format() != chr->format() ); + // we flush on anchor changes + flush |= ( nextchr->isAnchor() != chr->isAnchor() ); + // we flush on start of run + flush |= nextchr->startOfRun; + // we flush on bidi changes + flush |= ( nextchr->rightToLeft != chr->rightToLeft ); + // we flush on tab + flush |= ( chr->c == '\t' ); + // we flush on soft hypens + flush |= ( chr->c.unicode() == 0xad ); + // we flush on custom items + flush |= chr->isCustom(); + // we flush before custom items + flush |= nextchr->isCustom(); + // when painting justified, we flush on spaces + if ((alignment() & Qt3::AlignJustify) == Qt3::AlignJustify ) + flush |= QTextFormatter::isBreakable( str, i ); + // we flush when the string is getting too long + flush |= ( i - paintStart >= 256 ); + // we flush when the selection state changes + if ( drawSelections ) { + for ( QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->begin(); + it != mSelections->end(); ++it ) + selectionStateChanged |=( (*it).start == i || (*it).start == i+1 || (*it).end == i+1 ); + flush |= selectionStateChanged; + } } -#endif - cw = string()->width( i ); - if ( chr->c == '\t' && i < length() - 1 ) - cw = at( i + 1 )->x - chr->x + 1; - if ( chr->c.unicode() == 0xad && i < length() - 1 ) - cw = 0; // init a new line if ( chr->lineStart ) { -#if 0 // seems we don't need that anymore - tw = 0; -#endif + if (fullWidthStyle && drawSelections && selection >= 0) + painter.fillRect( xend, y, fullSelectionWidth - xend, h, + (selection == QTextDocument::Standard || !hasdoc) ? + cg.color( QColorGroup::Highlight ) : + document()->selectionColor( selection ) ); ++line; - lineInfo( line, cy, h, baseLine ); - lasth = h; - if ( clipy != -1 && cy > clipy - r.y() + cliph ) // outside clip area, leave + paintStart = i; + lineInfo( line, y, h, baseLine ); + if ( clipy != -1 && cliph != 0 && y + r.y() - h > clipy + cliph ) { // outside clip area, leave break; - if ( lastBaseLine == 0 ) - lastBaseLine = baseLine; - } + } - // draw bullet list items - if ( !didListLabel && line == 0 && style() && style()->displayMode() == QStyleSheetItem::DisplayListItem ) { - didListLabel = TRUE; - drawLabel( &painter, chr->x, cy, 0, 0, baseLine, cg ); + // if this is the first line and we are a list item, draw the the bullet label + if ( line == 0 && isListItem() ) + drawLabel( &painter, chr->x, y, 0, 0, baseLine, cg ); } // check for cursor mark - if ( cursor && this == cursor->parag() && i == cursor->index() ) { - curx = cursor->x(); - QTextStringChar *c = chr; - if ( i > 0 ) - --c; - curh = c->format()->height(); - cury = cy + baseLine - c->format()->ascent(); - } - - // first time - start again... - if ( !formatChar || lastY == -1 ) { - formatChar = chr; - lastY = cy; - startX = chr->x; - if ( !chr->isCustom() && chr->c != '\n' ) - paintEnd = i; - bw = cw; - if ( !chr->isCustom() ) - continue; + if ( cursor && this == cursor->paragraph() && i == cursor->index() ) { + QTextStringChar *c = i == 0 ? chr : chr - 1; + cursorRect.setRect( cursor->x() , y + baseLine - c->format()->ascent(), + 1, c->format()->height() ); } - // check if selection state changed - bool selectionChange = FALSE; + // check if we are in a selection and store which one it is + selection = -1; if ( drawSelections ) { - for ( int j = 0; j < nSels; ++j ) { - selectionChange = selectionStarts[ j ] == i || selectionEnds[ j ] == i; - if ( selectionChange ) + for ( QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->begin(); + it != mSelections->end(); ++it ) + if ( (*it).start <= i && i < (*it).end + ( (*it).end == length()-1 && n && n->hasSelection(it.key()) ) ? 1:0 + // exclude the standard selection from printing + && (it.key() != QTextDocument::Standard || !is_printer( &painter) ) ) { + selection = it.key(); break; - } + } } - //if something (format, etc.) changed, draw what we have so far - if ( ( ( ( alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify && at(paintEnd)->c.isSpace() ) || - lastDirection != (bool)chr->rightToLeft || - chr->startOfRun || - lastY != cy || chr->format() != formatChar->format() || chr->isAnchor() != formatChar->isAnchor() || - ( paintEnd != -1 && at( paintEnd )->c =='\t' ) || chr->c == '\t' || - ( paintEnd != -1 && at( paintEnd )->c.unicode() == 0xad ) || chr->c.unicode() == 0xad || - selectionChange || chr->isCustom() ) ) { - if ( paintStart <= paintEnd ) { - // ### temporary hack until I get the new placement/shaping stuff working - int x = startX; - if ( ( alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify && paintEnd != -1 && - paintEnd > 1 && at( paintEnd )->c.isSpace() ) { - int add = str->at(paintEnd).x - str->at(paintEnd-1).x - str->width(paintEnd-1); - bw += ( lastDirection ? 0 : add ); - } - drawParagString( painter, qstr, paintStart, paintEnd - paintStart + 1, x, lastY, - lastBaseLine, bw, lasth, drawSelections, - formatChar, i, selectionStarts, selectionEnds, cg, lastDirection ); - } -#if 0 // seems we don't need that anymore - if ( !str->isBidi() && is_printer( &painter ) ) { // ### fix our broken ps-printer - if ( !chr->lineStart ) { - // ### the next line doesn't look 100% correct for arabic - tw = startX + painter.fontMetrics().width( qstr.mid(paintStart, paintEnd - paintStart +1) ); - chr->x = QMAX( chr->x, tw ); - } else { - tw = 0; - } - } -#endif - if ( !chr->isCustom() ) { - if ( chr->c != '\n' ) { - paintStart = i; - paintEnd = i; - } else { - paintStart = i+1; - paintEnd = -1; - } - formatChar = chr; - lastY = cy; - startX = chr->x; - bw = cw; + if ( flush ) { // something changed, draw what we have so far + if ( chr->rightToLeft ) { + xstart = chr->x; + xend = at( paintStart )->x + str->width( paintStart ); } else { - if ( chr->customItem()->placement() == QTextCustomItem::PlaceInline ) { - chr->customItem()->draw( &painter, chr->x, cy, clipx - r.x(), clipy - r.y(), clipw, cliph, cg, - nSels && selectionStarts[ 0 ] <= i && selectionEnds[ 0 ] >= i ); - paintStart = i+1; - paintEnd = -1; - formatChar = chr; - lastY = cy; - startX = chr->x + string()->width( i ); - bw = 0; - } else { - chr->customItem()->resize( chr->customItem()->width ); - paintStart = i+1; - paintEnd = -1; - formatChar = chr; - lastY = cy; - startX = chr->x + string()->width( i ); - bw = 0; - } + xstart = at( paintStart )->x; + if ( !selectionStateChanged && i < length() - 1 && !str->at( i + 1 ).lineStart ) + xend = str->at( i + 1 ).x; + else + xend = chr->x + str->width( i ); } - } else { - if ( chr->c != '\n' ) { - if( chr->rightToLeft ) { - startX = chr->x; - } - paintEnd = i; + + if ( (clipx == -1 || clipw == -1) || (xend >= clipx && xstart <= clipx + clipw) ) { + if ( !chr->isCustom() ) + drawString( painter, qstr, paintStart, i - paintStart + 1, xstart, y, + baseLine, xend-xstart, h, selection, + chr, cg, chr->rightToLeft ); + else if ( chr->customItem()->placement() == QTextCustomItem::PlaceInline ) + chr->customItem()->draw( &painter, chr->x, y, + clipx == -1 ? clipx : (clipx - r.x()), + clipy == -1 ? clipy : (clipy - r.y()), + clipw, cliph, cg, selection >= 0 ); } - bw += cw; + paintStart = i+1; } - lastBaseLine = baseLine; - lasth = h; - lastDirection = chr->rightToLeft; + } - // if we are through the parag, but still have some stuff left to draw, draw it now - if ( paintStart <= paintEnd ) { - bool selectionChange = FALSE; - if ( drawSelections ) { - for ( int j = 0; j < nSels; ++j ) { - selectionChange = selectionStarts[ j ] == i || selectionEnds[ j ] == i; - if ( selectionChange ) - break; + if (fullWidthStyle && drawSelections && selection >= 0 && next() && next()->mSelections) + for ( QMap<int, QTextParagraphSelection>::ConstIterator it = next()->mSelections->begin(); + it != next()->mSelections->end(); ++it ) + if (((*it).start) == 0) { + painter.fillRect( xend, y, fullSelectionWidth - xend, h, + (selection == QTextDocument::Standard || !hasdoc) ? + cg.color( QColorGroup::Highlight ) : + document()->selectionColor( selection ) ); + break; } - } - int x = startX; - drawParagString( painter, qstr, paintStart, paintEnd-paintStart+1, x, lastY, - lastBaseLine, bw, h, drawSelections, - formatChar, i, selectionStarts, selectionEnds, cg, lastDirection ); - } - // if we should draw a cursor, draw it now - if ( curx != -1 && cursor ) { - painter.fillRect( QRect( curx, cury, 1, curh - lineSpacing() ), cg.color( QColorGroup::Text ) ); + // time to draw the cursor + const int cursor_extent = 4; + if ( !cursorRect.isNull() && cursor && + ((clipx == -1 || clipw == -1) || (cursorRect.right()+cursor_extent >= clipx && cursorRect.left()-cursor_extent <= clipx + clipw)) ) { + painter.fillRect( cursorRect, cg.color( QColorGroup::Text ) ); painter.save(); if ( string()->isBidi() ) { - const int d = 4; if ( at( cursor->index() )->rightToLeft ) { painter.setPen( Qt::black ); - painter.drawLine( curx, cury, curx - d / 2, cury + d / 2 ); - painter.drawLine( curx, cury + d, curx - d / 2, cury + d / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); } else { painter.setPen( Qt::black ); - painter.drawLine( curx, cury, curx + d / 2, cury + d / 2 ); - painter.drawLine( curx, cury + d, curx + d / 2, cury + d / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); } } painter.restore(); } } //#define BIDI_DEBUG -void QTextParag::drawParagString( QPainter &painter, const QString &s, int start, int len, int startX, - int lastY, int baseLine, int bw, int h, bool drawSelections, - QTextStringChar *formatChar, int i, const QMemArray<int> &selectionStarts, - const QMemArray<int> &selectionEnds, const QColorGroup &cg, bool rightToLeft ) +void QTextParagraph::drawString( QPainter &painter, const QString &s, int start, int len, int xstart, + int y, int baseLine, int w, int h, int selection, + QTextStringChar *formatChar, const QColorGroup& cg, + bool rightToLeft ) { + int i = start + len - 1; bool plainText = hasdoc ? document()->textFormat() == Qt::PlainText : FALSE; QTextFormat* format = formatChar->format(); QString str( s ); if ( str[ (int)str.length() - 1 ].unicode() == 0xad ) @@ -4498,38 +4442,31 @@ void QTextParag::drawParagString( QPainter &painter, const QString &s, int start painter.setFont( fn ); } } - if ( drawSelections ) { - const int nSels = hasdoc ? document()->numSelections() : 1; - const int startSel = is_printer( 0 ) ? 1 : 0; - for ( int j = startSel; j < nSels; ++j ) { - if ( i > selectionStarts[ j ] && i <= selectionEnds[ j ] ) { - if ( !hasdoc || document()->invertSelectionText( j ) ) - painter.setPen( QPen( cg.color( QColorGroup::HighlightedText ) ) ); - if ( j == QTextDocument::Standard ) - painter.fillRect( startX, lastY, bw, h, cg.color( QColorGroup::Highlight ) ); - else - painter.fillRect( startX, lastY, bw, h, hasdoc ? document()->selectionColor( j ) : cg.color( QColorGroup::Highlight ) ); - } - } + if ( selection >= 0 ) { + if ( !hasdoc || document()->invertSelectionText( selection ) ) + painter.setPen( cg.color( QColorGroup::HighlightedText ) ); + painter.fillRect( xstart, y, w, h, + (selection == QTextDocument::Standard || !hasdoc) ? + cg.color( QColorGroup::Highlight ) : document()->selectionColor( selection ) ); } if ( str[ start ] != '\t' && str[ start ].unicode() != 0xad ) { if ( format->vAlign() == QTextFormat::AlignNormal ) { - painter.drawText( startX, lastY + baseLine, str.mid( start ), len ); + painter.drawText( xstart, y + baseLine, str.mid( start ), len ); #ifdef BIDI_DEBUG painter.save(); painter.setPen ( Qt::red ); - painter.drawLine( startX, lastY, startX, lastY + baseLine ); - painter.drawLine( startX, lastY + baseLine/2, startX + 10, lastY + baseLine/2 ); + painter.drawLine( xstart, y, xstart, y + baseLine ); + painter.drawLine( xstart, y + baseLine/2, xstart + 10, y + baseLine/2 ); int w = 0; int i = 0; while( i < len ) w += painter.fontMetrics().charWidth( str, start + i++ ); painter.setPen ( Qt::blue ); - painter.drawLine( startX + w - 1, lastY, startX + w - 1, lastY + baseLine ); - painter.drawLine( startX + w - 1, lastY + baseLine/2, startX + w - 1 - 10, lastY + baseLine/2 ); + painter.drawLine( xstart + w - 1, y, xstart + w - 1, y + baseLine ); + painter.drawLine( xstart + w - 1, y + baseLine/2, xstart + w - 1 - 10, y + baseLine/2 ); painter.restore(); #endif } else if ( format->vAlign() == QTextFormat::AlignSuperScript ) { QFont f( painter.font() ); @@ -4537,27 +4474,27 @@ void QTextParag::drawParagString( QPainter &painter, const QString &s, int start f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); else f.setPointSize( ( f.pointSize() * 2 ) / 3 ); painter.setFont( f ); - painter.drawText( startX, lastY + baseLine - ( painter.fontMetrics().height() / 2 ), - str.mid( start ), len ); + painter.drawText( xstart, y + baseLine - ( painter.fontMetrics().height() / 2 ), + str.mid( start ), len ); } else if ( format->vAlign() == QTextFormat::AlignSubScript ) { QFont f( painter.font() ); if ( format->fontSizesInPixels() ) f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); else f.setPointSize( ( f.pointSize() * 2 ) / 3 ); painter.setFont( f ); - painter.drawText( startX, lastY + baseLine + painter.fontMetrics().height() / 6, str.mid( start ), len ); + painter.drawText( xstart, y + baseLine + painter.fontMetrics().height() / 6, str.mid( start ), len ); } } if ( i + 1 < length() && at( i + 1 )->lineStart && at( i )->c.unicode() == 0xad ) { - painter.drawText( startX + bw, lastY + baseLine, "\xad" ); + painter.drawText( xstart + w, y + baseLine, "\xad" ); } if ( format->isMisspelled() ) { painter.save(); painter.setPen( QPen( Qt::red, 1, Qt::DotLine ) ); - painter.drawLine( startX, lastY + baseLine + 1, startX + bw, lastY + baseLine + 1 ); + painter.drawLine( xstart, y + baseLine + 1, xstart + w, y + baseLine + 1 ); painter.restore(); } i -= len; @@ -4567,43 +4504,48 @@ void QTextParag::drawParagString( QPainter &painter, const QString &s, int start ( document()->focusIndicator.start >= i && document()->focusIndicator.start + document()->focusIndicator.len <= i + len || document()->focusIndicator.start <= i && document()->focusIndicator.start + document()->focusIndicator.len >= i + len ) ) { - painter.drawWinFocusRect( QRect( startX, lastY, bw, h ) ); + painter.drawWinFocusRect( QRect( xstart, y, w, h ) ); } } -void QTextParag::drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg ) +void QTextParagraph::drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg ) { - if ( !style() ) - return; QRect r ( x, y, w, h ); QStyleSheetItem::ListStyle s = listStyle(); p->save(); - p->setPen( defFormat->color() ); - - QFont font2( defFormat->font() ); - if ( length() > 0 ) { - QTextFormat *format = at( 0 )->format(); - if ( format ) { - if ( format->fontSizesInPixels() ) - font2.setPixelSize( at( 0 )->format()->font().pixelSize() ); - else - font2.setPointSize( at( 0 )->format()->font().pointSize() ); - } + QTextFormat *format = at( 0 )->format(); + if ( format ) { + p->setPen( format->color() ); + p->setFont( format->font() ); } - p->setFont( font2 ); QFontMetrics fm( p->fontMetrics() ); int size = fm.lineSpacing() / 3; switch ( s ) { case QStyleSheetItem::ListDecimal: case QStyleSheetItem::ListLowerAlpha: case QStyleSheetItem::ListUpperAlpha: { - int n = numberOfSubParagraph(); + if ( list_val == -1 ) { // uninitialised list value, calcluate the right one + int depth = listDepth(); + list_val--; + // ### evil, square and expensive. This needs to be done when formatting, not when painting + QTextParagraph* s = prev(); + int depth_s; + while ( s && (depth_s = s->listDepth()) >= depth ) { + if ( depth_s == depth && s->isListItem() ) + list_val--; + s = s->prev(); + } + } + + int n = list_val; + if ( n < -1 ) + n = -n - 1; QString l; switch ( s ) { case QStyleSheetItem::ListLowerAlpha: if ( n < 27 ) { @@ -4626,9 +4568,9 @@ void QTextParag::drawLabel( QPainter* p, int x, int y, int w, int h, int base, c break; case QStyleSheetItem::ListSquare: { QRect er( r.right() - size * 2, r.top() + fm.height() / 2 - size / 2, size, size ); - p->fillRect( er , cg.brush( QColorGroup::Foreground ) ); + p->fillRect( er , cg.brush( QColorGroup::Text ) ); } break; case QStyleSheetItem::ListCircle: { @@ -4638,9 +4580,9 @@ void QTextParag::drawLabel( QPainter* p, int x, int y, int w, int h, int base, c break; case QStyleSheetItem::ListDisc: default: { - p->setBrush( cg.brush( QColorGroup::Foreground )); + p->setBrush( cg.brush( QColorGroup::Text )); QRect er( r.right()-size*2, r.top() + fm.height() / 2 - size / 2, size, size); p->drawEllipse( er ); p->setBrush( Qt::NoBrush ); } @@ -4649,144 +4591,50 @@ void QTextParag::drawLabel( QPainter* p, int x, int y, int w, int h, int base, c p->restore(); } -void QTextParag::setStyleSheetItems( const QPtrVector<QStyleSheetItem> &vec ) -{ - styleSheetItemsVec() = vec; - invalidate( 0 ); - lm = rm = tm = bm = flm = -1; - numSubParag = -1; -} - -void QTextParag::setList( bool b, int listStyle ) +void QTextParagraph::readStyleInformation( QDataStream& stream ) { - if ( !hasdoc ) - return; - - if ( !style() ) { - styleSheetItemsVec().resize( 2 ); - mStyleSheetItemsVec->insert( 0, document()->styleSheet()->item( "html" ) ); - mStyleSheetItemsVec->insert( 1, document()->styleSheet()->item( "p" ) ); - } - - if ( b ) { - if ( style()->displayMode() != QStyleSheetItem::DisplayListItem || this->listStyle() != listStyle ) { - styleSheetItemsVec().remove( styleSheetItemsVec().size() - 1 ); - QStyleSheetItem *item = (*mStyleSheetItemsVec)[ mStyleSheetItemsVec->size() - 1 ]; - if ( item ) - mStyleSheetItemsVec->remove( mStyleSheetItemsVec->size() - 1 ); - mStyleSheetItemsVec->insert( mStyleSheetItemsVec->size() - 1, - listStyle == QStyleSheetItem::ListDisc || listStyle == QStyleSheetItem::ListCircle - || listStyle == QStyleSheetItem::ListSquare ? - document()->styleSheet()->item( "ul" ) : document()->styleSheet()->item( "ol" ) ); - mStyleSheetItemsVec->insert( mStyleSheetItemsVec->size() - 1, document()->styleSheet()->item( "li" ) ); - setListStyle( (QStyleSheetItem::ListStyle)listStyle ); - } else { - return; - } - } else { - if ( style()->displayMode() != QStyleSheetItem::DisplayBlock ) { - styleSheetItemsVec().remove( styleSheetItemsVec().size() - 1 ); - if ( mStyleSheetItemsVec->size() >= 2 ) { - mStyleSheetItemsVec->remove( mStyleSheetItemsVec->size() - 2 ); - mStyleSheetItemsVec->resize( mStyleSheetItemsVec->size() - 2 ); - } else { - mStyleSheetItemsVec->resize( mStyleSheetItemsVec->size() - 1 ); - } - } else { - return; - } - } - invalidate( 0 ); - lm = rm = tm = bm = flm = -1; - numSubParag = -1; - if ( next() ) { - QTextParag *s = next(); - while ( s ) { - s->numSubParag = -1; - s->lm = s->rm = s->tm = s->bm = flm = -1; - s->numSubParag = -1; - s->invalidate( 0 ); - s = s->next(); - } + int int_align, int_lstyle; + uchar uchar_litem, uchar_rtext, uchar_dir; + stream >> int_align >> int_lstyle >> utm >> ubm >> ulm >> urm >> uflm + >> ulinespacing >> ldepth >> uchar_litem >> uchar_rtext >> uchar_dir; + align = int_align; lstyle = (QStyleSheetItem::ListStyle) int_lstyle; + litem = uchar_litem; rtext = uchar_rtext; str->setDirection( (QChar::Direction)uchar_dir ); + QTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); } } -void QTextParag::incDepth() +void QTextParagraph::writeStyleInformation( QDataStream& stream ) const { - if ( !style() || !hasdoc ) - return; - if ( style()->displayMode() != QStyleSheetItem::DisplayListItem ) - return; - styleSheetItemsVec().resize( styleSheetItemsVec().size() + 1 ); - mStyleSheetItemsVec->insert( mStyleSheetItemsVec->size() - 1, (*mStyleSheetItemsVec)[ mStyleSheetItemsVec->size() - 2 ] ); - mStyleSheetItemsVec->insert( mStyleSheetItemsVec->size() - 2, - listStyle() == QStyleSheetItem::ListDisc || listStyle() == QStyleSheetItem::ListCircle || - listStyle() == QStyleSheetItem::ListSquare ? - document()->styleSheet()->item( "ul" ) : document()->styleSheet()->item( "ol" ) ); - invalidate( 0 ); - lm = -1; - flm = -1; + stream << (int) align << (int) lstyle << utm << ubm << ulm << urm << uflm << ulinespacing << ldepth << (uchar)litem << (uchar)rtext << (uchar)str->direction(); } -void QTextParag::decDepth() -{ - if ( !style() || !hasdoc ) - return; - if ( style()->displayMode() != QStyleSheetItem::DisplayListItem ) - return; - int numLists = 0; - QStyleSheetItem *lastList = 0; - int lastIndex = 0; - int i; - if ( mStyleSheetItemsVec ) { - for ( i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - QStyleSheetItem *item = (*mStyleSheetItemsVec)[ i ]; - if ( item->name() == "ol" || item->name() == "ul" ) { - lastList = item; - lastIndex = i; - numLists++; - } - } - } - if ( !lastList ) - return; - styleSheetItemsVec().remove( lastIndex ); - for ( i = lastIndex; i < (int)mStyleSheetItemsVec->size() - 1; ++i ) - mStyleSheetItemsVec->insert( i, (*mStyleSheetItemsVec)[ i + 1 ] ); - mStyleSheetItemsVec->resize( mStyleSheetItemsVec->size() - 1 ); - if ( numLists == 1 ) - setList( FALSE, -1 ); - invalidate( 0 ); - lm = -1; - flm = -1; -} -int QTextParag::listDepth() const -{ - int numLists = 0; - int i; - if ( mStyleSheetItemsVec ) { - for ( i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - QStyleSheetItem *item = (*mStyleSheetItemsVec)[ i ]; - if ( item->name() == "ol" || item->name() == "ul" ) - numLists++; - } +void QTextParagraph::setListDepth( int depth ) { + if ( !hasdoc || depth == ldepth ) + return; + ldepth = depth; + QTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); } - return numLists - 1; } -int *QTextParag::tabArray() const +int *QTextParagraph::tabArray() const { int *ta = tArray; if ( !ta && hasdoc ) ta = document()->tabArray(); return ta; } -int QTextParag::nextTab( int, int x ) +int QTextParagraph::nextTab( int, int x ) { int *ta = tArray; if ( hasdoc ) { if ( !ta ) @@ -4810,17 +4658,17 @@ int QTextParag::nextTab( int, int x ) return tabStopWidth * ( d + 1 ); } } -void QTextParag::adjustToPainter( QPainter *p ) +void QTextParagraph::adjustToPainter( QPainter *p ) { for ( int i = 0; i < length(); ++i ) { if ( at( i )->isCustom() ) at( i )->customItem()->adjustToPainter( p ); } } -QTextFormatCollection *QTextParag::formatCollection() const +QTextFormatCollection *QTextParagraph::formatCollection() const { if ( hasdoc ) return document()->formatCollection(); if ( !qFormatCollection ) { @@ -4830,16 +4678,21 @@ QTextFormatCollection *QTextParag::formatCollection() const } return qFormatCollection; } -QString QTextParag::richText() const +QString QTextParagraph::richText() const { QString s; QTextStringChar *formatChar = 0; QString spaces; - bool lastCharWasSpace = FALSE; - int firstcol = 0; - for ( int i = 0; i < length()-1; ++i ) { + bool doStart = richTextExportStart && richTextExportStart->paragraph() == this; + bool doEnd = richTextExportEnd && richTextExportEnd->paragraph() == this; + int i; + for ( i = 0; i < length()-1; ++i ) { + if ( doStart && i && richTextExportStart->index() == i ) + s += "<selstart/>"; + if ( doEnd && richTextExportEnd->index() == i ) + s += "<selend/>"; QTextStringChar *c = &str->at( i ); if ( c->isAnchor() && !c->anchorName().isEmpty() ) { if ( c->anchorName().contains( '#' ) ) { QStringList l = QStringList::split( '#', c->anchorName() ); @@ -4849,293 +4702,160 @@ QString QTextParag::richText() const s += "<a name=\"" + c->anchorName() + "\"></a>"; } } if ( !formatChar ) { - s += c->format()->makeFormatChangeTags( 0, QString::null, c->anchorHref() ); + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + 0, QString::null, c->anchorHref() ); formatChar = c; } else if ( ( formatChar->format()->key() != c->format()->key() ) || - (formatChar->isAnchor() != c->isAnchor() && - (!c->anchorHref().isEmpty() || !formatChar->anchorHref().isEmpty() ) ) ) {// lisp was here - - if ( !spaces.isEmpty() ) { - if ( spaces[0] == '\t' || lastCharWasSpace ) - s += "<wsp>" + spaces + "</wsp>"; - else if ( spaces.length() > 1 ) - s += "<wsp>" + spaces.mid(1) + "</wsp> "; - else - s += spaces; - lastCharWasSpace = TRUE; - spaces = QString::null; - } - s += c->format()->makeFormatChangeTags( formatChar->format() , formatChar->anchorHref(), c->anchorHref() ); + (c->anchorHref() != formatChar->anchorHref() ) ) { + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + formatChar->format() , formatChar->anchorHref(), c->anchorHref() ); formatChar = c; } - - if ( c->c == ' ' || c->c == '\t' ) { - spaces += c->c; - continue; - } else if ( !spaces.isEmpty() ) { - if ( spaces[0] == '\t' || lastCharWasSpace ) - s += "<wsp>" + spaces + "</wsp>"; - else if ( spaces.length() > 1 ) - s += "<wsp>" + spaces.mid(1) + "</wsp> "; - else - s += spaces; - spaces = QString::null; - if ( s.length() - firstcol > 60 ) { - s += '\n'; - firstcol = s.length(); - } - } - - lastCharWasSpace = FALSE; - if ( c->c == '<' ) { + if ( c->c == '<' ) s += "<"; - } else if ( c->c == '>' ) { + else if ( c->c == '>' ) s += ">"; - } else if ( c->isCustom() ) { + else if ( c->isCustom() ) s += c->customItem()->richText(); - } else { - s += c->c; - } - } - if ( !spaces.isEmpty() ) { - if ( spaces.length() > 1 || spaces[0] == '\t' || lastCharWasSpace ) - s += "<wsp>" + spaces + "</wsp>"; + else if ( c->c == '\n' || c->c == QChar_linesep ) + s += "<br />"; // space on purpose for compatibility with Netscape, Lynx & Co. else - s += spaces; + s += c->c; } - + if ( doEnd && richTextExportEnd->index() == i ) + s += "<selend/>"; if ( formatChar ) - s += formatChar->format()->makeFormatEndTags( formatChar->anchorHref() ); + s += formatChar->format()->makeFormatEndTags( formatCollection()->defaultFormat(), formatChar->anchorHref() ); return s; } -void QTextParag::addCommand( QTextCommand *cmd ) +void QTextParagraph::addCommand( QTextCommand *cmd ) { if ( !hasdoc ) pseudoDocument()->commandHistory->addCommand( cmd ); else document()->commands()->addCommand( cmd ); } -QTextCursor *QTextParag::undo( QTextCursor *c ) +QTextCursor *QTextParagraph::undo( QTextCursor *c ) { if ( !hasdoc ) return pseudoDocument()->commandHistory->undo( c ); return document()->commands()->undo( c ); } -QTextCursor *QTextParag::redo( QTextCursor *c ) +QTextCursor *QTextParagraph::redo( QTextCursor *c ) { if ( !hasdoc ) return pseudoDocument()->commandHistory->redo( c ); return document()->commands()->redo( c ); } -int QTextParag::topMargin() const +int QTextParagraph::topMargin() const { - if ( !p && ( !hasdoc || !document()->addMargins() ) ) - return 0; - if ( tm != -1 ) - return tm; - QStyleSheetItem *item = style(); - if ( !item ) { - ( (QTextParag*)this )->tm = 0; - return 0; - } - int m = 0; - if ( item->margin( QStyleSheetItem::MarginTop ) != QStyleSheetItem::Undefined ) - m = item->margin( QStyleSheetItem::MarginTop ); - if ( mStyleSheetItemsVec ) { - QStyleSheetItem *it = 0; - QStyleSheetItem *p = prev() ? prev()->style() : 0; - for ( int i = (int)mStyleSheetItemsVec->size() - 2 ; i >= 0; --i ) { - it = (*mStyleSheetItemsVec)[ i ]; - if ( it != p ) - break; - int mar = it->margin( QStyleSheetItem::MarginTop ); - m += (mar != QStyleSheetItem::Undefined) ? mar : 0; - if ( it->displayMode() != QStyleSheetItem::DisplayInline ) - break; - } + if ( rtext ) { + m = isListItem() ? (document()->li_tm/QMAX(1,listDepth())) : document()->par_tm; + if ( listDepth() == 1 &&( !prev() || prev()->listDepth() < listDepth() ) ) + m = QMAX( m, document()->list_tm ); } - m = scale( m, QTextFormat::painter() ); - - ( (QTextParag*)this )->tm = m; - return tm; + m += utm; + return scale( m, QTextFormat::painter() ); } -int QTextParag::bottomMargin() const +int QTextParagraph::bottomMargin() const { - if ( bm != -1 ) - return bm; - QStyleSheetItem *item = style(); - if ( !item || !next() ) { - ( (QTextParag*)this )->bm = 0; - return 0; - } - int m = 0; - if ( item->margin( QStyleSheetItem::MarginBottom ) != QStyleSheetItem::Undefined ) - m = item->margin( QStyleSheetItem::MarginBottom ); - if ( mStyleSheetItemsVec ) { - QStyleSheetItem *it = 0; - QStyleSheetItem *n = next() ? next()->style() : 0; - for ( int i =(int)mStyleSheetItemsVec->size() - 2 ; i >= 0; --i ) { - it = (*mStyleSheetItemsVec)[ i ]; - if ( it != n ) - break; - int mar = it->margin( QStyleSheetItem::MarginBottom ); - m += mar != QStyleSheetItem::Undefined ? mar : 0; - if ( it->displayMode() != QStyleSheetItem::DisplayInline ) - break; - } + if ( rtext ) { + m = isListItem() ? (document()->li_bm/QMAX(1,listDepth())) : document()->par_bm; + if ( listDepth() == 1 &&( !next() || next()->listDepth() < listDepth() ) ) + m = QMAX( m, document()->list_bm ); } - m = scale ( m, QTextFormat::painter() ); - - ( (QTextParag*)this )->bm = m; - return bm; + m += ubm; + return scale( m, QTextFormat::painter() ); } -int QTextParag::leftMargin() const +int QTextParagraph::leftMargin() const { - if ( lm != -1 ) - return lm; - QStyleSheetItem *item = style(); - if ( !item ) { - ( (QTextParag*)this )->lm = 0; - return 0; - } - int m = 0; - if ( mStyleSheetItemsVec ) { - for ( int i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - item = (*mStyleSheetItemsVec)[ i ]; - int mar = item->margin( QStyleSheetItem::MarginLeft ); - m += mar != QStyleSheetItem::Undefined ? mar : 0; - if ( item->name() == "ol" || item->name() == "ul" ) { - QPainter* oldPainter = QTextFormat::painter(); - QTextFormat::setPainter( 0 ); - m += defFormat->width( '1' ) + - defFormat->width( '2' ) + - defFormat->width( '3' ) + - defFormat->width( '.' ); - QTextFormat::setPainter( oldPainter ); - } - } - } - - m = scale ( m, QTextFormat::painter() ); - - ( (QTextParag*)this )->lm = m; - return lm; + int m = ulm; + if ( listDepth() ) + m += listDepth() * document()->list_lm; + return scale( m, QTextFormat::painter() ); } -int QTextParag::firstLineMargin() const +int QTextParagraph::firstLineMargin() const { - if ( flm != -1 ) - return lm; - QStyleSheetItem *item = style(); - if ( !item ) { - ( (QTextParag*)this )->flm = 0; - return 0; - } - int m = 0; - if ( mStyleSheetItemsVec ) { - for ( int i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - item = (*mStyleSheetItemsVec)[ i ]; - int mar = item->margin( QStyleSheetItem::MarginFirstLine ); - m += mar != QStyleSheetItem::Undefined ? mar : 0; - } - } - - m = scale( m, QTextFormat::painter() ); - - ( (QTextParag*)this )->flm = m; - return flm; + int m = uflm; + return scale( m, QTextFormat::painter() ); } -int QTextParag::rightMargin() const +int QTextParagraph::rightMargin() const { - if ( rm != -1 ) - return rm; - QStyleSheetItem *item = style(); - if ( !item ) { - ( (QTextParag*)this )->rm = 0; - return 0; - } - int m = 0; - if ( mStyleSheetItemsVec ) { - for ( int i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - item = (*mStyleSheetItemsVec)[ i ]; - int mar = item->margin( QStyleSheetItem::MarginRight ); - m += mar != QStyleSheetItem::Undefined ? mar : 0; - } - } - m = scale( m, QTextFormat::painter() ); - - ( (QTextParag*)this )->rm = m; - return rm; + int m = urm; + return scale( m, QTextFormat::painter() ); } -int QTextParag::lineSpacing() const +int QTextParagraph::lineSpacing() const { - QStyleSheetItem *item = style(); - if ( !item ) - return 0; - - int ls = item->lineSpacing(); - if ( ls == QStyleSheetItem::Undefined ) - return 0; - ls = scale( ls, QTextFormat::painter() ); - - return ls; + int l = ulinespacing; + l = scale( l, QTextFormat::painter() ); + return l; } -void QTextParag::copyParagData( QTextParag *parag ) +void QTextParagraph::copyParagData( QTextParagraph *parag ) { - setStyleSheetItems( parag->styleSheetItems() ); - setListStyle( parag->listStyle() ); - setAlignment( parag->alignment() ); + rtext = parag->rtext; + lstyle = parag->lstyle; + ldepth = parag->ldepth; + litem = parag->litem; + align = parag->align; + utm = parag->utm; + ubm = parag->ubm; + urm = parag->urm; + ulm = parag->ulm; + uflm = parag->uflm; + ulinespacing = parag->ulinespacing; QColor *c = parag->backgroundColor(); if ( c ) setBackgroundColor( *c ); + str->setDirection( parag->str->direction() ); } -void QTextParag::show() +void QTextParagraph::show() { if ( visible || !hasdoc ) return; visible = TRUE; } -void QTextParag::hide() +void QTextParagraph::hide() { if ( !visible || !hasdoc ) return; visible = FALSE; } -void QTextParag::setDirection( QChar::Direction d ) +void QTextParagraph::setDirection( QChar::Direction d ) { if ( str && str->direction() != d ) { str->setDirection( d ); invalidate( 0 ); } } -QChar::Direction QTextParag::direction() const +QChar::Direction QTextParagraph::direction() const { return (str ? str->direction() : QChar::DirON ); } -void QTextParag::setChanged( bool b, bool recursive ) +void QTextParagraph::setChanged( bool b, bool recursive ) { changed = b; if ( recursive ) { - if ( document() && document()->parentParag() ) - document()->parentParag()->setChanged( b, recursive ); + if ( document() && document()->parentParagraph() ) + document()->parentParagraph()->setChanged( b, recursive ); } } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -5151,18 +4871,15 @@ QTextFormatter::QTextFormatter() : thisminw(0), thiswused(0), wrapEnabled( TRUE ), wrapColumn( -1 ), biw( FALSE ) { } -/* only used for bidi or complex text reordering - */ -QTextParagLineStart *QTextFormatter::formatLine( QTextParag *parag, QTextString *string, QTextParagLineStart *line, +QTextLineStart *QTextFormatter::formatLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, QTextStringChar *startChar, QTextStringChar *lastChar, int align, int space ) { #ifndef QT_NO_COMPLEXTEXT if( string->isBidi() ) return bidiReorderLine( parag, string, line, startChar, lastChar, align, space ); #endif - space = QMAX( space, 0 ); // #### with nested tables this gets negative because of a bug I didn't find yet, so workaround for now. This also means non-left aligned nested tables do not work at the moment int start = (startChar - &string->at(0)); int last = (lastChar - &string->at(0) ); // do alignment Auto == Left in this case if ( align & Qt::AlignHCenter || align & Qt::AlignRight ) { @@ -5171,9 +4888,15 @@ QTextParagLineStart *QTextFormatter::formatLine( QTextParag *parag, QTextString for ( int j = start; j <= last; ++j ) string->at( j ).x += space; } else if ( align & Qt3::AlignJustify ) { int numSpaces = 0; - for ( int j = start; j < last; ++j ) { + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + if ( string->at( j ).c == '\t' ) { + start = j+1; + break; + } if( isBreakable( string, j ) ) { numSpaces++; } } @@ -5193,9 +4916,9 @@ QTextParagLineStart *QTextFormatter::formatLine( QTextParag *parag, QTextString line->w = string->at( last ).x + string->width( last ); else line->w = 0; - return new QTextParagLineStart(); + return new QTextLineStart(); } #ifndef QT_NO_COMPLEXTEXT @@ -5203,14 +4926,13 @@ QTextParagLineStart *QTextFormatter::formatLine( QTextParag *parag, QTextString #include <iostream> #endif // collects one line of the paragraph and transforms it to visual order -QTextParagLineStart *QTextFormatter::bidiReorderLine( QTextParag * /*parag*/, QTextString *text, QTextParagLineStart *line, +QTextLineStart *QTextFormatter::bidiReorderLine( QTextParagraph * /*parag*/, QTextString *text, QTextLineStart *line, QTextStringChar *startChar, QTextStringChar *lastChar, int align, int space ) { int start = (startChar - &text->at(0)); int last = (lastChar - &text->at(0) ); - //qDebug("doing BiDi reordering from %d to %d!", start, last); QBidiControl *control = new QBidiControl( line->context(), line->status ); QString str; str.setUnicode( 0, last - start + 1 ); @@ -5242,9 +4964,15 @@ QTextParagLineStart *QTextFormatter::bidiReorderLine( QTextParag * /*parag*/, QT x += space/2; else if ( align & Qt::AlignRight ) x += space; else if ( align & Qt3::AlignJustify ) { - for ( int j = start; j < last; ++j ) { + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + if ( text->at( j ).c == '\t' ) { + start = j+1; + break; + } if( isBreakable( text, j ) ) { numSpaces++; } } @@ -5304,9 +5032,8 @@ QTextParagLineStart *QTextFormatter::bidiReorderLine( QTextParag * /*parag*/, QT ww = text->width( pos ); } else { ww = c->format()->width( ' ' ); } - //qDebug("setting char %d at pos %d", pos, x); if ( xmax < x + toAdd + ww ) xmax = x + toAdd + ww; x += ww; pos++; } @@ -5315,19 +5042,21 @@ QTextParagLineStart *QTextFormatter::bidiReorderLine( QTextParag * /*parag*/, QT r = runs->next(); } line->w = xmax + 10; - QTextParagLineStart *ls = new QTextParagLineStart( control->context, control->status ); + QTextLineStart *ls = new QTextLineStart( control->context, control->status ); delete control; delete runs; return ls; } #endif -bool QTextFormatter::isBreakable( QTextString *string, int pos ) const +bool QTextFormatter::isBreakable( QTextString *string, int pos ) { const QChar &c = string->at( pos ).c; char ch = c.latin1(); + if ( c == QChar_linesep ) + return TRUE; if ( c.isSpace() && ch != '\n' && c.unicode() != 0x00a0U ) return TRUE; if ( c.unicode() == 0xad ) // soft hyphen return TRUE; @@ -5373,15 +5102,15 @@ bool QTextFormatter::isBreakable( QTextString *string, int pos ) const } return FALSE; } -void QTextFormatter::insertLineStart( QTextParag *parag, int index, QTextParagLineStart *ls ) +void QTextFormatter::insertLineStart( QTextParagraph *parag, int index, QTextLineStart *ls ) { if ( index > 0 ) { // we can assume that only first line starts are insrted multiple times parag->lineStartList().insert( index, ls ); return; } - QMap<int, QTextParagLineStart*>::Iterator it; + QMap<int, QTextLineStart*>::Iterator it; if ( ( it = parag->lineStartList().find( index ) ) == parag->lineStartList().end() ) { parag->lineStartList().insert( index, ls ); } else { delete *it; @@ -5393,16 +5122,16 @@ void QTextFormatter::insertLineStart( QTextParag *parag, int index, QTextParagLi /* Standard pagebreak algorithm using QTextFlow::adjustFlow. Returns the shift of the paragraphs bottom line. */ -int QTextFormatter::formatVertically( QTextDocument* doc, QTextParag* parag ) +int QTextFormatter::formatVertically( QTextDocument* doc, QTextParagraph* parag ) { int oldHeight = parag->rect().height(); - QMap<int, QTextParagLineStart*>& lineStarts = parag->lineStartList(); - QMap<int, QTextParagLineStart*>::Iterator it = lineStarts.begin(); - int h = doc->addMargins() ? parag->topMargin() : 0; + QMap<int, QTextLineStart*>& lineStarts = parag->lineStartList(); + QMap<int, QTextLineStart*>::Iterator it = lineStarts.begin(); + int h = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin() ) / 2: 0; for ( ; it != lineStarts.end() ; ++it ) { - QTextParagLineStart * ls = it.data(); + QTextLineStart * ls = it.data(); ls->y = h; QTextStringChar *c = ¶g->string()->at(it.key()); if ( c && c->customItem() && c->customItem()->ownLine() ) { int h = c->customItem()->height; @@ -5419,12 +5148,12 @@ int QTextFormatter::formatVertically( QTextDocument* doc, QTextParag* parag ) } h = ls->y + ls->h; } int m = parag->bottomMargin(); - if ( parag->next() && doc && !doc->addMargins() ) - m = QMAX( m, parag->next()->topMargin() ); - if ( parag->next() && parag->next()->isLineBreak() ) + if ( !parag->next() ) m = 0; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; h += m; parag->setHeight( h ); return h - oldHeight; } @@ -5434,17 +5163,19 @@ int QTextFormatter::formatVertically( QTextDocument* doc, QTextParag* parag ) QTextFormatterBreakInWords::QTextFormatterBreakInWords() { } -int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParag *parag, - int start, const QMap<int, QTextParagLineStart*> & ) +#define SPACE(s) doc?(s>0?s:0):s + +int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParagraph *parag, + int start, const QMap<int, QTextLineStart*> & ) { QTextStringChar *c = 0; QTextStringChar *firstChar = 0; - int left = doc ? parag->leftMargin() + doc->leftMargin() : 4; + int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; int x = left + ( doc ? parag->firstLineMargin() : 0 ); int dw = parag->documentVisibleWidth() - ( doc ? doc->rightMargin() : 0 ); - int y = doc && doc->addMargins() ? parag->topMargin() : 0; + int y = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; int h = y; int len = parag->length(); if ( doc ) x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 4 ); @@ -5459,9 +5190,9 @@ int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParag *parag, if ( start == 0 ) c = ¶g->string()->at( 0 ); int i = start; - QTextParagLineStart *lineStart = new QTextParagLineStart( y, y, 0 ); + QTextLineStart *lineStart = new QTextLineStart( y, y, 0 ); insertLineStart( parag, 0, lineStart ); QPainter *painter = QTextFormat::painter(); @@ -5500,9 +5231,9 @@ int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParag *parag, c->customItem()->resize( w - x ); w = dw; y += h; h = c->height(); - lineStart = new QTextParagLineStart( y, h, h ); + lineStart = new QTextLineStart( y, h, h ); insertLineStart( parag, i, lineStart ); c->lineStart = 1; firstChar = c; x = 0xffffff; @@ -5510,15 +5241,14 @@ int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParag *parag, } if ( wrapEnabled && ( wrapAtColumn() == -1 && x + ww > w || - wrapAtColumn() != -1 && col >= wrapAtColumn() ) || - parag->isNewLinesAllowed() && lastChr == '\n' ) { + wrapAtColumn() != -1 && col >= wrapAtColumn() ) ) { x = doc ? parag->document()->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; w = dw; y += h; h = c->height(); - lineStart = formatLine( parag, parag->string(), lineStart, firstChar, c-1 ); + lineStart = formatLine( parag, parag->string(), lineStart, firstChar, SPACE(c-1) ); lineStart->y = y; insertLineStart( parag, i, lineStart ); lineStart->baseLine = c->ascent(); lineStart->h = c->height(); @@ -5538,14 +5268,16 @@ int QTextFormatterBreakInWords::format( QTextDocument *doc,QTextParag *parag, wused = QMAX( wused, x ); } int m = parag->bottomMargin(); - if ( parag->next() && doc && !doc->addMargins() ) - m = QMAX( m, parag->next()->topMargin() ); - parag->setFullWidth( fullWidth ); - if ( parag->next() && parag->next()->isLineBreak() ) + if ( !parag->next() ) m = 0; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); y += h + m; + if ( doc ) + minw += doc->rightMargin(); if ( !wrapEnabled ) minw = QMAX(minw, wused); thisminw = minw; @@ -5565,17 +5297,17 @@ QTextFormatterBreakWords::QTextFormatterBreakWords() lineStart->y += shift;\ y += shift;\ }}while(FALSE) -int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, - int start, const QMap<int, QTextParagLineStart*> & ) +int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParagraph *parag, + int start, const QMap<int, QTextLineStart*> & ) { QTextStringChar *c = 0; QTextStringChar *firstChar = 0; QTextString *string = parag->string(); int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; int x = left + ( doc ? parag->firstLineMargin() : 0 ); - int y = doc && doc->addMargins() ? parag->topMargin() : 0; + int y = parag->prev() ? QMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; int h = y; int len = parag->length(); if ( doc ) x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 0 ); @@ -5589,17 +5321,17 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, int marg = left + rdiff; int minw = 0; int wused = 0; int tminw = marg; - int linespace = doc ? parag->lineSpacing() : 0; + int linespacing = doc ? parag->lineSpacing() : 0; bool wrapEnabled = isWrapEnabled( parag ); start = 0; if ( start == 0 ) c = ¶g->string()->at( 0 ); int i = start; - QTextParagLineStart *lineStart = new QTextParagLineStart( y, y, 0 ); + QTextLineStart *lineStart = new QTextLineStart( y, y, 0 ); insertLineStart( parag, 0, lineStart ); int lastBreak = -1; int tmpBaseLine = 0, tmph = 0; bool lastWasNonInlineCustom = FALSE; @@ -5653,9 +5385,9 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, QTextCustomItem* ci = c->customItem(); if ( c->isCustom() && ci->ownLine() ) { x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); - QTextParagLineStart *lineStart2 = formatLine( parag, string, lineStart, firstChar, c-1, align, w - x ); + QTextLineStart *lineStart2 = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x) ); ci->resize( w - x); if ( ci->width < w - x ) { if ( align & Qt::AlignHCenter ) x = ( w - ci->width ) / 2; @@ -5665,18 +5397,18 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, } c->x = x; curLeft = x; if ( i == 0 || !isBreakable( string, i - 1 ) || string->at( i - 1 ).lineStart == 0 ) { - y += QMAX( h, tmph ); - tmph = c->height() + linespace; + y += QMAX( h, QMAX( tmph, linespacing ) ); + tmph = c->height(); h = tmph; lineStart = lineStart2; lineStart->y = y; insertLineStart( parag, i, lineStart ); c->lineStart = 1; firstChar = c; } else { - tmph = c->height() + linespace; + tmph = c->height(); h = tmph; delete lineStart2; } lineStart->h = h; @@ -5685,9 +5417,9 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, lastBreak = -2; x = 0xffffff; minw = QMAX( minw, tminw ); - int tw = ci->minimumWidth(); + int tw = ci->minimumWidth() + ( doc ? doc->leftMargin() : 0 ); if ( tw < QWIDGETSIZE_MAX ) tminw = tw; else tminw = marg; @@ -5698,36 +5430,51 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, if ( tw < QWIDGETSIZE_MAX ) minw = QMAX( minw, tw ); } - if ( wrapEnabled && ( !c->c.isSpace() || lastBreak == -2 ) - && ( lastBreak != -1 || allowBreakInWords() ) && - ( wrapAtColumn() == -1 && x + ww > w && lastBreak != -1 || - wrapAtColumn() == -1 && x + ww > w - 4 && lastBreak == -1 && allowBreakInWords() || - wrapAtColumn() != -1 && col >= wrapAtColumn() ) || - parag->isNewLinesAllowed() && lastChr == '\n' && firstChar < c ) { + bool lastWasOwnLineCustomItem = lastBreak == -2; + bool hadBreakableChar = lastBreak != -1; + bool lastWasHardBreak = lastChr == QChar_linesep; + + // we break if + // 1. the last character was a hard break (QChar_linesep) or + // 2. the last charater was a own-line custom item (eg. table or ruler) or + // 3. wrapping was enabled, it was not a space and following + // condition is true: We either had a breakable character + // previously or we ar allowed to break in words and - either + // we break at w pixels and the current char would exceed that + // or - we break at a column and the current character would + // exceed that. + if ( lastWasHardBreak || lastWasOwnLineCustomItem || + ( wrapEnabled && + ( (!c->c.isSpace() && (hadBreakableChar || allowBreakInWords()) && + ( (wrapAtColumn() == -1 && x + ww > w) || + (wrapAtColumn() != -1 && col >= wrapAtColumn()) ) ) ) + ) + ) { if ( wrapAtColumn() != -1 ) minw = QMAX( minw, x + ww ); - if ( lastBreak < 0 ) { + // if a break was forced (no breakable char, hard break or own line custom item), break immediately.... + if ( !hadBreakableChar || lastWasHardBreak || lastWasOwnLineCustomItem ) { if ( lineStart ) { lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); h = QMAX( h, tmph ); lineStart->h = h; DO_FLOW( lineStart ); } - lineStart = formatLine( parag, string, lineStart, firstChar, c-1, align, w - x ); + lineStart = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x) ); x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); - if ( parag->isNewLinesAllowed() && c->c == '\t' ) { + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling int nx = parag->nextTab( i, x - left ) + left; if ( nx < x ) ww = w - x; else ww = nx - x; } curLeft = x; - y += h; - tmph = c->height() + linespace; + y += QMAX( h, linespacing ); + tmph = c->height(); h = 0; lineStart->y = y; insertLineStart( parag, i, lineStart ); lineStart->baseLine = c->ascent(); @@ -5736,24 +5483,24 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, firstChar = c; tmpBaseLine = lineStart->baseLine; lastBreak = -1; col = 0; - } else { + } else { // ... otherwise if we had a breakable char, break there DO_FLOW( lineStart ); i = lastBreak; - lineStart = formatLine( parag, string, lineStart, firstChar, parag->at( lastBreak ), align, w - string->at( i ).x ); + lineStart = formatLine( parag, string, lineStart, firstChar, parag->at( lastBreak ),align, SPACE(w - string->at( i ).x) ); x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); - if ( parag->isNewLinesAllowed() && c->c == '\t' ) { + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling int nx = parag->nextTab( i, x - left ) + left; if ( nx < x ) ww = w - x; else ww = nx - x; } curLeft = x; - y += h; - tmph = c->height() + linespace; + y += QMAX( h, linespacing ); + tmph = c->height(); h = tmph; lineStart->y = y; insertLineStart( parag, i + 1, lineStart ); lineStart->baseLine = c->ascent(); @@ -5765,12 +5512,12 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, col = 0; tminw = marg; continue; } - } else if ( lineStart && ( isBreakable( string, i ) || parag->isNewLinesAllowed() && c->c == '\n' ) ) { + } else if ( lineStart && isBreakable( string, i ) ) { if ( len <= 2 || i < len - 1 ) { tmpBaseLine = QMAX( tmpBaseLine, c->ascent() ); - tmph = QMAX( tmph, c->height() + linespace ); + tmph = QMAX( tmph, c->height() ); } minw = QMAX( minw, tminw ); tminw = marg + ww; lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); @@ -5779,9 +5526,9 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, if ( i < len - 2 || c->c != ' ' ) lastBreak = i; } else { tminw += ww; - int belowBaseLine = QMAX( tmph - tmpBaseLine, c->height() + linespace - c->ascent() ); + int belowBaseLine = QMAX( tmph - tmpBaseLine, c->height()- c->ascent() ); tmpBaseLine = QMAX( tmpBaseLine, c->ascent() ); tmph = tmpBaseLine + belowBaseLine; } @@ -5789,10 +5536,12 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, x += ww; wused = QMAX( wused, x ); } - // ### hack. The last char in the paragraph is always invisible, and somehow sometimes has a wrong format. It changes between - // layouting and printing. This corrects some layouting errors in BiDi mode due to this. + // ### hack. The last char in the paragraph is always invisible, + // ### and somehow sometimes has a wrong format. It changes + // ### between // layouting and printing. This corrects some + // ### layouting errors in BiDi mode due to this. if ( len > 1 && !c->isAnchor() ) { c->format()->removeRef(); c->setFormat( string->at( len - 2 ).format() ); c->format()->addRef(); @@ -5802,28 +5551,38 @@ int QTextFormatterBreakWords::format( QTextDocument *doc, QTextParag *parag, lineStart->baseLine = QMAX( lineStart->baseLine, tmpBaseLine ); h = QMAX( h, tmph ); lineStart->h = h; // last line in a paragraph is not justified - if ( align == Qt3::AlignJustify ) + if ( align == Qt3::AlignJustify || lastChr == QChar_linesep ) align = Qt3::AlignAuto; DO_FLOW( lineStart ); - lineStart = formatLine( parag, string, lineStart, firstChar, c, align, w - x ); + lineStart = formatLine( parag, string, lineStart, firstChar, c, align, SPACE(w - x) ); delete lineStart; } minw = QMAX( minw, tminw ); + if ( doc ) + minw += doc->rightMargin(); int m = parag->bottomMargin(); - if ( parag->next() && doc && !doc->addMargins() ) - m = QMAX( m, parag->next()->topMargin() ); - parag->setFullWidth( fullWidth ); - if ( parag->next() && parag->next()->isLineBreak() ) + if ( !parag->next() ) m = 0; - y += h + m; + else + m = QMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); + y += QMAX( h, linespacing ) + m; wused += rm; if ( !wrapEnabled || wrapAtColumn() != -1 ) minw = QMAX(minw, wused); + + // This is the case where we are breaking wherever we darn well please + // in cases like that, the minw should not be the length of the entire + // word, because we necessarily want to show the word on the whole line. + // example: word wrap in iconview + if ( allowBreakInWords() && minw > wused ) + minw = wused; + thisminw = minw; thiswused = wused; return y; } @@ -5836,9 +5595,9 @@ QTextIndent::QTextIndent() // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ QTextFormatCollection::QTextFormatCollection() - : cKey( 307 ), sheet( 0 ) + : cKey( 307 ) { defFormat = new QTextFormat( QApplication::font(), QApplication::palette().color( QPalette::Active, QColorGroup::Text ) ); lastFormat = cres = 0; @@ -5854,40 +5613,28 @@ QTextFormatCollection::~QTextFormatCollection() QTextFormat *QTextFormatCollection::format( QTextFormat *f ) { if ( f->parent() == this || f == defFormat ) { -#ifdef DEBUG_COLLECTION - qDebug( "need '%s', best case!", f->key().latin1() ); -#endif lastFormat = f; lastFormat->addRef(); return lastFormat; } if ( f == lastFormat || ( lastFormat && f->key() == lastFormat->key() ) ) { -#ifdef DEBUG_COLLECTION - qDebug( "need '%s', good case!", f->key().latin1() ); -#endif lastFormat->addRef(); return lastFormat; } QTextFormat *fm = cKey.find( f->key() ); if ( fm ) { -#ifdef DEBUG_COLLECTION - qDebug( "need '%s', normal case!", f->key().latin1() ); -#endif lastFormat = fm; lastFormat->addRef(); return lastFormat; } if ( f->key() == defFormat->key() ) return defFormat; -#ifdef DEBUG_COLLECTION - qDebug( "need '%s', worst case!", f->key().latin1() ); -#endif lastFormat = createFormat( *f ); lastFormat->collection = this; cKey.insert( lastFormat->key(), lastFormat ); return lastFormat; @@ -5895,11 +5642,8 @@ QTextFormat *QTextFormatCollection::format( QTextFormat *f ) QTextFormat *QTextFormatCollection::format( QTextFormat *of, QTextFormat *nf, int flags ) { if ( cres && kof == of->key() && knf == nf->key() && cflags == flags ) { -#ifdef DEBUG_COLLECTION - qDebug( "mix of '%s' and '%s, best case!", of->key().latin1(), nf->key().latin1() ); -#endif cres->addRef(); return cres; } @@ -5912,8 +5656,10 @@ QTextFormat *QTextFormatCollection::format( QTextFormat *of, QTextFormat *nf, in if ( flags & QTextFormat::Italic ) cres->fn.setItalic( nf->fn.italic() ); if ( flags & QTextFormat::Underline ) cres->fn.setUnderline( nf->fn.underline() ); + if ( flags & QTextFormat::StrikeOut ) + cres->fn.setStrikeOut( nf->fn.strikeOut() ); if ( flags & QTextFormat::Family ) cres->fn.setFamily( nf->fn.family() ); if ( flags & QTextFormat::Size ) { if ( of->usePixelSizes ) @@ -5930,17 +5676,11 @@ QTextFormat *QTextFormatCollection::format( QTextFormat *of, QTextFormat *nf, in cres->update(); QTextFormat *fm = cKey.find( cres->key() ); if ( !fm ) { -#ifdef DEBUG_COLLECTION - qDebug( "mix of '%s' and '%s, worst case!", of->key().latin1(), nf->key().latin1() ); -#endif cres->collection = this; cKey.insert( cres->key(), cres ); } else { -#ifdef DEBUG_COLLECTION - qDebug( "mix of '%s' and '%s, good case!", of->key().latin1(), nf->key().latin1() ); -#endif delete cres; cres = fm; cres->addRef(); } @@ -5950,11 +5690,8 @@ QTextFormat *QTextFormatCollection::format( QTextFormat *of, QTextFormat *nf, in QTextFormat *QTextFormatCollection::format( const QFont &f, const QColor &c ) { if ( cachedFormat && cfont == f && ccol == c ) { -#ifdef DEBUG_COLLECTION - qDebug( "format of font and col '%s' - best case", cachedFormat->key().latin1() ); -#endif cachedFormat->addRef(); return cachedFormat; } @@ -5963,11 +5700,8 @@ QTextFormat *QTextFormatCollection::format( const QFont &f, const QColor &c ) cfont = f; ccol = c; if ( cachedFormat ) { -#ifdef DEBUG_COLLECTION - qDebug( "format of font and col '%s' - good case", cachedFormat->key().latin1() ); -#endif cachedFormat->addRef(); return cachedFormat; } @@ -5978,11 +5712,8 @@ QTextFormat *QTextFormatCollection::format( const QFont &f, const QColor &c ) cachedFormat->collection = this; cKey.insert( cachedFormat->key(), cachedFormat ); if ( cachedFormat->key() != key ) qWarning("ASSERT: keys for format not identical: '%s '%s'", cachedFormat->key().latin1(), key.latin1() ); -#ifdef DEBUG_COLLECTION - qDebug( "format of font and col '%s' - worst case", cachedFormat->key().latin1() ); -#endif return cachedFormat; } void QTextFormatCollection::remove( QTextFormat *f ) @@ -5995,98 +5726,57 @@ void QTextFormatCollection::remove( QTextFormat *f ) cachedFormat = 0; cKey.remove( f->key() ); } -void QTextFormatCollection::debug() -{ -#ifdef DEBUG_COLLECTION - qDebug( "------------ QTextFormatCollection: debug --------------- BEGIN" ); - QDictIterator<QTextFormat> it( cKey ); - for ( ; it.current(); ++it ) { - qDebug( "format '%s' (%p): refcount: %d", it.current()->key().latin1(), - it.current(), it.current()->ref ); - } - qDebug( "------------ QTextFormatCollection: debug --------------- END" ); -#endif -} - -void QTextFormatCollection::updateStyles() -{ - QDictIterator<QTextFormat> it( cKey ); - QTextFormat *f; - while ( ( f = it.current() ) ) { - ++it; - f->updateStyle(); - } - updateKeys(); -} - -void QTextFormatCollection::updateFontSizes( int base, bool usePixels ) -{ - QDictIterator<QTextFormat> it( cKey ); - QTextFormat *f; - while ( ( f = it.current() ) ) { - ++it; - f->stdSize = base; - f->usePixelSizes = usePixels; - if ( usePixels ) - f->fn.setPixelSize( f->stdSize ); - else - f->fn.setPointSize( f->stdSize ); - styleSheet()->scaleFont( f->fn, f->logicalFontSize ); - f->update(); - } - f = defFormat; - f->stdSize = base; - f->usePixelSizes = usePixels; - if ( usePixels ) - f->fn.setPixelSize( f->stdSize ); - else - f->fn.setPointSize( f->stdSize ); - styleSheet()->scaleFont( f->fn, f->logicalFontSize ); - f->update(); - updateKeys(); -} +#define UPDATE( up, lo, rest ) \ + if ( font.lo##rest() != defFormat->fn.lo##rest() && fm->fn.lo##rest() == defFormat->fn.lo##rest() ) \ + fm->fn.set##up##rest( font.lo##rest() ) -void QTextFormatCollection::updateFontAttributes( const QFont &f, const QFont &old ) +void QTextFormatCollection::updateDefaultFormat( const QFont &font, const QColor &color, QStyleSheet *sheet ) { QDictIterator<QTextFormat> it( cKey ); QTextFormat *fm; + bool usePixels = font.pointSize() == -1; + bool changeSize = usePixels ? font.pixelSize() != defFormat->fn.pixelSize() : + font.pointSize() != defFormat->fn.pointSize(); + int base = usePixels ? font.pixelSize() : font.pointSize(); while ( ( fm = it.current() ) ) { ++it; - if ( fm->fn.family() == old.family() && - fm->fn.weight() == old.weight() && - fm->fn.italic() == old.italic() && - fm->fn.underline() == old.underline() ) { - fm->fn.setFamily( f.family() ); - fm->fn.setWeight( f.weight() ); - fm->fn.setItalic( f.italic() ); - fm->fn.setUnderline( f.underline() ); - fm->update(); - } - } - fm = defFormat; - if ( fm->fn.family() == old.family() && - fm->fn.weight() == old.weight() && - fm->fn.italic() == old.italic() && - fm->fn.underline() == old.underline() ) { - fm->fn.setFamily( f.family() ); - fm->fn.setWeight( f.weight() ); - fm->fn.setItalic( f.italic() ); - fm->fn.setUnderline( f.underline() ); + UPDATE( F, f, amily ); + UPDATE( W, w, eight ); + UPDATE( B, b, old ); + UPDATE( I, i, talic ); + UPDATE( U, u, nderline ); + if ( changeSize ) { + fm->stdSize = base; + fm->usePixelSizes = usePixels; + if ( usePixels ) + fm->fn.setPixelSize( fm->stdSize ); + else + fm->fn.setPointSize( fm->stdSize ); + sheet->scaleFont( fm->fn, fm->logicalFontSize ); + } + if ( color.isValid() && color != defFormat->col && fm->col == defFormat->col ) + fm->col = color; fm->update(); } + + defFormat->fn = font; + defFormat->col = color; + defFormat->update(); + defFormat->stdSize = base; + defFormat->usePixelSizes = usePixels; + updateKeys(); } - // the keys in cKey have changed, rebuild the hashtable void QTextFormatCollection::updateKeys() { if ( cKey.isEmpty() ) return; cKey.setAutoDelete( FALSE ); - QTextFormat** formats = new QTextFormat*[ cKey.count() + 1]; + QTextFormat** formats = new QTextFormat*[ cKey.count() + 1 ]; QTextFormat **f = formats; QDictIterator<QTextFormat> it( cKey ); while ( ( *f = it.current() ) ) { ++it; @@ -6095,8 +5785,9 @@ void QTextFormatCollection::updateKeys() cKey.clear(); for ( f = formats; *f; f++ ) cKey.insert( (*f)->key(), *f ); cKey.setAutoDelete( TRUE ); + delete [] formats; } @@ -6141,8 +5832,16 @@ void QTextFormat::setUnderline( bool b ) fn.setUnderline( b ); update(); } +void QTextFormat::setStrikeOut( bool b ) +{ + if ( b == fn.strikeOut() ) + return; + fn.setStrikeOut( b ); + update(); +} + void QTextFormat::setFamily( const QString &f ) { if ( f == fn.family() ) return; @@ -6174,194 +5873,185 @@ void QTextFormat::setColor( const QColor &c ) col = c; update(); } -static int makeLogicFontSize( int s ) -{ - int defSize = QApplication::font().pointSize(); - if ( s < defSize - 4 ) - return 1; - if ( s < defSize ) - return 2; - if ( s < defSize + 4 ) - return 3; - if ( s < defSize + 8 ) - return 4; - if ( s < defSize + 12 ) - return 5; - if (s < defSize + 16 ) - return 6; - return 7; -} - -static QTextFormat *defaultFormat = 0; - -QString QTextFormat::makeFormatChangeTags( QTextFormat *f, const QString& oldAnchorHref, const QString& anchorHref ) const +QString QTextFormat::makeFormatChangeTags( QTextFormat* defaultFormat, QTextFormat *f, + const QString& oldAnchorHref, const QString& anchorHref ) const { - if ( !defaultFormat ) // #### wrong, use the document's default format instead - defaultFormat = new QTextFormat( QApplication::font(), - QApplication::palette().color( QPalette::Active, QColorGroup::Text ) ); - QString tag; - if ( f ) { - if ( f->font() != defaultFormat->font() ) { - if ( f->font().family() != defaultFormat->font().family() - || f->font().pointSize() != defaultFormat->font().pointSize() - || f->color().rgb() != defaultFormat->color().rgb() ) - tag += "</font>"; - if ( f->font().underline() && f->font().underline() != defaultFormat->font().underline() ) - tag += "</u>"; - if ( f->font().italic() && f->font().italic() != defaultFormat->font().italic() ) - tag += "</i>"; - if ( f->font().bold() && f->font().bold() != defaultFormat->font().bold() ) - tag += "</b>"; - } - if ( !oldAnchorHref.isEmpty() ) - tag += "</a>"; - } + if ( f ) + tag += f->makeFormatEndTags( defaultFormat, oldAnchorHref ); if ( !anchorHref.isEmpty() ) tag += "<a href=\"" + anchorHref + "\">"; - if ( font() != defaultFormat->font() ) { - if ( font().bold() && font().bold() != defaultFormat->font().bold() ) - tag += "<b>"; - if ( font().italic() && font().italic() != defaultFormat->font().italic() ) - tag += "<i>"; - if ( font().underline() && font().underline() != defaultFormat->font().underline() ) - tag += "<u>"; - } if ( font() != defaultFormat->font() + || vAlign() != defaultFormat->vAlign() || color().rgb() != defaultFormat->color().rgb() ) { - QString f; + QString s; if ( font().family() != defaultFormat->font().family() ) - f +=" face=\"" + fn.family() + "\""; - if ( font().pointSize() != defaultFormat->font().pointSize() ) { - f +=" size=\"" + QString::number( makeLogicFontSize( fn.pointSize() ) ) + "\""; - f +=" style=\"font-size:" + QString::number( fn.pointSize() ) + "pt\""; + s += QString(!!s?";":"") + "font-family:" + fn.family(); + if ( font().italic() && font().italic() != defaultFormat->font().italic() ) + s += QString(!!s?";":"") + "font-style:" + (font().italic() ? "italic" : "normal"); + if ( font().pointSize() != defaultFormat->font().pointSize() ) + s += QString(!!s?";":"") + "font-size:" + QString::number( fn.pointSize() ) + "pt"; + if ( font().weight() != defaultFormat->font().weight() ) + s += QString(!!s?";":"") + "font-weight:" + QString::number( fn.weight() * 8 ); + if ( font().underline() != defaultFormat->font().underline() ) + s += QString(!!s?";":"") + "text-decoration:" + ( font().underline() ? "underline" : "none"); + if ( vAlign() != defaultFormat->vAlign() ) { + s += QString(!!s?";":"") + "vertical-align:"; + if ( vAlign() == QTextFormat::AlignSuperScript ) + s += "super"; + else if ( vAlign() == QTextFormat::AlignSubScript ) + s += "sub"; + else + s += "normal"; } if ( color().rgb() != defaultFormat->color().rgb() ) - f +=" color=\"" + col.name() + "\""; - if ( !f.isEmpty() ) - tag += "<font" + f + ">"; + s += QString(!!s?";":"") + "color:" + col.name(); + if ( !s.isEmpty() ) + tag += "<span style=\"" + s + "\">"; } return tag; } -QString QTextFormat::makeFormatEndTags( const QString& anchorHref ) const +QString QTextFormat::makeFormatEndTags( QTextFormat* defaultFormat, const QString& anchorHref ) const { - if ( !defaultFormat ) - defaultFormat = new QTextFormat( QApplication::font(), - QApplication::palette().color( QPalette::Active, QColorGroup::Text ) ); - QString tag; - if ( font() != defaultFormat->font() ) { - if ( font().family() != defaultFormat->font().family() - || font().pointSize() != defaultFormat->font().pointSize() - || color().rgb() != defaultFormat->color().rgb() ) - tag += "</font>"; - if ( font().underline() && font().underline() != defaultFormat->font().underline() ) - tag += "</u>"; - if ( font().italic() && font().italic() != defaultFormat->font().italic() ) - tag += "</i>"; - if ( font().bold() && font().bold() != defaultFormat->font().bold() ) - tag += "</b>"; - } + if ( font().family() != defaultFormat->font().family() + || font().pointSize() != defaultFormat->font().pointSize() + || font().weight() != defaultFormat->font().weight() + || font().italic() != defaultFormat->font().italic() + || font().underline() != defaultFormat->font().underline() + || font().strikeOut() != defaultFormat->font().strikeOut() + || vAlign() != defaultFormat->vAlign() + || color().rgb() != defaultFormat->color().rgb() ) + tag += "</span>"; if ( !anchorHref.isEmpty() ) tag += "</a>"; return tag; } -QTextFormat QTextFormat::makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr ) const +QTextFormat QTextFormat::makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr, double scaleFontsFactor ) const { QTextFormat format(*this); - if ( style ) { - format.style = style->name(); - if ( style->name() == "font") { - if ( attr.contains("color") ) { - QString s = attr["color"]; - if ( !s.isEmpty() ) { - format.col.setNamedColor( s ); - format.linkColor = FALSE; - } - } - if ( attr.contains("size") ) { - QString a = attr["size"]; - int n = a.toInt(); - if ( a[0] == '+' || a[0] == '-' ) - n += format.logicalFontSize; - format.logicalFontSize = n; - if ( format.usePixelSizes ) - format.fn.setPixelSize( format.stdSize ); - else - format.fn.setPointSize( format.stdSize ); - style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); - } - if ( attr.contains("style" ) ) { - QString a = attr["style"]; - if ( a.startsWith( "font-size:" ) ) { - QString s = a.mid( a.find( ':' ) + 1 ); - int n = s.left( s.length() - 2 ).toInt(); - format.logicalFontSize = 0; - if ( format.usePixelSizes ) - format.fn.setPixelSize( n ); - else - format.fn.setPointSize( n ); - } - } - if ( attr.contains("face") ) { - QString a = attr["face"]; - if ( a.contains(',') ) - a = a.left( a.find(',') ); - format.fn.setFamily( a ); - } - } else { - if ( !style->isAnchor() && style->color().isValid() ) { - // the style is not an anchor and defines a color. - // It might be used inside an anchor and it should - // override the link color. + if (!style ) + return format; + + if ( !style->isAnchor() && style->color().isValid() ) { + // the style is not an anchor and defines a color. + // It might be used inside an anchor and it should + // override the link color. + format.linkColor = FALSE; + } + switch ( style->verticalAlignment() ) { + case QStyleSheetItem::VAlignBaseline: + format.setVAlign( QTextFormat::AlignNormal ); + break; + case QStyleSheetItem::VAlignSuper: + format.setVAlign( QTextFormat::AlignSuperScript ); + break; + case QStyleSheetItem::VAlignSub: + format.setVAlign( QTextFormat::AlignSubScript ); + break; + } + + if ( style->fontWeight() != QStyleSheetItem::Undefined ) + format.fn.setWeight( style->fontWeight() ); + if ( style->fontSize() != QStyleSheetItem::Undefined ) { + format.fn.setPointSize( style->fontSize() ); + } else if ( style->logicalFontSize() != QStyleSheetItem::Undefined ) { + format.logicalFontSize = style->logicalFontSize(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } else if ( style->logicalFontSizeStep() ) { + format.logicalFontSize += style->logicalFontSizeStep(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + if ( !style->fontFamily().isEmpty() ) + format.fn.setFamily( style->fontFamily() ); + if ( style->color().isValid() ) + format.col = style->color(); + if ( style->definesFontItalic() ) + format.fn.setItalic( style->fontItalic() ); + if ( style->definesFontUnderline() ) + format.fn.setUnderline( style->fontUnderline() ); + if ( style->definesFontStrikeOut() ) + format.fn.setStrikeOut( style->fontStrikeOut() ); + + + if ( style->name() == "font") { + if ( attr.contains("color") ) { + QString s = attr["color"]; + if ( !s.isEmpty() ) { + format.col.setNamedColor( s ); format.linkColor = FALSE; } - switch ( style->verticalAlignment() ) { - case QStyleSheetItem::VAlignBaseline: - format.setVAlign( QTextFormat::AlignNormal ); - break; - case QStyleSheetItem::VAlignSuper: - format.setVAlign( QTextFormat::AlignSuperScript ); - break; - case QStyleSheetItem::VAlignSub: - format.setVAlign( QTextFormat::AlignSubScript ); - break; - } - - if ( style->fontWeight() != QStyleSheetItem::Undefined ) - format.fn.setWeight( style->fontWeight() ); - if ( style->fontSize() != QStyleSheetItem::Undefined ) { - format.fn.setPointSize( style->fontSize() ); - } else if ( style->logicalFontSize() != QStyleSheetItem::Undefined ) { - format.logicalFontSize = style->logicalFontSize(); - if ( format.usePixelSizes ) - format.fn.setPixelSize( format.stdSize ); - else - format.fn.setPointSize( format.stdSize ); - style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); - } else if ( style->logicalFontSizeStep() ) { - format.logicalFontSize += style->logicalFontSizeStep(); - if ( format.usePixelSizes ) - format.fn.setPixelSize( format.stdSize ); + } + if ( attr.contains("face") ) { + QString a = attr["face"]; + QString family = QTextDocument::section( a, ",", 0, 0 ); + if ( !!family ) + format.fn.setFamily( family ); + } + if ( attr.contains("size") ) { + QString a = attr["size"]; + int n = a.toInt(); + if ( a[0] == '+' || a[0] == '-' ) + n += format.logicalFontSize; + format.logicalFontSize = n; + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + } + if ( attr.contains("style" ) ) { + QString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + QString style = QTextDocument::section( a, ";", s, s ); + if ( style.startsWith("font-size:" ) && QTextDocument::endsWith(style, "pt") ) { + format.logicalFontSize = 0; + format.setPointSize( int( scaleFontsFactor * style.mid( 10, style.length() - 12 ).toInt() ) ); + } if ( style.startsWith("font-style:" ) ) { + QString s = style.mid( 11 ).stripWhiteSpace(); + if ( s == "normal" ) + format.fn.setItalic( FALSE ); + else if ( s == "italic" || s == "oblique" ) + format.fn.setItalic( TRUE ); + } else if ( style.startsWith("font-weight:" ) ) { + QString s = style.mid( 12 ); + bool ok = TRUE; + int n = s.toInt( &ok ); + if ( ok ) + format.fn.setWeight( n/8 ); + } else if ( style.startsWith("font-family:" ) ) { + format.fn.setFamily( QTextDocument::section(style.mid(12),",",0,0).stripWhiteSpace() ); + } else if ( style.startsWith("text-decoration:" ) ) { + QString s = style.mid( 16 ).stripWhiteSpace(); + format.fn.setUnderline( s == "underline" ); + } else if ( style.startsWith("vertical-align:" ) ) { + QString s = style.mid( 15 ).stripWhiteSpace(); + if ( s == "sub" ) + format.setVAlign( QTextFormat::AlignSubScript ); + else if ( s == "super" ) + format.setVAlign( QTextFormat::AlignSuperScript ); else - format.fn.setPointSize( format.stdSize ); - style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + format.setVAlign( QTextFormat::AlignNormal ); + } else if ( style.startsWith("color:" ) ) { + format.col.setNamedColor( style.mid(6) ); + format.linkColor = FALSE; } - if ( !style->fontFamily().isEmpty() ) - format.fn.setFamily( style->fontFamily() ); - if ( style->color().isValid() ) - format.col = style->color(); - if ( style->definesFontItalic() ) - format.fn.setItalic( style->fontItalic() ); - if ( style->definesFontUnderline() ) - format.fn.setUnderline( style->fontUnderline() ); } } format.update(); @@ -6380,12 +6070,8 @@ static QMap<QString, QPixmapInt> *pixmap_map = 0; QTextImage::QTextImage( QTextDocument *p, const QMap<QString, QString> &attr, const QString& context, QMimeSourceFactory &factory ) : QTextCustomItem( p ) { -#if defined(PARSER_DEBUG) - qDebug( debug_indent + "new QTextImage (pappi: %p)", p ); -#endif - width = height = 0; if ( attr.contains("width") ) width = attr["width"].toInt(); if ( attr.contains("height") ) @@ -6396,12 +6082,8 @@ QTextImage::QTextImage( QTextDocument *p, const QMap<QString, QString> &attr, co if (!imageName) imageName = attr["source"]; -#if defined(PARSER_DEBUG) - qDebug( debug_indent + " .." + imageName ); -#endif - if ( !imageName.isEmpty() ) { imgId = QString( "%1,%2,%3,%4" ).arg( imageName ).arg( width ).arg( height ).arg( (ulong)&factory ); if ( !pixmap_map ) pixmap_map = new QMap<QString, QPixmapInt>; @@ -6486,8 +6168,9 @@ QTextImage::~QTextImage() pixmap_map = 0; } } } + delete reg; } QString QTextImage::richText() const { @@ -6644,9 +6327,9 @@ static bool qt_is_cell_in_use( QPtrList<QTextTableCell>& cells, int row, int col return FALSE; } QTextCustomItem* QTextDocument::parseTable( const QMap<QString, QString> &attr, const QTextFormat &fmt, - const QChar* doc, int length, int& pos, QTextParag *curpar ) + const QChar* doc, int length, int& pos, QTextParagraph *curpar ) { QTextTable* table = new QTextTable( this, attr ); int row = -1; @@ -6664,11 +6347,8 @@ QTextCustomItem* QTextDocument::parseTable( const QMap<QString, QString> &attr, if (hasPrefix(doc, length, pos, QChar('<')) ){ if (hasPrefix(doc, length, pos+1, QChar('/'))) { tagname = parseCloseTag( doc, length, pos ); if ( tagname == "table" ) { -#if defined(PARSER_DEBUG) - debug_indent.remove( debug_indent.length() - 3, 2 ); -#endif return table; } } else { QMap<QString, QString> attr2; @@ -6723,12 +6403,12 @@ QTextCustomItem* QTextDocument::parseTable( const QMap<QString, QString> &attr, } end++; } QTextTableCell* cell = new QTextTableCell( table, row, col, - attr2, s, fmt.makeTextFormat( s, attr2 ), - contxt, *factory_, sheet_, - QString( doc, length).mid( pos, end - pos ) ); - cell->richText()->parParag = curpar; + attr2, s, fmt.makeTextFormat( s, attr2, scaleFontsFactor ), + contxt, *factory_, sheet_, + QString( doc, length).mid( pos, end - pos ) ); + cell->richText()->parentPar = curpar; if ( cell->colspan() > 1 || cell->rowspan() > 1 ) multicells.append( cell ); col += cell->colspan()-1; pos = end; @@ -6739,11 +6419,8 @@ QTextCustomItem* QTextDocument::parseTable( const QMap<QString, QString> &attr, } else { ++pos; } } -#if defined(PARSER_DEBUG) - debug_indent.remove( debug_indent.length() - 3, 2 ); -#endif return table; } bool QTextDocument::eatSpace(const QChar* doc, int length, int& pos, bool includeNbsp ) @@ -7130,21 +6807,12 @@ QChar QTextDocument::parseChar(const QChar* doc, int length, int& pos, QStyleShe return QChar::null; if ( c.isSpace() && c != QChar::nbsp ) { if ( wsm == QStyleSheetItem::WhiteSpacePre ) { - if ( c == ' ' ) - return QChar::nbsp; - else - return c; - } else if ( wsm == QStyleSheetItem_WhiteSpaceNoCompression ) { - return c; - } else if ( wsm == QStyleSheetItem_WhiteSpaceNormalWithNewlines ) { if ( c == '\n' ) + return QChar_linesep; + else return c; - while ( pos< length && - doc[pos].isSpace() && doc[pos] != QChar::nbsp && doc[pos] != '\n' ) - pos++; - return ' '; } else { // non-pre mode: collapse whitespace except nbsp while ( pos< length && doc[pos].isSpace() && doc[pos] != QChar::nbsp ) pos++; @@ -7354,13 +7022,8 @@ void QTextCustomItem::pageBreak( int /*y*/ , QTextFlow* /*flow*/ ) QTextTable::QTextTable( QTextDocument *p, const QMap<QString, QString> & attr ) : QTextCustomItem( p ) { cells.setAutoDelete( FALSE ); -#if defined(PARSER_DEBUG) - debug_indent += "\t"; - qDebug( debug_indent + "new QTextTable (%p)", this ); - debug_indent += "\t"; -#endif cellspacing = 2; if ( attr.contains("cellspacing") ) cellspacing = attr["cellspacing"].toInt(); cellpadding = 1; @@ -7439,12 +7102,12 @@ QString QTextTable::richText() const s += "<tr>"; lastRow = cell->row(); needEnd = TRUE; } - s += "<td "; + s += "<td"; it = cell->attributes.begin(); for ( ; it != cell->attributes.end(); ++it ) - s += it.key() + "=" + *it + " "; + s += " " + it.key() + "=" + *it; s += ">"; s += cell->richText()->richText(); s += "</td>"; } @@ -7575,14 +7238,8 @@ void QTextTable::draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch, qDrawShadePanel( p, r, cg, FALSE, border ); } } -#if defined(DEBUG_TABLE_RENDERING) - p->save(); - p->setPen( Qt::red ); - p->drawRect( x, y, width, height ); - p->restore(); -#endif } int QTextTable::minimumWidth() const { @@ -7642,18 +7299,18 @@ void QTextTable::addCell( QTextTableCell* cell ) layout->addMultiCell( cell, cell->row(), cell->row() + cell->rowspan()-1, cell->column(), cell->column() + cell->colspan()-1 ); } -bool QTextTable::enter( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, bool atEnd ) +bool QTextTable::enter( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd ) { currCell.remove( c ); if ( !atEnd ) return next( c, doc, parag, idx, ox, oy ); currCell.insert( c, cells.count() ); return prev( c, doc, parag, idx, ox, oy ); } -bool QTextTable::enterAt( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, const QPoint &pos ) +bool QTextTable::enterAt( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint &pos ) { currCell.remove( c ); int lastCell = -1; int lastY = -1; @@ -7691,16 +7348,16 @@ bool QTextTable::enterAt( QTextCursor *c, QTextDocument *&doc, QTextParag *¶ QTextTableCell *cell = cells.at( *currCell.find( c ) ); if ( !cell ) return FALSE; doc = cell->richText(); - parag = doc->firstParag(); + parag = doc->firstParagraph(); idx = 0; ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; return TRUE; } -bool QTextTable::next( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextTable::next( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) { int cc = -1; if ( currCell.find( c ) != currCell.end() ) cc = *currCell.find( c ); @@ -7724,16 +7381,16 @@ bool QTextTable::next( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, QTextTableCell *cell = cells.at( *currCell.find( c ) ); if ( !cell ) return FALSE; doc = cell->richText(); - parag = doc->firstParag(); + parag = doc->firstParagraph(); idx = 0; ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; return TRUE; } -bool QTextTable::prev( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextTable::prev( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) { int cc = -1; if ( currCell.find( c ) != currCell.end() ) cc = *currCell.find( c ); @@ -7757,16 +7414,16 @@ bool QTextTable::prev( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, QTextTableCell *cell = cells.at( *currCell.find( c ) ); if ( !cell ) return FALSE; doc = cell->richText(); - parag = doc->firstParag(); + parag = doc->lastParagraph(); idx = parag->length() - 1; ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; return TRUE; } -bool QTextTable::down( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextTable::down( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) { if ( currCell.find( c ) == currCell.end() ) return FALSE; QTextTableCell *cell = cells.at( *currCell.find( c ) ); @@ -7795,16 +7452,16 @@ bool QTextTable::down( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, } doc = cell->richText(); if ( !cell ) return FALSE; - parag = doc->firstParag(); + parag = doc->firstParagraph(); idx = 0; ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; return TRUE; } -bool QTextTable::up( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextTable::up( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ) { if ( currCell.find( c ) == currCell.end() ) return FALSE; QTextTableCell *cell = cells.at( *currCell.find( c ) ); @@ -7833,9 +7490,9 @@ bool QTextTable::up( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, in } doc = cell->richText(); if ( !cell ) return FALSE; - parag = doc->lastParag(); + parag = doc->lastParagraph(); idx = parag->length() - 1; ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; return TRUE; @@ -7848,12 +7505,8 @@ QTextTableCell::QTextTableCell( QTextTable* table, const QTextFormat& /*fmt*/, const QString& context, QMimeSourceFactory &factory, QStyleSheet *sheet, const QString& doc) { -#if defined(PARSER_DEBUG) - qDebug( debug_indent + "new QTextTableCell1 (pappi: %p)", table ); - qDebug( debug_indent + doc ); -#endif cached_width = -1; cached_sizehint = -1; maxw = QWIDGETSIZE_MAX; @@ -7887,9 +7540,10 @@ QTextTableCell::QTextTableCell( QTextTable* table, richtext->setFormatter( table->parent->formatter() ); richtext->setUseFormatCollection( table->parent->useFormatCollection() ); richtext->setMimeSourceFactory( &factory ); richtext->setStyleSheet( sheet ); - richtext->setDefaultFont( table->parent->formatCollection()->defaultFormat()->font() ); + richtext->setDefaultFormat( table->parent->formatCollection()->defaultFormat()->font(), + table->parent->formatCollection()->defaultFormat()->color() ); richtext->setRichText( doc, context ); rowspan_ = 1; colspan_ = 1; if ( attr.contains("colspan") ) @@ -7923,36 +7577,8 @@ QTextTableCell::QTextTableCell( QTextTable* table, parent->addCell( this ); } -QTextTableCell::QTextTableCell( QTextTable* table, int row, int column ) -{ -#if defined(PARSER_DEBUG) - qDebug( debug_indent + "new QTextTableCell2( pappi: %p", table ); -#endif - maxw = QWIDGETSIZE_MAX; - minw = 0; - cached_width = -1; - cached_sizehint = -1; - - parent = table; - row_ = row; - col_ = column; - stretch_ = 0; - richtext = new QTextDocument( table->parent ); - richtext->setTableCell( this ); - richtext->setFormatter( table->parent->formatter() ); - richtext->setUseFormatCollection( table->parent->useFormatCollection() ); - richtext->setDefaultFont( table->parent->formatCollection()->defaultFormat()->font() ); - richtext->setRichText( "<html></html>", QString::null ); - rowspan_ = 1; - colspan_ = 1; - background = 0; - hasFixedWidth = FALSE; - parent->addCell( this ); -} - - QTextTableCell::~QTextTableCell() { delete background; background = 0; @@ -8026,9 +7652,9 @@ int QTextTableCell::heightForWidth( int w ) const } void QTextTableCell::adjustToPainter( QPainter* p ) { - QTextParag *parag = richtext->firstParag(); + QTextParagraph *parag = richtext->firstParagraph(); while ( parag ) { parag->adjustToPainter( p ); parag = parag->next(); } @@ -8070,16 +7696,149 @@ void QTextTableCell::draw( QPainter* p, int x, int y, int cx, int cy, int cw, in p->translate( horizontalAlignmentOffset(), verticalAlignmentOffset() ); QRegion r; - QTextCursor *c = 0; - if ( richtext->parent()->tmpCursor ) - c = richtext->parent()->tmpCursor; if ( cx >= 0 && cy >= 0 ) richtext->draw( p, cx - ( x + horizontalAlignmentOffset() + geom.x() ), cy - ( y + geom.y() + verticalAlignmentOffset() ), - cw, ch, g, FALSE, (c != 0), c ); + cw, ch, g, FALSE, FALSE, 0 ); else - richtext->draw( p, -1, -1, -1, -1, g, FALSE, (c != 0), c ); + richtext->draw( p, -1, -1, -1, -1, g, FALSE, FALSE, 0 ); p->restore(); } + +QString QTextDocument::section( QString str, const QString &sep, int start, int end ) +{ + const QChar *uc = str.unicode(); + if ( !uc ) + return QString(); + QString _sep = sep; + const QChar *uc_sep = _sep.unicode(); + if(!uc_sep) + return QString(); + bool match = FALSE, last_match = TRUE; + + //find start + int n = str.length(), sep_len = _sep.length(); + const QChar *begin = start < 0 ? uc + n : uc; + while(start) { + match = FALSE; + int c = 0; + for(const QChar *tmp = start < 0 ? begin - sep_len : begin; + c < sep_len && tmp < uc + n && tmp >= uc; tmp++, c++) { + if( *tmp != *(uc_sep + c) ) + break; + if(c == sep_len - 1) { + match = TRUE; + break; + } + } + last_match = match; + + if(start < 0) { + if(match) { + begin -= sep_len; + if(!++start) + break; + } else { + if(start == -1 && begin == uc) + break; + begin--; + } + } else { + if(match) { + if(!--start) + break; + begin += sep_len; + } else { + if(start == 1 && begin == uc + n) + break; + begin++; + } + } + if(begin > uc + n || begin < uc) + return QString(); + } + if(match) + begin+=sep_len; + if(begin > uc + n || begin < uc) + return QString(); + + //now find last + match = FALSE; + const QChar *last = end < 0 ? uc + n : uc; + if(end == -1) { + int c = 0; + for(const QChar *tmp = end < 0 ? last - sep_len : last; + c < sep_len && tmp < uc + n && tmp >= uc; tmp++, c++) { + if( *tmp != *(uc_sep + c) ) + break; + if(c == sep_len - 1) { + match = TRUE; + break; + } + } + } else { + end++; + last_match = TRUE; + while(end) { + match = FALSE; + int c = 0; + for(const QChar *tmp = end < 0 ? last - sep_len : last; + c < sep_len && tmp < uc + n && tmp >= uc; tmp++, c++) { + if( *tmp != *(uc_sep + c) ) + break; + if(c == sep_len - 1) { + match = TRUE; + break; + } + } + last_match = match; + + if(end < 0) { + if(match) { + if(!++end) + break; + last -= sep_len; + } else { + last--; + } + } else { + if(match) { + last += sep_len; + if(!--end) + break; + } else { + last++; + } + } + if(last >= uc + n) { + last = uc + n; + break; + } else if(last < uc) { + return QString(); + } + } + } + if(match) + last -= sep_len; + if(last < uc || last > uc + n || begin >= last) + return QString(); + + //done + return QString(begin, last - begin); +} + +bool QTextDocument::endsWith( QString str, const QString &s) +{ + if ( str.isNull() ) + return s.isNull(); + int pos = str.length() - s.length(); + if ( pos < 0 ) + return FALSE; + for ( uint i = 0; i < s.length(); i++ ) { + if ( str.unicode()[pos+i] != s[(int)i] ) + return FALSE; + } + return TRUE; +} diff --git a/noncore/apps/opie-write/qrichtext_p.cpp b/noncore/apps/opie-write/qrichtext_p.cpp index fb20730..6783e0b 100644 --- a/noncore/apps/opie-write/qrichtext_p.cpp +++ b/noncore/apps/opie-write/qrichtext_p.cpp @@ -55,32 +55,31 @@ bool QTextCustomItem::isNested() const { return FALSE; } int QTextCustomItem::minimumWidth() const { return 0; } QString QTextCustomItem::richText() const { return QString::null; } -bool QTextCustomItem::enter( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, bool atEnd ) +bool QTextCustomItem::enter( QTextCursor *, QTextDocument*&, QTextParagraph *&, int &, int &, int &, bool ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; Q_UNUSED( atEnd ) return TRUE; - + return TRUE; } -bool QTextCustomItem::enterAt( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, const QPoint & ) +bool QTextCustomItem::enterAt( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int &, const QPoint & ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return TRUE; + return TRUE; } -bool QTextCustomItem::next( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextCustomItem::next( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return TRUE; + return TRUE; } -bool QTextCustomItem::prev( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextCustomItem::prev( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return TRUE; + return TRUE; } -bool QTextCustomItem::down( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextCustomItem::down( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return TRUE; + return TRUE; } -bool QTextCustomItem::up( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ) +bool QTextCustomItem::up( QTextCursor *, QTextDocument *&, QTextParagraph *&, int &, int &, int & ) { - doc = doc; parag = parag; idx = idx; ox = ox; oy = oy; return TRUE; + return TRUE; } void QTextFlow::setPageSize( int ps ) { pagesize = ps; } bool QTextFlow::isEmpty() { return leftItems.isEmpty() && rightItems.isEmpty(); } @@ -88,10 +87,10 @@ bool QTextFlow::isEmpty() { return leftItems.isEmpty() && rightItems.isEmpty(); void QTextTableCell::invalidate() { cached_width = -1; cached_sizehint = -1; } void QTextTable::invalidate() { cachewidth = -1; } -QTextParagData::~QTextParagData() {} -void QTextParagData::join( QTextParagData * ) {} +QTextParagraphData::~QTextParagraphData() {} +void QTextParagraphData::join( QTextParagraphData * ) {} QTextFormatter::~QTextFormatter() {} void QTextFormatter::setWrapEnabled( bool b ) { wrapEnabled = b; } void QTextFormatter::setWrapAtColumn( int c ) { wrapColumn = c; } @@ -99,25 +98,53 @@ void QTextFormatter::setWrapAtColumn( int c ) { wrapColumn = c; } int QTextCursor::x() const { - QTextStringChar *c = string->at( idx ); + QTextStringChar *c = para->at( idx ); int curx = c->x; if ( !c->rightToLeft && c->c.isSpace() && idx > 0 && - ( string->alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify ) - curx = string->at( idx - 1 )->x + string->string()->width( idx - 1 ); + !c->lineStart && + ( para->alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify ) + curx = para->at( idx - 1 )->x + para->string()->width( idx - 1 ); if ( c->rightToLeft ) - curx += string->string()->width( idx ); + curx += para->string()->width( idx ); return curx; } int QTextCursor::y() const { int dummy, line; - string->lineStartOfChar( idx, &dummy, &line ); - return string->lineY( line ); + para->lineStartOfChar( idx, &dummy, &line ); + return para->lineY( line ); +} + +int QTextCursor::globalX() const { return totalOffsetX() + para->rect().x() + x(); } +int QTextCursor::globalY() const { return totalOffsetY() + para->rect().y() + y(); } + +QTextDocument *QTextCursor::document() const +{ + return para ? para->document() : 0; +} + +void QTextCursor::gotoPosition( QTextParagraph* p, int index ) +{ + if ( para && p != para ) { + while ( para->document() != p->document() && !indices.isEmpty() ) + pop(); + Q_ASSERT( indices.isEmpty() || para->document() == p->document() ); + } + para = p; + if ( index < 0 || index >= para->length() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "QTextCursor::gotoParagraph Index: %d out of range", index ); +#endif + index = index < 0 ? 0 : para->length() - 1; + } + + tmpIndex = -1; + idx = index; } bool QTextDocument::hasSelection( int id, bool visible ) const { @@ -126,20 +153,20 @@ bool QTextDocument::hasSelection( int id, bool visible ) const ( (QTextDocument*)this )->selectionStartCursor( id ) != ( (QTextDocument*)this )->selectionEndCursor( id ) ) ); } -void QTextDocument::setSelectionStart( int id, QTextCursor *cursor ) +void QTextDocument::setSelectionStart( int id, const QTextCursor &cursor ) { QTextDocumentSelection sel; - sel.startCursor = *cursor; - sel.endCursor = *cursor; + sel.startCursor = cursor; + sel.endCursor = cursor; sel.swapped = FALSE; selections[ id ] = sel; } -QTextParag *QTextDocument::paragAt( int i ) const +QTextParagraph *QTextDocument::paragAt( int i ) const { - QTextParag* p = curParag; + QTextParagraph* p = curParag; if ( !p || p->paragId() > i ) p = fParag; while ( p && p->paragId() != i ) p = p->next(); @@ -152,27 +179,25 @@ QTextFormat::~QTextFormat() { } QTextFormat::QTextFormat() - : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ), - different( NoFlags ) + : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) { ref = 0; usePixelSizes = FALSE; if ( stdSize == -1 ) { stdSize = qApp->font().pixelSize(); usePixelSizes = TRUE; } - + missp = FALSE; ha = AlignNormal; collection = 0; } QTextFormat::QTextFormat( const QStyleSheetItem *style ) - : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ), - different( NoFlags ) + : fm( QFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) { ref = 0; usePixelSizes = FALSE; @@ -180,17 +205,17 @@ QTextFormat::QTextFormat( const QStyleSheetItem *style ) stdSize = qApp->font().pixelSize(); usePixelSizes = TRUE; } - this->style = style->name(); missp = FALSE; ha = AlignNormal; collection = 0; fn = QFont( style->fontFamily(), style->fontSize(), style->fontWeight(), style->fontItalic() ); fn.setUnderline( style->fontUnderline() ); + fn.setStrikeOut( style->fontStrikeOut() ); col = style->color(); fm = QFontMetrics( fn ); leftBearing = fm.minLeftBearing(); rightBearing = fm.minRightBearing(); @@ -201,14 +226,13 @@ QTextFormat::QTextFormat( const QStyleSheetItem *style ) ha = AlignNormal; memset( widths, 0, 256 ); generateKey(); addRef(); - updateStyleFlags(); } QTextFormat::QTextFormat( const QFont &f, const QColor &c, QTextFormatCollection *parent ) : fn( f ), col( c ), fm( QFontMetrics( f ) ), linkColor( TRUE ), - logicalFontSize( 3 ), stdSize( f.pointSize() ), different( NoFlags ) + logicalFontSize( 3 ), stdSize( f.pointSize() ) { ref = 0; usePixelSizes = FALSE; if ( stdSize == -1 ) { @@ -225,9 +249,8 @@ QTextFormat::QTextFormat( const QFont &f, const QColor &c, QTextFormatCollection ha = AlignNormal; memset( widths, 0, 256 ); generateKey(); addRef(); - updateStyleFlags(); } QTextFormat::QTextFormat( const QTextFormat &f ) : fm( f.fm ) @@ -248,10 +271,8 @@ QTextFormat::QTextFormat( const QTextFormat &f ) missp = f.missp; ha = f.ha; k = f.k; linkColor = f.linkColor; - style = f.style; - different = f.different; addRef(); } QTextFormat& QTextFormat::operator=( const QTextFormat &f ) @@ -273,10 +294,8 @@ QTextFormat& QTextFormat::operator=( const QTextFormat &f ) missp = f.missp; ha = f.ha; k = f.k; linkColor = f.linkColor; - style = f.style; - different = f.different; addRef(); return *this; } @@ -289,9 +308,8 @@ void QTextFormat::update() asc = fm.ascent() + (fm.leading()+1)/2; dsc = fm.descent(); memset( widths, 0, 256 ); generateKey(); - updateStyleFlags(); } QPainter* QTextFormat::pntr = 0; @@ -371,54 +389,8 @@ QString QTextFormat::getKey( const QFont &fn, const QColor &col, bool misspelled k += QString::number( (int)a ); return k; } -void QTextFormat::updateStyle() -{ - if ( !collection || !collection->styleSheet() ) - return; - QStyleSheetItem *item = collection->styleSheet()->item( style ); - if ( !item ) - return; - if ( !( different & Color ) && item->color().isValid() ) - col = item->color(); - if ( !( different & Size ) && item->fontSize() != -1 ) - fn.setPointSize( item->fontSize() ); - if ( !( different & Family ) && !item->fontFamily().isEmpty() ) - fn.setFamily( item->fontFamily() ); - if ( !( different & Bold ) && item->fontWeight() != -1 ) - fn.setWeight( item->fontWeight() ); - if ( !( different & Italic ) && item->definesFontItalic() ) - fn.setItalic( item->fontItalic() ); - if ( !( different & Underline ) && item->definesFontUnderline() ) - fn.setUnderline( item->fontUnderline() ); - generateKey(); - update(); - -} - -void QTextFormat::updateStyleFlags() -{ - different = NoFlags; - if ( !collection || !collection->styleSheet() ) - return; - QStyleSheetItem *item = collection->styleSheet()->item( style ); - if ( !item ) - return; - if ( item->color() != col ) - different |= Color; - if ( item->fontSize() != fn.pointSize() ) - different |= Size; - if ( item->fontFamily() != fn.family() ) - different |= Family; - if ( item->fontItalic() != fn.italic() ) - different |= Italic; - if ( item->fontUnderline() != fn.underline() ) - different |= Underline; - if ( item->fontWeight() != fn.weight() ) - different |= Bold; -} - QString QTextString::toString( const QMemArray<QTextStringChar> &data ) { QString s; int l = data.size(); @@ -442,256 +414,181 @@ QString QTextString::toString() const { return toString( data ); } -void QTextParag::setSelection( int id, int start, int end ) +void QTextParagraph::setSelection( int id, int start, int end ) { - QMap<int, QTextParagSelection>::ConstIterator it = selections().find( id ); + QMap<int, QTextParagraphSelection>::ConstIterator it = selections().find( id ); if ( it != mSelections->end() ) { if ( start == ( *it ).start && end == ( *it ).end ) return; } - QTextParagSelection sel; + QTextParagraphSelection sel; sel.start = start; sel.end = end; (*mSelections)[ id ] = sel; setChanged( TRUE, TRUE ); } -void QTextParag::removeSelection( int id ) +void QTextParagraph::removeSelection( int id ) { if ( !hasSelection( id ) ) return; if ( mSelections ) mSelections->remove( id ); setChanged( TRUE, TRUE ); } -int QTextParag::selectionStart( int id ) const +int QTextParagraph::selectionStart( int id ) const { if ( !mSelections ) return -1; - QMap<int, QTextParagSelection>::ConstIterator it = mSelections->find( id ); + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); if ( it == mSelections->end() ) return -1; return ( *it ).start; } -int QTextParag::selectionEnd( int id ) const +int QTextParagraph::selectionEnd( int id ) const { if ( !mSelections ) return -1; - QMap<int, QTextParagSelection>::ConstIterator it = mSelections->find( id ); + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); if ( it == mSelections->end() ) return -1; return ( *it ).end; } -bool QTextParag::hasSelection( int id ) const +bool QTextParagraph::hasSelection( int id ) const { - if ( !mSelections ) - return FALSE; - QMap<int, QTextParagSelection>::ConstIterator it = mSelections->find( id ); - if ( it == mSelections->end() ) - return FALSE; - return ( *it ).start != ( *it ).end || length() == 1; + return mSelections ? mSelections->contains( id ) : FALSE; } -bool QTextParag::fullSelected( int id ) const +bool QTextParagraph::fullSelected( int id ) const { if ( !mSelections ) return FALSE; - QMap<int, QTextParagSelection>::ConstIterator it = mSelections->find( id ); + QMap<int, QTextParagraphSelection>::ConstIterator it = mSelections->find( id ); if ( it == mSelections->end() ) return FALSE; return ( *it ).start == 0 && ( *it ).end == str->length() - 1; } -int QTextParag::lineY( int l ) const +int QTextParagraph::lineY( int l ) const { if ( l > (int)lineStarts.count() - 1 ) { - qWarning( "QTextParag::lineY: line %d out of range!", l ); + qWarning( "QTextParagraph::lineY: line %d out of range!", l ); return 0; } if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while ( l-- > 0 ) ++it; return ( *it )->y; } -int QTextParag::lineBaseLine( int l ) const +int QTextParagraph::lineBaseLine( int l ) const { if ( l > (int)lineStarts.count() - 1 ) { - qWarning( "QTextParag::lineBaseLine: line %d out of range!", l ); + qWarning( "QTextParagraph::lineBaseLine: line %d out of range!", l ); return 10; } if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while ( l-- > 0 ) ++it; return ( *it )->baseLine; } -int QTextParag::lineHeight( int l ) const +int QTextParagraph::lineHeight( int l ) const { if ( l > (int)lineStarts.count() - 1 ) { - qWarning( "QTextParag::lineHeight: line %d out of range!", l ); + qWarning( "QTextParagraph::lineHeight: line %d out of range!", l ); return 15; } if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while ( l-- > 0 ) ++it; return ( *it )->h; } -void QTextParag::lineInfo( int l, int &y, int &h, int &bl ) const +void QTextParagraph::lineInfo( int l, int &y, int &h, int &bl ) const { if ( l > (int)lineStarts.count() - 1 ) { - qWarning( "QTextParag::lineInfo: line %d out of range!", l ); + qWarning( "QTextParagraph::lineInfo: line %d out of range!", l ); qDebug( "%d %d", (int)lineStarts.count() - 1, l ); y = 0; h = 15; bl = 10; return; } if ( !isValid() ) - ( (QTextParag*)this )->format(); + ( (QTextParagraph*)this )->format(); - QMap<int, QTextParagLineStart*>::ConstIterator it = lineStarts.begin(); + QMap<int, QTextLineStart*>::ConstIterator it = lineStarts.begin(); while ( l-- > 0 ) ++it; y = ( *it )->y; h = ( *it )->h; bl = ( *it )->baseLine; } -int QTextParag::alignment() const -{ - if ( align != -1 ) - return align; - QStyleSheetItem *item = style(); - if ( !item ) - return Qt3::AlignAuto; - if ( mStyleSheetItemsVec ) { - for ( int i = 0; i < (int)mStyleSheetItemsVec->size(); ++i ) { - item = (*mStyleSheetItemsVec)[ i ]; - if ( item->alignment() != QStyleSheetItem::Undefined ) - return item->alignment(); - } - } - return Qt3::AlignAuto; -} -QPtrVector<QStyleSheetItem> QTextParag::styleSheetItems() const +void QTextParagraph::setAlignment( int a ) { - QPtrVector<QStyleSheetItem> vec; - if ( mStyleSheetItemsVec ) { - vec.resize( mStyleSheetItemsVec->size() ); - for ( int i = 0; i < (int)vec.size(); ++i ) - vec.insert( i, (*mStyleSheetItemsVec)[ i ] ); - } - return vec; -} - -QStyleSheetItem *QTextParag::style() const -{ - if ( !mStyleSheetItemsVec || mStyleSheetItemsVec->size() == 0 ) - return 0; - return (*mStyleSheetItemsVec)[ mStyleSheetItemsVec->size() - 1 ]; -} - -int QTextParag::numberOfSubParagraph() const -{ - if ( list_val != -1 ) - return list_val; - if ( numSubParag != -1 ) - return numSubParag; - int n = 0; - QTextParag *p = (QTextParag*)this; - while ( p && ( styleSheetItemsVec().size() >= p->styleSheetItemsVec().size() && - styleSheetItemsVec()[ (int)p->styleSheetItemsVec().size() - 1 ] == p->style() || - p->styleSheetItemsVec().size() >= styleSheetItemsVec().size() && - p->styleSheetItemsVec()[ (int)styleSheetItemsVec().size() - 1 ] == style() ) ) { - if ( p->style() == style() && listStyle() != p->listStyle() - && p->styleSheetItemsVec().size() == styleSheetItemsVec().size() ) - break; - if ( p->style()->displayMode() == QStyleSheetItem::DisplayListItem - && p->style() != style() || styleSheetItemsVec().size() == p->styleSheetItemsVec().size() ) - ++n; - p = p->prev(); - } - ( (QTextParag*)this )->numSubParag = n; - return n; -} - -void QTextParag::setFormat( QTextFormat *fm ) -{ - bool doUpdate = FALSE; - if (defFormat && (defFormat != formatCollection()->defaultFormat())) - doUpdate = TRUE; - defFormat = formatCollection()->format( fm ); - if ( !doUpdate ) + if ( a == (int)align ) return; - for ( int i = 0; i < length(); ++i ) { - if ( at( i )->format()->styleName() == defFormat->styleName() ) - at( i )->format()->updateStyle(); - } + align = a; + invalidate( 0 ); } -QTextFormatter *QTextParag::formatter() const +QTextFormatter *QTextParagraph::formatter() const { if ( hasdoc ) return document()->formatter(); if ( pseudoDocument()->pFormatter ) return pseudoDocument()->pFormatter; - return ( ( (QTextParag*)this )->pseudoDocument()->pFormatter = new QTextFormatterBreakWords ); + return ( ( (QTextParagraph*)this )->pseudoDocument()->pFormatter = new QTextFormatterBreakWords ); } -void QTextParag::setTabArray( int *a ) +void QTextParagraph::setTabArray( int *a ) { delete [] tArray; tArray = a; } -void QTextParag::setTabStops( int tw ) +void QTextParagraph::setTabStops( int tw ) { if ( hasdoc ) document()->setTabStops( tw ); else tabStopWidth = tw; } -QMap<int, QTextParagSelection> &QTextParag::selections() const +QMap<int, QTextParagraphSelection> &QTextParagraph::selections() const { if ( !mSelections ) - ((QTextParag *)this)->mSelections = new QMap<int, QTextParagSelection>; + ((QTextParagraph *)this)->mSelections = new QMap<int, QTextParagraphSelection>; return *mSelections; } -QPtrVector<QStyleSheetItem> &QTextParag::styleSheetItemsVec() const -{ - if ( !mStyleSheetItemsVec ) - ((QTextParag *)this)->mStyleSheetItemsVec = new QPtrVector<QStyleSheetItem>; - return *mStyleSheetItemsVec; -} -QPtrList<QTextCustomItem> &QTextParag::floatingItems() const +QPtrList<QTextCustomItem> &QTextParagraph::floatingItems() const { if ( !mFloatingItems ) - ((QTextParag *)this)->mFloatingItems = new QPtrList<QTextCustomItem>; + ((QTextParagraph *)this)->mFloatingItems = new QPtrList<QTextCustomItem>; return *mFloatingItems; } QTextStringChar::~QTextStringChar() @@ -701,6 +598,6 @@ QTextStringChar::~QTextStringChar() if ( type ) // not Regular delete d.custom; } -QTextParagPseudoDocument::QTextParagPseudoDocument():pFormatter(0),commandHistory(0), minw(0),wused(0){} -QTextParagPseudoDocument::~QTextParagPseudoDocument(){ delete pFormatter; delete commandHistory; } +QTextParagraphPseudoDocument::QTextParagraphPseudoDocument():pFormatter(0),commandHistory(0), minw(0),wused(0){} +QTextParagraphPseudoDocument::~QTextParagraphPseudoDocument(){ delete pFormatter; delete commandHistory; } diff --git a/noncore/apps/opie-write/qrichtext_p.h b/noncore/apps/opie-write/qrichtext_p.h index 94ce913..e368edb 100644 --- a/noncore/apps/opie-write/qrichtext_p.h +++ b/noncore/apps/opie-write/qrichtext_p.h @@ -50,9 +50,8 @@ // // #ifndef QT_H -#include "qt3namespace.h" #include "qstring.h" #include "qlist.h" #include "qrect.h" #include "qfontmetrics.h" @@ -77,18 +76,16 @@ #include "qapplication.h" #include <limits.h> #endif // QT_H -//#define DEBUG_COLLECTION - namespace Qt3 { class QTextDocument; class QTextString; class QTextPreProcessor; class QTextFormat; class QTextCursor; -class QTextParag; +class QTextParagraph; class QTextFormatter; class QTextIndent; class QTextFormatCollection; class QStyleSheetItem; @@ -124,9 +121,8 @@ public: QTextFormat *format() const; QTextCustomItem *customItem() const; void setFormat( QTextFormat *f ); void setCustomItem( QTextCustomItem *i ); - QTextStringChar *clone() const; struct CustomData { QTextFormat *format; QTextCustomItem *custom; @@ -151,14 +147,14 @@ private: //abort(); return *this; } friend class QComplexText; - friend class QTextParag; + friend class QTextParagraph; }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QMemArray<QTextStringChar>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMemArray<QTextStringChar>; // MOC_SKIP_END #endif class Q_EXPORT QTextString @@ -177,9 +173,10 @@ public: int width( int idx ) const; void insert( int index, const QString &s, QTextFormat *f ); - void insert( int index, QTextStringChar *c ); + void insert( int index, const QChar *unicode, int len, QTextFormat *f ); + void insert( int index, QTextStringChar *c, bool doAddRefFormat = FALSE ); void truncate( int index ); void remove( int index, int len ); void clear(); @@ -194,9 +191,9 @@ public: QMemArray<QTextStringChar> subString( int start = 0, int len = 0xFFFFFF ) const; QMemArray<QTextStringChar> rawData() const { return data; } void operator=( const QString &s ) { clear(); insert( 0, s, 0 ); } - void operator+=( const QString &s ); + void operator+=( const QString &s ) {insert( length(), s, 0 ); } void prepend( const QString &s ) { insert( 0, s, 0 ); } private: void checkBidi() const; @@ -230,33 +227,32 @@ inline QChar::Direction QTextString::direction() const // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QValueStack<int>; -template class Q_EXPORT QValueStack<QTextParag*>; -template class Q_EXPORT QValueStack<bool>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<int>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<QTextParagraph*>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueStack<bool>; // MOC_SKIP_END #endif class Q_EXPORT QTextCursor { public: - QTextCursor( QTextDocument *d ); - QTextCursor(); + QTextCursor( QTextDocument *d = 0 ); QTextCursor( const QTextCursor &c ); QTextCursor &operator=( const QTextCursor &c ); virtual ~QTextCursor() {} bool operator==( const QTextCursor &c ) const; bool operator!=( const QTextCursor &c ) const { return !(*this == c); } - QTextDocument *document() const { return doc; } - void setDocument( QTextDocument *d ); - - QTextParag *parag() const; + QTextParagraph *paragraph() const; + void setParagraph( QTextParagraph*p ) { gotoPosition(p, 0 ); } + QTextDocument *document() const; int index() const; - void setParag( QTextParag *s, bool restore = TRUE ); + void setIndex( int index ) { gotoPosition(paragraph(), index ); } + void gotoPosition( QTextParagraph* p, int index = 0); void gotoLeft(); void gotoRight(); void gotoNextLetter(); void gotoPreviousLetter(); @@ -273,33 +269,31 @@ public: void gotoWordLeft(); void gotoWordRight(); void insert( const QString &s, bool checkNewLine, QMemArray<QTextStringChar> *formatting = 0 ); - void splitAndInsertEmptyParag( bool ind = TRUE, bool updateIds = TRUE ); + void splitAndInsertEmptyParagraph( bool ind = TRUE, bool updateIds = TRUE ); bool remove(); - void killLine(); void indent(); bool atParagStart(); bool atParagEnd(); - void setIndex( int i, bool restore = TRUE ); - - void checkIndex(); + int x() const; // x in current paragraph + int y() const; // y in current paragraph - int offsetX() const { return ox; } - int offsetY() const { return oy; } + int globalX() const; + int globalY() const; - QTextParag *topParag() const { return parags.isEmpty() ? string : parags.first(); } - int totalOffsetX() const; - int totalOffsetY() const; + QTextParagraph *topParagraph() const { return paras.isEmpty() ? para : paras.first(); } + int offsetX() const { return ox; } // inner document offset + int offsetY() const { return oy; } // inner document offset + int totalOffsetX() const; // total document offset + int totalOffsetY() const; // total document offset - bool place( const QPoint &pos, QTextParag *s ) { return place( pos, s, FALSE ); } - bool place( const QPoint &pos, QTextParag *s, bool link ); + bool place( const QPoint &pos, QTextParagraph *s ) { return place( pos, s, FALSE ); } + bool place( const QPoint &pos, QTextParagraph *s, bool link ); void restoreState(); - int x() const; - int y() const; int nestedDepth() const { return (int)indices.count(); } //### size_t/int cast void oneUp() { if ( !indices.isEmpty() ) pop(); } void setValid( bool b ) { valid = b; } @@ -313,18 +307,15 @@ private: void processNesting( Operation op ); void invalidateNested(); void gotoIntoNested( const QPoint &globalPos ); - QTextParag *string; - QTextDocument *doc; + QTextParagraph *para; int idx, tmpIndex; int ox, oy; QValueStack<int> indices; - QValueStack<QTextParag*> parags; + QValueStack<QTextParagraph*> paras; QValueStack<int> xOffsets; QValueStack<int> yOffsets; - QValueStack<bool> nestedStack; - uint nested : 1; uint valid : 1; }; @@ -332,9 +323,9 @@ private: class Q_EXPORT QTextCommand { public: - enum Commands { Invalid, Insert, Delete, Format, Alignment, ParagType }; + enum Commands { Invalid, Insert, Delete, Format, Style }; QTextCommand( QTextDocument *d ) : doc( d ), cursor( d ) {} virtual ~QTextCommand(); @@ -350,9 +341,9 @@ protected: }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QPtrList<QTextCommand>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextCommand>; // MOC_SKIP_END #endif class Q_EXPORT QTextCommandHistory @@ -420,27 +411,27 @@ public: int height; QRect geometry() const { return QRect( xpos, ypos, width, height ); } - virtual bool enter( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); - virtual bool enterAt( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, const QPoint & ); - virtual bool next( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool prev( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool down( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool up( QTextCursor *, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); + virtual bool enter( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint & ); + virtual bool next( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( QTextCursor *, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); - void setParagraph( QTextParag *p ) { parag = p; } - QTextParag *paragrapth() const { return parag; } + void setParagraph( QTextParagraph *p ) { parag = p; } + QTextParagraph *paragraph() const { return parag; } QTextDocument *parent; - QTextParag *parag; + QTextParagraph *parag; virtual void pageBreak( int y, QTextFlow* flow ); }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QMap<QString, QString>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<QString, QString>; // MOC_SKIP_END #endif class Q_EXPORT QTextImage : public QTextCustomItem @@ -488,9 +479,9 @@ private: }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QPtrList<QTextCustomItem>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextCustomItem>; // MOC_SKIP_END #endif class Q_EXPORT QTextFlow @@ -545,9 +536,8 @@ public: const QMap<QString, QString> &attr, const QStyleSheetItem* style, const QTextFormat& fmt, const QString& context, QMimeSourceFactory &factory, QStyleSheet *sheet, const QString& doc ); - QTextTableCell( QTextTable* table, int row, int column ); virtual ~QTextTableCell(); QSize sizeHint() const ; QSize minimumSize() const ; @@ -599,10 +589,10 @@ private: }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QPtrList<QTextTableCell>; -template class Q_EXPORT QMap<QTextCursor*, int>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextTableCell>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<QTextCursor*, int>; // MOC_SKIP_END #endif class Q_EXPORT QTextTable: public QTextCustomItem @@ -624,14 +614,14 @@ public: bool isNested() const { return TRUE; } void resize( int nwidth ); virtual void invalidate(); - virtual bool enter( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); - virtual bool enterAt( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy, const QPoint &pos ); - virtual bool next( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool prev( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool down( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); - virtual bool up( QTextCursor *c, QTextDocument *&doc, QTextParag *¶g, int &idx, int &ox, int &oy ); + virtual bool enter( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy, const QPoint &pos ); + virtual bool next( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( QTextCursor *c, QTextDocument *&doc, QTextParagraph *¶g, int &idx, int &ox, int &oy ); QString richText() const; int minimumWidth() const; @@ -665,9 +655,9 @@ private: // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class QTextTableCell; -class QTextParag; +class QTextParagraph; struct Q_EXPORT QTextDocumentSelection { QTextCursor startCursor, endCursor; @@ -675,12 +665,12 @@ struct Q_EXPORT QTextDocumentSelection }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QMap<int, QColor>; -template class Q_EXPORT QMap<int, bool>; -template class Q_EXPORT QMap<int, QTextDocumentSelection>; -template class Q_EXPORT QPtrList<QTextDocument>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QColor>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, bool>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextDocumentSelection>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrList<QTextDocument>; // MOC_SKIP_END #endif class Q_EXPORT QTextDocument : public QObject @@ -689,9 +679,9 @@ class Q_EXPORT QTextDocument : public QObject friend class QTextTableCell; friend class QTextCursor; friend class QTextEdit; - friend class QTextParag; + friend class QTextParagraph; public: enum SelectionIds { Standard = 0, @@ -703,9 +693,9 @@ public: QTextDocument( QTextDocument *d, QTextFormatCollection *f ); virtual ~QTextDocument(); QTextDocument *parent() const { return par; } - QTextParag *parentParag() const { return parParag; } + QTextParagraph *parentParagraph() const { return parentPar; } void setText( const QString &text, const QString &context ); QMap<QString, QString> attributes() const { return attribs; } void setAttributes( const QMap<QString, QString> &attr ) { attribs = attr; } @@ -721,20 +711,20 @@ public: int visibleWidth() const; int height() const; void setWidth( int w ); int minimumWidth() const; - bool setMinimumWidth( int needed, int used = -1, QTextParag *parag = 0 ); + bool setMinimumWidth( int needed, int used = -1, QTextParagraph *parag = 0 ); void setY( int y ); int leftMargin() const; void setLeftMargin( int lm ); int rightMargin() const; void setRightMargin( int rm ); - QTextParag *firstParag() const; - QTextParag *lastParag() const; - void setFirstParag( QTextParag *p ); - void setLastParag( QTextParag *p ); + QTextParagraph *firstParagraph() const; + QTextParagraph *lastParagraph() const; + void setFirstParagraph( QTextParagraph *p ); + void setLastParagraph( QTextParagraph *p ); void invalidate(); void setPreProcessor( QTextPreProcessor *sh ); @@ -750,37 +740,34 @@ public: bool invertSelectionText( int id ) const; void setSelectionColor( int id, const QColor &c ); void setInvertSelectionText( int id, bool b ); bool hasSelection( int id, bool visible = FALSE ) const; - void setSelectionStart( int id, QTextCursor *cursor ); - bool setSelectionEnd( int id, QTextCursor *cursor ); + void setSelectionStart( int id, const QTextCursor &cursor ); + bool setSelectionEnd( int id, const QTextCursor &cursor ); void selectAll( int id ); bool removeSelection( int id ); void selectionStart( int id, int ¶gId, int &index ); QTextCursor selectionStartCursor( int id ); QTextCursor selectionEndCursor( int id ); void selectionEnd( int id, int ¶gId, int &index ); void setFormat( int id, QTextFormat *f, int flags ); - QTextParag *selectionStart( int id ); - QTextParag *selectionEnd( int id ); int numSelections() const { return nSelections; } void addSelection( int id ); - QString selectedText( int id, bool withCustom = TRUE ) const; - void copySelectedText( int id ); + QString selectedText( int id, bool asRichText = FALSE ) const; void removeSelectedText( int id, QTextCursor *cursor ); void indentSelection( int id ); - QTextParag *paragAt( int i ) const; + QTextParagraph *paragAt( int i ) const; void addCommand( QTextCommand *cmd ); QTextCursor *undo( QTextCursor *c = 0 ); QTextCursor *redo( QTextCursor *c = 0 ); QTextCommandHistory *commands() const { return commandHistory; } QTextFormatCollection *formatCollection() const; - bool find( const QString &expr, bool cs, bool wo, bool forward, int *parag, int *index, QTextCursor *cursor ); + bool find( QTextCursor &cursor, const QString &expr, bool cs, bool wo, bool forward); void setTextFormat( Qt::TextFormat f ); Qt::TextFormat textFormat() const; @@ -790,33 +777,29 @@ public: QMimeSourceFactory *mimeSourceFactory() const { return factory_; } QString context() const { return contxt; } void setStyleSheet( QStyleSheet *s ); - void updateStyles(); - void updateFontSizes( int base, bool usePixels ); - void updateFontAttributes( const QFont &f, const QFont &old ); + void setDefaultFormat( const QFont &font, const QColor &color ); void setMimeSourceFactory( QMimeSourceFactory *f ) { if ( f ) factory_ = f; } void setContext( const QString &c ) { if ( !c.isEmpty() ) contxt = c; } - void setUnderlineLinks( bool b ) { underlLinks = b; } + void setUnderlineLinks( bool b ); bool underlineLinks() const { return underlLinks; } void setPaper( QBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; } QBrush *paper() const { return backBrush; } void doLayout( QPainter *p, int w ); void draw( QPainter *p, const QRect& rect, const QColorGroup &cg, const QBrush *paper = 0 ); - void drawParag( QPainter *p, QTextParag *parag, int cx, int cy, int cw, int ch, + void drawParagraph( QPainter *p, QTextParagraph *parag, int cx, int cy, int cw, int ch, QPixmap *&doubleBuffer, const QColorGroup &cg, bool drawCursor, QTextCursor *cursor, bool resetChanged = TRUE ); - QTextParag *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, + QTextParagraph *draw( QPainter *p, int cx, int cy, int cw, int ch, const QColorGroup &cg, bool onlyChanged = FALSE, bool drawCursor = FALSE, QTextCursor *cursor = 0, bool resetChanged = TRUE ); - void setDefaultFont( const QFont &f ); - - void registerCustomItem( QTextCustomItem *i, QTextParag *p ); - void unregisterCustomItem( QTextCustomItem *i, QTextParag *p ); + void registerCustomItem( QTextCustomItem *i, QTextParagraph *p ); + void unregisterCustomItem( QTextCustomItem *i, QTextParagraph *p ); void setFlow( QTextFlow *f ); void takeFlow(); QTextFlow *flow() const { return flow_; } @@ -830,10 +813,10 @@ public: void setTableCell( QTextTableCell *c ) { tc = c; } void setPlainText( const QString &text ); void setRichText( const QString &text, const QString &context ); - QString richText( QTextParag *p = 0 ) const; - QString plainText( QTextParag *p = 0 ) const; + QString richText() const; + QString plainText() const; bool focusNextPrevChild( bool next ); int alignment() const; @@ -849,23 +832,23 @@ public: int length() const; void clear( bool createEmptyParag = FALSE ); - virtual QTextParag *createParag( QTextDocument *d, QTextParag *pr = 0, QTextParag *nx = 0, bool updateIds = TRUE ); + virtual QTextParagraph *createParagraph( QTextDocument *d, QTextParagraph *pr = 0, QTextParagraph *nx = 0, bool updateIds = TRUE ); void insertChild( QObject *o ) { QObject::insertChild( o ); } void removeChild( QObject *o ) { QObject::removeChild( o ); } void insertChild( QTextDocument *d ) { childList.append( d ); } void removeChild( QTextDocument *d ) { childList.removeRef( d ); } QPtrList<QTextDocument> children() const { return childList; } - void setAddMargins( bool b ) { addMargs = b; } - int addMargins() const { return addMargs; } - bool hasFocusParagraph() const; QString focusHref() const; void invalidateOriginalText() { oTextValid = FALSE; oText = ""; } + static QString section( QString str, const QString &sep, int start, int end = 0xffffffff ); + static bool endsWith( QString str, const QString &s); + signals: void minimumWidthChanged( int ); private: @@ -874,27 +857,28 @@ private: // HTML parser bool hasPrefix(const QChar* doc, int length, int pos, QChar c); bool hasPrefix(const QChar* doc, int length, int pos, const QString& s); QTextCustomItem* parseTable( const QMap<QString, QString> &attr, const QTextFormat &fmt, - const QChar* doc, int length, int& pos, QTextParag *curpar ); + const QChar* doc, int length, int& pos, QTextParagraph *curpar ); bool eatSpace(const QChar* doc, int length, int& pos, bool includeNbsp = FALSE ); bool eat(const QChar* doc, int length, int& pos, QChar c); QString parseOpenTag(const QChar* doc, int length, int& pos, QMap<QString, QString> &attr, bool& emptyTag); QString parseCloseTag( const QChar* doc, int length, int& pos ); QChar parseHTMLSpecialChar(const QChar* doc, int length, int& pos); QString parseWord(const QChar* doc, int length, int& pos, bool lower = TRUE); QChar parseChar(const QChar* doc, int length, int& pos, QStyleSheetItem::WhiteSpaceMode wsm ); - void setRichTextInternal( const QString &text ); + void setRichTextInternal( const QString &text, QTextCursor* cursor = 0 ); + void setRichTextMarginsInternal( QPtrList< QPtrVector<QStyleSheetItem> >& styles, QTextParagraph* stylesPar ); private: struct Q_EXPORT Focus { - QTextParag *parag; + QTextParagraph *parag; int start, len; QString href; }; int cx, cy, cw, vw; - QTextParag *fParag, *lParag; + QTextParagraph *fParag, *lParag; QTextPreProcessor *pProcessor; QMap<int, QColor> selectionColors; QMap<int, QTextDocumentSelection> selections; QMap<int, bool> selectionText; @@ -908,26 +892,24 @@ private: uint useFC : 1; uint withoutDoubleBuffer : 1; uint underlLinks : 1; uint nextDoubleBuffered : 1; - uint addMargs : 1; uint oTextValid : 1; uint mightHaveCustomItems : 1; int align; int nSelections; QTextFlow *flow_; QTextDocument *par; - QTextParag *parParag; + QTextParagraph *parentPar; QTextTableCell *tc; - QTextCursor *tmpCursor; QBrush *backBrush; QPixmap *buf_pixmap; Focus focusIndicator; int minw; int wused; int leftmargin; int rightmargin; - QTextParag *minwParag, *curParag; + QTextParagraph *minwParag, *curParag; QStyleSheet* sheet_; QMimeSourceFactory* factory_; QString contxt; QMap<QString, QString> attribs; @@ -936,9 +918,11 @@ private: int uDepth; QString oText; QPtrList<QTextDocument> childList; QColor linkColor; + double scaleFontsFactor; + short list_tm,list_bm, list_lm, li_tm, li_bm, par_tm, par_bm; }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -946,37 +930,31 @@ private: class Q_EXPORT QTextDeleteCommand : public QTextCommand { public: QTextDeleteCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, - const QValueList< QPtrVector<QStyleSheetItem> > &os, - const QValueList<QStyleSheetItem::ListStyle> &ols, - const QMemArray<int> &oas ); - QTextDeleteCommand( QTextParag *p, int idx, const QMemArray<QTextStringChar> &str ); + const QByteArray& oldStyle ); + QTextDeleteCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ); virtual ~QTextDeleteCommand(); Commands type() const { return Delete; } QTextCursor *execute( QTextCursor *c ); QTextCursor *unexecute( QTextCursor *c ); protected: int id, index; - QTextParag *parag; + QTextParagraph *parag; QMemArray<QTextStringChar> text; - QValueList< QPtrVector<QStyleSheetItem> > oldStyles; - QValueList<QStyleSheetItem::ListStyle> oldListStyles; - QMemArray<int> oldAligns; + QByteArray styleInformation; }; class Q_EXPORT QTextInsertCommand : public QTextDeleteCommand { public: QTextInsertCommand( QTextDocument *d, int i, int idx, const QMemArray<QTextStringChar> &str, - const QValueList< QPtrVector<QStyleSheetItem> > &os, - const QValueList<QStyleSheetItem::ListStyle> &ols, - const QMemArray<int> &oas ) - : QTextDeleteCommand( d, i, idx, str, os, ols, oas ) {} - QTextInsertCommand( QTextParag *p, int idx, const QMemArray<QTextStringChar> &str ) + const QByteArray& oldStyleInfo ) + : QTextDeleteCommand( d, i, idx, str, oldStyleInfo ) {} + QTextInsertCommand( QTextParagraph *p, int idx, const QMemArray<QTextStringChar> &str ) : QTextDeleteCommand( p, idx, str ) {} virtual ~QTextInsertCommand() {} Commands type() const { return Insert; } @@ -1002,72 +980,53 @@ protected: int flags; }; -class Q_EXPORT QTextAlignmentCommand : public QTextCommand +class Q_EXPORT QTextStyleCommand : public QTextCommand { public: - QTextAlignmentCommand( QTextDocument *d, int fParag, int lParag, int na, const QMemArray<int> &oa ); - virtual ~QTextAlignmentCommand() {} + QTextStyleCommand( QTextDocument *d, int fParag, int lParag, const QByteArray& beforeChange ); + virtual ~QTextStyleCommand() {} - Commands type() const { return Alignment; } + Commands type() const { return Style; } QTextCursor *execute( QTextCursor *c ); QTextCursor *unexecute( QTextCursor *c ); -private: - int firstParag, lastParag; - int newAlign; - QMemArray<int> oldAligns; - -}; - -class Q_EXPORT QTextParagTypeCommand : public QTextCommand -{ -public: - QTextParagTypeCommand( QTextDocument *d, int fParag, int lParag, bool l, - QStyleSheetItem::ListStyle s, const QValueList< QPtrVector<QStyleSheetItem> > &os, - const QValueList<QStyleSheetItem::ListStyle> &ols ); - virtual ~QTextParagTypeCommand() {} - - Commands type() const { return ParagType; } - QTextCursor *execute( QTextCursor *c ); - QTextCursor *unexecute( QTextCursor *c ); + static QByteArray readStyleInformation( QTextDocument* d, int fParag, int lParag ); + static void writeStyleInformation( QTextDocument* d, int fParag, const QByteArray& style ); private: int firstParag, lastParag; - bool list; - QStyleSheetItem::ListStyle listStyle; - QValueList< QPtrVector<QStyleSheetItem> > oldStyles; - QValueList<QStyleSheetItem::ListStyle> oldListStyles; - + QByteArray before; + QByteArray after; }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -struct Q_EXPORT QTextParagSelection +struct Q_EXPORT QTextParagraphSelection { int start, end; }; -struct Q_EXPORT QTextParagLineStart +struct Q_EXPORT QTextLineStart { - QTextParagLineStart() : y( 0 ), baseLine( 0 ), h( 0 ) + QTextLineStart() : y( 0 ), baseLine( 0 ), h( 0 ) #ifndef QT_NO_COMPLEXTEXT , bidicontext( 0 ) #endif { } - QTextParagLineStart( ushort y_, ushort bl, ushort h_ ) : y( y_ ), baseLine( bl ), h( h_ ), + QTextLineStart( ushort y_, ushort bl, ushort h_ ) : y( y_ ), baseLine( bl ), h( h_ ), w( 0 ) #ifndef QT_NO_COMPLEXTEXT , bidicontext( 0 ) #endif { } #ifndef QT_NO_COMPLEXTEXT - QTextParagLineStart( QBidiContext *c, QBidiStatus s ) : y(0), baseLine(0), h(0), + QTextLineStart( QBidiContext *c, QBidiStatus s ) : y(0), baseLine(0), h(0), status( s ), bidicontext( c ) { if ( bidicontext ) bidicontext->ref(); } #endif - virtual ~QTextParagLineStart() + virtual ~QTextLineStart() { #ifndef QT_NO_COMPLEXTEXT if ( bidicontext && bidicontext->deref() ) delete bidicontext; @@ -1101,80 +1060,81 @@ private: }; #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QMap<int, QTextParagSelection>; -template class Q_EXPORT QMap<int, QTextParagLineStart*>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextParagraphSelection>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QMap<int, QTextLineStart*>; // MOC_SKIP_END #endif -class Q_EXPORT QTextParagData +class Q_EXPORT QTextParagraphData { public: - QTextParagData() {} - virtual ~QTextParagData(); - virtual void join( QTextParagData * ); + QTextParagraphData() {} + virtual ~QTextParagraphData(); + virtual void join( QTextParagraphData * ); }; -class Q_EXPORT QTextParagPseudoDocument +class Q_EXPORT QTextParagraphPseudoDocument { public: - QTextParagPseudoDocument(); - ~QTextParagPseudoDocument(); + QTextParagraphPseudoDocument(); + ~QTextParagraphPseudoDocument(); QRect docRect; QTextFormatter *pFormatter; QTextCommandHistory *commandHistory; int minw; int wused; }; //nase -class Q_EXPORT QTextParag +class Q_EXPORT QTextParagraph { friend class QTextDocument; friend class QTextCursor; public: - QTextParag( QTextDocument *d, QTextParag *pr = 0, QTextParag *nx = 0, bool updateIds = TRUE ); - virtual ~QTextParag(); + QTextParagraph( QTextDocument *d, QTextParagraph *pr = 0, QTextParagraph *nx = 0, bool updateIds = TRUE ); + virtual ~QTextParagraph(); QTextString *string() const; QTextStringChar *at( int i ) const; // maybe remove later int leftGap() const; int length() const; // maybe remove later - void setListStyle( QStyleSheetItem::ListStyle ls ); - QStyleSheetItem::ListStyle listStyle() const; + void setListStyle( QStyleSheetItem::ListStyle ls ) { lstyle = ls; changed = TRUE; } + QStyleSheetItem::ListStyle listStyle() const { return lstyle; } + void setListItem( bool li ) { litem = li; changed = TRUE; } + bool isListItem() const { return litem; } void setListValue( int v ) { list_val = v; } - int listValue() const { return list_val; } + int listValue() const { return list_val > 0 ? list_val : -1; } - void setList( bool b, int listStyle ); - void incDepth(); - void decDepth(); - int listDepth() const; + void setListDepth( int depth ); + int listDepth() const { return ldepth; } - void setFormat( QTextFormat *fm ); - QTextFormat *paragFormat() const; +// void setFormat( QTextFormat *fm ); +// QTextFormat *paragFormat() const; QTextDocument *document() const; - QTextParagPseudoDocument *pseudoDocument() const; + QTextParagraphPseudoDocument *pseudoDocument() const; QRect rect() const; void setHeight( int h ) { r.setHeight( h ); } void show(); void hide(); bool isVisible() const { return visible; } - QTextParag *prev() const; - QTextParag *next() const; - void setPrev( QTextParag *s ); - void setNext( QTextParag *s ); + QTextParagraph *prev() const; + QTextParagraph *next() const; + void setPrev( QTextParagraph *s ); + void setNext( QTextParagraph *s ); void insert( int index, const QString &s ); + void insert( int index, const QChar *unicode, int len ); void append( const QString &s, bool reallyAtEnd = FALSE ); void truncate( int index ); void remove( int index, int len ); - void join( QTextParag *s ); + void join( QTextParagraph *s ); void invalidate( int chr ); void move( int &dy ); @@ -1211,12 +1171,12 @@ public: void setFirstPreProcess( bool b ); void indent( int *oldIndent = 0, int *newIndent = 0 ); - void setExtraData( QTextParagData *data ); - QTextParagData *extraData() const; + void setExtraData( QTextParagraphData *data ); + QTextParagraphData *extraData() const; - QMap<int, QTextParagLineStart*> &lineStartList(); + QMap<int, QTextLineStart*> &lineStartList(); void setFormat( int index, int len, QTextFormat *f, bool useCollection = TRUE, int flags = -1 ); void setAlignment( int a ); @@ -1224,20 +1184,15 @@ public: virtual void paint( QPainter &painter, const QColorGroup &cg, QTextCursor *cursor = 0, bool drawSelections = FALSE, int clipx = -1, int clipy = -1, int clipw = -1, int cliph = -1 ); - void setStyleSheetItems( const QPtrVector<QStyleSheetItem> &vec ); - QPtrVector<QStyleSheetItem> styleSheetItems() const; - QStyleSheetItem *style() const; - virtual int topMargin() const; virtual int bottomMargin() const; virtual int leftMargin() const; virtual int firstLineMargin() const; virtual int rightMargin() const; virtual int lineSpacing() const; - int numberOfSubParagraph() const; void registerFloatingItem( QTextCustomItem *i ); void unregisterFloatingItem( QTextCustomItem *i ); void setFullWidth( bool b ) { fullWidth = b; } @@ -1269,70 +1224,70 @@ public: void addCommand( QTextCommand *cmd ); QTextCursor *undo( QTextCursor *c = 0 ); QTextCursor *redo( QTextCursor *c = 0 ); QTextCommandHistory *commands() const; - virtual void copyParagData( QTextParag *parag ); + virtual void copyParagData( QTextParagraph *parag ); void setBreakable( bool b ) { breakable = b; } bool isBreakable() const { return breakable; } void setBackgroundColor( const QColor &c ); QColor *backgroundColor() const { return bgcol; } void clearBackgroundColor(); - bool isLineBreak() const { return isBr; } - void setMovedDown( bool b ) { movedDown = b; } bool wasMovedDown() const { return movedDown; } void setDirection( QChar::Direction d ); QChar::Direction direction() const; + void readStyleInformation( QDataStream& stream ); + void writeStyleInformation( QDataStream& stream ) const; + protected: virtual void drawLabel( QPainter* p, int x, int y, int w, int h, int base, const QColorGroup& cg ); - virtual void drawParagString( QPainter &painter, const QString &str, int start, int len, int startX, - int lastY, int baseLine, int bw, int h, bool drawSelections, - QTextStringChar *formatChar, int i, const QMemArray<int> &selectionStarts, - const QMemArray<int> &selectionEnds, const QColorGroup &cg, bool rightToLeft ); + virtual void drawString( QPainter &painter, const QString &str, int start, int len, int xstart, + int y, int baseLine, int w, int h, int selection, + QTextStringChar *formatChar, const QColorGroup& cg, + bool rightToLeft ); private: - QMap<int, QTextParagSelection> &selections() const; - QPtrVector<QStyleSheetItem> &styleSheetItemsVec() const; + QMap<int, QTextParagraphSelection> &selections() const; QPtrList<QTextCustomItem> &floatingItems() const; + QBrush backgroundBrush( const QColorGroup&cg ) { if ( bgcol ) return *bgcol; return cg.brush( QColorGroup::Base ); } + void invalidateStyleCache(); - QMap<int, QTextParagLineStart*> lineStarts; + QMap<int, QTextLineStart*> lineStarts; int invalid; QRect r; - QTextParag *p, *n; + QTextParagraph *p, *n; void *docOrPseudo; uint changed : 1; uint firstFormat : 1; uint firstPProcess : 1; uint needPreProcess : 1; uint fullWidth : 1; - uint newLinesAllowed : 1; uint lastInFrame : 1; uint visible : 1; uint breakable : 1; - uint isBr : 1; uint movedDown : 1; uint mightHaveCustomItems : 1; uint hasdoc : 1; + uint litem : 1; // whether the paragraph is a list item + uint rtext : 1; // whether the paragraph needs rich text margin int align : 4; int state, id; QTextString *str; - QMap<int, QTextParagSelection> *mSelections; - QPtrVector<QStyleSheetItem> *mStyleSheetItemsVec; + QMap<int, QTextParagraphSelection> *mSelections; QPtrList<QTextCustomItem> *mFloatingItems; - QStyleSheetItem::ListStyle listS; - int numSubParag; - int tm, bm, lm, rm, flm; - QTextFormat *defFormat; + QStyleSheetItem::ListStyle lstyle; + short utm, ubm, ulm, urm, uflm, ulinespacing; int *tArray; - int tabStopWidth; - QTextParagData *eData; - int list_val; + short tabStopWidth; + QTextParagraphData *eData; + short list_val; QColor *bgcol; + ushort ldepth; }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -1342,12 +1297,12 @@ class Q_EXPORT QTextFormatter public: QTextFormatter(); virtual ~QTextFormatter(); - virtual int format( QTextDocument *doc, QTextParag *parag, int start, const QMap<int, QTextParagLineStart*> &oldLineStarts ) = 0; - virtual int formatVertically( QTextDocument* doc, QTextParag* parag ); + virtual int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ) = 0; + virtual int formatVertically( QTextDocument* doc, QTextParagraph* parag ); - bool isWrapEnabled( QTextParag *p ) const { if ( !wrapEnabled ) return FALSE; if ( p && !p->isBreakable() ) return FALSE; return TRUE;} + bool isWrapEnabled( QTextParagraph *p ) const { if ( !wrapEnabled ) return FALSE; if ( p && !p->isBreakable() ) return FALSE; return TRUE;} int wrapAtColumn() const { return wrapColumn;} virtual void setWrapEnabled( bool b ); virtual void setWrapAtColumn( int c ); virtual void setAllowBreakInWords( bool b ) { biw = b; } @@ -1355,17 +1310,18 @@ public: int minimumWidth() const { return thisminw; } int widthUsed() const { return thiswused; } + static bool isBreakable( QTextString *string, int pos ); + protected: - virtual QTextParagLineStart *formatLine( QTextParag *parag, QTextString *string, QTextParagLineStart *line, QTextStringChar *start, + virtual QTextLineStart *formatLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, QTextStringChar *start, QTextStringChar *last, int align = Qt3::AlignAuto, int space = 0 ); #ifndef QT_NO_COMPLEXTEXT - virtual QTextParagLineStart *bidiReorderLine( QTextParag *parag, QTextString *string, QTextParagLineStart *line, QTextStringChar *start, + virtual QTextLineStart *bidiReorderLine( QTextParagraph *parag, QTextString *string, QTextLineStart *line, QTextStringChar *start, QTextStringChar *last, int align, int space ); #endif - virtual bool isBreakable( QTextString *string, int pos ) const; - void insertLineStart( QTextParag *parag, int index, QTextParagLineStart *ls ); + void insertLineStart( QTextParagraph *parag, int index, QTextLineStart *ls ); int thisminw; int thiswused; @@ -1388,9 +1344,9 @@ class Q_EXPORT QTextFormatterBreakInWords : public QTextFormatter public: QTextFormatterBreakInWords(); virtual ~QTextFormatterBreakInWords() {} - int format( QTextDocument *doc, QTextParag *parag, int start, const QMap<int, QTextParagLineStart*> &oldLineStarts ); + int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ); }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -1400,9 +1356,9 @@ class Q_EXPORT QTextFormatterBreakWords : public QTextFormatter public: QTextFormatterBreakWords(); virtual ~QTextFormatterBreakWords() {} - int format( QTextDocument *doc, QTextParag *parag, int start, const QMap<int, QTextParagLineStart*> &oldLineStarts ); + int format( QTextDocument *doc, QTextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts ); }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -1412,9 +1368,9 @@ class Q_EXPORT QTextIndent public: QTextIndent(); virtual ~QTextIndent() {} - virtual void indent( QTextDocument *doc, QTextParag *parag, int *oldIndent = 0, int *newIndent = 0 ) = 0; + virtual void indent( QTextDocument *doc, QTextParagraph *parag, int *oldIndent = 0, int *newIndent = 0 ) = 0; }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -1428,9 +1384,9 @@ public: QTextPreProcessor(); virtual ~QTextPreProcessor() {} - virtual void process( QTextDocument *doc, QTextParag *, int, bool = TRUE ) = 0; + virtual void process( QTextDocument *doc, QTextParagraph *, int, bool = TRUE ) = 0; virtual QTextFormat *format( int id ) = 0; }; @@ -1451,9 +1407,10 @@ public: Size = 16, Color = 32, Misspelled = 64, VAlign = 128, - Font = Bold | Italic | Underline | Family | Size, + StrikeOut= 256, + Font = Bold | Italic | Underline | Family | Size | StrikeOut, Format = Font | Color | Misspelled | VAlign }; enum VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript }; @@ -1463,9 +1420,9 @@ public: QTextFormat( const QStyleSheetItem *s ); QTextFormat( const QFont &f, const QColor &c, QTextFormatCollection *parent = 0 ); QTextFormat( const QTextFormat &fm ); - QTextFormat makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr ) const; + QTextFormat makeTextFormat( const QStyleSheetItem *style, const QMap<QString,QString>& attr, double scaleFontsFactor ) const; QTextFormat& operator=( const QTextFormat &fm ); QColor color() const; QFont font() const; bool isMisspelled() const; @@ -1482,8 +1439,9 @@ public: void setBold( bool b ); void setItalic( bool b ); void setUnderline( bool b ); + void setStrikeOut( bool b ); void setFamily( const QString &f ); void setPointSize( int s ); void setFont( const QFont &f ); void setColor( const QColor &c ); @@ -1498,19 +1456,14 @@ public: void addRef(); void removeRef(); - QString makeFormatChangeTags( QTextFormat *f, const QString& oldAnchorHref, const QString& anchorHref ) const; - QString makeFormatEndTags( const QString& anchorHref ) const; + QString makeFormatChangeTags( QTextFormat* defaultFormat, QTextFormat *f, const QString& oldAnchorHref, const QString& anchorHref ) const; + QString makeFormatEndTags( QTextFormat* defaultFormat, const QString& anchorHref ) const; static void setPainter( QPainter *p ); static QPainter* painter(); - void updateStyle(); - void updateStyleFlags(); - void setStyle( const QString &s ); - QString styleName() const { return style; } - int changed() const { return different; } bool fontSizesInPixels() { return usePixelSizes; } protected: virtual void generateKey(); @@ -1534,18 +1487,16 @@ private: QString k; int logicalFontSize; int stdSize; static QPainter *pntr; - QString style; - int different; }; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QDict<QTextFormat>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QDict<QTextFormat>; // MOC_SKIP_END #endif class Q_EXPORT QTextFormatCollection @@ -1564,15 +1515,10 @@ public: virtual QTextFormat *format( const QFont &f, const QColor &c ); virtual void remove( QTextFormat *f ); virtual QTextFormat *createFormat( const QTextFormat &f ) { return new QTextFormat( f ); } virtual QTextFormat *createFormat( const QFont &f, const QColor &c ) { return new QTextFormat( f, c, this ); } - void debug(); - QStyleSheet *styleSheet() const { return sheet; } - void setStyleSheet( QStyleSheet *s ) { sheet = s; } - void updateStyles(); - void updateFontSizes( int base, bool usePixels ); - void updateFontAttributes( const QFont &f, const QFont &old ); + void updateDefaultFormat( const QFont &font, const QColor &c, QStyleSheet *sheet ); QDict<QTextFormat> dict() const { return cKey; } private: void updateKeys(); @@ -1584,10 +1530,8 @@ private: QFont cfont; QColor ccol; QString kof, knf; int cflags; - QStyleSheet *sheet; - }; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -1595,62 +1539,28 @@ inline int QTextString::length() const { return data.size(); } -inline void QTextString::operator+=( const QString &s ) -{ - insert( length(), s, 0 ); -} - -inline int QTextParag::length() const +inline int QTextParagraph::length() const { return str->length(); } -inline QRect QTextParag::rect() const +inline QRect QTextParagraph::rect() const { return r; } -inline QTextParag *QTextCursor::parag() const +inline QTextParagraph *QTextCursor::paragraph() const { - return string; + return para; } inline int QTextCursor::index() const { return idx; } -inline void QTextCursor::setIndex( int i, bool restore ) -{ - if ( restore ) - restoreState(); - if ( i < 0 || i >= string->length() ) { -#if defined(QT_CHECK_RANGE) - qWarning( "QTextCursor::setIndex: %d out of range", i ); -#endif - i = i < 0 ? 0 : string->length() - 1; - } - - tmpIndex = -1; - idx = i; -} - -inline void QTextCursor::setParag( QTextParag *s, bool restore ) -{ - if ( restore ) - restoreState(); - idx = 0; - string = s; - tmpIndex = -1; -} - -inline void QTextCursor::checkIndex() -{ - if ( idx >= string->length() ) - idx = string->length() - 1; -} // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inline int QTextDocument::x() const @@ -1672,24 +1582,24 @@ inline int QTextDocument::visibleWidth() const { return vw; } -inline QTextParag *QTextDocument::firstParag() const +inline QTextParagraph *QTextDocument::firstParagraph() const { return fParag; } -inline QTextParag *QTextDocument::lastParag() const +inline QTextParagraph *QTextDocument::lastParagraph() const { return lParag; } -inline void QTextDocument::setFirstParag( QTextParag *p ) +inline void QTextDocument::setFirstParagraph( QTextParagraph *p ) { fParag = p; } -inline void QTextDocument::setLastParag( QTextParag *p ) +inline void QTextDocument::setLastParagraph( QTextParagraph *p ) { lParag = p; } @@ -1869,11 +1779,8 @@ inline QTextFormatCollection *QTextFormat::parent() const inline void QTextFormat::addRef() { ref++; -#ifdef DEBUG_COLLECTION - qDebug( "add ref of '%s' to %d (%p)", k.latin1(), ref, this ); -#endif } inline void QTextFormat::removeRef() { @@ -1881,11 +1788,8 @@ inline void QTextFormat::removeRef() if ( !collection ) return; if ( this == collection->defFormat ) return; -#ifdef DEBUG_COLLECTION - qDebug( "remove ref of '%s' to %d (%p)", k.latin1(), ref, this ); -#endif if ( ref == 0 ) collection->remove( this ); } @@ -1898,13 +1802,8 @@ inline bool QTextFormat::useLinkColor() const { return linkColor; } -inline void QTextFormat::setStyle( const QString &s ) -{ - style = s; - updateStyleFlags(); -} // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inline QTextStringChar &QTextString::at( int i ) const @@ -1913,207 +1812,178 @@ inline QTextStringChar &QTextString::at( int i ) const } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -inline QTextStringChar *QTextParag::at( int i ) const +inline QTextStringChar *QTextParagraph::at( int i ) const { return &str->at( i ); } -inline bool QTextParag::isValid() const +inline bool QTextParagraph::isValid() const { return invalid == -1; } -inline bool QTextParag::hasChanged() const +inline bool QTextParagraph::hasChanged() const { return changed; } -inline void QTextParag::setBackgroundColor( const QColor & c ) +inline void QTextParagraph::setBackgroundColor( const QColor & c ) { delete bgcol; bgcol = new QColor( c ); setChanged( TRUE ); } -inline void QTextParag::clearBackgroundColor() +inline void QTextParagraph::clearBackgroundColor() { delete bgcol; bgcol = 0; setChanged( TRUE ); } -inline void QTextParag::append( const QString &s, bool reallyAtEnd ) +inline void QTextParagraph::append( const QString &s, bool reallyAtEnd ) { if ( reallyAtEnd ) insert( str->length(), s ); else insert( QMAX( str->length() - 1, 0 ), s ); } -inline QTextParag *QTextParag::prev() const +inline QTextParagraph *QTextParagraph::prev() const { return p; } -inline QTextParag *QTextParag::next() const +inline QTextParagraph *QTextParagraph::next() const { return n; } -inline bool QTextParag::hasAnySelection() const +inline bool QTextParagraph::hasAnySelection() const { return mSelections ? !selections().isEmpty() : FALSE; } -inline void QTextParag::setEndState( int s ) +inline void QTextParagraph::setEndState( int s ) { if ( s == state ) return; state = s; } -inline int QTextParag::endState() const +inline int QTextParagraph::endState() const { return state; } -inline void QTextParag::setParagId( int i ) +inline void QTextParagraph::setParagId( int i ) { id = i; } -inline int QTextParag::paragId() const +inline int QTextParagraph::paragId() const { if ( id == -1 ) qWarning( "invalid parag id!!!!!!!! (%p)", (void*)this ); return id; } -inline bool QTextParag::firstPreProcess() const +inline bool QTextParagraph::firstPreProcess() const { return firstPProcess; } -inline void QTextParag::setFirstPreProcess( bool b ) +inline void QTextParagraph::setFirstPreProcess( bool b ) { firstPProcess = b; } -inline QMap<int, QTextParagLineStart*> &QTextParag::lineStartList() +inline QMap<int, QTextLineStart*> &QTextParagraph::lineStartList() { return lineStarts; } -inline QTextString *QTextParag::string() const +inline QTextString *QTextParagraph::string() const { return str; } -inline QTextDocument *QTextParag::document() const +inline QTextDocument *QTextParagraph::document() const { if ( hasdoc ) return (QTextDocument*) docOrPseudo; return 0; } -inline QTextParagPseudoDocument *QTextParag::pseudoDocument() const +inline QTextParagraphPseudoDocument *QTextParagraph::pseudoDocument() const { if ( hasdoc ) return 0; - return (QTextParagPseudoDocument*) docOrPseudo; + return (QTextParagraphPseudoDocument*) docOrPseudo; } -inline QTextTableCell *QTextParag::tableCell() const +inline QTextTableCell *QTextParagraph::tableCell() const { return hasdoc ? document()->tableCell () : 0; } -inline QTextCommandHistory *QTextParag::commands() const +inline QTextCommandHistory *QTextParagraph::commands() const { return hasdoc ? document()->commands() : pseudoDocument()->commandHistory; } -inline void QTextParag::setAlignment( int a ) -{ - if ( a == (int)align ) - return; - align = a; - invalidate( 0 ); -} - -inline void QTextParag::setListStyle( QStyleSheetItem::ListStyle ls ) -{ - listS = ls; - invalidate( 0 ); -} - -inline QStyleSheetItem::ListStyle QTextParag::listStyle() const -{ - return listS; -} - -inline QTextFormat *QTextParag::paragFormat() const +inline int QTextParagraph::alignment() const { - return defFormat; + return align; } -inline void QTextParag::registerFloatingItem( QTextCustomItem *i ) +inline void QTextParagraph::registerFloatingItem( QTextCustomItem *i ) { floatingItems().append( i ); } -inline void QTextParag::unregisterFloatingItem( QTextCustomItem *i ) +inline void QTextParagraph::unregisterFloatingItem( QTextCustomItem *i ) { floatingItems().removeRef( i ); } -inline QBrush *QTextParag::background() const +inline QBrush *QTextParagraph::background() const { return tableCell() ? tableCell()->backGround() : 0; } -inline int QTextParag::documentWidth() const +inline int QTextParagraph::documentWidth() const { return hasdoc ? document()->width() : pseudoDocument()->docRect.width(); } -inline int QTextParag::documentVisibleWidth() const +inline int QTextParagraph::documentVisibleWidth() const { return hasdoc ? document()->visibleWidth() : pseudoDocument()->docRect.width(); } -inline int QTextParag::documentX() const +inline int QTextParagraph::documentX() const { return hasdoc ? document()->x() : pseudoDocument()->docRect.x(); } -inline int QTextParag::documentY() const +inline int QTextParagraph::documentY() const { return hasdoc ? document()->y() : pseudoDocument()->docRect.y(); } -inline void QTextParag::setExtraData( QTextParagData *data ) +inline void QTextParagraph::setExtraData( QTextParagraphData *data ) { eData = data; } -inline QTextParagData *QTextParag::extraData() const +inline QTextParagraphData *QTextParagraph::extraData() const { return eData; } -inline void QTextParag::setNewLinesAllowed( bool b ) -{ - newLinesAllowed = b; -} - -inline bool QTextParag::isNewLinesAllowed() const -{ - return newLinesAllowed; -} - // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inline void QTextFormatCollection::setDefaultFormat( QTextFormat *f ) { diff --git a/noncore/apps/opie-write/qstylesheet.cpp b/noncore/apps/opie-write/qstylesheet.cpp index 7ab9ec6..67cd828 100644 --- a/noncore/apps/opie-write/qstylesheet.cpp +++ b/noncore/apps/opie-write/qstylesheet.cpp @@ -53,8 +53,9 @@ class QStyleSheetItemData public: QStyleSheetItem::DisplayMode disp; int fontitalic; int fontunderline; + int fontstrikeout; int fontweight; int fontsize; int fontsizelog; int fontsizestep; @@ -77,68 +78,76 @@ public: } /*! - \class QStyleSheetItem qstylesheet.h - \ingroup text - \brief The QStyleSheetItem class provides an encapsulation of a set of text styles. - - A style sheet item consists of a name and a set of attributes that - specifiy its font, color, etc. When used in a \link QStyleSheet - style sheet\endlink (see styleSheet()), items define the name() of a - rich text tag and the display property changes associated with it. - - The \link QStyleSheetItem::DisplayMode display mode\endlink - attribute indicates whether the item is a block, an inline element - or a list element; see setDisplayMode(). The treatment of whitespace - is controlled by the \link QStyleSheetItem::WhiteSpaceMode white - space mode\endlink; see setWhiteSpaceMode(). An item's margins are - set with setMargin(), and line spacing is set with setLineSpacing(). - In the case of list items, the list style is set with - setListStyle(). An item may be a hypertext link anchor; see - setAnchor(). Other attributes are set with setAlignment(), - setVerticalAlignment(), setFontFamily(), setFontSize(), - setFontWeight(), setFontItalic(), setFontUnderline() and setColor(). + \class QStyleSheetItem qstylesheet.h + \brief The QStyleSheetItem class provides an encapsulation of a set of text styles. + + \ingroup text + + A style sheet item consists of a name and a set of attributes that + specifiy its font, color, etc. When used in a \link QStyleSheet + style sheet\endlink (see styleSheet()), items define the name() of + a rich text tag and the display property changes associated with + it. + + The \link QStyleSheetItem::DisplayMode display mode\endlink + attribute indicates whether the item is a block, an inline element + or a list element; see setDisplayMode(). The treatment of + whitespace is controlled by the \link + QStyleSheetItem::WhiteSpaceMode white space mode\endlink; see + setWhiteSpaceMode(). An item's margins are set with setMargin(), + In the case of list items, the list style is set with + setListStyle(). An item may be a hypertext link anchor; see + setAnchor(). Other attributes are set with setAlignment(), + setVerticalAlignment(), setFontFamily(), setFontSize(), + setFontWeight(), setFontItalic(), setFontUnderline(), + setFontStrikeOut and setColor(). */ /*! \enum QStyleSheetItem::AdditionalStyleValues \internal */ -/*! \enum QStyleSheetItem::WhiteSpaceMode +/*! + \enum QStyleSheetItem::WhiteSpaceMode - This enum defines the ways in which QStyleSheet can treat whitespace. There are three values at present: + This enum defines the ways in which QStyleSheet can treat + whitespace. - \value WhiteSpaceNormal any sequence of whitespace (including - line-breaks) is equivalent to a single space. + \value WhiteSpaceNormal any sequence of whitespace (including + line-breaks) is equivalent to a single space. - \value WhiteSpacePre whitespace must be output exactly as given - in the input. + \value WhiteSpacePre whitespace must be output exactly as given + in the input. - \value WhiteSpaceNoWrap multiple spaces are collapsed as with - WhiteSpaceNormal, but no automatic line-breaks occur. To break lines manually, - use the \c{<br>} tag. + \value WhiteSpaceNoWrap multiple spaces are collapsed as with + WhiteSpaceNormal, but no automatic line-breaks occur. To break + lines manually, use the \c{<br>} tag. */ -/*! \enum QStyleSheetItem::Margin - - \value MarginLeft left margin - \value MarginRight right margin - \value MarginTop top margin - \value MarginBottom bottom margin - \value MarginAll all margins (left, right, top and bottom) - \value MarginVertical top and bottom margins - \value MarginHorizontal left and right margins - \value MarginFirstLine margin (indentation) of the first line of a paragarph (in addition to the MarginLeft of the paragraph) +/*! + \enum QStyleSheetItem::Margin + + \value MarginLeft left margin + \value MarginRight right margin + \value MarginTop top margin + \value MarginBottom bottom margin + \value MarginAll all margins (left, right, top and bottom) + \value MarginVertical top and bottom margins + \value MarginHorizontal left and right margins + \value MarginFirstLine margin (indentation) of the first line of + a paragarph (in addition to the MarginLeft of the paragraph) */ /*! - Constructs a new style named \a name for the stylesheet \a parent. + Constructs a new style called \a name for the stylesheet \a + parent. - All properties in QStyleSheetItem are initially in the "do not change" state, - except \link QStyleSheetItem::DisplayMode display mode\endlink, which defaults - to \c DisplayInline. + All properties in QStyleSheetItem are initially in the "do not + change" state, except \link QStyleSheetItem::DisplayMode display + mode\endlink, which defaults to \c DisplayInline. */ QStyleSheetItem::QStyleSheetItem( QStyleSheet* parent, const QString& name ) { d = new QStyleSheetItemData; @@ -149,41 +158,42 @@ QStyleSheetItem::QStyleSheetItem( QStyleSheet* parent, const QString& name ) parent->insert( this ); } /*! - Copy constructor. Constructs a copy of \a other that is - not bound to any style sheet. - */ + Copy constructor. Constructs a copy of \a other that is not bound + to any style sheet. +*/ QStyleSheetItem::QStyleSheetItem( const QStyleSheetItem & other ) { d = new QStyleSheetItemData; *d = *other.d; } /*! - Destroys the style. Note that QStyleSheetItem objects become owned - by QStyleSheet when they are created. - */ + Destroys the style. Note that QStyleSheetItem objects become + owned by QStyleSheet when they are created. +*/ QStyleSheetItem::~QStyleSheetItem() { delete d; } /*! - Returns the style sheet this item is in. - */ + Returns the style sheet this item is in. +*/ QStyleSheet* QStyleSheetItem::styleSheet() { return d->sheet; } /*! \overload - Returns the style sheet this item is in. - */ + + Returns the style sheet this item is in. +*/ const QStyleSheet* QStyleSheetItem::styleSheet() const { return d->sheet; } @@ -197,8 +207,9 @@ void QStyleSheetItem::init() d->disp = DisplayInline; d->fontitalic = Undefined; d->fontunderline = Undefined; + d->fontstrikeout = Undefined; d->fontweight = Undefined; d->fontsize = Undefined; d->fontsizelog = Undefined; d->fontsizestep = 0; @@ -211,131 +222,135 @@ void QStyleSheetItem::init() d->margin[1] = Undefined; d->margin[2] = Undefined; d->margin[3] = Undefined; d->margin[4] = Undefined; - d->list = QStyleSheetItem::ListDisc; - d->whitespacemode = QStyleSheetItem::WhiteSpaceNormal; + d->list = (ListStyle) Undefined; + d->whitespacemode = (WhiteSpaceMode) Undefined; d->selfnest = TRUE; d->lineSpacing = Undefined; } /*! - Returns the name of the style item. + Returns the name of the style item. */ QString QStyleSheetItem::name() const { return d->stylename; } /*! - Returns the \link QStyleSheetItem::DisplayMode display mode\endlink - of the style. + Returns the \link QStyleSheetItem::DisplayMode display + mode\endlink of the style. - \sa setDisplayMode() - */ + \sa setDisplayMode() +*/ QStyleSheetItem::DisplayMode QStyleSheetItem::displayMode() const { return d->disp; } -/*! \enum QStyleSheetItem::DisplayMode +/*! + \enum QStyleSheetItem::DisplayMode - This enum type defines the way adjacent elements are displayed. The possible values are: + This enum type defines the way adjacent elements are displayed. - \value DisplayBlock elements are displayed as a rectangular block - (e.g. \c{<p>...</p>}). + \value DisplayBlock elements are displayed as a rectangular block + (e.g. \c{<p>...</p>}). - \value DisplayInline elements are displayed in a horizontally flowing - sequence (e.g. \c{<em>...</em>}). + \value DisplayInline elements are displayed in a horizontally + flowing sequence (e.g. \c{<em>...</em>}). - \value DisplayListItem elements are displayed in a vertical sequence - (e.g. \c{<li>...</li>}). + \value DisplayListItem elements are displayed in a vertical + sequence (e.g. \c{<li>...</li>}). - \value DisplayNone elements are not displayed at all. + \value DisplayNone elements are not displayed at all. */ /*! - Sets the display mode of the style to \a m. + Sets the display mode of the style to \a m. - \sa displayMode() + \sa displayMode() */ void QStyleSheetItem::setDisplayMode(DisplayMode m) { d->disp=m; } /*! - Returns the alignment of this style. Possible values are AlignAuto, AlignLeft, - AlignRight, AlignCenter and AlignJustify. + Returns the alignment of this style. Possible values are \c + AlignAuto, \c AlignLeft, \c AlignRight, \c AlignCenter or \c + AlignJustify. - \sa setAlignment(), Qt::AlignmentFlags - */ + \sa setAlignment(), Qt::AlignmentFlags +*/ int QStyleSheetItem::alignment() const { return d->align; } /*! - Sets the alignment to \a f. This only makes sense for styles with a - \link QStyleSheetItem::DisplayMode display mode\endlink of - DisplayBlock. Possible values are AlignAuto, AlignLeft, AlignRight, - AlignCenter and AlignJustify. + Sets the alignment to \a f. This only makes sense for styles with + a \link QStyleSheetItem::DisplayMode display mode\endlink of + DisplayBlock. Possible values are \c AlignAuto, \c AlignLeft, + \c AlignRight, \c AlignCenter or \c AlignJustify. - \sa alignment(), displayMode(), Qt::AlignmentFlags - */ + \sa alignment(), displayMode(), Qt::AlignmentFlags +*/ void QStyleSheetItem::setAlignment( int f ) { d->align = f; } /*! - Returns the vertical alignment of the style. Possible values are - VAlignBaseline, VAlignSub and VAlignSuper. + Returns the vertical alignment of the style. Possible values are + \c VAlignBaseline, \c VAlignSub or \c VAlignSuper. - psa setVerticalAlignment() - */ + \sa setVerticalAlignment() +*/ QStyleSheetItem::VerticalAlignment QStyleSheetItem::verticalAlignment() const { return d->valign; } -/*! \enum QStyleSheetItem::VerticalAlignment +/*! + \enum QStyleSheetItem::VerticalAlignment - This enum type defines the way elements are aligned vertically. This - is supported for text elements only. The possible values are: + This enum type defines the way elements are aligned vertically. + This is only supported for text elements. - \value VAlignBaseline align the baseline of the element (or the - bottom, if the element doesn't have a baseline) with the baseline of - the parent + \value VAlignBaseline align the baseline of the element (or the + bottom, if the element doesn't have a baseline) with the + baseline of the parent - \value VAlignSub subscript the element + \value VAlignSub subscript the element - \value VAlignSuper superscript the element + \value VAlignSuper superscript the element */ /*! - Sets the vertical alignment to \a valign. Possible values are - VAlignBaseline, VAlignSub and VAlignSuper. + Sets the vertical alignment to \a valign. Possible values are + \c VAlignBaseline, \c VAlignSub or \c VAlignSuper. - The vertical alignment property is not inherited. + The vertical alignment property is not inherited. - \sa verticalAlignment() - */ + \sa verticalAlignment() +*/ void QStyleSheetItem::setVerticalAlignment( VerticalAlignment valign ) { d->valign = valign; } /*! - Returns TRUE if the style sets an italic font; otherwise returns FALSE. + Returns TRUE if the style sets an italic font; otherwise returns + FALSE. - \sa setFontItalic(), definesFontItalic() - */ + \sa setFontItalic(), definesFontItalic() +*/ bool QStyleSheetItem::fontItalic() const { return d->fontitalic > 0; } @@ -343,167 +358,205 @@ bool QStyleSheetItem::fontItalic() const /*! If \a italic is TRUE sets italic for the style; otherwise sets upright. - \sa fontItalic(), definesFontItalic() - */ + \sa fontItalic(), definesFontItalic() +*/ void QStyleSheetItem::setFontItalic(bool italic) { d->fontitalic = italic?1:0; } /*! - Returns whether the style defines a font shape. A style - does not define any shape until setFontItalic() is called. + Returns TRUE if the style defines a font shape; otherwise returns + FALSE. A style does not define any shape until setFontItalic() is + called. - \sa setFontItalic(), fontItalic() - */ + \sa setFontItalic(), fontItalic() +*/ bool QStyleSheetItem::definesFontItalic() const { return d->fontitalic != Undefined; } /*! - Returns TRUE if the style sets an underlined font; otherwise returns FALSE. + Returns TRUE if the style sets an underlined font; otherwise + returns FALSE. - \sa setFontUnderline(), definesFontUnderline() - */ + \sa setFontUnderline(), definesFontUnderline() +*/ bool QStyleSheetItem::fontUnderline() const { return d->fontunderline > 0; } /*! - If \a underline is TRUE sets underline for the style; otherwise sets - no underline. + If \a underline is TRUE, sets underline for the style; otherwise + sets no underline. - \sa fontUnderline(), definesFontUnderline() - */ + \sa fontUnderline(), definesFontUnderline() +*/ void QStyleSheetItem::setFontUnderline(bool underline) { d->fontunderline = underline?1:0; } /*! - Returns whether the style defines a setting for the underline - property of the font. A style does not define this until - setFontUnderline() is called. + Returns TRUE if the style defines a setting for the underline + property of the font; otherwise returns FALSE. A style does not + define this until setFontUnderline() is called. - \sa setFontUnderline(), fontUnderline() */ + \sa setFontUnderline(), fontUnderline() +*/ bool QStyleSheetItem::definesFontUnderline() const { return d->fontunderline != Undefined; } /*! - Returns the font weight setting of the style. This is either a - valid QFont::Weight or the value QStyleSheetItem::Undefined. + Returns TRUE if the style sets a strike out font; otherwise + returns FALSE. - \sa setFontWeight(), QFont - */ + \sa setFontStrikeOut(), definesFontStrikeOut() +*/ +bool QStyleSheetItem::fontStrikeOut() const +{ + return d->fontstrikeout > 0; +} + +/*! + If \a strikeOut is TRUE, sets strike out for the style; otherwise + sets no strike out. + + \sa fontStrikeOut(), definesFontStrikeOut() +*/ +void QStyleSheetItem::setFontStrikeOut(bool strikeOut) +{ + d->fontstrikeout = strikeOut?1:0; +} + +/*! + Returns TRUE if the style defines a setting for the strikeOut + property of the font; otherwise returns FALSE. A style does not + define this until setFontStrikeOut() is called. + + \sa setFontStrikeOut(), fontStrikeOut() +*/ +bool QStyleSheetItem::definesFontStrikeOut() const +{ + return d->fontstrikeout != Undefined; +} + + +/*! + Returns the font weight setting of the style. This is either a + valid \c QFont::Weight or the value \c QStyleSheetItem::Undefined. + + \sa setFontWeight(), QFont +*/ int QStyleSheetItem::fontWeight() const { return d->fontweight; } /*! - Sets the font weight setting of the style to \a w. Valid values are - those defined by QFont::Weight. + Sets the font weight setting of the style to \a w. Valid values + are those defined by \c QFont::Weight. - \sa QFont, fontWeight() - */ + \sa QFont, fontWeight() +*/ void QStyleSheetItem::setFontWeight(int w) { d->fontweight = w; } /*! - Returns the logical font size setting of the style. This is either a valid - size between 1 and 7 or QStyleSheetItem::Undefined. + Returns the logical font size setting of the style. This is either + a valid size between 1 and 7 or \c QStyleSheetItem::Undefined. - \sa setLogicalFontSize(), setLogicalFontSizeStep(), QFont::pointSize(), QFont::setPointSize() - */ + \sa setLogicalFontSize(), setLogicalFontSizeStep(), QFont::pointSize(), QFont::setPointSize() +*/ int QStyleSheetItem::logicalFontSize() const { return d->fontsizelog; } /*! - Sets the logical font size setting of the style to \a s. - Valid logical sizes are 1 to 7. + Sets the logical font size setting of the style to \a s. Valid + logical sizes are 1 to 7. - \sa logicalFontSize(), QFont::pointSize(), QFont::setPointSize() - */ + \sa logicalFontSize(), QFont::pointSize(), QFont::setPointSize() +*/ void QStyleSheetItem::setLogicalFontSize(int s) { d->fontsizelog = s; } /*! - Returns the logical font size step of this style. + Returns the logical font size step of this style. - The default is 0. Tags such as \c big define \c +1; \c small defines - \c -1. + The default is 0. Tags such as \c big define \c +1; \c small + defines \c -1. - \sa setLogicalFontSizeStep() - */ + \sa setLogicalFontSizeStep() +*/ int QStyleSheetItem::logicalFontSizeStep() const { return d->fontsizestep; } /*! - Sets the logical font size step of this style to \a s. + Sets the logical font size step of this style to \a s. - \sa logicalFontSizeStep() - */ + \sa logicalFontSizeStep() +*/ void QStyleSheetItem::setLogicalFontSizeStep( int s ) { d->fontsizestep = s; } /*! - Sets the font size setting of the style to \a s points. + Sets the font size setting of the style to \a s points. - \sa fontSize(), QFont::pointSize(), QFont::setPointSize() - */ + \sa fontSize(), QFont::pointSize(), QFont::setPointSize() +*/ void QStyleSheetItem::setFontSize(int s) { d->fontsize = s; } /*! - Returns the font size setting of the style. This is either a valid - point size or QStyleSheetItem::Undefined. + Returns the font size setting of the style. This is either a valid + point size or \c QStyleSheetItem::Undefined. - \sa setFontSize(), QFont::pointSize(), QFont::setPointSize() - */ + \sa setFontSize(), QFont::pointSize(), QFont::setPointSize() +*/ int QStyleSheetItem::fontSize() const { return d->fontsize; } /*! - Returns the font family setting of the style. This is either a valid - font family or QString::null if no family has been set. + Returns the font family setting of the style. This is either a + valid font family or QString::null if no family has been set. - \sa setFontFamily(), QFont::family(), QFont::setFamily() - */ + \sa setFontFamily(), QFont::family(), QFont::setFamily() +*/ QString QStyleSheetItem::fontFamily() const { return d->fontfamily; } /*! - Sets the font family setting of the style to \a fam. + Sets the font family setting of the style to \a fam. - \sa fontFamily(), QFont::family(), QFont::setFamily() - */ + \sa fontFamily(), QFont::family(), QFont::setFamily() +*/ void QStyleSheetItem::setFontFamily( const QString& fam) { d->fontfamily = fam; } @@ -521,9 +574,9 @@ int QStyleSheetItem::numberOfColumns() const } /*!\obsolete - Sets the number of columns for this style. Elements in the style + Sets the number of columns for this style. Elements in the style are divided into columns. This makes sense only if the style uses a block display mode (see QStyleSheetItem::DisplayMode). @@ -537,95 +590,96 @@ void QStyleSheetItem::setNumberOfColumns(int ncols) } /*! - Returns the text color of this style or an invalid color - if no color has been set. + Returns the text color of this style or an invalid color if no + color has been set. - \sa setColor() QColor::isValid() - */ + \sa setColor() QColor::isValid() +*/ QColor QStyleSheetItem::color() const { return d->col; } /*! - Sets the text color of this style to \a c. + Sets the text color of this style to \a c. - \sa color() - */ + \sa color() +*/ void QStyleSheetItem::setColor( const QColor &c) { d->col = c; } /*! - Returns whether this style is an anchor. + Returns whether this style is an anchor. - \sa setAnchor() - */ + \sa setAnchor() +*/ bool QStyleSheetItem::isAnchor() const { return d->anchor; } /*! - If \a anc is TRUE sets this style to be an anchor (hypertext link); - otherwise sets it to not be an anchor. Elements in this style have - connections to other documents or anchors. + If \a anc is TRUE, sets this style to be an anchor (hypertext + link); otherwise sets it to not be an anchor. Elements in this + style link to other documents or anchors. - \sa isAnchor() - */ + \sa isAnchor() +*/ void QStyleSheetItem::setAnchor(bool anc) { d->anchor = anc; } /*! - Returns the whitespace mode. + Returns the whitespace mode. - \sa setWhiteSpaceMode() WhiteSpaceMode - */ + \sa setWhiteSpaceMode() WhiteSpaceMode +*/ QStyleSheetItem::WhiteSpaceMode QStyleSheetItem::whiteSpaceMode() const { return d->whitespacemode; } /*! - Sets the whitespace mode to \a m. - \sa WhiteSpaceMode - */ + Sets the whitespace mode to \a m. + + \sa WhiteSpaceMode +*/ void QStyleSheetItem::setWhiteSpaceMode(WhiteSpaceMode m) { d->whitespacemode = m; } /*! - Returns the width of margin \a m in pixels. + Returns the width of margin \a m in pixels. - The margin, \a m, can be \c MarginLeft, \c MarginRight, - \c MarginTop, \c MarginBottom, \c MarginAll, \c MarginVertical or \c - MarginHorizontal. + The margin, \a m, can be \c MarginLeft, \c MarginRight, \c + MarginTop, \c MarginBottom, \c MarginAll, \c MarginVertical or \c + MarginHorizontal. - \sa setMargin() Margin - */ + \sa setMargin() Margin +*/ int QStyleSheetItem::margin(Margin m) const { return d->margin[m]; } /*! - Sets the width of margin \a m to \a v pixels. + Sets the width of margin \a m to \a v pixels. - The margin, \a m, can be \c MarginLeft, \c MarginRight, - \c MarginTop, \c MarginBottom, \c MarginAll, \c MarginVertical or \c - MarginHorizontal. The value \a v must be >= 0. + The margin, \a m, can be \c MarginLeft, \c MarginRight, \c + MarginTop, \c MarginBottom, \c MarginAll, \c MarginVertical or \c + MarginHorizontal. The value \a v must be >= 0. - \sa margin() - */ + \sa margin() +*/ void QStyleSheetItem::setMargin(Margin m, int v) { if (m == MarginAll ) { d->margin[0] = v; @@ -645,73 +699,76 @@ void QStyleSheetItem::setMargin(Margin m, int v) } /*! - Returns the list style of the style. + Returns the list style of the style. - \sa setListStyle() ListStyle + \sa setListStyle() ListStyle */ QStyleSheetItem::ListStyle QStyleSheetItem::listStyle() const { return d->list; } -/*! \enum QStyleSheetItem::ListStyle +/*! + \enum QStyleSheetItem::ListStyle - This enum type defines how the items in a list are prefixed when - displayed. The currently defined values are: + This enum type defines how the items in a list are prefixed when + displayed. - \value ListDisc a filled circle (i.e. a bullet) - \value ListCircle an unfilled circle - \value ListSquare a filled square - \value ListDecimal an integer in base 10: \e 1, \e 2, \e 3, ... - \value ListLowerAlpha a lowercase letter: \e a, \e b, \e c, ... - \value ListUpperAlpha an uppercase letter: \e A, \e B, \e C, ... + \value ListDisc a filled circle (i.e. a bullet) + \value ListCircle an unfilled circle + \value ListSquare a filled square + \value ListDecimal an integer in base 10: \e 1, \e 2, \e 3, ... + \value ListLowerAlpha a lowercase letter: \e a, \e b, \e c, ... + \value ListUpperAlpha an uppercase letter: \e A, \e B, \e C, ... */ + /*! - Sets the list style of the style to \a s. + Sets the list style of the style to \a s. - This is used by nested elements that have a display mode of - \c DisplayListItem. + This is used by nested elements that have a display mode of \c + DisplayListItem. - \sa listStyle() DisplayMode ListStyle - */ + \sa listStyle() DisplayMode ListStyle +*/ void QStyleSheetItem::setListStyle(ListStyle s) { d->list=s; } -/*! Returns a space-separated list of names of styles that may - contain elements of this style. If nothing has been set, contexts() - returns an empty string, which indicates that this style can be - nested everywhere. +/*! + Returns a space-separated list of names of styles that may contain + elements of this style. If nothing has been set, contexts() + returns an empty string, which indicates that this style can be + nested everywhere. - \sa setContexts() - */ + \sa setContexts() +*/ QString QStyleSheetItem::contexts() const { return d->contxt; } /*! - Sets a space-separated list of names of styles that may contain - elements of this style. If \a c is empty, the style can be nested - everywhere. + Sets a space-separated list of names of styles that may contain + elements of this style. If \a c is empty, the style can be nested + everywhere. - \sa contexts() - */ + \sa contexts() +*/ void QStyleSheetItem::setContexts( const QString& c) { d->contxt = QChar(' ') + c + QChar(' '); } /*! - Returns TRUE if this style can be nested into an element - of style \a s; otherwise returns FALSE. + Returns TRUE if this style can be nested into an element of style + \a s; otherwise returns FALSE. - \sa contexts(), setContexts() - */ + \sa contexts(), setContexts() +*/ bool QStyleSheetItem::allowedInContext( const QStyleSheetItem* s) const { if ( d->contxt.isEmpty() ) return TRUE; @@ -719,40 +776,52 @@ bool QStyleSheetItem::allowedInContext( const QStyleSheetItem* s) const } /*! - Returns TRUE if this style has self-nesting enabled; otherwise - returns FALSE. + Returns TRUE if this style has self-nesting enabled; otherwise + returns FALSE. - \sa setSelfNesting() - */ + \sa setSelfNesting() +*/ bool QStyleSheetItem::selfNesting() const { return d->selfnest; } /*! - Sets the self-nesting property for this style to \a nesting. + Sets the self-nesting property for this style to \a nesting. - In order to support "dirty" HTML, paragraphs \c{<p>} and list items - \c{<li>} are not self-nesting. This means that starting a new - paragraph or list item automatically closes the previous one. + In order to support "dirty" HTML, paragraphs \c{<p>} and list + items \c{<li>} are not self-nesting. This means that starting a + new paragraph or list item automatically closes the previous one. - \sa selfNesting() - */ + \sa selfNesting() +*/ void QStyleSheetItem::setSelfNesting( bool nesting ) { d->selfnest = nesting; } -/*! Sets the linespacing to be \a ls pixels */ +/* + Sets the linespacing to be at least \a ls pixels. + + For compatibility with previous Qt releases, small values get + treated differently: If \a ls is smaller than the default font + line spacing in pixels at parse time, the resulting line spacing + is the sum of the default line spacing plus \a ls. We recommend + not relying on this behavior. +*/ void QStyleSheetItem::setLineSpacing( int ls ) { d->lineSpacing = ls; } -/*! Returns the linespacing */ +/*! + \obsolete + + Returns the linespacing +*/ int QStyleSheetItem::lineSpacing() const { return d->lineSpacing; @@ -766,236 +835,234 @@ int QStyleSheetItem::lineSpacing() const //************************************************************************ /*! - \class QStyleSheet qstylesheet.h - \ingroup text - \brief The QStyleSheet class is a collection of styles for rich text - rendering and a generator of tags. - - \ingroup graphics - \ingroup helpsystem - - By creating QStyleSheetItem objects for a style sheet you build a - definition of a set of tags. This definition will be used by the - internal rich text rendering system to parse and display text - documents to which the style sheet applies. Rich text is normally - visualized in a QTextView or a QTextBrowser. However, QLabel, - QWhatsThis and QMessageBox also support it, and other classes are - likely to follow. With QSimpleRichText it is possible to use the - rich text renderer for custom widgets as well. - - The default QStyleSheet object has the following style bindings, - sorted by structuring bindings, anchors, character style bindings - (i.e. inline styles), special elements such as horizontal lines or - images, and other tags. In addition, rich text supports simple HTML - tables. - - The structuring tags are - \list - \i \c{<qt>}...\c{</qt>} - - A Qt rich text document. It understands the following attributes: - \list - \i title - - The caption of the document. This attribute is easily accessible with - QTextView::documentTitle(). - \i type - - The type of the document. The default type is \c page . It - indicates that the document is displayed in a page of its - own. Another style is \c detail, which can be used to - explain certain expressions in more detail in a few - sentences. The QTextBrowser will then keep the current page - and display the new document in a small popup similar to - QWhatsThis. Note that links will not work in documents with - \c{<qt type="detail">...</qt>}. - \i bgcolor - - The background color, for example \c bgcolor="yellow" or \c - bgcolor="#0000FF". - \i background - - The background pixmap, for example \c - background="granit.xpm". The pixmap name will be resolved by - a QMimeSourceFactory(). - \i text - - The default text color, for example \c text="red". - \i link - - The link color, for example \c link="green". - \endlist - \i \c{<h1>...</h1>} - - A top-level heading. - \i \c{<h2>...</h2>} - - A sublevel heading. - \i \c{<h3>...</h3>} - - A sub-sublevel heading. - \i \c{<p>...</p>} - - A left-aligned paragraph. Adjust the alignment with - the \c align attribute. Possible values are - \c left, \c right and \c center. - \i \c{<center>...</center>} - - A centered paragraph. - \i \c{<blockquote>...</blockquote>} - - An indented paragraph that is useful for quotes. - \i \c{<ul>...</ul>} - - An unordered list. You can also pass a type argument to - define the bullet style. The default is \c type=disc; other - types are \c circle and \c square. - \i \c{<ol>...</ol>} - - An ordered list. You can also pass a type argument to define - the enumeration label style. The default is \c type="1"; other - types are \c "a" and \c "A". - \i <tt><li></tt>...<tt></li></tt> - - A list item. This tag can be used only within the context of - \c ol or \c ul. - \i \c{<pre>...</pre>} - - For larger chunks of code. Whitespaces in the contents are preserved. - For small bits of code use the inline-style \c code. - \endlist - - Anchors and links are done with a single tag: - \list - \i \c{<a>...</a>} - - An anchor or link. The reference target is defined in the \c - href attribute of the tag as in \c{<a - href="target.qml">...</a>}. You can also specify an - additional anchor within the specified target document, for - example \c{<a href="target.qml#123">...</a>}. If \c a is - meant to be an anchor, the reference source is given in the - \c name attribute. - \endlist - - The default character style bindings are - \list - \i \c{<em>...</em>} - - Emphasized. By default this is the same as - \c{<i>...</i>} (italic). - \i \c{<strong>...</strong>} - - Strong. By default this is the same as - \c{<b>...</b>} (bold). - \i \c{<i>...</i>} - - Italic font style. - \i \c{<b>...</b>} - - Bold font style. - \i \c{<u>...</u>} - - Underlined font style. - \i \c{<big>...</big>} - - A larger font size. - \i \c{<small>...</small>} - - A smaller font size. - \i \c{<code>...</code>} - - Indicates code. By default this is the same as - \c{<tt>...</tt>} (typewriter). For - larger junks of code use the block-tag \c pre. - \i \c{<tt>...</tt>} - - Typewriter font style. - \i \c{<font>...</font>} - - Customizes the font size, family and text color. The tag understands - the following attributes: - \list - \i color - - The text color, for example \c color="red" or \c color="#FF0000". - \i size - - The logical size of the font. Logical sizes 1 to 7 are supported. - The value may either be absolute (for example, - \c size=3) or relative (\c size=-2). In the latter case the sizes - are simply added. - \i face - - The family of the font, for example \c face=times. - \endlist - \endlist - - Special elements are: - \list - \i \c{<img>} - - An image. The image name for the mime source - factory is given in the source attribute, for example - \c{<img src="qt.xpm">} - The image tag also understands the attributes \c width and \c - height that determine the size of the image. If the pixmap - does not fit the specified size it will be scaled - automatically (by using QImage::smoothScale()). - - The \c align attribute determines where the image is - placed. By default, an image is placed inline just like a - normal character. Specify \c left or \c right to place the - image at the respective side. - \i \c{<hr>} - - A horizonal line. - \i \c{<br>} - - A line break. - \endlist - - Another tag not in any of the above cathegories is - \list - \i \c{<nobr>...</nobr>} - - No break. Prevents word wrap. - \endlist - - In addition, rich text supports simple HTML tables. A table consists - of one or more rows each of which contains one or more cells. Cells - are either data cells or header cells, depending on their - content. Cells which span rows and columns are supported. - - \list - \i \c{<table>...</table>} - - A table. Tables support the following attributes: - \list - \i bgcolor - - The background color. - \i width - - The table width. This is either an absolute pixel width or a relative - percentage of the table's width, for example \c width=80%. - \i border - - The width of the table border. The default is 0 (= no border). - \i cellspacing - - Additional space around the table cells. The default is 2. - \i cellpadding - - Additional space around the contents of table cells. The default is 1. - \endlist - \i \c{<tr>...</tr>} - - A table row. This is only valid within a \c table. Rows support - the following attribute: - \list - \i bgcolor - - The background color. - \endlist - \i \c{<th>...</th>} - - A table header cell. Similar to \c td, but defaults to center alignment - and a bold font. - \i \c{<td>...</td>} - - A table data cell. This is only valid within a \c tr. Cells - support the following attributes: - \list - \i bgcolor - - The background color. - \i width - - The cell width. This is either an absolute pixel width or a relative - percentage of table's width, for example \c width=50%. - \i colspan - - Specifies how many columns this cell spans. The default is 1. - \i rowspan - - Specifies how many rows this cell spans. The default is 1. - \i align - - Alignment; possible values are \c left, \c right, and \c center. The - default is left. - \endlist - \endlist -*/ - -/*! - Creates a style sheet with parent \a parent and name \a name. Like - any QObject it will be deleted when its parent is - destroyed (if the child still exists). - - By default the style sheet has the tag definitions defined above. + \class QStyleSheet qstylesheet.h + \ingroup text + \brief The QStyleSheet class is a collection of styles for rich text + rendering and a generator of tags. + + \ingroup graphics + \ingroup helpsystem + + By creating QStyleSheetItem objects for a style sheet you build a + definition of a set of tags. This definition will be used by the + internal rich text rendering system to parse and display text + documents to which the style sheet applies. Rich text is normally + visualized in a QTextView or a QTextBrowser. However, QLabel, + QWhatsThis and QMessageBox also support it, and other classes are + likely to follow. With QSimpleRichText it is possible to use the + rich text renderer for custom widgets as well. + + The default QStyleSheet object has the following style bindings, + sorted by structuring bindings, anchors, character style bindings + (i.e. inline styles), special elements such as horizontal lines or + images, and other tags. In addition, rich text supports simple + HTML tables. + + The structuring tags are + \table + \header \i Structuring tags \i Notes + \row \i \c{<qt>}...\c{</qt>} + \i A Qt rich text document. It understands the following + attributes: + \list + \i \c title -- The caption of the document. This attribute is + easily accessible with QTextView::documentTitle(). + \i \c type -- The type of the document. The default type is \c + page. It indicates that the document is displayed in a + page of its own. Another style is \c detail, which can be + used to explain certain expressions in more detail in a + few sentences. For \c detail, QTextBrowser will then keep + the current page and display the new document in a small + popup similar to QWhatsThis. Note that links will not work + in documents with \c{<qt type="detail">...</qt>}. + \i \c bgcolor -- The background color, for example \c + bgcolor="yellow" or \c bgcolor="#0000FF". + \i \c background -- The background pixmap, for example \c + background="granite.xpm". The pixmap name will be resolved + by a QMimeSourceFactory(). + \i \c text -- The default text color, for example \c text="red". + \i \c link -- The link color, for example \c link="green". + \endlist + \row \i \c{<h1>...</h1>} + \i A top-level heading. + \row \i \c{<h2>...</h2>} + \i A sublevel heading. + \row \i \c{<h3>...</h3>} + \i A sub-sublevel heading. + \row \i \c{<p>...</p>} + \i A left-aligned paragraph. Adjust the alignment with the \c + align attribute. Possible values are \c left, \c right and + \c center. + \row \i \c{<center>...}<br>\c{</center>} + \i A centered paragraph. + \row \i \c{<blockquote>...}<br>\c{</blockquote>} + \i An indented paragraph that is useful for quotes. + \row \i \c{<ul>...</ul>} + \i An unordered list. You can also pass a type argument to + define the bullet style. The default is \c type=disc; + other types are \c circle and \c square. + \row \i \c{<ol>...</ol>} + \i An ordered list. You can also pass a type argument to + define the enumeration label style. The default is \c + type="1"; other types are \c "a" and \c "A". + \row \i \c{<li>...</li>} + \i A list item. This tag can be used only within the context + of \c{<ol>} or \c{<ul>}. + \row \i \c{<pre>...</pre>} + \i For larger chunks of code. Whitespaces in the contents are + preserved. For small bits of code use the inline-style \c + code. + \endtable + + Anchors and links are done with a single tag: + \table + \header \i Anchor tags \i Notes + \row \i \c{<a>...</a>} + \i An anchor or link. + \list + \i A link is created by using an \c href + attribute, for example + <br>\c{<a href="target.qml">Link Text</a>}. Links to + targets within a document are achieved in the same way + as for HTML, e.g. + <br>\c{<a href="target.qml#subtitle">Link Text</a>}. + \i A target is created by using a \c name + attribute, for example + <br>\c{<a name="subtitle"><h2>Sub Title</h2></a>}. + \endlist + \endtable + + The default character style bindings are + \table + \header \i Style tags \i Notes + \row \i \c{<em>...</em>} + \i Emphasized. By default this is the same as \c{<i>...</i>} + (italic). + \row \i \c{<strong>...</strong>} + \i Strong. By default this is the same as \c{<b>...</b>} + (bold). + \row \i \c{<i>...</i>} + \i Italic font style. + \row \i \c{<b>...</b>} + \i Bold font style. + \row \i \c{<u>...</u>} + \i Underlined font style. + \row \i \c{<s>...</s>} + \i Strike out font style. + \row \i \c{<big>...</big>} + \i A larger font size. + \row \i \c{<small>...</small>} + \i A smaller font size. + \row \i \c{<code>...</code>} + \i Indicates code. By default this is the same as + \c{<tt>...</tt>} (typewriter). For larger junks of code + use the block-tag \c{<}\c{pre>}. + \row \i \c{<tt>...</tt>} + \i Typewriter font style. + \row \i \c{<font>...</font>} + \i Customizes the font size, family and text color. The tag + understands the following attributes: + \list + \i \c color -- The text color, for example \c color="red" or + \c color="#FF0000". + \i \c size -- The logical size of the font. Logical sizes 1 + to 7 are supported. The value may either be absolute + (for example, \c size=3) or relative (\c size=-2). In + the latter case the sizes are simply added. + \i \c face -- The family of the font, for example \c face=times. + \endlist + \endtable + + Special elements are: + \table + \header \i Special tags \i Notes + \row \i \c{<img>} + \i An image. The image name for the mime source factory is + given in the source attribute, for example + \c{<img src="qt.xpm">} The image tag also understands the + attributes \c width and \c height that determine the size + of the image. If the pixmap does not fit the specified + size it will be scaled automatically (by using + QImage::smoothScale()). + <br> + The \c align attribute determines where the image is + placed. By default, an image is placed inline just like a + normal character. Specify \c left or \c right to place the + image at the respective side. + \row \i \c{<hr>} + \i A horizonal line. + \row \i \c{<br>} + \i A line break. + \row \i \c{<nobr>...</nobr>} + \i No break. Prevents word wrap. + \endtable + + In addition, rich text supports simple HTML tables. A table + consists of one or more rows each of which contains one or more + cells. Cells are either data cells or header cells, depending on + their content. Cells which span rows and columns are supported. + + \table + \header \i Table tags \i Notes + \row \i \c{<table>...</table>} + \i A table. Tables support the following attributes: + \list + \i \c bgcolor -- The background color. + \i \c width -- The table width. This is either an absolute + pixel width or a relative percentage of the table's + width, for example \c width=80%. + \i \c border -- The width of the table border. The default is + 0 (= no border). + \i \c cellspacing -- Additional space around the table cells. + The default is 2. + \i \c cellpadding -- Additional space around the contents of + table cells. The default is 1. + \endlist + \row \i \c{<tr>...</tr>} + \i A table row. This is only valid within a \c table. Rows + support the following attribute: + \list + \i \c bgcolor -- The background color. + \endlist + \row \i \c{<th>...</th>} + \i A table header cell. Similar to \c td, but defaults to + center alignment and a bold font. + \row \i \c{<td>...</td>} + \i A table data cell. This is only valid within a \c tr. + Cells support the following attributes: + \list + \i \c bgcolor -- The background color. + \i \c width -- The cell width. This is either an absolute + pixel width or a relative percentage of table's width, + for example \c width=50%. + \i \c colspan -- Specifies how many columns this cell spans. + The default is 1. + \i \c rowspan -- Specifies how many rows this cell spans. The + default is 1. + \i \c align -- Alignment; possible values are \c left, \c + right, and \c center. The default is left. + \endlist + \endtable +*/ + +/*! + Creates a style sheet called \a name, with parent \a parent. Like + any QObject it will be deleted when its parent is destroyed (if + the child still exists). + + By default the style sheet has the tag definitions defined above. */ QStyleSheet::QStyleSheet( QObject *parent, const char *name ) : QObject( parent, name ) { init(); } /*! - Destroys the style sheet. All styles inserted into the style sheet - will be deleted. + Destroys the style sheet. All styles inserted into the style sheet + will be deleted. */ QStyleSheet::~QStyleSheet() { } @@ -1017,9 +1084,8 @@ void QStyleSheet::init() style->setDisplayMode( QStyleSheetItem::DisplayBlock ); style = new QStyleSheetItem( this, QString::fromLatin1("qt") ); style->setDisplayMode( QStyleSheetItem::DisplayBlock ); - //style->setMargin( QStyleSheetItem::MarginAll, 4 ); style = new QStyleSheetItem( this, QString::fromLatin1("a") ); style->setAnchor( TRUE ); @@ -1046,42 +1112,41 @@ void QStyleSheet::init() style = new QStyleSheetItem( this, QString::fromLatin1("h1") ); style->setFontWeight( QFont::Bold); style->setLogicalFontSize(6); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginTop, 12); - style-> setMargin(QStyleSheetItem::MarginBottom, 6); + style-> setMargin(QStyleSheetItem::MarginTop, 18); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); style = new QStyleSheetItem( this, QString::fromLatin1("h2") ); style->setFontWeight( QFont::Bold); style->setLogicalFontSize(5); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginTop, 10); - style-> setMargin(QStyleSheetItem::MarginBottom, 5); + style-> setMargin(QStyleSheetItem::MarginTop, 16); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); style = new QStyleSheetItem( this, QString::fromLatin1("h3") ); style->setFontWeight( QFont::Bold); style->setLogicalFontSize(4); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginTop, 8); - style-> setMargin(QStyleSheetItem::MarginBottom, 4); + style-> setMargin(QStyleSheetItem::MarginTop, 14); + style-> setMargin(QStyleSheetItem::MarginBottom, 12); style = new QStyleSheetItem( this, QString::fromLatin1("h4") ); style->setFontWeight( QFont::Bold); style->setLogicalFontSize(3); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginTop, 8); - style-> setMargin(QStyleSheetItem::MarginBottom, 4); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); style = new QStyleSheetItem( this, QString::fromLatin1("h5") ); style->setFontWeight( QFont::Bold); style->setLogicalFontSize(2); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginTop, 8); + style-> setMargin(QStyleSheetItem::MarginTop, 12); style-> setMargin(QStyleSheetItem::MarginBottom, 4); style = new QStyleSheetItem( this, QString::fromLatin1("p") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginVertical, 8); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); style->setSelfNesting( FALSE ); style = new QStyleSheetItem( this, QString::fromLatin1("center") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock); @@ -1096,20 +1161,21 @@ void QStyleSheet::init() (void) new QStyleSheetItem( this, QString::fromLatin1("font") ); style = new QStyleSheetItem( this, QString::fromLatin1("ul") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock); - style-> setMargin(QStyleSheetItem::MarginVertical, 4); + style->setListStyle( QStyleSheetItem::ListDisc ); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + style->setMargin( QStyleSheetItem::MarginLeft, 40 ); style = new QStyleSheetItem( this, QString::fromLatin1("ol") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock); style->setListStyle( QStyleSheetItem::ListDecimal ); - style-> setMargin(QStyleSheetItem::MarginVertical, 4); + style-> setMargin(QStyleSheetItem::MarginVertical, 12); + style->setMargin( QStyleSheetItem::MarginLeft, 40 ); style = new QStyleSheetItem( this, QString::fromLatin1("li") ); style->setDisplayMode(QStyleSheetItem::DisplayListItem); style->setSelfNesting( FALSE ); - style->setContexts(QString::fromLatin1("ol ul")); - style-> setMargin(QStyleSheetItem::MarginVertical, 4); style = new QStyleSheetItem( this, QString::fromLatin1("code") ); style->setFontFamily( QString::fromLatin1("courier") ); @@ -1118,8 +1184,9 @@ void QStyleSheet::init() new QStyleSheetItem(this, QString::fromLatin1("img")); new QStyleSheetItem(this, QString::fromLatin1("br")); new QStyleSheetItem(this, QString::fromLatin1("hr")); + style = new QStyleSheetItem(this, QString::fromLatin1("sub")); style->setVerticalAlignment( QStyleSheetItem::VAlignSub ); style = new QStyleSheetItem(this, QString::fromLatin1("sup")); style->setVerticalAlignment( QStyleSheetItem::VAlignSuper ); @@ -1134,11 +1201,15 @@ void QStyleSheet::init() style->setMargin(QStyleSheetItem::MarginHorizontal, 40 ); style = new QStyleSheetItem( this, QString::fromLatin1("head") ); style->setDisplayMode(QStyleSheetItem::DisplayNone); + style = new QStyleSheetItem( this, QString::fromLatin1("body") ); + style->setDisplayMode(QStyleSheetItem::DisplayBlock); style = new QStyleSheetItem( this, QString::fromLatin1("div") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock) ; + style = new QStyleSheetItem( this, QString::fromLatin1("span") ); style = new QStyleSheetItem( this, QString::fromLatin1("dl") ); + style-> setMargin(QStyleSheetItem::MarginVertical, 8); style->setDisplayMode(QStyleSheetItem::DisplayBlock); style = new QStyleSheetItem( this, QString::fromLatin1("dt") ); style->setDisplayMode(QStyleSheetItem::DisplayBlock); style->setContexts(QString::fromLatin1("dl") ); @@ -1147,12 +1218,17 @@ void QStyleSheet::init() style->setMargin(QStyleSheetItem::MarginLeft, 30); style->setContexts(QString::fromLatin1("dt dl") ); style = new QStyleSheetItem( this, QString::fromLatin1("u") ); style->setFontUnderline( TRUE); + style = new QStyleSheetItem( this, QString::fromLatin1("s") ); + style->setFontStrikeOut( TRUE); style = new QStyleSheetItem( this, QString::fromLatin1("nobr") ); style->setWhiteSpaceMode( QStyleSheetItem::WhiteSpaceNoWrap ); - style = new QStyleSheetItem( this, QString::fromLatin1("wsp") ); // qt extension for QTextEdit - style->setWhiteSpaceMode( (QStyleSheetItem::WhiteSpaceMode) 3 ); // WhiteSpaceModeNoCompression + + // compatibily with some minor 3.0.x Qt versions that had an + // undocumented <wsp> tag. ### Remove 3.1 + style = new QStyleSheetItem( this, QString::fromLatin1("wsp") ); + style->setWhiteSpaceMode( QStyleSheetItem::WhiteSpacePre ); // tables style = new QStyleSheetItem( this, QString::fromLatin1("table") ); style = new QStyleSheetItem( this, QString::fromLatin1("tr") ); @@ -1172,16 +1248,16 @@ void QStyleSheet::init() static QStyleSheet* defaultsheet = 0; static QSingleCleanupHandler<QStyleSheet> qt_cleanup_stylesheet; /*! - Returns the application-wide default style sheet. This style sheet is - used by rich text rendering classes such as QSimpleRichText, - QWhatsThis and QMessageBox to define the rendering style and - available tags within rich text documents. It serves also as initial - style sheet for the more complex render widgets QTextEdit and - QTextBrowser. - - \sa setDefaultSheet() + Returns the application-wide default style sheet. This style sheet + is used by rich text rendering classes such as QSimpleRichText, + QWhatsThis and QMessageBox to define the rendering style and + available tags within rich text documents. It also serves as the + initial style sheet for the more complex render widgets, QTextEdit + and QTextBrowser. + + \sa setDefaultSheet() */ QStyleSheet* QStyleSheet::defaultSheet() { if (!defaultsheet) { @@ -1191,13 +1267,13 @@ QStyleSheet* QStyleSheet::defaultSheet() return defaultsheet; } /*! - Sets the application-wide default style sheet to \a sheet, deleting - any style sheet previously set. The ownership is transferred to - QStyleSheet. + Sets the application-wide default style sheet to \a sheet, + deleting any style sheet previously set. The ownership is + transferred to QStyleSheet. - \sa defaultSheet() + \sa defaultSheet() */ void QStyleSheet::setDefaultSheet( QStyleSheet* sheet) { if ( defaultsheet != sheet ) { @@ -1210,10 +1286,10 @@ void QStyleSheet::setDefaultSheet( QStyleSheet* sheet) qt_cleanup_stylesheet.set( &defaultsheet ); } /*!\internal - Inserts \a style. Any tags generated after this time will be - bound to this style. Note that \a style becomes owned by the + Inserts \a style. Any tags generated after this time will be + bound to this style. Note that \a style becomes owned by the style sheet and will be deleted when the style sheet is destroyed. */ void QStyleSheet::insert( QStyleSheetItem* style ) { @@ -1221,10 +1297,10 @@ void QStyleSheet::insert( QStyleSheetItem* style ) } /*! - Returns the style with name \a name or 0 if there is no such style. - */ + Returns the style called \a name or 0 if there is no such style. +*/ QStyleSheetItem* QStyleSheet::item( const QString& name) { if ( name.isNull() ) return 0; @@ -1232,10 +1308,12 @@ QStyleSheetItem* QStyleSheet::item( const QString& name) } /*! \overload - Returns the style with name \a name or 0 if there is no such style (const version) - */ + + Returns the style called \a name or 0 if there is no such style + (const version) +*/ const QStyleSheetItem* QStyleSheet::item( const QString& name) const { if ( name.isNull() ) return 0; @@ -1244,98 +1322,107 @@ const QStyleSheetItem* QStyleSheet::item( const QString& name) const /*! \preliminary - Generates an internal object for the tag called \a name, given the - attributes \a attr, and using additional information provided - by the mime source factory \a factory. - \a context is the optional context of the document, i.e. the path to - look for relative links. This becomes important if the text contains - relative references, for example within image tags. QSimpleRichText - always uses the default mime source factory (see - \l{QMimeSourceFactory::defaultFactory()}) to resolve these references. - The context will then be used to calculate the absolute path. See - QMimeSourceFactory::makeAbsolute() for details. + Generates an internal object for the tag called \a name, given the + attributes \a attr, and using additional information provided by + the mime source factory \a factory. - \a emptyTag and \a doc are for internal use only. + \a context is the optional context of the document, i.e. the path + to look for relative links. This becomes important if the text + contains relative references, for example within image tags. + QSimpleRichText always uses the default mime source factory (see + \l{QMimeSourceFactory::defaultFactory()}) to resolve these + references. The context will then be used to calculate the + absolute path. See QMimeSourceFactory::makeAbsolute() for details. - This function should not (yet) be used in application code. + \a emptyTag and \a doc are for internal use only. + + This function should not be used in application code. */ QTextCustomItem* QStyleSheet::tag( const QString& name, const QMap<QString, QString> &attr, const QString& context, const QMimeSourceFactory& factory, bool /*emptyTag */, QTextDocument *doc ) const { - static QString s_img = QString::fromLatin1("img"); - static QString s_hr = QString::fromLatin1("hr"); - const QStyleSheetItem* style = item( name ); // first some known tags if ( !style ) return 0; - if ( style->name() == s_img ) + if ( style->name() == "img" ) return new QTextImage( doc, attr, context, (QMimeSourceFactory&)factory ); - if ( style->name() == s_hr ) + if ( style->name() == "hr" ) return new QTextHorizontalLine( doc, attr, context, (QMimeSourceFactory&)factory ); return 0; } -/*! - Auxiliary function. Converts the plain text string \a plain to a - rich text formatted paragraph while preserving its look. +/*! Auxiliary function. Converts the plain text string \a plain to a + rich text formatted paragraph while preserving most of its look. - \a mode defines the whitespace mode. Possible values are \c - QStyleSheetItem::WhiteSpacePre (no wrapping, all whitespaces - preserved) and \c QStyleSheetItem::WhiteSpaceNormal (wrapping, - simplified whitespaces). + \a mode defines the whitespace mode. Possible values are \c + QStyleSheetItem::WhiteSpacePre (no wrapping, all whitespaces + preserved) and \c QStyleSheetItem::WhiteSpaceNormal (wrapping, + simplified whitespaces). - \sa escape() - */ + \sa escape() +*/ QString QStyleSheet::convertFromPlainText( const QString& plain, QStyleSheetItem::WhiteSpaceMode mode ) { int col = 0; QString rich; rich += "<p>"; for ( int i = 0; i < int(plain.length()); ++i ) { if ( plain[i] == '\n' ){ - if ( col == 1 ) - rich += "<p></p>"; - else - rich += "<br>"; + int c = 1; + while ( i+1 < int(plain.length()) && plain[i+1] == '\n' ) { + i++; + c++; + } + if ( c == 1) + rich += "<br>\n"; + else { + rich += "</p>\n"; + while ( --c > 1 ) + rich += "<br>\n"; + rich += "<p>"; + } col = 0; - } - else if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i] == '\t' ){ - rich += 0x00a0U; - while ( col % 4 ) { + } else { + if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i] == '\t' ){ rich += 0x00a0U; ++col; + while ( col % 8 ) { + rich += 0x00a0U; + ++col; + } } + else if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i].isSpace() ) + rich += 0x00a0U; + else if ( plain[i] == '<' ) + rich +="<"; + else if ( plain[i] == '>' ) + rich +=">"; + else if ( plain[i] == '&' ) + rich +="&"; + else + rich += plain[i]; + ++col; } - else if ( mode == QStyleSheetItem::WhiteSpacePre && plain[i].isSpace() ) - rich += 0x00a0U; - else if ( plain[i] == '<' ) - rich +="<"; - else if ( plain[i] == '>' ) - rich +=">"; - else if ( plain[i] == '&' ) - rich +="&"; - else - rich += plain[i]; - ++col; } - rich += "</p>"; + if ( col != 0 ) + rich += "</p>"; return rich; } /*! - Auxiliary function. Converts the plain text string \a plain to a - rich text formatted string with any HTML meta-characters escaped. + Auxiliary function. Converts the plain text string \a plain to a + rich text formatted string with any HTML meta-characters escaped. - \sa convertFromPlainText() - */ + \sa convertFromPlainText() +*/ QString QStyleSheet::escape( const QString& plain) { QString rich; for ( int i = 0; i < int(plain.length()); ++i ) { @@ -1353,34 +1440,35 @@ QString QStyleSheet::escape( const QString& plain) // Must doc this enum somewhere, and it is logically related to QStyleSheet /*! - \enum Qt::TextFormat + \enum Qt::TextFormat - This enum is used in widgets that can display both plain text and - rich text, e.g. QLabel. It is used for deciding whether a text - string should be interpreted as one or the other. This is - normally done by passing one of the enum values to a setTextFormat() - function. + This enum is used in widgets that can display both plain text and + rich text, e.g. QLabel. It is used for deciding whether a text + string should be interpreted as one or the other. This is normally + done by passing one of the enum values to a setTextFormat() + function. - \value PlainText The text string is interpreted as a plain text string. + \value PlainText The text string is interpreted as a plain text + string. - \value RichText The text string is interpreted as a rich text string - using the current QStyleSheet::defaultSheet(). + \value RichText The text string is interpreted as a rich text + string using the current QStyleSheet::defaultSheet(). - \value AutoText The text string is interpreted as for \c RichText if - QStyleSheet::mightBeRichText() returns TRUE, otherwise as for \c - PlainText. + \value AutoText The text string is interpreted as for \c RichText + if QStyleSheet::mightBeRichText() returns TRUE, otherwise as + \c PlainText. */ /*! - Returns TRUE if the string \a text is likely to be rich text; - otherwise returns FALSE. + Returns TRUE if the string \a text is likely to be rich text; + otherwise returns FALSE. - Note: The function uses a fast and therefore simple heuristic. It - mainly checks whether there is something that looks like a tag - before the first line break. Although the result may be correct for - most common cases, there is no guarantee. + This function uses a fast and therefore simple heuristic. It + mainly checks whether there is something that looks like a tag + before the first line break. Although the result may be correct + for common cases, there is no guarantee. */ bool QStyleSheet::mightBeRichText( const QString& text) { if ( text.isEmpty() ) @@ -1412,36 +1500,36 @@ bool QStyleSheet::mightBeRichText( const QString& text) return FALSE; } -/*! \fn void QStyleSheet::error( const QString& msg) const +/*! + \fn void QStyleSheet::error( const QString& msg) const - This virtual function is called when an error occurs when - processing rich text. Reimplement it if you need to catch - error messages. + This virtual function is called when an error occurs when + processing rich text. Reimplement it if you need to catch error + messages. - Errors might occur if some rich text strings contain tags that are - not understood by the stylesheet, if some tags are nested incorrectly, or - if tags are not closed properly. + Errors might occur if some rich text strings contain tags that are + not understood by the stylesheet, if some tags are nested + incorrectly, or if tags are not closed properly. - \a msg is the error message. + \a msg is the error message. */ void QStyleSheet::error( const QString& ) const { } /*! - Scales the font \a font to the appropriate physical point size - corresponding to the logical font size \a logicalSize. + Scales the font \a font to the appropriate physical point size + corresponding to the logical font size \a logicalSize. - When calling this function, \a font has a point size corresponding to - the logical font size 3. + When calling this function, \a font has a point size corresponding + to the logical font size 3. - Logical font sizes range from 1 to 7, with 1 being the smallest. + Logical font sizes range from 1 to 7, with 1 being the smallest. - \sa QStyleSheetItem::logicalFontSize(), - QStyleSheetItem::logicalFontSizeStep(), QFont::setPointSize() + \sa QStyleSheetItem::logicalFontSize(), QStyleSheetItem::logicalFontSizeStep(), QFont::setPointSize() */ void QStyleSheet::scaleFont( QFont& font, int logicalSize ) const { if ( logicalSize < 1 ) diff --git a/noncore/apps/opie-write/qstylesheet.h b/noncore/apps/opie-write/qstylesheet.h index bb209fa..cb786f5 100644 --- a/noncore/apps/opie-write/qstylesheet.h +++ b/noncore/apps/opie-write/qstylesheet.h @@ -80,15 +80,15 @@ public: void setDisplayMode(DisplayMode m); int alignment() const; void setAlignment( int f); - + enum VerticalAlignment { VAlignBaseline, VAlignSub, VAlignSuper }; - + VerticalAlignment verticalAlignment() const; void setVerticalAlignment( VerticalAlignment valign ); int fontWeight() const; @@ -119,8 +119,12 @@ public: bool fontUnderline() const; void setFontUnderline( bool ); bool definesFontUnderline() const; + bool fontStrikeOut() const; + void setFontStrikeOut( bool ); + bool definesFontStrikeOut() const; + bool isAnchor() const; void setAnchor(bool anc); enum WhiteSpaceMode { WhiteSpaceNormal, WhiteSpacePre, WhiteSpaceNoWrap }; @@ -170,12 +174,12 @@ private: #if defined(Q_TEMPLATEDLL) // MOC_SKIP_BEGIN -template class Q_EXPORT QDict<QStyleSheetItem>; -template class Q_EXPORT QValueList< QPtrVector<QStyleSheetItem> >; -template class Q_EXPORT QPtrVector<QStyleSheetItem>; -template class Q_EXPORT QValueList<QStyleSheetItem::ListStyle>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QDict<QStyleSheetItem>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueList< QPtrVector<QStyleSheetItem> >; +Q_TEMPLATE_EXTERN template class Q_EXPORT QPtrVector<QStyleSheetItem>; +Q_TEMPLATE_EXTERN template class Q_EXPORT QValueList<QStyleSheetItem::ListStyle>; // MOC_SKIP_END #endif class QTextCustomItem; diff --git a/noncore/apps/opie-write/qt3namespace.h b/noncore/apps/opie-write/qt3namespace.h index 81c5020..557131a 100644 --- a/noncore/apps/opie-write/qt3namespace.h +++ b/noncore/apps/opie-write/qt3namespace.h @@ -1,8 +1,9 @@ #ifndef QT3NAMESPACE_H #define QT3NAMESPACE_H #include <qnamespace.h> +#include <qstring.h> #define Q_ASSERT ASSERT #define Q_WS_QWS diff --git a/noncore/apps/opie-write/qtextedit.cpp b/noncore/apps/opie-write/qtextedit.cpp index 9c5ea79..82401c6 100644 --- a/noncore/apps/opie-write/qtextedit.cpp +++ b/noncore/apps/opie-write/qtextedit.cpp @@ -65,8 +65,10 @@ #define ACCEL_KEY(k) "\t" + QString("Ctrl+" #k) using namespace Qt3; +static bool qt_enable_richtext_copy = FALSE; + struct QUndoRedoInfoPrivate { QTextString text; }; @@ -76,46 +78,108 @@ namespace Qt3 { class QTextEditPrivate { public: QTextEditPrivate() - :preeditStart(-1),preeditLength(-1),ensureCursorVisibleInShowEvent(FALSE) {} + :preeditStart(-1),preeditLength(-1),ensureCursorVisibleInShowEvent(FALSE) + { + for ( int i=0; i<7; i++ ) + id[i] = 0; + } int id[ 7 ]; int preeditStart; int preeditLength; bool ensureCursorVisibleInShowEvent; + QString scrollToAnchor; // used to deferr scrollToAnchor() until the show event when we are resized }; } static bool block_set_alignment = FALSE; /*! \class QTextEdit qtextedit.h - \brief The QTextEdit widget provides a sophisticated single-page rich text editor. + \brief The QTextEdit widget provides a powerful single-page rich text editor. \ingroup basic \ingroup text \mainclass - QTextEdit is an advanced WYSIWYG editor supporting rich text - formatting. It is optimized to handle large documents and to - respond quickly to user input. + \tableofcontents + + \section1 Introduction and Concepts + + QTextEdit is an advanced WYSIWYG viewer/editor supporting rich + text formatting using HTML-style tags. It is optimized to handle + large documents and to respond quickly to user input. + + QTextEdit has three modes of operation: + \table + \header \i Mode \i Command \i Notes + \row \i Plain Text Editor \i setTextFormat(PlainText) + \i Set text with setText(); text() returns plain text. Text + attributes (e.g. colors) can be set, but plain text is always + returned.<sup>1.</sup> + \row \i Rich Text Editor \i setTextFormat(RichText) + \i Set text with setText(); text() returns rich text. Rich + text editing is fairly limited. You can't set margins or + insert images for example (although you can read and + correctly display files that have margins set and that + include images). This mode is mostly useful for editing small + amounts of rich text. <sup>2.</sup> + \row \i Text Viewer<sup>3.</sup> \i setReadOnly(TRUE) + \i Set text with setText() or append() (which has no undo + history so is faster and uses less memory); text() returns + plain or rich text depending on the textFormat(). This mode + can correctly display a large subset of HTML tags. + \endtable + + <sup>1.</sup><small>We do \e not recommend using QTextEdit to + create syntax highlighting editors because the current API is + insufficient for this purpose. We hope to release a more complete + API that will support syntax highlighting in a later + release.</small> + + <sup>2.</sup><small>A more complete API that supports setting + margins, images, etc., is planned for a later Qt release.</small> + + <sup>3.</sup><small>Qt 3.1 will provide a Log Viewer mode which is + optimised for the fast and memory efficient display of large + amounts of read only text.</small> + + We recommend that you always call setTextFormat() to set the mode + you want to use. If you use \c AutoText then setText() and + append() will try to determine whether the text they are given is + plain text or rich text. If you use \c RichText then setText() and + append() will assume that the text they are given is rich text. + insert() simply inserts the text it is given. QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of - the widget. A document consists of zero or more paragraphs, - indexed from 0. Characters are indexed on a per-paragraph basis, - also indexed from 0. The words in the paragraph are aligned in - accordance with the paragraph's alignment(). Paragraphs are - separated by hard line breaks. Each character within a paragraph - has its own attributes, for example, font and color. + the widget. By default when reading plain text, two newlines + signify a paragraph. A document consists of zero or more + paragraphs, indexed from 0. Characters are indexed on a + per-paragraph basis, also indexed from 0. The words in the + paragraph are aligned in accordance with the paragraph's + alignment(). Paragraphs are separated by hard line breaks. Each + character within a paragraph has its own attributes, for example, + font and color. + + The text edit documentation uses the following concepts: + \list + \i \e{current format} -- + this is the format at the current cursor position, \e and it + is the format of the selected text if any. + \i \e{current paragraph} -- the paragraph which contains the + cursor. + \endlist QTextEdit can display images (using QMimeSourceFactory), lists and tables. If the text is too large to view within the text edit's viewport, scrollbars will appear. The text edit can load both plain text and HTML files (a subset of HTML 3.2 and 4). The rendering style and the set of valid tags are defined by a - styleSheet(). Change the style sheet with \l{setStyleSheet()}; see + styleSheet(). Custom tags can be created and placed in a custom + style sheet. Change the style sheet with \l{setStyleSheet()}; see QStyleSheet for details. The images identified by image tags are displayed if they can be interpreted using the text edit's \l{QMimeSourceFactory}; see setMimeSourceFactory(). @@ -133,66 +197,31 @@ static bool block_set_alignment = FALSE; like HTML or XML it will probably be interpreted as rich text, so you should call setTextFormat(Qt::PlainText) to preserve such text. - The text edit documentation uses the following concepts: - \list - \i <i>current format</i> -- - this is the format at the current cursor position, \e and it - is the format of the selected text if any. - \i <i>current paragraph</i> -- the paragraph which contains the - cursor. - \endlist + Note that we do not intend to add a full-featured web browser + widget to Qt (because that would easily double Qt's size and only + a few applications would benefit from it). The rich + text support in Qt is designed to provide a fast, portable and + efficient way to add reasonable online help facilities to + applications, and to provide a basis for rich text editors. + \section1 Using QTextEdit as a Display Widget + + QTextEdit can display a large HTML subset, including tables and + images. The text is set or replaced using setText() which deletes any existing text and replaces it with the text passed in the - setText() call. Text can be inserted with insert(), paste() and - pasteSubType(). Text can also be cut(). The entire text is deleted - with clear() and the selected text is deleted with - removeSelectedText(). Selected (marked) text can also be deleted - with del() (which will delete the character to the right of the - cursor if no text is selected). - - The current format's attributes are set with setItalic(), - setBold(), setUnderline(), setFamily() (font family), - setPointSize(), setColor() and setCurrentFont(). The current - paragraph's style is set with setParagType() and its alignment is - set with setAlignment(). - - Use setSelection() to select text. The setSelectionAttributes() - function is used to indicate how selected text should be - displayed. Use hasSelectedText() to find out if any text is - selected. The currently selected text's position is available - using getSelection() and the selected text itself is returned by - selectedText(). The selection can be copied to the clipboard with - copy(), or cut to the clipboard with cut(). It can be deleted with - removeSelectedText(). The entire text can be selected (or - unselected) using selectAll(). QTextEdit supports multiple - selections. Most of the selection functions operate on the default - selection, selection 0. If the user presses a non-selecting key, - e.g. a cursor key without also holding down Shift, all selections - are cleared. - - Set and get the position of the cursor with setCursorPosition() - and getCursorPosition() respectively. When the cursor is moved, - the signals currentFontChanged(), currentColorChanged() and - currentAlignmentChanged() are emitted to reflect the font, color - and alignment at the new cursor position. - - If the text changes, the textChanged() signal is emitted, and if - the user inserts a new line by pressing Return or Enter, - returnPressed() is emitted. The isModified() function will return - TRUE if the text has been modified. - - QTextEdit provides command-based undo and redo. To set the depth - of the command history use setUndoDepth() which defaults to 100 - steps. To undo or redo the last operation call undo() or redo(). - The signals undoAvailable() and redoAvailable() indicate whether - the undo and redo operations can be executed. - - The indent() function is used to reindent a paragraph. It is - useful for code editors, for example in <em>Qt Designer</em>'s - code editor \e{Ctrl+I} invokes the indent() function. + setText() call. If you call setText() with legacy HTML (with + setTextFormat(RichText) in force), and then call text(), the text + that is returned may have different markup, but will render the + same. Text can be inserted with insert(), paste(), pasteSubType() + and append(). Text that is appended does not go into the undo + history; this makes append() faster and consumes less memory. Text + can also be cut(). The entire text is deleted with clear() and the + selected text is deleted with removeSelectedText(). Selected + (marked) text can also be deleted with del() (which will delete + the character to the right of the cursor if no text is selected). Loading and saving text is achieved using setText() and text(), for example: \code @@ -242,8 +271,87 @@ static bool block_set_alignment = FALSE; You can scroll to an anchor in the text, e.g. \c{<a name="anchor">} with scrollToAnchor(). The find() function can be used to find and select a given string within the text. + A read-only QTextEdit provides the same functionality as the + (obsolete) QTextView. (QTextView is still supplied for + compatibility with old code.) + + \section2 Read-only key bindings + + When QTextEdit is used read-only the key-bindings are limited to + navigation, and text may only be selected with the mouse: + \table + \header \i Keypresses \i Action + \row \i \e{UpArrow} \i Move one line up + \row \i \e{DownArrow} \i Move one line down + \row \i \e{LeftArrow} \i Move one character left + \row \i \e{RightArrow} \i Move one character right + \row \i \e{PageUp} \i Move one (viewport) page up + \row \i \e{PageDown} \i Move one (viewport) page down + \row \i \e{Home} \i Move to the beginning of the text + \row \i \e{End} \i Move to the end of the text + \row \i \e{Shift+Wheel} \i Scroll the page horizontally (the Wheel is the mouse wheel) + \row \i \e{Ctrl+Wheel} \i Zoom the text + \endtable + + The text edit may be able to provide some meta-information. For + example, the documentTitle() function will return the text from + within HTML \c{<title>} tags. + + The text displayed in a text edit has a \e context. The context is + a path which the text edit's QMimeSourceFactory uses to resolve + the locations of files and images. It is passed to the + mimeSourceFactory() when quering data. (See QTextEdit() and + \l{context()}.) + + \section1 Using QTextEdit as an Editor + + All the information about using QTextEdit as a display widget also + applies here. + + The current format's attributes are set with setItalic(), + setBold(), setUnderline(), setFamily() (font family), + setPointSize(), setColor() and setCurrentFont(). The current + paragraph's alignment is set with setAlignment(). + + Use setSelection() to select text. The setSelectionAttributes() + function is used to indicate how selected text should be + displayed. Use hasSelectedText() to find out if any text is + selected. The currently selected text's position is available + using getSelection() and the selected text itself is returned by + selectedText(). The selection can be copied to the clipboard with + copy(), or cut to the clipboard with cut(). It can be deleted with + removeSelectedText(). The entire text can be selected (or + unselected) using selectAll(). QTextEdit supports multiple + selections. Most of the selection functions operate on the default + selection, selection 0. If the user presses a non-selecting key, + e.g. a cursor key without also holding down Shift, all selections + are cleared. + + Set and get the position of the cursor with setCursorPosition() + and getCursorPosition() respectively. When the cursor is moved, + the signals currentFontChanged(), currentColorChanged() and + currentAlignmentChanged() are emitted to reflect the font, color + and alignment at the new cursor position. + + If the text changes, the textChanged() signal is emitted, and if + the user inserts a new line by pressing Return or Enter, + returnPressed() is emitted. The isModified() function will return + TRUE if the text has been modified. + + QTextEdit provides command-based undo and redo. To set the depth + of the command history use setUndoDepth() which defaults to 100 + steps. To undo or redo the last operation call undo() or redo(). + The signals undoAvailable() and redoAvailable() indicate whether + the undo and redo operations can be executed. + + The indent() function is used to reindent a paragraph. It is + useful for code editors, for example in <em>Qt Designer</em>'s + code editor \e{Ctrl+I} invokes the indent() function. + + \section2 Editing key bindings + The list of key-bindings which are implemented for editing: \table \header \i Keypresses \i Action \row \i \e{Backspace} \i Delete the character to the left of the cursor @@ -294,45 +402,8 @@ static bool block_set_alignment = FALSE; text to the right of the cursor is moved out of the way. The mode can be changed to overwrite, where new text overwrites any text to the right of the cursor, using setOverwriteMode(). - QTextEdit can also be used as read-only text viewer. Call - setReadOnly( TRUE ) to disable editing. A read-only QTextEdit - provides the same functionality as the (obsolete) QTextView. - (QTextView is still supplied for compatibility with old code.) - - When QTextEdit is used read-only the key-bindings are limited to - navigation, and text may only be selected with the mouse: - \table - \header \i Keypresses \i Action - \row \i \e{UpArrow} \i Move one line up - \row \i \e{DownArrow} \i Move one line down - \row \i \e{LeftArrow} \i Move one character left - \row \i \e{RightArrow} \i Move one character right - \row \i \e{PageUp} \i Move one (viewport) page up - \row \i \e{PageDown} \i Move one (viewport) page down - \row \i \e{Home} \i Move to the beginning of the text - \row \i \e{End} \i Move to the end of the text - \row \i \e{Shift+Wheel} \i Scroll the page horizontally (the Wheel is the mouse wheel) - \row \i \e{Ctrl+Wheel} \i Zoom the text - \endtable - - The text edit may be able to provide some meta-information. For - example, the documentTitle() function will return the text from - within HTML \c{<title>} tags. - - The text displayed in a text edit has a \e context. The context is - a path which the text edit's QMimeSourceFactory uses to resolve - the locations of files and images. It is passed to the - mimeSourceFactory() when quering data. (See QTextEdit() and - \l{context()}.) - - Note that we do not intend to add a full-featured web browser - widget to Qt (because that would easily double Qt's size and only - a few applications would benefit from it). The rich - text support in Qt is designed to provide a fast, portable and - efficient way to add reasonable online help facilities to - applications, and to provide a basis for rich text editors. */ /*! \enum QTextEdit::KeyboardAction @@ -639,15 +710,17 @@ void QTextEdit::init() wPolicy = AtWhiteSpace; inDnD = FALSE; doc->setFormatter( new QTextFormatterBreakWords ); + doc->formatCollection()->defaultFormat()->setFont( QScrollView::font() ); + doc->formatCollection()->defaultFormat()->setColor( colorGroup().color( QColorGroup::Text ) ); currentFormat = doc->formatCollection()->defaultFormat(); currentAlignment = Qt3::AlignAuto; viewport()->setBackgroundMode( PaletteBase ); viewport()->setAcceptDrops( TRUE ); - resizeContents( 0, doc->lastParag() ? - ( doc->lastParag()->paragId() + 1 ) * doc->formatCollection()->defaultFormat()->height() : 0 ); + resizeContents( 0, doc->lastParagraph() ? + ( doc->lastParagraph()->paragId() + 1 ) * doc->formatCollection()->defaultFormat()->height() : 0 ); setKeyCompression( TRUE ); viewport()->setMouseTracking( TRUE ); #ifndef QT_NO_CURSOR @@ -657,9 +730,9 @@ void QTextEdit::init() formatTimer = new QTimer( this ); connect( formatTimer, SIGNAL( timeout() ), this, SLOT( formatMore() ) ); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); scrollTimer = new QTimer( this ); connect( scrollTimer, SIGNAL( timeout() ), this, SLOT( autoScrollTimerDone() ) ); @@ -712,12 +785,12 @@ void QTextEdit::paintDocument( bool drawAll, QPainter *p, int cx, int cy, int cw p->setBrushOrigin( -contentsX(), -contentsY() ); lastFormatted = doc->draw( p, cx, cy, cw, ch, g, !drawAll, drawCur, cursor ); - if ( lastFormatted == doc->lastParag() ) + if ( lastFormatted == doc->lastParagraph() ) resizeContents( contentsWidth(), doc->height() ); - if ( contentsHeight() < visibleHeight() && ( !doc->lastParag() || doc->lastParag()->isValid() ) && drawAll ) + if ( contentsHeight() < visibleHeight() && ( !doc->lastParagraph() || doc->lastParagraph()->isValid() ) && drawAll ) p->fillRect( 0, contentsHeight(), visibleWidth(), visibleHeight() - contentsHeight(), g.brush( QColorGroup::Base ) ); } @@ -812,12 +885,18 @@ bool QTextEdit::event( QEvent *e ) } } } - if ( e->type() == QEvent::Show && d->ensureCursorVisibleInShowEvent ) { - sync(); - ensureCursorVisible(); - d->ensureCursorVisibleInShowEvent = FALSE; + if ( e->type() == QEvent::Show ) { + if ( d->ensureCursorVisibleInShowEvent ) { + sync(); + ensureCursorVisible(); + d->ensureCursorVisibleInShowEvent = FALSE; + } + if ( !d->scrollToAnchor.isEmpty() ) { + scrollToAnchor( d->scrollToAnchor ); + d->scrollToAnchor = QString::null; + } } return QWidget::event( e ); } @@ -844,9 +923,9 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) for ( int i = 1; i < doc->numSelections(); ++i ) // start with 1 as we don't want to remove the Standard-Selection selChanged = doc->removeSelection( i ) || selChanged; if ( selChanged ) { - cursor->parag()->document()->nextDoubleBuffered = TRUE; + cursor->paragraph()->document()->nextDoubleBuffered = TRUE; repaintChanged(); } bool clearUndoRedoInfo = TRUE; @@ -857,9 +936,9 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) case Key_Right: { // a bit hacky, but can't change this without introducing new enum values for move and keeping the // correct semantics and movement for BiDi and non BiDi text. CursorAction a; - if ( cursor->parag()->string()->isRightToLeft() == (e->key() == Key_Right) ) + if ( cursor->paragraph()->string()->isRightToLeft() == (e->key() == Key_Right) ) a = e->state() & ControlButton ? MoveWordBackward : MoveBackward; else a = e->state() & ControlButton ? MoveWordForward : MoveForward; moveCursor( a, e->state() & ShiftButton ); @@ -885,14 +964,19 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) break; case Key_Return: case Key_Enter: if ( doc->hasSelection( QTextDocument::Standard, FALSE ) ) removeSelectedText(); + if ( textFormat() == Qt::RichText && ( e->state() & ControlButton ) ) { + // Ctrl-Enter inserts a line break in rich text mode + insert( QString( QChar( 0x2028) ), TRUE, FALSE, TRUE ); + } else { #ifndef QT_NO_CURSOR - viewport()->setCursor( isReadOnly() ? arrowCursor : ibeamCursor ); + viewport()->setCursor( isReadOnly() ? arrowCursor : ibeamCursor ); #endif - clearUndoRedoInfo = FALSE; - doKeyboardAction( ActionReturn ); - emit returnPressed(); + clearUndoRedoInfo = FALSE; + doKeyboardAction( ActionReturn ); + emit returnPressed(); + } break; case Key_Delete: #if defined (Q_WS_WIN) if ( e->state() & ShiftButton ) { @@ -910,19 +994,19 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) break; case Key_Insert: if ( e->state() & ShiftButton ) paste(); +#if defined (Q_WS_WIN) + else if ( e->state() & ControlButton ) + copy(); +#endif break; case Key_Backspace: if ( doc->hasSelection( QTextDocument::Standard, TRUE ) ) { removeSelectedText(); break; } - if ( !cursor->parag()->prev() && - cursor->atParagStart() ) - break; - doKeyboardAction( ActionBackspace ); clearUndoRedoInfo = FALSE; break; @@ -942,43 +1026,52 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) ( ( e->state() & ControlButton | AltButton ) == (ControlButton|AltButton) ) ) && ( !e->ascii() || e->ascii() >= 32 || e->text() == "\t" ) ) { clearUndoRedoInfo = FALSE; if ( e->key() == Key_Tab ) { - if ( textFormat() == Qt::RichText && - cursor->index() == 0 && cursor->parag()->style() && - cursor->parag()->style()->displayMode() == - QStyleSheetItem::DisplayListItem ) { - cursor->parag()->incDepth(); + if ( textFormat() == Qt::RichText && cursor->paragraph()->isListItem() ) { + clearUndoRedo(); + undoRedoInfo.type = UndoRedoInfo::Style; + undoRedoInfo.id = cursor->paragraph()->paragId(); + undoRedoInfo.eid = undoRedoInfo.id; + undoRedoInfo.styleInformation = QTextStyleCommand::readStyleInformation( doc, undoRedoInfo.id, undoRedoInfo.eid ); + cursor->paragraph()->setListDepth( cursor->paragraph()->listDepth() +1 ); + clearUndoRedo(); drawCursor( FALSE ); repaintChanged(); drawCursor( TRUE ); break; } } - if ( textFormat() == Qt::RichText && ( !cursor->parag()->style() || - cursor->parag()->style()->displayMode() == QStyleSheetItem::DisplayBlock ) && - cursor->index() == 0 && ( e->text()[0] == '-' || e->text()[0] == '*' ) ) { - setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc ); - cursor->parag()->incDepth(); - drawCursor( FALSE ); - repaintChanged(); - drawCursor( TRUE ); - } else { - if ( overWrite && !cursor->atParagEnd() ) - cursor->remove(); - QString t = e->text(); - QTextParag *p = cursor->parag(); - if ( p && p->string() && p->string()->isRightToLeft() ) { - QChar *c = (QChar *)t.unicode(); - int l = t.length(); - while( l-- ) { - if ( c->mirrored() ) - *c = c->mirroredChar(); - c++; - } + + if ( textFormat() == Qt::RichText && !cursor->paragraph()->isListItem() ) { + if ( cursor->index() == 0 && ( e->text()[0] == '-' || e->text()[0] == '*' ) ) { + clearUndoRedo(); + undoRedoInfo.type = UndoRedoInfo::Style; + undoRedoInfo.id = cursor->paragraph()->paragId(); + undoRedoInfo.eid = undoRedoInfo.id; + undoRedoInfo.styleInformation = QTextStyleCommand::readStyleInformation( doc, undoRedoInfo.id, undoRedoInfo.eid ); + setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc ); + clearUndoRedo(); + drawCursor( FALSE ); + repaintChanged(); + drawCursor( TRUE ); + break; + } + } + if ( overWrite && !cursor->atParagEnd() ) + cursor->remove(); + QString t = e->text(); + QTextParagraph *p = cursor->paragraph(); + if ( p && p->string() && p->string()->isRightToLeft() ) { + QChar *c = (QChar *)t.unicode(); + int l = t.length(); + while( l-- ) { + if ( c->mirrored() ) + *c = c->mirroredChar(); + c++; } - insert( t, TRUE, FALSE, TRUE ); } + insert( t, TRUE, FALSE, TRUE ); break; } else if ( e->state() & ControlButton ) { switch ( e->key() ) { case Key_C: case Key_F16: // Copy key on Sun keyboards @@ -1018,9 +1111,9 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) if ( doc->hasSelection( QTextDocument::Standard ) ) { removeSelectedText(); break; } - if ( !cursor->parag()->prev() && + if ( !cursor->paragraph()->prev() && cursor->atParagStart() ) break; doKeyboardAction( ActionBackspace ); @@ -1035,9 +1128,12 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) case Key_P: moveCursor( MoveUp, e->state() & ShiftButton ); break; case Key_Z: - undo(); + if(e->state() & ShiftButton) + redo(); + else + undo(); break; case Key_Y: redo(); break; @@ -1062,9 +1158,9 @@ void QTextEdit::keyPressEvent( QKeyEvent *e ) } } emit cursorPositionChanged( cursor ); - emit cursorPositionChanged( cursor->parag()->paragId(), cursor->index() ); + emit cursorPositionChanged( cursor->paragraph()->paragId(), cursor->index() ); if ( clearUndoRedoInfo ) clearUndoRedo(); changeIntervalTimer->start( 100, TRUE ); if ( unknown ) @@ -1083,189 +1179,139 @@ void QTextEdit::doKeyboardAction( KeyboardAction action ) if ( cursor->nestedDepth() != 0 ) // #### for 3.0, disable editing of tables as this is not advanced enough return; - lastFormatted = cursor->parag(); + lastFormatted = cursor->paragraph(); drawCursor( FALSE ); bool doUpdateCurrentFormat = TRUE; switch ( action ) { - case ActionDelete: { - checkUndoRedoInfo( UndoRedoInfo::Delete ); - if ( !undoRedoInfo.valid() ) { - undoRedoInfo.id = cursor->parag()->paragId(); - undoRedoInfo.index = cursor->index(); - undoRedoInfo.d->text = QString::null; - } - undoRedoInfo.d->text += cursor->parag()->at( cursor->index() )->c; - if ( cursor->parag()->at( cursor->index() )->format() ) { - cursor->parag()->at( cursor->index() )->format()->addRef(); - undoRedoInfo.d->text.at( undoRedoInfo.d->text.length() - 1 ).setFormat( cursor->parag()->at( cursor->index() )->format() ); - } - QTextParag *old = cursor->parag(); - if ( cursor->remove() ) { - if ( old != cursor->parag() && lastFormatted == old ) - lastFormatted = cursor->parag() ? cursor->parag()->prev() : 0; - undoRedoInfo.d->text += "\n"; + case ActionDelete: + if ( !cursor->atParagEnd() ) { + checkUndoRedoInfo( UndoRedoInfo::Delete ); + if ( !undoRedoInfo.valid() ) { + undoRedoInfo.id = cursor->paragraph()->paragId(); + undoRedoInfo.index = cursor->index(); + undoRedoInfo.d->text = QString::null; + } + undoRedoInfo.d->text.insert( undoRedoInfo.d->text.length(), cursor->paragraph()->at( cursor->index() ), TRUE ); + cursor->remove(); + } else { + clearUndoRedo(); + doc->setSelectionStart( QTextDocument::Temp, *cursor ); + cursor->gotoNextLetter(); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); + removeSelectedText( QTextDocument::Temp ); } - } break; + break; case ActionBackspace: - if ( textFormat() == Qt::RichText && - cursor->parag()->style() && - cursor->parag()->style()->displayMode() == QStyleSheetItem::DisplayListItem && - cursor->index() == 0 ) { - cursor->parag()->decDepth(); - lastFormatted = cursor->parag(); + if ( textFormat() == Qt::RichText && cursor->paragraph()->isListItem() && cursor->index() == 0 ) { + clearUndoRedo(); + undoRedoInfo.type = UndoRedoInfo::Style; + undoRedoInfo.id = cursor->paragraph()->paragId(); + undoRedoInfo.eid = undoRedoInfo.id; + undoRedoInfo.styleInformation = QTextStyleCommand::readStyleInformation( doc, undoRedoInfo.id, undoRedoInfo.eid ); + int ldepth = cursor->paragraph()->listDepth(); + ldepth = QMAX( ldepth-1, 0 ); + cursor->paragraph()->setListDepth( ldepth ); + if ( ldepth == 0 ) + cursor->paragraph()->setListItem( FALSE ); + clearUndoRedo(); + lastFormatted = cursor->paragraph(); repaintChanged(); drawCursor( TRUE ); return; } - checkUndoRedoInfo( UndoRedoInfo::Delete ); - if ( !undoRedoInfo.valid() ) { - undoRedoInfo.id = cursor->parag()->paragId(); - undoRedoInfo.index = cursor->index(); - undoRedoInfo.d->text = QString::null; - } - cursor->gotoPreviousLetter(); - undoRedoInfo.d->text.prepend( QString( cursor->parag()->at( cursor->index() )->c ) ); - if ( cursor->parag()->at( cursor->index() )->format() ) { - cursor->parag()->at( cursor->index() )->format()->addRef(); - undoRedoInfo.d->text.at( 0 ).setFormat( cursor->parag()->at( cursor->index() )->format() ); - } - undoRedoInfo.index = cursor->index(); - if ( cursor->remove() ) { - undoRedoInfo.d->text.remove( 0, 1 ); - undoRedoInfo.d->text.prepend( "\n" ); + if ( !cursor->atParagStart() ) { + checkUndoRedoInfo( UndoRedoInfo::Delete ); + if ( !undoRedoInfo.valid() ) { + undoRedoInfo.id = cursor->paragraph()->paragId(); + undoRedoInfo.index = cursor->index(); + undoRedoInfo.d->text = QString::null; + } + cursor->gotoPreviousLetter(); + undoRedoInfo.d->text.insert( 0, cursor->paragraph()->at( cursor->index() ), TRUE ); undoRedoInfo.index = cursor->index(); - undoRedoInfo.id = cursor->parag()->paragId(); + cursor->remove(); + lastFormatted = cursor->paragraph(); + } else if ( cursor->paragraph()->prev() ){ + clearUndoRedo(); + doc->setSelectionStart( QTextDocument::Temp, *cursor ); + cursor->gotoPreviousLetter(); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); + removeSelectedText( QTextDocument::Temp ); } - lastFormatted = cursor->parag(); break; - case ActionReturn: { + case ActionReturn: checkUndoRedoInfo( UndoRedoInfo::Return ); if ( !undoRedoInfo.valid() ) { - undoRedoInfo.id = cursor->parag()->paragId(); + undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.index = cursor->index(); undoRedoInfo.d->text = QString::null; } undoRedoInfo.d->text += "\n"; - cursor->splitAndInsertEmptyParag(); - if ( cursor->parag()->prev() ) { - lastFormatted = cursor->parag()->prev(); + cursor->splitAndInsertEmptyParagraph(); + if ( cursor->paragraph()->prev() ) { + lastFormatted = cursor->paragraph()->prev(); lastFormatted->invalidate( 0 ); } doUpdateCurrentFormat = FALSE; - } break; - case ActionKill: - checkUndoRedoInfo( UndoRedoInfo::Delete ); - if ( !undoRedoInfo.valid() ) { - undoRedoInfo.id = cursor->parag()->paragId(); - undoRedoInfo.index = cursor->index(); - undoRedoInfo.d->text = QString::null; - } - if ( cursor->atParagEnd() ) { - undoRedoInfo.d->text += cursor->parag()->at( cursor->index() )->c; - if ( cursor->parag()->at( cursor->index() )->format() ) { - cursor->parag()->at( cursor->index() )->format()->addRef(); - undoRedoInfo.d->text.at( undoRedoInfo.d->text.length() - 1 ).setFormat( cursor->parag()->at( cursor->index() )->format() ); - } - QTextParag *old = cursor->parag(); - if ( cursor->remove() ) { - if ( old != cursor->parag() && lastFormatted == old ) - lastFormatted = cursor->parag() ? cursor->parag()->prev() : 0; - undoRedoInfo.d->text += "\n"; - } - } else { - int oldLen = undoRedoInfo.d->text.length(); - undoRedoInfo.d->text += cursor->parag()->string()->toString().mid( cursor->index() ); - for ( int i = cursor->index(); i < cursor->parag()->length(); ++i ) { - if ( cursor->parag()->at( i )->format() ) { - cursor->parag()->at( i )->format()->addRef(); - undoRedoInfo.d->text.at( oldLen + i - cursor->index() ).setFormat( cursor->parag()->at( i )->format() ); - } - } - undoRedoInfo.d->text.remove( undoRedoInfo.d->text.length() - 1, 1 ); - cursor->killLine(); - } break; + case ActionKill: + clearUndoRedo(); + doc->setSelectionStart( QTextDocument::Temp, *cursor ); + if ( cursor->atParagEnd() ) + cursor->gotoNextLetter(); + else + cursor->setIndex( cursor->paragraph()->length() - 1 ); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); + removeSelectedText( QTextDocument::Temp ); + break; } formatMore(); repaintChanged(); ensureCursorVisible(); drawCursor( TRUE ); - - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } - + updateMicroFocusHint(); if ( doUpdateCurrentFormat ) updateCurrentFormat(); setModified(); emit textChanged(); } -void QTextEdit::readFormats( QTextCursor &c1, QTextCursor &c2, int oldLen, QTextString &text, bool fillStyles ) +void QTextEdit::readFormats( QTextCursor &c1, QTextCursor &c2, QTextString &text, bool fillStyles ) { + QDataStream styleStream( undoRedoInfo.styleInformation, IO_WriteOnly ); c2.restoreState(); c1.restoreState(); - if ( c1.parag() == c2.parag() ) { - for ( int i = c1.index(); i < c2.index(); ++i ) { - if ( c1.parag()->at( i )->format() ) { - c1.parag()->at( i )->format()->addRef(); - text.at( oldLen + i - c1.index() ).setFormat( c1.parag()->at( i )->format() ); - } - } + int lastIndex = text.length(); + if ( c1.paragraph() == c2.paragraph() ) { + for ( int i = c1.index(); i < c2.index(); ++i ) + text.insert( lastIndex + i - c1.index(), c1.paragraph()->at( i ), TRUE ); if ( fillStyles ) { - undoRedoInfo.oldAligns[ 0 ] = c1.parag()->alignment(); - undoRedoInfo.oldStyles << c1.parag()->styleSheetItems(); - undoRedoInfo.oldListStyles << c1.parag()->listStyle(); + styleStream << (int) 1; + c1.paragraph()->writeStyleInformation( styleStream ); } } else { - int lastIndex = oldLen; int i; - for ( i = c1.index(); i < c1.parag()->length(); ++i ) { - if ( c1.parag()->at( i )->format() ) { - c1.parag()->at( i )->format()->addRef(); - text.at( lastIndex ).setFormat( c1.parag()->at( i )->format() ); - lastIndex++; - } - } - QTextParag *p = c1.parag()->next(); - while ( p && p != c2.parag() ) { - for ( int i = 0; i < p->length(); ++i ) { - if ( p->at( i )->format() ) { - p->at( i )->format()->addRef(); - text.at( i + lastIndex ).setFormat( p->at( i )->format() ); - } - } - lastIndex += p->length(); + for ( i = c1.index(); i < c1.paragraph()->length()-1; ++i ) + text.insert( lastIndex++, c1.paragraph()->at( i ), TRUE ); + int num = 2; // start and end, being different + text += "\n"; lastIndex++; + QTextParagraph *p = c1.paragraph()->next(); + while ( p && p != c2.paragraph() ) { + for ( i = 0; i < p->length()-1; ++i ) + text.insert( lastIndex++ , p->at( i ), TRUE ); + text += "\n"; num++; lastIndex++; p = p->next(); } - for ( i = 0; i < c2.index(); ++i ) { - if ( c2.parag()->at( i )->format() ) { - c2.parag()->at( i )->format()->addRef(); - text.at( i + lastIndex ).setFormat( c2.parag()->at( i )->format() ); - } - } + for ( i = 0; i < c2.index(); ++i ) + text.insert( i + lastIndex, c2.paragraph()->at( i ), TRUE ); if ( fillStyles ) { - QTextParag *p = c1.parag(); - i = 0; - while ( p ) { - if ( i < (int)undoRedoInfo.oldAligns.size() ) - undoRedoInfo.oldAligns[ i ] = p->alignment(); - undoRedoInfo.oldStyles << p->styleSheetItems(); - undoRedoInfo.oldListStyles << p->listStyle(); - if ( p == c2.parag() ) - break; - p = p->next(); - ++i; - } + styleStream << num; + for ( QTextParagraph *p = c1.paragraph(); --num >= 0; p = p->next() ) + p->writeStyleInformation( styleStream ); } } } @@ -1293,11 +1339,13 @@ void QTextEdit::removeSelectedText( int selNum ) if ( isReadOnly() ) return; QTextCursor c1 = doc->selectionStartCursor( selNum ); + c1.restoreState(); QTextCursor c2 = doc->selectionEndCursor( selNum ); + c2.restoreState(); - // ### no support for editing tables yet + // ### no support for editing tables yet, plus security for broken selections if ( c1.nestedDepth() || c2.nestedDepth() ) return; for ( int i = 0; i < (int)doc->numSelections(); ++i ) { @@ -1311,16 +1359,15 @@ void QTextEdit::removeSelectedText( int selNum ) if ( !undoRedoInfo.valid() ) { doc->selectionStart( selNum, undoRedoInfo.id, undoRedoInfo.index ); undoRedoInfo.d->text = QString::null; } - int oldLen = undoRedoInfo.d->text.length(); - undoRedoInfo.d->text = doc->selectedText( selNum, FALSE ); - undoRedoInfo.oldAligns.resize( undoRedoInfo.oldAligns.size() + QMAX( 0, c2.parag()->paragId() - c1.parag()->paragId() + 1 ) ); - readFormats( c1, c2, oldLen, undoRedoInfo.d->text, TRUE ); + readFormats( c1, c2, undoRedoInfo.d->text, TRUE ); + + doc->removeSelectedText( selNum, cursor ); if ( cursor->isValid() ) { ensureCursorVisible(); - lastFormatted = cursor->parag(); + lastFormatted = cursor->paragraph(); formatMore(); repaintChanged(); ensureCursorVisible(); drawCursor( TRUE ); @@ -1333,20 +1380,12 @@ void QTextEdit::removeSelectedText( int selNum ) #endif #ifndef QT_NO_CURSOR viewport()->setCursor( isReadOnly() ? arrowCursor : ibeamCursor ); #endif - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); } else { - cursor->setDocument( doc ); - cursor->setParag( doc->firstParag() ); - cursor->setIndex( 0 ); + delete cursor; + cursor = new QTextCursor( doc ); drawCursor( TRUE ); viewport()->repaint( TRUE ); } setModified(); @@ -1364,12 +1403,12 @@ void QTextEdit::moveCursor( CursorAction action, bool select ) { drawCursor( FALSE ); if ( select ) { if ( !doc->hasSelection( QTextDocument::Standard ) ) - doc->setSelectionStart( QTextDocument::Standard, cursor ); + doc->setSelectionStart( QTextDocument::Standard, *cursor ); moveCursor( action ); - if ( doc->setSelectionEnd( QTextDocument::Standard, cursor ) ) { - cursor->parag()->document()->nextDoubleBuffered = TRUE; + if ( doc->setSelectionEnd( QTextDocument::Standard, *cursor ) ) { + cursor->paragraph()->document()->nextDoubleBuffered = TRUE; repaintChanged(); } else { drawCursor( TRUE ); } @@ -1382,9 +1421,9 @@ void QTextEdit::moveCursor( CursorAction action, bool select ) if ( !redraw ) { ensureCursorVisible(); drawCursor( TRUE ); } else { - cursor->parag()->document()->nextDoubleBuffered = TRUE; + cursor->paragraph()->document()->nextDoubleBuffered = TRUE; repaintChanged(); ensureCursorVisible(); drawCursor( TRUE ); #ifndef QT_NO_CURSOR @@ -1398,16 +1437,9 @@ void QTextEdit::moveCursor( CursorAction action, bool select ) } drawCursor( TRUE ); updateCurrentFormat(); - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); } /*! \overload */ @@ -1448,42 +1480,39 @@ void QTextEdit::moveCursor( CursorAction action ) case MoveLineEnd: cursor->gotoLineEnd(); break; case MoveEnd: - ensureFormatted( doc->lastParag() ); + ensureFormatted( doc->lastParagraph() ); cursor->gotoEnd(); break; } - - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); updateCurrentFormat(); } /*! \reimp */ void QTextEdit::resizeEvent( QResizeEvent *e ) { QScrollView::resizeEvent( e ); + if ( doc->visibleWidth() == 0 ) + doResize(); } /*! \reimp */ void QTextEdit::viewportResizeEvent( QResizeEvent *e ) { QScrollView::viewportResizeEvent( e ); - if ( e->oldSize().width() != e->size().width() ) + if ( e->oldSize().width() != e->size().width() ) { + bool stayAtBottom = e->oldSize().height() != e->size().height() && + contentsY() > 0 && contentsY() >= doc->height() - e->oldSize().height(); doResize(); + if ( stayAtBottom ) + scrollToBottom(); + } } -static bool blockEnsureCursorVisible = FALSE; - /*! Ensures that the cursor is visible by scrolling the text edit if necessary. @@ -1491,22 +1520,20 @@ static bool blockEnsureCursorVisible = FALSE; */ void QTextEdit::ensureCursorVisible() { - if ( blockEnsureCursorVisible ) - return; if ( !isVisible() ) { d->ensureCursorVisibleInShowEvent = TRUE; return; } - lastFormatted = cursor->parag(); + lastFormatted = cursor->paragraph(); formatMore(); - QTextStringChar *chr = cursor->parag()->at( cursor->index() ); - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - int x = cursor->parag()->rect().x() + chr->x + cursor->offsetX(); + QTextStringChar *chr = cursor->paragraph()->at( cursor->index() ); + int h = cursor->paragraph()->lineHeightOfChar( cursor->index() ); + int x = cursor->paragraph()->rect().x() + chr->x + cursor->offsetX(); int y = 0; int dummy; - cursor->parag()->lineHeightOfChar( cursor->index(), &dummy, &y ); - y += cursor->parag()->rect().y() + cursor->offsetY(); + cursor->paragraph()->lineHeightOfChar( cursor->index(), &dummy, &y ); + y += cursor->paragraph()->rect().y() + cursor->offsetY(); int w = 1; ensureVisible( x, y + h / 2, w, h / 2 + 2 ); } @@ -1516,40 +1543,40 @@ void QTextEdit::ensureCursorVisible() void QTextEdit::drawCursor( bool visible ) { if ( !isUpdatesEnabled() || !viewport()->isUpdatesEnabled() || - !cursor->parag() || - !cursor->parag()->isValid() || + !cursor->paragraph() || + !cursor->paragraph()->isValid() || !selectedText().isEmpty() || ( visible && !hasFocus() && !viewport()->hasFocus() && !inDnD ) || isReadOnly() ) return; QPainter p( viewport() ); - QRect r( cursor->topParag()->rect() ); - cursor->parag()->setChanged( TRUE ); + QRect r( cursor->topParagraph()->rect() ); + cursor->paragraph()->setChanged( TRUE ); p.translate( -contentsX() + cursor->totalOffsetX(), -contentsY() + cursor->totalOffsetY() ); QPixmap *pix = 0; QColorGroup cg( colorGroup() ); - if ( cursor->parag()->background() ) - cg.setBrush( QColorGroup::Base, *cursor->parag()->background() ); + if ( cursor->paragraph()->background() ) + cg.setBrush( QColorGroup::Base, *cursor->paragraph()->background() ); else if ( doc->paper() ) cg.setBrush( QColorGroup::Base, *doc->paper() ); p.setBrushOrigin( -contentsX(), -contentsY() ); - cursor->parag()->document()->nextDoubleBuffered = TRUE; + cursor->paragraph()->document()->nextDoubleBuffered = TRUE; if ( !cursor->nestedDepth() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); + int h = cursor->paragraph()->lineHeightOfChar( cursor->index() ); int dist = 5; - if ( ( cursor->parag()->alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify ) + if ( ( cursor->paragraph()->alignment() & Qt3::AlignJustify ) == Qt3::AlignJustify ) dist = 50; int x = r.x() - cursor->totalOffsetX() + cursor->x() - dist; x = QMAX( x, 0 ); p.setClipRect( QRect( x - contentsX(), r.y() - cursor->totalOffsetY() + cursor->y() - contentsY(), 2 * dist, h ) ); - doc->drawParag( &p, cursor->parag(), x, + doc->drawParagraph( &p, cursor->paragraph(), x, r.y() - cursor->totalOffsetY() + cursor->y(), 2 * dist, h, pix, cg, visible, cursor ); } else { - doc->drawParag( &p, cursor->parag(), r.x() - cursor->totalOffsetX(), + doc->drawParagraph( &p, cursor->paragraph(), r.x() - cursor->totalOffsetX(), r.y() - cursor->totalOffsetY(), r.width(), r.height(), pix, cg, visible, cursor ); } cursorVisible = visible; @@ -1601,11 +1628,11 @@ void QTextEdit::contentsMousePressEvent( QMouseEvent *e ) if ( isReadOnly() && linksEnabled() ) { QTextCursor c = *cursor; placeCursor( e->pos(), &c, TRUE ); - if ( c.parag() && c.parag()->at( c.index() ) && - c.parag()->at( c.index() )->isAnchor() ) { - pressedLink = c.parag()->at( c.index() )->anchorHref(); + if ( c.paragraph() && c.paragraph()->at( c.index() ) && + c.paragraph()->at( c.index() )->isAnchor() ) { + pressedLink = c.paragraph()->at( c.index() )->anchorHref(); } } #ifndef QT_NO_DRAGANDDROP @@ -1621,18 +1648,18 @@ void QTextEdit::contentsMousePressEvent( QMouseEvent *e ) bool redraw = FALSE; if ( doc->hasSelection( QTextDocument::Standard ) ) { if ( !( e->state() & ShiftButton ) ) { redraw = doc->removeSelection( QTextDocument::Standard ); - doc->setSelectionStart( QTextDocument::Standard, cursor ); + doc->setSelectionStart( QTextDocument::Standard, *cursor ); } else { - redraw = doc->setSelectionEnd( QTextDocument::Standard, cursor ) || redraw; + redraw = doc->setSelectionEnd( QTextDocument::Standard, *cursor ) || redraw; } } else { if ( isReadOnly() || !( e->state() & ShiftButton ) ) { - doc->setSelectionStart( QTextDocument::Standard, cursor ); + doc->setSelectionStart( QTextDocument::Standard, *cursor ); } else { - doc->setSelectionStart( QTextDocument::Standard, &c ); - redraw = doc->setSelectionEnd( QTextDocument::Standard, cursor ) || redraw; + doc->setSelectionStart( QTextDocument::Standard, c ); + redraw = doc->setSelectionEnd( QTextDocument::Standard, *cursor ) || redraw; } } for ( int i = 1; i < doc->numSelections(); ++i ) // start with 1 as we don't want to remove the Standard-Selection @@ -1713,9 +1740,9 @@ void QTextEdit::contentsMouseReleaseEvent( QMouseEvent * e ) if ( mousePressed ) { mousePressed = FALSE; } emit cursorPositionChanged( cursor ); - emit cursorPositionChanged( cursor->parag()->paragId(), cursor->index() ); + emit cursorPositionChanged( cursor->paragraph()->paragId(), cursor->index() ); if ( oldCursor != *cursor ) updateCurrentFormat(); inDoubleClick = FALSE; @@ -1743,15 +1770,15 @@ void QTextEdit::contentsMouseReleaseEvent( QMouseEvent * e ) void QTextEdit::contentsMouseDoubleClickEvent( QMouseEvent * ) { QTextCursor c1 = *cursor; QTextCursor c2 = *cursor; - if ( cursor->index() > 0 && !cursor->parag()->at( cursor->index()-1 )->c.isSpace() ) + if ( cursor->index() > 0 && !cursor->paragraph()->at( cursor->index()-1 )->c.isSpace() ) c1.gotoPreviousWord(); - if ( !cursor->parag()->at( cursor->index() )->c.isSpace() && !cursor->atParagEnd() ) + if ( !cursor->paragraph()->at( cursor->index() )->c.isSpace() && !cursor->atParagEnd() ) c2.gotoNextWord(); - doc->setSelectionStart( QTextDocument::Standard, &c1 ); - doc->setSelectionEnd( QTextDocument::Standard, &c2 ); + doc->setSelectionStart( QTextDocument::Standard, c1 ); + doc->setSelectionEnd( QTextDocument::Standard, c2 ); *cursor = c2; repaintChanged(); @@ -1805,11 +1832,44 @@ void QTextEdit::contentsDropEvent( QDropEvent *e ) e->acceptAction(); QString text; bool intern = FALSE; if ( QTextDrag::decode( e, text ) ) { - if ( ( e->source() == this || - e->source() == viewport() ) && - e->action() == QDropEvent::Move ) { + bool hasSel = doc->hasSelection( QTextDocument::Standard ); + bool internalDrag = e->source() == this || e->source() == viewport(); + int dropId, dropIndex; + QTextCursor insertCursor = *cursor; + dropId = cursor->paragraph()->paragId(); + dropIndex = cursor->index(); + if ( hasSel && internalDrag ) { + QTextCursor c1, c2; + int selStartId, selStartIndex; + int selEndId, selEndIndex; + c1 = doc->selectionStartCursor( QTextDocument::Standard ); + c1.restoreState(); + c2 = doc->selectionEndCursor( QTextDocument::Standard ); + c2.restoreState(); + selStartId = c1.paragraph()->paragId(); + selStartIndex = c1.index(); + selEndId = c2.paragraph()->paragId(); + selEndIndex = c2.index(); + if ( ( ( dropId > selStartId ) || + ( dropId == selStartId && dropIndex > selStartIndex ) ) && + ( ( dropId < selEndId ) || + ( dropId == selEndId && dropIndex <= selEndIndex ) ) ) + insertCursor = c1; + if ( dropId == selEndId && dropIndex > selEndIndex ) { + insertCursor = c1; + if ( selStartId == selEndId ) { + insertCursor.setIndex( dropIndex - + ( selEndIndex - selStartIndex ) ); + } else { + insertCursor.setIndex( dropIndex - selEndIndex + + selStartIndex ); + } + } + } + + if ( internalDrag && e->action() == QDropEvent::Move ) { removeSelectedText(); intern = TRUE; } else { doc->removeSelection( QTextDocument::Standard ); @@ -1817,9 +1877,10 @@ void QTextEdit::contentsDropEvent( QDropEvent *e ) viewport()->setCursor( isReadOnly() ? arrowCursor : ibeamCursor ); #endif } drawCursor( FALSE ); - placeCursor( e->pos(), cursor ); + cursor->setParagraph( insertCursor.paragraph() ); + cursor->setIndex( insertCursor.index() ); drawCursor( TRUE ); if ( !cursor->nestedDepth() ) { insert( text, FALSE, TRUE, FALSE ); } else { @@ -1858,15 +1919,15 @@ void QTextEdit::handleMouseMove( const QPoint& pos ) cl.gotoPreviousWord(); QTextCursor cr = *cursor; cr.gotoNextWord(); - int diff = QABS( oldCursor.parag()->at( oldCursor.index() )->x - mousePos.x() ); - int ldiff = QABS( cl.parag()->at( cl.index() )->x - mousePos.x() ); - int rdiff = QABS( cr.parag()->at( cr.index() )->x - mousePos.x() ); + int diff = QABS( oldCursor.paragraph()->at( oldCursor.index() )->x - mousePos.x() ); + int ldiff = QABS( cl.paragraph()->at( cl.index() )->x - mousePos.x() ); + int rdiff = QABS( cr.paragraph()->at( cr.index() )->x - mousePos.x() ); - if ( cursor->parag()->lineStartOfChar( cursor->index() ) != - oldCursor.parag()->lineStartOfChar( oldCursor.index() ) ) + if ( cursor->paragraph()->lineStartOfChar( cursor->index() ) != + oldCursor.paragraph()->lineStartOfChar( oldCursor.index() ) ) diff = 0xFFFFFF; if ( rdiff < diff && rdiff < ldiff ) *cursor = cr; @@ -1879,9 +1940,9 @@ void QTextEdit::handleMouseMove( const QPoint& pos ) ensureCursorVisible(); bool redraw = FALSE; if ( doc->hasSelection( QTextDocument::Standard ) ) { - redraw = doc->setSelectionEnd( QTextDocument::Standard, cursor ) || redraw; + redraw = doc->setSelectionEnd( QTextDocument::Standard, *cursor ) || redraw; } if ( !redraw ) { drawCursor( TRUE ); @@ -1889,11 +1950,11 @@ void QTextEdit::handleMouseMove( const QPoint& pos ) repaintChanged(); drawCursor( TRUE ); } - if ( currentFormat && currentFormat->key() != cursor->parag()->at( cursor->index() )->format()->key() ) { + if ( currentFormat && currentFormat->key() != cursor->paragraph()->at( cursor->index() )->format()->key() ) { currentFormat->removeRef(); - currentFormat = doc->formatCollection()->format( cursor->parag()->at( cursor->index() )->format() ); + currentFormat = doc->formatCollection()->format( cursor->paragraph()->at( cursor->index() )->format() ); if ( currentFormat->isMisspelled() ) { currentFormat->removeRef(); currentFormat = doc->formatCollection()->format( currentFormat->font(), currentFormat->color() ); } @@ -1901,10 +1962,10 @@ void QTextEdit::handleMouseMove( const QPoint& pos ) emit currentColorChanged( currentFormat->color() ); emit currentVerticalAlignmentChanged( (VerticalAlignment)currentFormat->vAlign() ); } - if ( currentAlignment != cursor->parag()->alignment() ) { - currentAlignment = cursor->parag()->alignment(); + if ( currentAlignment != cursor->paragraph()->alignment() ) { + currentAlignment = cursor->paragraph()->alignment(); block_set_alignment = TRUE; emit currentAlignmentChanged( currentAlignment ); block_set_alignment = FALSE; } @@ -1924,28 +1985,40 @@ void QTextEdit::placeCursor( const QPoint &pos, QTextCursor *c, bool link ) if ( !c ) c = cursor; c->restoreState(); - QTextParag *s = doc->firstParag(); + QTextParagraph *s = doc->firstParagraph(); c->place( pos, s, link ); + updateMicroFocusHint(); +} + + +void QTextEdit::updateMicroFocusHint() +{ + QTextCursor c( *cursor ); + if ( d->preeditStart != -1 ) + c.setIndex( d->preeditStart ); + if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); + int h = c.paragraph()->lineHeightOfChar( cursor->index() ); if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); + QFont f = c.paragraph()->at( c.index() )->format()->font(); + setMicroFocusHint( c.x() - contentsX() + frameWidth(), + c.y() + cursor->paragraph()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); } } } + + void QTextEdit::formatMore() { if ( !lastFormatted ) return; int bottom = contentsHeight(); int lastBottom = -1; - int to = !sender() ? 2 : 20; + int to = 20; bool firstVisible = FALSE; QRect cr( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); for ( int i = 0; ( i < to || firstVisible ) && lastFormatted; ++i ) { lastFormatted->format(); @@ -1960,12 +2033,16 @@ void QTextEdit::formatMore() if ( lastFormatted ) lastBottom = -1; } - if ( bottom > contentsHeight() ) + if ( bottom > contentsHeight() ) { resizeContents( contentsWidth(), QMAX( doc->height(), bottom ) ); - else if ( lastBottom != -1 && lastBottom < contentsHeight() ) - resizeContents( contentsWidth(), QMAX( doc->height(), lastBottom ) ); + } else if ( lastBottom != -1 && lastBottom < contentsHeight() ) { + resizeContents( contentsWidth(), QMAX( doc->height(), lastBottom ) ); + if ( contentsHeight() < visibleHeight() ) + updateContents( 0, contentsHeight(), visibleWidth(), + visibleHeight() - contentsHeight() ); + } if ( lastFormatted ) formatTimer->start( interval, TRUE ); else @@ -1979,9 +2056,9 @@ void QTextEdit::doResize() doc->setMinimumWidth( -1 ); resizeContents( 0, 0 ); doc->setWidth( visibleWidth() ); doc->invalidate(); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); interval = 0; formatMore(); repaintContents( contentsX(), contentsY(), visibleWidth(), visibleHeight(), FALSE ); } @@ -2000,18 +2077,9 @@ bool QTextEdit::eventFilter( QObject *o, QEvent *e ) if ( o == this || o == viewport() ) { if ( e->type() == QEvent::FocusIn ) { blinkTimer->start( QApplication::cursorFlashTime() / 2 ); drawCursor( TRUE ); - - if ( !readonly ) { - // make sure the micro focus hint is updated... - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - - contentsY() + frameWidth(), 0, - cursor->parag()->lineHeightOfChar( cursor->index() ), - TRUE ); - } + updateMicroFocusHint(); } else if ( e->type() == QEvent::FocusOut ) { blinkTimer->stop(); drawCursor( FALSE ); } @@ -2019,16 +2087,16 @@ bool QTextEdit::eventFilter( QObject *o, QEvent *e ) return QScrollView::eventFilter( o, e ); } -/*! - Inserts \a text at the current cursor position. If \a indent is TRUE, - the paragraph is re-indented. If \a checkNewLine is TRUE, newline - characters in \a text result in hard line breaks (i.e. new - paragraphs). If \a checkNewLine is FALSE the behaviour of the editor - is undefined if the \a text contains newlines. If \a removeSelected is - TRUE, any selected text (in selection 0) is removed before the text is - inserted. +/*! Inserts \a text at the current cursor position. If \a indent is + TRUE, the paragraph is re-indented. If \a checkNewLine is TRUE, + newline characters in \a text result in hard line breaks (i.e. new + paragraphs). If \a checkNewLine is FALSE and there are newlines in + \a text, the behavior is undefined. If \a checkNewLine is FALSE the + behaviour of the editor is undefined if the \a text contains + newlines. If \a removeSelected is TRUE, any selected text (in + selection 0) is removed before the text is inserted. \sa paste() pasteSubType() */ @@ -2045,22 +2113,22 @@ void QTextEdit::insert( const QString &text, bool indent, bool checkNewLine, boo if ( undoEnabled && !isReadOnly() ) { checkUndoRedoInfo( UndoRedoInfo::Insert ); if ( !undoRedoInfo.valid() ) { - undoRedoInfo.id = cursor->parag()->paragId(); + undoRedoInfo.id = cursor->paragraph()->paragId(); undoRedoInfo.index = cursor->index(); undoRedoInfo.d->text = QString::null; } oldLen = undoRedoInfo.d->text.length(); } - lastFormatted = checkNewLine && cursor->parag()->prev() ? - cursor->parag()->prev() : cursor->parag(); + lastFormatted = checkNewLine && cursor->paragraph()->prev() ? + cursor->paragraph()->prev() : cursor->paragraph(); QTextCursor oldCursor = *cursor; cursor->insert( txt, checkNewLine ); if ( doc->useFormatCollection() ) { - doc->setSelectionStart( QTextDocument::Temp, &oldCursor ); - doc->setSelectionEnd( QTextDocument::Temp, cursor ); + doc->setSelectionStart( QTextDocument::Temp, oldCursor ); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); doc->setFormat( QTextDocument::Temp, currentFormat, QTextFormat::Format ); doc->removeSelection( QTextDocument::Temp ); } @@ -2074,43 +2142,37 @@ void QTextEdit::insert( const QString &text, bool indent, bool checkNewLine, boo if ( undoEnabled && !isReadOnly() ) { undoRedoInfo.d->text += txt; if ( !doc->preProcessor() ) { for ( int i = 0; i < (int)txt.length(); ++i ) { - if ( txt[ i ] != '\n' && c2.parag()->at( c2.index() )->format() ) { - c2.parag()->at( c2.index() )->format()->addRef(); - undoRedoInfo.d->text.setFormat( oldLen + i, c2.parag()->at( c2.index() )->format(), TRUE ); + if ( txt[ i ] != '\n' && c2.paragraph()->at( c2.index() )->format() ) { + c2.paragraph()->at( c2.index() )->format()->addRef(); + undoRedoInfo.d->text.setFormat( oldLen + i, c2.paragraph()->at( c2.index() )->format(), TRUE ); } c2.gotoNextLetter(); } } } - setModified(); - emit textChanged(); if ( !removeSelected ) { - doc->setSelectionStart( QTextDocument::Standard, &oldCursor ); - doc->setSelectionEnd( QTextDocument::Standard, cursor ); + doc->setSelectionStart( QTextDocument::Standard, oldCursor ); + doc->setSelectionEnd( QTextDocument::Standard, *cursor ); repaintChanged(); } - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); + setModified(); + emit textChanged(); } /*! Inserts \a text in the paragraph \a para and position \a index */ void QTextEdit::insertAt( const QString &text, int para, int index ) { - QTextParag *p = doc->paragAt( para ); + removeSelection( QTextDocument::Standard ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return; QTextCursor tmp = *cursor; - cursor->setParag( p ); + cursor->setParagraph( p ); cursor->setIndex( index ); insert( text, FALSE, TRUE, FALSE ); *cursor = tmp; removeSelection( QTextDocument::Standard ); @@ -2121,15 +2183,15 @@ void QTextEdit::insertAt( const QString &text, int para, int index ) */ void QTextEdit::insertParagraph( const QString &text, int para ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( p ) { QTextCursor tmp( doc ); - tmp.setParag( p ); + tmp.setParagraph( p ); tmp.setIndex( 0 ); tmp.insert( text, TRUE ); - tmp.splitAndInsertEmptyParag(); + tmp.splitAndInsertEmptyParagraph(); repaintChanged(); } else { append( text ); } @@ -2138,30 +2200,30 @@ void QTextEdit::insertParagraph( const QString &text, int para ) /*! Removes the paragraph \a para */ void QTextEdit::removeParagraph( int para ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return; for ( int i = 0; i < doc->numSelections(); ++i ) doc->removeSelection( i ); - if ( p == doc->firstParag() && p == doc->lastParag() ) { + if ( p == doc->firstParagraph() && p == doc->lastParagraph() ) { p->remove( 0, p->length() - 1 ); repaintChanged(); return; } drawCursor( FALSE ); - bool resetCursor = cursor->parag() == p; + bool resetCursor = cursor->paragraph() == p; if ( p->prev() ) p->prev()->setNext( p->next() ); else - doc->setFirstParag( p->next() ); + doc->setFirstParagraph( p->next() ); if ( p->next() ) p->next()->setPrev( p->prev() ); else - doc->setLastParag( p->prev() ); - QTextParag *start = p->next(); + doc->setLastParagraph( p->prev() ); + QTextParagraph *start = p->next(); int h = p->rect().height(); delete p; p = start; int dy = -h; @@ -2173,9 +2235,9 @@ void QTextEdit::removeParagraph( int para ) p = p->next(); } if ( resetCursor ) { - cursor->setParag( doc->firstParag() ); + cursor->setParagraph( doc->firstParagraph() ); cursor->setIndex( 0 ); } repaintChanged(); drawCursor( TRUE ); @@ -2216,18 +2278,11 @@ void QTextEdit::undo() lastFormatted = 0; ensureCursorVisible(); repaintChanged(); drawCursor( TRUE ); + updateMicroFocusHint(); setModified(); emit textChanged(); - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } } /*! Redoes the last operation. @@ -2261,18 +2316,11 @@ void QTextEdit::redo() ensureCursorVisible(); repaintChanged(); ensureCursorVisible(); drawCursor( TRUE ); + updateMicroFocusHint(); setModified(); emit textChanged(); - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } } /*! Pastes the text from the clipboard into the text edit at the current @@ -2288,16 +2336,9 @@ void QTextEdit::paste() #ifndef QT_NO_CLIPBOARD if ( isReadOnly() ) return; pasteSubType( "plain" ); - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); #endif } void QTextEdit::checkUndoRedoInfo( UndoRedoInfo::Type t ) @@ -2336,20 +2377,15 @@ void QTextEdit::cut() { if ( isReadOnly() ) return; - if ( doc->hasSelection( QTextDocument::Standard ) ) { - doc->copySelectedText( QTextDocument::Standard ); + QString t; + if ( doc->hasSelection( QTextDocument::Standard ) && + !( t = doc->selectedText( QTextDocument::Standard, qt_enable_richtext_copy ) ).isEmpty() ) { + QApplication::clipboard()->setText( t ); removeSelectedText(); } - if ( hasFocus() || viewport()->hasFocus() ) { - int h = cursor->parag()->lineHeightOfChar( cursor->index() ); - if ( !readonly ) { - QFont f = cursor->parag()->at( cursor->index() )->format()->font(); - setMicroFocusHint( cursor->x() - contentsX() + frameWidth(), - cursor->y() + cursor->parag()->rect().y() - contentsY() + frameWidth(), 0, h, TRUE ); - } - } + updateMicroFocusHint(); } /*! Copies any selected text (from selection 0) to the clipboard. @@ -2357,10 +2393,12 @@ void QTextEdit::cut() */ void QTextEdit::copy() { - if ( !doc->selectedText( QTextDocument::Standard ).isEmpty() ) - doc->copySelectedText( QTextDocument::Standard ); + QString t = doc->selectedText( QTextDocument::Standard, qt_enable_richtext_copy ); + if ( doc->hasSelection( QTextDocument::Standard ) && + !t.isEmpty() && t.simplifyWhiteSpace() != "<selstart/>" ) + QApplication::clipboard()->setText( t ); } /*! Re-indents the current paragraph. @@ -2411,19 +2449,19 @@ bool QTextEdit::focusNextPrevChild( bool n ) void QTextEdit::setFormat( QTextFormat *f, int flags ) { if ( doc->hasSelection( QTextDocument::Standard ) ) { drawCursor( FALSE ); - QString str = doc->selectedText( QTextDocument::Standard ); QTextCursor c1 = doc->selectionStartCursor( QTextDocument::Standard ); + c1.restoreState(); QTextCursor c2 = doc->selectionEndCursor( QTextDocument::Standard ); + c2.restoreState(); clearUndoRedo(); undoRedoInfo.type = UndoRedoInfo::Format; - undoRedoInfo.id = c1.parag()->paragId(); + undoRedoInfo.id = c1.paragraph()->paragId(); undoRedoInfo.index = c1.index(); - undoRedoInfo.eid = c2.parag()->paragId(); + undoRedoInfo.eid = c2.paragraph()->paragId(); undoRedoInfo.eindex = c2.index(); - undoRedoInfo.d->text = str; - readFormats( c1, c2, 0, undoRedoInfo.d->text ); + readFormats( c1, c2, undoRedoInfo.d->text ); undoRedoInfo.format = f; undoRedoInfo.flags = flags; clearUndoRedo(); doc->setFormat( QTextDocument::Standard, f, flags ); @@ -2442,14 +2480,14 @@ void QTextEdit::setFormat( QTextFormat *f, int flags ) } emit currentFontChanged( currentFormat->font() ); emit currentColorChanged( currentFormat->color() ); emit currentVerticalAlignmentChanged( (VerticalAlignment)currentFormat->vAlign() ); - if ( cursor->index() == cursor->parag()->length() - 1 ) { + if ( cursor->index() == cursor->paragraph()->length() - 1 ) { currentFormat->addRef(); - cursor->parag()->string()->setFormat( cursor->index(), currentFormat, TRUE ); - if ( cursor->parag()->length() == 1 ) { - cursor->parag()->invalidate( 0 ); - cursor->parag()->format(); + cursor->paragraph()->string()->setFormat( cursor->index(), currentFormat, TRUE ); + if ( cursor->paragraph()->length() == 1 ) { + cursor->paragraph()->invalidate( 0 ); + cursor->paragraph()->format(); repaintChanged(); } } } @@ -2466,9 +2504,14 @@ void QTextEdit::setPalette( const QPalette &p ) updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); } } -/*! +/*! \internal + + \warning In Qt 3.1 we will provide a cleaer API for the + functionality which is provided by this function and in Qt 4.0 this + function will go away. + Sets the paragraph style of the current paragraph to \a dm. If \a dm is QStyleSheetItem::DisplayListItem, the type of the list item is set to \a listStyle. @@ -2480,51 +2523,39 @@ void QTextEdit::setParagType( QStyleSheetItem::DisplayMode dm, QStyleSheetItem:: if ( isReadOnly() ) return; drawCursor( FALSE ); - if ( !doc->hasSelection( QTextDocument::Standard ) ) { - clearUndoRedo(); - undoRedoInfo.type = UndoRedoInfo::ParagType; - QValueList< QPtrVector<QStyleSheetItem> > oldStyles; - undoRedoInfo.oldStyles.clear(); - undoRedoInfo.oldStyles << cursor->parag()->styleSheetItems(); - undoRedoInfo.oldListStyles.clear(); - undoRedoInfo.oldListStyles << cursor->parag()->listStyle(); - undoRedoInfo.list = dm == QStyleSheetItem::DisplayListItem; - undoRedoInfo.listStyle = listStyle; - undoRedoInfo.id = cursor->parag()->paragId(); - undoRedoInfo.eid = cursor->parag()->paragId(); - undoRedoInfo.d->text = " "; - undoRedoInfo.index = 1; - clearUndoRedo(); - cursor->parag()->setList( dm == QStyleSheetItem::DisplayListItem, listStyle ); - repaintChanged(); - } else { - QTextParag *start = doc->selectionStart( QTextDocument::Standard ); - QTextParag *end = doc->selectionEnd( QTextDocument::Standard ); - lastFormatted = start; - clearUndoRedo(); - undoRedoInfo.type = UndoRedoInfo::ParagType; - undoRedoInfo.id = start->paragId(); - undoRedoInfo.eid = end->paragId(); - undoRedoInfo.list = dm == QStyleSheetItem::DisplayListItem; - undoRedoInfo.listStyle = listStyle; - undoRedoInfo.oldStyles.clear(); - undoRedoInfo.oldListStyles.clear(); - while ( start ) { - undoRedoInfo.oldStyles << start->styleSheetItems(); - undoRedoInfo.oldListStyles << start->listStyle(); - start->setList( dm == QStyleSheetItem::DisplayListItem, listStyle ); - if ( start == end ) - break; - start = start->next(); + QTextParagraph *start = cursor->paragraph(); + QTextParagraph *end = start; + if ( doc->hasSelection( QTextDocument::Standard ) ) { + start = doc->selectionStartCursor( QTextDocument::Standard ).topParagraph(); + end = doc->selectionEndCursor( QTextDocument::Standard ).topParagraph(); + if ( end->paragId() < start->paragId() ) + return; // do not trust our selections + } + + clearUndoRedo(); + undoRedoInfo.type = UndoRedoInfo::Style; + undoRedoInfo.id = start->paragId(); + undoRedoInfo.eid = end->paragId(); + undoRedoInfo.styleInformation = QTextStyleCommand::readStyleInformation( doc, undoRedoInfo.id, undoRedoInfo.eid ); + + while ( start != end->next() ) { + start->setListStyle( listStyle ); + if ( dm == QStyleSheetItem::DisplayListItem ) { + start->setListItem( TRUE ); + if( start->listDepth() == 0 ) + start->setListDepth( 1 ); + } else if ( start->isListItem() ) { + start->setListItem( FALSE ); + start->setListDepth( QMAX( start->listDepth()-1, 0 ) ); } - undoRedoInfo.d->text = " "; - undoRedoInfo.index = 1; - clearUndoRedo(); - repaintChanged(); - formatMore(); + start = start->next(); } + + clearUndoRedo(); + repaintChanged(); + formatMore(); drawCursor( TRUE ); setModified(); emit textChanged(); } @@ -2533,61 +2564,39 @@ void QTextEdit::setParagType( QStyleSheetItem::DisplayMode dm, QStyleSheetItem:: Sets the alignment of the current paragraph to \a a. Valid alignments are \c Qt::AlignLeft, \c Qt::AlignRight, Qt::AlignJustify and Qt::AlignCenter (which centers horizontally). - \sa setParagType() */ void QTextEdit::setAlignment( int a ) { if ( isReadOnly() || block_set_alignment ) return; drawCursor( FALSE ); - if ( !doc->hasSelection( QTextDocument::Standard ) ) { - if ( cursor->parag()->alignment() != a ) { - clearUndoRedo(); - undoRedoInfo.type = UndoRedoInfo::Alignment; - QMemArray<int> oa( 1 ); - oa[ 0 ] = cursor->parag()->alignment(); - undoRedoInfo.oldAligns = oa; - undoRedoInfo.newAlign = a; - undoRedoInfo.id = cursor->parag()->paragId(); - undoRedoInfo.eid = cursor->parag()->paragId(); - undoRedoInfo.d->text = " "; - undoRedoInfo.index = 1; - clearUndoRedo(); - cursor->parag()->setAlignment( a ); - repaintChanged(); - } - } else { - QTextParag *start = doc->selectionStart( QTextDocument::Standard ); - QTextParag *end = doc->selectionEnd( QTextDocument::Standard ); - lastFormatted = start; - int len = end->paragId() - start->paragId() + 1; - clearUndoRedo(); - undoRedoInfo.type = UndoRedoInfo::Alignment; - undoRedoInfo.id = start->paragId(); - undoRedoInfo.eid = end->paragId(); - QMemArray<int> oa( QMAX( 0, len ) ); - int i = 0; - while ( start ) { - if ( i < (int)oa.size() ) - oa[ i ] = start->alignment(); - start->setAlignment( a ); - if ( start == end ) - break; - start = start->next(); - ++i; - } - undoRedoInfo.oldAligns = oa; - undoRedoInfo.newAlign = a; - undoRedoInfo.d->text = " "; - undoRedoInfo.index = 1; - clearUndoRedo(); - repaintChanged(); - formatMore(); + QTextParagraph *start = cursor->paragraph(); + QTextParagraph *end = start; + if ( doc->hasSelection( QTextDocument::Standard ) ) { + start = doc->selectionStartCursor( QTextDocument::Standard ).topParagraph(); + end = doc->selectionEndCursor( QTextDocument::Standard ).topParagraph(); + if ( end->paragId() < start->paragId() ) + return; // do not trust our selections } + + clearUndoRedo(); + undoRedoInfo.type = UndoRedoInfo::Style; + undoRedoInfo.id = start->paragId(); + undoRedoInfo.eid = end->paragId(); + undoRedoInfo.styleInformation = QTextStyleCommand::readStyleInformation( doc, undoRedoInfo.id, undoRedoInfo.eid ); + + while ( start != end->next() ) { + start->setAlignment( a ); + start = start->next(); + } + + clearUndoRedo(); + repaintChanged(); + formatMore(); drawCursor( TRUE ); if ( currentAlignment != a ) { currentAlignment = a; emit currentAlignmentChanged( currentAlignment ); @@ -2601,12 +2610,12 @@ void QTextEdit::updateCurrentFormat() int i = cursor->index(); if ( i > 0 ) --i; if ( doc->useFormatCollection() && - ( !currentFormat || currentFormat->key() != cursor->parag()->at( i )->format()->key() ) ) { + ( !currentFormat || currentFormat->key() != cursor->paragraph()->at( i )->format()->key() ) ) { if ( currentFormat ) currentFormat->removeRef(); - currentFormat = doc->formatCollection()->format( cursor->parag()->at( i )->format() ); + currentFormat = doc->formatCollection()->format( cursor->paragraph()->at( i )->format() ); if ( currentFormat->isMisspelled() ) { currentFormat->removeRef(); currentFormat = doc->formatCollection()->format( currentFormat->font(), currentFormat->color() ); } @@ -2614,10 +2623,10 @@ void QTextEdit::updateCurrentFormat() emit currentColorChanged( currentFormat->color() ); emit currentVerticalAlignmentChanged( (VerticalAlignment)currentFormat->vAlign() ); } - if ( currentAlignment != cursor->parag()->alignment() ) { - currentAlignment = cursor->parag()->alignment(); + if ( currentAlignment != cursor->paragraph()->alignment() ) { + currentAlignment = cursor->paragraph()->alignment(); block_set_alignment = TRUE; emit currentAlignmentChanged( currentAlignment ); block_set_alignment = FALSE; } @@ -2777,9 +2786,10 @@ QString QTextEdit::text( int para ) const */ void QTextEdit::setText( const QString &text, const QString &context ) { - if ( !isModified() && this->context() == context && this->text() == text ) + if ( !isModified() && isReadOnly() && + this->context() == context && this->text() == text ) return; emit undoAvailable( FALSE ); emit redoAvailable( FALSE ); @@ -2798,19 +2808,19 @@ void QTextEdit::setText( const QString &text, const QString &context ) doc->setMinimumWidth( -1 ); resizeContents( 0, 0 ); } - cursor->setDocument( doc ); - lastFormatted = doc->firstParag(); - cursor->setParag( doc->firstParag() ); - cursor->setIndex( 0 ); + lastFormatted = doc->firstParagraph(); + delete cursor; + cursor = new QTextCursor( doc ); updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); if ( isModified() ) setModified( FALSE ); emit textChanged(); formatMore(); updateCurrentFormat(); + d->scrollToAnchor = QString::null; } /*! \property QTextEdit::text @@ -2846,11 +2856,11 @@ void QTextEdit::setText( const QString &text, const QString &context ) Finds the next occurrence of the string, \a expr. Returns TRUE if \a expr is found; otherwise returns FALSE. If \a para and \a index are both null the search begins from the - start of the text. If \a para and \a index are both not null, the - search begins from the \e *\a index character position in the \e - *\a para paragraph. + current cursor position. If \a para and \a index are both not + null, the search begins from the \e *\a index character position + in the \e *\a para paragraph. If \a cs is TRUE the search is case sensitive, otherwise it is case insensitive. If \a wo is TRUE the search looks for whole word matches only; otherwise it searches for any matching text. If \a @@ -2872,16 +2882,36 @@ void QTextEdit::setText( const QString &text, const QString &context ) bool QTextEdit::find( const QString &expr, bool cs, bool wo, bool forward, int *para, int *index ) { drawCursor( FALSE ); - doc->removeSelection( QTextDocument::Standard ); #ifndef QT_NO_CURSOR viewport()->setCursor( isReadOnly() ? arrowCursor : ibeamCursor ); #endif - bool found = doc->find( expr, cs, wo, forward, para, index, cursor ); - ensureCursorVisible(); + QTextCursor findcur = *cursor; + if ( para && index ) { + if ( doc->paragAt( *para ) ) + findcur.gotoPosition( doc->paragAt(*para), *index ); + else + findcur.gotoEnd(); + } else if ( doc->hasSelection( QTextDocument::Standard ) ){ + // maks sure we do not find the same selection again + if ( forward ) + findcur.gotoNextLetter(); + else + findcur.gotoPreviousLetter(); + } + removeSelection( QTextDocument::Standard ); + bool found = doc->find( findcur, expr, cs, wo, forward ); + if ( found ) { + if ( para ) + *para = findcur.paragraph()->paragId(); + if ( index ) + *index = findcur.index(); + *cursor = findcur; + repaintChanged(); + ensureCursorVisible(); + } drawCursor( TRUE ); - repaintChanged(); return found; } void QTextEdit::blinkCursor() @@ -2901,22 +2931,23 @@ void QTextEdit::blinkCursor() */ void QTextEdit::setCursorPosition( int para, int index ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return; if ( index > p->length() - 1 ) index = p->length() - 1; drawCursor( FALSE ); - cursor->setParag( p ); + cursor->setParagraph( p ); cursor->setIndex( index ); ensureCursorVisible(); drawCursor( TRUE ); + updateCurrentFormat(); emit cursorPositionChanged( cursor ); - emit cursorPositionChanged( cursor->parag()->paragId(), cursor->index() ); + emit cursorPositionChanged( cursor->paragraph()->paragId(), cursor->index() ); } /*! This function sets the \e *\a para and \e *\a index parameters to the @@ -2929,9 +2960,9 @@ void QTextEdit::setCursorPosition( int para, int index ) void QTextEdit::getCursorPosition( int *para, int *index ) const { if ( !para || !index ) return; - *para = cursor->parag()->paragId(); + *para = cursor->paragraph()->paragId(); *index = cursor->index(); } /*! Sets a selection which starts at position \a indexFrom in @@ -2957,12 +2988,12 @@ void QTextEdit::setSelection( int paraFrom, int indexFrom, repaintChanged(); } if ( selNum > doc->numSelections() - 1 ) doc->addSelection( selNum ); - QTextParag *p1 = doc->paragAt( paraFrom ); + QTextParagraph *p1 = doc->paragAt( paraFrom ); if ( !p1 ) return; - QTextParag *p2 = doc->paragAt( paraTo ); + QTextParagraph *p2 = doc->paragAt( paraTo ); if ( !p2 ) return; if ( indexFrom > p1->length() - 1 ) @@ -2972,14 +3003,14 @@ void QTextEdit::setSelection( int paraFrom, int indexFrom, drawCursor( FALSE ); QTextCursor c = *cursor; QTextCursor oldCursor = *cursor; - c.setParag( p1 ); + c.setParagraph( p1 ); c.setIndex( indexFrom ); - cursor->setParag( p2 ); + cursor->setParagraph( p2 ); cursor->setIndex( indexTo ); - doc->setSelectionStart( selNum, &c ); - doc->setSelectionEnd( selNum, cursor ); + doc->setSelectionStart( selNum, c ); + doc->setSelectionEnd( selNum, *cursor ); repaintChanged(); ensureCursorVisible(); if ( selNum != QTextDocument::Standard ) *cursor = oldCursor; @@ -3055,9 +3086,9 @@ Qt::TextFormat QTextEdit::textFormat() const */ int QTextEdit::paragraphs() const { - return doc->lastParag()->paragId() + 1; + return doc->lastParagraph()->paragId() + 1; } /*! Returns the number of lines in paragraph \a para, or -1 if there @@ -3065,9 +3096,9 @@ int QTextEdit::paragraphs() const */ int QTextEdit::linesOfParagraph( int para ) const { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return -1; return p->lines(); } @@ -3078,9 +3109,9 @@ int QTextEdit::linesOfParagraph( int para ) const */ int QTextEdit::paragraphLength( int para ) const { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return -1; return p->length() - 1; } @@ -3094,9 +3125,9 @@ int QTextEdit::paragraphLength( int para ) const */ int QTextEdit::lines() const { - QTextParag *p = doc->firstParag(); + QTextParagraph *p = doc->firstParagraph(); int l = 0; while ( p ) { l += p->lines(); p = p->next(); @@ -3114,9 +3145,9 @@ int QTextEdit::lines() const */ int QTextEdit::lineOfChar( int para, int index ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return -1; int idx, line; @@ -3247,9 +3278,9 @@ void QTextEdit::startDrag() { #ifndef QT_NO_DRAGANDDROP mousePressed = FALSE; inDoubleClick = FALSE; - QDragObject *drag = new QTextDrag( doc->selectedText( QTextDocument::Standard ), viewport() ); + QDragObject *drag = new QTextDrag( doc->selectedText( QTextDocument::Standard, qt_enable_richtext_copy ), viewport() ); if ( isReadOnly() ) { drag->dragCopy(); } else { if ( drag->drag() && QDragObject::target() != this && QDragObject::target() != viewport() ) @@ -3284,24 +3315,22 @@ void QTextEdit::selectAll( bool select ) void QTextEdit::UndoRedoInfo::clear() { if ( valid() ) { if ( type == Insert || type == Return ) - doc->addCommand( new QTextInsertCommand( doc, id, index, d->text.rawData(), oldStyles, oldListStyles, oldAligns ) ); + doc->addCommand( new QTextInsertCommand( doc, id, index, d->text.rawData(), styleInformation ) ); else if ( type == Format ) doc->addCommand( new QTextFormatCommand( doc, id, index, eid, eindex, d->text.rawData(), format, flags ) ); - else if ( type == Alignment ) - doc->addCommand( new QTextAlignmentCommand( doc, id, eid, newAlign, oldAligns ) ); - else if ( type == ParagType ) - doc->addCommand( new QTextParagTypeCommand( doc, id, eid, list, listStyle, oldStyles, oldListStyles ) ); - else if ( type != Invalid ) - doc->addCommand( new QTextDeleteCommand( doc, id, index, d->text.rawData(), oldStyles, oldListStyles, oldAligns ) ); + else if ( type == Style ) + doc->addCommand( new QTextStyleCommand( doc, id, eid, styleInformation ) ); + else if ( type != Invalid ) { + doc->addCommand( new QTextDeleteCommand( doc, id, index, d->text.rawData(), styleInformation ) ); + } } + type = Invalid; d->text = QString::null; id = -1; index = -1; - oldStyles.clear(); - oldListStyles.clear(); - oldAligns.resize( 0 ); + styleInformation = QByteArray(); } /*! @@ -3339,9 +3368,9 @@ QTextEdit::UndoRedoInfo::~UndoRedoInfo() } bool QTextEdit::UndoRedoInfo::valid() const { - return d->text.length() > 0 && id >= 0 && index >= 0; + return id >= 0 && type != Invalid; } /*! \internal @@ -3408,12 +3437,9 @@ QBrush QTextEdit::paper() const */ void QTextEdit::setLinkUnderline( bool b ) { - if ( b == doc->underlineLinks() ) - return; doc->setUnderlineLinks( b ); - updateStyles(); } bool QTextEdit::linkUnderline() const { @@ -3464,49 +3490,50 @@ int QTextEdit::heightForWidth( int w ) const void QTextEdit::append( const QString &text ) { // flush and clear the undo/redo stack if necessary - if ( isReadOnly() && undoRedoInfo.valid() ) { - undoRedoInfo.clear(); - doc->commands()->clear(); - } + undoRedoInfo.clear(); + doc->commands()->clear(); + doc->removeSelection( QTextDocument::Standard ); TextFormat f = doc->textFormat(); if ( f == AutoText ) { if ( QStyleSheet::mightBeRichText( text ) ) f = RichText; else f = PlainText; } - if ( f == PlainText ) { - QTextCursor oldc( *cursor ); - ensureFormatted( doc->lastParag() ); - bool scrollToEnd = contentsY() >= contentsHeight() - visibleHeight() - - ( horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0 ); - if ( !scrollToEnd ) - blockEnsureCursorVisible = TRUE; - cursor->gotoEnd(); - if ( cursor->index() > 0 ) - cursor->splitAndInsertEmptyParag(); - QTextCursor oldCursor2 = *cursor; - cursor->insert( text, TRUE ); - if ( doc->useFormatCollection() && currentFormat != cursor->parag()->at( cursor->index() )->format() ) { - doc->setSelectionStart( QTextDocument::Temp, &oldCursor2 ); - doc->setSelectionEnd( QTextDocument::Temp, cursor ); + + drawCursor( FALSE ); + QTextCursor oldc( *cursor ); + ensureFormatted( doc->lastParagraph() ); + bool atBottom = contentsY() >= contentsHeight() - visibleHeight(); + cursor->gotoEnd(); + if ( cursor->index() > 0 ) + cursor->splitAndInsertEmptyParagraph(); + QTextCursor oldCursor2 = *cursor; + + if ( f == Qt::PlainText ) { + cursor->insert( text, TRUE ); + if ( doc->useFormatCollection() && + currentFormat != cursor->paragraph()->at( cursor->index() )->format() ) { + doc->setSelectionStart( QTextDocument::Temp, oldCursor2 ); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); doc->setFormat( QTextDocument::Temp, currentFormat, QTextFormat::Format ); doc->removeSelection( QTextDocument::Temp ); } - formatMore(); - repaintChanged(); - ensureCursorVisible(); - drawCursor( TRUE ); - *cursor = oldc; - if ( !scrollToEnd ) - blockEnsureCursorVisible = FALSE; - } else if ( f == RichText ) { + } else { + if ( cursor->paragraph()->prev() ) + cursor->paragraph()->prev()->invalidate(0); // vertical margins might have to change doc->setRichTextInternal( text ); - repaintChanged(); } + formatMore(); + repaintChanged(); + if ( atBottom ) + scrollToBottom(); + *cursor = oldc; + if ( !isReadOnly() ) + cursorVisible = TRUE; setModified(); emit textChanged(); } @@ -3616,9 +3643,9 @@ QString QTextEdit::documentTitle() const { return doc->attributes()[ "title" ]; } -void QTextEdit::makeParagVisible( QTextParag *p ) +void QTextEdit::makeParagVisible( QTextParagraph *p ) { setContentsPos( contentsX(), QMIN( p->rect().y(), contentsHeight() - visibleHeight() ) ); } @@ -3629,25 +3656,31 @@ void QTextEdit::makeParagVisible( QTextParag *p ) */ void QTextEdit::scrollToAnchor( const QString& name ) { + if ( !isVisible() ) { + d->scrollToAnchor = name; + return; + } if ( name.isEmpty() ) return; sync(); QTextCursor cursor( doc ); - QTextParag* last = doc->lastParag(); - do { - QTextStringChar* c = cursor.parag()->at( cursor.index() ); + QTextParagraph* last = doc->lastParagraph(); + for (;;) { + QTextStringChar* c = cursor.paragraph()->at( cursor.index() ); if( c->isAnchor() ) { QString a = c->anchorName(); if ( a == name || (a.contains( '#' ) && QStringList::split( '#', a ).contains( name ) ) ) { - setContentsPos( contentsX(), QMIN( cursor.parag()->rect().top() + cursor.totalOffsetY(), contentsHeight() - visibleHeight() ) ); - return; + setContentsPos( contentsX(), QMIN( cursor.paragraph()->rect().top() + cursor.totalOffsetY(), contentsHeight() - visibleHeight() ) ); + break; } } + if ( cursor.paragraph() == last && cursor.atParagEnd() ) + break; cursor.gotoNextLetter(); - } while( cursor.parag() != last || !cursor.atParagEnd() ); + } } /*! If there is an anchor at position \a pos (in contents coordinates), its name is returned, otherwise an empty string is @@ -3657,34 +3690,34 @@ void QTextEdit::scrollToAnchor( const QString& name ) QString QTextEdit::anchorAt( const QPoint& pos ) { QTextCursor c( doc ); placeCursor( pos, &c ); - return c.parag()->at( c.index() )->anchorHref(); + return c.paragraph()->at( c.index() )->anchorHref(); } void QTextEdit::documentWidthChanged( int w ) { resizeContents( QMAX( visibleWidth(), w), contentsHeight() ); } -/*! - Updates all the rendering styles used to display the text. You will - probably want to call this function after calling setStyleSheet(). +/*! \internal + + This function does nothing */ void QTextEdit::updateStyles() { - doc->updateStyles(); - updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); } void QTextEdit::setDocument( QTextDocument *dc ) { if ( dc == doc ) return; doc = dc; - cursor->setDocument( doc ); + delete cursor; + cursor = new QTextCursor( doc ); clearUndoRedo(); + undoRedoInfo.doc = doc; lastFormatted = 0; } #ifndef QT_NO_CLIPBOARD @@ -3702,9 +3735,54 @@ void QTextEdit::setDocument( QTextDocument *dc ) void QTextEdit::pasteSubType( const QCString& subtype ) { QCString st = subtype; QString t = QApplication::clipboard()->text(st); + if ( doc->hasSelection( QTextDocument::Standard ) ) + removeSelectedText(); if ( !t.isEmpty() ) { + if ( t.startsWith( "<selstart/>" ) ) { + t.remove( 0, 11 ); + QTextCursor oldC = *cursor; + lastFormatted = cursor->paragraph(); + if ( lastFormatted->prev() ) + lastFormatted = lastFormatted->prev(); + doc->setRichTextInternal( t, cursor ); + + if ( undoEnabled && !isReadOnly() ) { + doc->setSelectionStart( QTextDocument::Temp, oldC ); + doc->setSelectionEnd( QTextDocument::Temp, *cursor ); + + checkUndoRedoInfo( UndoRedoInfo::Insert ); + if ( !undoRedoInfo.valid() ) { + undoRedoInfo.id = oldC.paragraph()->paragId(); + undoRedoInfo.index = oldC.index(); + undoRedoInfo.d->text = QString::null; + } + int oldLen = undoRedoInfo.d->text.length(); + if ( !doc->preProcessor() ) { + QString txt = doc->selectedText( QTextDocument::Temp ); + undoRedoInfo.d->text += txt; + for ( int i = 0; i < (int)txt.length(); ++i ) { + if ( txt[ i ] != '\n' && oldC.paragraph()->at( oldC.index() )->format() ) { + oldC.paragraph()->at( oldC.index() )->format()->addRef(); + undoRedoInfo.d->text. + setFormat( oldLen + i, oldC.paragraph()->at( oldC.index() )->format(), TRUE ); + } + oldC.gotoNextLetter(); + } + } + undoRedoInfo.clear(); + removeSelection( QTextDocument::Temp ); + } + + formatMore(); + setModified(); + emit textChanged(); + repaintChanged(); + ensureCursorVisible(); + return; + } + #if defined(Q_OS_WIN32) // Need to convert CRLF to LF int index = t.find( QString::fromLatin1("\r\n"), 0 ); while ( index != -1 ) { @@ -3837,9 +3915,9 @@ void QTextEdit::setWordWrap( WordWrap mode ) doc->setWidth( visibleWidth() ); doc->setMinimumWidth( -1 ); doc->invalidate(); updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); interval = 0; formatMore(); break; case WidgetWidth: @@ -3898,9 +3976,9 @@ void QTextEdit::setWrapColumnOrWidth( int value ) return; } doc->invalidate(); updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); interval = 0; formatMore(); } @@ -3951,9 +4029,9 @@ void QTextEdit::setWrapPolicy( WrapPolicy policy ) formatter->setWrapEnabled( document()->formatter()->isWrapEnabled( 0 ) ); document()->setFormatter( formatter ); doc->invalidate(); updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); interval = 0; formatMore(); } @@ -3978,16 +4056,15 @@ void QTextEdit::clear() setContentsPos( 0, 0 ); if ( cursor->isValid() ) cursor->restoreState(); doc->clear( TRUE ); - cursor->setDocument( doc ); - cursor->setParag( doc->firstParag() ); - cursor->setIndex( 0 ); + delete cursor; + cursor = new QTextCursor( doc ); lastFormatted = 0; updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); emit cursorPositionChanged( cursor ); - emit cursorPositionChanged( cursor->parag()->paragId(), cursor->index() ); + emit cursorPositionChanged( cursor->paragraph()->paragId(), cursor->index() ); } int QTextEdit::undoDepth() const { @@ -4024,9 +4101,9 @@ void QTextEdit::setUndoDepth( int d ) void QTextEdit::setTabStopWidth( int ts ) { document()->setTabStops( ts ); doc->invalidate(); - lastFormatted = doc->firstParag(); + lastFormatted = doc->firstParagraph(); interval = 0; formatMore(); updateContents( contentsX(), contentsY(), visibleWidth(), visibleHeight() ); } @@ -4045,9 +4122,15 @@ void QTextEdit::clearUndoRedo() emit undoAvailable( doc->commands()->isUndoAvailable() ); emit redoAvailable( doc->commands()->isRedoAvailable() ); } -/*! This function gets the format of the character at position \a +/*! \internal + + \warning In Qt 3.1 we will provide a cleaer API for the + functionality which is provided by this function and in Qt 4.0 this + function will go away. + + This function gets the format of the character at position \a index in paragraph \a para. Sets \a font to the character's font, \a color to the character's color and \a verticalAlignment to the character's vertical alignment. @@ -4058,9 +4141,9 @@ void QTextEdit::clearUndoRedo() bool QTextEdit::getFormat( int para, int index, QFont *font, QColor *color, VerticalAlignment *verticalAlignment ) { if ( !font || !color ) return FALSE; - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return FALSE; if ( index < 0 || index >= p->length() ) return FALSE; @@ -4069,9 +4152,15 @@ bool QTextEdit::getFormat( int para, int index, QFont *font, QColor *color, Vert *verticalAlignment = (VerticalAlignment)p->at( index )->format()->vAlign(); return TRUE; } -/*! This function gets the format of the paragraph \a para. Sets \a +/*! \internal + + \warning In Qt 3.1 we will provide a cleaer API for the + functionality which is provided by this function and in Qt 4.0 this + function will go away. + + This function gets the format of the paragraph \a para. Sets \a font to the paragraphs's font, \a color to the paragraph's color, \a verticalAlignment to the paragraph's vertical alignment, \a alignment to the paragraph's alignment, \a displayMode to the paragraph's display mode, \a listStyle to the paragraph's list style @@ -4089,16 +4178,16 @@ bool QTextEdit::getParagraphFormat( int para, QFont *font, QColor *color, int *listDepth ) { if ( !font || !color || !alignment || !displayMode || !listStyle ) return FALSE; - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return FALSE; - *font = p->paragFormat()->font(); - *color = p->paragFormat()->color(); - *verticalAlignment = (VerticalAlignment)p->paragFormat()->vAlign(); + *font = p->at(0)->format()->font(); + *color = p->at(0)->format()->color(); + *verticalAlignment = (VerticalAlignment)p->at(0)->format()->vAlign(); *alignment = p->alignment(); - *displayMode = p->style() ? p->style()->displayMode() : QStyleSheetItem::DisplayBlock; + *displayMode = p->isListItem() ? QStyleSheetItem::DisplayListItem : QStyleSheetItem::DisplayBlock; *listStyle = p->listStyle(); *listDepth = p->listDepth(); return TRUE; } @@ -4154,12 +4243,16 @@ QPopupMenu *QTextEdit::createPopupMenu( const QPoint& pos ) #endif } /*! \overload + \obsolete This function is called to create a right mouse button popup menu. If you want to create a custom popup menu, reimplement this function and return the created popup menu. Ownership of the popup menu is transferred to the caller. + + This function is only called if createPopupMenu( const QPoint & ) + returns 0. */ QPopupMenu *QTextEdit::createPopupMenu() { @@ -4172,23 +4265,10 @@ void QTextEdit::setFont( const QFont &f ) { QFont old( QScrollView::font() ); QScrollView::setFont( f ); doc->setMinimumWidth( -1 ); - - // ### that is a bit hacky - static short diff = 1; - diff *= -1; - doc->setWidth( visibleWidth() + diff ); - - int s = f.pointSize(); - bool usePixels = FALSE; - if ( s == -1 ) { - s = f.pixelSize(); - usePixels = TRUE; - } - doc->updateFontSizes( s, usePixels ); - doc->updateFontAttributes( f, old ); - lastFormatted = doc->firstParag(); + doc->setDefaultFormat( f, doc->formatCollection()->defaultFormat()->color() ); + lastFormatted = doc->firstParagraph(); formatMore(); repaintChanged(); } @@ -4272,12 +4352,11 @@ void QTextEdit::zoomTo( int size ) */ void QTextEdit::sync() { - QTextParag *p = lastFormatted; - while ( p ) { - p->format(); - p = p->next(); + while ( lastFormatted ) { + lastFormatted->format(); + lastFormatted = lastFormatted->next(); } resizeContents( contentsWidth(), doc->height() ); } @@ -4359,9 +4438,9 @@ void QTextEdit::scrollToBottom() QRect QTextEdit::paragraphRect( int para ) const { QTextEdit *that = (QTextEdit *)this; that->sync(); - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return QRect( -1, -1, -1, -1 ); return p->rect(); } @@ -4373,11 +4452,11 @@ QRect QTextEdit::paragraphRect( int para ) const int QTextEdit::paragraphAt( const QPoint &pos ) const { QTextCursor c( doc ); - c.place( pos, doc->firstParag() ); - if ( c.parag() ) - return c.parag()->paragId(); + c.place( pos, doc->firstParagraph() ); + if ( c.paragraph() ) + return c.paragraph()->paragId(); return -1; } /*! @@ -4389,12 +4468,12 @@ int QTextEdit::paragraphAt( const QPoint &pos ) const int QTextEdit::charAt( const QPoint &pos, int *para ) const { QTextCursor c( doc ); - c.place( pos, doc->firstParag() ); - if ( c.parag() ) { + c.place( pos, doc->firstParagraph() ); + if ( c.paragraph() ) { if ( para ) - *para = c.parag()->paragId(); + *para = c.paragraph()->paragId(); return c.index(); } return -1; } @@ -4402,9 +4481,9 @@ int QTextEdit::charAt( const QPoint &pos, int *para ) const /*! Sets the background color of the paragraph \a para to \a bg */ void QTextEdit::setParagraphBackgroundColor( int para, const QColor &bg ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return; p->setBackgroundColor( bg ); repaintChanged(); @@ -4415,9 +4494,9 @@ void QTextEdit::setParagraphBackgroundColor( int para, const QColor &bg ) */ void QTextEdit::clearParagraphBackground( int para ) { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return; p->clearBackgroundColor(); repaintChanged(); @@ -4429,9 +4508,9 @@ void QTextEdit::clearParagraphBackground( int para ) */ QColor QTextEdit::paragraphBackgroundColor( int para ) const { - QTextParag *p = doc->paragAt( para ); + QTextParagraph *p = doc->paragAt( para ); if ( !p ) return QColor(); QColor *c = p->backgroundColor(); if ( c ) @@ -4468,9 +4547,9 @@ bool QTextEdit::isRedoAvailable() const { return doc->commands()->isRedoAvailable(); } -void QTextEdit::ensureFormatted( QTextParag *p ) +void QTextEdit::ensureFormatted( QTextParagraph *p ) { while ( !p->isValid() ) { if ( !lastFormatted ) return; @@ -4485,13 +4564,13 @@ void QTextEdit::updateCursor( const QPoint & pos ) QTextCursor c = *cursor; placeCursor( pos, &c, TRUE ); #ifndef QT_NO_NETWORKPROTOCOL - if ( c.parag() && c.parag()->at( c.index() ) && - c.parag()->at( c.index() )->isAnchor() && - !c.parag()->at( c.index() )->anchorHref().isEmpty() ) { - if ( c.index() < c.parag()->length() - 1 ) - onLink = c.parag()->at( c.index() )->anchorHref(); + if ( c.paragraph() && c.paragraph()->at( c.index() ) && + c.paragraph()->at( c.index() )->isAnchor() && + !c.paragraph()->at( c.index() )->anchorHref().isEmpty() ) { + if ( c.index() < c.paragraph()->length() - 1 ) + onLink = c.paragraph()->at( c.index() )->anchorHref(); else onLink = QString::null; #ifndef QT_NO_CURSOR diff --git a/noncore/apps/opie-write/qtextedit.h b/noncore/apps/opie-write/qtextedit.h index b4e5701..64e8f45 100644 --- a/noncore/apps/opie-write/qtextedit.h +++ b/noncore/apps/opie-write/qtextedit.h @@ -60,11 +60,13 @@ namespace Qt3 { class QTextString; class QTextDocument; class QTextCursor; class QTextCommand; -class QTextParag; +class QTextParagraph; class QTextFormat; +class QTextEdit; class QTextBrowser; +class QTextString; class QTextEditPrivate; class Q_EXPORT QTextEdit : public QScrollView { @@ -195,14 +197,19 @@ public: QColor color() const; QFont font() const; int alignment() const; int undoDepth() const; + + // do not use, will go away virtual bool getFormat( int para, int index, QFont *font, QColor *color, VerticalAlignment *verticalAlignment ); + // do not use, will go away virtual bool getParagraphFormat( int para, QFont *font, QColor *color, VerticalAlignment *verticalAlignment, int *alignment, QStyleSheetItem::DisplayMode *displayMode, QStyleSheetItem::ListStyle *listStyle, int *listDepth ); + + bool isOverwriteMode() const { return overWrite; } QColor paragraphBackgroundColor( int para ) const; bool isUndoRedoEnabled() const; @@ -256,9 +263,12 @@ public slots: virtual void setColor( const QColor &c ); virtual void setFont( const QFont &f ); virtual void setVerticalAlignment( VerticalAlignment a ); virtual void setAlignment( int a ); + + // do not use, will go away virtual void setParagType( QStyleSheetItem::DisplayMode dm, QStyleSheetItem::ListStyle listStyle ); + virtual void setCursorPosition( int parag, int index ); virtual void setSelection( int parag_from, int index_from, int parag_to, int index_to, int selNum = 0 ); virtual void setSelectionAttributes( int selNum, const QColor &back, bool invertText ); virtual void setModified( bool m ); @@ -345,9 +355,9 @@ private slots: void documentWidthChanged( int w ); private: struct Q_EXPORT UndoRedoInfo { - enum Type { Invalid, Insert, Delete, Backspace, Return, RemoveSelected, Format, Alignment, ParagType }; + enum Type { Invalid, Insert, Delete, Backspace, Return, RemoveSelected, Format, Style }; UndoRedoInfo( QTextDocument *dc ); ~UndoRedoInfo(); void clear(); @@ -361,14 +371,9 @@ private: QTextFormat *format; int flags; Type type; QTextDocument *doc; - QMemArray<int> oldAligns; - int newAlign; - bool list; - QStyleSheetItem::ListStyle listStyle; - QValueList< QPtrVector<QStyleSheetItem> > oldStyles; - QValueList<QStyleSheetItem::ListStyle> oldListStyles; + QByteArray styleInformation; }; private: void updateCursor( const QPoint & pos ); @@ -378,9 +383,9 @@ private: void init(); void checkUndoRedoInfo( UndoRedoInfo::Type t ); void updateCurrentFormat(); bool handleReadOnlyKeyEvent( QKeyEvent *e ); - void makeParagVisible( QTextParag *p ); + void makeParagVisible( QTextParagraph *p ); #ifndef QT_NO_MIME QCString pickSpecial(QMimeSource* ms, bool always_ask, const QPoint&); #endif #ifndef QT_NO_MIMECLIPBOARD @@ -390,20 +395,21 @@ private: virtual void emitHighlighted( const QString & ) {} virtual void emitLinkClicked( const QString & ) {} - void readFormats( QTextCursor &c1, QTextCursor &c2, int oldLen, QTextString &text, bool fillStyles = FALSE ); + void readFormats( QTextCursor &c1, QTextCursor &c2, QTextString &text, bool fillStyles = FALSE ); void clearUndoRedo(); void paintDocument( bool drawAll, QPainter *p, int cx = -1, int cy = -1, int cw = -1, int ch = -1 ); void moveCursor( CursorAction action ); - void ensureFormatted( QTextParag *p ); + void ensureFormatted( QTextParagraph *p ); void placeCursor( const QPoint &pos, QTextCursor *c, bool link ); + void updateMicroFocusHint(); private: QTextDocument *doc; QTextCursor *cursor; QTimer *formatTimer, *scrollTimer, *changeIntervalTimer, *blinkTimer, *dragStartTimer; - QTextParag *lastFormatted; + QTextParagraph *lastFormatted; int interval; UndoRedoInfo undoRedoInfo; QTextFormat *currentFormat; int currentAlignment; |