⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtextview.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    QFontMetrics fm( p->fontMetrics() );    while ( b && tc.y() <= cy + ch ) {	if ( b && b->dirty ) //ensure the paragraph is laid out	    tc.updateLayout( p, cy + ch );	tc.gotoParagraph( p, b );	if ( tc.y() + tc.paragraph->height > cy ) {	    do {		tc.makeLineLayout( p );		QRect geom( tc.lineGeometry() );		if ( geom.bottom() > cy && geom.top() < cy+ch )		    tc.drawLine( p, ox, oy, cx, cy, cw, ch, r, paperColorGroup(), to );	    }	    while ( tc.gotoNextLine( p ) );	}	b = b->nextInDocument();    }    to.selstart = QtTriple();    to.selstart = to.selend;    richText().flow()->drawFloatingItems( p, ox, oy, cx, cy, cw, ch, r, paperColorGroup(), to );    p->setClipRegion(r);    if ( paper().pixmap() && !testWState(WState_GlobalBrushOrigin) )	p->drawTiledPixmap(0, 0, visibleWidth(), visibleHeight(),			   *paper().pixmap(), ox, oy);    else	p->fillRect(0, 0, visibleWidth(), visibleHeight(), paper() );    p->setClipping( FALSE );#if 0    int pagesize = richText().flow()->pagesize;    if ( pagesize > 0 ) {	p->setPen( DotLine );	for (int page = cy / pagesize; page <= (cy+ch) / pagesize; ++page ) {	    p->drawLine( cx-ox, page * pagesize - oy, cx-ox+cw, page*			 pagesize - oy );	}    }#endif}/*!  \reimp*/void QTextView::viewportResizeEvent(QResizeEvent* ){}void QTextView::doResize(){    if ( !d->fcresize->updateLayout( 0, d->fcresize->y() + d->fcresize->paragraph->height + 1000 ) )	d->resizeTimer->start( 0, TRUE );    QTextFlow* flow = richText().flow();    resizeContents( QMAX( flow->widthUsed-1, visibleWidth() ), flow->height );}/*!  \reimp*/void QTextView::resizeEvent( QResizeEvent* e ){    if ( e->size().width() == e->oldSize().width() ) {	int vw = visibleWidth();	QScrollView::resizeEvent( e );	if ( vw == visibleWidth() )	    return;    } else {	setUpdatesEnabled( FALSE ); // to hinder qscrollview from showing/hiding scrollbars. Safe since we call resizeContents later!	QScrollView::resizeEvent( e );	setUpdatesEnabled( TRUE);    }    richText().invalidateLayout();    richText().flow()->initialize( visibleWidth() );    updateLayout();}/*!  \reimp*/void QTextView::viewportMousePressEvent( QMouseEvent* e ){    if ( e->button() != LeftButton )	return;#ifdef QT_KEYPAD_MODE    if( qt_modalEditingEnabled ) {	if ( !isModalEditing() )	    setModalEditing( TRUE );    }#endif    d->cursor = e->pos() + QPoint( contentsX(), contentsY() );    QRichTextIterator it( richText() );    bool within = it.goTo( d->cursor );    bool sel = d->selection && it.position() >= d->selstart && it.position() < d->selend;    if ( !sel || !within ) {	clearSelection();	d->selorigin = it.position();	d->selstart = d->selorigin;	d->selend = d->selstart;	d->dragselection = TRUE;#ifndef QT_NO_DRAGANDDROP    } else {	d->dragTimer->start( QApplication::startDragTime(), TRUE );#endif    }}/*!  \reimp*/void QTextView::viewportMouseReleaseEvent( QMouseEvent* e ){    if ( e->button() == LeftButton ) {	d->scrollTimer->stop();#ifndef QT_NO_CLIPBOARD	if ( d->dragselection ) {#if defined(_WS_X11_)	    if ( style() == MotifStyle )		copy();#endif	    d->dragselection = FALSE;	} else#endif	{	    clearSelection();	}    }}/*!  Returns TRUE if there is any text selected, FALSE otherwise.  \sa selectedText()*/bool QTextView::hasSelectedText() const{    return d->selection;}/*!  Returns a copy of the selected text in plain text format.  \sa hasSelectedText()*/QString QTextView::selectedText() const{    if ( !d->selection )	return QString::null;    QRichTextIterator it( richText() );    it.goTo( d->selstart );    if ( d->selstart.a == d->selend.a && d->selstart.b == d->selend.b )	return it.text().mid( d->selstart.c, d->selend.c - d->selstart.c );    int column = 0;    QString txt;    QString s = it.text().mid( d->selstart.c );    while ( it.position() < d->selend ) {	if ( !s.isEmpty() ) {	    if ( column + s.length() > 79 &&		it.outmostParagraph()->style->whiteSpaceMode() == QStyleSheetItem::WhiteSpaceNormal ) {		txt += '\n';		column = 0;	    }	    if ( s[(int)s.length()-1]== '\n' )		column = 0;	    txt += s;	    column += s.length();	}	int oldpar = it.position().a;	if ( !it.right( FALSE ) )	    break;	if ( it.position().a != oldpar ) {	    txt += '\n';	    column = 0;	}	s = it.text();	if ( it.position().a == d->selend.a && it.position().b == d->selend.b )	    s = s.left( d->selend.c );    }    return txt;}static int logicalFontSize( QStyleSheet* style, QFont base, int pt ){    for (int i=0; i<10; i++) {	QFont b = base;	style->scaleFont(b,i);	if ( b.pointSize() >= pt )	    return i;    }    return 1; // else what?}static QString formatDiff(const QTextView* view, QTextCharFormat* pfmt, QTextCharFormat* nfmt){    QString txt;    QFont basefont = view->font();    if ( pfmt != nfmt ) {	QString t,pre,post;	if ( pfmt->color() != nfmt->color() ) {	    QString c;	    t += c.sprintf("color=#%06x ", nfmt->color().rgb());	}	if ( pfmt->font() != nfmt->font() ) {	    int plsz = logicalFontSize( view->styleSheet(), basefont, pfmt->font().pointSize() );	    int nlsz = logicalFontSize( view->styleSheet(), basefont, nfmt->font().pointSize() );	    if ( nlsz != plsz ) {		QString f;		t += f.sprintf("size=%d ",nlsz-plsz);	    }	    if ( pfmt->font().family() != nfmt->font().family() ) {		t += "face=";		t += nfmt->font().family();		t += " ";	    }	    if ( pfmt->font().italic() != nfmt->font().italic() ) {		bool on = nfmt->font().italic();		if ( on )		    post = post + "<i>";		else		    pre = "</i>" + pre;	    }	    if ( pfmt->font().weight() != nfmt->font().weight() ) {		bool on = nfmt->font().weight() > 50;		if ( on )		    post = post + "<b>";		else		    pre = "</b>" + pre;	    }	}	txt += pre;	if ( !t.isEmpty() ) {	    t.truncate(t.length()-1); // chop space	    txt += "<font " + t + ">";	}	txt += post;    }    return txt;}/*!  Returns a copy of the selected text in rich text format (XML).  \sa hasSelectedText()*/QString QTextView::selectedRichTextInternal() const{    if ( !d->selection )	return QString::null;    QRichTextIterator it( richText() );    it.goTo( d->selstart );    QString txt;    QString s = it.text().mid( d->selstart.c );    QTextCharFormat ifmt;    QTextCharFormat* pfmt = &ifmt;    while ( it.position() < d->selend ) {	QTextCharFormat* nfmt = it.format();	txt += formatDiff(this,pfmt,nfmt);	pfmt = nfmt;	txt += s;	int oldpar = it.position().a;	if ( !it.right( FALSE ) )	    break;	if ( it.position().a != oldpar )	    txt += "</p><p>";	s = it.text();	if ( it.position().a == d->selend.a && it.position().b == d->selend.b )	    s = s.left( d->selend.c );    }    txt += formatDiff(this,pfmt,&ifmt);    return txt;}#ifndef QT_NO_CLIPBOARD/*!  Copies the marked text to the clipboard.*/void QTextView::copy(){#if defined(_WS_X11_)    disconnect( QApplication::clipboard(), SIGNAL(dataChanged()), this, 0);#endif    QString t = selectedText();    QRegExp nbsp(QChar(0x00a0U));    t.replace( nbsp, " " );#if defined(_OS_WIN32_)    // Need to convert NL to CRLF    QRegExp nl("\\n");    t.replace( nl, "\r\n" );#endif    QApplication::clipboard()->setText( t );#if defined(_WS_X11_)    connect( QApplication::clipboard(), SIGNAL(dataChanged()),	     this, SLOT(clipboardChanged()) );#endif}#endif/*!  Selects all text.*/void QTextView::selectAll(){    QRichTextIterator it( richText() );    d->selstart = it.position();    while ( it.right( FALSE ) ) { }    d->selend = it.position();    viewport()->update();    d->selection = TRUE;#if defined(_WS_X11_)    copy();#endif}/*!  \reimp*/void QTextView::viewportMouseMoveEvent( QMouseEvent* e){    if (e->state() & LeftButton ) {	if (d->dragselection ) {	    doSelection( e->pos() );	    ensureVisible( d->cursor.x(), d->cursor.y() );#ifndef QT_NO_DRAGANDDROP	} else if ( d->dragTimer->isActive() ) {	    d->dragTimer->stop();	    doStartDrag();#endif	}    }}/*!  Provides scrolling and paging.*/void QTextView::keyPressEvent( QKeyEvent * e){#ifdef QT_KEYPAD_MODE    switch( e->key() ) {	case Key_Select:	    if( qt_modalEditingEnabled ) {		setModalEditing( !isModalEditing() );	    }	    return;	case Key_Back:	case Key_No:	    if ( qt_modalEditingEnabled && isModalEditing() ) {		setModalEditing( FALSE );	    } else {		e->ignore();	    }	    return;	default:	    if( qt_modalEditingEnabled ) {		if ( !isModalEditing() ) {		    e->ignore();		    return;		}	    }    }#endif    int unknown = 0;    switch (e->key()) {    case Key_Right:	scrollBy( 10, 0 );	break;    case Key_Left:	scrollBy( -10, 0 );	break;    case Key_Up:	scrollBy( 0, -10 );	break;    case Key_Down:	scrollBy( 0, 10 );	break;    case Key_Home:	setContentsPos(0,0);	break;    case Key_End:	setContentsPos(0,contentsHeight()-visibleHeight());	break;    case Key_PageUp:	scrollBy( 0, -visibleHeight() );	break;    case Key_PageDown:	scrollBy( 0, visibleHeight() );	break;#ifndef QT_NO_CLIPBOARD    case Key_F16: // Copy key on Sun keyboards	copy();	break;#if defined (_WS_WIN_)    case Key_Insert:#endif    case Key_C:	if ( e->state() & ControlButton )	    copy();	break;#endif    default:	unknown++;    }    if ( unknown )				// unknown key	e->ignore();}/*!  \reimp*/void QTextView::paletteChange( const QPalette & p ){    QScrollView::paletteChange( p );    if ( !d->ownpalette ) {	d->mypapcolgrp = palette().active();	d->papcolgrp = d->mypapcolgrp;    }}/*!  Returns the current text format.  \sa setTextFormat() */Qt::TextFormat QTextView::textFormat() const{    return d->textformat;}/*!  Sets the text format to \a format. Possible choices are  <ul>  <li> \c PlainText - all characters are displayed verbatim,  including all blanks and linebreaks.  <li> \c RichText - rich text rendering. The available  styles are defined in the default stylesheet  QStyleSheet::defaultSheet().  <li> \c AutoText - this is also the default. The label  autodetects which rendering style suits best, \c PlainText  or \c RichText. Technically, this is done by using the  QStyleSheet::mightBeRichText() heuristic.  </ul> */void QTextView::setTextFormat( Qt::TextFormat format ){    d->textformat = format;    setText( d->original_txt, d->contxt ); // trigger update}/*!\internal */void QTextView::updateLayout(){    if ( !isVisible() ) {	d->dirty = TRUE;	return;    }    QSize cs( viewportSize( contentsWidth(), contentsHeight() ) );    int ymax = contentsY() + cs.height() + 1;    delete d->fcresize;    d->fcresize = new QRichTextFormatter( richText() );    d->fcresize->initParagraph( 0, &richText() );    d->fcresize->updateLayout( 0, ymax );    QTextFlow* flow = richText().flow();    QSize vs( viewportSize( flow->widthUsed, flow->height ) );    // This condition is wrong. See Qtopia bug 2271 (among others).    //if ( vs.width() != visibleWidth() ) {	flow->initialize( vs.width() );	richText().invalidateLayout();	d->fcresize->gotoParagraph( 0, &richText() );	d->fcresize->updateLayout( 0, ymax );    //}    resizeContents( QMAX( flow->widthUsed-1, vs.width() ), flow->height );    d->resizeTimer->start( 0, TRUE );    d->dirty = FALSE;}/*!\reimp */void QTextView::showEvent( QShowEvent* ){    if ( d->dirty )	updateLayout();}void QTextView::clearSelection(){#ifndef QT_NO_DRAGANDDROP    d->dragTimer->stop();#endif    if ( !d->selection )	return; // nothing to do    d->selection = FALSE;    QRichTextIterator it( richText() );    it.goTo( d->selend );    int y = it.lineGeometry().bottom();    it.goTo( d->selstart );    if ( y - it.lineGeometry().top()  >= visibleHeight() )	viewport()->update();    else {	QRect r = it.lineGeometry();	while ( it.position() < d->selend && it.right() ) {	    r = r.unite( it.lineGeometry() );	}	updateContents( r );    }}#ifndef QT_NO_DRAGANDDROPvoid QTextView::doStartDrag(){    QTextDrag* drag = new QTextDrag( selectedText(), this ) ;    drag->drag();}#endifvoid QTextView::doAutoScroll(){    QPoint pos = viewport()->mapFromGlobal( QCursor::pos() );    if ( pos.y() < 0 )	scrollBy( 0, -32 );    else if (pos.y() > visibleHeight() )	scrollBy( 0, 32 );    doSelection( pos );}void QTextView::doSelection( const QPoint& pos ){    QPoint to( pos + QPoint( contentsX(), contentsY()  ) );    if ( to != d->cursor ) {	QRichTextIterator it( richText() );	it.goTo( to );	d->selection = TRUE;	if ( (it.position() != d->selstart) && (it.position()  != d->selend) ) {	    if ( it.position() < d->selorigin ) {		d->selstart = it.position();		d->selend = d->selorigin;	    } else {		d->selstart = d->selorigin;		d->selend = it.position();	    }	    QRichTextIterator it2( richText() );	    it2.goTo( d->cursor );	    QRect r = it2.lineGeometry();	    r = r.unite( it.lineGeometry() );	    while ( it.position() < it2.position() && it.right( FALSE ) )		r = r.unite( it.lineGeometry() );	    while ( it2.position() < it.position() && it2.right( FALSE ) )		r = r.unite( it2.lineGeometry() );	    d->cursor = to;	    repaintContents( r, FALSE );	}    }    if ( pos.y() < 0 || pos.y() > visibleHeight() )	d->scrollTimer->start( 100, FALSE );    else	d->scrollTimer->stop();}void QTextView::clipboardChanged(){#if defined(_WS_X11_)    disconnect( QApplication::clipboard(), SIGNAL(dataChanged()),		this, SLOT(clipboardChanged()) );    clearSelection();#endif}/*!\reimp */void QTextView::focusInEvent( QFocusEvent * ){    setMicroFocusHint(width()/2, 0, 1, height(), FALSE);}/*!\reimp */void QTextView::focusOutEvent( QFocusEvent * ){}#endif  // QT_NO_TEXTVIEW

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -