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

The main view for displaying and interacting with a specific Wavelength chat session. More...

#include <chat_view.h>

Inheritance diagram for ChatView:
Collaboration diagram for ChatView:

Public Slots

void AttachFile ()
 Opens a file dialog to allow the user to select a file for sending. If a file is selected, initiates the file sending process via WavelengthMessageService and displays an initial progress message in the message area.
 

Signals

void wavelengthAborted ()
 Emitted when the user aborts the connection or the Wavelength is closed by the host. Signals the parent view to switch back.
 

Public Member Functions

 ChatView (QWidget *parent=nullptr)
 Constructs a WavelengthChatView. Initializes the UI elements (labels, message area, input fields, buttons), sets up layouts, initializes audio components (input/output, format, sounds), connects signals/slots for user interaction and backend events, and starts timers for time display and random visual effects.
 
void Clear ()
 Clears the view and resets its state. Resets the current frequency, clears the message area, header, and input field, and hides the view.
 
double GetScanlineOpacity () const
 Gets the current opacity of the scanline effect.
 
void OnMessageReceived (const QString &frequency, const QString &message)
 Handles displaying a received message for the current Wavelength. Adds the message to the message_area_ if the frequency matches the current one. Triggers a visual activity effect.
 
void OnMessageSent (const QString &frequency, const QString &message) const
 Handles displaying a message sent by the local user on the current Wavelength. Adds the message to the message_area_ if the frequency matches the current one.
 
void OnWavelengthClosed (const QString &frequency)
 Handles the event when the host closes the current Wavelength. Updates the status indicator, displays a system message, and schedules the view to be cleared, and the wavelengthAborted() signal to be emitted after a delay. Does nothing if the frequency doesn't match or if the local user initiated the closure.
 
void SetScanlineOpacity (double opacity)
 Sets the opacity of the scanline effect. Triggers a repaint of the widget.
 
void SetWavelength (const QString &frequency, const QString &name=QString())
 Configures the view for a specific Wavelength. Sets the header title with the frequency and optional name, clears the message area, displays a connection message, resets PTT state, enables controls, triggers a connection visual effect, and makes the view visible.
 
 ~ChatView () override
 Destructor. Stops audio input/output and cleans up audio resources.
 

Protected Member Functions

void paintEvent (QPaintEvent *event) override
 Overridden paint event handler. Draws the custom cyberpunk border, AR markers, and scanline effect.
 

Properties

double scanlineOpacity
 Property controlling the opacity of the animated scanline effect (0.0 to 1.0). Animatable.
 

Private Types

enum  PttState { Idle , Requesting , Transmitting , Receiving }
 Enum representing the possible states of the Push-to-Talk (PTT) functionality. More...
 

Private Slots

void AbortWavelength ()
 Initiates the process of leaving or closing the current Wavelength. Updates the status indicator, calls the appropriate method on WavelengthSessionCoordinator (CloseWavelength if hosted, LeaveWavelength otherwise), emits wavelengthAborted(), and schedules the view to be cleared after a short delay.
 
void OnAudioDataReceived (const QString &frequency, const QByteArray &audio_data) const
 Slot called when binary audio data is received from the server. If the frequency matches and the state is Received, writes the data to the audio output device, calculates the amplitude of the received audio, and updates the audio visualization in the message area.
 
void OnPttButtonPressed ()
 Slot called when the PTT button is pressed. Plays the PTT activation sound, sets the state to Requesting, updates the button appearance, and sends a PTT request to the server via WavelengthMessageService.
 
void OnPttButtonReleased ()
 Slot called when the PTT button is released. Plays the PTT deactivation sound. If transmitting, stops audio input and sends a PTT release message. Resets the PTT state to Idle and updates the button appearance.
 
void OnPttDenied (const QString &frequency, const QString &reason)
 Slot called when the server denies the PTT request. If the frequency matches and the state is Requested, displays a system message with the reason, resets the PTT state to Idle, and updates the button appearance.
 
void OnPttGranted (const QString &frequency)
 Slot called when the server grants the PTT request. If the frequency matches and the state is Requested, sets the state to Transmitting, updates the button appearance, starts audio input, and updates the message area to show the local user is transmitting.
 
void OnPttStartReceiving (const QString &frequency, const QString &sender_id)
 Slot called when another user starts transmitting PTT on the current Wavelength. If the state is Idle, sets the state to Receiving, updates the button appearance (disabling it), starts audio output, and updates the message area to show the remote sender's ID.
 
