Skip to content

Commit

Permalink
Don't let QTreeView steal the mouse back and forward buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
shundhammer committed Aug 28, 2021
1 parent c41af9d commit 93e75e3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/DirTreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ void DirTreeView::setExpanded( FileInfo * item, bool expanded )
}


void DirTreeView::mousePressEvent( QMouseEvent * event )
{
if ( event )
{
switch ( event->button() )
{
// Leave the the back / forward buttons on the mouse to act like the
// history back / forward buttons in the tool bar.
//
// By default, the QTreeView parent class uses them to act as
// cursor up / cursor down in the tree which defeats the idea of
// using them as history consistently throughout the application,
// making those mouse buttons pretty much unusable.
//
// So this makes sure those events are immediately propagated up to
// the parent widget.

case Qt::BackButton:
case Qt::ForwardButton:
event->ignore();
break;

default:
QTreeView::mousePressEvent( event );
break;
}
}
}


void DirTreeView::keyPressEvent( QKeyEvent * event )
{
if ( event )
Expand Down
11 changes: 11 additions & 0 deletions src/DirTreeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ namespace QDirStat
**/
virtual void keyPressEvent( QKeyEvent * event ) Q_DECL_OVERRIDE;

/**
* Mouse button handler.
*
* Don't let QTreeView steal and misappropriate the mouse back /
* forward buttons; we want consistent history buttons throughout the
* application.
*
* Reimplemented from QTreeView.
**/
virtual void mousePressEvent( QMouseEvent * event ) Q_DECL_OVERRIDE;


// Data members

Expand Down
7 changes: 4 additions & 3 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,27 +1066,28 @@ void MainWindow::mousePressEvent( QMouseEvent * event )
{
QAction * action = 0;

switch ( event->buttons() )
switch ( event->button() )
{
// Handle the back / forward buttons on the mouse to act like the
// history back / forward buttons in the tool bar

case Qt::BackButton:
// logDebug() << "BackButton" << endl;
action = _ui->actionGoBack;
break;

case Qt::ForwardButton:
// logDebug() << "ForwardButton" << endl;
action = _ui->actionGoForward;
break;

default:
QMainWindow::mousePressEvent( event );
break;
}

if ( action )
{
event->accept();

if ( action->isEnabled() )
action->trigger();
}
Expand Down

0 comments on commit 93e75e3

Please sign in to comment.