/*
QtWagon: a project about 3D objects.
Science and technology promotion license applied. Third party license automatically cascaded.
Zhikai Wang/ www.heteroclinic.net 2013
You can do anything with this file or any file(s) published as part QtWagon project, given this header is kept.
*/

#include "qtCamera.h"

//#include "qtMovingObject.h"
camera * qtCamera::getCurrentCamera () 
		{
			return currentCamera;
};
void qtCamera::emitGLWidgetOrders(int op,float val) {
	//std::cout<<"qtCamera::emitGLWidgetOrders(int op,float val)"<<std::endl;
	emit
		 sigCAM3dMoveOrders(op,val);
};
//valDialZoomPrev,valDialFocalLengthPrev,valDialSwirlUpDownPrev,valDialRightLeftPrev;
void qtCamera::dialZoom_valueChanged(int nval) {
	//std::cout<<"qtCamera::dialZoom_valueChanged(int nval) "<<nval<<std::endl;
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valDialZoomPrev, dialZoom->maximum());
	//emit
	//	sigCAM3dMoveOrders(enum3dops::zoom,delta);
	emitGLWidgetOrders(enum3dops::zoom,delta);
};
void qtCamera::dialFocalLength_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valDialFocalLengthPrev, dialFocalLength->maximum());
	//emit
	//	//sig3dMoveOrders(enum3dops::wfb,delta);
	//	sigCAM3dMoveOrders(enum3dops::cfb,delta);
	emitGLWidgetOrders(enum3dops::cfb,delta);

};
void qtCamera::dialSwirlUpDown_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valDialSwirlUpDownPrev,dialSwirlUpDown->maximum());
	//emit
	//	//sig3dMoveOrders(enum3dops::wfb,delta);
	//	sigCAM3dMoveOrders(enum3dops::swirlud,delta);
	emitGLWidgetOrders(enum3dops::swirlud,delta);
};
void qtCamera::dialRightLeft_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valDialRightLeftPrev,dialRightLeft->maximum());
	//emit
	//	//sig3dMoveOrders(enum3dops::wfb,delta);
	//	sigCAM3dMoveOrders(enum3dops::swirlrl,delta);
	emitGLWidgetOrders(enum3dops::swirlrl,delta);
};
void qtCamera::dialpanLeftRight_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valdialpanLeftRightPrev,dialpanLeftRight->maximum());
	delta *= currentCamera->default_deta_pan;
	currentCamera->panLeftRight(delta);

	//emit
	//	//sigGLWidgetSingletonUpdateGL();
	//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
	////setControlsByWagon();
	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
};
void qtCamera::dialpanUpDown_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valdialpanUpDownPrev,dialpanUpDown->maximum());
	delta *= currentCamera->default_deta_pan;
	currentCamera->panUpDown(delta);

	//emit
	//	//sigGLWidgetSingletonUpdateGL();
	//emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
	////setControlsByWagon();
	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
};

void qtCamera::dialpanLevelForwardBackward_valueChanged(int nval){
	if (NULL == currentCamera)
		return;
	GLfloat delta = dialDerivative(nval,valdialpanLevelForwardBackwardPrev,dialpanLevelForwardBackward->maximum());
	delta *= currentCamera->default_deta_pan;
	currentCamera->panLevelForwardBackward(delta);

	//emit
	//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
	////	sigGLWidgetSingletonUpdateGL();
	////setControlsByWagon();
		emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
};



void qtCamera::slotCAMUpdateWagonValueOnly() {
	setControlsByWagon();
};

//zoom,
//cfb, // camera forward/backward
//swirlud,// up/down
//swirlrl // right/left

