RenderingDialog::RenderingDialog(Project_sV *project, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RenderingDialog),
    m_project(project)
{
    ui->setupUi(this);

    // Render section
    m_sectionGroup = new QButtonGroup(this);
    m_sectionGroup->addButton(ui->radioFullProject);
    m_sectionGroup->addButton(ui->radioSection);
    m_sectionGroup->addButton(ui->radioTagSection);
    QString mode(m_project->preferences()->renderSectionMode());
    if (mode == "full") {
        ui->radioFullProject->setChecked(true);
    } else if (mode == "expr") {
        ui->radioSection->setChecked(true);
    } else if (mode == "tags") {
        ui->radioTagSection->setChecked(true);
    } else {
        qDebug() << "Unknown render section mode: " << mode;
        Q_ASSERT(false);
    }

    // Optical flow
    ui->lambda->setValue(m_project->preferences()->flowV3DLambda());

    // Motion blur
    ui->maxSamples->setValue(m_project->motionBlur()->maxSamples());
    ui->slowmoSamples->setValue(m_project->motionBlur()->slowmoSamples());
    m_blurGroup = new QButtonGroup(this);
    m_blurGroup->addButton(ui->radioBlurConvolution);
    m_blurGroup->addButton(ui->radioBlurStacking);
    m_blurGroup->addButton(ui->radioBlurNearest);
    if (m_project->preferences()->renderMotionblurType() == MotionblurType_Convolving) {
        ui->radioBlurConvolution->setChecked(true);
    } else if (m_project->preferences()->renderMotionblurType() == MotionblurType_Stacking) {
        ui->radioBlurStacking->setChecked(true);
    } else {
        ui->radioBlurNearest->setChecked(true);
    }

    fillTagLists();

    // Output target type
    m_targetGroup = new QButtonGroup(this);
    m_targetGroup->addButton(ui->radioImages);
    m_targetGroup->addButton(ui->radioVideo);
    if (m_project->preferences()->renderTarget() == "images") {
        ui->radioImages->setChecked(true);
    } else {
        ui->radioVideo->setChecked(true);
    }

    // Output target files
    ui->imagesOutputDir->setText(m_project->preferences()->imagesOutputDir());
    ui->imagesFilenamePattern->setText(m_project->preferences()->imagesFilenamePattern());
    ui->videoOutputFile->setText(m_project->preferences()->videoFilename());
    ui->vcodec->setText(m_project->preferences()->videoCodec());

    // FPS
    QString fps = QVariant(m_project->preferences()->renderFPS().fps()).toString();
    if (ui->cbFps->findText(fps) < 0 && fps.toFloat() > 0) {
        ui->cbFps->addItem(fps);
    }
    ui->cbFps->setCurrentIndex(ui->cbFps->findText(fps));

    // Output size
    ui->cbSize->addItem("Original size", QVariant(FrameSize_Orig));
    ui->cbSize->addItem("Small", QVariant(FrameSize_Small));
    ui->cbSize->setCurrentIndex(ui->cbSize->findData(QVariant(m_project->preferences()->renderFrameSize())));

    // Interpolation type
    ui->cbInterpolation->addItem(toString(InterpolationType_Forward), QVariant(InterpolationType_Forward));
    ui->cbInterpolation->addItem(toString(InterpolationType_ForwardNew), QVariant(InterpolationType_ForwardNew));
    ui->cbInterpolation->addItem(toString(InterpolationType_Twoway), QVariant(InterpolationType_Twoway));
    ui->cbInterpolation->addItem(toString(InterpolationType_TwowayNew), QVariant(InterpolationType_TwowayNew));
    ui->cbInterpolation->addItem(toString(InterpolationType_Bezier), QVariant(InterpolationType_Bezier));
    if (ui->cbInterpolation->findData(QVariant(m_project->preferences()->renderInterpolationType())) >= 0) {
        ui->cbInterpolation->setCurrentIndex(ui->cbInterpolation->findData(QVariant(m_project->preferences()->renderInterpolationType())));
    }

    bool b = true;
    b &= connect(m_targetGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateRenderTarget()));
    b &= connect(m_sectionGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotSectionModeChanged()));
    b &= connect(ui->timeStart, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    b &= connect(ui->timeEnd, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));

    b &= connect(ui->cbStartTag, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTagIndexChanged()));
    b &= connect(ui->cbEndTag, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTagIndexChanged()));

    b &= connect(ui->bAbort, SIGNAL(clicked()), this, SLOT(reject()));
    b &= connect(ui->bOk, SIGNAL(clicked()), this, SLOT(accept()));
    b &= connect(ui->bSave, SIGNAL(clicked()), this, SLOT(slotSaveSettings()));

    b &= connect(ui->cbFps, SIGNAL(editTextChanged(QString)), this, SLOT(slotValidate()));

    b &= connect(ui->imagesOutputDir, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    b &= connect(ui->imagesFilenamePattern, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    b &= connect(ui->videoOutputFile, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    b &= connect(ui->bImagesBrowseDir, SIGNAL(clicked()), this, SLOT(slotBrowseImagesDir()));
    b &= connect(ui->bBrowseVideoOutputFile, SIGNAL(clicked()), this, SLOT(slotBrowseVideoFile()));
    Q_ASSERT(b);

    // Restore rendering start/end
    int index;
    index = ui->cbStartTag->findText(m_project->preferences()->renderStartTag());
    if (index >= 0) {
        ui->cbStartTag->setCurrentIndex(index);
    }
    index = ui->cbEndTag->findText(m_project->preferences()->renderEndTag());
    if (index >= 0) {
        ui->cbEndTag->setCurrentIndex(index);
    }
    if (m_project->preferences()->renderStartTime().length() > 0) {
        ui->timeStart->setText(m_project->preferences()->renderStartTime());
    }
    if (m_project->preferences()->renderEndTime().length() > 0) {
        ui->timeEnd->setText(m_project->preferences()->renderEndTime());
    }

#if QT_VERSION >= 0x040700
    ui->timeStart->setPlaceholderText(QVariant(m_project->nodes()->startTime()).toString());
    ui->timeEnd->setPlaceholderText(QVariant(m_project->nodes()->endTime()).toString());
#endif

    slotUpdateRenderTarget();
    slotSectionModeChanged();
}
RenderingDialog::RenderingDialog(Project_sV *project, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RenderingDialog),
    m_project(project)
{
    ui->setupUi(this);

    // Render section
    m_sectionGroup = new QButtonGroup(this);
    m_sectionGroup->addButton(ui->radioFullProject);
    m_sectionGroup->addButton(ui->radioSection);
    m_sectionGroup->addButton(ui->radioTagSection);
    QString mode(m_project->preferences()->renderSectionMode());
    if (mode == "full") {
        ui->radioFullProject->setChecked(true);
    } else if (mode == "expr") {
        ui->radioSection->setChecked(true);
    } else if (mode == "tags") {
        ui->radioTagSection->setChecked(true);
    } else {
        qDebug() << "Unknown render section mode: " << mode;
        Q_ASSERT(false);
    }

    // Optical flow
    ui->lambda->setValue(m_project->preferences()->flowV3DLambda());

    QSettings settings; //TODO: better define in project ?
    ui->opticalFlowAlgo->clear();
    QString flow_method = settings.value("preferences/flowMethod", "OpenCV-CPU").toString();
    if (flow_method == "V3D") {
        ui->opticalFlowAlgo->addItem(tr("flowBuilder"), QVariant(1));
    } else {
        ui->opticalFlowAlgo->addItem(tr("OpenCV - Farneback"), QVariant(2));
        ui->opticalFlowAlgo->addItem(tr("OpenCV - Dual TVL1"), QVariant(3));
    }

    connect(ui->opticalFlowAlgo, SIGNAL(activated(int)),
            ui->flowStackedWidget, SLOT(setCurrentIndex(int)));

    QWidget *flowbuilder_pane = ui->flowStackedWidget->widget(0);
    QWidget *farneback_pane = ui->flowStackedWidget->widget(1);
    QWidget *tvl1_pane = ui->flowStackedWidget->widget(2);
    if (flow_method == "V3D") {
        ui->opticalFlowAlgo->setCurrentIndex(0);
        ui->flowStackedWidget->setCurrentIndex(0);
        ui->flowStackedWidget->removeWidget(farneback_pane);
        ui->flowStackedWidget->removeWidget(tvl1_pane);
    } else {
        int algo = settings.value("preferences/preferredOpenCVAlgo", 0).toInt();
        ui->opticalFlowAlgo->setCurrentIndex(algo);
        ui->flowStackedWidget->setCurrentIndex(algo+1);
        ui->flowStackedWidget->removeWidget(flowbuilder_pane);
    }
    connect(ui->clearflow, SIGNAL(clicked()), this, SLOT(slotClearFlowCache()));
    // Motion blur
    ui->maxSamples->setValue(m_project->motionBlur()->maxSamples());
    ui->slowmoSamples->setValue(m_project->motionBlur()->slowmoSamples());
    m_blurGroup = new QButtonGroup(this);
    m_blurGroup->addButton(ui->radioBlurConvolution);
    m_blurGroup->addButton(ui->radioBlurStacking);
    m_blurGroup->addButton(ui->radioBlurNearest);
    if (m_project->preferences()->renderMotionblurType() == MotionblurType_Convolving) {
        ui->radioBlurConvolution->setChecked(true);
    } else if (m_project->preferences()->renderMotionblurType() == MotionblurType_Stacking) {
        ui->radioBlurStacking->setChecked(true);
    } else {
        ui->radioBlurNearest->setChecked(true);
    }

    fillTagLists();

    // Output target type
    m_targetGroup = new QButtonGroup(this);
    m_targetGroup->addButton(ui->radioImages);
    m_targetGroup->addButton(ui->radioVideo);
    if (m_project->preferences()->renderTarget() == "images") {
        ui->radioImages->setChecked(true);
    } else {
        ui->radioVideo->setChecked(true);
    }

    // Output target files
    ui->imagesOutputDir->setText(m_project->preferences()->imagesOutputDir());
    ui->imagesFilenamePattern->setText(m_project->preferences()->imagesFilenamePattern());
    ui->videoOutputFile->setText(m_project->preferences()->videoFilename());
    ui->vcodec->setText(m_project->preferences()->videoCodec());

    // FPS
    QString fps = QVariant(m_project->preferences()->renderFPS().fps()).toString();
    if (ui->cbFps->findText(fps) < 0 && fps.toFloat() > 0) {
        ui->cbFps->addItem(fps);
    }
    ui->cbFps->setCurrentIndex(ui->cbFps->findText(fps));

    // Output size
    ui->cbSize->addItem(tr("Original size"), QVariant(FrameSize_Orig));
    ui->cbSize->addItem(tr("Small"), QVariant(FrameSize_Small));
    ui->cbSize->setCurrentIndex(ui->cbSize->findData(QVariant(m_project->preferences()->renderFrameSize())));

    // Interpolation type
    ui->cbInterpolation->addItem(toString(InterpolationType_Forward), QVariant(InterpolationType_Forward));
    ui->cbInterpolation->addItem(toString(InterpolationType_ForwardNew), QVariant(InterpolationType_ForwardNew));
    ui->cbInterpolation->addItem(toString(InterpolationType_Twoway), QVariant(InterpolationType_Twoway));
    ui->cbInterpolation->addItem(toString(InterpolationType_TwowayNew), QVariant(InterpolationType_TwowayNew));
    ui->cbInterpolation->addItem(toString(InterpolationType_Bezier), QVariant(InterpolationType_Bezier));
    ui->cbInterpolation->addItem(toString(InterpolationType_None), QVariant(InterpolationType_None));
    ui->cbInterpolation->addItem(toString(InterpolationType_Nearest), QVariant(InterpolationType_Nearest));
    if (ui->cbInterpolation->findData(QVariant(m_project->preferences()->renderInterpolationType())) >= 0) {
        ui->cbInterpolation->setCurrentIndex(ui->cbInterpolation->findData(QVariant(m_project->preferences()->renderInterpolationType())));
    }

    connect(m_targetGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateRenderTarget()));
    connect(m_sectionGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotSectionModeChanged()));
    connect(ui->timeStart, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    connect(ui->timeEnd, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));

    connect(ui->cbStartTag, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTagIndexChanged()));
    connect(ui->cbEndTag, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTagIndexChanged()));

    connect(ui->bAbort, SIGNAL(clicked()), this, SLOT(reject()));
    connect(ui->bOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->bSave, SIGNAL(clicked()), this, SLOT(slotSaveSettings()));

    connect(ui->cbFps, SIGNAL(editTextChanged(QString)), this, SLOT(slotValidate()));

    connect(ui->imagesOutputDir, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    connect(ui->imagesFilenamePattern, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    connect(ui->videoOutputFile, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
    connect(ui->bImagesBrowseDir, SIGNAL(clicked()), this, SLOT(slotBrowseImagesDir()));
    connect(ui->bBrowseVideoOutputFile, SIGNAL(clicked()), this, SLOT(slotBrowseVideoFile()));

    // Restore rendering start/end
    int index = ui->cbStartTag->findText(m_project->preferences()->renderStartTag());
    if (index >= 0) {
        ui->cbStartTag->setCurrentIndex(index);
    }
    index = ui->cbEndTag->findText(m_project->preferences()->renderEndTag());
    if (index >= 0) {
        ui->cbEndTag->setCurrentIndex(index);
    }
    if (m_project->preferences()->renderStartTime().length() > 0) {
        ui->timeStart->setText(m_project->preferences()->renderStartTime());
    }
    if (m_project->preferences()->renderEndTime().length() > 0) {
        ui->timeEnd->setText(m_project->preferences()->renderEndTime());
    }

#if QT_VERSION >= 0x040700
    ui->timeStart->setPlaceholderText(QVariant(m_project->nodes()->startTime()).toString());
    ui->timeEnd->setPlaceholderText(QVariant(m_project->nodes()->endTime()).toString());
#endif

#ifndef USE_QTKIT
     ui->use_qt->setChecked(false);
     ui->use_qt->setEnabled(false);
#endif

    slotUpdateRenderTarget();
    slotSectionModeChanged();
}