working condition

This commit is contained in:
Yura 2024-09-15 20:57:02 +03:00
parent 417e54da96
commit 511e0b0379
517 changed files with 29187 additions and 32696 deletions

View file

@ -2,57 +2,8 @@
Components/FitImage
===================
Feature to automatically crop a `Kivy` image to fit your layout
Write by Benedikt Zwölfer
Referene - https://gist.github.com/benni12er/95a45eb168fc33a4fcd2d545af692dad
Example:
========
.. tabs::
.. tab:: Declarative KV styles
.. code-block:: kv
MDBoxLayout:
size_hint_y: None
height: "200dp"
orientation: 'vertical'
FitImage:
size_hint_y: 3
source: 'images/img1.jpg'
FitImage:
size_hint_y: 1
source: 'images/img2.jpg'
.. tab:: Declarative python styles
.. code-block:: python
MDBoxLayout(
FitImage(
size_hint_y=.3,
source='images/img1.jpg',
),
FitImage(
size_hint_y=.7,
source='images/img2.jpg',
),
size_hint_y=None,
height="200dp",
orientation='vertical',
)
Example with round corners:
===========================
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fitimage-round-corners.png
:align: center
Example
=======
.. tabs::
@ -66,33 +17,36 @@ Example with round corners:
KV = '''
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
MDCard:
radius: 36
md_bg_color: "grey"
MDBoxLayout:
radius: "36dp"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: .4, .8
md_bg_color: self.theme_cls.onSurfaceVariantColor
FitImage:
source: "bg.jpg"
source: "image.png"
size_hint_y: .35
pos_hint: {"top": 1}
radius: 36, 36, 0, 0
radius: "36dp", "36dp", 0, 0
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
.. tab:: Declarative python styles
.. code-block:: python
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.fitimage import FitImage
@ -101,19 +55,18 @@ Example with round corners:
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return (
MDScreen(
MDCard(
MDBoxLayout(
FitImage(
source="bg.jpg",
source="image.png",
size_hint_y=0.35,
pos_hint={"top": 1},
radius=(36, 36, 0, 0),
radius=(dp(36), dp(36), 0, 0),
),
radius=36,
md_bg_color="grey",
pos_hint={"center_x": .5, "center_y": .5},
radius=dp(36),
md_bg_color=self.theme_cls.onSurfaceVariantColor,
pos_hint={"center_x": 0.5, "center_y": 0.5},
size_hint=(0.4, 0.8),
),
)
@ -121,118 +74,39 @@ Example with round corners:
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fitimage-round-corners.png
:align: center
"""
__all__ = ("FitImage",)
from kivy.clock import Clock
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.properties import BooleanProperty, ObjectProperty
from kivy.properties import OptionProperty
from kivy.uix.image import AsyncImage
from kivy.uix.widget import Widget
from kivymd.uix.behaviors import StencilBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.behaviors import DeclarativeBehavior
class FitImage(MDBoxLayout, StencilBehavior):
class FitImage(DeclarativeBehavior, StencilBehavior, AsyncImage):
"""
Fit image class.
For more information, see in the
:class:`~kivymd.uix.boxlayout.MDLayout` and
:class:`~kivymd.uix.behaviors.StencilBehavior` classes documentation.
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivy.uix.image.AsyncImage` and
:class:`~kivymd.uix.behaviors.stencil_behavior.StencilBehavior`
classes documentation.
"""
source = ObjectProperty()
fit_mode = OptionProperty(
"cover", options=["scale-down", "fill", "contain", "cover"]
)
"""
Filename/source of your image.
Image will be stretched horizontally or vertically to fill the widget box,
**maintaining its aspect ratio**. If the image has a different aspect ratio
than the widget, then the image will be clipped to fit.
:attr:`source` is a :class:`~kivy.properties.StringProperty`
and defaults to None.
:attr:`fit_mode` is a :class:`~kivy.properties.OptionProperty` and
defaults to `'cover'`.
"""
mipmap = BooleanProperty(False)
"""
Indicate if you want OpenGL mipmapping to be applied to the texture.
Read :ref:`mipmap` for more information.
.. versionadded:: 1.0.0
:attr:`mipmap` is a :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""
_container = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self._late_init)
def _late_init(self, *args):
self._container = Container(self.source, self.mipmap)
self.bind(source=self._container.setter("source"))
self.add_widget(self._container)
def reload(self):
self._container.image.reload()
class Container(Widget):
source = ObjectProperty()
image = ObjectProperty()
def __init__(self, source, mipmap, **kwargs):
super().__init__(**kwargs)
self.image = AsyncImage(mipmap=mipmap)
self.loader_clock = Clock.schedule_interval(
self.adjust_size, self.image.anim_delay
)
self.image.bind(
on_load=lambda inst: (
self.adjust_size(),
self.loader_clock.cancel(),
)
)
self.source = source
self.bind(size=self.adjust_size, pos=self.adjust_size)
def on_source(self, instance, value):
if isinstance(value, str):
self.image.source = value
else:
self.image.texture = value
self.adjust_size()
def adjust_size(self, *args):
if not self.parent or not self.image.texture:
return
(par_x, par_y) = self.parent.size
if par_x == 0 or par_y == 0:
with self.canvas:
self.canvas.clear()
return
par_scale = par_x / par_y
(img_x, img_y) = self.image.texture.size
img_scale = img_x / img_y
if par_scale > img_scale:
(img_x_new, img_y_new) = (img_x, img_x / par_scale)
else:
(img_x_new, img_y_new) = (img_y * par_scale, img_y)
crop_pos_x = (img_x - img_x_new) / 2
crop_pos_y = (img_y - img_y_new) / 2
subtexture = self.image.texture.get_region(
crop_pos_x, crop_pos_y, img_x_new, img_y_new
)
with self.canvas:
self.canvas.clear()
Color(1, 1, 1)
Rectangle(texture=subtexture, pos=self.pos, size=(par_x, par_y))