358
int TreeView::expandedDepth(const QModelIndex& index)
362
if(!isExpanded(index) || !model()->hasChildren(index))
369
for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
371
depth = qMax(depth, expandedDepth(index.child(row, 0)));
380
for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
382
depth = qMax(depth, expandedDepth(model()->index(row, 0)));
389
void TreeView::expandToDepth(const QModelIndex& index, int depth)
395
if(!isExpanded(index))
403
for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
405
expandToDepth(index.child(row, 0), depth - 1);
411
for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
413
expandToDepth(model()->index(row, 0), depth);
418
void TreeView::collapseFromDepth(const QModelIndex& index, int depth)
424
if(isExpanded(index))
430
for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
432
collapseFromDepth(index.child(row, 0), depth - 1);
437
for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
439
collapseFromDepth(model()->index(row, 0), depth);
352
444
void TreeView::restoreExpansion(const QModelIndex& index)
354
446
if(index.isValid())
462
void TreeView::keyPressEvent(QKeyEvent* event)
464
const bool verticalKeys = event->key() == Qt::Key_Up || event->key() == Qt::Key_Down;
465
const bool horizontalKeys = event->key() == Qt::Key_Left || event->key() == Qt::Key_Right;
467
const QModelIndex selection = firstIndex(selectedIndexes());
469
// If Shift is pressed, the view is scrolled up or down.
470
if(event->modifiers().testFlag(Qt::ShiftModifier) && verticalKeys)
472
QScrollBar* scrollBar = verticalScrollBar();
474
if(event->key() == Qt::Key_Up && scrollBar->value() > scrollBar->minimum())
476
scrollBar->triggerAction(QAbstractSlider::SliderSingleStepSub);
481
else if(event->key() == Qt::Key_Down && scrollBar->value() < scrollBar->maximum())
483
scrollBar->triggerAction(QAbstractSlider::SliderSingleStepAdd);
490
// If Control is pressed, all children of the selected item are expanded or collapsed.
491
if(event->modifiers().testFlag(Qt::ControlModifier) && horizontalKeys)
493
if(event->key() == Qt::Key_Left)
495
collapseAll(selection);
497
else if(event->key() == Qt::Key_Right)
499
expandAll(selection);
506
// If Shift is pressed, one level of children of the selected item are expanded or collapsed.
507
if(event->modifiers().testFlag(Qt::ShiftModifier) && horizontalKeys)
509
const int depth = expandedDepth(selection);
511
if(event->key() == Qt::Key_Left)
513
collapseFromDepth(selection, depth - 1);
515
else if(event->key() == Qt::Key_Right)
517
expandToDepth(selection, depth + 1);
524
QTreeView::keyPressEvent(event);
527
void TreeView::wheelEvent(QWheelEvent* event)
529
const QModelIndex selection = firstIndex(selectedIndexes());
531
// If Control is pressed, expand or collapse the selected entry.
532
if(event->modifiers().testFlag(Qt::ControlModifier) && selection.isValid())
534
if(event->delta() > 0)
543
// Fall through when Shift is also pressed.
544
if(!event->modifiers().testFlag(Qt::ShiftModifier))
551
// If Shift is pressed, move the selected entry up and down.
552
if(event->modifiers().testFlag(Qt::ShiftModifier) && selection.isValid())
556
if(event->delta() > 0)
558
sibling = indexAbove(selection);
562
sibling = indexBelow(selection);
565
if(sibling.isValid())
567
setCurrentIndex(sibling);
574
QTreeView::wheelEvent(event);
370
577
void TreeView::contextMenuEvent(QContextMenuEvent* event)
372
579
QTreeView::contextMenuEvent(event);