#include "CameraPreview.h"
#include "CameraThread.h"
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QLabel>
#include <QCloseEvent>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <QPropertyEditorWidget.h>

#include <QDebug>

CameraPreview::CameraPreview(CameraThread* thread, QWidget *parent, Qt::WindowFlags flags)
	: QWidget(parent, flags)
{
	ui.setupUi(this);
	QGraphicsScene *scene = new QGraphicsScene(this);
	ui.graphicsView->setScene(scene);
	ui.graphicsView->show();
	contextMenu=  new QMenu(this);
	actionProperties=contextMenu->addAction("Wlasciwosci");
	actionClose=contextMenu->addAction("Zamknij");
	connect(actionProperties,SIGNAL(triggered()),this,SLOT(Properties()));
	connect(actionClose,SIGNAL(triggered()),this,SLOT(close()));

	ct = thread;

	//ui.propertyEditor->addObject(ct->cameraObject);
	//ui.propertyEditor->addObject(ct->cameraObject->cam);
	dlg=new QDialog(this);
	QPropertyEditorWidget *prop=new QPropertyEditorWidget(dlg);
	QVBoxLayout *verticalLayout;
	verticalLayout = new QVBoxLayout(dlg);
	verticalLayout->addWidget(prop);
    
	prop->addObject(ct->cameraObject);
	prop->addObject(ct->cameraObject->cam);
    
//	prop->addObject(ct->cameraObject->markersGenerator);
}

CameraPreview::~CameraPreview()
{
	//disconnect ( ct->cameraObject, SIGNAL( frameOut(cv::Mat*,int,int) ), this, SLOT( frameIn(cv::Mat*,int,int) ));

	//emit closing(this);

}
void CameraPreview::contextMenuRequested(QPoint point)
		{
			contextMenu->popup(this->mapToGlobal(point));
			
		}
void CameraPreview::contextMenuEvent ( QContextMenuEvent * event )
{

contextMenu->popup(this->mapToGlobal(event->pos()));
			
}
void CameraPreview::Properties() {
	dlg->show();
	//ui.propertyEditor->addObject(ct->cameraObject);
	//ui.propertyEditor->addObject(ct->cameraObject->cam);
}
void CameraPreview::frameIn(cv::Mat* frame, int width, int height) {
	QImage img(width, height, QImage::Format_RGB888);
	memcpy(img.bits(),frame->data,width*height*3);
    img = img.rgbSwapped();
	//img.loadFromData(frame->ptr(),width*height*4);
	/*for(int i=0; i<width; ++i)
	{
		for(int j=0; j<height; ++j)
		{
			img.setPixel(i,j,qRgb(frame->data[j*width+i], frame->data[j*width+i], frame->data[j*width+i]));
		}
	}*/
	QPixmap item = QPixmap::fromImage(img);
	//item.loadFromData(frame->ptr(),width*height*3);
	//memcpy(item.data_ptr().data(), frame->ptr(), width*height*3);
	
	ui.graphicsView->scene()->clear();
	ui.graphicsView->scene()->addPixmap(item);
	ui.graphicsView->scene()->setSceneRect(0,0,width,height);
	ui.graphicsView->fitInView(ui.graphicsView->scene()->sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
	ui.graphicsView->show();

	delete frame;
}

void CameraPreview::closeEvent(QCloseEvent * event)
{	
	if(ct->cameraObject->isRunning)
	{
		event->ignore();
		return;
	}

	disconnect ( ct->cameraObject, SIGNAL( frameOut(cv::Mat*,int,int) ), this, SLOT( frameIn(cv::Mat*,int,int) ));

	emit closing(this);
	event->accept();
}
