Skip to content

files_widget_workarea

MultiWorkAreaFilesModel

Bases: QStandardItemModel

A model for workarea workfiles.

Parameters:

Name Type Description Default
controller AbstractWorkfilesFrontend

The control object.

required
Source code in client/ayon_wrap/workfiles/widgets/files_widget_workarea.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
class MultiWorkAreaFilesModel(QtGui.QStandardItemModel):
    """A model for workarea workfiles.

        Args:
            controller (AbstractWorkfilesFrontend): The control object.
        """

    refreshed = QtCore.Signal()
    columns = [
        "Name",
        "Author",
        "Date Modified",
    ]
    date_modified_col = columns.index("Date Modified")

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

        self.setColumnCount(len(self.columns))

        for idx, label in enumerate(self.columns):
            self.setHeaderData(idx, QtCore.Qt.Horizontal, label)

        self._file_icon = qtawesome.icon(
            "fa.file-o",
            color=get_default_entity_icon_color()
        )
        self._controller = controller
        self._items_by_filename = {}
        self._missing_context_item = None
        self._missing_context_used = False
        self._empty_root_item = None
        self._empty_item_used = False

        self._add_missing_context_item()

        controller.register_event_callback(
            "template_changed",
            self._on_template_changed
        )

        self._selected_template_name = None

    def get_index_by_filename(self, filename):
        item = self._items_by_filename.get(filename)
        if item is None:
            return QtCore.QModelIndex()
        return self.indexFromItem(item)

    def has_available_items(self):
        return bool(self._items_by_filename)

    def _get_missing_context_item(self):
        if self._missing_context_item is None:
            message = "Select folder and task"
            item = QtGui.QStandardItem(message)
            icon = qtawesome.icon(
                "fa.times",
                color=get_disabled_entity_icon_color()
            )
            item.setData(icon, QtCore.Qt.DecorationRole)
            item.setFlags(QtCore.Qt.NoItemFlags)
            item.setColumnCount(self.columnCount())
            self._missing_context_item = item
        return self._missing_context_item

    def _clear_items(self):
        self._remove_missing_context_item()
        self._remove_empty_item()
        if self._items_by_filename:
            root = self.invisibleRootItem()
            root.removeRows(0, root.rowCount())
            self._items_by_filename = {}

    def _add_missing_context_item(self):
        if self._missing_context_used:
            return
        self._clear_items()
        root_item = self.invisibleRootItem()
        root_item.appendRow(self._get_missing_context_item())
        self._missing_context_used = True

    def _remove_missing_context_item(self):
        if not self._missing_context_used:
            return
        root_item = self.invisibleRootItem()
        root_item.takeRow(self._missing_context_item.row())
        self._missing_context_used = False

    def _get_empty_root_item(self):
        if self._empty_root_item is None:
            message = "Work Area is empty.."
            item = QtGui.QStandardItem(message)
            icon = qtawesome.icon(
                "fa.exclamation-circle",
                color=get_disabled_entity_icon_color()
            )
            item.setData(icon, QtCore.Qt.DecorationRole)
            item.setFlags(QtCore.Qt.NoItemFlags)
            item.setColumnCount(self.columnCount())
            self._empty_root_item = item
        return self._empty_root_item

    def _add_empty_item(self):
        if self._empty_item_used:
            return
        self._clear_items()
        root_item = self.invisibleRootItem()
        root_item.appendRow(self._get_empty_root_item())
        self._empty_item_used = True

    def _remove_empty_item(self):
        if not self._empty_item_used:
            return
        root_item = self.invisibleRootItem()
        root_item.takeRow(self._empty_root_item.row())
        self._empty_item_used = False

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

    def _fill_items_impl(self):
        template_name = self._selected_template_name
        if not template_name:
            self._add_missing_context_item()
            return

        file_items = self._controller.get_workarea_file_items(template_name)
        root_item = self.invisibleRootItem()
        if not file_items:
            self._add_empty_item()
            return
        self._remove_empty_item()
        self._remove_missing_context_item()
        #user_items_by_name = self._controller.get_user_items_by_name()
        user_items_by_name = {}  # TODO

        items_to_remove = set(self._items_by_filename.keys())
        new_items = []
        for file_item in file_items:
            filename = file_item.filename
            if filename in self._items_by_filename:
                items_to_remove.discard(filename)
                item = self._items_by_filename[filename]
            else:
                item = QtGui.QStandardItem()
                new_items.append(item)
                item.setColumnCount(self.columnCount())
                item.setFlags(
                    QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
                )
                item.setData(self._file_icon, QtCore.Qt.DecorationRole)
                item.setData(file_item.filename, QtCore.Qt.DisplayRole)
                item.setData(file_item.filename, FILENAME_ROLE)

            updated_by = file_item.updated_by
            user_item = user_items_by_name.get(updated_by)
            if user_item is not None and user_item.full_name:
                updated_by = user_item.full_name

            item.setData(file_item.filepath, FILEPATH_ROLE)
            item.setData(updated_by, AUTHOR_ROLE)
            item.setData(file_item.modified, DATE_MODIFIED_ROLE)

            self._items_by_filename[file_item.filename] = item

        if new_items:
            root_item.appendRows(new_items)

        for filename in items_to_remove:
            item = self._items_by_filename.pop(filename)
            root_item.removeRow(item.row())

        if root_item.rowCount() == 0:
            self._add_empty_item()

    def flags(self, index):
        # Use flags of first column for all columns
        if index.column() != 0:
            index = self.index(index.row(), 0, index.parent())
        return super().flags(index)

    def data(self, index, role=None):
        if role is None:
            role = QtCore.Qt.DisplayRole

        # Handle roles for first column
        col = index.column()
        if col == 0:
            return super().data(index, role)

        if role == QtCore.Qt.DecorationRole:
            return None

        if role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole):
            if col == 1:
                role = AUTHOR_ROLE
            elif col == 2:
                role = DATE_MODIFIED_ROLE
            else:
                return None
        index = self.index(index.row(), 0, index.parent())

        return super().data(index, role)

    def _on_template_changed(self, event):
        self._selected_template_name = event["template_name"]
        self._fill_items()

MultiWorkAreaFilesWidget

Bases: QWidget

Workarea files widget.

Parameters:

Name Type Description Default
controller AbstractWorkfilesFrontend

The control object.

required
parent QWidget

The parent widget.

required
Source code in client/ayon_wrap/workfiles/widgets/files_widget_workarea.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
class MultiWorkAreaFilesWidget(QtWidgets.QWidget):
    """Workarea files widget.

    Args:
        controller (AbstractWorkfilesFrontend): The control object.
        parent (QtWidgets.QWidget): The parent widget.
    """

    refreshed = QtCore.Signal()
    selection_changed = QtCore.Signal()
    open_current_requested = QtCore.Signal()
    duplicate_requested = QtCore.Signal()

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

        view = TreeView(self)
        view.setSortingEnabled(True)
        view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        # Smaller indentation
        view.setIndentation(0)

        model = MultiWorkAreaFilesModel(controller)
        proxy_model = QtCore.QSortFilterProxyModel()
        proxy_model.setSourceModel(model)
        proxy_model.setSortCaseSensitivity(QtCore.Qt.CaseInsensitive)
        proxy_model.setDynamicSortFilter(True)

        view.setModel(proxy_model)

        time_delegate = PrettyTimeDelegate()
        view.setItemDelegateForColumn(model.date_modified_col, time_delegate)

        # Default to a wider first filename column it is what we mostly care
        # about and the date modified is relatively small anyway.
        view.setColumnWidth(0, 300)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.addWidget(view, 1)

        view.double_clicked.connect(self._on_mouse_double_click)
        view.customContextMenuRequested.connect(self._on_context_menu)
        model.refreshed.connect(self.refreshed)
        model.refreshed.connect(self._on_model_refresh)

        self._view = view
        self._model = model
        self._proxy_model = proxy_model
        self._time_delegate = time_delegate
        self._controller = controller

        self._published_mode = False

    def has_available_items(self):
        return self._model.has_available_items()

    def set_text_filter(self, text_filter):
        """Set the text filter.

        Args:
            text_filter (str): The text filter.
        """

        self._proxy_model.setFilterFixedString(text_filter)

    def _get_selected_info(self):
        selection_model = self._view.selectionModel()
        filepath = None
        filename = None
        for index in selection_model.selectedIndexes():
            filepath = index.data(FILEPATH_ROLE)
            filename = index.data(FILENAME_ROLE)
        return {
            "filepath": filepath,
            "filename": filename,
        }

    def get_selected_path(self):
        """Selected filepath.

        Returns:
            Union[str, None]: The selected filepath or None if nothing is
                selected.
        """
        return self._get_selected_info()["filepath"]

    def _on_mouse_double_click(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.open_current_requested.emit()

    def _on_context_menu(self, point):
        index = self._view.indexAt(point)
        if not index.isValid():
            return

        if not index.flags() & QtCore.Qt.ItemIsEnabled:
            return

        menu = QtWidgets.QMenu(self)

        # Duplicate
        action = QtWidgets.QAction("Duplicate", menu)
        tip = "Duplicate selected file."
        action.setToolTip(tip)
        action.setStatusTip(tip)
        action.triggered.connect(self._on_duplicate_pressed)
        menu.addAction(action)

        # Show the context action menu
        global_point = self._view.mapToGlobal(point)
        _ = menu.exec_(global_point)

    def _on_duplicate_pressed(self):
        self.duplicate_requested.emit()

    def _on_model_refresh(self):
        if 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
            )
        )

get_selected_path()

Selected filepath.

Returns:

Type Description

Union[str, None]: The selected filepath or None if nothing is selected.

Source code in client/ayon_wrap/workfiles/widgets/files_widget_workarea.py
309
310
311
312
313
314
315
316
def get_selected_path(self):
    """Selected filepath.

    Returns:
        Union[str, None]: The selected filepath or None if nothing is
            selected.
    """
    return self._get_selected_info()["filepath"]

set_text_filter(text_filter)

Set the text filter.

Parameters:

Name Type Description Default
text_filter str

The text filter.

required
Source code in client/ayon_wrap/workfiles/widgets/files_widget_workarea.py
288
289
290
291
292
293
294
295
def set_text_filter(self, text_filter):
    """Set the text filter.

    Args:
        text_filter (str): The text filter.
    """

    self._proxy_model.setFilterFixedString(text_filter)