File indexing completed on 2024-05-12 15:54:20

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "KTextDocument.h"
0021 
0022 #include "KChartMath_p.h"
0023 
0024 #include <QRect>
0025 #include <QAbstractTextDocumentLayout>
0026 #include <QtDebug>
0027 #include <QTextBlock>
0028 
0029 // This is an internal class that mimicks some of the behavior of a
0030 // QLabel with rich text assigned, this is mostly a workaround around
0031 // QTextDocumentLayout not being a public class.
0032 
0033 KTextDocument::KTextDocument( QObject * p )
0034     : QTextDocument( p ),
0035       mHintValid( false ),
0036       mSizeHint(),
0037       mMinimumSizeHint()
0038 {
0039 
0040 }
0041 
0042 KTextDocument::KTextDocument( const QString & text, QObject * p )
0043     : QTextDocument( text, p ),
0044       mHintValid( false ),
0045       mSizeHint(),
0046       mMinimumSizeHint()
0047 {
0048 
0049 }
0050 
0051 KTextDocument::~KTextDocument() {}
0052 
0053 
0054 QSize KTextDocument::sizeHint()
0055 {
0056     if ( !mHintValid )
0057         (void)minimumSizeHint();
0058     return mSizeHint;
0059 }
0060 
0061 QSize KTextDocument::minimumSizeHint()
0062 {
0063     /*
0064     QTextCursor cursor( this );
0065     if ( ! cursor.atEnd() )
0066         cursor.movePosition( QTextCursor::NextBlock );
0067     qDebug() << "KTextDocument::minimumSizeHint() found:" << cursor.block().text();
0068     QSizeF s( documentLayout()->blockBoundingRect( cursor.block() ).size() );
0069     qDebug() << "KTextDocument::minimumSizeHint() found rect" << documentLayout()->blockBoundingRect( cursor.block());
0070     return QSize( static_cast<int>(s.width()),
0071                   static_cast<int>(s.height()) );
0072     */
0073 
0074     if ( mHintValid )
0075         return mMinimumSizeHint;
0076 
0077     mHintValid = true;
0078     mSizeHint = sizeForWidth( -1 );
0079     QSize sz(-1, -1);
0080 
0081     // PENDING(kalle) Cache
0082     sz.rwidth() = sizeForWidth( 0 ).width();
0083     sz.rheight() = sizeForWidth( 32000 ).height();
0084     if ( mSizeHint.height() < sz.height())
0085         sz.rheight() = mSizeHint.height();
0086 
0087     mMinimumSizeHint = sz;
0088     return sz;
0089 }
0090 
0091 
0092 QSize KTextDocument::sizeForWidth(int w)
0093 {
0094     Q_UNUSED( w );
0095 
0096     setPageSize(QSize(0, 100000));
0097 
0098     return documentLayout()->documentSize().toSize();
0099 }