MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //connect signals of GUI elements with slots of this class
    connectSignalSlots();

    //hide advanced settings and connect signals/slots to show them
    hideAdvancedSettings();

    //initialize graphicsview
    GraphicsScene *scene = new GraphicsScene();
    ui->graphicsView->setScene(scene);
    scene->setBackgroundBrush(QBrush(Qt::darkGray));
    ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    scene->addText("Start by dragging images here.");
    ui->graphicsView->setRenderHints(QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
    ui->graphicsView->setAcceptDrops(true);

    //initialize QImage objects to store the calculated maps
    input = QImage();
    channelIntensity = QImage();
    normalmap = QImage();
    specmap = QImage();
    displacementmap = QImage();
    ssaomap = QImage();

    //initialize calctimes
    lastCalctime_normal = 0;
    lastCalctime_specular = 0;
    lastCalctime_displace = 0;
    lastCalctime_ssao = 0;

    //initialize stopQueue flag
    stopQueue = false;

    //show default status message
    ui->statusBar->showMessage("Drag images into the empty preview window to load them.");

    //hide queue progressbar
    ui->progressBar_Queue->hide();

    //if the program was opened via "open with" by the OS, extract the image paths from the arguments
    //(args[0] is the name of the application)
    QStringList args = QCoreApplication::arguments();
    if(args.size() > 1) {
        QList<QUrl> imageUrls;

        for(int i = 1; i < args.size(); i++) {
            imageUrls.append(QUrl::fromLocalFile(args[i]));
        }

        loadMultipleDropped(imageUrls);
    }
}
DTIVisualization::DTIVisualization(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	_volumeVis = new VolumeVis();
	QHBoxLayout * layout = new QHBoxLayout();
	layout->setMargin(0);
	layout->addWidget(_volumeVis);
	ui.volumeTab->setLayout(layout);

	_seedVis = new SeedVis();
	_fiberVis = new FiberVis();

	connectSignalSlots();
}
void ExperimentManager_Dialog::cleanUp()
{
	docContentStructToRun.strDocContent = "";
	docContentStructToRun.strDocExtension = "";
	docContentStructToRun.strDocHomeDir = "";
	docContentStructToRun.bIsFile = true;
	if(ExperimentManagerObj)
	{
		if ((currentExperimentState == ExperimentManager::ExperimentManager_IsStarting) || (currentExperimentState == ExperimentManager::ExperimentManager_Started))
		{
			ExperimentManagerObj->abortExperiment();
			int nRetries = 10;
			while(currentExperimentState != ExperimentManager::ExperimentManager_Stopped)
			{
				qApp->processEvents(QEventLoop::ExcludeSocketNotifiers,50);
				nRetries--;
				if (nRetries == 0)
				{
					break;//Could not stop the experiment....
				}
			}
			nRetries = nRetries;
		}		
	}
	connectSignalSlots(true);
	if(ExperimentManagerObj)
	{
		delete ExperimentManagerObj;
		ExperimentManagerObj = NULL;
	}
	if(Qml2ViewerObject)
	{
		Qml2ViewerObject->stopObject();
		delete Qml2ViewerObject;
		Qml2ViewerObject = NULL;
	}
	return;
}
bool ExperimentManager_Dialog::executeActiveDocument()
{
	if (((currentExperimentSubObjectState == Experiment_SubObject_Initialized) || (currentExperimentSubObjectState == Experiment_SubObject_Stopped)) == false)
		return false;

	QString fileSource = "";
	if(docContentStructToRun.strDocExtension == PLUGIN_EXMLDOC_EXTENSION)
	{
		if(docContentStructToRun.bIsFile)
		{
			if(docContentStructToRun.strDocContent.isEmpty())
			{
				fileSource = QFileDialog::getOpenFileName(NULL, tr("Open a Experiment file"), MainAppInfo::appExampleDirPath(), tr("Experiment Files (*.exml);;Any file (*)"));
			}
			fileSource = docContentStructToRun.strDocContent;
			if (fileSource.isEmpty())
				return false;
			QFile file(fileSource);
			if (!file.exists())
				return false;
			if(file.open(QIODevice::ReadOnly))
			{
				fileSource = file.readAll();
				file.close();
			}
			else
			{
				return false;
			}
			QFileInfo fi(file);
			QDir::setCurrent(fi.canonicalPath());	
		}
		else
		{
			fileSource = docContentStructToRun.strDocContent;
			//QString dd = MainAppInfo::appDirPath();
			QDir::setCurrent(docContentStructToRun.strDocHomeDir);
		}
		ExperimentManagerObj = new ExperimentManager(this);
		connectSignalSlots(false);
		if(ExperimentManagerObj->loadExperiment(fileSource,docContentStructToRun.bIsFile))
		{
			return ExperimentManagerObj->runExperiment();
		}
		else
		{
			LogMessage("Error: Could not execute the document!");
		}
	}
	else if (docContentStructToRun.strDocExtension == PLUGIN_QMLDOC_EXTENSION)
	{
		if(docContentStructToRun.bIsFile)
		{
			if(docContentStructToRun.strDocContent.isEmpty())
			{
				fileSource = QFileDialog::getOpenFileName(NULL, tr("Open a QtQuick File"), MainAppInfo::appExampleDirPath(), tr("QtQuick Files (*.qml);;Any file (*)"));
			}
			fileSource = docContentStructToRun.strDocContent;
			if (fileSource.isEmpty())
				return false;
			QFile file(fileSource);
			if (!file.exists())
				return false;

			if(file.open(QIODevice::ReadOnly))
			{
				fileSource = file.readAll();
				file.close();
			}
			else
			{
				return false;
			}
			QFileInfo fi(file);
			QDir::setCurrent(fi.canonicalPath());			
		}
		else
		{
			fileSource = docContentStructToRun.strDocContent;
			//QString dd = MainAppInfo::appDirPath();
			QDir::setCurrent(docContentStructToRun.strDocHomeDir);
		}

		if (fileSource.isEmpty() == false)
		{
			QRegExp tmpRegExp;
			tmpRegExp.setPatternSyntax(QRegExp::Wildcard);
			tmpRegExp.setCaseSensitivity(Qt::CaseInsensitive);
			tmpRegExp.setPattern("import QtQuick 1.");

			if(fileSource.contains(tmpRegExp))
			{
				qWarning() << __FUNCTION__ << "::QtQuick version 1.* is not supported anymore, please update the *.qml document to QtQuick version 2.* or later!";
				return false;
			}
			else
			{
				tmpRegExp.setPattern("import QtQuick 2.");
				if(fileSource.contains(tmpRegExp))
				{
					if(Qml2ViewerObject)
					{
						Qml2ViewerObject->stopObject();
						delete Qml2ViewerObject;
						Qml2ViewerObject = NULL;
					}
					Qml2ViewerObject = new QML2Viewer(this);
					connectSignalSlots(false);
					if(Qml2ViewerObject->executeQML2Document(docContentStructToRun.strDocContent,docContentStructToRun.bIsFile))
					{
						Qml2ViewerObject->startObject();
						return true;
					}
					return false;
				}
				else
				{
					qWarning() << __FUNCTION__ << "::Could not determine QtQuick version, no \"import QtQuick\" statement found!";
					return false;
				}
			}
		}
	}
	return false;
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    lastCalctime_normal(0),
    lastCalctime_specular(0),
    lastCalctime_displace(0),
    lastCalctime_ssao(0),
    stopQueue(false)
{
    ui->setupUi(this);

    supportedImageformats << "*.png" << "*.jpg" << "*.jpeg" << "*.tiff"
                          << "*.tif" << "*.ppm" << "*.bmp"  << "*.xpm"
                          << "*.tga" << "*.gif";

    //connect signals of GUI elements with slots of this class
    connectSignalSlots();

    //hide advanced settings and connect signals/slots to show them
    hideAdvancedSettings();

    //initialize graphicsview
    GraphicsScene *scene = new GraphicsScene();
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    scene->addText("Start by dragging images here.");
    ui->graphicsView->setRenderHints(QPainter::HighQualityAntialiasing
                                     | QPainter::SmoothPixmapTransform);
    ui->graphicsView->setAcceptDrops(true);

    //show default status message
    ui->statusBar->showMessage("Drag images into the empty preview window to load them.");

    //hide queue progressbar
    ui->progressBar_Queue->hide();
    
    // SSAO map generator is not ready yet, remove it from the UI
    ui->tabWidget->removeTab(4);

    //default UI colors
    useCustomUiColors = false;
    uiColorMainDefault = QColor("#444");
    uiColorTextDefault = QColor("#eee");
    uiColorGraphicsViewDefault = QColor(Qt::darkGray);
    uiColorMain = uiColorMainDefault;
    uiColorText = uiColorTextDefault;
    uiColorGraphicsView = uiColorGraphicsViewDefault;

    //read last window position and color settings from registry
    readSettings();

    //set UI colors
    setUiColors();

    //if the program was opened via "open with" by the OS,
    //extract the image paths from the arguments
    QStringList args = QCoreApplication::arguments();
    if(args.size() > 1) {
        QList<QUrl> imageUrls;

        for(int i = 1; i < args.size(); i++) {
            imageUrls.append(QUrl::fromLocalFile(args[i]));
        }

        loadMultipleDropped(imageUrls);
    }
}