void OnPttStopReceiving (const QString &frequency)
 Slot called when the remote user stops transmitting PTT. If the frequency matches and the state is Receiving, stops audio output, resets the PTT state to Idle, updates the button appearance, clears the transmitting user display in the message area, and resets the audio amplitude visualization.
 
void OnReadyReadInput () const
 Slot called when the audio input device has data ready to be read. Reads all available audio data, sends it to the server via WavelengthMessageService, calculates the amplitude, and updates the local audio visualization in the message area.
 
void SendMessage () const
 Sends the text message currently in the input field. Clears the input field and sends the message via WavelengthMessageService.
 
void TriggerActivityEffect ()
 Triggers a subtle scanline effect animation when there is activity (e.g., a message received).
 
void TriggerConnectionEffect ()
 Triggers a more pronounced scanline effect animation when connecting to a Wavelength.
 
void TriggerVisualEffect ()
 Triggers a random, brief increase in the scanline effect opacity.
 
void UpdateProgressMessage (const QString &message_id, const QString &message) const
 Slot to update a message in the message area, typically used for file transfer progress. Finds the message by its ID and updates its content.
 

Private Member Functions

qreal CalculateAmplitude (const QByteArray &buffer) const
 Calculates the Root Mean Square (RMS) amplitude of a raw audio buffer. Used for audio visualization. Assumes 16-bit signed integer PCM data.
 
void InitializeAudio ()
 Initializes the audio format and creates QAudioInput/QAudioOutput objects. Determines supported formats and sets buffer sizes.
 
void ResetStatusIndicator () const
 Resets the status_indicator_ label to its default "CONNECTION ACTIVE" state and style.
 
void StartAudioInput ()
 Starts capturing audio from the microphone if PTT state is Transmitting. Connects the readyRead signal to OnReadyReadInput().
 
void StartAudioOutput ()
 Starts audio playback if PTT state is Receiving. Prepares the audio output device to receive data.
 
void StopAudioInput ()
 Stops capturing audio from the microphone. Disconnects signals and resets the input device pointer.
 
void StopAudioOutput ()
 Stops audio playback. Resets the output device pointer.
 
void UpdatePttButtonState () const
 Updates the appearance and enabled state of the PTT button based on the current ptt_state_. Also enables/disables other input controls (text field, send/attach buttons) accordingly.
 

Private Attributes

QPushButton * abort_button_
 Button to disconnect from the current Wavelength.
 
QPushButton * attach_button_
 Button to open the file attachment dialog.
 
QAudioFormat audio_format_
 The audio format used for both input and output (PCM, 16 kHz, 16-bit mono).
 
QAudioInput * audio_input_
 Object managing audio input from the microphone.
 
QAudioOutput * audio_output_
 Object managing audio output to the speakers.
 
QString current_frequency_ = "-1.0"
 The frequency of the Wavelength currently displayed in this view. "-1.0" if inactive.
 
QString current_transmitter_id_
 Stores the ID of the user currently transmitting audio (if receiving).
 
QLabel * header_label_
 Label displaying the Wavelength frequency and name.
 
QIODevice * input_device_
 I/O device providing access to the raw audio stream from audio_input_.
 
QLineEdit * input_field_
 Input field for typing text messages.
 
bool is_aborting_ = false
 Flag indicating if the view is in the process of aborting the connection (prevents duplicate actions).
 
StreamDisplaymessage_area_
 Custom widget for displaying the stream of messages and audio visualization.
 
QIODevice * output_device_
 I/O device providing access to the raw audio stream for audio_output_.
 
QPushButton * ptt_button_
 Button for Push-to-Talk functionality.
 
QSoundEffect * ptt_off_sound_
 Sound effect played when the PTT button is released.
 
QSoundEffect * ptt_on_sound_
 Sound effect played when the PTT button is pressed.
 
PttState ptt_state_
 Current state of the Push-to-Talk interaction.
 
double scanline_opacity_
 Current opacity level for the scanline effect overlay.
 
QPushButton * send_button_
 Button to send the text message in the input field.
 
QLabel * status_indicator_
 Label indicating the connection status ("CONNECTION ACTIVE", "ABORTING", etc.).
 
TranslationManagertranslator_
 Pointer to the translation manager for handling UI translations.
 

Detailed Description

The main view for displaying and interacting with a specific Wavelength chat session.

