Source code for ooodev.utils.gallery

# PM - Notes, Oct 25, 2022
#
# this module has issues.
# find_gallery_item() returns XGalleryItem which in may have properties such as Graphic (XGraphic).
# the issues is as soon as the XGalleryItem is returned from find_gallery_item() it has already lost its properties.
# see my post: https://ask.libreoffice.org/t/libreoffice-wiping-object-properties-issue-with-gallerythemeprovider/83182
#
# Checking the reference count inside of find_gallery_item() show there is only 1 ref could. Which means 0 references
# because sys.getrefcount() add 1 reference to the object that is being checked.
# find_gallery_graphic() has been added which duplicates the code of find_gallery_item() due to these issues.
# find_gallery_graphic() does successfully return XGraphic object, However I am not sure if this graphic can be used
# as it is. I tried inserting the XGraphic into a Draw XShape and putting it on the document, however it appears to
# always be the same graphic even though the criteria is change for find_gallery_graphic()
from __future__ import annotations
from typing import TYPE_CHECKING, Any, List, cast
from pathlib import Path
from typing import overload
import uno
from com.sun.star.gallery import XGalleryItem
from com.sun.star.gallery import XGalleryTheme
from com.sun.star.gallery import XGalleryThemeProvider
from com.sun.star.graphic import XGraphic


from ooodev.utils import file_io as mFileIo
from ooodev.loader import lo as mLo
from ooodev.utils import props as mProps
from ooodev.utils import info as mInfo
from ooodev.exceptions import ex as mEx
from ooodev.utils.kind.search_match_kind import SearchMatchKind as SearchMatchKind
from ooodev.utils.kind.gallery_kind import GalleryKind as GalleryKind
from ooodev.utils.kind.gallery_search_by_kind import SearchByKind as SearchByKind

from ooodev.events.event_singleton import _Events
from ooodev.events.lo_named_event import LoNamedEvent

from ooo.dyn.gallery.gallery_item_type import GalleryItemTypeEnum as GalleryItemTypeEnum
from ooo.lo.gallery.gallery_item_type import GalleryItemType as GalleryItemType

from ooodev.meta.static_meta import StaticProperty, classproperty

if TYPE_CHECKING:
    from com.sun.star.lang import EventObject
    from com.sun.star.beans import XPropertySetInfo


[docs]class GalleryObj: """ Represents Most properties of ``XGalleryItem``. An instance of this class is returned from :py:meth:`~.gallery.Gallery.find_gallery_obj` in place of ``XGalleryItem`` instance. This is due to a `bug <https://bugs.documentfoundation.org/show_bug.cgi?id=151932>`_ in ``LO 7.4``. """ # special case for this class. Matching most of GalleryItem properties so # using Camel case
[docs] def __init__(self, itm: XGalleryItem) -> None: self._graphic = mProps.Props.get(itm, "Graphic", None) self._drawing = mProps.Props.get(itm, "Drawing", None) url = cast(str, mProps.Props.get(itm, "URL", "")) self._url = Gallery.get_absolute_url(url) self._gallery_item_type = mProps.Props.get(itm, "GalleryItemType", 0) self._title = mProps.Props.get(itm, "Title", "") self._implementation_id = getattr(itm, "ImplementationId", None) self._implementation_name = getattr(itm, "ImplementationName", "") self._property_set_info = getattr(itm, "PropertySetInfo", None) self._property_to_default = getattr(itm, "PropertyToDefault", None) self._thumbnail = mProps.Props.get(itm, "Thumbnail", None)
@property def Graphic(self) -> XGraphic: return self._graphic @property def Drawing(self) -> Any: return self._drawing @property def URL(self) -> str: return self._url @property def GalleryItemType(self) -> int: return self._gallery_item_type @property def Title(self) -> str: return self._title @property def ImplementationId(self) -> uno.ByteSequence: return self._implementation_id # type: ignore @property def ImplementationName(self) -> str: return self._implementation_name @property def PropertySetInfo(self) -> XPropertySetInfo: return self._property_set_info # type: ignore @property def PropertyToDefault(self) -> Any: return self._property_to_default @property def Thumbnail(self) -> Any: return self._thumbnail
class _GalleryManager: """Manages clearing and resetting for Gallery static class""" @staticmethod def on_disposed(source: Any, event: EventObject) -> None: # Clean up static properties that may have been dynamically created. # print("Gallery Static Property Cleanup") data_attrs = ("_gallery_dir",) for attr in data_attrs: if hasattr(Gallery, attr): delattr(Gallery, attr) _Events().on(LoNamedEvent.BRIDGE_DISPOSED, _GalleryManager.on_disposed) __all__ = ("Gallery",)