00001 /**************************************************************************** 00002 ** Copyright (C) 2007 Klarälvdalens Datakonsult AB. All rights reserved. 00003 ** 00004 ** This file is part of the KD Chart library. 00005 ** 00006 ** This file may be distributed and/or modified under the terms of the 00007 ** GNU General Public License version 2 as published by the Free Software 00008 ** Foundation and appearing in the file LICENSE.GPL included in the 00009 ** packaging of this file. 00010 ** 00011 ** Licensees holding valid commercial KD Chart licenses may use this file in 00012 ** accordance with the KD Chart Commercial License Agreement provided with 00013 ** the Software. 00014 ** 00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 ** 00018 ** See http://www.kdab.net/kdchart for 00019 ** information about KDChart Commercial License Agreements. 00020 ** 00021 ** Contact info@kdab.net if any conditions of this 00022 ** licensing are not clear to you. 00023 ** 00024 **********************************************************************/ 00025 00026 #include "KDTextDocument.h" 00027 #include <QRect> 00028 #include <QAbstractTextDocumentLayout> 00029 #include <QtDebug> 00030 #include <QTextBlock> 00031 00032 #include <KDABLibFakes> 00033 00034 // This is an internal class that mimicks some of the behavior of a 00035 // QLabel with rich text assigned, this is mostly a workaround around 00036 // QTextDocumentLayout not being a public class. 00037 00038 KDTextDocument::KDTextDocument( QObject * p ) 00039 : QTextDocument( p ), 00040 mHintValid( false ), 00041 mSizeHint(), 00042 mMinimumSizeHint() 00043 { 00044 00045 } 00046 00047 KDTextDocument::KDTextDocument( const QString & text, QObject * p ) 00048 : QTextDocument( text, p ), 00049 mHintValid( false ), 00050 mSizeHint(), 00051 mMinimumSizeHint() 00052 { 00053 00054 } 00055 00056 KDTextDocument::~KDTextDocument() {} 00057 00058 00059 QSize KDTextDocument::sizeHint() 00060 { 00061 if( !mHintValid ) 00062 (void)minimumSizeHint(); 00063 return mSizeHint; 00064 } 00065 00066 QSize KDTextDocument::minimumSizeHint() 00067 { 00068 /* 00069 QTextCursor cursor( this ); 00070 if( ! cursor.atEnd() ) 00071 cursor.movePosition( QTextCursor::NextBlock ); 00072 qDebug() << "KDTextDocument::minimumSizeHint() found:" << cursor.block().text(); 00073 QSizeF s( documentLayout()->blockBoundingRect( cursor.block() ).size() ); 00074 qDebug() << "KDTextDocument::minimumSizeHint() found rect" << documentLayout()->blockBoundingRect( cursor.block()); 00075 return QSize( static_cast<int>(s.width()), 00076 static_cast<int>(s.height()) ); 00077 */ 00078 00079 if( mHintValid ) 00080 return mMinimumSizeHint; 00081 00082 mHintValid = true; 00083 mSizeHint = sizeForWidth( -1 ); 00084 QSize sz(-1, -1); 00085 00086 // PENDING(kalle) Cache 00087 sz.rwidth() = sizeForWidth( 0 ).width(); 00088 sz.rheight() = sizeForWidth( 32000 ).height(); 00089 if( mSizeHint.height() < sz.height()) 00090 sz.rheight() = mSizeHint.height(); 00091 00092 mMinimumSizeHint = sz; 00093 return sz; 00094 } 00095 00096 00097 QSize KDTextDocument::sizeForWidth(int w) 00098 { 00099 Q_UNUSED( w ); 00100 00101 setPageSize(QSize(0, 100000)); 00102 00103 return documentLayout()->documentSize().toSize(); 00104 }