Skip to content

Commit

Permalink
Merge pull request #5620 from OpenShot/select-new-files
Browse files Browse the repository at this point in the history
Select + Scroll to Imported Files
  • Loading branch information
jonoomph authored Sep 17, 2024
2 parents 5211899 + e9bde72 commit e707721
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doc/files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ You can also toggle the view between :guilabel:`details` and :guilabel:`thumbnai

Import Files
------------
These are all possible methods to import media files into OpenShot:
There are many different ways to import media files into an OpenShot project. When a file is imported successfully,
it will be automatically selected and scrolled into view (in the **Project Files** panel). Also, if the **Project Files** panel
is not currently visible, OpenShot will automatically display the panel.

.. table::
:widths: 25 80
Expand Down
23 changes: 22 additions & 1 deletion src/windows/models/files_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@

from PyQt5.QtCore import (
QMimeData, Qt, pyqtSignal, QEventLoop, QObject,
QSortFilterProxyModel, QItemSelectionModel, QPersistentModelIndex,
QSortFilterProxyModel, QItemSelectionModel, QPersistentModelIndex, QModelIndex
)
from PyQt5.QtGui import (
QIcon, QStandardItem, QStandardItemModel
)
from PyQt5.QtWidgets import QAbstractItemView
from classes import updates
from classes import info
from classes.image_types import get_media_type
Expand Down Expand Up @@ -95,6 +96,12 @@ def mimeData(self, indexes):
# Return Mimedata
return data

def get_file_index(self, file_id):
# Find the index in the proxy model based on the file ID
if file_id in self.parent.model_ids:
return self.mapFromSource(QModelIndex(self.parent.model_ids[file_id]))
return QModelIndex()

def __init__(self, **kwargs):
if "parent" in kwargs:
self.parent = kwargs["parent"]
Expand Down Expand Up @@ -271,6 +278,7 @@ def add_files(self, files, image_seq_details=None, quiet=False,
# Make sure we're working with a list of files
if not isinstance(files, (list, tuple)):
files = [files]
scroll_to_files = []

start_count = len(files)
for count, filepath in enumerate(files):
Expand All @@ -281,6 +289,8 @@ def add_files(self, files, image_seq_details=None, quiet=False,

# If this file is already found, exit
if new_file:
# Still add the file (to be selected and scrolled to)
scroll_to_files.append(new_file)
del new_file
continue

Expand Down Expand Up @@ -361,6 +371,7 @@ def add_files(self, files, image_seq_details=None, quiet=False,

# Save file
new_file.save()
scroll_to_files.append(new_file)

if start_count > 15:
message = _("Importing %(count)d / %(total)d") % {
Expand All @@ -386,6 +397,16 @@ def add_files(self, files, image_seq_details=None, quiet=False,
# Reset list of ignored paths
self.ignore_image_sequence_paths = []

# Select all new files (clear previous selection)
self.selection_model.clearSelection()
for file_object in scroll_to_files:
# Get the index of the newly added file in the proxy model
index = self.proxy_model.get_file_index(file_object.id)
if index.isValid():
# Select & scroll to selection
self.selection_model.select(index, QItemSelectionModel.Select | QItemSelectionModel.Rows)
get_app().window.filesView.scrollTo(index.siblingAtColumn(0), QAbstractItemView.PositionAtCenter)

message = _("Imported %(count)d files") % {"count": len(files) - 1}
app.window.statusBar.showMessage(message, 3000)

Expand Down

0 comments on commit e707721

Please sign in to comment.