void MainWindow::slotShowRenderDialog()
{
    if (m_project->renderTask() != NULL) {
        disconnect(SIGNAL(signalRendererContinue()), m_project->renderTask());
    }
    
    RenderingDialog renderingDialog(m_project, this);
    if (renderingDialog.exec() == QDialog::Accepted) {
        
        RenderTask_sV *task = renderingDialog.buildTask();
        if (task != 0) {
            task->moveToThread(&m_rendererThread);
            
            if (m_project->renderTask() != NULL) {
                disconnect(SIGNAL(signalRendererContinue()), m_project->renderTask());
            }
            //m_project->replaceRenderTask(task);
            
            if (m_renderProgressDialog == NULL) {
                m_renderProgressDialog = new ProgressDialog(this);
                m_renderProgressDialog->setWindowTitle(tr("Rendering progress"));
            } else {
                m_renderProgressDialog->disconnect();
            }
            
            connect(task, SIGNAL(signalNewTask(QString,int)), m_renderProgressDialog, SLOT(slotNextTask(QString,int)));
            connect(task, SIGNAL(signalItemDesc(QString)), m_renderProgressDialog, SLOT(slotTaskItemDescription(QString)));
            connect(task, SIGNAL(signalTaskProgress(int)), m_renderProgressDialog, SLOT(slotTaskProgress(int)));
            connect(task, SIGNAL(signalRenderingFinished(QString)), m_renderProgressDialog, SLOT(slotAllTasksFinished(QString)));
            connect(task, SIGNAL(signalRenderingAborted(QString)), this, SLOT(slotRenderingAborted(QString)));
            connect(task, SIGNAL(signalRenderingAborted(QString)), m_renderProgressDialog, SLOT(close()));
            connect(task, SIGNAL(signalRenderingStopped(QString)), m_renderProgressDialog, SLOT(slotAborted(QString)));
            connect(m_renderProgressDialog, SIGNAL(signalAbortTask()), task, SLOT(slotStopRendering()));
            //connect(this, SIGNAL(signalRendererContinue()), task, SLOT(slotContinueRendering()), Qt::UniqueConnection);
            
            connect(task, SIGNAL(workFlowRequested()), &m_rendererThread, SLOT(start()));
            connect(&m_rendererThread, SIGNAL(started()), task, SLOT(slotContinueRendering()));
 
            connect(task, SIGNAL(signalRenderingFinished(QString)), &m_rendererThread, SLOT(quit()));
            // done another way ?!
            connect(task, SIGNAL(signalRenderingFinished(QString)), task, SLOT(deleteLater()));
             
            //connect(&m_rendererThread, &QThread::finished, task, &QObject::deleteLater);
            // let's start
            m_rendererThread.wait(); // If the thread is not running, this will immediately return.
            
            m_renderProgressDialog->show();
            
            //emit signalRendererContinue();
            //m_rendererThread.exec ();
            m_rendererThread.start();
            task->requestWork();
        }
    } else {
    	QMessageBox(QMessageBox::Warning, tr("Aborted"), tr("Aborted by user"), QMessageBox::Ok).exec();
    }
}
void SlowmoRenderer_sV::load(QString filename) throw(Error)
{
    if (m_project != NULL) {
        delete m_project;
        m_project = NULL;
    }
    QString warning;
    try {
        Project_sV *proj = XmlProjectRW_sV::loadProject(QString(filename), &warning);

        if (warning.length() > 0) {
            std::cout << warning.toStdString() << std::endl;
        }

        m_project = proj;

        RenderTask_sV *task = new RenderTask_sV(m_project);
        m_project->replaceRenderTask(task);
        task->renderPreferences().setFps(24);
        task->setTimeRange(m_start, m_end);
        task->setQtConnectionType(Qt::AutoConnection);

        bool b = true;
        b &= connect(m_project->renderTask(), SIGNAL(signalNewTask(QString,int)), this, SLOT(slotTaskSize(QString,int)));
        b &= connect(m_project->renderTask(), SIGNAL(signalTaskProgress(int)), this, SLOT(slotProgressInfo(int)));
        b &= connect(m_project->renderTask(), SIGNAL(signalRenderingAborted(QString)), this, SLOT(slotFinished(QString)));
        b &= connect(m_project->renderTask(), SIGNAL(signalRenderingFinished(QString)), this, SLOT(slotFinished(QString)));
        b &= connect(m_project->renderTask(), SIGNAL(signalRenderingStopped(QString)), this, SLOT(slotFinished(QString)));
        Q_ASSERT(b);

    } catch (Error_sV &err) {
        throw Error(err.message().toStdString());
    }
}
/**
 *  this is the real workhorse.
 * maybe we should not call this directly, but instead from doWork ?
 */
