#include "stereolab.h"
#include <qmessagebox.h>
#include <qfiledialog.h>

StereoLab::StereoLab(QWidget *parent)
    : QDialog(parent)
{
	ui.setupUi(this);

	buffer1 = NULL;
	buffer2 = NULL;


	connect(ui.browse1Button, SIGNAL(clicked()), this, SLOT(activateLoadDialog1()));
	connect(ui.browse2Button, SIGNAL(clicked()), this, SLOT(activateLoadDialog2()));

	connect(ui.load1Button, SIGNAL(released()), this, SLOT(reloadBuffer1()));
	connect(ui.load2Button, SIGNAL(released()), this, SLOT(reloadBuffer2()));

	connect(ui.viMatchButton, SIGNAL(pressed()), &matcherDialog, SLOT(show()));
	connect(ui.rectifyButton, SIGNAL(pressed()), &rectifyDialog, SLOT(show()));
	connect(ui.sgmButton, SIGNAL(pressed()), &sgmDialog, SLOT(show()));
	connect(ui.cloudButton, SIGNAL(pressed()), &cloudDialog, SLOT(show()));


	connect(&matcherDialog, SIGNAL(corrListChanged(Correspondance *, int)), &rectifyDialog, SLOT(corrListChanged(Correspondance *, int)));

	connect(&rectifyDialog, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &sgmDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
	connect(&rectifyDialog, SIGNAL(maxDisparityChanged(int)), &sgmDialog, SLOT(maxDisparityChangedSlot(int)));


	ui.shouldRectifyBox->setChecked(true);
	connect(ui.shouldRectifyBox, SIGNAL(stateChanged(int)), this, SLOT(rectifySwap(int)));


	connect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &matcherDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
	connect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &rectifyDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));


	connect(&sgmDialog, SIGNAL(setBuffers(G12Buffer *, DisparityBuffer *, double)), cloudDialog.ui.widget, SLOT(setBuffersSlot(G12Buffer *, DisparityBuffer *, double)));

}

void StereoLab::rectifySwap(int state)
{
	printf("Swapping the rectify state\n");

	if (!ui.shouldRectifyBox->isChecked())
	{
		disconnect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &matcherDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
		disconnect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &rectifyDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
		disconnect(&rectifyDialog, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &sgmDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));


		connect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &sgmDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));

		//connect(&rectifyDialog, SIGNAL(maxDisparityChanged(int)), &sgmDialog, SLOT(maxDisparityChangedSlot(int)));
	}
	else
	{
		disconnect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &sgmDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));

		connect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &matcherDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
		connect(this, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &rectifyDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
		connect(&rectifyDialog, SIGNAL(buffersChanged(G12Buffer *, G12Buffer *)), &sgmDialog, SLOT(buffersChangedSlot(G12Buffer *, G12Buffer *)));
	}

	emit buffersChanged(buffer1, buffer2);
}


void StereoLab::reloadBuffer1()
{
	// Store the old buffer untill everybody will be notified about the change
	G12Buffer *old = buffer1;
	buffer1 = g12BufferCreateFromFile((char *)(ui.inputName1->text().toAscii().constData()));
	if (buffer1 == NULL)
	{
		QMessageBox::information(this, "Problem opening the file", QString("Could not open the file: %1").arg(ui.inputName1->text()),  "OK");
		buffer1 = old;
	}
	else
	{
		emit buffersChanged(buffer1, buffer2);
		if (old != NULL)
				g12BufferDelete(old);
	}


}

void StereoLab::reloadBuffer2()
{
	// Store the old buffer untill everybody will be notified about the change
	G12Buffer *old = buffer2;
	buffer2 = g12BufferCreateFromFile((char *)(ui.inputName2->text().toAscii().constData()));
	if (buffer2 == NULL)
	{
		QMessageBox::information(this, "Problem opening the file", QString("Could not open the file: %1").arg(ui.inputName1->text()),  "OK");
		buffer2 = old;
	}
	else
	{
		emit buffersChanged(buffer1, buffer2);
		if (old != NULL)
				g12BufferDelete(old);
	}
}


void StereoLab::activateLoadDialog1()
{
	activateLoadDialog(0);
}

void StereoLab::activateLoadDialog2()
{
	activateLoadDialog(1);
}


void StereoLab::activateLoadDialog(int num)
{
   QString s = QFileDialog::getOpenFileName(
		this,
		"Choose an input file",
		".",
#ifdef TIFF_SUPPORTED
		"Images (*.tiff *.ppm *.pgm *.raw)"
#else
		"Images (*.ppm *.pgm *.raw)"
#endif

	   );
    if (s.length() == 0)
    	return;
    if (num == 0)
    {
    	ui.inputName1->setText(s);
    	emit reloadBuffer1();
    }
    else
    {
    	ui.inputName2->setText(s);
    	emit reloadBuffer2();
    }
}

void StereoLab::setInputs(char *first, char *second)
{
	  ui.inputName1->setText(QString::fromUtf8(first));
	  ui.inputName2->setText(QString::fromUtf8(second));
}


StereoLab::~StereoLab()
{
	if (buffer1 != NULL)
		g12BufferDelete(buffer1);
	if (buffer2 != NULL)
		g12BufferDelete(buffer2);

}
