~adamreichold/qpdfview/trunk

« back to all changes in this revision

Viewing changes to sources/miscellaneous.cpp

Merge outline keyboard navigation branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <QLabel>
31
31
#include <QMenu>
32
32
#include <QMouseEvent>
 
33
#include <QScrollBar>
33
34
#include <QTimer>
34
35
#include <QToolTip>
35
36
#include <QVBoxLayout>
54
55
    return true;
55
56
}
56
57
 
 
58
inline QModelIndex firstIndex(const QModelIndexList& indexes)
 
59
{
 
60
    return !indexes.isEmpty() ? indexes.first() : QModelIndex();
 
61
}
 
62
 
57
63
} // anonymous
58
64
 
59
65
namespace qpdfview
349
355
    }
350
356
}
351
357
 
 
358
int TreeView::expandedDepth(const QModelIndex& index)
 
359
{
 
360
    if(index.isValid())
 
361
    {
 
362
        if(!isExpanded(index) || !model()->hasChildren(index))
 
363
        {
 
364
            return 0;
 
365
        }
 
366
 
 
367
        int depth = 0;
 
368
 
 
369
        for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
 
370
        {
 
371
            depth = qMax(depth, expandedDepth(index.child(row, 0)));
 
372
        }
 
373
 
 
374
        return 1 + depth;
 
375
    }
 
376
    else
 
377
    {
 
378
        int depth = 0;
 
379
 
 
380
        for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
 
381
        {
 
382
            depth = qMax(depth, expandedDepth(model()->index(row, 0)));
 
383
        }
 
384
 
 
385
        return depth;
 
386
    }
 
387
}
 
388
 
 
389
void TreeView::expandToDepth(const QModelIndex& index, int depth)
 
390
{
 
391
    if(index.isValid())
 
392
    {
 
393
        if(depth > 0)
 
394
        {
 
395
            if(!isExpanded(index))
 
396
            {
 
397
                expand(index);
 
398
            }
 
399
        }
 
400
 
 
401
        if(depth > 1)
 
402
        {
 
403
            for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
 
404
            {
 
405
                expandToDepth(index.child(row, 0), depth - 1);
 
406
            }
 
407
        }
 
408
    }
 
409
    else
 
410
    {
 
411
        for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
 
412
        {
 
413
            expandToDepth(model()->index(row, 0), depth);
 
414
        }
 
415
    }
 
416
}
 
417
 
 
418
void TreeView::collapseFromDepth(const QModelIndex& index, int depth)
 
419
{
 
420
    if(index.isValid())
 
421
    {
 
422
        if(depth <= 0)
 
423
        {
 
424
            if(isExpanded(index))
 
425
            {
 
426
                collapse(index);
 
427
            }
 
428
        }
 
429
 
 
430
        for(int row = 0, rowCount = model()->rowCount(index); row < rowCount; ++row)
 
431
        {
 
432
            collapseFromDepth(index.child(row, 0), depth - 1);
 
433
        }
 
434
    }
 
435
    else
 
436
    {
 
437
        for(int row = 0, rowCount = model()->rowCount(); row < rowCount; ++row)
 
438
        {
 
439
            collapseFromDepth(model()->index(row, 0), depth);
 
440
        }
 
441
    }
 
442
}
 
443
 
352
444
void TreeView::restoreExpansion(const QModelIndex& index)
353
445
{
354
446
    if(index.isValid())
367
459
    }
368
460
}
369
461
 
 
462
void TreeView::keyPressEvent(QKeyEvent* event)
 
463
{
 
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;
 
466
 
 
467
    const QModelIndex selection = firstIndex(selectedIndexes());
 
468
 
 
469
    // If Shift is pressed, the view is scrolled up or down.
 
470
    if(event->modifiers().testFlag(Qt::ShiftModifier) && verticalKeys)
 
471
    {
 
472
        QScrollBar* scrollBar = verticalScrollBar();
 
473
 
 
474
        if(event->key() == Qt::Key_Up && scrollBar->value() > scrollBar->minimum())
 
475
        {
 
476
            scrollBar->triggerAction(QAbstractSlider::SliderSingleStepSub);
 
477
 
 
478
            event->accept();
 
479
            return;
 
480
        }
 
481
        else if(event->key() == Qt::Key_Down && scrollBar->value() < scrollBar->maximum())
 
482
        {
 
483
            scrollBar->triggerAction(QAbstractSlider::SliderSingleStepAdd);
 
484
 
 
485
            event->accept();
 
486
            return;
 
487
        }
 
488
    }
 
489
 
 
490
    // If Control is pressed, all children of the selected item are expanded or collapsed.
 
491
    if(event->modifiers().testFlag(Qt::ControlModifier) && horizontalKeys)
 
492
    {
 
493
        if(event->key() == Qt::Key_Left)
 
494
        {
 
495
            collapseAll(selection);
 
496
        }
 
497
        else if(event->key() == Qt::Key_Right)
 
498
        {
 
499
            expandAll(selection);
 
500
        }
 
501
 
 
502
        event->accept();
 
503
        return;
 
504
    }
 
505
 
 
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)
 
508
    {
 
509
        const int depth = expandedDepth(selection);
 
510
 
 
511
        if(event->key() == Qt::Key_Left)
 
512
        {
 
513
            collapseFromDepth(selection, depth - 1);
 
514
        }
 
515
        else if(event->key() == Qt::Key_Right)
 
516
        {
 
517
            expandToDepth(selection, depth + 1);
 
518
        }
 
519
 
 
520
        event->accept();
 
521
        return;
 
522
    }
 
523
 
 
524
    QTreeView::keyPressEvent(event);
 
525
}
 
526
 
 
527
void TreeView::wheelEvent(QWheelEvent* event)
 
528
{
 
529
    const QModelIndex selection = firstIndex(selectedIndexes());
 
530
 
 
531
    // If Control is pressed, expand or collapse the selected entry.
 
532
    if(event->modifiers().testFlag(Qt::ControlModifier) && selection.isValid())
 
533
    {
 
534
        if(event->delta() > 0)
 
535
        {
 
536
            collapse(selection);
 
537
        }
 
538
        else
 
539
        {
 
540
            expand(selection);
 
541
        }
 
542
 
 
543
        // Fall through when Shift is also pressed.
 
544
        if(!event->modifiers().testFlag(Qt::ShiftModifier))
 
545
        {
 
546
            event->accept();
 
547
            return;
 
548
        }
 
549
    }
 
550
 
 
551
    // If Shift is pressed, move the selected entry up and down.
 
552
    if(event->modifiers().testFlag(Qt::ShiftModifier) && selection.isValid())
 
553
    {
 
554
        QModelIndex sibling;
 
555
 
 
556
        if(event->delta() > 0)
 
557
        {
 
558
            sibling = indexAbove(selection);
 
559
        }
 
560
        else
 
561
        {
 
562
            sibling = indexBelow(selection);
 
563
        }
 
564
 
 
565
        if(sibling.isValid())
 
566
        {
 
567
            setCurrentIndex(sibling);
 
568
        }
 
569
 
 
570
        event->accept();
 
571
        return;
 
572
    }
 
573
 
 
574
    QTreeView::wheelEvent(event);
 
575
}
 
576
 
370
577
void TreeView::contextMenuEvent(QContextMenuEvent* event)
371
578
{
372
579
    QTreeView::contextMenuEvent(event);