?? qlistbox.cpp
字號:
if ( index < 0 || index > d->count -1 ) return 0; QListBoxItem * i = d->head; if ( d->cache && index > 0 ) { i = d->cache; int idx = d->cacheIndex; while ( i && idx < index ) { idx++; i = i->n; } while ( i && idx > index ) { idx--; i = i->p; } } else { int idx = index; while ( i && idx > 0 ) { idx--; i = i->n; } } d->cache = i; d->cacheIndex = index; return i;}/*! Returns the index of \a lbi, or -1 if the item is not in this list box or \a lbi is a NULL-Pointer.\sa item()*/int QListBox::index( const QListBoxItem * lbi ) const{ if ( !lbi ) return -1; int c = 0; QListBoxItem * i = d->head; while ( i && i != lbi ) { c++; i = i->n; } return i ? c : -1;}/*! Returns TRUE if the item at position \a index is at least partly visible.*/bool QListBox::itemVisible( int index ){ QListBoxItem * i = item( index ); return i ? itemVisible( i ) : FALSE;}/*! Returns TRUE if \a item is at least partly visible, or else FALSE.*/bool QListBox::itemVisible( const QListBoxItem * item ){ int i = index( item ); int col = i / numRows(); int row = i % numRows(); return ( d->columnPos[col] < contentsX()+visibleWidth() && d->rowPos[row] < contentsY()+visibleHeight() && d->columnPos[col+1] > contentsX() && d->rowPos[row+1] > contentsY() );}/*! \reimp */void QListBox::viewportMousePressEvent( QMouseEvent *e ){ // ### this (and the others) assume that the coordinates of this // and viewport() are the same. mousePressEvent( e );}/*! \reimp */void QListBox::mousePressEvent( QMouseEvent *e ){ QListBoxItem * i = itemAt( e->pos() );#ifdef QT_KEYPAD_MODE if( qt_modalEditingEnabled ) { if ( e->button() == LeftButton && !isModalEditing() ) setModalEditing( TRUE ); }#endif if ( !i && !d->current && d->head ) { d->current = d->head; updateItem( d->head ); } if ( !i && ( d->selectionMode != Single || e->button() == RightButton ) && !( e->state() & ControlButton ) ) clearSelection(); d->select = d->selectionMode == Multi ? ( i ? !i->selected() : FALSE ) : TRUE; d->pressedSelected = i && i->s; if ( i ) d->selectAnchor = i; if ( i ) { switch( selectionMode() ) { default: case Single: if ( !i->s || i != d->current ) { if ( i->isSelectable() ) setSelected( i, TRUE ); else setCurrentItem( i ); } break; case Extended: if ( i ) { if ( !(e->state() & QMouseEvent::ShiftButton) && !(e->state() & QMouseEvent::ControlButton) ) { if ( !i->selected() ) { bool b = signalsBlocked(); blockSignals( TRUE ); clearSelection(); blockSignals( b ); } setSelected( i, TRUE ); } else if ( e->state() & ControlButton ) { setSelected( i, !i->selected() ); d->pressedSelected = FALSE; } else if ( e->state() & ShiftButton ) { d->pressedSelected = FALSE; QListBoxItem *oldCurrent = item( currentItem() ); bool down = index( oldCurrent ) < index( i ); QListBoxItem *lit = down ? oldCurrent : i; bool select = d->select; bool blocked = signalsBlocked(); blockSignals( TRUE ); for ( ;; lit = lit->n ) { if ( !lit ) { triggerUpdate( FALSE ); break; } if ( down && lit == i ) { setSelected( i, select ); triggerUpdate( FALSE ); break; } if ( !down && lit == oldCurrent ) { setSelected( oldCurrent, select ); triggerUpdate( FALSE ); break; } setSelected( lit, select ); } blockSignals( blocked ); emit selectionChanged(); } setCurrentItem( i ); } break; case Multi: if ( i ) { //d->current = i; setSelected( i, !i->s ); setCurrentItem( i ); } break; case NoSelection: setCurrentItem( i ); break; } } else { bool unselect = TRUE; if ( e->button() == LeftButton ) { if ( d->selectionMode == Multi || d->selectionMode == Extended ) { d->tmpCurrent = d->current; d->current = 0; updateItem( d->tmpCurrent ); if ( d->rubber ) delete d->rubber; d->rubber = 0; d->rubber = new QRect( e->x(), e->y(), 0, 0 ); if ( d->selectionMode == Extended && !( e->state() & ControlButton ) ) selectAll( FALSE ); unselect = FALSE; } if ( unselect && ( e->button() == RightButton || isMultiSelection() ) ) clearSelection(); } } // for sanity, in case people are event-filtering or whatnot delete d->scrollTimer; d->scrollTimer = 0; if ( i ) { d->mousePressColumn = d->currentColumn; d->mousePressRow = d->currentRow; } else { d->mousePressColumn = -1; d->mousePressRow = -1; } d->ignoreMoves = FALSE; d->pressedItem = i; emit pressed( i ); emit pressed( i, e->globalPos() ); emit mouseButtonPressed( e->button(), i, e->globalPos() ); if ( e->button() == RightButton ) emit rightButtonPressed( i, e->globalPos() );}/*! \reimp */void QListBox::viewportMouseReleaseEvent( QMouseEvent *e ){ mouseReleaseEvent( e );}/*! \reimp */void QListBox::mouseReleaseEvent( QMouseEvent *e ){ if ( d->rubber ) { drawRubber(); delete d->rubber; d->rubber = 0; d->current = d->tmpCurrent; updateItem( d->current ); } if ( d->scrollTimer ) mouseMoveEvent( e ); delete d->scrollTimer; d->scrollTimer = 0; d->ignoreMoves = FALSE; if ( d->selectionMode == Extended && d->current == d->pressedItem && d->pressedSelected && d->current ) { bool block = signalsBlocked(); blockSignals( TRUE ); clearSelection(); blockSignals( block ); d->current->s = TRUE; emit selectionChanged(); } QListBoxItem * i = itemAt( e->pos() ); bool emitClicked = d->mousePressColumn != -1 && d->mousePressRow != -1 || !d->pressedItem; emitClicked = emitClicked && d->pressedItem == i; d->pressedItem = 0; d->mousePressRow = -1; d->mousePressColumn = -1; if ( emitClicked ) { emit clicked( i ); emit clicked( i, e->globalPos() ); emit mouseButtonClicked( e->button(), i, e->globalPos() ); if ( e->button() == RightButton ) emit rightButtonClicked( i, e->globalPos() ); }}/*! \reimp */void QListBox::viewportMouseDoubleClickEvent( QMouseEvent * e ){ mouseDoubleClickEvent( e );}/*! \reimp */void QListBox::mouseDoubleClickEvent( QMouseEvent *e ){ bool ok = TRUE; QListBoxItem *i = itemAt( e->pos() ); if ( !i || selectionMode() == NoSelection ) ok = FALSE; d->ignoreMoves = TRUE; if ( d->current && ok ) { QListBoxItem * i = d->current; QString tmp = d->current->text(); emit selected( currentItem() ); emit selected( i ); if ( !tmp.isNull() ) emit selected( tmp ); emit doubleClicked( i ); }}/*! \reimp */void QListBox::viewportMouseMoveEvent( QMouseEvent *e ){ mouseMoveEvent( e );}/*! \reimp */void QListBox::mouseMoveEvent( QMouseEvent *e ){ QListBoxItem * i = itemAt( e->pos() ); if ( i != d->highlighted ) { if ( i ) { emit onItem( i ); } else { emit onViewport(); } d->highlighted = i; } if ( d->rubber ) { QRect r = d->rubber->normalize(); drawRubber(); d->rubber->setCoords( d->rubber->x(), d->rubber->y(), e->x(), e->y() ); doRubberSelection( r, d->rubber->normalize() ); drawRubber(); return; } if ( ( (e->state() & ( RightButton | LeftButton | MidButton ) ) == 0 ) || d->ignoreMoves ) return; // hack to keep the combo (and what else?) working: if we get a // move outside the listbox without having seen a press, discard // it. if ( !QRect( 0, 0, visibleWidth(), visibleHeight() ).contains( e->pos() ) && ( d->mousePressColumn < 0 && d->mousePressRow < 0 || e->state() == NoButton || !d->pressedItem ) ) return; // figure out in what direction to drag-select and perhaps scroll int dx = 0; int x = e->x(); if ( x >= visibleWidth() ) { x = visibleWidth()-1; dx = 1; } else if ( x < 0 ) { x = 0; dx = -1; } d->mouseMoveColumn = columnAt( x + contentsX() ); // sanitize mousePressColumn, if we got here without a mouse press event if ( d->mousePressColumn < 0 && d->mouseMoveColumn >= 0 ) d->mousePressColumn = d->mouseMoveColumn; if ( d->mousePressColumn < 0 && d->currentColumn >= 0 ) d->mousePressColumn = d->currentColumn; // if it's beyond the last column, use the last one if ( d->mouseMoveColumn < 0 ) d->mouseMoveColumn = dx >= 0 ? numColumns()-1 : 0; // repeat for y int dy = 0; int y = e->y(); if ( y >= visibleHeight() ) { y = visibleHeight()-1; dy = 1; } else if ( y < 0 ) { y = 0; dy = -1; } d->mouseMoveRow = rowAt( y + contentsY() ); if ( d->mousePressRow < 0 && d->mouseMoveRow >= 0 ) d->mousePressRow = d->mouseMoveRow; if ( d->mousePressRow < 0 && d->currentRow >= 0 ) d->mousePressRow = d->currentRow; if ( d->mousePressRow < 0 ) d->mousePressRow = rowAt( x + contentsX() ); d->scrollPos = QPoint( dx, dy ); if ( ( dx || dy ) && !d->scrollTimer ) { // start autoscrolling if necessary d->scrollTimer = new QTimer( this ); connect( d->scrollTimer, SIGNAL(timeout()), this, SLOT(doAutoScroll()) ); d->scrollTimer->start( 100, FALSE ); doAutoScroll(); } else if ( !d->scrollTimer ) { // or just select the required bits updateSelection(); }}void QListBox::updateSelection(){ if ( d->mouseMoveColumn >= 0 && d->mouseMoveRow >= 0 && d->mousePressColumn >= 0 && d->mousePressRow >= 0 ) { QListBoxItem * i = item( d->mouseMoveColumn * numRows() + d->mouseMoveRow ); if ( selectionMode() == Single || selectionMode() == NoSelection ) { if ( i ) setCurrentItem( i ); } else { if ( d->selectionMode == Extended && d->current == d->pressedItem && d->pressedSelected ) { d->pressedItem = 0; bool block = signalsBlocked(); blockSignals( TRUE ); clearSelection(); i->s = TRUE; blockSignals( block ); emit selectionChanged(); triggerUpdate( FALSE ); } else { int c = QMIN( d->mouseMoveColumn, d->mousePressColumn ); int r = QMIN( d->mouseMoveRow, d->mousePressRow ); int c2 = QMAX( d->mouseMoveColumn, d->mousePressColumn ); int r2 = QMAX( d->mouseMoveRow, d->mousePressRow ); bool changed = FALSE; while( c <= c2 ) { QListBoxItem * i = item( c*numRows()+r ); int rtmp = r; while( i && rtmp <= r2 ) { if ( (bool)i->s != d->select && i->isSelectable() ) { i->s = d->select; i->dirty = TRUE; changed = TRUE; } i = i->n; rtmp++; } c++; } if ( changed ) { emit selectionChanged(); triggerUpdate( FALSE ); } } if ( i ) setCurrentItem( i ); } }}/*!\reimp*/void QListBox::keyPressEvent( QKeyEvent *e ){ if ( count() == 0 ) { e->ignore(); return; }#ifdef QT_KEYPAD_MODE switch( e->key() ) { case Key_Select: if( qt_modalEditingEnabled ) { if ( !isModalEditing() ) { setModalEditing( TRUE ); if ( !d->current && d->head ) setCurrentItem(0); return; } } break; 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 QListBoxItem *old = d->current; if ( !old ) { setCurrentItem( d->head ); if ( d->selectionMode == Single ) setSelected( d->head, TRUE ); e->ignore(); return; } bool selectCurrent = FALSE; switch ( e->key() ) { case Key_Up: d->currInputString = QString::null; if ( currentItem() > 0 ) { setCurrentItem( currentItem() - 1 ); handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton ); }#ifdef QT_KEYPAD_MODE else { setCurrentItem( count() - 1 );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -