Skip to content

Commit

Permalink
test tab bar menu
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizzFTD committed Sep 2, 2024
1 parent 2514bbd commit f164701
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
39 changes: 15 additions & 24 deletions grill/views/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,33 +596,24 @@ def tabRemoved(self, index: int) -> None:
del self._browsers_by_layer[self._tab_layer_by_idx.pop(index)]

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.RightButton:
if (tab_index:=self.tabBar().tabAt(event.pos())) != -1:
def _copy(content):
QtWidgets.QApplication.instance().clipboard().setText(content)

widget = self.widget(tab_index)
copy_identifier = QtGui.QAction("Copy Identifier", self)
copy_identifier.triggered.connect(partial(_copy, widget._identifier))
copy_resolved_path = QtGui.QAction("Copy Resolved Path", self)
copy_resolved_path.triggered.connect(partial(_copy, widget._resolved_path))

menu = QtWidgets.QMenu(self)
menu.addAction(copy_identifier)
menu.addAction(copy_resolved_path)
menu.addSeparator()
if tab_index < (max_tab_idx:=len(self._tab_layer_by_idx))-1:
close_right_tabs = QtGui.QAction("Close Tabs to the Right", self)
close_right_tabs.triggered.connect(partial(self._close_many, range(tab_index+1, max_tab_idx+1)))
menu.addAction(close_right_tabs)
if tab_index > 0:
close_left_tabs = QtGui.QAction("Close Tabs to the Left", self)
close_left_tabs.triggered.connect(partial(self._close_many, range(tab_index)))
menu.addAction(close_left_tabs)
menu.exec(event.globalPos())
if event.button() == QtCore.Qt.RightButton and (tab_index := self.tabBar().tabAt(event.pos())) != -1:
self._menu_for_tab(tab_index).exec(event.globalPos())

Check warning on line 600 in grill/views/description.py

View check run for this annotation

Codecov / codecov/patch

grill/views/description.py#L599-L600

Added lines #L599 - L600 were not covered by tests

super().mousePressEvent(event)

Check warning on line 602 in grill/views/description.py

View check run for this annotation

Codecov / codecov/patch

grill/views/description.py#L602

Added line #L602 was not covered by tests

def _menu_for_tab(self, tab_index):
widget = self.widget(tab_index)
clipboard = QtWidgets.QApplication.instance().clipboard()
menu = QtWidgets.QMenu(self)
menu.addAction("Copy Identifier", partial(clipboard.setText, widget._identifier))
menu.addAction("Copy Resolved Path", partial(clipboard.setText, widget._resolved_path))
menu.addSeparator()
if tab_index < (max_tab_idx := len(self._tab_layer_by_idx)) - 1:
menu.addAction("Close Tabs to the Right", partial(self._close_many, range(tab_index + 1, max_tab_idx + 1)))
if tab_index > 0:
menu.addAction("Close Tabs to the Left", partial(self._close_many, range(tab_index)))

Check warning on line 614 in grill/views/description.py

View check run for this annotation

Codecov / codecov/patch

grill/views/description.py#L614

Added line #L614 was not covered by tests
return menu

def _close_many(self, indices: range):
for index in reversed(indices):
self.tabCloseRequested.emit(index)

Check warning on line 619 in grill/views/description.py

View check run for this annotation

Codecov / codecov/patch

grill/views/description.py#L618-L619

Added lines #L618 - L619 were not covered by tests
Expand Down
13 changes: 8 additions & 5 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def _fake_run(run_args: list):
# sdffilter still not coming via pypi, so patch for now
with mock.patch("grill.views.description._core._run", new=_fake_run if not description._which("sdffilter") else _core_run):
dialog = description._start_content_browser(*args)
browser = dialog.findChild(description._PseudoUSDBrowser)
browser: description._PseudoUSDBrowser = dialog.findChild(description._PseudoUSDBrowser)
assert browser._browsers_by_layer.values()
first_browser_widget, = browser._browsers_by_layer.values()
first_browser_widget._format_options.setCurrentIndex(0) # pseudoLayer (through sdffilter)
Expand All @@ -643,6 +643,10 @@ def _fake_run(run_args: list):
browser._on_identifier_requested(anchor, layers[1].identifier)
with mock.patch(f"{QtWidgets.__name__}.QMessageBox.warning", new=_log):
browser._on_identifier_requested(anchor, "/missing/file.usd")

menu = browser._menu_for_tab(0)
self.assertTrue(bool(menu.actions()))

browser.tabCloseRequested.emit(0) # request closing our first tab
for child in dialog.findChildren(description._PseudoUSDBrowser):
child._resolved_layers.clear()
Expand All @@ -651,10 +655,9 @@ def _fake_run(run_args: list):
_, sourcepath = tempfile.mkstemp()
prim_index.DumpToDotGraph(sourcepath)
targetpath = f"{sourcepath}.png"
error, __ = _core_run([_core._which("dot"), sourcepath, "-Tpng", "-o", targetpath])
if error:
raise RuntimeError(error)
browser._addImageTab(targetpath, identifier=targetpath)
# create a temporary file loadable by our image tab
_core_run([_core._which("dot"), sourcepath, "-Tpng", "-o", targetpath])
browser._on_identifier_requested(anchor, targetpath)

invalid_crate_layer = Sdf.Layer.CreateAnonymous()
invalid_crate_layer.ImportFromString(
Expand Down

0 comments on commit f164701

Please sign in to comment.