?? qtextview.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qtextview.cpp 2.3.12 edited 2005-10-27 $**** Implementation of the QTextView class**** Created : 990101**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the widgets module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qtextview.h"#ifndef QT_NO_TEXTVIEW#include "../kernel/qrichtext_p.h"#include "qapplication.h"#include "qlayout.h"#include "qpainter.h"#include "qstack.h"#include "stdio.h"#include "qfile.h"#include "qtextstream.h"#include "qlayout.h"#include "qbitmap.h"#include "qtimer.h"#include "qimage.h"#include "qmime.h"#include "qdragobject.h"#include "qclipboard.h"#include "qdragobject.h"#ifdef QT_KEYPAD_MODEextern bool qt_singleFocusWidget(const QWidget *in);extern bool qt_modalEditingEnabled;#endif/*! \class QTextView qtextview.h \brief A sophisticated single-page rich text viewer. \ingroup basic \ingroup helpsystem Unlike QSimpleRichText, which merely draws small pieces of rich text, a QTextView is a real widget, with scrollbars when necessary, for showing large text documents. The rendering style and available tags are defined by a styleSheet(). Custom tags can be created and placed in a custom style sheet. Change the style sheet with \l{setStyleSheet()}; see QStyleSheet for details. Currently, a small XML/CSS1 subset including embedded images and tables is supported. See QStyleSheet for details. Possible images within the text document are resolved by using a QMimeSourceFactory. See setMimeSourceFactory() for details. Using QTextView is quite similar to QLabel. It's mainly a call to setText() to set the contents. Setting the background color is slightly different from other widgets, since a text view is a scrollable widget that naturally provides a scrolling background. You can specify the colorgroup of the displayed text with setPaperColorGroup() or directly define the paper background with setPaper(). QTextView supports both plain color and complex pixmap backgrounds. Note that we do not intend to add a full-featured web browser widget to Qt (since that would easily double Qt's size and only few applications would benefit from it). In particular, the rich text support in Qt is supposed to provide a fast, portable and sufficient way to add reasonable online help facilities to applications. We will, however, extend it to some degree in future versions of Qt. For even more, like hypertext capabilities, see QTextBrowser.*/class QTextViewData{public: QStyleSheet* sheet_; QRichText* doc_; QMimeSourceFactory* factory_; QString original_txt; QString txt; QString contxt; QColorGroup mypapcolgrp; QColorGroup papcolgrp; QColor mylinkcol; QColor paplinkcol; bool linkunderline; QTimer* resizeTimer;#ifndef QT_NO_DRAGANDDROP QTimer* dragTimer;#endif QTimer* scrollTimer; Qt::TextFormat textformat; QRichTextFormatter* fcresize; QPoint cursor; QtTriple selorigin; QtTriple selstart; QtTriple selend; uint selection :1; uint dirty :1; uint dragselection :1; uint ownpalette : 1;};/*! Constructs an empty QTextView with the standard \a parent and \a name optional arguments.*/QTextView::QTextView(QWidget *parent, const char *name) : QScrollView( parent, name, WRepaintNoErase ){ init();}/*! Constructs a QTextView displaying the contents \a text with context \a context, with the standard \a parent and \a name optional arguments.*/QTextView::QTextView( const QString& text, const QString& context, QWidget *parent, const char *name) : QScrollView( parent, name, WRepaintNoErase ){ init(); setText( text, context );}void QTextView::init(){ d = new QTextViewData; d->mypapcolgrp = palette().active(); d->papcolgrp = d->mypapcolgrp; d->mylinkcol = blue; d->paplinkcol = d->mylinkcol; d->linkunderline = TRUE; d->fcresize = 0; setKeyCompression( TRUE ); setVScrollBarMode( QScrollView::Auto ); setHScrollBarMode( QScrollView::Auto ); d->doc_ = 0; d->sheet_ = 0; d->factory_ = 0; d->txt = QString::fromLatin1("<p></p>"); d->textformat = AutoText; d->dirty = TRUE; d->selection = FALSE; d->dragselection = FALSE; d->ownpalette = FALSE; viewport()->setBackgroundMode( PaletteBase ); viewport()->setFocusProxy( this ); viewport()->setFocusPolicy( WheelFocus ); d->resizeTimer = new QTimer( this, "qt_resizetimer" ); connect( d->resizeTimer, SIGNAL( timeout() ), this, SLOT( doResize() ));#ifndef QT_NO_DRAGANDDROP d->dragTimer = new QTimer( this ); connect( d->dragTimer, SIGNAL( timeout() ), this, SLOT( doStartDrag() ));#endif d->scrollTimer = new QTimer( this ); connect( d->scrollTimer, SIGNAL( timeout() ), this, SLOT( doAutoScroll() ));}/*! Destructs the view.*/QTextView::~QTextView(){ delete d->fcresize; QTextFormatCollection* formats = d->doc_?d->doc_->formats:0; delete d->doc_; delete formats; //#### fix inheritance structure in rich text delete d;}/*! Changes the contents of the view to the string \a text and the context to \a context. \a text may be interpreted either as plain text or as rich text, depending on the textFormat(). The default setting is \c AutoText, i.e. the text view autodetects the format from \a text. For rich text the rendering style and available tags are defined by a styleSheet(); see QStyleSheet for details. The optional \a context is used to resolve references within the text document, for example image sources. It is passed directly to the mimeSourceFactory() when quering data. \sa text(), setTextFormat()*/void QTextView::setText( const QString& text, const QString& context){ QTextFormatCollection* formats = d->doc_?d->doc_->formats:0; delete d->doc_; delete formats; //#### fix inheritance structure in rich text d->doc_ = 0; d->selection = FALSE; d->original_txt = text; d->contxt = context; if ( text.isEmpty() ) d->txt = QString::fromLatin1("<p></p>"); else if ( d->textformat == AutoText ) { if ( QStyleSheet::mightBeRichText( text ) ) d->txt = text; else d->txt = QStyleSheet::convertFromPlainText( text ); } else if ( d->textformat == PlainText ) d->txt = QStyleSheet::convertFromPlainText( text ); else // rich text d->txt = text; setContentsPos( 0, 0 ); richText().invalidateLayout(); richText().flow()->initialize( visibleWidth() ); updateLayout(); viewport()->update();}/*!\overload Changes the contents of the view to the string \a text. \a text may be interpreted either as plain text or as rich text, depending on the textFormat(). The default setting is \c AutoText, i.e. the text view autodetects the format from \a text. This function calls setText( text, QString::null ), i.e. it sets a text without any context. \sa text(), setTextFormat() */void QTextView::setText( const QString& text ){ setText( text, QString::null );}/*! Appends \a text to the current text. Useful for log viewers. \warning This function has known problems (incorrect painting and layouting). If this problem occures to you, use setText( text() + theNewText ) instead. The new richtext engine, which is part of Qt 3.0, is able to handle append(), insert(), etc. properly.*/void QTextView::append( const QString& text ){ richText().append( text, mimeSourceFactory(), styleSheet() ); if ( isVisible() ) { int y = contentsHeight(); int h = richText().lastChild()->bottomMargin(); if ( d->fcresize ) { d->fcresize->updateLayout(); doResize(); } else updateLayout(); updateContents( contentsX(), y-h, visibleWidth(), h ); } d->original_txt += text;}/*! Returns the contents of the view. \sa context(), setText()*/QString QTextView::text() const{ return d->original_txt;}/*! Returns the context of the view. \sa text(), setText()*/QString QTextView::context() const{ return d->contxt;}void QTextView::createRichText(){ if ( d->mypapcolgrp != d->papcolgrp ) viewport()->setBackgroundColor( d->mypapcolgrp.base() ); d->papcolgrp = d->mypapcolgrp; d->paplinkcol = d->mylinkcol; int offset = 8; if ( width() < 240) offset = 4; d->doc_ = new QRichText( d->txt, viewport()->font(), d->contxt, offset, mimeSourceFactory(), styleSheet() ); if (d->doc_->attributes().contains("bgcolor")){ QColor col ( d->doc_->attributes()["bgcolor"].latin1() ); if ( col.isValid() ) { d->papcolgrp.setColor( QColorGroup::Base, col ); viewport()->setBackgroundColor( col ); } } if (d->doc_->attributes().contains("link")){ QColor col ( d->doc_->attributes()["link"].latin1() ); if ( col.isValid() ) d->paplinkcol = col; } if (d->doc_->attributes().contains("text")){ QColor col ( d->doc_->attributes()["text"].latin1() ); if ( col.isValid() ) d->papcolgrp.setColor( QColorGroup::Text, col ); } if (d->doc_->attributes().contains("background")){ QString imageName = d->doc_->attributes()["background"]; QPixmap pm; const QMimeSource* m = context().isNull() ? mimeSourceFactory()->data( imageName ) : mimeSourceFactory()->data( imageName, context() ); if ( m ) { if ( !QImageDrag::decode( m, pm ) ) { qWarning("QTextImage: cannot load %s", imageName.latin1() ); } } if (!pm.isNull()) d->papcolgrp.setBrush( QColorGroup::Base, QBrush(d->papcolgrp.base(), pm) ); } d->cursor = QPoint(0,0);}/*! Returns the current style sheet of the view. \sa setStyleSheet()*/QStyleSheet* QTextView::styleSheet() const{ if (!d->sheet_) return QStyleSheet::defaultSheet(); else return d->sheet_;}/*! Sets the style sheet of the view. \sa styleSheet()*/void QTextView::setStyleSheet( QStyleSheet* styleSheet ){ d->sheet_ = styleSheet; viewport()->update();}/*! Returns the current mime source factory for the view. \sa setMimeSourceFactory()*/QMimeSourceFactory* QTextView::mimeSourceFactory() const{ if (!d->factory_) return QMimeSourceFactory::defaultFactory(); else return d->factory_;}/*! Sets the mime source factory for the view. The factory is used to resolve named references within rich text documents. If no factory has been specified, the text view uses the default factory QMimeSourceFactory::defaultFactory(). Ownership of \a factory is \e not transferred to make it possible for several text view widgets to share the same mime source. \sa mimeSourceFactory()*/void QTextView::setMimeSourceFactory( QMimeSourceFactory* factory ){ d->factory_ = factory; viewport()->update();}/*! Sets the brush to use as the background to \a pap. This may be a nice parchment or marble pixmap or simply another plain color. Technically, setPaper() is just a convenience function to set the base brush of the paperColorGroup(). \sa paper()*/void QTextView::setPaper( const QBrush& pap){ d->mypapcolgrp.setBrush( QColorGroup::Base, pap ); d->papcolgrp.setBrush( QColorGroup::Base, pap ); d->ownpalette = TRUE; viewport()->setBackgroundColor( pap.color() ); viewport()->update();}/*! Sets the full colorgroup of the paper to \a colgrp. If not specified otherwise in the document itself, any text will use QColorGroup::text(). The background will be painted with QColorGroup::brush(QColorGroup::Base). \sa paperColorGroup(), setPaper()*/void QTextView::setPaperColorGroup( const QColorGroup& colgrp){ d->mypapcolgrp = colgrp; d->papcolgrp = colgrp; d->ownpalette = TRUE; viewport()->setBackgroundColor( colgrp.base() ); viewport()->update();}/*! Returns the colorgroup of the paper. \sa setPaperColorGroup(), setPaper()*/const QColorGroup& QTextView::paperColorGroup() const{ return d->papcolgrp;}/*! Sets the color used to display links in the document to \c col. \sa linkColor() */void QTextView::setLinkColor( const QColor& col ){ d->mylinkcol = col; d->paplinkcol = col;}/*! Returns the current link color. The color may either have been set with setLinkColor() or stem from the document's body tag. \sa setLinkColor() */const QColor& QTextView::linkColor() const{ return d->paplinkcol;}/*! Defines whether or not links should be displayed underlined. */void QTextView::setLinkUnderline( bool u){ d->linkunderline = u;}/*! Returns whether or not links should be displayed underlined. */bool QTextView::linkUnderline() const{ return d->linkunderline;}/*! Returns the document title parsed from the content.*/QString QTextView::documentTitle() const{ return richText().attributes()["title"];}/*! Returns the height of the view given a width of \a w.*/int QTextView::heightForWidth( int w ) const{ QRichText doc( d->txt, viewport()->font(), d->contxt, 8, mimeSourceFactory(), styleSheet() ); doc.doLayout( 0, w ); return doc.height;}/*! Returns the document defining the view as drawable and queryable rich text object. This is not currently useful for applications.*/QRichText& QTextView::richText() const{ if (!d->doc_){ QTextView* that = (QTextView*) this; that->createRichText(); } return *d->doc_;}/*! Returns the brush used to paint the background.*/const QBrush& QTextView::paper(){#ifdef QT_KEYPAD_MODE if( qt_modalEditingEnabled ) { if ( !isModalEditing() && !qt_singleFocusWidget(this) ) return d->papcolgrp.brush( QColorGroup::Background ); }#endif return d->papcolgrp.brush( QColorGroup::Base );}/*! Returns the brush used to paint the background.*/const QBrush& QTextView::paper() const{#ifdef QT_KEYPAD_MODE if( qt_modalEditingEnabled ) { if ( !isModalEditing() && !qt_singleFocusWidget(this) ) return d->papcolgrp.brush( QColorGroup::Background ); }#endif return d->papcolgrp.brush( QColorGroup::Base );}/*! \reimp*/void QTextView::drawContentsOffset(QPainter* p, int ox, int oy, int cx, int cy, int cw, int ch){ if ( !d->ownpalette && d->mypapcolgrp == d->papcolgrp ) { d->mypapcolgrp = colorGroup(); d->papcolgrp = d->mypapcolgrp; } QTextOptions to(&paper(), d->paplinkcol, d->linkunderline ); to.offsetx = ox; to.offsety = oy; if ( d->selection ) { to.selstart = d->selstart; to.selend = d->selend; } QRegion r(cx-ox, cy-oy, cw, ch); QRichTextFormatter tc( richText() ); tc.gotoParagraph( p, richText().getParBefore( cy ) ); QTextParagraph* b = tc.paragraph;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -