1+ #include " tts/config_window.hh"
2+ #include " tts/services/azure.hh"
3+ #include " tts/services/dummy.hh"
4+ #include " tts/services/local_command.hh"
5+ #include " tts/config_file_main.hh"
6+
7+ #include < QDialogButtonBox>
8+ #include < QGridLayout>
9+ #include < QGroupBox>
10+ #include < QLabel>
11+ #include < QPushButton>
12+ #include < QLineEdit>
13+
14+ #include < QStringLiteral>
15+
16+ namespace TTS {
17+
18+ // TODO: split preview pane to a seprate file.
19+ void ConfigWindow::setupUi ()
20+ {
21+ setWindowTitle ( " Service Config" );
22+ this ->setAttribute ( Qt::WA_DeleteOnClose );
23+ this ->setWindowModality ( Qt::WindowModal );
24+ this ->setWindowFlag ( Qt::Dialog );
25+
26+ MainLayout = new QGridLayout ( this );
27+
28+ configPane = new QGroupBox ( " Service Config" , this );
29+ auto * previewPane = new QGroupBox ( " Audio Preview" , this );
30+
31+ configPane->setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
32+ previewPane->setSizePolicy ( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
33+
34+ configPane->setLayout ( new QVBoxLayout () );
35+ previewPane->setLayout ( new QVBoxLayout () );
36+
37+ auto * serviceSelectLayout = new QHBoxLayout ( nullptr );
38+ auto * serviceLabel = new QLabel ( " Select service" , this );
39+ serviceSelector = new QComboBox ();
40+ serviceSelector->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Maximum );
41+
42+ serviceSelectLayout->addWidget ( serviceLabel );
43+ serviceSelectLayout->addWidget ( serviceSelector );
44+
45+ previewLineEdit = new QLineEdit ( this );
46+ previewButton = new QPushButton ( " Preview" , this );
47+
48+ previewPane->layout ()->addWidget ( previewLineEdit );
49+ previewPane->layout ()->addWidget ( previewButton );
50+ qobject_cast< QVBoxLayout * >( previewPane->layout () )->addStretch ();
51+
52+ buttonBox = new QDialogButtonBox ( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, this );
53+ MainLayout->addLayout ( serviceSelectLayout, 0 , 0 , 1 , 2 );
54+ MainLayout->addWidget ( configPane, 1 , 0 , 1 , 1 );
55+ MainLayout->addWidget ( previewPane, 1 , 1 , 1 , 1 );
56+ MainLayout->addWidget ( buttonBox, 2 , 0 , 1 , 2 );
57+ MainLayout->addWidget (
58+ new QLabel (
59+ R"( <font color="red">Experimental feature. The default API key may stop working at anytime. Feedback & Coding help are welcomed. </font>)" ,
60+ this ),
61+ 3 ,
62+ 0 ,
63+ 1 ,
64+ 2 );
65+ }
66+
67+ ConfigWindow::ConfigWindow ( QWidget * parent, const QString & configRootPath ):
68+ QWidget ( parent, Qt::Window ),
69+ configRootDir ( configRootPath )
70+ {
71+ configRootDir.mkpath ( QStringLiteral ( " ctts" ) );
72+ configRootDir.cd ( QStringLiteral ( " ctts" ) );
73+
74+
75+ this ->setupUi ();
76+
77+ serviceSelector->addItem ( " Azure Text to Speech" , QStringLiteral ( " azure" ) );
78+ serviceSelector->addItem ( " Local Command Line" , QStringLiteral ( " local_command" ) );
79+ serviceSelector->addItem ( " Dummy" , QStringLiteral ( " dummy" ) );
80+
81+
82+ this ->currentService = get_service_name_from_path ( configRootDir );
83+
84+ if ( auto i = serviceSelector->findData ( this ->currentService ); i != -1 ) {
85+ serviceSelector->setCurrentIndex ( i );
86+ }
87+
88+
89+ connect ( previewButton, &QPushButton::clicked, this , [ this ] {
90+ this ->serviceConfigUI ->save ();
91+
92+
93+ if ( currentService == " azure" ) {
94+ previewService.reset ( TTS::AzureService::Construct ( this ->configRootDir ) );
95+ }
96+ else if ( currentService == " local_command" ) {
97+ auto * s = new TTS::LocalCommandService ( this ->configRootDir );
98+ s->loadCommandFromConfigFile (); // TODO:: error unhandled.
99+ previewService.reset ( s );
100+ }
101+ else {
102+ previewService.reset ( new TTS::DummyService () );
103+ }
104+
105+ if ( previewService != nullptr ) {
106+ previewService->speak ( previewLineEdit->text ().toUtf8 () );
107+ }
108+ else {
109+ exit ( 1 ); // TODO
110+ }
111+ } );
112+
113+
114+ updateConfigPaneBasedOnCurrentService ();
115+
116+ connect ( serviceSelector, &QComboBox::currentIndexChanged, this , [ this ] {
117+ updateConfigPaneBasedOnCurrentService ();
118+ } );
119+
120+ connect ( buttonBox, &QDialogButtonBox::accepted, this , [ this ]() {
121+ qDebug () << " accept" ;
122+ this ->serviceConfigUI ->save ();
123+ save_service_name_to_path ( configRootDir, this ->serviceSelector ->currentData ().toByteArray () );
124+
125+ emit this ->service_changed ();
126+ this ->close ();
127+ } );
128+
129+ connect ( buttonBox, &QDialogButtonBox::rejected, this , [ this ]() {
130+ qDebug () << " rejected" ;
131+ this ->close ();
132+ } );
133+
134+ connect ( buttonBox->button ( QDialogButtonBox::Help ), &QPushButton::clicked, this , [ this ]() {
135+ qDebug () << " help" ;
136+ } );
137+ }
138+
139+
140+ void ConfigWindow::updateConfigPaneBasedOnCurrentService ()
141+ {
142+ if ( serviceSelector->currentData () == " azure" ) {
143+ serviceConfigUI.reset ( new TTS::AzureConfigWidget ( this , this ->configRootDir ) );
144+ }
145+ else if ( serviceSelector->currentData () == " local_command" ) {
146+ serviceConfigUI.reset ( new TTS::LocalCommandConfigWidget ( this , this ->configRootDir ) );
147+ }
148+ else {
149+ serviceConfigUI.reset ( new TTS::DummyConfigWidget ( this ) );
150+ }
151+ configPane->layout ()->addWidget ( serviceConfigUI.get () );
152+ }
153+ } // namespace TTS
0 commit comments