Skip to content

Commit

Permalink
Merge pull request #443 from BigRoy/enhancement/select-latest-workfile
Browse files Browse the repository at this point in the history
Workfiles: Select latest workfile automatically
  • Loading branch information
LiborBatek authored Apr 24, 2024
2 parents 76417ea + f6411be commit ce8bf35
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
39 changes: 39 additions & 0 deletions client/ayon_core/tools/workfiles/widgets/files_widget_workarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class WorkAreaFilesModel(QtGui.QStandardItemModel):
controller (AbstractWorkfilesFrontend): The control object.
"""

refreshed = QtCore.Signal()

def __init__(self, controller):
super(WorkAreaFilesModel, self).__init__()

Expand Down Expand Up @@ -163,6 +165,12 @@ def _on_save_as_finished(self, event):
self._fill_items()

def _fill_items(self):
try:
self._fill_items_impl()
finally:
self.refreshed.emit()

def _fill_items_impl(self):
folder_id = self._selected_folder_id
task_id = self._selected_task_id
if not folder_id or not task_id:
Expand Down Expand Up @@ -285,6 +293,7 @@ def __init__(self, controller, parent):
selection_model.selectionChanged.connect(self._on_selection_change)
view.double_clicked.connect(self._on_mouse_double_click)
view.customContextMenuRequested.connect(self._on_context_menu)
model.refreshed.connect(self._on_model_refresh)

controller.register_event_callback(
"expected_selection_changed",
Expand All @@ -298,6 +307,7 @@ def __init__(self, controller, parent):
self._controller = controller

self._published_mode = False
self._change_selection_on_refresh = True

def set_published_mode(self, published_mode):
"""Set the published mode.
Expand Down Expand Up @@ -379,7 +389,9 @@ def _on_expected_selection_change(self, event):
if not workfile_info["current"]:
return

self._change_selection_on_refresh = False
self._model.refresh()
self._change_selection_on_refresh = True

workfile_name = workfile_info["name"]
if (
Expand All @@ -394,3 +406,30 @@ def _on_expected_selection_change(self, event):
self._controller.expected_workfile_selected(
event["folder"]["id"], event["task"]["name"], workfile_name
)

def _on_model_refresh(self):
if (
not self._change_selection_on_refresh
or self._proxy_model.rowCount() < 1
):
return

# Find the row with latest date modified
latest_index = max(
(
self._proxy_model.index(idx, 0)
for idx in range(self._proxy_model.rowCount())
),
key=lambda model_index: model_index.data(DATE_MODIFIED_ROLE)
)

# Select row of latest modified
selection_model = self._view.selectionModel()
selection_model.select(
latest_index,
(
QtCore.QItemSelectionModel.ClearAndSelect
| QtCore.QItemSelectionModel.Current
| QtCore.QItemSelectionModel.Rows
)
)
17 changes: 8 additions & 9 deletions client/ayon_core/tools/workfiles/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def __init__(self, controller=None, parent=None):
overlay_invalid_host = InvalidHostOverlay(self)
overlay_invalid_host.setVisible(False)

first_show_timer = QtCore.QTimer()
first_show_timer.setSingleShot(True)
first_show_timer.setInterval(50)
show_timer = QtCore.QTimer()
show_timer.setSingleShot(True)
show_timer.setInterval(50)

first_show_timer.timeout.connect(self._on_first_show)
show_timer.timeout.connect(self._on_show)

controller.register_event_callback(
"save_as.finished",
Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(self, controller=None, parent=None):
self._tasks_widget = tasks_widget
self._side_panel = side_panel

self._first_show_timer = first_show_timer
self._show_timer = show_timer

self._post_init()

Expand Down Expand Up @@ -287,9 +287,9 @@ def refresh(self):

def showEvent(self, event):
super(WorkfilesToolWindow, self).showEvent(event)
self._show_timer.start()
if self._first_show:
self._first_show = False
self._first_show_timer.start()
self.setStyleSheet(style.load_stylesheet())

def keyPressEvent(self, event):
Expand All @@ -303,9 +303,8 @@ def keyPressEvent(self, event):

pass

def _on_first_show(self):
if not self._controller_refreshed:
self.refresh()
def _on_show(self):
self.refresh()

def _on_file_text_filter_change(self, text):
self._files_widget.set_text_filter(text)
Expand Down

0 comments on commit ce8bf35

Please sign in to comment.