void RenderTask_sV::slotContinueRendering()
{
    qDebug()<<"Starting rendering process in Thread "<<thread()->currentThreadId();   

    /* real workhorse, need to account for exporting */
    setupProgress(trUtf8("Rendering Slow-Mo …"), 2* int(m_prefs.fps().fps() * (m_timeEnd-m_timeStart)));
        
    //TODO: initialize
    m_stopwatch.start();
    
    m_nextFrameTime=m_timeStart;
    
    int framesBefore;
    qreal snapped = m_project->snapToOutFrame(m_nextFrameTime, false, m_prefs.fps(), &framesBefore);
    qDebug() << "Frame snapping in from " << m_nextFrameTime << " to " << snapped;
    m_nextFrameTime = snapped;
    
    Q_ASSERT(int((m_nextFrameTime - m_project->nodes()->startTime()) * m_prefs.fps().fps() + .5) == framesBefore);
    
    try {
    	m_renderTarget->openRenderTarget();
    } catch (Error_sV &err) {
            m_stopRendering = true;
            emit signalRenderingAborted(tr("Rendering aborted.") + " " + err.message());
            return;
    }
    
    // render loop
    // TODO: add more threading here
    while(m_nextFrameTime<m_timeEnd) {
    	    QCoreApplication::processEvents();
    	    
        // Checks if the process should be aborted
        mutex.lock();
        bool abort = m_stopRendering;
        mutex.unlock();
        
        if (abort) {
        	// user stop the process
            qDebug()<<"Aborting Rendering process in Thread "<<thread()->currentThreadId();
						m_renderTimeElapsed = m_stopwatch.elapsed();
            emit signalRenderingStopped(QTime().addMSecs(m_renderTimeElapsed).toString("hh:mm:ss"));
        	qDebug() << "Rendering stopped after " << QTime().addMSecs(m_renderTimeElapsed).toString("hh:mm:ss");
            break;
        }
        
        // do the work
        int outputFrame = (m_nextFrameTime - m_project->nodes()->startTime()) * m_prefs.fps().fps() + .5;
        qreal srcTime = m_project->nodes()->sourceTime(m_nextFrameTime);
        
        qDebug() << "Rendering frame number " << outputFrame << " @" << m_nextFrameTime << " from source time " << srcTime;
        updateMessage(tr("Rendering frame %1 @ %2 s  from input position: %3 s (frame %4)")
                            .arg( QString::number(outputFrame),QString::number(m_nextFrameTime),
                                  QString::number(srcTime),
                                  QString::number(srcTime*m_project->frameSource()->fps()->fps())
                             ) );
           
    	 try {
                QImage rendered = m_project->render(m_nextFrameTime, m_prefs);

                m_renderTarget->slotConsumeFrame(rendered, outputFrame);
                m_nextFrameTime = m_nextFrameTime + 1/m_prefs.fps().fps();

                updateProgress( (m_nextFrameTime-m_timeStart) * m_prefs.fps().fps() );
                
            } catch (FlowBuildingError &err) {
                m_stopRendering = true;
                emit signalRenderingAborted(err.message());
            } catch (InterpolationError &err) {
                updateMessage(err.message());
            }
        
        
    } /* while */
    
    
    // Checks if the process should be aborted
    mutex.lock();
    bool abort = m_stopRendering;
    mutex.unlock();
        
    if (abort) {
						qDebug() << "Rendering : aborting";
						updateMessage(tr("Rendering : aborting"));
	  } else {
						//TODO: closing rendering project
						qDebug() << "Rendering : exporting";
						updateMessage(tr("Rendering : exporting"));
						m_renderTarget->closeRenderTarget();
	  }

		m_renderTimeElapsed = m_stopwatch.elapsed();
		qDebug() << "time : " << m_renderTimeElapsed;
		emit signalRenderingFinished(QTime(0,0).addMSecs(m_renderTimeElapsed).toString("hh:mm:ss"));
		qDebug() << "Rendering stopped after " << QTime(0,0).addMSecs(m_renderTimeElapsed).toString("hh:mm:ss");
   
		qDebug()<<"Rendering process finished in Thread "<<thread()->currentThreadId();

    // Set _working to false, meaning the process can't be aborted anymore.
    mutex.lock();
    _working = false;
    mutex.unlock();
}