This widget provides the user interface for a single chat, including:

  • Displaying incoming and outgoing messages (text and file transfers).
  • An input field for typing messages.
  • Buttons for sending messages, attaching files, and aborting the connection.
  • A Push-to-Talk (PTT) button for voice communication.
  • Visual indicators for connection status, current frequency/name, and who is transmitting audio.
  • Cyberpunk-themed styling with custom borders, scanlines, and audio visualization. It interacts with WavelengthSessionCoordinator and WavelengthMessageService for session management, message handling, and PTT functionality.

Member Enumeration Documentation

◆ PttState

enum ChatView::PttState
private

Enum representing the possible states of the Push-to-Talk (PTT) functionality.

Enumerator
Idle 

Not requesting, transmitting, or receiving PTT.

Requesting 

PTT button pressed, waiting for grant from the server.

Transmitting 

PTT granted, microphone is active, sending audio data.

Receiving 

Receiving audio data from another user.

Constructor & Destructor Documentation

◆ ChatView()

ChatView::ChatView ( QWidget * parent = nullptr)
explicit

Constructs a WavelengthChatView. Initializes the UI elements (labels, message area, input fields, buttons), sets up layouts, initializes audio components (input/output, format, sounds), connects signals/slots for user interaction and backend events, and starts timers for time display and random visual effects.

Parameters
parentOptional parent widget.
Here is the call graph for this function:

◆ ~ChatView()

ChatView::~ChatView ( )
override

Destructor. Stops audio input/output and cleans up audio resources.

Here is the call graph for this function:

Member Function Documentation

◆ AbortWavelength

void ChatView::AbortWavelength ( )
privateslot

Initiates the process of leaving or closing the current Wavelength. Updates the status indicator, calls the appropriate method on WavelengthSessionCoordinator (CloseWavelength if hosted, LeaveWavelength otherwise), emits wavelengthAborted(), and schedules the view to be cleared after a short delay.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AttachFile

void ChatView::AttachFile ( )
slot

Opens a file dialog to allow the user to select a file for sending. If a file is selected, initiates the file sending process via WavelengthMessageService and displays an initial progress message in the message area.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateAmplitude()

qreal ChatView::CalculateAmplitude ( const QByteArray & buffer) const
nodiscardprivate

Calculates the Root Mean Square (RMS) amplitude of a raw audio buffer. Used for audio visualization. Assumes 16-bit signed integer PCM data.

Parameters
bufferThe raw audio data.
Returns
The calculated RMS amplitude, normalized to approximately [0.0, 1.0].
Here is the caller graph for this function:

◆ Clear()

void ChatView::Clear ( )

Clears the view and resets its state. Resets the current frequency, clears the message area, header, and input field, and hides the view.

Here is the caller graph for this function:

◆ GetScanlineOpacity()

double ChatView::GetScanlineOpacity ( ) const
inlinenodiscard

Gets the current opacity of the scanline effect.

Returns
The scanline opacity value (0.0 to 1.0).

◆ InitializeAudio()

void ChatView::InitializeAudio ( )
private

Initializes the audio format and creates QAudioInput/QAudioOutput objects. Determines supported formats and sets buffer sizes.

Here is the caller graph for this function:

◆ OnAudioDataReceived

void ChatView::OnAudioDataReceived ( const QString & frequency,
const QByteArray & audio_data ) const
privateslot

Slot called when binary audio data is received from the server. If the frequency matches and the state is Received, writes the data to the audio output device, calculates the amplitude of the received audio, and updates the audio visualization in the message area.

Parameters
frequencyThe frequency the audio data belongs to.
audio_dataThe raw audio data chunk.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnMessageReceived()

void ChatView::OnMessageReceived ( const QString & frequency,
const QString & message )

Handles displaying a received message for the current Wavelength. Adds the message to the message_area_ if the frequency matches the current one. Triggers a visual activity effect.

Parameters
frequencyThe frequency the message was received on.
messageThe content of the message.
Here is the call graph for this function:

◆ OnMessageSent()

void ChatView::OnMessageSent ( const QString & frequency,
const QString & message ) const

Handles displaying a message sent by the local user on the current Wavelength. Adds the message to the message_area_ if the frequency matches the current one.

Parameters
frequencyThe frequency the message was sent on.
messageThe content of the message.

◆ OnPttButtonPressed

void ChatView::OnPttButtonPressed ( )
privateslot

