BCDA-APS / mdaviz

Data visualization for mda
https://bcda-aps.github.io/mdaviz/
Other
3 stars 0 forks source link

disable file highlight with keyboard arrow keys in folder tableview #83

Closed rodolakis closed 1 month ago

rodolakis commented 5 months ago

After clicking in the folder tableview, the arrow keys allow to select to the next/previous files but it is not yet connected to doFileSelected so nothing happens Since the "currentFileIndex" is updated only in "dofileselected", it creates weird jump when using the next/previous button.

Related to #82.

prjemian commented 5 months ago

Could be milestone 2, unless it is a one-hour process. Shouldn't delay the initial release.

rodolakis commented 5 months ago

@apsIEX says she'd rather not have the keyboard arrows connected (for remote operation, could become an annoyance). To solve my problem (i.e. Next and Previous button only recall the last selected file vs the last highlighted file), could just disable the highlight with keyboard completely., ie field table view only update with double click or next/previous button click (like now) BUT moving keyboard arrows does not highlight next/previous file in folder table view (basically arrow keys do nothing)

rodolakis commented 4 months ago

In PyQt, you can disable the default behavior of arrow keys in a QTableView by subclassing the QTableView and overriding the keyPressEvent method to ignore arrow key presses. This will prevent the selection from changing when the arrow keys are pressed.

Here's an example of how you might subclass QTableView to ignore arrow key presses:

from PyQt5.QtWidgets import QTableView
from PyQt5.QtCore import Qt

class MDAFolderTableView(QTableView):
    def keyPressEvent(self, event):
        if event.key() in (Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right):
            # Ignore the arrow key press events
            event.ignore()
        else:
            # Handle other key press events as usual
            super().keyPressEvent(event)

In this subclass, the keyPressEvent is overridden to check if the key pressed is an arrow key. If it is, the event is ignored with event.ignore(), which means that the default behavior (changing the selected row or column) does not occur. For any other key press, the event is passed to the base class implementation with super().keyPressEvent(event) to maintain the default behavior.

You would then use MDAFolderTableView in place of QTableView when creating the table view for your folder content.

To integrate this into your PyQt application, replace your current QTableView with MDAFolderTableView in the UI setup code. Here's an example of how you might do that:

# ... your other PyQt imports and setup ...

class YourMainWindow(QMainWindow):
    def __init__(self, ...):  # your other parameters
        super().__init__()

        # ... your other setup code ...

        # Create an instance of MDAFolderTableView instead of QTableView
        self.folderTableView = MDAFolderTableView(self)

        # ... set up the model and other properties for the table view ...

This approach should work for disabling arrow key navigation in any QTableView based widget.

rodolakis commented 3 months ago

This feature might actually come from this: self.mda_folder_tableview.tableView.setFocus()

I added this because it ensures the table view has focus to get the blue highlight on Mac OS But this also sets the keyboard focus to the table view, making it the active component in the GUI. This means any keyboard inputs would go to the table view, such as arrow keys for navigation. Talking with JM, this brings confusion (arrow keys vs back/next button)

Trying to uncomment it to see if that helps.

rodolakis commented 3 months ago

Try in https://github.com/BCDA-APS/mdaviz/commit/2d0f10e7a0322764a7759abe76cb3c24b8b03d21, no difference..

rodolakis commented 3 months ago

Refactor the handling of button (first/previous/next/last) using QItemSelectionModel (https://doc.qt.io/qtforpython-5/PySide2/QtCore/QItemSelectionModel.html#qitemselectionmodel), a lot cleaner and fixes all of my problem, included the mishandling of the current index which would diverge if user uses the keyboard arrow. Looks all good now.