LoginDialog changes

This commit is contained in:
Jan Dalheimer 2014-04-16 17:38:26 +02:00
parent 4674aad125
commit 3ed5b1570b
2 changed files with 29 additions and 72 deletions

View File

@ -29,8 +29,6 @@ LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent), ui(new Ui::LoginDia
ui->progressBar->setVisible(false); ui->progressBar->setVisible(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
setAttribute(Qt::WA_ShowModal);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
} }
@ -43,14 +41,16 @@ LoginDialog::~LoginDialog()
// Stage 1: User interaction // Stage 1: User interaction
void LoginDialog::accept() void LoginDialog::accept()
{ {
setResult(Accepted); setUserInputsEnabled(false);
loop.quit(); ui->progressBar->setVisible(true);
}
void LoginDialog::reject() m_account = MojangAccount::createFromUsername(ui->userTextBox->text());
{ auto task = m_account->login(nullptr, ui->passTextBox->text());
setResult(Rejected); connect(task.get(), &Task::failed, this, &LoginDialog::onTaskFailed);
loop.quit(); connect(task.get(), &Task::succeeded, this, &LoginDialog::onTaskSucceeded);
connect(task.get(), &Task::status, this, &LoginDialog::onTaskStatus);
connect(task.get(), &Task::progress, this, &LoginDialog::onTaskProgress);
task->start();
} }
void LoginDialog::setUserInputsEnabled(bool enable) void LoginDialog::setUserInputsEnabled(bool enable)
@ -61,92 +61,50 @@ void LoginDialog::setUserInputsEnabled(bool enable)
} }
// Enable the OK button only when both textboxes contain something. // Enable the OK button only when both textboxes contain something.
void LoginDialog::on_userTextBox_textEdited(QString newText) void LoginDialog::on_userTextBox_textEdited(const QString &newText)
{ {
ui->buttonBox->button(QDialogButtonBox::Ok) ui->buttonBox->button(QDialogButtonBox::Ok)
->setEnabled(!newText.isEmpty() && !ui->passTextBox->text().isEmpty()); ->setEnabled(!newText.isEmpty() && !ui->passTextBox->text().isEmpty());
} }
void LoginDialog::on_passTextBox_textEdited(const QString &newText)
void LoginDialog::on_passTextBox_textEdited(QString newText)
{ {
ui->buttonBox->button(QDialogButtonBox::Ok) ui->buttonBox->button(QDialogButtonBox::Ok)
->setEnabled(!newText.isEmpty() && !ui->userTextBox->text().isEmpty()); ->setEnabled(!newText.isEmpty() && !ui->userTextBox->text().isEmpty());
} }
// Stage 2: Task interaction void LoginDialog::onTaskFailed(const QString &reason)
void LoginDialog::onTaskFailed(QString failure)
{ {
// Set message // Set message
ui->label->setText("<span style='color:red'>" + failure + "</span>"); ui->label->setText("<span style='color:red'>" + reason + "</span>");
// Return setUserInputsEnabled(true);
setResult(Rejected); ui->progressBar->setVisible(false);
loop.quit();
} }
void LoginDialog::onTaskSucceeded() void LoginDialog::onTaskSucceeded()
{ {
setResult(Accepted); QDialog::accept();
loop.quit();
} }
void LoginDialog::onTaskStatus(QString status) void LoginDialog::onTaskStatus(const QString &status)
{ {
ui->label->setText(status); ui->label->setText(status);
} }
void LoginDialog::onTaskProgress(qint64 current, qint64 total) void LoginDialog::onTaskProgress(qint64 value, qint64 max)
{ {
ui->progressBar->setMaximum(total); ui->progressBar->setMaximum(max);
ui->progressBar->setValue(current); ui->progressBar->setValue(value);
} }
// Public interface // Public interface
MojangAccountPtr LoginDialog::newAccount(QWidget *parent, QString msg) MojangAccountPtr LoginDialog::newAccount(QWidget *parent, QString msg)
{ {
LoginDialog dlg(parent); LoginDialog dlg(parent);
dlg.show();
dlg.ui->label->setText(msg); dlg.ui->label->setText(msg);
if (dlg.exec() == QDialog::Accepted)
while (1)
{ {
// Show dialog return dlg.m_account;
dlg.loop.exec();
// Close if cancel was clicked
if (dlg.result() == Rejected)
return nullptr;
// Read values
QString username(dlg.ui->userTextBox->text());
QString password(dlg.ui->passTextBox->text());
// disable inputs
dlg.setUserInputsEnabled(false);
dlg.ui->progressBar->setVisible(true);
// Start login process
MojangAccountPtr account = MojangAccount::createFromUsername(username);
auto task = account->login(nullptr, password);
// show progess
connect(task.get(), &ProgressProvider::failed, &dlg, &LoginDialog::onTaskFailed);
connect(task.get(), &ProgressProvider::succeeded, &dlg, &LoginDialog::onTaskSucceeded);
connect(task.get(), &ProgressProvider::status, &dlg, &LoginDialog::onTaskStatus);
connect(task.get(), &ProgressProvider::progress, &dlg, &LoginDialog::onTaskProgress);
// Start task
if (!task->isRunning())
task->start();
if (task->isRunning())
dlg.loop.exec();
// Be done
if (dlg.result() == Accepted)
return account;
// Otherwise, re-enable user inputs and begin anew
dlg.setUserInputsEnabled(true);
dlg.ui->progressBar->setVisible(false);
} }
return 0;
} }

View File

@ -45,17 +45,16 @@ private:
protected protected
slots: slots:
void accept(); void accept();
void reject();
void onTaskFailed(QString); void onTaskFailed(const QString &reason);
void onTaskSucceeded(); void onTaskSucceeded();
void onTaskStatus(QString); void onTaskStatus(const QString &status);
void onTaskProgress(qint64, qint64); void onTaskProgress(qint64 value, qint64 max);
void on_userTextBox_textEdited(QString); void on_userTextBox_textEdited(const QString &newText);
void on_passTextBox_textEdited(QString); void on_passTextBox_textEdited(const QString &newText);
private: private:
Ui::LoginDialog *ui; Ui::LoginDialog *ui;
QEventLoop loop; MojangAccountPtr m_account;
}; };