Slot called when the PTT button is pressed. Plays the PTT activation sound, sets the state to Requesting, updates the button appearance, and sends a PTT request to the server via WavelengthMessageService.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPttButtonReleased

void ChatView::OnPttButtonReleased ( )
privateslot

Slot called when the PTT button is released. Plays the PTT deactivation sound. If transmitting, stops audio input and sends a PTT release message. Resets the PTT state to Idle and updates the button appearance.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPttDenied

void ChatView::OnPttDenied ( const QString & frequency,
const QString & reason )
privateslot

Slot called when the server denies the PTT request. If the frequency matches and the state is Requested, displays a system message with the reason, resets the PTT state to Idle, and updates the button appearance.

Parameters
frequencyThe frequency for which PTT was denied.
reasonThe reason for denial provided by the server.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPttGranted

void ChatView::OnPttGranted ( const QString & frequency)
privateslot

Slot called when the server grants the PTT request. If the frequency matches and the state is Requested, sets the state to Transmitting, updates the button appearance, starts audio input, and updates the message area to show the local user is transmitting.

Parameters
frequencyThe frequency for which PTT was granted.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPttStartReceiving

void ChatView::OnPttStartReceiving ( const QString & frequency,
const QString & sender_id )
privateslot

Slot called when another user starts transmitting PTT on the current Wavelength. If the state is Idle, sets the state to Receiving, updates the button appearance (disabling it), starts audio output, and updates the message area to show the remote sender's ID.

Parameters
frequencyThe frequency on which transmission started.
sender_idThe identifier of the user transmitting.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPttStopReceiving

void ChatView::OnPttStopReceiving ( const QString & frequency)
privateslot

Slot called when the remote user stops transmitting PTT. If the frequency matches and the state is Receiving, stops audio output, resets the PTT state to Idle, updates the button appearance, clears the transmitting user display in the message area, and resets the audio amplitude visualization.

Parameters
frequencyThe frequency on which transmission stopped.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnReadyReadInput

void ChatView::OnReadyReadInput ( ) const
privateslot

Slot called when the audio input device has data ready to be read. Reads all available audio data, sends it to the server via WavelengthMessageService, calculates the amplitude, and updates the local audio visualization in the message area.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnWavelengthClosed()

void ChatView::OnWavelengthClosed ( const QString & frequency)

Handles the event when the host closes the current Wavelength. Updates the status indicator, displays a system message, and schedules the view to be cleared, and the wavelengthAborted() signal to be emitted after a delay. Does nothing if the frequency doesn't match or if the local user initiated the closure.

Parameters
frequencyThe frequency of the closed Wavelength.
Here is the call graph for this function:

◆ paintEvent()

void ChatView::paintEvent ( QPaintEvent * event)
overrideprotected

Overridden paint event handler. Draws the custom cyberpunk border, AR markers, and scanline effect.

Parameters
eventThe paint event.

◆ ResetStatusIndicator()

void ChatView::ResetStatusIndicator ( ) const
private

Resets the status_indicator_ label to its default "CONNECTION ACTIVE" state and style.

Here is the caller graph for this function:

◆ SendMessage

void ChatView::SendMessage ( ) const
privateslot

Sends the text message currently in the input field. Clears the input field and sends the message via WavelengthMessageService.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetScanlineOpacity()

void ChatView::SetScanlineOpacity ( double opacity)

Sets the opacity of the scanline effect. Triggers a repaint of the widget.

Parameters
opacityThe desired opacity (0.0 to 1.0).

◆ SetWavelength()

void ChatView::SetWavelength ( const QString & frequency,
const QString & name = QString() )

Configures the view for a specific Wavelength. Sets the header title with the frequency and optional name, clears the message area, displays a connection message, resets PTT state, enables controls, triggers a connection visual effect, and makes the view visible.

Parameters
frequencyThe frequency of the Wavelength.
nameOptional name associated with the Wavelength.
Here is the call graph for this function:

◆ StartAudioInput()

void ChatView::StartAudioInput ( )
private

Starts capturing audio from the microphone if PTT state is Transmitting. Connects the readyRead signal to OnReadyReadInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StartAudioOutput()

void ChatView::StartAudioOutput ( )
private

Starts audio playback if PTT state is Receiving. Prepares the audio output device to receive data.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StopAudioInput()

void ChatView::StopAudioInput ( )
private

Stops capturing audio from the microphone. Disconnects signals and resets the input device pointer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StopAudioOutput()