//myfloat default_deta_fb;
//myfloat default_deta_fovy;
//myfloat default_deta_osize;
//myfloat default_deta_swirl;
void qtCamera::editZoomDeltaPerspective_editingFinished() {
	bool OK = false;
	double val =editZoomDeltaPerspective->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			currentCamera->default_deta_fovy = val;

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera::editZoomDeltaOrthogonal_editingFinished(){
	bool OK = false;
	double val =editZoomDeltaOrthogonal->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			currentCamera->default_deta_osize = val;

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera::editLookAtRadiusDelta_editingFinished() {
	bool OK = false;
	double val =editLookAtRadiusDelta->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			currentCamera->default_deta_fb = val;

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};
void qtCamera::editdefault_deta_pan_editingFinished() {
	bool OK = false;
	double val =editdefault_deta_pan->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			currentCamera->default_deta_pan = val;

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};



void qtCamera::editSwirlDelta_editingFinished() {
	bool OK = false;
	double val =editSwirlDelta->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			currentCamera->default_deta_swirl = val;

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};

void qtCamera::editOrthorgonalFarZ_editingFinished(){
	bool OK = false;
	double val =editOrthorgonalFarZ->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if (val > currentCamera->soStructOrthorgonal.near_z  && val > 0.0f) {
				currentCamera->soStructOrthorgonal.far_z = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};


void qtCamera::editOrthorgonalNearZ_editingFinished(){
	bool OK = false;
	double val =editOrthorgonalNearZ->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if (val < currentCamera->soStructOrthorgonal.far_z  && val > 0.0f) {
				currentCamera->soStructOrthorgonal.near_z = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};
void qtCamera::editOsize_editingFinished(){
	bool OK = false;
	double val =editOsize->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if (val <= currentCamera->soStructOrthorgonal.o_size_limit && val > 0.0f) {
				currentCamera->soStructOrthorgonal.o_size1 = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera::editOsizeLimit_editingFinished(){
	bool OK = false;
	double val =editOsizeLimit->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if ( val > 0.0f) {
				currentCamera->soStructOrthorgonal.o_size_limit = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
//typedef struct _StructOrthorgonal {
//	GLfloat o_size1;
//	GLfloat o_size2;
//	GLfloat o_size3;
//	GLfloat o_size4;
//	GLfloat near_z;
//	GLfloat far_z;
//	GLfloat o_size_limit;
//} StructOrthorgonal, * p_StructOrthorgonal;
//soStructOrthorgonal
void qtCamera::editSafeLookAtRadius_editingFinished(){
	bool OK = false;
	double val =editSafeLookAtRadius->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if ( val > 0.0f) {
				currentCamera->safe_lookat_radius = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};


void qtCamera::editPerspectiveFarZ_editingFinished() {
	bool OK = false;
	double val =editPerspectiveFarZ->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->spStructPserpective.far_z = val; // you can set a value other than values below even a negative one, but you don't want things become weired.
			if (val > currentCamera->spStructPserpective.near_z  && val > 0.0f) {
				currentCamera->spStructPserpective.far_z = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};


void qtCamera::editPerspectiveFovy_editingFinished(){
	bool OK = false;
	double val =editPerspectiveFovy->text().toDouble(&OK);   
	if (OK ) {
		if (NULL != currentCamera)  { // normally the range is [fovy_min, fovy_max] \subset (0,180)
			if (val <=  currentCamera->spStructPserpective.fovy_max  && val >= currentCamera->spStructPserpective.fovy_min && val > 0.0f) {
				currentCamera->spStructPserpective.fovy = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};

void qtCamera::editPerspectiveFovyMin_editingFinished(){
	bool OK = false;
	double val =editPerspectiveFovyMin->text().toDouble(&OK);   
	if (OK ) {
		if (NULL != currentCamera)  { // normally the range is [fovy_min, fovy_max] \subset (0,180) also less than the current fovy
			if (val <  currentCamera->spStructPserpective.fovy_max  && val > 0.0f) {
				if ( val >  currentCamera->spStructPserpective.fovy )
					currentCamera->spStructPserpective.fovy = val;
				currentCamera->spStructPserpective.fovy_min = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();// this is a bound value , it doesn't have consequence on the display. same with max.
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};


void qtCamera::editPerspectiveFovyMax_editingFinished(){
	bool OK = false;
	double val =editPerspectiveFovyMax->text().toDouble(&OK);   
	if (OK ) {
		if (NULL != currentCamera)  { // normally the range is [fovy_min, fovy_max] \subset (0,180) also less than the current fovy
			if (val < 180.0f && val >  currentCamera->spStructPserpective.fovy_min  && val > 0.0f) {
				if ( val <  currentCamera->spStructPserpective.fovy )
					currentCamera->spStructPserpective.fovy = val;

				currentCamera->spStructPserpective.fovy_max = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();// this is a bound value , it doesn't have consequence on the display. same with max.
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};


};
//spStructPserpective
//typedef struct _StructPserpective {
//	GLfloat fovy;
//	GLfloat near_z;
//	GLfloat far_z;
//	GLfloat fovy_min;
//	GLfloat fovy_max;
//
//} StructPserpective, * p_StructPserpective;

//editPerspectiveFarZ->setText( QString("%1").arg(currentCamera->spStructPserpective.far_z) );
void qtCamera::editPerspectiveFovyNearZ_editingFinished(){
	bool OK = false;
	double val = editPerspectiveFovyNearZ->text().toDouble(&OK);   
	if (OK ) {
		if (NULL != currentCamera)  { 
			if (val <=  currentCamera->spStructPserpective.far_z  && val > 0.0f) {
				currentCamera->spStructPserpective.near_z = val;
				//emit
				//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				////	sigGLWidgetSingletonUpdateGL();// this is a bound value , it doesn't have consequence on the display. same with max.
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};

};


void qtCamera:: editFocusPositionx_editingFinished() {
	bool OK = false;
	double val =editFocusPositionx->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			//currentCamera->focusSize = val;
			mathPoint<GLfloat> tmpt =  currentCamera->getLookedAt();
			mathPoint<GLfloat> tmpt2 = mathPoint<GLfloat>(val,tmpt.gety(),tmpt.getz()) ;
			GLfloat ftmp = (tmpt2-tmpt).norm();
			if (ftmp >=  currentCamera->safe_lookat_radius) {
				currentCamera->orthorganalizeToWorld();
				currentCamera->setLookedAt ( tmpt2);
				//emit
				//	//emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				//	emitGLWidgetOrders(enum3dops::focus_x,val);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}

		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera:: editFocusPositionz_editingFinished() {
	bool OK = false;
	double val =editFocusPositionz->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			mathPoint<GLfloat> tmpt =  currentCamera->getLookedAt();
			mathPoint<GLfloat> tmpt2 = mathPoint<GLfloat>(tmpt.getx(),tmpt.gety(),val) ;
			GLfloat ftmp = (tmpt2-tmpt).norm();
			if (ftmp >=  currentCamera->safe_lookat_radius) {
				currentCamera->orthorganalizeToWorld();
				currentCamera->setLookedAt (tmpt2 );
				//emit
				//	//emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				//emitGLWidgetOrders(enum3dops::focus_z,val);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}

			//	//currentCamera->focusSize = val;
			//mathPoint<GLfloat> tmpt =  currentCamera->getLookedAt();
			//currentCamera->setLookedAt (mathPoint<GLfloat>(tmpt.getx(),tmpt.gety(),val) ) ;
			//emit
			//	sigUpdateGL();
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera:: editFocusPositiony_editingFinished() {
	bool OK = false;
	double val =editFocusPositiony->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera)  {
			mathPoint<GLfloat> tmpt =  currentCamera->getLookedAt();
			mathPoint<GLfloat> tmpt2 = mathPoint<GLfloat>(tmpt.getx(),val,tmpt.getz()) ;
			GLfloat ftmp = (tmpt2-tmpt).norm();
			if (ftmp >=  currentCamera->safe_lookat_radius) {
				currentCamera->orthorganalizeToWorld();
				currentCamera->setLookedAt ( tmpt2);
				//emit
				//	//emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
				//	emitGLWidgetOrders(enum3dops::focus_y,val);
				////	sigGLWidgetSingletonUpdateGL();
				////setControlsByWagon();
				emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			} else {
				setControlsByWagon();
			}

			//	//currentCamera->focusSize = val;
			//mathPoint<GLfloat> tmpt =  currentCamera->getLookedAt();
			//currentCamera->setLookedAt (mathPoint<GLfloat>(tmpt.getx(),val,tmpt.getz())) ;
			//emit
			//	sigUpdateGL();
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();
	};
};
void qtCamera::editFocusSize_editingFinished() {
	//??
	bool OK = false;
	double val =editFocusSize->text().toDouble(&OK);   // val == 1234.56 
	if (OK ) {
		if (NULL != currentCamera && val > 0)  {
			currentCamera->focusSize = val;
			//emit
			//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			////	sigGLWidgetSingletonUpdateGL();
			////setControlsByWagon();
			emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
		} else {
			setControlsByWagon();
		}
	} else {
		setControlsByWagon();

	};
};

void qtCamera::checkShowFocus_stateChanged(int val) {
	if (NULL != currentCamera) {
		if (val == Qt::Unchecked)
			currentCamera->showFocus = false;
		else
			currentCamera->showFocus = true;
		//emit
		//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
		////	sigGLWidgetSingletonUpdateGL();
		////setControlsByWagon();
		emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
	}

};

void qtCamera::radioPerspective_toggled(bool checked) {
	if (checked) {
		if (NULL != currentCamera) {
			currentCamera->orthorgonal_disp = false;
			// has no use, we must resize or do sth in paintGL
			//emit
			//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			////	sigGLWidgetSingletonUpdateGL();
			////setControlsByWagon();
			//////this->update();
			emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
		}
	}
};
void qtCamera::radioOrhorgonal_toggled(bool checked) {
	if (checked) {
		if (NULL != currentCamera) {
			currentCamera->orthorgonal_disp =true;
			//emit
			//	emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
			////	sigGLWidgetSingletonUpdateGL();
			////setControlsByWagon();
			////////this->update();
			emitGLWidgetOrders(enum3dops::updateGLonly,1.0f);
		}
	}
};
void qtCamera::setControlsByWagon(bool inited) {
	//qtMovingObject::setControlsByWagon();
	qtMovingObject::setControls();
	if (NULL != currentCamera) {
		if (! inited) {
			valDialZoomPrev =0,valDialFocalLengthPrev=0,valDialSwirlUpDownPrev=0,valDialRightLeftPrev=0;
				valdialpanLeftRightPrev =0,valdialpanUpDownPrev =0, valdialpanLevelForwardBackwardPrev =0;
		}
		//radioVertices->setChecked(true);
		if (currentCamera->orthorgonal_disp)
			radioOrhorgonal->setChecked(true);
		else
			radioPerspective->setChecked(true);

		if (currentCamera->showFocus ){
			checkShowFocus->setCheckState (Qt::Checked ) ;
		} else {
			checkShowFocus->setCheckState (Qt::Unchecked ) ;
		}
		mathPoint<GLfloat> lkt =currentCamera->getLookedAt();
		editFocusPositionx->setText( QString("%1").arg( formatDouble(lkt.getx()).c_str() ) );
		editFocusPositiony->setText( QString("%1").arg( formatDouble(lkt.gety()).c_str() ) );
		editFocusPositionz->setText( QString("%1").arg( formatDouble(lkt.getz()).c_str() ) );

		editFocusSize->setText( QString("%1").arg(      formatDouble(currentCamera->focusSize).c_str() ) );
		labelLookAtRadius->setText( QString("%1").arg(  formatDouble(currentCamera->lookat_radius).c_str() ) );

		//spStructPserpective
		//typedef struct _StructPserpective {
		//	GLfloat fovy;
		//	GLfloat near_z;
		//	GLfloat far_z;
		//	GLfloat fovy_min;
		//	GLfloat fovy_max;
		//
		//} StructPserpective, * p_StructPserpective;

		editPerspectiveFarZ->setText( QString("%1").arg(formatDouble(currentCamera->spStructPserpective.far_z).c_str()) );
		editPerspectiveFovy->setText( QString("%1").arg(formatDouble(currentCamera->spStructPserpective.fovy).c_str()) );
		editPerspectiveFovyMin->setText( QString("%1").arg(formatDouble(currentCamera->spStructPserpective.fovy_min).c_str()) );
		editPerspectiveFovyMax->setText( QString("%1").arg(formatDouble(currentCamera->spStructPserpective.fovy_max).c_str()) );
		editPerspectiveFovyNearZ->setText( QString("%1").arg(formatDouble(currentCamera->spStructPserpective.near_z).c_str()) );


		//typedef struct _StructOrthorgonal {
		//	GLfloat o_size1;// we only use o_size1;
		//	GLfloat o_size2;
		//	GLfloat o_size3;
		//	GLfloat o_size4;
		//	GLfloat near_z;
		//	GLfloat far_z;
		//	GLfloat o_size_limit;
		//} StructOrthorgonal, * p_StructOrthorgonal;
		//soStructOrthorgonal
		editOrthorgonalFarZ->setText( QString("%1").arg(formatDouble(currentCamera->soStructOrthorgonal. far_z).c_str() ) );
		editOrthorgonalNearZ->setText( QString("%1").arg(formatDouble(currentCamera->soStructOrthorgonal. near_z).c_str() ) );
		editOsize->setText( QString("%1").arg(formatDouble(currentCamera->soStructOrthorgonal.o_size1).c_str() ) );
		editOsizeLimit->setText( QString("%1").arg(formatDouble(currentCamera->soStructOrthorgonal.  o_size_limit).c_str() ) );
		editSafeLookAtRadius->setText( QString("%1").arg(formatDouble(currentCamera->safe_lookat_radius).c_str() ) );

		//myfloat default_deta_fb;
		////mybool free_camera;
		//myfloat default_deta_fovy;
		//myfloat default_deta_osize;
		editZoomDeltaPerspective->setText( QString("%1").arg(formatDouble(currentCamera->default_deta_fb).c_str()) );
		editZoomDeltaOrthogonal->setText( QString("%1").arg(formatDouble(currentCamera->default_deta_fovy).c_str()) );
		editLookAtRadiusDelta->setText( QString("%1").arg(formatDouble(currentCamera->default_deta_osize).c_str()) );
		editSwirlDelta->setText( QString("%1").arg(formatDouble(currentCamera->default_deta_swirl).c_str()) );;
		editdefault_deta_pan->setText( QString("%1").arg(formatDouble(currentCamera->default_deta_pan).c_str()) );;


		//mathPoint<GLfloat> pos = mvopt->getPosition();
		//
		//GLfloat glbPosx_f  = pos.getx(),glbPosy_f= pos.gety(),glbPosz_f= pos.getz();
		//editGlbPosx->setText( QString("%1").arg(glbPosx_f) );
		//editGlbPosy->setText( QString("%1").arg(glbPosy_f) );
		//editGlbPosz->setText( QString("%1").arg(glbPosz_f) );

		//mathVector<GLfloat> tmpmv =  mvopt->getUpVectorValue() ;
		//editUpx->setText( QString("%1").arg(tmpmv.getx()) );
		//editUpy->setText( QString("%1").arg(tmpmv.gety()) );
		//editUpz->setText( QString("%1").arg(tmpmv.getz()) );

		//tmpmv =  mvopt->getLeftVectorValue() ;
		//editLeftx->setText( QString("%1").arg(tmpmv.getx()) );
		//editLefty->setText( QString("%1").arg(tmpmv.gety()) );
		//editLeftz->setText( QString("%1").arg(tmpmv.getz()) );

		//tmpmv =  mvopt->getFrontVectorValue() ;
		//editFrontx->setText( QString("%1").arg(tmpmv.getx()) );
		//editFronty->setText( QString("%1").arg(tmpmv.gety()) );
		//editFrontz->setText( QString("%1").arg(tmpmv.getz()) );
	} else {
		radioOrhorgonal->setChecked(true);
		checkShowFocus->setCheckState (Qt::Checked ) ;
		editFocusPositionx->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editFocusPositiony->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editFocusPositionz->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editFocusSize->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		labelLookAtRadius->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );


		editPerspectiveFarZ->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editPerspectiveFovy->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editPerspectiveFovyMin->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editPerspectiveFovyMax->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editPerspectiveFovyNearZ->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editOrthorgonalFarZ->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editOrthorgonalNearZ->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editOsize->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editOsizeLimit->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editSafeLookAtRadius->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editZoomDeltaPerspective->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editZoomDeltaOrthogonal->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editLookAtRadiusDelta->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editSwirlDelta->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
		editdefault_deta_pan->setText( QString("%1").arg(formatDouble(  0.0).c_str()) );
	}
};

//qtCamera::qtCamera(QWidget * newScreen)
qtCamera::qtCamera()
//	:qtMovingObject(newScreen)  
{
	//myBox = new QGroupBox(QObject::tr("Moving object panel"));
	//myLayout = new QVBoxLayout;

	int insertindex = 0;
	currentCamera = NULL;
	//currentCamera = new camera;
	//mvopt = currentCamera;

	int ncol = 1,nrow = 1;//span number
	//g->setTitle("&User information");
	//void QBoxLayout::insertWidget ( int index, QWidget * widget, int stretch = 0, Qt::Alignment alignment = 0 )

	//myLayout2->addWidget(qtMovingObject::getMyBox());
	//qtMovingObject::getMyLayout() ;
	myBox2 = qtMovingObject::getMyBox();
	myLayout2 = qtMovingObject::getMyLayout() ;
	myBox2->setTitle(QObject::tr("Camera panel"));


	//myBox2 = new QGroupBox(QObject::tr("Camera panel"));

	//myLayout2 = new QVBoxLayout;
	//myBox2->setLayout(myLayout2);

	//QRadioButton * radioPerspective, * radioOrhorgonal;
	radioPerspective = new QRadioButton (tr("Perspective"));
	radioOrhorgonal = new QRadioButton (tr("Orthogonal"));
	// connect(radioPerspective , SIGNAL(toggled(bool)), this, SLOT(radioPerspective_toggled(bool)));
	//  connect(radioOrhorgonal , SIGNAL(toggled(bool)), this, SLOT(radioOrhorgonal_toggled(bool)));
	//
	QGroupBox * boxCameraSettings1 = new QGroupBox(tr("Camera type"));
	QVBoxLayout *layoutCameraSettings1 = new QVBoxLayout;
	boxCameraSettings1->setLayout(layoutCameraSettings1);


	QGroupBox * boxPespOrOrtho = new QGroupBox;
	QGridLayout *layoutPespOrOrtho = new QGridLayout;
	boxPespOrOrtho->setLayout(layoutPespOrOrtho);
	radioOrhorgonal->setChecked(true);//d ?? efault values has a separate setfunction.
	ncol  = 1, nrow =1;
	layoutPespOrOrtho->addWidget( radioOrhorgonal,0,0,ncol,nrow);
	layoutPespOrOrtho->addWidget( radioPerspective,0,1,ncol,nrow);

	layoutCameraSettings1->addWidget(boxPespOrOrtho);

	QGroupBox * boxFocus = new QGroupBox;
	QGridLayout *layoutFocus = new QGridLayout;
	boxFocus->setLayout(layoutFocus);
	//QCheckBox * checkShowFocus;
	checkShowFocus = new QCheckBox (tr("Show focus"));	
	//QLabel* labelLookAtRadius;
	labelLookAtRadius = new QLabel;

	//QLineEdit * editFocusPositionx, * editFocusPositiony, * editFocusPositionz;
	editFocusPositionx= createDoubleEditLine();	 editFocusPositiony= createDoubleEditLine();	 editFocusPositionz= createDoubleEditLine();
	//QLineEdit *   editFocusSize;
	editFocusSize= createDoubleEditLine(); 



	layoutFocus->addWidget(checkShowFocus,0,0,1,1);
	layoutFocus->addWidget(new QLabel(tr("Focus size:")),2,0,1,1);
	layoutFocus->addWidget(editFocusSize,2,1,1,1);

	layoutFocus->addWidget(new QLabel(tr("Focus position:")),3,0,1,1);
	layoutFocus->addWidget(editFocusPositionx,4,0,1,1);
	layoutFocus->addWidget(editFocusPositiony,4,1,1,1);
	layoutFocus->addWidget(editFocusPositionz,4,2,1,1);
	
	layoutFocus->addWidget(new QLabel(tr("Focal length/Radius")),5,0,1,1);
	layoutFocus->addWidget(labelLookAtRadius,5,1,1,1);

	layoutCameraSettings1->addWidget( boxFocus);
	
	
	//myLayout2->addWidget(boxCameraSettings1);
	//void QBoxLayout::insertWidget ( int index, QWidget * widget, int stretch = 0, Qt::Alignment alignment = 0 )
	myLayout2->insertWidget(insertindex++,boxCameraSettings1);


	//- perspectiveGroup
	//QLineEdit *  editPerspectiveFarZ, *  editPerspectiveFovy, * editPerspectiveFovyMin, *  editPerspectiveFovyMax, *  editPerspectiveFovyNearZ;
	editPerspectiveFarZ= createDoubleEditLine(); editPerspectiveFovy= createDoubleEditLine();editPerspectiveFovyMin= createDoubleEditLine();
	editPerspectiveFovyMax= createDoubleEditLine(); editPerspectiveFovyNearZ = createDoubleEditLine();
	QGroupBox * boxPerspectivProperties = new QGroupBox(tr("Perspective camera properties"));
	QGridLayout *layoutPerspectivProperties = new QGridLayout;
	boxPerspectivProperties->setLayout(layoutPerspectivProperties);


	layoutPerspectivProperties->addWidget(new QLabel(tr("Far-Z:")),0,0,1,1);
	layoutPerspectivProperties->addWidget(new QLabel(tr("Fovy:")),0,1,1,1);
	layoutPerspectivProperties->addWidget(new QLabel(tr("Fovy Min:")),0,2,1,1);

	layoutPerspectivProperties->addWidget(editPerspectiveFarZ,1,0,1,1);
	
	layoutPerspectivProperties->addWidget(editPerspectiveFovy,1,1,1,1);

	layoutPerspectivProperties->addWidget(editPerspectiveFovyMin,1,2,1,1);

	layoutPerspectivProperties->addWidget(new QLabel(tr("Fovy Max:")),2,0,1,1);
		layoutPerspectivProperties->addWidget(new QLabel(tr("Near-Z:")),2,1,1,1);

	layoutPerspectivProperties->addWidget(editPerspectiveFovyMax,3,0,1,1);
	layoutPerspectivProperties->addWidget(editPerspectiveFovyNearZ,3,1,1,1);

	//myLayout2->addWidget(boxPerspectivProperties);
	myLayout2->insertWidget(insertindex++,boxPerspectivProperties);
	//myLayout2->insertWidget(1,boxCameraSettings1);


	////- orthorgonalGroup
	//QLineEdit *  editOrthorgonalFarZ, *  editOrthorgonalNearZ, * editOsizeLimit, *editSafeLookAtRadius;
	editOrthorgonalFarZ= createDoubleEditLine();  editOrthorgonalNearZ= createDoubleEditLine(); editOsize= createDoubleEditLine(); 
	editOsizeLimit= createDoubleEditLine(); 	editSafeLookAtRadius= createDoubleEditLine(); 

	QGroupBox * boxOrthorgonalProperties = new QGroupBox(tr("Orthorgonal camera properties"));
	QGridLayout *layoutOrthorgonalProperties = new QGridLayout;
	boxOrthorgonalProperties->setLayout(layoutOrthorgonalProperties);
	layoutOrthorgonalProperties->addWidget(new QLabel(tr("Far-Z:")),0,0,1,1);
	layoutOrthorgonalProperties->addWidget(new QLabel(tr("Near-Z:")),0,1,1,1);
	layoutOrthorgonalProperties->addWidget(new QLabel(tr("Size:")),0,2,1,1);

	layoutOrthorgonalProperties->addWidget(editOrthorgonalFarZ,1,0,1,1);
	layoutOrthorgonalProperties->addWidget(editOrthorgonalNearZ,1,1,1,1);
	layoutOrthorgonalProperties->addWidget(editOsize,1,2,1,1);

	layoutOrthorgonalProperties->addWidget(new QLabel(tr("Size limit:")),2,0,1,1);
		layoutOrthorgonalProperties->addWidget(new QLabel(tr("Safe radius:")),2,1,1,1);
	layoutOrthorgonalProperties->addWidget(editOsizeLimit,3,0,1,1);

	layoutOrthorgonalProperties->addWidget(editSafeLookAtRadius,3,1,1,1);



	//myLayout2->addWidget(boxOrthorgonalProperties);

	myLayout2->insertWidget(insertindex++,boxOrthorgonalProperties);

	//QLineEdit *  editLookedAtx, *editLookedAty, *editLookedAtz; // we use focus
	// editLookedAtx= createDoubleEditLine();  editLookedAty= createDoubleEditLine(); editLookedAtz= createDoubleEditLine(); 

	//QDial  *  dialZoom, *dialFocalLength, * dialSwirlUpDown, * dialRightLeft;
	dialZoom = createDial();dialFocalLength = createDial();dialSwirlUpDown = createDial();dialRightLeft = createDial();
	dialpanLeftRight = createDial();
	dialpanUpDown = createDial();
	dialpanLevelForwardBackward = createDial();
	QGroupBox * boxDials = new QGroupBox(tr("Dials for Zoom, focal length adjust, swirl up/down, right/left"));
	QGridLayout *layoutDials = new QGridLayout;boxDials->setLayout(layoutDials);


	layoutDials->addWidget(dialZoom,0,0);
	layoutDials->addWidget(dialFocalLength,0,1);
	layoutDials->addWidget(dialSwirlUpDown,1,0);
	layoutDials->addWidget(dialRightLeft ,1,1);
		layoutDials->addWidget(dialpanLeftRight ,2,0);
			layoutDials->addWidget(dialpanUpDown ,2,1);
				layoutDials->addWidget(dialpanLevelForwardBackward ,3,0);
	//myLayout2->addWidget(boxDials);
	myLayout2->insertWidget(insertindex++,boxDials);



	//QLineEdit *  editZoomDeltaPerspective, * editZoomDeltaOrthogonal, * editLookAtRadiusDelta,* editSwirlDelta;;
	editZoomDeltaPerspective= createDoubleEditLine();  editZoomDeltaOrthogonal= createDoubleEditLine(); editLookAtRadiusDelta= createDoubleEditLine(); 
	editdefault_deta_pan= createDoubleEditLine();
	editSwirlDelta = createDoubleEditLine();
	QGroupBox * boxDialsDelta = new QGroupBox(tr("Dials' delta:"));
	QGridLayout *layoutDialsDelta = new QGridLayout;
	boxDialsDelta->setLayout(layoutDialsDelta);

	layoutDialsDelta->addWidget(new QLabel(tr("Perspective:")),0,0,1,1);
	layoutDialsDelta->addWidget(new QLabel(tr("Orthogonal:")),0,1,1,1);
	layoutDialsDelta->addWidget(new QLabel(tr("Focal length:")),0,2,1,1);
	
	layoutDialsDelta->addWidget(editZoomDeltaPerspective,1,0,1,1);
	layoutDialsDelta->addWidget(editZoomDeltaOrthogonal,1,1,1,1);
	layoutDialsDelta->addWidget(editLookAtRadiusDelta,1,2,1,1);

	layoutDialsDelta->addWidget(new QLabel(tr("Swirl angle:")),2,0,1,1);
	layoutDialsDelta->addWidget(new QLabel(tr("Pan delta:")),2,1,1,1);
	layoutDialsDelta->addWidget(editSwirlDelta ,3,0,1,1);
	layoutDialsDelta->addWidget(editdefault_deta_pan ,3,1,1,1);

	myLayout2->addWidget(boxDialsDelta);
	myLayout2->insertWidget(insertindex++,boxDialsDelta);

	
	//myLayout2->addWidget(qtMovingObject::getMyBox());


	setControlsByWagon(false);
	setConnections();

}; 



//camera * currentCamera;
void qtCamera::setConnections() {
	qtMovingObject::setConnections();


	//connect(this, SIGNAL(sig3dMoveOrders(int,GLfloat)), glwidgetSingleton::getInstance(), SLOT(slotCAM3dMoveOrders(int,GLfloat)));
	//connect(this, SIGNAL(sigCAM3dMoveOrders(int,GLfloat)),glwidgetSingleton::getInstance(), SLOT(slotCAM3dMoveOrders(int,GLfloat)));

	//QDial  *  dialZoom, * dialFocalLength, * dialSwirlUpDown, * dialRightLeft;
	connect(dialZoom, SIGNAL(valueChanged(int)), this, SLOT(dialZoom_valueChanged(int)));
	connect(dialFocalLength, SIGNAL(valueChanged(int)), this, SLOT(dialFocalLength_valueChanged(int)));
	connect(dialSwirlUpDown, SIGNAL(valueChanged(int)), this, SLOT(dialSwirlUpDown_valueChanged(int)));
	connect(dialRightLeft, SIGNAL(valueChanged(int)), this, SLOT(dialRightLeft_valueChanged(int)));

	connect(dialpanLeftRight, SIGNAL(valueChanged(int)), this, SLOT(dialpanLeftRight_valueChanged(int)));
	connect(dialpanUpDown, SIGNAL(valueChanged(int)), this, SLOT(dialpanUpDown_valueChanged(int)));
	connect(dialpanLevelForwardBackward, SIGNAL(valueChanged(int)), this, SLOT(dialpanLevelForwardBackward_valueChanged(int)));


	//QLineEdit *  editZoomDeltaPerspective, * editZoomDeltaOrthogonal, * editLookAtRadiusDelta,* editSwirlDelta;
	connect(editZoomDeltaPerspective, SIGNAL(editingFinished()), this, SLOT(editZoomDeltaPerspective_editingFinished()));
	connect(editZoomDeltaOrthogonal, SIGNAL(editingFinished()), this, SLOT(editZoomDeltaOrthogonal_editingFinished()));
	connect(editLookAtRadiusDelta, SIGNAL(editingFinished()), this, SLOT(editLookAtRadiusDelta_editingFinished()));
	connect(editSwirlDelta, SIGNAL(editingFinished()), this, SLOT(editSwirlDelta_editingFinished()));
	connect(editdefault_deta_pan, SIGNAL(editingFinished()), this, SLOT(editdefault_deta_pan_editingFinished()));

	//editOrthorgonalFarZ, *  editOrthorgonalNearZ, * editOsize , * editOsizeLimit, *editSafeLookAtRadius;
	connect(editOrthorgonalFarZ, SIGNAL(editingFinished()), this, SLOT(editOrthorgonalFarZ_editingFinished()));
	connect(editOrthorgonalNearZ, SIGNAL(editingFinished()), this, SLOT(editOrthorgonalNearZ_editingFinished()));
	connect(editOsize, SIGNAL(editingFinished()), this, SLOT(editOsize_editingFinished()));
	connect(editOsizeLimit, SIGNAL(editingFinished()), this, SLOT(editOsizeLimit_editingFinished()));
	connect(editSafeLookAtRadius, SIGNAL(editingFinished()), this, SLOT(editSafeLookAtRadius_editingFinished()));

	//editPerspectiveFarZ->setText( QString("%1").arg(currentCamera->spStructPserpective.far_z) );
	//editPerspectiveFovy->setText( QString("%1").arg(currentCamera->spStructPserpective.fovy) );
	//editPerspectiveFovyMin->setText( QString("%1").arg(currentCamera->spStructPserpective.fovy_min) );
	//editPerspectiveFovyMax->setText( QString("%1").arg(currentCamera->spStructPserpective.fovy_max) );
	//editPerspectiveFovyNearZ->setText( QString("%1").arg(currentCamera->spStructPserpective.near_z) );
	connect(editPerspectiveFarZ, SIGNAL(editingFinished()), this, SLOT(editPerspectiveFarZ_editingFinished()));
	connect(editPerspectiveFovy, SIGNAL(editingFinished()), this, SLOT(editPerspectiveFovy_editingFinished()));
	connect(editPerspectiveFovyMin, SIGNAL(editingFinished()), this, SLOT(editPerspectiveFovyMin_editingFinished()));
	connect(editPerspectiveFovyMax, SIGNAL(editingFinished()), this, SLOT(editPerspectiveFovyMax_editingFinished()));
	connect(editPerspectiveFovyNearZ, SIGNAL(editingFinished()), this, SLOT(editPerspectiveFovyNearZ_editingFinished()));

	//editFocusPositionx
	connect(editFocusPositionx, SIGNAL(editingFinished()), this, SLOT(editFocusPositionx_editingFinished()));
	connect(editFocusPositiony, SIGNAL(editingFinished()), this, SLOT(editFocusPositiony_editingFinished()));
	connect(editFocusPositionz, SIGNAL(editingFinished()), this, SLOT(editFocusPositionz_editingFinished()));
	connect(editFocusSize, SIGNAL(editingFinished()), this, SLOT(editFocusSize_editingFinished()));
	connect(radioPerspective , SIGNAL(toggled(bool)), this, SLOT(radioPerspective_toggled(bool)));
	connect(radioOrhorgonal , SIGNAL(toggled(bool)), this, SLOT(radioOrhorgonal_toggled(bool)));
	connect(checkShowFocus, SIGNAL(stateChanged(int)), this, SLOT(checkShowFocus_stateChanged(int)));

};
void qtCamera::setCameraPt(camera * ncurrentCamera) {
	currentCamera = ncurrentCamera;
	setMovingObjPt(ncurrentCamera);
	setControlsByWagon();
};

QGroupBox * qtCamera::getMyBox() {
	//std::cout<<"QGroupBox * qtCamera::getMyBox()"<<std::endl;
	return myBox2;
};
qtCamera::~qtCamera() {
}