1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt3Support module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "q3textbrowser.h"
|
---|
43 | #ifndef QT_NO_TEXTBROWSER
|
---|
44 | #include <private/q3richtext_p.h>
|
---|
45 |
|
---|
46 | #include "qevent.h"
|
---|
47 | #include "qdesktopwidget.h"
|
---|
48 | #include "qapplication.h"
|
---|
49 | #include "qlayout.h"
|
---|
50 | #include "qpainter.h"
|
---|
51 | #include "qstack.h"
|
---|
52 | #include "stdio.h"
|
---|
53 | #include "qfile.h"
|
---|
54 | #include "qtextstream.h"
|
---|
55 | #include "qbitmap.h"
|
---|
56 | #include "qtimer.h"
|
---|
57 | #include "qimage.h"
|
---|
58 | #include "q3simplerichtext.h"
|
---|
59 | #include "q3dragobject.h"
|
---|
60 | #include "qurl.h"
|
---|
61 | #include "qcursor.h"
|
---|
62 |
|
---|
63 | QT_BEGIN_NAMESPACE
|
---|
64 |
|
---|
65 | /*!
|
---|
66 | \class Q3TextBrowser
|
---|
67 | \brief The Q3TextBrowser class provides a rich text browser with hypertext navigation.
|
---|
68 |
|
---|
69 | \compat
|
---|
70 |
|
---|
71 | This class extends Q3TextEdit (in read-only mode), adding some
|
---|
72 | navigation functionality so that users can follow links in
|
---|
73 | hypertext documents. The contents of Q3TextEdit is set with
|
---|
74 | setText(), but Q3TextBrowser has an additional function,
|
---|
75 | setSource(), which makes it possible to set the text to a named
|
---|
76 | document. The name is looked up in the text view's mime source
|
---|
77 | factory. If a document name ends with an anchor (for example, "\c
|
---|
78 | #anchor"), the text browser automatically scrolls to that position
|
---|
79 | (using scrollToAnchor()). When the user clicks on a hyperlink, the
|
---|
80 | browser will call setSource() itself, with the link's \c href
|
---|
81 | value as argument. You can track the current source by connetion
|
---|
82 | to the sourceChanged() signal.
|
---|
83 |
|
---|
84 | Q3TextBrowser provides backward() and forward() slots which you can
|
---|
85 | use to implement Back and Forward buttons. The home() slot sets
|
---|
86 | the text to the very first document displayed. The linkClicked()
|
---|
87 | signal is emitted when the user clicks a link.
|
---|
88 |
|
---|
89 | By using Q3TextEdit::setMimeSourceFactory() you can provide your
|
---|
90 | own subclass of Q3MimeSourceFactory. This makes it possible to
|
---|
91 | access data from anywhere, for example from a network or from a
|
---|
92 | database. See Q3MimeSourceFactory::data() for details.
|
---|
93 |
|
---|
94 | If you intend using the mime factory to read the data directly
|
---|
95 | from the file system, you may have to specify the encoding for the
|
---|
96 | file extension you are using. For example:
|
---|
97 | \snippet doc/src/snippets/code/src_qt3support_text_q3textbrowser.cpp 0
|
---|
98 | This is to ensure that the factory is able to resolve the document
|
---|
99 | names.
|
---|
100 |
|
---|
101 | Q3TextBrowser interprets the tags it processes in accordance with
|
---|
102 | the default style sheet. Change the style sheet with
|
---|
103 | \l{setStyleSheet()}; see QStyleSheet for details.
|
---|
104 |
|
---|
105 | If you want to provide your users with editable rich text use
|
---|
106 | Q3TextEdit. If you want a text browser without hypertext navigation
|
---|
107 | use Q3TextEdit, and use Q3TextEdit::setReadOnly() to disable
|
---|
108 | editing. If you just need to display a small piece of rich text
|
---|
109 | use QSimpleRichText or QLabel.
|
---|
110 | */
|
---|
111 |
|
---|
112 | class Q3TextBrowserData
|
---|
113 | {
|
---|
114 | public:
|
---|
115 | Q3TextBrowserData():textOrSourceChanged(false) {}
|
---|
116 |
|
---|
117 | QStack<QString> stack;
|
---|
118 | QStack<QString> forwardStack;
|
---|
119 | QString home;
|
---|
120 | QString curmain;
|
---|
121 | QString curmark;
|
---|
122 |
|
---|
123 | /*flag necessary to give the linkClicked() signal some meaningful
|
---|
124 | semantics when somebody connected to it calls setText() or
|
---|
125 | setSource() */
|
---|
126 | bool textOrSourceChanged;
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | /*!
|
---|
131 | Constructs an empty Q3TextBrowser called \a name, with parent \a
|
---|
132 | parent.
|
---|
133 | */
|
---|
134 | Q3TextBrowser::Q3TextBrowser(QWidget *parent, const char *name)
|
---|
135 | : Q3TextEdit(parent, name)
|
---|
136 | {
|
---|
137 | setReadOnly(true);
|
---|
138 | d = new Q3TextBrowserData;
|
---|
139 |
|
---|
140 | viewport()->setMouseTracking(true);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*!
|
---|
144 | \internal
|
---|
145 | */
|
---|
146 | Q3TextBrowser::~Q3TextBrowser()
|
---|
147 | {
|
---|
148 | delete d;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /*!
|
---|
153 | \property Q3TextBrowser::source
|
---|
154 | \brief the name of the displayed document.
|
---|
155 |
|
---|
156 | This is a an empty string if no document is displayed or if the
|
---|
157 | source is unknown.
|
---|
158 |
|
---|
159 | Setting this property uses the mimeSourceFactory() to lookup the
|
---|
160 | named document. It also checks for optional anchors and scrolls
|
---|
161 | the document accordingly.
|
---|
162 |
|
---|
163 | If the first tag in the document is \c{<qt type=detail>}, the
|
---|
164 | document is displayed as a popup rather than as new document in
|
---|
165 | the browser window itself. Otherwise, the document is displayed
|
---|
166 | normally in the text browser with the text set to the contents of
|
---|
167 | the named document with setText().
|
---|
168 |
|
---|
169 | If you are using the filesystem access capabilities of the mime
|
---|
170 | source factory, you must ensure that the factory knows about the
|
---|
171 | encoding of specified files; otherwise no data will be available.
|
---|
172 | The default factory handles a couple of common file extensions
|
---|
173 | such as \c *.html and \c *.txt with reasonable defaults. See
|
---|
174 | Q3MimeSourceFactory::data() for details.
|
---|
175 | */
|
---|
176 |
|
---|
177 | QString Q3TextBrowser::source() const
|
---|
178 | {
|
---|
179 | if (d->stack.isEmpty())
|
---|
180 | return QString();
|
---|
181 | else
|
---|
182 | return d->stack.top();
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*!
|
---|
186 | Reloads the current set source.
|
---|
187 | */
|
---|
188 |
|
---|
189 | void Q3TextBrowser::reload()
|
---|
190 | {
|
---|
191 | QString s = d->curmain;
|
---|
192 | d->curmain = QLatin1String("");
|
---|
193 | setSource(s);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | void Q3TextBrowser::setSource(const QString& name)
|
---|
198 | {
|
---|
199 | #ifndef QT_NO_CURSOR
|
---|
200 | if (isVisible())
|
---|
201 | qApp->setOverrideCursor(Qt::WaitCursor);
|
---|
202 | #endif
|
---|
203 | d->textOrSourceChanged = true;
|
---|
204 | QString source = name;
|
---|
205 | QString mark;
|
---|
206 | int hash = name.indexOf(QLatin1Char('#'));
|
---|
207 | if (hash != -1) {
|
---|
208 | source = name.left(hash);
|
---|
209 | mark = name.mid(hash+1);
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (source.left(5) == QLatin1String("file:"))
|
---|
213 | source = source.mid(6);
|
---|
214 |
|
---|
215 | QString url = mimeSourceFactory()->makeAbsolute(source, context());
|
---|
|
---|