void ChatView::StopAudioOutput ( )
private

Stops audio playback. Resets the output device pointer.

Here is the caller graph for this function:

◆ TriggerActivityEffect

void ChatView::TriggerActivityEffect ( )
privateslot

Triggers a subtle scanline effect animation when there is activity (e.g., a message received).

Here is the caller graph for this function:

◆ TriggerConnectionEffect

void ChatView::TriggerConnectionEffect ( )
privateslot

Triggers a more pronounced scanline effect animation when connecting to a Wavelength.

Here is the caller graph for this function:

◆ TriggerVisualEffect

void ChatView::TriggerVisualEffect ( )
privateslot

Triggers a random, brief increase in the scanline effect opacity.

Here is the caller graph for this function:

◆ UpdateProgressMessage

void ChatView::UpdateProgressMessage ( const QString & message_id,
const QString & message ) const
privateslot

Slot to update a message in the message area, typically used for file transfer progress. Finds the message by its ID and updates its content.

Parameters
message_idThe unique identifier of the message to update.
messageThe new content for the message.
Here is the caller graph for this function:

◆ UpdatePttButtonState()

void ChatView::UpdatePttButtonState ( ) const
private

Updates the appearance and enabled state of the PTT button based on the current ptt_state_. Also enables/disables other input controls (text field, send/attach buttons) accordingly.

Here is the caller graph for this function:

◆ wavelengthAborted

void ChatView::wavelengthAborted ( )
signal

Emitted when the user aborts the connection or the Wavelength is closed by the host. Signals the parent view to switch back.

Here is the caller graph for this function:

Member Data Documentation

◆ abort_button_

QPushButton* ChatView::abort_button_
private

Button to disconnect from the current Wavelength.

◆ attach_button_

QPushButton* ChatView::attach_button_
private

Button to open the file attachment dialog.

◆ audio_format_

QAudioFormat ChatView::audio_format_
private

The audio format used for both input and output (PCM, 16 kHz, 16-bit mono).

◆ audio_input_

QAudioInput* ChatView::audio_input_
private

Object managing audio input from the microphone.

◆ audio_output_

QAudioOutput* ChatView::audio_output_
private

Object managing audio output to the speakers.

◆ current_frequency_

QString ChatView::current_frequency_ = "-1.0"
private

The frequency of the Wavelength currently displayed in this view. "-1.0" if inactive.

◆ current_transmitter_id_

QString ChatView::current_transmitter_id_
private

Stores the ID of the user currently transmitting audio (if receiving).

◆ header_label_

QLabel* ChatView::header_label_
private

Label displaying the Wavelength frequency and name.

◆ input_device_

QIODevice* ChatView::input_device_
private

I/O device providing access to the raw audio stream from audio_input_.

◆ input_field_

QLineEdit* ChatView::input_field_
private

Input field for typing text messages.

◆ is_aborting_

bool ChatView::is_aborting_ = false
private

Flag indicating if the view is in the process of aborting the connection (prevents duplicate actions).

◆ message_area_

StreamDisplay* ChatView::message_area_
private

Custom widget for displaying the stream of messages and audio visualization.

◆ output_device_

QIODevice* ChatView::output_device_
private

I/O device providing access to the raw audio stream for audio_output_.

◆ ptt_button_

QPushButton* ChatView::ptt_button_
private

Button for Push-to-Talk functionality.

◆ ptt_off_sound_

QSoundEffect* ChatView::ptt_off_sound_
private

Sound effect played when the PTT button is released.

◆ ptt_on_sound_

QSoundEffect* ChatView::ptt_on_sound_
private

Sound effect played when the PTT button is pressed.

◆ ptt_state_

PttState ChatView::ptt_state_
private

Current state of the Push-to-Talk interaction.

◆ scanline_opacity_

double ChatView::scanline_opacity_
private

Current opacity level for the scanline effect overlay.

◆ send_button_

QPushButton* ChatView::send_button_
private

Button to send the text message in the input field.

◆ status_indicator_

QLabel* ChatView::status_indicator_
private

Label indicating the connection status ("CONNECTION ACTIVE", "ABORTING", etc.).

◆ translator_

TranslationManager* ChatView::translator_
private

Pointer to the translation manager for handling UI translations.

Property Documentation

◆ scanlineOpacity

double ChatView::scanlineOpacity
readwrite

Property controlling the opacity of the animated scanline effect (0.0 to 1.0). Animatable.


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