|
Wavelength
Privacy-focused, cross-platform, and open-source communication application
|
A widget for displaying and playing animated GIFs within a chat interface. More...
#include <gif_player.h>
Signals | |
| void | gifLoaded () |
| Emitted after the GIF information (dimensions, duration, etc.) has been successfully received from the decoder. Can be used by parent widgets to adjust layout once the GIF size is known. | |
Public Member Functions | |
| GifPlayer (const QByteArray &gif_data, QWidget *parent=nullptr) | |
| Constructs an InlineGifPlayer widget. Initializes the UI (QLabel for display), creates the GifDecoder instance, connects signals for frame updates, errors, and GIF info, and triggers asynchronous initialization of the decoder to load the first frame. | |
| void | ReleaseResources () |
| Stops the decoder thread, waits for it to finish (with timeout), and resets the decoder pointer. Ensures proper cleanup, especially when the widget is destroyed or the application quits. | |
| QSize | sizeHint () const override |
| Provides a size hint based on the original dimensions of the GIF. Returns the actual GIF width and height once known, otherwise falls back to the default QFrame size hint. | |
| void | StartPlayback () |
| Starts or resumes GIF playback. Sets the is_playing_ flag and calls Resume() on the GifDecoder. Typically called automatically on mouse enter. | |
| void | StopPlayback () |
| Stops or pauses GIF playback. Clears the is_playing_ flag and calls Pause() on the GifDecoder. Typically called automatically on mouse leave. | |
| ~GifPlayer () override | |
| Destructor. Ensures resources are released by calling ReleaseResources(). | |
Protected Member Functions | |
| void | enterEvent (QEvent *event) override |
| Handles mouse enter events. Calls StartPlayback() to begin animation when the mouse cursor enters the widget area. | |
| void | leaveEvent (QEvent *event) override |
| Handles mouse leave events. Calls StopPlayback() to pause animation when the mouse cursor leaves the widget area. | |
Private Slots | |
| void | DisplayThumbnail (const QImage &frame) |
| Slot connected to the decoder's firstFrameReady signal. Displays the provided frame as a static thumbnail in the QLabel. Stores the pixmap for later use (e.g., when pausing). | |
| void | HandleError (const QString &message) |
| Slot connected to the decoder's error signal. Logs the error message and displays it in the QLabel. Sets a minimum size for the widget. | |
| void | HandleGifInfo (int width, int height, double duration, double frame_rate) |
| Slot connected to the decoder's gifInfo signal. Stores the GIF's dimensions, duration, and frame rate. Updates the widget's geometry based on the dimensions and emits the gifLoaded() signal. Redisplays the thumbnail if needed. | |
| void | UpdateFrame (const QImage &frame) const |
| Slot connected to the decoder's frameReady signal. Updates the QLabel to display the new frame, but only if playback is currently active (is_playing_ is true). Relies on QLabel's setScaledContents(true) for rendering. | |
Private Attributes | |
| double | current_position_ = 0 |
| Current playback position in seconds. Updated by UpdatePosition. | |
| std::shared_ptr< GifDecoder > | decoder_ |
| Shared pointer to the GifDecoder instance responsible for decoding. | |
| double | frame_rate_ = 0 |
| Average frame rate of the GIF. Set by HandleGifInfo. | |
| QByteArray | gif_data_ |
| Stores the raw GIF data passed in the constructor. | |
| double | gif_duration_ = 0 |
| Total duration of the GIF in seconds. Set by HandleGifInfo. | |
| int | gif_height_ = 0 |
| Original height of the GIF in pixels. Set by HandleGifInfo. | |
| QLabel * | gif_label_ |
| QLabel used to display the GIF frames. ScaledContents is enabled. | |
| int | gif_width_ = 0 |
| Original width of the GIF in pixels. Set by HandleGifInfo. | |
| bool | is_playing_ = false |
| Flag indicating if the GIF is currently playing (mouse hover). | |
| QPixmap | thumbnail_pixmap_ |
| Stores the first frame as a QPixmap for efficient display as a thumbnail. | |
| TranslationManager * | translator_ = nullptr |
| Pointer to the translation manager for handling UI translations. | |
A widget for displaying and playing animated GIFs within a chat interface.
This class uses GifDecoder to decode GIF data in a separate thread. It displays the first frame as a static thumbnail initially. When the user hovers the mouse over the widget, playback starts automatically. Playback pauses when the mouse leaves. The widget scales the displayed GIF while maintaining an aspect ratio.
|
explicit |
Constructs an InlineGifPlayer widget. Initializes the UI (QLabel for display), creates the GifDecoder instance, connects signals for frame updates, errors, and GIF info, and triggers asynchronous initialization of the decoder to load the first frame.
| gif_data | The raw GIF data to be played. |
| parent | Optional parent widget. |
|
inlineoverride |
Destructor. Ensures resources are released by calling ReleaseResources().
|
privateslot |
Slot connected to the decoder's firstFrameReady signal. Displays the provided frame as a static thumbnail in the QLabel. Stores the pixmap for later use (e.g., when pausing).
| frame | The first decoded frame of the GIF. |
|
overrideprotected |
Handles mouse enter events. Calls StartPlayback() to begin animation when the mouse cursor enters the widget area.
| event | The enter event. |
|
signal |
Emitted after the GIF information (dimensions, duration, etc.) has been successfully received from the decoder. Can be used by parent widgets to adjust layout once the GIF size is known.
|
privateslot |
Slot connected to the decoder's error signal. Logs the error message and displays it in the QLabel. Sets a minimum size for the widget.
| message | The error message from the decoder. |
|
privateslot |
Slot connected to the decoder's gifInfo signal. Stores the GIF's dimensions, duration, and frame rate. Updates the widget's geometry based on the dimensions and emits the gifLoaded() signal. Redisplays the thumbnail if needed.
| width | The width of the GIF in pixels. |
| height | The height of the GIF in pixels. |
| duration | The total duration of the GIF in seconds. |
| frame_rate | The calculated average frame rate. |
|
overrideprotected |
Handles mouse leave events. Calls StopPlayback() to pause animation when the mouse cursor leaves the widget area.
| event | The leave event. |
| void GifPlayer::ReleaseResources | ( | ) |
Stops the decoder thread, waits for it to finish (with timeout), and resets the decoder pointer. Ensures proper cleanup, especially when the widget is destroyed or the application quits.
|
nodiscardoverride |
Provides a size hint based on the original dimensions of the GIF. Returns the actual GIF width and height once known, otherwise falls back to the default QFrame size hint.
| void GifPlayer::StartPlayback | ( | ) |
Starts or resumes GIF playback. Sets the is_playing_ flag and calls Resume() on the GifDecoder. Typically called automatically on mouse enter.
| void GifPlayer::StopPlayback | ( | ) |
Stops or pauses GIF playback. Clears the is_playing_ flag and calls Pause() on the GifDecoder. Typically called automatically on mouse leave.
|
privateslot |
Slot connected to the decoder's frameReady signal. Updates the QLabel to display the new frame, but only if playback is currently active (is_playing_ is true). Relies on QLabel's setScaledContents(true) for rendering.
| frame | The newly decoded frame from the GIF. |
|
private |
Current playback position in seconds. Updated by UpdatePosition.
|
private |
Shared pointer to the GifDecoder instance responsible for decoding.
|
private |
Average frame rate of the GIF. Set by HandleGifInfo.
|
private |
Stores the raw GIF data passed in the constructor.
|
private |
Total duration of the GIF in seconds. Set by HandleGifInfo.
|
private |
Original height of the GIF in pixels. Set by HandleGifInfo.
|
private |
QLabel used to display the GIF frames. ScaledContents is enabled.
|
private |
Original width of the GIF in pixels. Set by HandleGifInfo.
|
private |
Flag indicating if the GIF is currently playing (mouse hover).
|
private |
Stores the first frame as a QPixmap for efficient display as a thumbnail.
|
private |
Pointer to the translation manager for handling UI translations.