#include "testqglcrash.h"


testQGLCrash::testQGLCrash(QWidget *parent)
    : QWidget(parent)
{
    m_dummyGLWidget = new QGLWidget;
    m_dummyGLWidget->setMinimumSize(QSize(200, 100));
    m_glWnd = 0;

    QVBoxLayout *lo = new QVBoxLayout;
    lo->setSizeConstraint(QLayout::SetFixedSize);

    m_button = new QPushButton("press me", this);
    connect(m_button, SIGNAL(clicked()), this, SLOT(buttonPressed()));

    lo->addWidget(m_button);
    lo->addWidget(m_dummyGLWidget);
    setLayout(lo);
}

testQGLCrash::~testQGLCrash()
{
    delete m_dummyGLWidget;
}

void testQGLCrash::buttonPressed()
{
    if (!m_glWnd) {
        m_glWnd = new glWindow(this, m_dummyGLWidget);
        layout()->addWidget(m_glWnd);
        m_button->setText("Trigger crash");
        adjustSize();
        return;
    }

    layout()->removeWidget(m_glWnd);
    delete m_glWnd;
    m_glWnd = 0;

    m_glWnd = new glWindow(this, m_dummyGLWidget);
    layout()->addWidget(m_glWnd);
}

glWindow::glWindow(QWidget *parent, QGLWidget *sharedWidget)
    : QGLWidget(parent, sharedWidget)
{
    setAutoFillBackground(true);
}

glWindow::~glWindow()
{

}

void glWindow::paintEvent(QPaintEvent *e)
{
    makeCurrent();

    QPainter p(this);

    p.drawText(QPointF(0, 20), "Hello World!");
}

