Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

silx view: Fixed setting focus at startup when opening a dataset #3953

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/silx/app/view/Viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ def __createCustomNxdataWindow(self, customNxdataWidget):
layout.addWidget(customNxdataWidget)
return widget

def __h5FileLoaded(self, loadedH5):
def __h5FileLoaded(self, loadedH5, filename):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieved from Hdf5TreeModel.sigH5pyObjectLoaded

self.__context.pushRecentFile(loadedH5.file.filename)
if loadedH5.file.filename == self.__displayIt:
if filename == self.__displayIt:
self.__displayIt = None
self.displayData(loadedH5)

Expand Down
26 changes: 16 additions & 10 deletions src/silx/gui/hdf5/Hdf5TreeModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LoadingItemRunnable(qt.QRunnable):

class __Signals(qt.QObject):
"""Signal holder"""
itemReady = qt.Signal(object, object, object)
itemReady = qt.Signal(object, object, object, str)
runnerFinished = qt.Signal(object)

def __init__(self, filename, item):
Expand Down Expand Up @@ -129,7 +129,7 @@ def run(self):
if h5file is not None:
h5file.close()

self.itemReady.emit(self.oldItem, newItem, error)
self.itemReady.emit(self.oldItem, newItem, error, self.filename)
self.runnerFinished.emit(self)

def autoDelete(self):
Expand Down Expand Up @@ -184,7 +184,7 @@ class Hdf5TreeModel(qt.QAbstractItemModel):
]
"""List of logical columns available"""

sigH5pyObjectLoaded = qt.Signal(object)
sigH5pyObjectLoaded = qt.Signal(object, str)
"""Emitted when a new root item was loaded and inserted to the model."""

sigH5pyObjectRemoved = qt.Signal(object)
Expand Down Expand Up @@ -250,7 +250,6 @@ def _closeFileList(fileList):
"""Static method to close explicit references to internal objects."""
_logger.debug("Clear Hdf5TreeModel")
for obj in fileList:
_logger.debug("Close file %s", obj.filename)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing issues with the tests since h5py.Dataset do not have a filename attribute.

obj.close()
fileList[:] = []

Expand All @@ -269,14 +268,21 @@ def __updateLoadingItems(self, icon):
index2 = self.index(i, self.columnCount() - 1, qt.QModelIndex())
self.dataChanged.emit(index1, index2)

def __itemReady(self, oldItem, newItem, error):
def __itemReady(
self,
oldItem: Hdf5Node,
newItem: Optional[Hdf5Node],
error: Optional[Exception],
filename: str,
):
"""Called at the end of a concurent file loading, when the loading
item is ready. AN error is defined if an exception occured when
loading the newItem .

:param Hdf5Node oldItem: current displayed item
:param Hdf5Node newItem: item loaded, or None if error is defined
:param Exception error: An exception, or None if newItem is defined
:param oldItem: current displayed item
:param newItem: item loaded, or None if error is defined
:param error: An exception, or None if newItem is defined
:param filename: The filename used to load the new item
"""
row = self.__root.indexOfChild(oldItem)

Expand All @@ -294,7 +300,7 @@ def __itemReady(self, oldItem, newItem, error):
self.endInsertRows()

if isinstance(oldItem, Hdf5LoadingItem):
self.sigH5pyObjectLoaded.emit(newItem.obj)
self.sigH5pyObjectLoaded.emit(newItem.obj, filename)
else:
self.sigH5pyObjectSynchronized.emit(oldItem.obj, newItem.obj)

Expand Down Expand Up @@ -703,7 +709,7 @@ def insertFile(self, filename, row=-1):
h5file = silx_io.open(filename)
if self.__ownFiles:
self.__openedFiles.append(h5file)
self.sigH5pyObjectLoaded.emit(h5file)
self.sigH5pyObjectLoaded.emit(h5file, filename)
self.insertH5pyObject(h5file, row=row, filename=filename)
Comment on lines +712 to 713
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure here why the signal is emitted before inserting the item into the model? 🤷

except IOError:
_logger.debug("File '%s' can't be read.", filename, exc_info=True)
Expand Down
19 changes: 13 additions & 6 deletions src/silx/gui/hdf5/test/test_hdf5.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /*##########################################################################
#
# Copyright (c) 2016-2021 European Synchrotron Radiation Facility
# Copyright (c) 2016-2023 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,6 +40,7 @@
from silx.gui.utils.testutils import SignalListener
from silx.io import commonh5
from silx.io import h5py_utils
from silx.io.url import DataUrl
import weakref

import h5py
Expand Down Expand Up @@ -400,11 +401,17 @@ def testInsert(self):
self.assertEqual(self.listener.callCount(), 0)

def testLoaded(self):
self.model.insertFile(self.filename)
self.assertEqual(self.listener.callCount(), 1)
self.assertEqual(self.listener.karguments(argumentName="signal")[0], "loaded")
self.assertIsNot(self.listener.arguments(callIndex=0)[0], self.h5)
self.assertEqual(self.listener.arguments(callIndex=0)[0].filename, self.filename)
for data_path in [None, "/arrays/scalar"]:
with self.subTest(data_path=data_path):
url = DataUrl(file_path=self.filename, data_path=data_path)
insertedFilename = url.path()
self.model.insertFile(insertedFilename)
self.assertEqual(self.listener.callCount(), 1)
self.assertEqual(self.listener.karguments(argumentName="signal")[0], "loaded")
self.assertIsNot(self.listener.arguments(callIndex=0)[0], self.h5)
self.assertEqual(self.listener.arguments(callIndex=0)[0].file.filename, self.filename)
self.assertEqual(self.listener.arguments(callIndex=0)[1], insertedFilename)
self.listener.clear()

def testRemoved(self):
self.model.removeH5pyObject(self.h5)
Expand Down