QmitkSelectableGLWidget::QmitkSelectableGLWidget(QWidget *parent)
  : QWidget(parent), m_Ui(new Ui::QmitkSelectableGLWidget)
{
  m_Ui->setupUi(this);
  QLayout *hlayout;
  hlayout = m_Ui->hboxLayout;
  hlayout->setMargin(3);

  QString rendererName("Renderer::");
  rendererName += objectName();

  // create Renderer
  m_Renderer = mitk::VtkPropRenderer::New(
    qPrintable(rendererName), NULL, mitk::RenderingManager::GetInstance(), mitk::BaseRenderer::RenderingMode::Standard);

  // create widget
  QString composedName("QSGLWt::");
  if (!objectName().isEmpty())
    composedName += objectName();
  else
    composedName += "QmitkGLWidget";
  /*
  * here is the place to define QT-Flags to enable and disable certain OpenGL elements, like StencilBuffer, RGBA and so
  * on.
  * See QGLFormat for futher informations
  * QGL::AlphaChannel: Enable Alpha in Framebuffer
  * QGL::Rgba enable use of rgba rather than color_index
  * QGL::StencilBuffer for use of stencilbuffer in OpenGL
  */
  m_RenderWindow = new QmitkRenderWindow(this, composedName, m_Renderer);

  hlayout->addWidget(m_RenderWindow);
}
void RendererCreator::generateSource()
{
    QString widgetName( iUi.widgetName().toLower() );
    QString rendererName( iUi.rendererName().toLower() );

    replaceData("data/renderingplugin/src/renderer.cpp",
                widgetName + "/renderingplugin/src/" + rendererName +  "renderer.cpp");
}
void RendererCreator::generateHeader()
{
    QString widgetName( iUi.widgetName().toLower() );
    QString rendererName( iUi.rendererName().toLower() );

    replaceData("data/renderingplugin/inc/renderer.h",
                 widgetName + "/renderingplugin/inc/" + rendererName +  "renderer.h");
}
void RendererCreator::generateMMP()
{
    QString widgetName( iUi.widgetName().toLower() );
    QString rendererName( iUi.rendererName().toLower() );

    replaceData("data/renderingplugin/group/renderer.mmp",
                widgetName + "/renderingplugin/group/" + rendererName +  "renderer.mmp");
}
void MainWindow::on_pushButton_clicked()
{
    if( widgetName().isEmpty() || widgetUid().isEmpty() )
    {
        QMessageBox msgBox;
        msgBox.setText("Widget name and UID must be given.");
        msgBox.exec();
        return;
    }

    bool ok;
    uint uidVal = widgetUid().toUInt(&ok, 16 );
    if( widgetUid().length() != 10 || !widgetUid().startsWith( "0x",Qt::CaseInsensitive ) || !ok )
    {
        QMessageBox msgBox;
        msgBox.setText("UID must be in 4 byte hexadecimal format\n 0x11223344.");
        msgBox.exec();
        return;
    }

    if( uidVal >= 2147483648 ) //0x80000000
    {
        QMessageBox msgBox;
        msgBox.setText("UID must be less than\n 0x80000000.");
        msgBox.exec();
        return;
    }

    if (widgetLayout().length() == 0 && !rendererCheckBox().isChecked())
    {
        QMessageBox msgBox;
        msgBox.setText("Layout data not available yet.");
        msgBox.exec();
        return;
    }

    generateMainBuildFile();

    PublisherCreator* publisherCreator = new PublisherCreator(*this);
    delete publisherCreator;
    
    WidgetCreator* widgetCreator = new WidgetCreator(*this);
    delete widgetCreator;
    
    if( ui->renderingCheckBox->isChecked() )
    {
        if( rendererName().isEmpty() || rendererUid().isEmpty() )
        {
            QMessageBox msgBox;
            msgBox.setText("Renderer name and UID must be given.");
            msgBox.exec();
            return;
        }
        if( rendererUid().length() != 10 || !rendererUid().startsWith( "0x",Qt::CaseInsensitive ))
        {
            QMessageBox msgBox;
            msgBox.setText("UID must be in hexadecimal format\n 0x11223344.");
            msgBox.exec();
            return;
        }
        RendererCreator * renderer = new RendererCreator(*this);
        delete renderer;
    }

    QMessageBox msgBox;
    msgBox.setText("Widget created.");
    msgBox.exec();
    return;
}