2022-01-09 06:42:13 +01:00
#include "soundpropertiesdialog.h"
#include "ui_soundpropertiesdialog.h"
#include <QFileDialog>
2022-01-09 21:07:41 +01:00
#include <QFile>
2022-01-09 06:42:13 +01:00
#include <QDebug>
#include <QMessageBox>
#include <QFileInfo>
#include "projectcontainer.h"
2022-01-09 23:05:01 +01:00
#include "projecttreemodel.h"
2022-01-09 06:42:13 +01:00
2022-01-09 23:05:01 +01:00
SoundPropertiesDialog :: SoundPropertiesDialog ( Sound & sound , ProjectTreeModel & projectModel , QWidget * parent ) :
2022-01-09 06:42:13 +01:00
QDialog { parent },
m_ui { std :: make_unique < Ui :: SoundPropertiesDialog > ()},
2022-01-09 23:05:01 +01:00
m_sound { sound },
2022-01-10 21:49:06 +01:00
m_projectModel { projectModel },
m_path { m_sound . path }
2022-01-09 06:42:13 +01:00
{
m_ui -> setupUi ( this );
2022-01-09 23:05:01 +01:00
updateTitle ();
if ( auto button = m_ui -> buttonBox -> button ( QDialogButtonBox :: Ok ))
button -> setIcon ( QIcon { ":/qtgameengine/icons/ok.png" });
if ( auto button = m_ui -> buttonBox -> button ( QDialogButtonBox :: Cancel ))
button -> setIcon ( QIcon { ":/qtgameengine/icons/delete.png" });
2022-01-09 06:42:13 +01:00
m_ui -> lineEditName -> setText ( m_sound . name );
2022-01-09 21:07:41 +01:00
if ( ! m_sound . path . isEmpty ())
{
m_ui -> labelFilename -> setText ( tr ( "Filename: %0" ). arg ( QFileInfo { m_sound . path }. fileName ()));
m_soundEffect . setSource ( QUrl :: fromLocalFile ( m_sound . path ));
}
2022-01-09 06:42:13 +01:00
m_ui -> radioButtonNormal -> setChecked ( m_sound . type == Sound :: Type :: Sound );
m_ui -> radioButtonMusic -> setChecked ( m_sound . type == Sound :: Type :: Music );
2022-01-10 21:49:06 +01:00
m_ui -> radioButton3D -> setChecked ( false );
m_ui -> radioButtonMultimedia -> setChecked ( false );
2022-01-09 06:42:13 +01:00
m_ui -> checkBoxChorus -> setChecked ( m_sound . effects . chorus );
m_ui -> checkBoxFlanger -> setChecked ( m_sound . effects . flanger );
m_ui -> checkBoxGargle -> setChecked ( m_sound . effects . gargle );
m_ui -> checkBoxEcho -> setChecked ( m_sound . effects . echo );
m_ui -> checkBoxReverb -> setChecked ( m_sound . effects . reverb );
m_ui -> horizontalSliderVolume -> setValue ( m_sound . volume );
m_ui -> horizontalSliderPan -> setValue ( m_sound . pan );
m_ui -> checkBoxPreload -> setChecked ( m_sound . preload );
2022-01-09 23:05:01 +01:00
connect ( & m_projectModel , & ProjectTreeModel :: soundNameChanged ,
this , & SoundPropertiesDialog :: soundNameChanged );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonLoad , & QAbstractButton :: clicked ,
2022-01-09 06:42:13 +01:00
this , & SoundPropertiesDialog :: loadSound );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonPlay , & QAbstractButton :: clicked ,
2022-01-09 06:42:13 +01:00
this , & SoundPropertiesDialog :: playSound );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonStop , & QAbstractButton :: clicked ,
2022-01-09 06:42:13 +01:00
this , & SoundPropertiesDialog :: stopSound );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonSave , & QAbstractButton :: clicked ,
2022-01-09 06:42:13 +01:00
this , & SoundPropertiesDialog :: saveSound );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonEdit , & QAbstractButton :: clicked ,
2022-01-09 06:42:13 +01:00
this , & SoundPropertiesDialog :: editSound );
connect ( m_ui -> lineEditName , & QLineEdit :: textChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> radioButtonNormal , & QRadioButton :: toggled ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> radioButtonMusic , & QRadioButton :: toggled ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> radioButton3D , & QRadioButton :: toggled ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> radioButtonMultimedia , & QRadioButton :: toggled ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxChorus , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxFlanger , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxGargle , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxEcho , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxReverb , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> horizontalSliderVolume , & QSlider :: valueChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> horizontalSliderPan , & QSlider :: valueChanged ,
this , & SoundPropertiesDialog :: changed );
connect ( m_ui -> checkBoxPreload , & QCheckBox :: stateChanged ,
this , & SoundPropertiesDialog :: changed );
}
SoundPropertiesDialog ::~ SoundPropertiesDialog () = default ;
void SoundPropertiesDialog :: accept ()
{
2022-01-10 21:49:06 +01:00
if ( ! m_unsavedChanges )
{
QDialog :: reject ();
return ;
}
2022-01-09 06:42:13 +01:00
if ( m_sound . name != m_ui -> lineEditName -> text ())
{
2022-01-10 01:43:41 +01:00
if ( ! m_projectModel . rename < Sound > ( m_sound , m_ui -> lineEditName -> text ()))
2022-01-09 23:05:01 +01:00
{
QMessageBox :: critical ( this , tr ( "Renaming Sound failed!" ), tr ( "Renaming Sound failed!" ));
return ;
}
2022-01-09 06:42:13 +01:00
}
2022-01-10 21:49:06 +01:00
m_sound . path = std :: move ( m_path );
2022-01-09 21:07:41 +01:00
2022-01-09 06:42:13 +01:00
if ( m_ui -> radioButtonNormal -> isChecked ())
m_sound . type = Sound :: Type :: Sound ;
else if ( m_ui -> radioButtonMusic -> isChecked ())
m_sound . type = Sound :: Type :: Music ;
2022-01-10 21:49:06 +01:00
else if ( m_ui -> radioButton3D -> isChecked () ||
m_ui -> radioButtonMultimedia -> isChecked ())
2022-01-09 06:42:13 +01:00
{
QMessageBox :: critical ( this , tr ( "Not implemented" ), tr ( "This kind of sound is not yet supported!" ));
return ;
}
2022-01-10 21:49:06 +01:00
else
{
QMessageBox :: critical ( this , tr ( "No kind selected!" ), tr ( "No kind selected!" ));
return ;
}
2022-01-09 06:42:13 +01:00
m_sound . effects . chorus = m_ui -> checkBoxChorus -> isChecked ();
m_sound . effects . flanger = m_ui -> checkBoxFlanger -> isChecked ();
m_sound . effects . gargle = m_ui -> checkBoxGargle -> isChecked ();
m_sound . effects . echo = m_ui -> checkBoxEcho -> isChecked ();
m_sound . effects . reverb = m_ui -> checkBoxReverb -> isChecked ();
m_sound . volume = m_ui -> horizontalSliderVolume -> value ();
m_sound . pan = m_ui -> horizontalSliderPan -> value ();
QDialog :: accept ();
}
void SoundPropertiesDialog :: reject ()
{
if ( ! m_unsavedChanges )
{
QDialog :: reject ();
return ;
}
const auto result = QMessageBox :: warning (
this ,
tr ( "The Sound has been modified." ),
tr ( "Do you want to save your changes?" ),
QMessageBox :: Save | QMessageBox :: Discard | QMessageBox :: Cancel ,
QMessageBox :: Save
);
switch ( result )
{
case QMessageBox :: Save :
accept ();
return ;
case QMessageBox :: Discard :
QDialog :: reject ();
return ;
case QMessageBox :: Cancel :
return ;
default :
qWarning () << "unexpected dialog result" << result ;
}
}
void SoundPropertiesDialog :: loadSound ()
{
2022-01-10 21:49:06 +01:00
auto path = QFileDialog :: getOpenFileName ( this , tr ( "Open a Sound File..." ));
2022-01-09 21:07:41 +01:00
if ( path . isEmpty ())
return ;
if ( QFile file { path }; ! file . open ( QIODevice :: ReadOnly ))
{
QMessageBox :: warning ( this , tr ( "Could not load Sound!" ), tr ( "Could not load Sound!" ) + " \n\n " + file . errorString ());
return ;
}
2022-01-10 21:49:06 +01:00
m_path = std :: move ( path );
2022-01-09 21:07:41 +01:00
changed ();
2022-01-10 21:49:06 +01:00
m_ui -> labelFilename -> setText ( tr ( "Filename: %0" ). arg ( QFileInfo { m_path }. fileName ()));
m_soundEffect . setSource ( QUrl :: fromLocalFile ( m_path ));
2022-01-09 21:07:41 +01:00
}
void SoundPropertiesDialog :: saveSound ()
{
2022-01-10 21:49:06 +01:00
if ( m_path . isEmpty ())
2022-01-09 21:07:41 +01:00
{
QMessageBox :: warning ( this , tr ( "Could not save Sound!" ), tr ( "Could not save Sound!" ) + " \n\n " + tr ( "No sound has been selected yet." ));
return ;
}
const auto savePath = QFileDialog :: getSaveFileName ( this , tr ( "Save a Sound File..." ), m_sound . name + ".wav" , tr ( "WAV Files (*.wav)" ));
if ( savePath . isEmpty ())
return ;
2022-01-10 21:49:06 +01:00
if ( ! QFile :: copy ( m_path , savePath ))
2022-01-09 21:07:41 +01:00
{
QMessageBox :: warning ( this , tr ( "Could not save Sound!" ), tr ( "Could not save Sound!" ));
return ;
}
2022-01-09 06:42:13 +01:00
}
void SoundPropertiesDialog :: playSound ()
{
m_soundEffect . play ();
}
void SoundPropertiesDialog :: stopSound ()
{
m_soundEffect . stop ();
}
void SoundPropertiesDialog :: editSound ()
{
QMessageBox :: critical ( this , tr ( "Setup not complete" ), tr ( "No valid external editor has been indicated for this type of sound. You can specify this editor in the Preferences." ));
}
void SoundPropertiesDialog :: changed ()
{
if ( ! m_unsavedChanges )
{
m_unsavedChanges = true ;
2022-01-09 23:05:01 +01:00
updateTitle ();
2022-01-09 06:42:13 +01:00
}
}
2022-01-09 23:05:01 +01:00
void SoundPropertiesDialog :: soundNameChanged ( const Sound & sound )
{
if ( & sound != & m_sound )
return ;
{
QSignalBlocker blocker { m_ui -> lineEditName };
m_ui -> lineEditName -> setText ( sound . name );
}
updateTitle ();
}
void SoundPropertiesDialog :: updateTitle ()
{
setWindowTitle ( tr ( "Sound Properties: %0%1" )
. arg ( m_sound . name )
. arg ( m_unsavedChanges ? tr ( "*" ) : QString {})
);
}