📄 qtextbrowser.cpp
字号:
/*! \property QTextBrowser::undoRedoEnabled \brief whether the text browser supports undo/redo operations*/void QTextBrowserPrivate::init(){ Q_Q(QTextBrowser); q->setReadOnly(true);#ifndef QT_NO_CURSOR viewport->setCursor(oldCursor);#endif q->setUndoRedoEnabled(false); viewport->setMouseTracking(true); QObject::connect(q->document(), SIGNAL(contentsChanged()), q, SLOT(_q_documentModified()));}/*! Constructs an empty QTextBrowser with parent \a parent.*/QTextBrowser::QTextBrowser(QWidget *parent) : QTextEdit(*new QTextBrowserPrivate, parent){ Q_D(QTextBrowser); d->init();}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QTextBrowser::QTextBrowser(QWidget *parent, const char *name) : QTextEdit(*new QTextBrowserPrivate, parent){ setObjectName(QString::fromAscii(name)); Q_D(QTextBrowser); d->init();}#endif/*! \internal*/QTextBrowser::~QTextBrowser(){}/*! \property QTextBrowser::source \brief the name of the displayed document. This is a an invalid url if no document is displayed or if the source is unknown. When setting this property QTextBrowser tries to find a document with the specified name in the paths of the searchPaths property and directory of the current source, unless the value is an absolute file path. 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 setHtml().*/QUrl QTextBrowser::source() const{ Q_D(const QTextBrowser); if (d->stack.isEmpty()) return QUrl(); else return d->stack.top().url;}/*! \property QTextBrowser::searchPaths \brief the search paths used by the text browser to find supporting content QTextBrowser uses this list to locate images and documents.*/QStringList QTextBrowser::searchPaths() const{ Q_D(const QTextBrowser); return d->searchPaths;}void QTextBrowser::setSearchPaths(const QStringList &paths){ Q_D(QTextBrowser); d->searchPaths = paths;}/*! Reloads the current set source.*/void QTextBrowser::reload(){ Q_D(QTextBrowser); QUrl s = d->currentURL; d->currentURL = QUrl(); setSource(s);}void QTextBrowser::setSource(const QUrl &url){ Q_D(QTextBrowser); int hpos = d->hbar->value(); int vpos = d->vbar->value(); d->setSource(url); if (!url.isValid()) return; if (!d->stack.isEmpty() && d->stack.top().url == url) { // the same url you are already watching } else { if (!d->stack.isEmpty()) { d->stack.top().hpos = hpos; d->stack.top().vpos = vpos; } QTextBrowserPrivate::HistoryEntry entry; entry.url = url; entry.hpos = 0; entry.vpos = 0; d->stack.push(entry); emit backwardAvailable(d->stack.count() > 1); if (!d->forwardStack.isEmpty() && d->forwardStack.top().url == url) { d->forwardStack.pop(); emit forwardAvailable(d->forwardStack.count() > 0); } else { d->forwardStack.clear(); emit forwardAvailable(false); } }}/*! \fn void QTextBrowser::backwardAvailable(bool available) This signal is emitted when the availability of backward() changes. \a available is false when the user is at home(); otherwise it is true.*//*! \fn void QTextBrowser::forwardAvailable(bool available) This signal is emitted when the availability of forward() changes. \a available is true after the user navigates backward() and false when the user navigates or goes forward().*//*! \fn void QTextBrowser::sourceChanged(const QUrl &src) This signal is emitted when the source has changed, \a src being the new source. Source changes happen both programmatically when calling setSource(), forward(), backword() or home() or when the user clicks on links or presses the equivalent key sequences.*//*! \fn void QTextBrowser::highlighted(const QUrl &link) This signal is emitted when the user has selected but not activated an anchor in the document. The URL referred to by the anchor is passed in \a link.*//*! \fn void QTextBrowser::highlighted(const QString &link) \overload Convenience signal that allows connecting to a slot that takes just a QString, like for example QStatusBar's message().*//*! \fn void QTextBrowser::anchorClicked(const QUrl &link) This signal is emitted when the user clicks an anchor. The URL referred to by the anchor is passed in \a link. Note that the browser will automatically handle navigation to the location specified by \a link unless you call setSource() in a slot connected. This mechanism is used to override the default navigation features of the browser.*//*! Changes the document displayed to the previous document in the list of documents built by navigating links. Does nothing if there is no previous document. \sa forward(), backwardAvailable()*/void QTextBrowser::backward(){ Q_D(QTextBrowser); if (d->stack.count() <= 1) return; d->forwardStack.push(d->stack.pop()); d->forwardStack.top().hpos = d->hbar->value(); d->forwardStack.top().vpos = d->vbar->value(); d->setSource(d->stack.top().url); d->hbar->setValue(d->stack.top().hpos); d->vbar->setValue(d->stack.top().vpos); emit backwardAvailable(d->stack.count() > 1); emit forwardAvailable(true);}/*! Changes the document displayed to the next document in the list of documents built by navigating links. Does nothing if there is no next document. \sa backward(), forwardAvailable()*/void QTextBrowser::forward(){ Q_D(QTextBrowser); if (d->forwardStack.isEmpty()) return; if (!d->stack.isEmpty()) { d->stack.top().hpos = d->hbar->value(); d->stack.top().vpos = d->vbar->value(); } d->stack.push(d->forwardStack.pop()); setSource(d->stack.top().url); d->hbar->setValue(d->stack.top().hpos); d->vbar->setValue(d->stack.top().vpos); emit backwardAvailable(true); emit forwardAvailable(!d->forwardStack.isEmpty());}/*! Changes the document displayed to be the first document the browser displayed.*/void QTextBrowser::home(){ Q_D(QTextBrowser); if (d->home.isValid()) setSource(d->home);}/*! The event \a ev is used to provide the following keyboard shortcuts: \table \header \i Keypress \i Action \row \i Alt+Left Arrow \i \l backward() \row \i Alt+Right Arrow \i \l forward() \row \i Alt+Up Arrow \i \l home() \endtable*/void QTextBrowser::keyPressEvent(QKeyEvent *ev){ Q_D(QTextBrowser);#ifdef QT_KEYPAD_NAVIGATION switch (ev->key()) { case Qt::Key_Select: if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { setEditFocus(true); return; } break; case Qt::Key_Back: if (QApplication::keypadNavigationEnabled()) { if (hasEditFocus()) { setEditFocus(false); ev->accept(); return; } } QTextEdit::keyPressEvent(ev); return; default: if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { ev->ignore(); return; } }#endif if (ev->modifiers() & Qt::AltModifier) { switch (ev->key()) { case Qt::Key_Right: forward(); ev->accept(); return; case Qt::Key_Left: backward(); ev->accept(); return; case Qt::Key_Up: home(); ev->accept(); return; } } else if ((ev->key() == Qt::Key_Return#ifdef QT_KEYPAD_NAVIGATION || ev->key() == Qt::Key_Select#endif || ev->key() == Qt::Key_Enter) && d->focusIndicator.hasSelection()) { QTextCursor cursor = d->focusIndicator; if (cursor.selectionStart() != cursor.position()) cursor.setPosition(cursor.selectionStart()); cursor.movePosition(QTextCursor::NextCharacter); ev->accept(); const QString href = cursor.charFormat().anchorHref(); d->activateAnchor(href); return; }#ifdef QT_KEYPAD_NAVIGATION else if (QApplication::keypadNavigationEnabled()) { if (ev->key() == Qt::Key_Up) { d->keypadMove(false); return; } else if (ev->key() == Qt::Key_Down) { d->keypadMove(true); return; } }#endif QTextEdit::keyPressEvent(ev);}/*! \reimp*/void QTextBrowser::mouseMoveEvent(QMouseEvent *e){ Q_D(QTextBrowser); QTextEdit::mouseMoveEvent(e); QString anchor = anchorAt(e->pos()); if (anchor.isEmpty()) {#ifndef QT_NO_CURSOR if (d->viewport->cursor().shape() != Qt::PointingHandCursor) d->oldCursor = d->viewport->cursor(); d->viewport->setCursor(d->oldCursor);#endif emit highlighted(QUrl()); emit highlighted(QString()); } else {#ifndef QT_NO_CURSOR d->viewport->setCursor(Qt::PointingHandCursor);#endif const QUrl url = isAbsoluteFileName(d->currentURL.toLocalFile()) ? d->currentURL.resolved(anchor) : QUrl(anchor); emit highlighted(url); // convenience to ease connecting to QStatusBar::showMessage(const QString &) emit highlighted(url.toString()); }}/*! \reimp*/void QTextBrowser::mousePressEvent(QMouseEvent *e){ Q_D(QTextBrowser); d->anchorOnMousePress = anchorAt(e->pos()); if (!d->cursor.hasSelection() && !d->anchorOnMousePress.isEmpty()) d->setCursorPosition(e->pos()); QTextEdit::mousePressEvent(e); d->hadSelectionOnMousePress = d->cursor.hasSelection();}/*! \reimp*/void QTextBrowser::mouseReleaseEvent(QMouseEvent *e){ Q_D(QTextBrowser); QTextEdit::mouseReleaseEvent(e); if (!(e->button() & Qt::LeftButton)) return; const QString anchor = anchorAt(e->pos()); if (anchor.isEmpty()) return; if (!d->cursor.hasSelection() || (anchor == d->anchorOnMousePress && d->hadSelectionOnMousePress)) d->activateAnchor(anchor);}/*! \reimp*/void QTextBrowser::focusOutEvent(QFocusEvent *ev){ Q_D(QTextBrowser); if (ev->reason() != Qt::ActiveWindowFocusReason && ev->reason() != Qt::PopupFocusReason) { d->focusIndicator.clearSelection(); d->viewport->update(); }#ifndef QT_NO_CURSOR d->viewport->setCursor(d->readOnly ? d->oldCursor : Qt::IBeamCursor);#endif QTextEdit::focusOutEvent(ev);}/*! \reimp*/bool QTextBrowser::focusNextPrevChild(bool next){ Q_D(QTextBrowser); if (!d->readOnly) return QTextEdit::focusNextPrevChild(next); int anchorStart, anchorEnd; if (d->findNextPrevAnchor(next, anchorStart, anchorEnd)) { d->focusIndicator.setPosition(anchorStart); d->focusIndicator.setPosition(anchorEnd, QTextCursor::KeepAnchor); } else { d->focusIndicator.clearSelection(); } if (d->focusIndicator.hasSelection()) { qSwap(d->focusIndicator, d->cursor); ensureCursorVisible(); qSwap(d->focusIndicator, d->cursor); d->viewport->update(); return true; } else { d->viewport->update(); return QTextEdit::focusNextPrevChild(next); }}/*! \reimp*/void QTextBrowser::paintEvent(QPaintEvent *e){ Q_D(QTextBrowser); QPainter p(d->viewport); d->paint(&p, e);}/*! This function is called when the document is loaded. The \a type indicates the type of resource to be loaded. For each image in the document, this function is called once. The default implementation ignores \a type and tries to locate the resources by interpreting \a name as a file name. If it is not an absolute path it tries to find the file in the paths of the \l searchPaths property and in the same directory as the current source. On success, the result is a QVariant that stores a QByteArray with the contents of the file. If you reimplement this function, you can return other QVariant types. The table below shows which variant types are supported depending on the resource type: \table \header \i ResourceType \i QVariant::Type \row \i QTextDocument::HtmlResource \i QString or QByteArray \row \i QTextDocument::ImageResource \i QImage, QPixmap or QByteArray \endtable*/QVariant QTextBrowser::loadResource(int /*type*/, const QUrl &name){ Q_D(QTextBrowser); QByteArray data; QUrl resolved = name; if (!isAbsoluteFileName(name.toLocalFile()) && isAbsoluteFileName(source().toLocalFile())) resolved = source().resolved(name); QString fileName = d->findFile(resolved); QFile f(fileName); if (f.open(QFile::ReadOnly)) { data = f.readAll(); f.close(); } else { qWarning("QTextBrowser: cannot open '%s' for reading", fileName.toLocal8Bit().data()); } return data;}/*! \reimp */bool QTextBrowser::event(QEvent *e){ return QTextEdit::event(e);}#include "moc_qtextbrowser.cpp"#endif // QT_NO_TEXTBROWSER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -