📄 qlabel.cpp
字号:
}/*!\reimp*/QSizePolicy QLabel::sizePolicy() const{ //### removeme 3.0 return QWidget::sizePolicy();}/*! \reimp*/void QLabel::resizeEvent( QResizeEvent* e ){ QFrame::resizeEvent( e );#ifdef QT_NO_RICHTEXT static const bool doc = FALSE;#endif // optimize for standard labels if ( frameShape() == NoFrame && (align & WordBreak) == 0 && !doc && ( e->oldSize().width() >= e->size().width() && (align & AlignLeft ) == AlignLeft ) && ( e->oldSize().height() >= e->size().height() && (align & AlignTop ) == AlignTop ) ) { setWFlags( WResizeNoErase ); return; } clearWFlags( WResizeNoErase ); QRect cr = contentsRect(); if ( !lpixmap || !cr.isValid() || // masked pixmaps can only reduce flicker when being top/left // aligned and when we do not perform scaled contents ( lpixmap->hasAlpha() && ( scaledcontents || ( ( align & (AlignLeft|AlignTop) ) != (AlignLeft|AlignTop) ) ) ) ) return; setWFlags( WResizeNoErase ); if ( !scaledcontents ) { // don't we all love QFrame? Reduce pixmap flicker QRegion reg = QRect( QPoint(0, 0), e->size() ); reg = reg.subtract( cr ); int x = cr.x(); int y = cr.y(); int w = lpixmap->width(); int h = lpixmap->height(); if ( (align & Qt::AlignVCenter) == Qt::AlignVCenter ) y += cr.height()/2 - h/2; else if ( (align & Qt::AlignBottom) == Qt::AlignBottom) y += cr.height() - h; if ( (align & Qt::AlignRight) == Qt::AlignRight ) x += cr.width() - w; else if ( (align & Qt::AlignHCenter) == Qt::AlignHCenter ) x += cr.width()/2 - w/2; if ( x > cr.x() ) reg = reg.unite( QRect( cr.x(), cr.y(), x - cr.x(), cr.height() ) ); if ( y > cr.y() ) reg = reg.unite( QRect( cr.x(), cr.y(), cr.width(), y - cr.y() ) ); if ( x + w < cr.right() ) reg = reg.unite( QRect( x + w, cr.y(), cr.right() - x - w, cr.height() ) ); if ( y + h < cr.bottom() ) reg = reg.unite( QRect( cr.x(), y + h, cr.width(), cr.bottom() - y - h ) ); erase( reg ); }}/*! Draws the label contents using the painter \a p.*/void QLabel::drawContents( QPainter *p ){ QRect cr = contentsRect();#ifndef QT_NO_MOVIE QMovie *mov = movie();#else const int mov = 0;#endif int m = indent(); if ( m < 0 && !mov ) { // This is ugly. if ( frameWidth() > 0 ) m = p->fontMetrics().width('x')/2; else m = 0; } if ( m > 0 ) { if ( align & AlignLeft ) cr.setLeft( cr.left() + m ); if ( align & AlignRight ) cr.setRight( cr.right() - m ); if ( align & AlignTop ) cr.setTop( cr.top() + m ); if ( align & AlignBottom ) cr.setBottom( cr.bottom() - m ); }#ifndef QT_NO_MOVIE if ( mov ) { // ### should add movie to qDrawItem QRect r = style().itemRect( p, cr.x(), cr.y(), cr.width(), cr.height(), align, isEnabled(), &(mov->framePixmap()), QString::null ); // ### could resize movie frame at this point p->drawPixmap(r.x(), r.y(), mov->framePixmap() ); } else#endif#ifndef QT_NO_RICHTEXT if ( doc ) { doc->setWidth(p, cr.width() ); int rw = doc->widthUsed(); int rh = doc->height(); int xo = 0; int yo = 0; if ( align & AlignVCenter ) yo = (cr.height()-rh)/2; else if ( align & AlignBottom ) yo = cr.height()-rh; if ( align & AlignRight ) xo = cr.width()-rw; else if ( align & AlignHCenter ) xo = (cr.width()-rw)/2; if ( style() == WindowsStyle && !isEnabled() ) { QColorGroup cg = colorGroup(); cg.setColor( QColorGroup::Text, cg.light() ); doc->draw(p, cr.x()+xo+1, cr.y()+yo+1, cr, cg, 0); } doc->draw(p, cr.x()+xo, cr.y()+yo, cr, colorGroup(), 0); } else#endif { QPixmap* pix = lpixmap;#ifndef QT_NO_IMAGE_SMOOTHSCALE if ( scaledcontents && lpixmap ) { if ( !d->img ) d->img = new QImage( lpixmap->convertToImage() ); if ( !d->pix ) d->pix = new QPixmap; if ( d->pix->size() != cr.size() ) d->pix->convertFromImage( d->img->smoothScale( cr.width(), cr.height() ) ); pix = d->pix; }#endif // ordinary text or pixmap label style().drawItem( p, cr.x(), cr.y(), cr.width(), cr.height(), align, colorGroup(), isEnabled(), pix, ltext ); }}/*! \reimp*/void QLabel::setAutoMask(bool b){ if ( b ) setBackgroundMode( PaletteText ); else setBackgroundMode( PaletteBackground ); QFrame::setAutoMask( b );}/*! Draws the label contents mask using the painter \a p. Used only in transparent mode. \sa QWidget::setAutoMask();*/void QLabel::drawContentsMask( QPainter *p ){ QRect cr = contentsRect(); int m = indent(); if ( m < 0 ) { if ( frameWidth() > 0 ) m = p->fontMetrics().width('x')/2; else m = 0; } if ( m > 0 ) { if ( align & AlignLeft ) cr.setLeft( cr.left() + m ); if ( align & AlignRight ) cr.setRight( cr.right() - m ); if ( align & AlignTop ) cr.setTop( cr.top() + m ); if ( align & AlignBottom ) cr.setBottom( cr.bottom() - m ); }#ifndef QT_NO_MOVIE QMovie *mov = movie(); if ( mov ) { // ### could add movie to qDrawItem QRect r = style().itemRect( p, cr.x(), cr.y(), cr.width(), cr.height(), align, isEnabled(), &(mov->framePixmap()), QString::null ); // ### could resize movie frame at this point QPixmap pm = mov->framePixmap(); if ( pm.mask() ) { p->setPen( color1); p->drawPixmap(r.x(), r.y(), *pm.mask() ); } else p->fillRect( r, color1 ); return; }#endif QColorGroup g( color1, color1, color1, color1, color1, color1, color1, color1, color0); QBitmap bm; QPixmap* pix = lpixmap;#ifndef QT_NO_IMAGE_SMOOTHSCALE if ( scaledcontents && lpixmap ) { if ( !d->img ) d->img = new QImage( lpixmap->convertToImage() ); if ( !d->pix ) d->pix = new QPixmap; if ( d->pix->size() != cr.size() ) d->pix->convertFromImage( d->img->smoothScale( cr.width(), cr.height() ) ); pix = d->pix; }#endif if (pix ) { if (pix->mask()) { bm = *pix->mask(); } else { bm.resize( pix->size() ); bm.fill(color1); } }#ifndef QT_NO_RICHTEXT if ( doc ) { doc->setWidth(p, cr.width() ); int rw = doc->widthUsed(); int rh = doc->height(); int xo = 0; int yo = 0; if ( align & AlignVCenter ) yo = (cr.height()-rh)/2; else if ( align & AlignBottom ) yo = cr.height()-rh; if ( align & AlignRight ) xo = cr.width()-rw; else if ( align & AlignHCenter ) xo = (cr.width()-rw)/2; if ( style() == WindowsStyle && !isEnabled() ) { doc->draw(p, cr.x()+xo+1, cr.y()+yo+1, cr, g, 0); } doc->draw(p, cr.x()+xo, cr.y()+yo, cr, g, 0); } else#endif { style().drawItem( p, cr.x(), cr.y(), cr.width(), cr.height(), align, g, isEnabled(), bm.isNull()?0:&bm, ltext ); }}/*! Updates the label, not the frame.*/void QLabel::updateLabel( QSize oldSizeHint ){ d->shDirty = TRUE; QSizePolicy policy = sizePolicy(); if (#ifndef QT_NO_RICHTEXT doc ||#endif (align & WordBreak) ) { if ( policy == QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) ) policy = QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred, TRUE ); else policy.setHeightForWidth( TRUE ); } else { policy.setHeightForWidth( FALSE ); } setSizePolicy( policy ); QRect cr = contentsRect(); if ( sizeHint() != oldSizeHint ) updateGeometry(); if ( autoresize ) { adjustSize(); update(cr.x(), cr.y(), cr.width(), cr.height()); } else { update(cr.x(), cr.y(), cr.width(), cr.height()); if ( autoMask() ) updateMask(); }}/*! \internal Internal slot, used to set focus for accelerator labels.*/#ifndef QT_NO_ACCELvoid QLabel::acceleratorSlot(){ if ( !lbuddy ) return; QWidget * w = lbuddy; while ( w->focusProxy() ) w = w->focusProxy(); if ( !w->hasFocus() && w->isEnabled() && w->isVisible() && w->focusPolicy() != NoFocus ) { w->setFocus();#ifndef QT_NO_LINEEDIT if ( w->inherits( "QLineEdit" ) ) ( (QLineEdit*)w )->selectAll();#endif }}#endif/*! \internal Internal slot, used to clean up if the buddy widget dies.*/#ifndef QT_NO_ACCELvoid QLabel::buddyDied() // I can't remember if I cried.{ lbuddy = 0;}/*! Sets the buddy of this label to \a buddy. When the user presses the accelerator key indicated by this label, the keyboard focus is transferred to the label's buddy widget. The buddy mechanism is only available for QLabels that contain a plain text in which one letter is prefixed with '&'. It is this letter that is set as the accelerator key. The letter is displayed underlined, and the '&' is not displayed (i.e. the \c ShowPrefix alignment flag is turned on; see setAlignment()). In a dialog, you might create two data entry widgets and a label for each, and set up the geometry layout so each label is just to the left of its data entry widget (its "buddy"), somewhat like this: \code QLineEdit *nameEd = new QLineEdit( this ); QLabel *nameLb = new QLabel( "&Name:", this ); nameLb->setBuddy( nameEd ); QLineEdit *phoneEd = new QLineEdit( this ); QLabel *phoneLb = new QLabel( "&Phone:", this ); phoneLb->setBuddy( phoneEd ); // ( layout setup not shown ) \endcode With the code above, the focus jumps to the Name field when the user presses Alt-N, and to the Phone field when the user presses Alt-P. To unset a previously set buddy, call this function with \a buddy set to 0. \sa buddy(), setText(), QAccel, setAlignment()*/void QLabel::setBuddy( QWidget *buddy ){ if ( buddy ) setAlignment( alignment() | ShowPrefix ); else setAlignment( alignment() & ~ShowPrefix ); if ( lbuddy ) disconnect( lbuddy, SIGNAL(destroyed()), this, SLOT(buddyDied()) ); lbuddy = buddy; if ( !lbuddy ) return; int p = QAccel::shortcutKey( ltext ); if ( p ) { if ( !accel ) accel = new QAccel( this, "accel label accel" ); accel->connectItem( accel->insertItem( p ), this, SLOT(acceleratorSlot()) ); } connect( lbuddy, SIGNAL(destroyed()), this, SLOT(buddyDied()) );}/*! Returns the buddy of this label, or 0 if no buddy is currently set. \sa setBuddy()*/QWidget * QLabel::buddy() const{ return lbuddy;}#endif //QT_NO_ACCEL#ifndef QT_NO_MOVIEvoid QLabel::movieUpdated(const QRect& rect){ QMovie *mov = movie(); if ( mov && !mov->isNull() ) { QRect r = contentsRect(); r = style().itemRect( 0, r.x(), r.y(), r.width(), r.height(), align, isEnabled(), &(mov->framePixmap()), QString::null ); r.moveBy(rect.x(), rect.y()); r.setWidth(QMIN(r.width(), rect.width())); r.setHeight(QMIN(r.height(), rect.height())); repaint( r, mov->framePixmap().mask() != 0 ); if ( autoMask() ) updateMask(); }}void QLabel::movieResized( const QSize& size ){ if ( autoresize ) adjustSize(); movieUpdated( QRect( QPoint(0,0), size ) ); updateGeometry();}/*! Sets the label contents to \a movie. Any previous content is cleared. The buddy accelerator, if any, is disabled. The label resizes itself if auto-resizing is enabled. \sa movie(), setBuddy()*/void QLabel::setMovie( const QMovie& movie ){ QSize osh = sizeHint(); clearContents(); lmovie = new QMovie( movie ); lmovie->connectResize(this, SLOT(movieResized(const QSize&))); lmovie->connectUpdate(this, SLOT(movieUpdated(const QRect&))); if ( !lmovie->running() ) // Assume that if the movie is running, updateLabel( osh ); // resize/update signals will come soon enough}#endif // QT_NO_MOVIE/*! \internal Clears any contents, without updating/repainting the label.*/void QLabel::clearContents(){#ifndef QT_NO_RICHTEXT delete doc; doc = 0;#endif delete lpixmap; lpixmap = 0; delete d->img; d->img = 0; delete d->pix; d->pix = 0; ltext = QString::null;#ifndef QT_NO_ACCEL if ( accel ) accel->clear();#endif#ifndef QT_NO_MOVIE if ( lmovie ) { lmovie->disconnectResize(this, SLOT(movieResized(const QSize&))); lmovie->disconnectUpdate(this, SLOT(movieUpdated(const QRect&))); delete lmovie; lmovie = 0; }#endif}#ifndef QT_NO_MOVIE/*! If the label contains a movie, returns a pointer to it. Otherwise, returns 0. \sa setMovie()*/QMovie* QLabel::movie() const{ return lmovie;}#endif // QT_NO_MOVIE/*! Returns the current text format. \sa setTextFormat()*/Qt::TextFormat QLabel::textFormat() const{ return textformat;}/*! Sets the text format to \a format. See the Qt::TextFormat enum for an explanation of the possible options. The default format is \c AutoText. \sa textFormat(), setText()*/void QLabel::setTextFormat( Qt::TextFormat format ){#ifndef QT_NO_RICHTEXT if ( format != textformat ) { textformat = format; updateRichText(); d->shDirty = TRUE; if ( !ltext.isEmpty() ) updateLabel( QSize( -1, -1 ) ); }#endif}/*! \reimp*/void QLabel::fontChange( const QFont & ){ if ( !ltext.isEmpty() ) updateLabel( QSize( -1, -1 ) );}#ifndef QT_NO_IMAGE_SMOOTHSCALE/*! Returns whether the label will scale its contents to fill all available space. \sa setScaledContents() */bool QLabel::hasScaledContents() const{ return scaledcontents;}/*! When called with \a enable == TRUE, and the label shows a pixmap, it will scale the pixmap to fill available space. \sa hasScaledContents() */void QLabel::setScaledContents( bool enable ){ if ( (bool)scaledcontents == enable ) return; scaledcontents = enable; if ( !enable ) { delete d->img; d->img = 0; delete d->pix; d->pix = 0; } if ( autoMask() ) updateMask(); update();}#endif // QT_NO_IMAGE_SMOOTHSCALE#endif // QT_NO_LABEL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -