#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include <QInputDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::modalClick()
{
    // Modal dialog can be allocated from stack, since this
    // function will not return until dialog has been closed
    Dialog d;
    int result = d.exec();
    if (result == QDialog::Accepted) {
        QMessageBox::information(this, "Message", "Got OK");
    } else {
        QMessageBox::information(this, "Message", "Got cancel");
    }
}

void MainWindow::modelessClick()
{
    // Modeless dialog needs to be allocated from the heap,
    // otherwise it would be destroyed at end
    Dialog *d = new Dialog(this);
    d->setModal(false);
    d->show();
    d->raise();
    d->activateWindow();

    // Results from modeless dialogs need to come via
    // signal-slot connections
    connect(d, SIGNAL(accepted()), SLOT(dialogAccepted()));
    connect(d, SIGNAL(rejected()), SLOT(dialogRejected()));
}

void MainWindow::dialogAccepted()
{
    QMessageBox::information(this, "Message", "Got OK");
}

void MainWindow::dialogRejected()
{
    QMessageBox::information(this, "Message", "Got cancel");
}

void MainWindow::inputClick()
{
    QString msg = QInputDialog::getText(this, "Query", "Enter message");
    int value = QInputDialog::getInt(this, "Query", "Enter value", 0, -100, 100);
    QMessageBox::information(this, "Message", "You said: " + msg +
                             " and chose " + QString::number(value));
}

void MainWindow::errorClick()
{
    QMessageBox::warning(this, "Error", "Something bad happened", QMessageBox::Close);
}

void MainWindow::messageClick()
{
    QMessageBox::information(this, "Hello", "Just a message...");
}

void MainWindow::questionClick()
{
    QMessageBox::StandardButton result = QMessageBox::question(
            this, "Question", "Would you like to quit?",
            QMessageBox::Yes | QMessageBox::No);
    if (result == QMessageBox::Yes) {
        QMessageBox::warning(this, "Quit", "That's not working...",
                             QMessageBox::Close);
    } else {
        QMessageBox::information(this, "Quit", "Good choice");
    }
}
