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

A widget for displaying static images (PNG, JPEG, etc.) within a chat interface. More...

#include <image_viewer.h>

Inheritance diagram for ImageViewer:
Collaboration diagram for ImageViewer:

Signals

void imageInfoReady (int width, int height, bool has_alpha)
 Emitted after the image information (dimensions, alpha) has been successfully received from the decoder.
 
void imageLoaded ()
 Emitted after the image has been successfully decoded and set in the QLabel. Can be used by parent widgets to adjust layout once the image is ready for display.
 

Public Member Functions

 ImageViewer (const QByteArray &image_data, QWidget *parent=nullptr)
 Constructs an InlineImageViewer widget. Initializes the UI (QLabel), creates the ImageDecoder instance, connects signals and triggers asynchronous decoding of the image data.
 
void ReleaseResources ()
 Releases resources held by the ImageDecoder. Resets the shared pointer to the decoder.
 
QSize sizeHint () const override
 Provides a size hint based on the original dimensions of the loaded image. Returns the actual image size once loaded, otherwise falls back to the default QFrame size hint.
 
 ~ImageViewer () override
 Destructor. Ensures resources are released by calling ReleaseResources().
 

Private Slots

void HandleError (const QString &message)
 Slot connected to the decoder's error signal. Logs the error and displays an error message in the QLabel. Sets a minimum size to ensure the error message is visible.
 
void HandleImageInfo (int width, int height, bool has_alpha)
 Slot connected to the decoder's imageInfo signal. Stores the image dimensions and alpha channel information. Emits the imageInfoReady() signal.
 
void HandleImageReady (const QImage &image)
 Slot connected to the decoder's imageReady signal. Stores the decoded image, sets it as the pixmap for the internal QLabel, updates the widget's geometry, and emits the imageLoaded() signal. Relies on the QLabel's setScaledContents(true) for display scaling.
 
void LoadImage () const
 Initiates the image decoding process by calling Decode() on the ImageDecoder. Called asynchronously via QTimer::singleShot after the constructor finishes.
 

Private Attributes

std::shared_ptr< ImageDecoderdecoder_
 Shared pointer to the ImageDecoder instance responsible for decoding.
 
bool has_alpha_ = false
 Flag indicating if the image has an alpha channel. Set by HandleImageInfo.
 
QByteArray image_data_
 Stores the raw image data passed in the constructor.
 
int image_height_ = 0
 Original height of the image in pixels. Set by HandleImageInfo.
 
QLabel * image_label_
 QLabel used to display the image. ScaledContents is enabled.
 
int image_width_ = 0
 Original width of the image in pixels. Set by HandleImageInfo.
 
QImage original_image_
 Stores the original, unscaled decoded image.
 
TranslationManagertranslator_ = nullptr
 Pointer to the translation manager for handling UI translations.
 

Detailed Description

A widget for displaying static images (PNG, JPEG, etc.) within a chat interface.

This class uses ImageDecoder (which relies on Qt's image loading) to decode image data. It displays the image within a QLabel, automatically scaling the content to fit the label's size while preserving an aspect ratio (due to QLabel's setScaledContents(true)). It provides signals for when the image is loaded and when its information (size, alpha) is available.

Constructor & Destructor Documentation

◆ ImageViewer()

ImageViewer::ImageViewer ( const QByteArray & image_data,
QWidget * parent = nullptr )
explicit

Constructs an InlineImageViewer widget. Initializes the UI (QLabel), creates the ImageDecoder instance, connects signals and triggers asynchronous decoding of the image data.

Parameters
image_dataThe raw image data to be displayed.
parentOptional parent widget.
Here is the call graph for this function:

◆ ~ImageViewer()

ImageViewer::~ImageViewer ( )
inlineoverride

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

Here is the call graph for this function:

Member Function Documentation

◆ HandleError

void ImageViewer::HandleError ( const QString & message)
privateslot

Slot connected to the decoder's error signal. Logs the error and displays an error message in the QLabel. Sets a minimum size to ensure the error message is visible.

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

◆ HandleImageInfo

void ImageViewer::HandleImageInfo ( int width,
int height,
bool has_alpha )
privateslot

Slot connected to the decoder's imageInfo signal. Stores the image dimensions and alpha channel information. Emits the imageInfoReady() signal.

Parameters
widthThe width of the image in pixels.
heightThe height of the image in pixels.
has_alphaTrue if the image has an alpha channel.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleImageReady

void ImageViewer::HandleImageReady ( const QImage & image)
privateslot

Slot connected to the decoder's imageReady signal. Stores the decoded image, sets it as the pixmap for the internal QLabel, updates the widget's geometry, and emits the imageLoaded() signal. Relies on the QLabel's setScaledContents(true) for display scaling.

Parameters
imageThe successfully decoded QImage.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ imageInfoReady

void ImageViewer::imageInfoReady ( int width,
int height,
bool has_alpha )
signal

Emitted after the image information (dimensions, alpha) has been successfully received from the decoder.

Parameters
widthThe width of the image in pixels.
heightThe height of the image in pixels.
has_alphaTrue if the image has an alpha channel.
Here is the caller graph for this function:

◆ imageLoaded

void ImageViewer::imageLoaded ( )
signal

Emitted after the image has been successfully decoded and set in the QLabel. Can be used by parent widgets to adjust layout once the image is ready for display.

Here is the caller graph for this function:

◆ LoadImage

void ImageViewer::LoadImage ( ) const
privateslot

Initiates the image decoding process by calling Decode() on the ImageDecoder. Called asynchronously via QTimer::singleShot after the constructor finishes.

Here is the caller graph for this function:

◆ ReleaseResources()

void ImageViewer::ReleaseResources ( )

Releases resources held by the ImageDecoder. Resets the shared pointer to the decoder.

Here is the caller graph for this function:

◆ sizeHint()

QSize ImageViewer::sizeHint ( ) const
nodiscardoverride

Provides a size hint based on the original dimensions of the loaded image. Returns the actual image size once loaded, otherwise falls back to the default QFrame size hint.

Returns
The recommended QSize for the widget (original image size).

Member Data Documentation

◆ decoder_

std::shared_ptr<ImageDecoder> ImageViewer::decoder_
private

Shared pointer to the ImageDecoder instance responsible for decoding.

◆ has_alpha_

bool ImageViewer::has_alpha_ = false
private

Flag indicating if the image has an alpha channel. Set by HandleImageInfo.

◆ image_data_

QByteArray ImageViewer::image_data_
private

Stores the raw image data passed in the constructor.

◆ image_height_

int ImageViewer::image_height_ = 0
private

Original height of the image in pixels. Set by HandleImageInfo.

◆ image_label_

QLabel* ImageViewer::image_label_
private

QLabel used to display the image. ScaledContents is enabled.

◆ image_width_

int ImageViewer::image_width_ = 0
private

Original width of the image in pixels. Set by HandleImageInfo.

◆ original_image_

QImage ImageViewer::original_image_
private

Stores the original, unscaled decoded image.

◆ translator_

TranslationManager* ImageViewer::translator_ = nullptr
private

Pointer to the translation manager for handling UI translations.


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