Commit 2feded88 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetbrowser] Implement setCurrentAsset and currentAsset

These will allow for synchronisation between editor
and the browser.

This patch also regactors the code for testing purpose.
parent f65a3f46
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from PyQt5.QtCore import Qt
from ..backend.asset import AssetType
from ..backend.asset import Asset
from ..widgets.assetbrowser import AssetBrowser
class TestAssetBrowser:
def test_set_current_asset(self, qtbot, test_prefix, beat_context):
browser = AssetBrowser()
qtbot.addWidget(browser)
browser.set_context(beat_context)
asset = Asset(test_prefix, AssetType.ALGORITHM, "user/integers_add_v2/1")
with qtbot.waitSignal(browser.view.selectionModel().currentChanged):
browser.setCurrentAsset(asset)
assert browser.currentAsset() == asset
def test_double_click(self, qtbot, test_prefix, beat_context):
browser = AssetBrowser()
qtbot.addWidget(browser)
browser.set_context(beat_context)
asset = Asset(test_prefix, AssetType.ALGORITHM, "autonomous/add/1")
browser.setCurrentAsset(asset)
index = browser.view.currentIndex()
browser.view.scrollTo(index)
rect = browser.view.visualRect(index)
click_point = rect.center()
with qtbot.waitSignal(browser.view.clicked):
qtbot.mouseClick(browser.view.viewport(), Qt.LeftButton, pos=click_point)
with qtbot.waitSignal(browser.assetSelected):
qtbot.mouseDClick(browser.view.viewport(), Qt.LeftButton, pos=click_point)
def test_deletion_request(self, qtbot, test_prefix, beat_context):
browser = AssetBrowser()
qtbot.addWidget(browser)
browser.set_context(beat_context)
asset = Asset(test_prefix, AssetType.ALGORITHM, "autonomous/add/1")
browser.setCurrentAsset(asset)
index = browser.view.currentIndex()
browser.view.scrollTo(index)
rect = browser.view.visualRect(index)
click_point = rect.center()
right_click_point = browser.view.mapFromGlobal(
browser.view.viewport().mapToGlobal(click_point)
)
# customContextMenuRequested doesn't currently seem to work in testing.
# with qtbot.waitSignal(browser.view.customContextMenuRequested):
# qtbot.mouseClick(browser.view, Qt.RightButton, pos=right_click_point)
# therefore let's manually trigger the slot
browser._openMenu(right_click_point)
with qtbot.waitSignal(browser.deletionRequested):
qtbot.mouseClick(browser.contextual_menu, Qt.LeftButton)
......@@ -98,6 +98,7 @@ class AssetBrowser(QWidget):
"""Constructor"""
super(AssetBrowser, self).__init__(parent)
self.contextual_menu = None
self.filesystem_model = QFileSystemModel()
self.filesystem_model.setNameFilters(["*.json"])
self.filesystem_model.setNameFilterDisables(False)
......@@ -114,11 +115,11 @@ class AssetBrowser(QWidget):
layout = QVBoxLayout(self)
layout.addWidget(self.view)
self.view.doubleClicked.connect(self.__onItemSelected)
self.view.customContextMenuRequested.connect(self.__openMenu)
self.view.doubleClicked.connect(self._onItemDoubleClicked)
self.view.customContextMenuRequested.connect(self._openMenu)
@pyqtSlot("QModelIndex")
def __onItemSelected(self, index):
def _onItemDoubleClicked(self, index):
"""When an item is selected, emit the jsonSelected signal with
the corresponding JSON file path.
"""
......@@ -133,23 +134,44 @@ class AssetBrowser(QWidget):
)
@pyqtSlot("QPoint")
def __openMenu(self, position):
def _openMenu(self, position):
"""Handle the contextual menu"""
indexes = self.view.selectedIndexes()
if indexes and indexes[0].data().endswith(".json"):
menu = QMenu()
action = menu.addAction(self.tr("Delete"))
action.triggered.connect(self.__onDelete)
if self.contextual_menu is None:
self.contextual_menu = QMenu()
action = self.contextual_menu.addAction(self.tr("Delete"))
action.triggered.connect(self._onDelete)
menu.exec_(self.view.viewport().mapToGlobal(position))
self.contextual_menu.popup(self.view.viewport().mapToGlobal(position))
@pyqtSlot()
def __onDelete(self):
def _onDelete(self):
"""Delete the selected asset"""
file_index, _ = self.view.selectedIndexes()
source_index = self.proxy_model.mapToSource(file_index)
path = self.filesystem_model.filePath(source_index)
self.deletionRequested.emit(path)
@pyqtSlot(Asset)
def setCurrentAsset(self, asset):
"""Set the current index of the view to match the asset"""
source_index = self.filesystem_model.index(asset.declaration_path)
self.view.setCurrentIndex(self.proxy_model.mapFromSource(source_index))
def currentAsset(self):
"""Returns the asset matching the currently selected index"""
current_index = self.view.currentIndex()
source_index = self.proxy_model.mapToSource(current_index)
return Asset.from_path(
self.filesystem_model.rootPath(),
self.filesystem_model.filePath(source_index),
)
def set_context(self, context):
"""Sets the root path of the prefix to edit"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment