Skip to content

Commit

Permalink
[UI] +NetworkStateEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
zhang-ray committed Nov 1, 2018
1 parent 08dddef commit 0fdbe60
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
80 changes: 47 additions & 33 deletions client_qt5/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

QEvent::Type AudioVolumeEvent::sType = (QEvent::Type)QEvent::registerEventType();
QEvent::Type VadEvent::sType = (QEvent::Type)QEvent::registerEventType();
QEvent::Type NetworkStateEvent::sType = (QEvent::Type)QEvent::registerEventType();


MainWindow::MainWindow(QWidget *parent)
Expand Down Expand Up @@ -137,7 +138,7 @@ MainWindow::MainWindow(QWidget *parent)
{
onVolumeChanged({AudioInOut::In, 0u});
onVolumeChanged({AudioInOut::Out, 0u});
updateUiState(NetworkState::Disconnected);
onNetworkChanged(NetworkState::Disconnected);
toggleAdvancedMode(true);
showMessage("F1: help F2: advanced mode");
}
Expand Down Expand Up @@ -179,44 +180,49 @@ void MainWindow::on_pushButton_connecting_clicked(){
worker_->syncStop();
worker_=nullptr;
}
updateUiState(NetworkState::Disconnected);
onNetworkChanged(NetworkState::Disconnected);
}
break;
}
}
}

void MainWindow::updateUiState(const NetworkState networkState){
switch(networkState){
case NetworkState::Disconnected:{
ui->lineEdit_serverHost->setEnabled(true);
ui->lineEdit_serverPort->setEnabled(true);
ui->checkBox_needAec->setEnabled(true);
ui->pushButton_connecting->setEnabled(true);
ui->pushButton_connecting->setText("Connect!");
//showMessage("");
break;
}
case NetworkState::Connecting:{
ui->lineEdit_serverHost->setEnabled(false);
ui->lineEdit_serverPort->setEnabled(false);
ui->checkBox_needAec->setEnabled(false);
ui->pushButton_connecting->setEnabled(false);
ui->pushButton_connecting->setText("Connecting...");
break;
}
case NetworkState::Connected:{
ui->lineEdit_serverHost->setEnabled(false);
ui->lineEdit_serverPort->setEnabled(false);
ui->checkBox_needAec->setEnabled(false);
ui->pushButton_connecting->setEnabled(true);
ui->pushButton_connecting->setText("Disconnect!");
break;
void MainWindow::onNetworkChanged(const NetworkState networkState){
if(mainThreadId_ == std::this_thread::get_id()){
switch(networkState){
case NetworkState::Disconnected:{
ui->lineEdit_serverHost->setEnabled(true);
ui->lineEdit_serverPort->setEnabled(true);
ui->checkBox_needAec->setEnabled(true);
ui->pushButton_connecting->setEnabled(true);
ui->pushButton_connecting->setText("Connect!");
//showMessage("");
break;
}
case NetworkState::Connecting:{
ui->lineEdit_serverHost->setEnabled(false);
ui->lineEdit_serverPort->setEnabled(false);
ui->checkBox_needAec->setEnabled(false);
ui->pushButton_connecting->setEnabled(false);
ui->pushButton_connecting->setText("Connecting...");
break;
}
case NetworkState::Connected:{
ui->lineEdit_serverHost->setEnabled(false);
ui->lineEdit_serverPort->setEnabled(false);
ui->checkBox_needAec->setEnabled(false);
ui->pushButton_connecting->setEnabled(true);
ui->pushButton_connecting->setText("Disconnect!");
break;
}
default:
break;
}
currentUiState_=networkState;
}
default:
break;
else{
QCoreApplication::postEvent(this, new NetworkStateEvent(networkState));
}
currentUiState_=networkState;
}

void MainWindow::onVolumeChanged(const AudioIoVolume aivl) {
Expand Down Expand Up @@ -249,7 +255,7 @@ void MainWindow::toggleAdvancedMode(bool newMode)

void MainWindow::gotoWork(){
try {
updateUiState(NetworkState::Connecting);
onNetworkChanged(NetworkState::Connecting);


worker_ = std::make_shared<Worker>(ui->checkBox_needAec->checkState()==Qt::CheckState::Checked);
Expand All @@ -276,7 +282,7 @@ void MainWindow::gotoWork(){
)
{
showMessage(extraMessage);
updateUiState(newState);
onNetworkChanged(newState);
}
);

Expand Down Expand Up @@ -315,11 +321,19 @@ bool MainWindow::event(QEvent *event)
} else if (event->type() == AudioVolumeEvent::sType) {
AudioVolumeEvent *myEvent = static_cast<AudioVolumeEvent *>(event);
onVolumeChanged({myEvent->io_, myEvent->level_});
delete myEvent;
return true;
}
else if (event->type() == VadEvent::sType){
VadEvent *myEvent = static_cast<VadEvent *>(event);
onVad(myEvent->isActive_);
delete myEvent;
return true;
}
else if (event->type() == NetworkStateEvent::sType){
NetworkStateEvent *myEvent = static_cast<NetworkStateEvent *>(event);
onNetworkChanged(myEvent->state_);
delete myEvent;
return true;
}

Expand Down
11 changes: 10 additions & 1 deletion client_qt5/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class VadEvent : public QEvent {
VadEvent(const bool isActive):isActive_(isActive), QEvent(sType){}
};


class NetworkStateEvent : public QEvent {
public:
static QEvent::Type sType;
NetworkState state_;
NetworkStateEvent(const NetworkState state):state_(state), QEvent(sType){}
};


class MainWindow : public QMainWindow
{
Q_OBJECT
Expand All @@ -48,10 +57,10 @@ class MainWindow : public QMainWindow

private slots:
void on_pushButton_connecting_clicked();
void updateUiState(const NetworkState networkState);


/// TODO: render max volume bar
void onNetworkChanged(const NetworkState networkState);
void onVolumeChanged(const AudioIoVolume);
void onDeviceNameChanged(const std::string &newMic, const std::string &newSpk){}
void onVad(bool isActive);
Expand Down

0 comments on commit 0fdbe60

Please sign in to comment.