2019-11-10 02:37:54 +01:00
# include "settingstabgeneral.h"
SettingsTabGeneral : : SettingsTabGeneral ( json _data , SoundPlayback * soundPlayback ) : SettingsTab ( " general " , _data )
{
this - > soundPlayback = soundPlayback ;
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
layout - > setMargin ( 0 ) ;
QGridLayout * general_layout = new QGridLayout ( ) ;
layout - > addLayout ( general_layout ) ;
int row = 0 ;
general_layout - > addWidget ( new QLabel ( tr ( " Language: " ) ) , row , 0 ) ;
languageSelection = new QComboBox ( ) ;
// TODO
languageSelection - > setDisabled ( true ) ;
languageSelection - > addItem ( QLocale : : languageToString ( QLocale ( " en-US " ) . language ( ) ) ) ;
languageSelection - > addItem ( QLocale : : languageToString ( QLocale ( " de-DE " ) . language ( ) ) ) ;
general_layout - > addWidget ( languageSelection , row , 1 ) ;
row + + ;
general_layout - > addWidget ( new QLabel ( tr ( " Stop hotkey: " ) ) , row , 0 ) ;
stopHotkey = new CustomKeySequenceEdit ( this ) ;
general_layout - > addWidget ( stopHotkey , row , 1 ) ;
2019-11-10 15:22:15 +01:00
row + + ;
general_layout - > addWidget ( new QLabel ( tr ( " Theme: " ) ) , row , 0 ) ;
themeSelection = new QComboBox ( ) ;
themeSelection - > addItem ( " System " ) ;
themeSelection - > addItem ( " Dark " ) ;
general_layout - > addWidget ( themeSelection , row , 1 ) ;
reset ( ) ;
}
void SettingsTabGeneral : : reset ( )
{
2019-11-10 02:37:54 +01:00
if ( this - > data ! = nullptr ) {
if ( this - > data . contains ( " language " ) ) {
languageSelection - > setCurrentText ( QString : : fromStdString ( this - > data [ " language " ] . get < std : : string > ( ) ) ) ;
}
if ( this - > data . contains ( " stopHotkey " ) ) {
stopHotkey - > setKeySequence ( QKeySequence ( QString : : fromStdString ( this - > data [ " stopHotkey " ] . get < std : : string > ( ) ) ) ) ;
updateStopHotkey ( ) ;
}
2019-11-10 15:22:15 +01:00
if ( this - > data . contains ( " theme " ) ) {
themeSelection - > setCurrentText ( QString : : fromStdString ( this - > data [ " theme " ] . get < std : : string > ( ) ) ) ;
updateTheme ( ) ;
}
2019-11-10 02:37:54 +01:00
}
}
void SettingsTabGeneral : : updateStopHotkey ( )
{
if ( hotkeyStop ! = nullptr ) {
delete hotkeyStop ;
hotkeyStop = nullptr ;
}
if ( ! stopHotkey | | stopHotkey - > keySequence ( ) . isEmpty ( ) ) {
return ;
}
hotkeyStop = new QHotkey ( stopHotkey - > keySequence ( ) , true , this ) ;
if ( hotkeyStop - > isRegistered ( ) )
{
connect ( hotkeyStop , & QHotkey : : activated , this , [ = ] ( ) {
soundPlayback - > stopSound ( ) ;
} ) ;
} else {
QMessageBox : : warning ( this , " Could not register " + stopHotkey - > keySequence ( ) . toString ( ) , " Either the key combination is not valid or it's not possible to use this combination (Maybe another program is using it) " , QMessageBox : : Ok ) ;
}
}
2019-11-10 15:22:15 +01:00
void SettingsTabGeneral : : updateTheme ( )
{
if ( themeSelection - > currentIndex ( ) = = 0 ) {
qApp - > setPalette ( qApp - > style ( ) - > standardPalette ( ) ) ;
qApp - > setStyleSheet ( QString ( ) ) ;
} else if ( themeSelection - > currentIndex ( ) = = 1 ) {
QFile f ( " :qdarkstyle/style.qss " ) ;
if ( ! f . exists ( ) )
{
printf ( " Unable to set stylesheet, file not found \n " ) ;
}
else
{
f . open ( QFile : : ReadOnly | QFile : : Text ) ;
QTextStream ts ( & f ) ;
qApp - > setStyleSheet ( ts . readAll ( ) ) ;
}
}
}
2019-11-10 02:37:54 +01:00
json SettingsTabGeneral : : tabSettings ( )
{
json j ;
j [ " language " ] = languageSelection - > currentText ( ) . toStdString ( ) ;
j [ " stopHotkey " ] = stopHotkey - > keySequence ( ) . toString ( ) . toStdString ( ) ;
2019-11-10 15:22:15 +01:00
j [ " theme " ] = themeSelection - > currentText ( ) . toStdString ( ) ;
2019-11-10 02:37:54 +01:00
updateStopHotkey ( ) ;
2019-11-10 15:22:15 +01:00
updateTheme ( ) ;
2019-11-10 02:37:54 +01:00
this - > data = j ;
return j ;
}