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

📄 qtabbar.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*!  Repaints the tab row.  All the painting is done by paint();  paintEvent() only decides which tabs need painting and in what  order.  \sa paint()*/void QTabBar::paintEvent( QPaintEvent * e ){    QPainter p( this );    if ( backgroundMode() == X11ParentRelative ) {	erase();    } else {	if ( !testWState(WState_GlobalBrushOrigin) ) 	    p.setBrushOrigin( rect().bottomLeft() );	p.fillRect( 0, 0, width(), height(),		    QBrush( colorGroup().brush( QColorGroup::Background ) ));    }    QTab * t;    t = l->first();    do {	QTab * n = l->next();	if ( t && t->r.intersects( e->rect() ) )	    paint( &p, t, n == 0 );	t = n;    } while ( t != 0 );    if ( d->scrolls && lstatic->first()->r.left() < 0 ) {	QPointArray a;	int h = height();	if ( d->s == RoundedAbove ) {	    p.fillRect( 0, 3, 4, h-5,			QBrush( colorGroup().brush( QColorGroup::Background ) ));	    a.setPoints( 5,  0,2,  3,h/4, 0,h/2, 3,3*h/4, 0,h );	} else if ( d->s == RoundedBelow ) {	    p.fillRect( 0, 2, 4, h-5,			QBrush( colorGroup().brush( QColorGroup::Background ) ));	    a.setPoints( 5,  0,0,  3,h/4, 0,h/2, 3,3*h/4, 0,h-3 );	}	if ( !a.isEmpty() ) {	    p.setPen( colorGroup().light() );	    p.drawPolyline( a );	    a.translate( 1, 0 );	    p.setPen( colorGroup().midlight() );	    p.drawPolyline( a );	}    }}/*!  This virtual functions is called by the mouse event handlers to  determine which tab is pressed.  The default implementation returns  a pointer to the tab whose bounding rectangle contains \a p, if  exactly one tab's bounding rectangle contains \a p.  It returns 0  else.  \sa mousePressEvent() mouseReleaseEvent()*/QTab * QTabBar::selectTab( const QPoint & p ) const{    QTab * selected = 0;    bool moreThanOne = FALSE;    QListIterator<QTab> i( *l );    while( i.current() ) {	QTab * t = i.current();	++i;	if ( t && t->r.contains( p ) ) {	    if ( selected )		moreThanOne = TRUE;	    else		selected = t;	}    }    return moreThanOne ? 0 : selected;}/*!\reimp*/void QTabBar::mousePressEvent( QMouseEvent * e ){    if ( e->button() != LeftButton )	return;    QTab * t = selectTab( e->pos() );    if ( t != 0 && t == selectTab( e->pos() ) && t->enabled ) {	setCurrentTab( t );    }}/*!\reimp*/void QTabBar::mouseReleaseEvent( QMouseEvent * ){}/*!  \reimp*/void QTabBar::show(){    //  ensures that one tab is selected.    QTab * t = l->last();    QWidget::show();#ifdef QT_KEYPAD_MODE    // grab focus to make tab navigation easier    if( qt_modalEditingEnabled ) {	setFocus();    }#endif    if ( t )	emit selected( t->id );}/*!  If a page is currently visible, returns its ID.  If no page is  currently visible, returns either -1 or the ID of one of the pages.  Even if the return value is not -1, you cannot assume either that  the user can see the relevant page, or that the tab \link  isTabEnabled() is enabled.\endlink  When you need to display something, the return value from this  function represents the best page to display.  That's all.  \sa selected()*/int QTabBar::currentTab() const{    const QTab * t = l->getLast();    return t ? t->id : -1;}/*! Raises the tab with ID \a id and emits the selected() signal.  \sa currentTab() selected() tab()*/void QTabBar::setCurrentTab( int id ){    setCurrentTab( tab( id ) );}/*! Raises \a tab and emits the selected() signal unless the tab was  already current.  \sa currentTab() selected()*/void QTabBar::setCurrentTab( QTab * tab ){    if ( tab && l ) {	if ( l->last() == tab )	    return;	QRect r = l->last()->r;	if ( l->findRef( tab ) >= 0 )	    l->append( l->take() );	d->focus = tab->id;	updateMask();	if ( tab->r.intersects( r ) ) {	    repaint( r.unite( tab->r ) );	} else {	    repaint( r );	    repaint( tab->r );	}	makeVisible( tab );	emit selected( tab->id );    }}/*!  If this tab control has keyboard focus, returns the ID of the  tab Space will select.  Otherwise, returns -1.*/int QTabBar::keyboardFocusTab() const{    return hasFocus() ? d->focus : -1;}/*!\reimp*/void QTabBar::keyPressEvent( QKeyEvent * e ){    //   The right and left arrow keys move a selector, the spacebar    //   makes the tab with the selector active.  All other keys are    //   ignored.    int old = d->focus;    if ( e->key() == Key_Left ) {	// left - skip past any disabled ones	QTab *t = 0;	if ( d->focus > 0 ) {	    t = lstatic->last();	    while ( t && t->id != d->focus )		t = lstatic->prev();	    do {		t = lstatic->prev();	    } while ( t && !t->enabled);	    if (t)		d->focus = t->id;	}#ifdef QT_KEYPAD_MODE	if( qt_modalEditingEnabled ) {	    if (!t) {		t = lstatic->last();		while ( t && !t->enabled)		    t = lstatic->prev();		if (t)		    d->focus = t->id;	    }	}#endif	if ( d->focus < 0 )	    d->focus = old;    } else if ( e->key() == Key_Right #ifdef QT_KEYPAD_MODE		|| (qt_modalEditingEnabled && e->key() == Key_Select)#endif	      ) {	QTab * t = lstatic->first();	while ( t && t->id != d->focus )	    t = lstatic->next();	do {	    t = lstatic->next();	} while ( t && !t->enabled);	if (t)	    d->focus = t->id;#ifdef QT_KEYPAD_MODE	else if( qt_modalEditingEnabled ) {	    t = lstatic->first();	    while ( t && !t->enabled)		t = lstatic->next();	    if (t)		d->focus = t->id;	}#endif	if ( d->focus >= d->id )	    d->focus = old;    } else {	// other keys - ignore	e->ignore();	return;    }    // if the focus moved, repaint and signal    if ( old != d->focus ) {	setCurrentTab( d->focus );    }}/*!  Returns a pointer to the tab with ID \a id, or 0 if there is no  such tab.  \sa count()*/QTab * QTabBar::tab( int id ){    QTab * t;    for( t = l->first(); t; t = l->next() )	if ( t && t->id == id )	    return t;    return 0;}/*! Returns the number of tabs in the tab bar.  \sa tab()*/int QTabBar::count() const{    return l->count();}/*!  The list of QTab objects added.*/QList<QTab> * QTabBar::tabList(){    return l;}/*!  Returns the shape of this tab bar. \sa setShape() */QTabBar::Shape QTabBar::shape() const{    return d ? d->s : RoundedAbove;}/*!  Sets the shape of this tab bar to \a s and refreshes the bar.*/void QTabBar::setShape( Shape s ){    if ( !d || d->s == s )	return;    //######### must recalculate heights    d->s = s;    updateMask();    update();}/*!  Layout all existing tabs (i.e. setting their \c r attribute) according  to their label and their iconset. */void QTabBar::layoutTabs(){    if ( lstatic->isEmpty() )	return;    int hframe, vframe, overlap;    style().tabbarMetrics( this, hframe, vframe, overlap );    QFontMetrics fm = fontMetrics();    int x = 0;    QRect r;    QTab *t;    for ( t = lstatic->first(); t; t = lstatic->next() ) {	int lw = fm.width( t->label );	int iw = 0;	int ih = 0;	if ( t->iconset != 0 ) {	    iw = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).width();	    ih = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).height();	    if (!t->label.isNull())		iw +=2;	}	int h = QMAX( fm.height(), ih );	h = QMAX( h, QApplication::globalStrut().height() );	h += vframe;	t->r.setRect( x, 0, QMAX( lw + hframe + iw,		    QApplication::globalStrut().width() ), h );	x += t->r.width() - overlap;	r = r.unite( t->r );    }    for ( t = lstatic->first(); t; t = lstatic->next() )	t->r.setHeight( r.height() );}/*!  \reimp*/void QTabBar::styleChange( QStyle& old ){	layoutTabs();	QWidget::styleChange( old );}/*!  \reimp*/void QTabBar::focusInEvent( QFocusEvent * ){    QTab *t = l->first();    for ( ; t; t = l->next() ) {	if ( t->id == d->focus ) {	    QPainter p;	    p.begin( this );	    QRect r = t->r;	    p.setFont( font() );	    int iw = 0;	    int ih = 0;	    if ( t->iconset != 0 ) {		iw = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).width();		ih = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).height();		if (!t->label.isEmpty())		    iw +=2; 	    }	    int w = iw + p.fontMetrics().width( t->label ) + 4 + style().pixelMetric(QStyle::TabHMargin);	    int h = QMAX(p.fontMetrics().height() + 4, ih );	    paintLabel( &p, QRect( r.left() + ( r.width() -w ) /2,				   r.top() + ( r.height()-h ) / 2,				   w, h ), t, TRUE );	    p.end();	}    }}/*!  \reimp*/void QTabBar::focusOutEvent( QFocusEvent * ){    QTab *t = l->first();    for ( ; t; t = l->next() ) {	if ( t->id == d->focus ) {	    QPainter p;	    p.begin( this );	    if ( !testWState(WState_GlobalBrushOrigin) ) 		p.setBrushOrigin( rect().bottomLeft() );	    QRect r = t->r;	    p.setFont( font() );	    int iw = 0;	    int ih = 0;	    if ( t->iconset != 0 ) {		iw = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).width();		ih = t->iconset->pixmap( QIconSet::Small, QIconSet::Normal ).height();		if (!t->label.isEmpty())		    iw +=2; 	    }	    int w = iw + p.fontMetrics().width( t->label ) + 4 + style().pixelMetric(QStyle::TabHMargin);	    int h = QMAX(p.fontMetrics().height() + 4, ih );	    p.fillRect( QRect( r.left() + ( r.width() -w ) / 2 - 1,				   r.top() + ( r.height()-h ) / 2 - 1,			       w + 2, h + 2 ), colorGroup().brush(QColorGroup::Background ) );	    style().drawTab( &p, this, t, TRUE );	    paintLabel( &p, QRect( r.left() + ( r.width() -w ) /2,				   r.top() + ( r.height()-h ) / 2,				   w, h ), t, FALSE );	    p.end();	}    }}/*!  \reimp*/void QTabBar::resizeEvent( QResizeEvent * ){#ifndef QT_NO_TOOLBUTTON    const int arrowWidth = 16;    d->rightB->setGeometry( width() - arrowWidth, 0, arrowWidth, height() );    d->leftB->setGeometry( width() - 2*arrowWidth, 0, arrowWidth, height() );#endif    layoutTabs();    updateArrowButtons();    makeVisible( tab( currentTab() ));}void QTabBar::scrollTabs(){#ifndef QT_NO_TOOLBUTTON    QTab* left = 0;    QTab* right = 0;    for ( QTab* t = lstatic->first(); t; t = lstatic->next() ) {	if ( t->r.left() < 0 && t->r.right() > 0 )	    left = t;	if ( t->r.left() < d->leftB->x()+2 )	    right = t;    }    if ( sender() == d->leftB )	makeVisible( left );    else  if ( sender() == d->rightB )	makeVisible( right );#endif}void QTabBar::makeVisible( QTab* tab  ){#ifndef QT_NO_TOOLBUTTON    bool tooFarLeft = ( tab && tab->r.left() < 0 );    bool tooFarRight = ( tab && tab->r.right() >= d->leftB->x() );    if ( !d->scrolls || ( !tooFarLeft && ! tooFarRight ) )	return;    layoutTabs();    int offset = 0;    if ( tooFarLeft )	offset = tab == lstatic->first() ? 0 : tab->r.left() - 8;    else if ( tooFarRight ) {	offset = tab->r.right() - d->leftB->x() + 1;    }    for ( QTab* t = lstatic->first(); t; t = lstatic->next() )	t->r.moveBy( -offset, 0 );    d->leftB->setEnabled( offset != 0 );    d->rightB->setEnabled( lstatic->last()->r.right() >= d->leftB->x() );    update();#endif}void QTabBar::updateArrowButtons(){#ifndef QT_NO_TOOLBUTTON    bool b = lstatic->last() &&	( lstatic->last()->r.right() > width() );    d->scrolls = b;    if ( d->scrolls ) {	d->leftB->setEnabled( FALSE );	d->rightB->setEnabled( TRUE );	d->leftB->show();	d->rightB->show();    } else {	d->leftB->hide();	d->rightB->hide();    }#endif}#endif

⌨️ 快捷键说明

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