source: trunk/src/qt3support/text/q3simplerichtext.cpp@ 352

Last change on this file since 352 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt3Support module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3simplerichtext.h"
43
44#ifndef QT_NO_RICHTEXT
45#include "q3richtext_p.h"
46#include "qapplication.h"
47
48QT_BEGIN_NAMESPACE
49
50class Q3SimpleRichTextData
51{
52public:
53 Q3TextDocument *doc;
54 QFont font;
55 int cachedWidth;
56 bool cachedWidthWithPainter;
57 void adjustSize();
58};
59
60// Pull this private function in from qglobal.cpp
61Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n);
62
63void Q3SimpleRichTextData::adjustSize() {
64 QFontMetrics fm(font);
65 int mw = fm.width(QString(QLatin1Char('x'))) * 80;
66 int w = mw;
67 doc->doLayout(0,w);
68 if (doc->widthUsed() != 0) {
69 w = qt_int_sqrt(5 * doc->height() * doc->widthUsed() / 3);
70 doc->doLayout(0, qMin(w, mw));
71
72 if (w*3 < 5*doc->height()) {
73 w = qt_int_sqrt(2 * doc->height() * doc->widthUsed());
74 doc->doLayout(0,qMin(w, mw));
75 }
76 }
77 cachedWidth = doc->width();
78 cachedWidthWithPainter = false;
79}
80
81/*!
82 \class Q3SimpleRichText
83 \brief The Q3SimpleRichText class provides a small displayable piece of rich text.
84
85 \compat
86
87 This class encapsulates simple rich text usage in which a string
88 is interpreted as rich text and can be drawn. This is particularly
89 useful if you want to display some rich text in a custom widget. A
90 Q3StyleSheet is needed to interpret the tags and format the rich
91 text. Qt provides a default HTML-like style sheet, but you may
92 define custom style sheets.
93
94 Once created, the rich text object can be queried for its width(),
95 height(), and the actual width used (see widthUsed()). Most
96 importantly, it can be drawn on any given QPainter with draw().
97 Q3SimpleRichText can also be used to implement hypertext or active
98 text facilities by using anchorAt(). A hit test through inText()
99 makes it possible to use simple rich text for text objects in
100 editable drawing canvases.
101
102 Once constructed from a string the contents cannot be changed,
103 only resized. If the contents change, just throw the rich text
104 object away and make a new one with the new contents.
105
106 For large documents use QTextEdit or QTextBrowser. For very small
107 items of rich text you can use a QLabel.
108
109 If you are using Q3SimpleRichText to print in high resolution you
110 should call setWidth(QPainter, int) so that the content will be
111 laid out properly on the page.
112*/
113
114/*!
115 Constructs a Q3SimpleRichText from the rich text string \a text and
116 the font \a fnt.
117
118 The font is used as a basis for the text rendering. When using
119 rich text rendering on a widget \e w, you would normally specify
120 the widget's font, for example:
121
122 \snippet doc/src/snippets/code/src_qt3support_text_q3simplerichtext.cpp 0
123
124 \a context is the optional context of the rich text object. This