| 373 | } |
| 374 | |
| 375 | void RDTableView::scrollTo(const QModelIndex &index, ScrollHint hint) |
| 376 | { |
| 377 | if(!index.isValid()) |
| 378 | return; |
| 379 | |
| 380 | QRect cellRect = QRect(columnViewportPosition(index.column()), rowViewportPosition(index.row()), |
| 381 | columnWidth(index.column()), rowHeight(index.row())); |
| 382 | |
| 383 | QRect dataRect = viewport()->rect(); |
| 384 | dataRect.setLeft(horizontalHeader()->pinnedWidth()); |
| 385 | |
| 386 | // if it's already visible then just bail, common case |
| 387 | if(dataRect.contains(cellRect) && hint == QAbstractItemView::EnsureVisible) |
| 388 | return; |
| 389 | |
| 390 | // assume per-item vertical scrolling and per-pixel horizontal scrolling |
| 391 | |
| 392 | // for any hint except position at center, we just ensure it's visible horizontally |
| 393 | if(hint == QAbstractItemView::PositionAtCenter) |
| 394 | { |
| 395 | // center it horizontally from the left |
| 396 | QPoint dataCenter = dataRect.center(); |
| 397 | QPoint cellCenter = cellRect.center(); |
| 398 | |
| 399 | if(dataCenter.x() > cellCenter.x()) |
| 400 | { |
| 401 | horizontalScrollBar()->setValue(horizontalScrollBar()->value() - |
| 402 | (dataCenter.x() - cellCenter.x())); |
| 403 | } |
| 404 | |
| 405 | // center it horizontally from the right |
| 406 | if(dataCenter.x() < cellCenter.x()) |
| 407 | { |
| 408 | horizontalScrollBar()->setValue(horizontalScrollBar()->value() + |
| 409 | (cellCenter.x() - dataCenter.x())); |
| 410 | } |
| 411 | } |
| 412 | else if(hint == QAbstractItemView::PositionAtTop) |
| 413 | { |
| 414 | horizontalScrollBar()->setValue(cellRect.left() - dataRect.left()); |
| 415 | } |
| 416 | else if(hint == QAbstractItemView::PositionAtBottom) |
| 417 | { |
| 418 | horizontalScrollBar()->setValue(cellRect.right() - dataRect.right()); |
| 419 | } |
| 420 | |
| 421 | // collapse EnsureVisible to either PositionAtTop or PositionAtBottom depending on which side it's |
| 422 | // on, or just return if we only had to make it visible horizontally |
| 423 | if(hint == QAbstractItemView::EnsureVisible) |
| 424 | { |
| 425 | if(dataRect.bottom() < cellRect.bottom()) |
| 426 | hint = QAbstractItemView::PositionAtBottom; |
| 427 | else if(dataRect.top() > cellRect.top()) |
| 428 | hint = QAbstractItemView::PositionAtTop; |
| 429 | else |
| 430 | return; |
| 431 | } |
| 432 | |