?? qheader.cpp
字號:
if ( section < 0 ) return; int index = d->s2i[section]; if ( (index > 0 && c < d->positions[index] + GRIPMARGIN) || (c > d->positions[index] + d->sizes[section] - GRIPMARGIN) ) { if ( c < d->positions[index] + GRIPMARGIN ) handleIdx = index-1; else handleIdx = index; oldHIdxSize = d->sizes[ d->i2s[handleIdx] ]; state = d->resize[d->i2s[handleIdx] ] ? Sliding : Blocked; } else if ( index >= 0 ) { handleIdx = index; moveToIdx = -1; state = d->clicks[ d->i2s[handleIdx] ] ? Pressed : Blocked; clickPos = c; repaint( sRect( handleIdx ) ); emit pressed( section ); }}/*! \reimp*/void QHeader::mouseReleaseEvent( QMouseEvent *e ){ if ( e->button() != LeftButton ) return; State oldState = state; state = Idle; switch ( oldState ) { case Pressed: { int section = d->i2s[handleIdx]; repaint(sRect( handleIdx ), FALSE); emit released( section ); if ( sRect( handleIdx ).contains( e->pos() ) ) { emit sectionClicked( handleIdx ); emit clicked( section ); } } break; case Sliding: { int c = orient == Horizontal ? e->pos().x() : e->pos().y(); c += offset(); handleColumnResize( handleIdx, c, TRUE ); } break; case Moving: {#ifndef QT_NO_CURSOR unsetCursor();#endif int section = d->i2s[handleIdx]; if ( handleIdx != moveToIdx && moveToIdx != -1 ) { moveSection( section, moveToIdx ); repaint(); // a bit overkill, but removes the handle as well emit moved( handleIdx, moveToIdx ); emit indexChange( section, handleIdx, moveToIdx ); emit released( section ); } else { repaint(sRect( handleIdx ), FALSE ); if ( sRect( handleIdx).contains( e->pos() ) ) { emit released( section ); emit sectionClicked( handleIdx ); emit clicked( section ); } } break; } case Blocked: //nothing break; default: // empty, probably. Idle, at any rate. break; }}/*! \reimp*/void QHeader::mouseMoveEvent( QMouseEvent *e ){ int section; bool hit; int c = orient == Horizontal ? e->pos().x() : e->pos().y(); c += offset(); switch( state ) { case Idle: hit = FALSE; if ( (section = sectionAt( c )) >= 0 ) { int index = d->s2i[section]; if ( (index > 0 && c < d->positions[index] + GRIPMARGIN) || (c > d->positions[index] + d->sizes[section] - GRIPMARGIN) ) { if ( index > 0 && c < d->positions[index] + GRIPMARGIN ) section = d->i2s[--index]; if ( d->resize.testBit(section) ) { hit = TRUE;#ifndef QT_NO_CURSOR if ( orient == Horizontal ) setCursor( splitHCursor ); else setCursor( splitVCursor );#endif } } }#ifndef QT_NO_CURSOR if ( !hit ) unsetCursor();#endif break; case Blocked: break; case Pressed: if ( QABS( c - clickPos ) > 4 && d->move ) { state = Moving; moveToIdx = -1;#ifndef QT_NO_CURSOR if ( orient == Horizontal ) setCursor( sizeHorCursor ); else setCursor( sizeVerCursor );#endif } break; case Sliding: handleColumnResize( handleIdx, c, FALSE ); break; case Moving: { int newPos = findLine( c ); if ( newPos != moveToIdx ) { if ( moveToIdx == handleIdx || moveToIdx == handleIdx + 1 ) repaint( sRect(handleIdx) ); else unMarkLine( moveToIdx ); moveToIdx = newPos; if ( moveToIdx == handleIdx || moveToIdx == handleIdx + 1 ) paintRect( pPos( handleIdx ), pSize( handleIdx ) ); else markLine( moveToIdx ); } break; } default: qWarning( "QHeader::mouseMoveEvent: (%s) unknown state", name() ); break; }}/*! Handles resizing of sections. This means it redraws the relevant parts of the header.*/void QHeader::handleColumnResize( int index, int c, bool final ){ int section = d->i2s[index]; int lim = d->positions[index] + 2*GRIPMARGIN; if ( c == lim ) return; if ( c < lim ) c = lim; int oldSize = d->sizes[section]; int newSize = c - d->positions[index]; d->sizes[section] = newSize; d->calculatePositions(); int pos = d->positions[index]-offset(); if ( orient == Horizontal ) repaint( pos, 0, width() - pos, height() ); else repaint( 0, pos, width(), height() - pos ); if ( tracking() && oldSize != newSize ) emit sizeChange( section, oldSize, newSize ); else if ( !tracking() && final && oldHIdxSize != newSize ) emit sizeChange( section, oldHIdxSize, newSize );}/*! Returns the rectangle covered by index \a index.*/QRect QHeader::sRect( int index ){ int section = mapToSection( index ); if ( section < 0 ) return rect(); // ### eeeeevil if ( orient == Horizontal ) return QRect( d->positions[index]-offset(), 0, d->sizes[section], height() ); else return QRect( 0, d->positions[index]-offset(), width(), d->sizes[section] );}/*! Sets the icon on the section \a section to \a iconset and the text to \a s. If the section does not exist, nothing happens. If \a size is non-negative, the section width is set to \a size. Any icon set that has been defined for this section remains unchanged.*/void QHeader::setLabel( int section, const QIconSet& iconset, const QString &s, int size ){ if ( section < 0 || section >= count() ) return; d->iconsets.insert( section, new QIconSet( iconset ) ); setLabel( section, s, size );}/*! Sets the text on section \a section to \a s. If the section does not exist, nothing happens. If \a size is non-negative, the section width is set to \a size. Any icon set that has been defined for this section remains unchanged.*/void QHeader::setLabel( int section, const QString &s, int size ){ if ( section < 0 || section >= count() ) return; d->labels.insert( section, new QString( s ) ); if ( size >= 0 ) d->sizes[section] = size; if ( isUpdatesEnabled() ) { d->calculatePositions(); update(); }}/*! Returns the text set on section \a section.*/QString QHeader::label( int section ) const{ if ( section < 0 || section >= count() ) return QString::null; if ( d->labels[ section ] ) return *( d->labels[ section ] ); else return QString::null;}/*! Returns the icon set set on section \a section.*/QIconSet *QHeader::iconSet( int section ) const{ if ( section < 0 || section >= count() ) return 0; return d->iconsets[ section ];}/*! Adds a new section, with icon set \a iconset and label text \a s. Returns the index. If \a size is non-negative, the section width is set to \a size, otherwise a size currently sufficient for the label is used.*/int QHeader::addLabel( const QIconSet& iconset, const QString &s, int size ){ int n = count() + 1; d->iconsets.resize( n + 1 ); d->iconsets.insert( n - 1, new QIconSet( iconset ) ); return addLabel( s, size );}/*! Removes the section \a section.*/void QHeader::removeLabel( int section ){ if ( section < 0 || section > count() - 1 ) return; int index = d->s2i[section]; int n = --d->count; int i; for ( i = section; i < n; ++i ) { d->sizes[i] = d->sizes[i+1]; d->heights[i] = d->heights[i+1]; d->labels.insert( i, d->labels.take( i + 1 ) ); d->iconsets.insert( i, d->iconsets.take( i + 1 ) ); } d->sizes.resize( n ); d->positions.resize( n ); d->heights.resize( n ); d->labels.resize( n ); d->iconsets.resize( n ); for ( i = section; i < n; ++i ) d->s2i[i] = d->s2i[i+1]; d->s2i.resize( n ); if ( isUpdatesEnabled() ) { for ( i = 0; i < n; ++i ) if ( d->s2i[i] > index ) --d->s2i[i]; } for ( i = index; i < n; ++i ) d->i2s[i] = d->i2s[i+1]; d->i2s.resize( n ); if ( isUpdatesEnabled() ) { for ( i = 0; i < n ; ++i ) if ( d->i2s[i] > section ) --d->i2s[i]; } if ( isUpdatesEnabled() ) { d->calculatePositions(); update(); }}/*! Appends or removes (from the end) sections so that the total number equals /a n. New sections get a default label. Faster than calling addLabel()/removeLabel() in a loop. */void QHeader::setNumLabels( int n ){ if ( n > d->count ) { // add labels int lastpos = 0; if ( d->count ) lastpos = d->positions[d->count-1] + d->sizes[d->i2s[d->count-1]]; d->labels.resize( n ); if ( int( d->iconsets.size() ) < n ) d->iconsets.resize( n ); d->sizes.resize( n ); d->positions.resize( n ); d->heights.resize( n ); d->i2s.resize( n ); d->s2i.resize( n ); d->clicks.resize( n ); d->resize.resize( n ); QFontMetrics fm(font()); int height = fm.lineSpacing() + 6; int width = 20; int size; if ( orient == Horizontal ) size = width; else size = height; for ( int section = d->count; section < n; section++ ) { d->labels.insert( section, new QString( QString::number(section) ) ); /* int iw = 0; int ih = 0; if ( d->iconsets[section] != 0 ) { iw = d->iconsets[section]->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 2; ih = d->iconsets[section]->pixmap( QIconSet::Small, QIconSet::Normal ).height(); } */ int index = section; d->sizes[section] = size; d->positions[index] = lastpos; // we abuse the heights as widths for vertical layout d->heights[section] = orient == Horizontal ? height : width; d->s2i[section] = index; d->i2s[index] = section; d->clicks.setBit( section, d->clicks_default ); d->resize.setBit( section, d->resize_default ); lastpos += size; } d->count = n; } else if ( n < d->count ) { qWarning( "not really implemented yet" ); while ( n < d->count ) removeLabel( d->count - 1 ); }}/*! Adds a new section, with label text \a s. Returns the index. If \a size is non-negative, the section width is set to \a size, otherwise a size currently sufficient for the label text is used.*/int QHeader::addLabel( const QString &s, int size ){ int lastpos = 0; if ( d->count ) lastpos = d->positions[d->count-1] + d->sizes[d->i2s[d->count-1]]; int n = ++d->count; d->labels.resize( n ); if ( int( d->iconsets.size() ) < n ) d->iconsets.resize( n ); d->sizes.resize( n ); d->positions.resize( n ); d->heights.resize( n ); int section = n - 1; d->labels.insert( section, new QString( s ) ); // n-1 is last real idx int iw = 0; int ih = 0; if ( d->iconsets[section] != 0 ) { iw = d->iconsets[section]->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 2; ih = d->iconsets[section]->pixmap( QIconSet::Small, QIconSet::Normal ).height(); } QFontMetrics fm = fontMetrics(); int height = QMAX( fm.lineSpacing() + 6, ih ); int width = fm.boundingRect( s ).width()+ QH_MARGIN * 3 + iw; if ( size < 0 ) { if ( orient == Horizontal ) size = width; else size = height; } int index = section; d->sizes[section] = size; d->positions[index] = lastpos; // we abuse the heights as widths for vertical layout d->heights[section] = orient == Horizontal ? height : width; d->i2s.resize( n ); d->s2i.resize( n ); d->s2i[section] = index; d->i2s[index] = section; d->clicks.resize( n ); d->resize.resize( n ); d->clicks.setBit( section, d->clicks_default ); d->resize.setBit( section, d->resize_default ); if ( isUpdatesEnabled() ) update(); return index;}/*!\reimp*/QSize QHeader::sizeHint() const{ constPolish(); QFontMetrics fm = fontMetrics(); if ( orient == Horizontal ) { int height = fm.lineSpacing() + 6; int width = 0; for ( int i=0 ; i<count() ; i++ ) { height = QMAX( height , d->heights[i] ); width += d->sizes[i]; } return QSize( width, height ); } else { int width = fm.width( ' ' ); int height = 0; for ( int i=0 ; i<count() ; i++ ) { width = QMAX( width , d->heights[i] ); height += d->sizes[i]; } return QSize( width, height ); }}/*!\reimp*/QSizePolicy QHeader::sizePolicy() const{ //### removeme 3.0 return QWidget::sizePolicy();}/*! Scrolls the header such that \a x becomes the leftmost (or uppermost for vertical headers) visible pixel.*/void QHeader::setOffset( int x ){ int oldOff = offs; offs = x; if ( orient == Horizontal ) scroll( oldOff-offs, 0 ); else scroll( 0, oldOff-offs);}/*! Returns the position of actual division line \a i in widget coordinates. May return a position outside the widget. Note that the last division line is numbered count(). (There are one more lines than sections).
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -