Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
GifPlayer Class Referencefinal

A widget for displaying and playing animated GIFs within a chat interface. More...

#include <gif_player.h>

Inheritance diagram for GifPlayer:
Collaboration diagram for GifPlayer:

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< GifDecoderdecoder_
 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.
 
TranslationManagertranslator_ = nullptr
 Pointer to the translation manager for handling UI translations.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ GifPlayer()

GifPlayer::GifPlayer ( const QByteArray & gif_data,
QWidget * parent = nullptr )
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.

Parameters
gif_dataThe raw GIF data to be played.
parentOptional parent widget.
Here is the call graph for this function:

◆ ~GifPlayer()

GifPlayer::~GifPlayer ( )
inlineoverride

Destructor. Ensures resources are released by calling ReleaseResources().

Here is the call graph for this function:

Member Function Documentation

◆ DisplayThumbnail

void GifPlayer::DisplayThumbnail ( const QImage & frame)
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).

Parameters
frameThe first decoded frame of the GIF.
Here is the caller graph for this function:

◆ enterEvent()

void GifPlayer::enterEvent ( QEvent * event)
overrideprotected

Handles mouse enter events. Calls StartPlayback() to begin animation when the mouse cursor enters the widget area.

Parameters
eventThe enter event.
Here is the call graph for this function:

◆ gifLoaded

void GifPlayer::gifLoaded ( )
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.

Here is the caller graph for this function:

◆ HandleError

void GifPlayer::HandleError ( const QString & message)
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.

Parameters
messageThe error message from the decoder.
Here is the caller graph for this function:

◆ HandleGifInfo

void GifPlayer::HandleGifInfo ( int width,
int height,
double duration,
double frame_rate )
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.

Parameters
widthThe width of the GIF in pixels.
heightThe height of the GIF in pixels.
durationThe total duration of the GIF in seconds.
frame_rateThe calculated average frame rate.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ leaveEvent()

void GifPlayer::leaveEvent ( QEvent * event)
overrideprotected

Handles mouse leave events. Calls StopPlayback() to pause animation when the mouse cursor leaves the widget area.

Parameters
eventThe leave event.
Here is the call graph for this function:

◆ ReleaseResources()

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.

Here is the caller graph for this function:

◆ sizeHint()

QSize GifPlayer::sizeHint ( ) const
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.

Returns
The recommended QSize for the widget.
Here is the caller graph for this function:

◆ StartPlayback()

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.

Here is the caller graph for this function:

◆ StopPlayback()

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.

Here is the caller graph for this function:

◆ UpdateFrame

void GifPlayer::UpdateFrame ( const QImage & frame) const
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.

Parameters
frameThe newly decoded frame from the GIF.
Here is the caller graph for this function:

Member Data Documentation

◆ current_position_

double GifPlayer::current_position_ = 0
private

Current playback position in seconds. Updated by UpdatePosition.

◆ decoder_

std::shared_ptr<GifDecoder> GifPlayer::decoder_
private

Shared pointer to the GifDecoder instance responsible for decoding.

◆ frame_rate_

double GifPlayer::frame_rate_ = 0
private

Average frame rate of the GIF. Set by HandleGifInfo.

◆ gif_data_

QByteArray GifPlayer::gif_data_
private

Stores the raw GIF data passed in the constructor.

◆ gif_duration_

double GifPlayer::gif_duration_ = 0
private

Total duration of the GIF in seconds. Set by HandleGifInfo.

◆ gif_height_

int GifPlayer::gif_height_ = 0
private

Original height of the GIF in pixels. Set by HandleGifInfo.

◆ gif_label_

QLabel* GifPlayer::gif_label_
private

QLabel used to display the GIF frames. ScaledContents is enabled.

◆ gif_width_

int GifPlayer::gif_width_ = 0
private

Original width of the GIF in pixels. Set by HandleGifInfo.

◆ is_playing_

bool GifPlayer::is_playing_ = false
private

Flag indicating if the GIF is currently playing (mouse hover).

◆ thumbnail_pixmap_

QPixmap GifPlayer::thumbnail_pixmap_
private

Stores the first frame as a QPixmap for efficient display as a thumbnail.

◆ translator_

TranslationManager* GifPlayer::translator_ = nullptr
private

Pointer to the translation manager for handling UI translations.


The documentation for this class was generated from the following files: