#include "dialog.h"

#include <QtWidgets>

class MyDialog : public QDialog
{
public:
    MyDialog(QWidget* p_Parent = nullptr)
        : QDialog(p_Parent)
    {
        QVBoxLayout* hlayout = new QVBoxLayout;
        
        for (int i = 0; i < 3	; ++i)
            hlayout->addWidget(new QLabel("Place holder:", this));
        
        QPushButton* closeBtn = new QPushButton("Close", this);
        
        for (int i = 0; i < 4; ++i)
        {
            auto lineEdt = new QLineEdit(this);
            if (i == 0)
            {
                m_Edit = lineEdt;
            }
            hlayout->addWidget(lineEdt);
        }
        
        for (int i = 0; i < 5; ++i)
            hlayout->addWidget(new QLabel("Place holder:", this));
        
        hlayout->addWidget(closeBtn);
        setLayout(hlayout);
        
        connect(closeBtn, &QPushButton::clicked, this, [this] { accept(); });
    }
    
    virtual ~MyDialog() {}
    
protected:
    virtual void showEvent(QShowEvent* p_Event) override
    {
        // Move to the center of screen
        const QRect& screenGeometry = QApplication::desktop()->screenGeometry(0);
        move(screenGeometry.center() - rect().center());
        
        // set focus to first line edit
        m_Edit->setFocus();
    }
    
private:
    QLineEdit* m_Edit =  nullptr;
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout* layout = new QHBoxLayout;
    QPushButton* btn = new QPushButton("Open Dialog", this);
    layout->addWidget(btn);
    setLayout(layout);
    
    connect(btn, &QPushButton::clicked, this, [this]
    {
        MyDialog dlg(nullptr);
        dlg.exec();
    });
    
    setStyleSheet("background-color: gray;");
}

MyWidget::~MyWidget()
{
}
