1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KTextDocument.h"
21 
22 #include "KChartMath_p.h"
23 
24 #include <QRect>
25 #include <QAbstractTextDocumentLayout>
26 #include <QtDebug>
27 #include <QTextBlock>
28 
29 // This is an internal class that mimicks some of the behavior of a
30 // QLabel with rich text assigned, this is mostly a workaround around
31 // QTextDocumentLayout not being a public class.
32 
KTextDocument(QObject * p)33 KTextDocument::KTextDocument( QObject * p )
34     : QTextDocument( p ),
35       mHintValid( false ),
36       mSizeHint(),
37       mMinimumSizeHint()
38 {
39 
40 }
41 
KTextDocument(const QString & text,QObject * p)42 KTextDocument::KTextDocument( const QString & text, QObject * p )
43     : QTextDocument( text, p ),
44       mHintValid( false ),
45       mSizeHint(),
46       mMinimumSizeHint()
47 {
48 
49 }
50 
~KTextDocument()51 KTextDocument::~KTextDocument() {}
52 
53 
sizeHint()54 QSize KTextDocument::sizeHint()
55 {
56     if ( !mHintValid )
57         (void)minimumSizeHint();
58     return mSizeHint;
59 }
60 
minimumSizeHint()61 QSize KTextDocument::minimumSizeHint()
62 {
63     /*
64     QTextCursor cursor( this );
65     if ( ! cursor.atEnd() )
66         cursor.movePosition( QTextCursor::NextBlock );
67     qDebug() << "KTextDocument::minimumSizeHint() found:" << cursor.block().text();
68     QSizeF s( documentLayout()->blockBoundingRect( cursor.block() ).size() );
69     qDebug() << "KTextDocument::minimumSizeHint() found rect" << documentLayout()->blockBoundingRect( cursor.block());
70     return QSize( static_cast<int>(s.width()),
71                   static_cast<int>(s.height()) );
72     */
73 
74     if ( mHintValid )
75         return mMinimumSizeHint;
76 
77     mHintValid = true;
78     mSizeHint = sizeForWidth( -1 );
79     QSize sz(-1, -1);
80 
81     // PENDING(kalle) Cache
82     sz.rwidth() = sizeForWidth( 0 ).width();
83     sz.rheight() = sizeForWidth( 32000 ).height();
84     if ( mSizeHint.height() < sz.height())
85         sz.rheight() = mSizeHint.height();
86 
87     mMinimumSizeHint = sz;
88     return sz;
89 }
90 
91 
sizeForWidth(int w)92 QSize KTextDocument::sizeForWidth(int w)
93 {
94     Q_UNUSED( w );
95 
96     setPageSize(QSize(0, 100000));
97 
98     return documentLayout()->documentSize().toSize();
99 }
100