📄 q3textbrowser.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3textbrowser.h"#ifndef QT_NO_TEXTBROWSER#include <private/q3richtext_p.h>#include "qevent.h"#include "qdesktopwidget.h"#include "qapplication.h"#include "qlayout.h"#include "qpainter.h"#include "qstack.h"#include "stdio.h"#include "qfile.h"#include "qtextstream.h"#include "qbitmap.h"#include "qtimer.h"#include "qimage.h"#include "q3simplerichtext.h"#include "q3dragobject.h"#include "qurl.h"#include "qcursor.h"/*! \class Q3TextBrowser q3textbrowser.h \brief The Q3TextBrowser class provides a rich text browser with hypertext navigation. \compat This class extends Q3TextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents. The contents of Q3TextEdit is set with setText(), but Q3TextBrowser has an additional function, setSource(), which makes it possible to set the text to a named document. The name is looked up in the text view's mime source factory. If a document name ends with an anchor (for example, "\c #anchor"), the text browser automatically scrolls to that position (using scrollToAnchor()). When the user clicks on a hyperlink, the browser will call setSource() itself, with the link's \c href value as argument. You can track the current source by connetion to the sourceChanged() signal. Q3TextBrowser provides backward() and forward() slots which you can use to implement Back and Forward buttons. The home() slot sets the text to the very first document displayed. The linkClicked() signal is emitted when the user clicks a link. By using Q3TextEdit::setMimeSourceFactory() you can provide your own subclass of Q3MimeSourceFactory. This makes it possible to access data from anywhere, for example from a network or from a database. See Q3MimeSourceFactory::data() for details. If you intend using the mime factory to read the data directly from the file system, you may have to specify the encoding for the file extension you are using. For example: \code mimeSourceFactory()->setExtensionType("qml", "text/utf8"); \endcode This is to ensure that the factory is able to resolve the document names. Q3TextBrowser interprets the tags it processes in accordance with the default style sheet. Change the style sheet with \l{setStyleSheet()}; see QStyleSheet for details. If you want to provide your users with editable rich text use Q3TextEdit. If you want a text browser without hypertext navigation use Q3TextEdit, and use Q3TextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QSimpleRichText or QLabel.*/class Q3TextBrowserData{public: Q3TextBrowserData():textOrSourceChanged(false) {} QStack<QString> stack; QStack<QString> forwardStack; QString home; QString curmain; QString curmark; /*flag necessary to give the linkClicked() signal some meaningful semantics when somebody connected to it calls setText() or setSource() */ bool textOrSourceChanged;};/*! Constructs an empty Q3TextBrowser called \a name, with parent \a parent.*/Q3TextBrowser::Q3TextBrowser(QWidget *parent, const char *name) : Q3TextEdit(parent, name){ setReadOnly(true); d = new Q3TextBrowserData; viewport()->setMouseTracking(true);}/*! \internal*/Q3TextBrowser::~Q3TextBrowser(){ delete d;}/*! \property Q3TextBrowser::source \brief the name of the displayed document. This is a an empty string if no document is displayed or if the source is unknown. Setting this property uses the mimeSourceFactory() to lookup the named document. It also checks for optional anchors and scrolls the document accordingly. If the first tag in the document is \c{<qt type=detail>}, the document is displayed as a popup rather than as new document in the browser window itself. Otherwise, the document is displayed normally in the text browser with the text set to the contents of the named document with setText(). If you are using the filesystem access capabilities of the mime source factory, you must ensure that the factory knows about the encoding of specified files; otherwise no data will be available. The default factory handles a couple of common file extensions such as \c *.html and \c *.txt with reasonable defaults. See Q3MimeSourceFactory::data() for details.*/QString Q3TextBrowser::source() const{ if (d->stack.isEmpty()) return QString(); else return d->stack.top();}/*! Reloads the current set source.*/void Q3TextBrowser::reload(){ QString s = d->curmain; d->curmain = QLatin1String(""); setSource(s);}void Q3TextBrowser::setSource(const QString& name){#ifndef QT_NO_CURSOR if (isVisible()) qApp->setOverrideCursor(Qt::WaitCursor);#endif d->textOrSourceChanged = true; QString source = name; QString mark; int hash = name.indexOf(QLatin1Char('#')); if (hash != -1) { source = name.left(hash); mark = name.mid(hash+1); } if (source.left(5) == QLatin1String("file:")) source = source.mid(6); QString url = mimeSourceFactory()->makeAbsolute(source, context()); QString txt; bool dosettext = false; if (!source.isEmpty() && url != d->curmain) { const QMimeSource* m = mimeSourceFactory()->data(source, context()); if (!m){ qWarning("Q3TextBrowser: no mimesource for %s", source.latin1()); } else { if (!Q3TextDrag::decode(m, txt)) { qWarning("Q3TextBrowser: cannot decode %s", source.latin1()); } } if (isVisible()) { QString firstTag = txt.left(txt.indexOf(QLatin1Char('>')) + 1); if (firstTag.left(3) == QLatin1String("<qt") && firstTag.contains(QLatin1String("type")) && firstTag.contains(QLatin1String("detail"))) { popupDetail(txt, QCursor::pos());#ifndef QT_NO_CURSOR qApp->restoreOverrideCursor();#endif return; } } d->curmain = url; dosettext = true; } d->curmark = mark; if (!mark.isEmpty()) { url += QLatin1Char('#'); url += mark; } if (d->home.count() == 0) d->home = url; if (d->stack.isEmpty() || d->stack.top() != url) d->stack.push(url); int stackCount = (int)d->stack.count(); if (d->stack.top() == url) stackCount--; emit backwardAvailable(stackCount > 0); stackCount = (int)d->forwardStack.count(); if (d->forwardStack.isEmpty() || d->forwardStack.top() == url) stackCount--; emit forwardAvailable(stackCount > 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -