- multiple bundled audio + custom audio selection
This commit is contained in:
parent
e157c638ad
commit
526f5852c3
19
app/audio_support.cpp
Normal file
19
app/audio_support.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "audio_support.h"
|
||||
|
||||
#include <QSound>
|
||||
|
||||
extern void play_audio(const app_settings::selected_audio& item)
|
||||
{
|
||||
// Play audio
|
||||
if (item.name != Audio_Empty && item.name != Audio_Custom)
|
||||
{
|
||||
// Find bundled audio
|
||||
auto iter = AudioMap.find(item.name);
|
||||
if (iter != AudioMap.end())
|
||||
QSound::play(iter->second);
|
||||
}
|
||||
else
|
||||
if (item.name == Audio_Custom && !item.path.isEmpty())
|
||||
QSound::play(item.path);
|
||||
}
|
||||
|
||||
8
app/audio_support.h
Normal file
8
app/audio_support.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef __AUDIO_SUPPORT_H
|
||||
#define __AUDIO_SUPPORT_H
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
extern void play_audio(const app_settings::selected_audio& item);
|
||||
|
||||
#endif
|
||||
@ -5,6 +5,7 @@
|
||||
#include "autostart.h"
|
||||
#include "aboutdlg.h"
|
||||
#include "config.h"
|
||||
#include "audio_support.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QAction>
|
||||
@ -318,17 +319,8 @@ void MainWindow::onLongBreakEnd()
|
||||
// Refresh UI
|
||||
onUpdateUI();
|
||||
|
||||
// Play audio
|
||||
if (mAppConfig.play_audio != Audio_Empty && mAppConfig.play_audio != Audio_Custom)
|
||||
{
|
||||
auto iter = AudioMap.find(mAppConfig.play_audio);
|
||||
if (iter != AudioMap.end())
|
||||
QSound::play(iter->second);
|
||||
}
|
||||
else
|
||||
if (mAppConfig.play_audio == Audio_Custom)
|
||||
QSound::play(mAppConfig.play_audio_custom);
|
||||
|
||||
// Play selecged audio
|
||||
play_audio(mAppConfig.play_audio);
|
||||
|
||||
// Run script
|
||||
if (!mAppConfig.script_on_break_finish.isEmpty())
|
||||
|
||||
@ -15,7 +15,9 @@ SOURCES += \
|
||||
mainwindow.cpp \
|
||||
settings.cpp \
|
||||
settingsdialog.cpp \
|
||||
runguard.cpp
|
||||
runguard.cpp \
|
||||
audio_support.cpp
|
||||
|
||||
|
||||
HEADERS += \
|
||||
aboutdlg.h \
|
||||
@ -24,7 +26,9 @@ HEADERS += \
|
||||
mainwindow.h \
|
||||
settings.h \
|
||||
settingsdialog.h \
|
||||
runguard.h
|
||||
runguard.h \
|
||||
audio_support.h
|
||||
|
||||
|
||||
FORMS += \
|
||||
aboutdlg.ui \
|
||||
|
||||
@ -10,8 +10,8 @@ const QString Key_Window_On_Top = "Window_On_Top";
|
||||
const QString Key_Verbose = "Verbose";
|
||||
const QString Key_Autostart = "Autostart";
|
||||
const QString Key_PreferredMonitor = "Preferred_Monitor";
|
||||
const QString Key_Audio = "Audio";
|
||||
const QString Key_Audio_Custom = "Audio_Custom";
|
||||
const QString Key_Audio_Name = "Audio_Name";
|
||||
const QString Key_Audio_Path = "Audio_Path";
|
||||
const QString Key_Script = "Script";
|
||||
|
||||
void app_settings::save(const config &cfg)
|
||||
@ -25,8 +25,8 @@ void app_settings::save(const config &cfg)
|
||||
s.setValue(Key_Verbose, cfg.verbose);
|
||||
s.setValue(Key_Autostart, cfg.autostart);
|
||||
s.setValue(Key_PreferredMonitor, cfg.preferred_monitor);
|
||||
s.setValue(Key_Audio, cfg.play_audio);
|
||||
s.setValue(Key_Audio_Custom, cfg.play_audio_custom);
|
||||
s.setValue(Key_Audio_Name, cfg.play_audio.name);
|
||||
s.setValue(Key_Audio_Path, cfg.play_audio.path);
|
||||
s.setValue(Key_Script, cfg.script_on_break_finish);
|
||||
}
|
||||
|
||||
@ -42,8 +42,8 @@ app_settings::config app_settings::load()
|
||||
r.verbose = s.value(Key_Verbose, Default_Verbose).toBool();
|
||||
r.autostart = s.value(Key_Autostart, Default_Autostart).toBool();
|
||||
r.preferred_monitor = s.value(Key_PreferredMonitor, Default_Monitor).toString();
|
||||
r.play_audio = s.value(Key_Audio, Audio_Empty).toString();
|
||||
r.play_audio_custom = s.value(Key_Audio_Custom, Audio_Custom).toString();
|
||||
r.play_audio.name = s.value(Key_Audio_Name, Audio_Empty).toString();
|
||||
r.play_audio.path = s.value(Key_Audio_Path, QString()).toString();
|
||||
r.script_on_break_finish = s.value(Key_Script, QString()).toString();
|
||||
|
||||
return r;
|
||||
|
||||
@ -25,7 +25,7 @@ const QString Primary_Monitor = "[Primary]";
|
||||
// Default behavior is not play any audio on break end.
|
||||
const QString Audio_Empty = "None";
|
||||
const QString Audio_Default_1 = "Default 1";
|
||||
const QString Audio_Default_2 = "Default_2";
|
||||
const QString Audio_Default_2 = "Default 2";
|
||||
const QString Audio_Custom = "Custom...";
|
||||
|
||||
struct audio_item
|
||||
@ -47,6 +47,12 @@ const QString AppName = "QBreak";
|
||||
class app_settings
|
||||
{
|
||||
public:
|
||||
struct selected_audio
|
||||
{
|
||||
QString name = Audio_Empty;
|
||||
QString path;
|
||||
};
|
||||
|
||||
struct config
|
||||
{
|
||||
// Seconds
|
||||
@ -59,8 +65,7 @@ public:
|
||||
QString preferred_monitor = Default_Monitor;
|
||||
|
||||
// This value can be path to audio file or empty or [embedded] string
|
||||
QString play_audio = Audio_Empty;
|
||||
QString play_audio_custom;
|
||||
selected_audio play_audio;
|
||||
QString script_on_break_finish;
|
||||
};
|
||||
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
#include "settingsdialog.h"
|
||||
#include "ui_settingsdialog.h"
|
||||
#include "settings.h"
|
||||
#include "audio_support.h"
|
||||
|
||||
#include <QToolTip>
|
||||
#include <QTimer>
|
||||
#include <QWindow>
|
||||
#include <QScreen>
|
||||
#include <QFileDialog>
|
||||
#include <QStandardPaths>
|
||||
|
||||
const QString ConversionError = "Integer value expected.";
|
||||
|
||||
@ -25,7 +27,7 @@ SettingsDialog::~SettingsDialog()
|
||||
|
||||
void SettingsDialog::init()
|
||||
{
|
||||
mDontRunEvents = true;
|
||||
mSkipAudioChangeEvent = true;
|
||||
|
||||
setWindowTitle("Settings");
|
||||
|
||||
@ -59,15 +61,15 @@ void SettingsDialog::init()
|
||||
|
||||
// Preselect active audio
|
||||
for (int i = 0; i < ui->mAudioComboBox->count(); i++)
|
||||
if (ui->mAudioComboBox->itemText(i) == c.play_audio)
|
||||
if (ui->mAudioComboBox->itemText(i) == c.play_audio.name)
|
||||
ui->mAudioComboBox->setCurrentIndex(i);
|
||||
|
||||
mCustomAudioPath = c.play_audio_custom;
|
||||
mCustomAudioPath = c.play_audio.path;
|
||||
|
||||
ui->mScriptEdit->setText(c.script_on_break_finish);
|
||||
connect(ui->mAudioComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onAudioIndexChanged(int)));
|
||||
|
||||
mDontRunEvents = false;
|
||||
mSkipAudioChangeEvent = false;
|
||||
}
|
||||
|
||||
void SettingsDialog::accept()
|
||||
@ -81,9 +83,9 @@ void SettingsDialog::accept()
|
||||
c.longbreak_postpone_interval = ui->mPostponeTimeEdit->text().toInt() * 60;
|
||||
c.preferred_monitor = ui->mPreferredMonitorCombobox->currentData().toString();
|
||||
c.script_on_break_finish = ui->mScriptEdit->text();
|
||||
c.play_audio = ui->mAudioComboBox->currentText();
|
||||
if (c.play_audio == Audio_Custom)
|
||||
c.play_audio_custom = mCustomAudioPath;
|
||||
c.play_audio.name = ui->mAudioComboBox->currentText();
|
||||
if (c.play_audio.name == Audio_Custom)
|
||||
c.play_audio.path = mCustomAudioPath;
|
||||
|
||||
app_settings::save(c);
|
||||
|
||||
@ -92,10 +94,14 @@ void SettingsDialog::accept()
|
||||
|
||||
void SettingsDialog::onAudioIndexChanged(int idx)
|
||||
{
|
||||
if (mSkipAudioChangeEvent)
|
||||
return;
|
||||
|
||||
if (idx == mCustomAudioIdx)
|
||||
{
|
||||
// Ask about path to audio file
|
||||
auto path = QFileDialog::getOpenFileName(this, tr("Select audio file"), QString(), ".wav;*.mp3;*.ogg");
|
||||
auto home = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
||||
auto path = QFileDialog::getOpenFileName(this, tr("Select audio file"), home, tr("Sound files(*.wav *.mp3 *.ogg)"));
|
||||
if (!path.isEmpty())
|
||||
{
|
||||
mCustomAudioPath = path;
|
||||
@ -106,4 +112,8 @@ void SettingsDialog::onAudioIndexChanged(int idx)
|
||||
// ToDo: show message "audio is not selected"
|
||||
}
|
||||
}
|
||||
|
||||
// Play selected audio to ensure this is fine
|
||||
play_audio({.name = ui->mAudioComboBox->itemText(idx), .path = mCustomAudioPath});
|
||||
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ private:
|
||||
Ui::SettingsDialog *ui;
|
||||
QString mCustomAudioPath;
|
||||
int mCustomAudioIdx;
|
||||
bool mDontRunEvents;
|
||||
bool mSkipAudioChangeEvent;
|
||||
|
||||
void init();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user