#include "operatorDialog.h"
#include "ui_operatorDialog.h"
#include "gimbaldata.h"

//	----------------------------------------------------------------------------
//	Constants
//	----------------------------------------------------------------------------

	#define PI (3.14159265358979323846)

	const double toRad = PI/180;
	const double toDeg = 180.0/PI;

//	----------------------------------------------------------------------------
//	global functions
//	----------------------------------------------------------------------------

	extern bool triangulateTarget(double (*position1)[3], float (*los1D)[3],
						   double (*position2)[3], float (*los2D)[3],
						   double (*positiont)[3]);

//	----------------------------------------------------------------------------
//	functions
//	----------------------------------------------------------------------------

	void initSim();
	int latLonToMeters(double lat,double lon,float *xP,float *yP);
	void setAltitude(double alt);
	void setTarget(double lat, double lon, double alt);

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	extern int cmdModeCount;
	extern int cmdMode;
	extern int cmdStandby;
	extern int cmdStandbyCount;
	extern int shift;
	extern int unwrapMode;
	extern int unwrapReq;
	extern float unwrapTarg;
	extern double unwrapLLA[3];

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	double position1[3];
	double position2[3];
	double positiont[3];
	float los1D[3];
	float los2D[3];
	int opMinimize;

//	----------------------------------------------------------------------------
//	target reset
//	----------------------------------------------------------------------------

	const float xtm0 = 1800;
	const float ytm0 = 800;
	const float dxtm = 60;
	const double atm0 = 217;

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	int shift;
	int mousemoved;
	int hoverTime;
	int mousereleased;
	int sliderup;
	int newOperatorSettings;
	int newOperatorSettingsX;
	int slideractive;
	int sliderid;
	int sliderindex;
	int mapup;
	int mapselectup;
	int maphistoryup;
	int insreadoutup;
	int mapsat = 2;
	int menuup;
	int tmenuup;
	int tmenuid;
	int tmenuindex;
	int modemenuup;
	int modemenuindex;
	int geomenuup;
	int shutdownmenuup;
	int menuid;
	int selectedtargetid = B_geoTargetButton_01_;
	float geoAltBase;

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	gimReadouts_t gimReadouts;
	operControls_t operControls;
	operSettings_t operSettings;
	joystickT_t joystickT;
	consInput_t consInput;
	steering_t steeringOut;

	int consInputRxCount;

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	QGraphicsPixmapItem *pixI1a;
	QGraphicsPixmapItem *pixI2a;
	QGraphicsPixmapItem *pixI1b;
	QGraphicsPixmapItem *pixI2b;
	QGraphicsPixmapItem *pixI1c;
	QGraphicsPixmapItem *pixI2c;

	const int numpixmaps = 4;
	QGraphicsPixmapItem **pixmaps[numpixmaps] = {
		&pixI1a,
		&pixI1b,
		&pixI1c,
		&pixI2c
	};
	int pixmapsOK = 0;

//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

	QPointF mousepoint;
	QSettings settings("SCS", "GimbalOp");

//	----------------------------------------------------------------------------
//	macros
//	----------------------------------------------------------------------------

	#define _max(a,b)  (((a) > (b)) ? (a) : (b))
	#define _min(a,b)  (((a) < (b)) ? (a) : (b))

	#define PI (3.14159265358979323846)

//	----------------------------------------------------------------------------
//	targetsX
//	----------------------------------------------------------------------------

	double dzzz = 217-255;

	const int NTARGETSX = 16;
	double targetsX[NTARGETSX][3]={
/*
		{43.30606244, -80.14241557, 217			},	// 1
		{43.30448887, -80.13785297, 217			},	// 2
		{43.310656,   -80.137231,   262	+ dzzz	},	// 3 golf hole
		{43.312542,   -80.129118,   255	+ dzzz	},	// 4 hangar
		{43.30607001, -80.14009783, 235			},	// 5 antenna
		{43.30783549, -80.13010211, 217			},	// 6
		{43.30828991, -80.12786107, 217			},	// 7
		{43.31286724, -80.12940089, 217			}	// 8


		{	43.30606244,	-80.14241557	,	255 + dzzz	}, //  1	Origin HWY 8
		{	43.30606108	,	-80.14016607	,	275	+ dzzz	}, //  2	Antenna Target
		{	43.310656	,	-80.137231		,	262	+ dzzz	}, //  3	Top of Pin, Golf Green
		{	43.30720905	,	-80.13482627	,	262	+ dzzz	}, //  4	Sign On Pole Target
		{	43.30724669	,	-80.13217796	,	263	+ dzzz	}, //  5	Light Pole Target
		{	43.30783549	,	-80.13010211	,	265	+ dzzz	}, //  6	Power Pole Target
		{	43.300635000,	-80.130046000	,	257	+ dzzz	}, //  7	Cell Tower #1
		{	43.31286724	,	-80.12940089	,	255	+ dzzz	}, //  8	Hangar
		{	43.312542	,	-80.129118		,	255	+ dzzz	}, //  9	Hangar Target
		{	43.30828991	,	-80.12786107	,	253	+ dzzz	}, // 10	Gate
		{	43.304723000,	-80.127494000	,	256	+ dzzz	}, // 11	Cell Tower #2
		{	43.300978000,	-80.110785000	,	250	+ dzzz	}, // 12	Light pole in a field
		{	43.300081000,	-80.107679000	,	250	+ dzzz	}, // 13	Tree in a field
		{	43.31142	,	-80.10875		,	255 + dzzz	}, // 14	Valens
		{	43.314267000,	-80.095942000	,	258 + dzzz	}, // 15	Transmission Tower #1
		{	43.316560000,	-80.096678000	,	258	+ dzzz	}  // 16	Transmisson Tower #2

		//	Rockton Area
		{	43.314267	,	-80.095942	,	258	+ dzzz	},	//	1	Transmission Tower #1
		{	43.316560	,	-80.096678	,	258	+ dzzz	},	//	2	Transmssiion Tower #2
		{	43.328648	,	-80.091624	,	262	+ dzzz	},	//	3	Enbridge Site
		{	43.300081	,	-80.107679	,	250	+ dzzz	},	//	4	Tree in a field
		{	43.300978	,	-80.110785	,	250	+ dzzz	},	//	5	Light pole in a field
		{	43.300635	,	-80.130046	,	257	+ dzzz	},	//	6	Cell Tower #1
		{	43.304723	,	-80.127494	,	256	+ dzzz	},	//	7	Cell Tower #2
		{	43.3125		,	-80.1294	,	255	+ dzzz	},	//	8	HeliPad
				//	Hamilton Area
		{	43.298290	,	-79.796954	,	 77	+ dzzz	},	//	9	SkyWay Centre Span
		{	43.269451	,	-79.884254	,	105	+ dzzz	},	//	10	Dundurn Castle Entrance
		{	43.273015	,	-79.802067	,	 80	+ dzzz	},	//	11	Dofasco Stack
		{	43.255695	,	-79.873058	,	102	+ dzzz	},	//	12	City Hall Entrance Dome
		{	43.256253	,	-79.868426	,	 97	+ dzzz	},	//	13	Gore Park Fountain
		{	43.252076	,	-79.830081	,	 84	+ dzzz	},	//	14	Tim Horton Field
		{	43.280809	,	-79.978650	,	236	+ dzzz	},	//	15	Tews Falls
		{	43.263320	,	-79.891357	,	 98	+ dzzz	},	//	16	Cathedral Steeple

		//	North
		{	43.367888	,	-79.928798	,	262	+ dzzz },	//	1	Origin-Eurotech
		{	43.636829	,	-79.976436	,	311	+ dzzz },	//	2	Limehouse Arch
		{	43.793811	,	-80.010934	,	358	+ dzzz },	//	3	Belfountain Falls
		{	43.821315	,	-80.022361	,	350	+ dzzz },	//	4	Cataract Falls
		{	44.007393	,	-79.927267	,	287	+ dzzz },	//	5	Quarry Conveyers
		{	44.146326	,	-79.849505	,	221	+ dzzz },	//	6	Honda Plant
		{	44.274440	,	-79.893596	,	220	+ dzzz },	//	7	CFB Borden Baseball Field
		{	44.395386	,	-79.822001	,	216	+ dzzz },	//	8	Fort Willow
		{	44.411090	,	-79.791888	,	240	+ dzzz },	//	9	Snow Valley-Base Lodge
		{	44.623165	,	-79.667490	,	240	+ dzzz },	//	10	Mount St Louis Moonstone-Base Lodge
		{	44.803971	,	-79.720527	,	176	+ dzzz },	//	11	Lock 45
		{	44.884794	,	-79.673991	,	198	+ dzzz },	//	12	Big Chutes boat lift
		{	45.012299	,	-79.613347	,	221	+ dzzz },	//	13	Balla Falls
		{	45.115964	,	-79.592519	,	250	+ dzzz },	//	14	Port Carling Golf Course
		{	45.223091	,	-79.558755	,	227	+ dzzz },	//	15	Destination-Skeleton Bay
		{	45.223091	,	-79.558755	,	227	+ dzzz },	//	16	Destination-Skeleton Bay
//		{	0,				0				,	0	}  // 16
//		{	43.3125		,	-80.1294	,	255	+ dzzz },	//	16	HeliPad
*/
		{	43.351786,	-79.798379,	117.8},	// 1 Aethon Tower Tops
		{	43.351641,	-79.798130,	117.8},	// 2
		{	43.351330,	-79.797971,	116.5},	// 3
		{	43.350953,	-79.797623,	116.5},	// 4
		{	43.350603,	-79.797355,	116.0},	// 5
		{	43.350241,	-79.797096,	116.0},	// 6
		{	43.350328,	-79.796676,	116.0},	// 7
		{	43.350533,	-79.796343,	116.0},	// 8
		{	43.350824,	-79.795852,	116.5},	// 9
		{	43.351115,	-79.795359,	116.0},	// 10
		{	43.351426,	-79.794865,	117.0},	// 11
		{	0,				0				,	0	}, // 12
		{	0,				0				,	0	}, // 13
		{	0,				0				,	0	}, // 14
		{	0,				0				,	0	}, // 15
		{	0,				0				,	0	}  // 16
/*
		{	43.30606244	,	-80.14241557	,	275	}, // 1
		{	43.30448887	,	-80.13785297	,	262	}, // 2
		{	43.3065785	,	-80.13817753	,	262	}, // 3
		{	43.30839539	,	-80.1277661		,	263	}, // 4
		{	43.314267	,	-80.095942		,	265	}, // 5
		{	43.300081	,	-80.107679		,	257	}, // 6
		{	43.304723	,	-80.127494		,	255	}, // 7
		{	43.301883	,	-80.1254656		,	255	}, // 8
		{	0,				0				,	0	}, // 9
		{	0,				0				,	0	}, // 10
		{	0,				0				,	0	}, // 11
		{	0,				0				,	0	}, // 12
		{	0,				0				,	0	}, // 13
		{	0,				0				,	0	}, // 14
		{	0,				0				,	0	}, // 15
		{	0,				0				,	0	}  // 16
*/
	};

//		{43.30657850, -80.13817753, 217			},	// 3 the one


//	----------------------------------------------------------------------------
//	globals
//	----------------------------------------------------------------------------

//	double latitude0 = 43.30657850;
//	double longitude0 = -80.13817753;
	double latitude0 = 43.30606244;
	double longitude0 = -80.14241557;

//	----------------------------------------------------------------------------
//	operatorDialog
//	----------------------------------------------------------------------------

void saveSettings()
{
	if(gimReadouts.targetLat){
		operSettings.geoLat = gimReadouts.targetLat;
		operSettings.geoLon = gimReadouts.targetLon;
	}

	settings.setValue("panGain",		operSettings.panGain		);
	settings.setValue("tiltGain",		operSettings.tiltGain		);
	settings.setValue("rollGain",		operSettings.rollGain		);
	settings.setValue("dynGain",		operSettings.dynGain		);
	settings.setValue("zoomGain",		operSettings.zoomGain		);
	settings.setValue("focusGain",		operSettings.focusGain		);
	settings.setValue("zmultGain",		operSettings.zmultGain		);
	settings.setValue("geoLat",			operSettings.geoLat			);
	settings.setValue("geoLon",			operSettings.geoLon			);
	settings.setValue("geoAlt",			operSettings.geoAlt			);
	settings.setValue("panRev",			operSettings.panRev			);
	settings.setValue("tiltRev",		operSettings.tiltRev		);
	settings.setValue("rollRev",		operSettings.rollRev		);
	settings.setValue("zoomRev",		operSettings.zoomRev		);
	settings.setValue("irisRev",		operSettings.irisRev		);
	settings.setValue("focusRev",		operSettings.focusRev		);
	settings.setValue("dynOn",			operSettings.dynOn			);
	settings.setValue("zmultOn",		operSettings.zmultOn		);
//	settings.setValue("mode",			operSettings.mode			);
	settings.setValue("encFunction",	operSettings.encFunction	);
	settings.setValue("jzFunction",		operSettings.jzFunction		);
	settings.setValue("lat1",			operSettings.llaS1[0]		);
	settings.setValue("lon1",			operSettings.llaS1[1]		);
	settings.setValue("alt1",			operSettings.llaS1[2]		);
	settings.setValue("lat2",			operSettings.llaS2[0]		);
	settings.setValue("lon2",			operSettings.llaS2[1]		);
	settings.setValue("alt2",			operSettings.llaS2[2]		);
	settings.setValue("lat3",			operSettings.llaS3[0]		);
	settings.setValue("lon3",			operSettings.llaS3[1]		);
	settings.setValue("alt3",			operSettings.llaS3[2]		);
	settings.setValue("lat4",			operSettings.llaS4[0]		);
	settings.setValue("lon4",			operSettings.llaS4[1]		);
	settings.setValue("alt4",			operSettings.llaS4[2]		);
	settings.setValue("lat5",			operSettings.llaS5[0]		);
	settings.setValue("lon5",			operSettings.llaS5[1]		);
	settings.setValue("alt5",			operSettings.llaS5[2]		);
	settings.sync();
}

//	----------------------------------------------------------------------------
//	operatorDialog
//	----------------------------------------------------------------------------

void loadSettings(){
	if(settings.contains("geoAlt")){
		operSettings.panGain		= settings.value("panGain"		).toFloat();
		operSettings.tiltGain		= settings.value("tiltGain"		).toFloat();
		operSettings.rollGain		= settings.value("rollGain"		).toFloat();
		operSettings.dynGain		= settings.value("dynGain"		).toFloat();
		operSettings.zoomGain		= settings.value("zoomGain"		).toFloat();
		operSettings.focusGain		= settings.value("focusGain"	).toFloat();
		operSettings.zmultGain		= settings.value("zmultGain"	).toFloat();
		operSettings.geoLat			= settings.value("geoLat"		).toDouble();
		operSettings.geoLon			= settings.value("geoLon"		).toDouble();
		operSettings.geoAlt			= settings.value("geoAlt"		).toFloat();
		operSettings.panRev			= settings.value("panRev"		).toInt();
		operSettings.tiltRev		= settings.value("tiltRev"		).toInt();
		operSettings.rollRev		= settings.value("rollRev"		).toInt();
		operSettings.zoomRev		= settings.value("zoomRev"		).toInt();
		operSettings.irisRev		= settings.value("irisRev"		).toInt();
		operSettings.focusRev		= settings.value("focusRev"		).toInt();
		operSettings.dynOn			= settings.value("dynOn"		).toInt();
		operSettings.zmultOn		= settings.value("zmultOn"		).toInt();
//		operSettings.mode			= settings.value("mode"			).toInt();
		operSettings.encFunction	= settings.value("encFunction"	).toInt();
		operSettings.jzFunction		= settings.value("jzFunction"	).toInt();
		operSettings.llaS1[0]		= settings.value("lat1"			).toDouble();
		operSettings.llaS1[1]		= settings.value("lon1"			).toDouble();
		operSettings.llaS1[2]		= settings.value("alt1"			).toDouble();
		operSettings.llaS2[0]		= settings.value("lat2"			).toDouble();
		operSettings.llaS2[1]		= settings.value("lon2"			).toDouble();
		operSettings.llaS2[2]		= settings.value("alt2"			).toDouble();
		operSettings.llaS3[0]		= settings.value("lat3"			).toDouble();
		operSettings.llaS3[1]		= settings.value("lon3"			).toDouble();
		operSettings.llaS3[2]		= settings.value("alt3"			).toDouble();
		operSettings.llaS4[0]		= settings.value("lat4"			).toDouble();
		operSettings.llaS4[1]		= settings.value("lon4"			).toDouble();
		operSettings.llaS4[2]		= settings.value("alt4"			).toDouble();
		operSettings.llaS5[0]		= settings.value("lat5"			).toDouble();
		operSettings.llaS5[1]		= settings.value("lon5"			).toDouble();
		operSettings.llaS5[2]		= settings.value("alt5"			).toDouble();
	}else{
		settings.clear();
		saveSettings();
	}
	newOperatorSettings = 2;
}

//	----------------------------------------------------------------------------
//	operatorDialog
//	----------------------------------------------------------------------------

operatorDialog::operatorDialog(QWidget *parent) : QDialog(parent), ui(new Ui::operatorDialog)
{
	init();
	initLabels();
	loadSettings();
	setTarget( operSettings.geoLat, operSettings.geoLon, operSettings.geoAlt );

	ui->setupUi(this);
	setWindowFlags(Qt::FramelessWindowHint);
	ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

	float ww = 1920;		// 800
	float hh = 1080;		// 480
	float yc = hh/2;
	float xc = ww/2;
	QFont font  = QFont("Calibri", 30, QFont::Black);
	QFont font2 = QFont("Calibri", 20, QFont::Black);

	graphScene = new GraphicsScene(ui->graphicsView);
	ui->graphicsView->setScene(graphScene);
	ui->graphicsView->setRenderHint(QPainter::Antialiasing);
	ui->graphicsView->setFixedHeight(hh);
	ui->graphicsView->setFixedWidth(ww);
	QBrush bgbrush;
	bgbrush.setColor(mcolor(M_lightgrey));
	bgbrush.setStyle(Qt::SolidPattern);
	ui->graphicsView->setBackgroundBrush(bgbrush);

	pixI1a = new QGraphicsPixmapItem;
	pixI1a->setPixmap(QPixmap(":/icons/s1a.jpg"));
	graphScene->addItem(pixI1a);
	pixI1a->moveBy(0,14);
	pixI1a->setVisible(false);

//	pixI2a = new QGraphicsPixmapItem;
//	pixI2a->setPixmap(QPixmap(":/icons/s2a.jpg"));
//	graphScene->addItem(pixI2a);
//	pixI2a->moveBy(0,14);
//	pixI2a->setVisible(false);

	pixI1b = new QGraphicsPixmapItem;
	pixI1b->setPixmap(QPixmap(":/icons/s1b.jpg"));
	graphScene->addItem(pixI1b);
	pixI1b->moveBy(0,14);
	pixI1b->setVisible(false);

//	pixI2b = new QGraphicsPixmapItem;
//	pixI2b->setPixmap(QPixmap(":/icons/s2b.jpg"));
//	graphScene->addItem(pixI2b);
//	pixI2b->moveBy(0,14);
//	pixI2b->setVisible(false);

	pixI1c = new QGraphicsPixmapItem;
	pixI1c->setPixmap(QPixmap(":/icons/s1c.jpg"));
	graphScene->addItem(pixI1c);
	pixI1c->moveBy(0,14);
	pixI1c->setVisible(false);

	pixI2c = new QGraphicsPixmapItem;
	pixI2c->setPixmap(QPixmap(":/icons/s2c.jpg"));
	graphScene->addItem(pixI2c);
	pixI2c->moveBy(0,14);
	pixI2c->setVisible(false);

	pixmapsOK = 1;

	float rx1 = 267*fixX;
	float xcl = xc-rx1;
	float xcr = xc+rx1;

	int r1 = 160*fixY;
	QPainterPath c1path;
	c1path.addEllipse(-r1,-r1,2*r1,2*r1);
	c1 = new mPathItem(c1path);
	QPen c1pen;
	c1pen.setWidthF(1);
	QBrush c1brush;
	c1brush.setColor(mcolor(M_grey));
	c1brush.setStyle(Qt::SolidPattern);
	c1->setPen(c1pen);
	c1->setBrush(c1brush);
	graphScene->addItem(c1);
	c1->moveBy(xcl,yc);
	c1->setVisible(true);

	int r2 = r1;
	QPainterPath c2path;
	c2path.addEllipse(-r2,-r2,2*r2,2*r2);
	c2 = new mPathItem(c2path);
	QPen c2pen;
	c2pen.setWidthF(1);
	QBrush c2brush;
	c2brush.setColor(mcolor(M_grey));
	c2brush.setStyle(Qt::SolidPattern);
	c2->setPen(c2pen);
	c2->setBrush(c2brush);
	graphScene->addItem(c2);
	c2->moveBy(xcr,yc);
	c2->setVisible(true);

	int group = MG_main;

	float rclock = 114*fixY;
	float a = -180;
	const int nclock = 7;
	float xrc[nclock];
	float yrc[nclock];
	float da = 360.0f/nclock;
	for(int i=0;i<nclock;i++){
		xrc[i] =  rclock*sinf(toRad*a);
		yrc[i] = -rclock*cosf(toRad*a);
		a += da;
	}
	float y1 = yrc[0] + yc;
	float y2 = yrc[1] + yc;
	float y3 = yrc[2] + yc;
	float y4 = yrc[3] + yc;
	float y5 = yrc[4] + yc;
	float y6 = yrc[5] + yc;
	float y7 = yrc[6] + yc;

//	float xl1 = xrc[0] + xcl;
//	float xl2 = xrc[1] + xcl;
	float xl3 = xrc[2] + xcl;
	float xl4 = xrc[3] + xcl;
	float xl5 = xrc[4] + xcl;
//	float xl6 = xrc[5] + xcl;
	float xl7 = xrc[6] + xcl;

	float xr1 = xrc[0] + xcr;
	float xr2 = xrc[1] + xcr;
	float xr3 = xrc[2] + xcr;
	float xr4 = xrc[3] + xcr;
	float xr5 = xrc[4] + xcr;
	float xr6 = xrc[5] + xcr;
//	float xr7 = xrc[6] + xcr;


	float rinner = 44*fixY;
	float router = 70*fixY;

	float x12 = xcl;
	float y12 = (321+40)*fixY;
	float rym = 145*fixY;
	float ryml = 125*fixY;
	float rxm =  100*fixY;
	float xml = xc - rxm;
	float xmr = xc + rxm;
	float ymb = yc + rym;
	float ymtl = yc - ryml;

	irisButton =    makeButton(xl3,y3,  M_orange,	B_irisButton_,		"IRIS",	 "NRM",		"",		font,group);
	xyzButton =		makeButton(xl4,y4,  M_orange,	B_xyzButton_,		"ENC",	 "ALT",		"",		font,group);
	focusButton =   makeButton(xl5,y5,  M_orange,	B_focusButton_,		"FOC",	 "100%",	"NRM",	font,group);
	zmulButton =    makeButton(xl7,y7,  M_orange,	B_zmulButton_,		"ZMUL",  "100%",	"ON",	font,group);

	losTiltButton =	makeButton(xr1,y1,  M_pink,		B_losTiltButton_,	"TILT",	 "45",		"LOS",	font,group);
	dynButton =		makeButton(xr2,y2,  M_orange,	B_dynButton_,		"DYN",	 "100%",	"ON",	font,group);
	aidButton =     makeButton(xr3,y3,  M_orange,	B_aidButton_,		"AID",	 "OFF",		"1000",	font,group);
	modeButton =	makeButton(xr4,y4,  M_orange,	B_modemenuButton_,	"MODE",  "STANDBY",	"",		font,group);
	stowButton =	makeButton(xr5,y5,  M_orange,	B_stowButton_,		"STOW",	 "",		"",		font,group);
	geoAltButton =	makeButton(xr6,y6,  M_orange,	B_geoAltButton_,	"ALT",	 "100",	"1000",		font,group);

	zoomButton =    makeButton(xcl,yc,  M_orange,	B_zoomButton_,		"ZOOM",  "100%",	"NRM",	font,group);
	camButton =     makeButton(x12,y12, M_orange,	B_camButton_,		"CAM",	 "REC",		"",		font,group);
	menuButton =	makeButton(xml,ymtl,M_orange,	B_menuButton_,		"MENU",	 "",		"",		font,group);
	insButton =		makeButton(xc ,ymtl,M_green,	B_insButton_,		"INS",	 "",		"",		font,group);
	alarmButton =	makeButton(xmr,ymtl,M_green,	B_alarmButton_,		"ALARM", "NONE",	"",		font,group);
	panButton =		makeButton(xml,ymb, M_orange,	B_panButton_,		"PAN",	 "100%",	"NRM",	font,group);
	tiltButton =	makeButton(	xc,ymb, M_orange,	B_tiltButton_,		"TILT",	 "100%",	"NRM",	font,group);
	rollButton =	makeButton(xmr,ymb, M_orange,	B_rollButton_,		"ROLL",	 "100%",	"NRM",	font,group);
	losPanButton =	makeButton(xcr,yc,  M_pink,		B_losPanButton_,	"PAN",	 "-165",	"LOS",	font,group);
	wrapButton =	makeButton(	xc, yc, M_orange,	B_wrapButton_,		"WRAP",	 "",		"",		font,group);

	losPanPoly =	makeRing(	xcr,yc,  rinner,router,	M_pink,B_losPanPoly_,"","","",font,group);

	losTiltPoly =	makeArc(	xr1,y1,  rinner,router,  45,180,M_pink,B_losTiltPoly_,	"","","",font,group);
	zoomPoly =		makeArc(	xcl,yc,  rinner,router,-158,158,M_pink,B_zoomPoly_,		"","","",font,group);
	wrapPoly01 =	makeArc(	xc, yc,  rinner,router,-158,158,M_pink,B_wrapPoly01_,	"","","",font,group);
	wrapPoly02 =	makeArc(	xc, yc,  rinner,router, -44, 44,M_pink,B_wrapPoly02_,	"","","",font,group);

	wrapPoly01->ball->setVisible(false);

	float wmenu = 90*fixY;		// button width
	float hmenu = 45*fixY;		// button height
	float mw = 10*fixY;			// margin width
	float xmenu = mw/2;
	float ymenu = hh - hmenu;
	mapButton =		makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_green,	B_mapButton_,	"MAP",		"",	 "",font,group);xmenu += mw + wmenu;

	group = MG_smarties;
	float wccb = 155*fixX;
	float hccb = 43*fixX;
	float yccb = 5*fixX;
	redButton    = makeButton2( 43*fixX,yccb,hccb,wccb,M_red,	B_redPoly_,		"CAMERA",	"RECORD",	"",font,group);
	yellowButton = makeButton2(227*fixX,yccb,hccb,wccb,M_yellow,B_yellowPoly_,	"SHIFT",	"",			"",font,group);
	greenButton  = makeButton2(411*fixX,yccb,hccb,wccb,M_green,	B_greenPoly_,	"L1/L2",	"",			"",font,group);
	blueButton   = makeButton2(595*fixX,yccb,hccb,wccb,M_blue,	B_bluePoly_,	"",			"",			"",font,group);

	makeMapLos();
	makeMapL1();
	makeMapL2();

	group = MG_menu;
	xmenu = xml;
	ymenu = ymtl;
	float wmenub = 230*fixX;

	hmenu = 40*fixY;		// button height
	float mh = 10*fixY;		// margin height
	mw = 25*fixY;			// margin width
	int nb = 5;
	float hmenub = (1+nb)*mh + nb*hmenu;
	menuBox = makeButton3(MB_menu,xmenu,ymenu,hmenub,wmenub,M_white,B_menuTBox_,"","","",font,group);
	ymenu += mh;
	xmenu += mw;
	wmenu = wmenub - 2*mw;
	menuButton01 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuButton_01_,	"SHUTDOWN...",		"",	"",font,group);ymenu += mh + hmenu;
	menuButton02 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuButton_02_,	"Full Screen",		"",	"",font,group);ymenu += mh + hmenu;
	menuButton03 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuButton_03_,	"JS.Z = Alt",		"",	"",font,group);ymenu += mh + hmenu;
	menuButton04 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuButton_04_,	"Slave Set",		"",	"",font,group);ymenu += mh + hmenu;
	menuButton05 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_green,			B_menuButton_05_,	"Cancel",			"",	"",font,group);ymenu += mh + hmenu;

	group = MG_shutdown;
	xmenu = xml;
	ymenu = ymtl;
	wmenub = 230*fixX;
	hmenu = 40*fixY;		// button height
	mh = 20*fixY;			// margin height
	mw = 15*fixY;			// margin width
	hmenub = 5*mh + 4*hmenu;
	shutdownBox = makeButton3(MB_menuX,xmenu,ymenu,hmenub,wmenub,M_white,B_shutdownBox_,"","","",font,group);
	ymenu += mh;
	xmenu += mw;
	wmenu = wmenub - 2*mw;
	shutdownButton01 = makeButton3(MB_menuItemX,xmenu,ymenu,hmenu,wmenu,M_red,		B_shutdownButton_01_,	"CAMERA",	"",		 "",font2,group);ymenu += mh + hmenu;
	shutdownButton02 = makeButton3(MB_menuItemX,xmenu,ymenu,hmenu,wmenu,M_red,		B_shutdownButton_02_,	"CONSOLE",	"",		 "",font2,group);ymenu += mh + hmenu;
	shutdownButton03 = makeButton3(MB_menuItemX,xmenu,ymenu,hmenu,wmenu,M_red,		B_shutdownButton_03_,	"ALL",		"",		 "",font2,group);ymenu += mh + hmenu;
	shutdownButton04 = makeButton3(MB_menuItemX,xmenu,ymenu,hmenu,wmenu,M_green,	B_shutdownButton_04_,	"CANCEL Shutdown","","",font2,group);ymenu += mh + hmenu;

	group = MG_geo;
	wmenub = 230*fixX;
	xmenu = xr3 - wmenub;
	ymenu = y3;
	hmenu = 40*fixY;		// button height
	mh = 10*fixY;			// margin height
	mw = 25*fixY;			// margin width
	int nm = 4;
	hmenub = (nm+1)*mh + nm*hmenu;
	geomenuBox = makeButton3(MB_menuX,xmenu,ymenu,hmenub,wmenub,M_white,B_geomenuBox_,"","","",font,group);
	ymenu += mh;
	xmenu += mw;
	wmenu = wmenub - 2*mw;
	geomenuButton_New		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_geomenuButton_New_,		"GEO NEW",	"",	 "",font,group);ymenu += mh + hmenu;
	geomenuButton_Prev		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_geomenuButton_Prev_,		"GEO PREV",	"",	 "",font,group);ymenu += mh + hmenu;
	geomenuButton_Alt		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_geomenuButton_Alt_,		"ALT",		"",	 "",font,group);ymenu += mh + hmenu;
	geomenuButton_Targets	= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_geomenuButton_Targets_,	"TARGETS",	"",	 "",font,group);ymenu += mh + hmenu;

	group = MG_map;
	wmenu = 90*fixY;		// button width
	hmenu = 45*fixY;		// button height
	mw = 10*fixY;			// margin width
	xmenu = mw/2;
	ymenu = hh - hmenu;
	mapButton_Exit		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_green,	B_mapButton_Exit_,	"MAIN",		"", "",font,group);xmenu += mw + wmenu;
	mapButton_Load		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Load_,	"LOAD",		"", "",font,group);xmenu += mw + wmenu;
	mapButton_Sat		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Sat_,	"SAT",		"", "",font,group);xmenu += mw + wmenu;
	mapButton_Select	= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Select_,"SELECT",	"", "",font,group);xmenu += mw + wmenu;
	mapButton_Hist		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Hist_,	"HIST",		"", "",font,group);xmenu += mw + wmenu;
	mapButton_Mode		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Mode_,	"MODE",	"RATE", "",font,group);xmenu += mw + wmenu;
	mapButton_Alt		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Alt_,	"ALT",	"1000", "",font,group);xmenu += mw + wmenu;
	mapButton_Aid		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_mapButton_Aid_,	"AID",	 "OFF", "",font,group);xmenu += mw + wmenu;

	float xwrap = 710*fixX;
	float ywrap = yc;
	float littlewrap = 1.0f;
	wrapMapPoly01 =	makeArc(xwrap, ywrap,  rinner*littlewrap, router*littlewrap,-158,158, M_pink, B_wrapMapPoly01_,"","","",font,group);
	wrapMapPoly02 =	makeArc(xwrap, ywrap,  rinner*littlewrap, router*littlewrap, -44, 44, M_pink, B_wrapMapPoly02_,"","","",font,group);
	wrapMapPoly01->ball->setVisible(false);
	wrapMapButton =	makeButton(xwrap,ywrap, M_orange,	B_wrapMapButton_,	"WRAP",	 "0",		"",		font,group);

	float xtt = 10;
	float ytt = 10;

	simWaypoint1Button		= makeButton3(MB_mapCross,xtt,ytt,0,0,M_blue,		B_simWaypoint1Button_,		 "",	"",	 "",font2,group);
	simWaypoint2Button		= makeButton3(MB_mapCross,xtt,ytt,0,0,M_blue,		B_simWaypoint1Button_,		 "",	"",	 "",font2,group);

	acposButton				= makeButton3(MB_aircraft,xtt,ytt,0,0,M_blue,		B_acposButton_,			 "",	"",	 "",font2,group);
	acpos1Button			= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_blue,		B_acpos1Button_,		"L1",	"",	 "",font2,group);
	acpos2Button			= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_blue,		B_acpos2Button_,		"L2",	"",	 "",font2,group);
	geoTwoPointButton		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_blue,		B_geoTwoPointButton_,    "L",	"",	 "",font2,group);
	geoTargetButton			= makeButton3(MB_mapCross, xtt,ytt,0,0,M_green,		B_geoTargetButton_,		  "",	"",	 "",font2,group);
	geoPassiveButton		= makeButton3(MB_mapCross, xtt,ytt,0,0,M_yellow,	B_geoPassiveButton_,	  "",	"",	 "",font2,group);

	acposButton->setLL(			0,0,0);
	acpos1Button->setLL(		0,0,0);
	acpos2Button->setLL(		0,0,0);
	geoTwoPointButton->setLL(	0,0,0);
	geoTargetButton->setLL(		0,0,0);

	geoTargetButton_01		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_01_,		 "1",	"",	 "",font2,group);
	geoTargetButton_02		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_02_,		 "2",	"",	 "",font2,group);
	geoTargetButton_03		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_03_,		 "3",	"",	 "",font2,group);
	geoTargetButton_04		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_04_,		 "4",	"",	 "",font2,group);
	geoTargetButton_05		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_05_,		 "5",	"",	 "",font2,group);
	geoTargetButton_06		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_06_,		 "6",	"",	 "",font2,group);
	geoTargetButton_07		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_07_,		 "7",	"",	 "",font2,group);
	geoTargetButton_08		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_08_,		 "8",	"",	 "",font2,group);
	geoTargetButton_09		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_09_,		 "9",	"",	 "",font2,group);
	geoTargetButton_10		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_10_,		"10",	"",	 "",font2,group);
	geoTargetButton_11		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_11_,		"11",	"",	 "",font2,group);
	geoTargetButton_12		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_12_,		"12",	"",	 "",font2,group);
	geoTargetButton_13		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_13_,		"13",	"",	 "",font2,group);
	geoTargetButton_14		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_14_,		"14",	"",	 "",font2,group);
	geoTargetButton_15		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_15_,		"15",	"",	 "",font2,group);
	geoTargetButton_16		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_red,		B_geoTargetButton_16_,		"16",	"",	 "",font2,group);

	geoTarget2Button_01		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_green,		B_geoTarget2Button_01_,		"-1",	"",	 "",font2,group);
	geoTarget2Button_02		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_green,		B_geoTarget2Button_02_,		"-2",	"",	 "",font2,group);
	geoTarget2Button_03		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_green,		B_geoTarget2Button_03_,		"-3",	"",	 "",font2,group);
	geoTarget2Button_04		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_green,		B_geoTarget2Button_04_,		"-4",	"",	 "",font2,group);
	geoTarget2Button_05		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_green,		B_geoTarget2Button_05_,		"-5",	"",	 "",font2,group);

	nTargetHistory = 0;
	targetHistory[nTargetHistory++] = geoTarget2Button_01;
	targetHistory[nTargetHistory++] = geoTarget2Button_02;
	targetHistory[nTargetHistory++] = geoTarget2Button_03;
	targetHistory[nTargetHistory++] = geoTarget2Button_04;
	targetHistory[nTargetHistory++] = geoTarget2Button_05;
	for(int i=0;i<nTargetHistory;i++){
		targetHistory[i]->lat = 0;
	}

	geoTargetSButton_01		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_pink,		B_geoTargetSButton_01_,		"1",	"",	 "",font2,group);
	geoTargetSButton_02		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_pink,		B_geoTargetSButton_02_,		"2",	"",	 "",font2,group);
	geoTargetSButton_03		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_pink,		B_geoTargetSButton_03_,		"3",	"",	 "",font2,group);
	geoTargetSButton_04		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_pink,		B_geoTargetSButton_04_,		"4",	"",	 "",font2,group);
	geoTargetSButton_05		= makeButton3(MB_mapTarget,xtt,ytt,0,0,M_pink,		B_geoTargetSButton_05_,		"5",	"",	 "",font2,group);

	if(operSettings.llaS1[0]==0){
		geoTargetSButton_01->setLL(0,0,0);
		geoTargetSButton_02->setLL(0,0,0);
		geoTargetSButton_03->setLL(0,0,0);
		geoTargetSButton_04->setLL(0,0,0);
		geoTargetSButton_05->setLL(0,0,0);
		operSettings.llaS1[0] = geoTargetSButton_01->lat; operSettings.llaS1[1] = geoTargetSButton_01->lon; operSettings.llaS1[2] = geoTargetSButton_01->alt;
		operSettings.llaS2[0] = geoTargetSButton_02->lat; operSettings.llaS2[1] = geoTargetSButton_02->lon;	operSettings.llaS2[2] = geoTargetSButton_02->alt;
		operSettings.llaS3[0] = geoTargetSButton_03->lat; operSettings.llaS3[1] = geoTargetSButton_03->lon; operSettings.llaS3[2] = geoTargetSButton_03->alt;
		operSettings.llaS4[0] = geoTargetSButton_04->lat; operSettings.llaS4[1] = geoTargetSButton_04->lon; operSettings.llaS4[2] = geoTargetSButton_04->alt;
		operSettings.llaS5[0] = geoTargetSButton_05->lat; operSettings.llaS5[1] = geoTargetSButton_05->lon; operSettings.llaS5[2] = geoTargetSButton_05->alt;
	}else{
		geoTargetSButton_01->setLL(operSettings.llaS1);
		geoTargetSButton_02->setLL(operSettings.llaS2);
		geoTargetSButton_03->setLL(operSettings.llaS3);
		geoTargetSButton_04->setLL(operSettings.llaS4);
		geoTargetSButton_05->setLL(operSettings.llaS5);
	}

//	float ymtl = yc - ryml;
	xmenu = xml + 80*fixX;
	ymenu = ymtl + 25*fixY;
	makeModeMenu(xmenu,ymenu);

	makeTargetMenu(xml,ymtl);

	group = MG_exit;
	wmenu = 90*fixY;		// button width
	hmenu = 45*fixY;		// button height
	mw = 10*fixY;			// margin width
	xmenu = mw/2;
	ymenu = hh - hmenu;
	exitButton =		makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_green,	B_exitButton_,	"EXIT",		"",	 "",font,group);xmenu += mw + wmenu;
	group = MG_slider;
	float xslider = 43*fixX;
	float yslider = 275*fixY;
	float hslider = 68*fixY;
	float wslider = 714*fixX;
	sliderButton = makeSlider( xslider,yslider,hslider,wslider,M_lightblue,	B_sliderPoly_,	"",	"",	"",font,group);
	float hsliderb = 60*fixY;
	float dsliderb = 100*fixY;
	float ysliderb = yslider - dsliderb;
	float wsliderb = 100*fixX;
	float xsliderb = xc - wsliderb/2;
	sliderButton01 = makeButton3(MB_button3,xsliderb,ysliderb,hsliderb,wsliderb,M_lightblue,B_menuButton_01_,"ROLL","100%","NRM",font,group);

	graphScene->addItem(&insText);
	insText.moveBy(100*fixX,450*fixY);
	insText.setFont(font);
	insReadout();

	makeSliderKnob();
	makeGeoPoly();

	graphScene->setSceneRect(0,16,ww,hh);

	initSim();
	greyOut();

	setWindowState(windowState() | Qt::WindowFullScreen);
//	raise();

}

//	----------------------------------------------------------------------------
//	insReadout
//	----------------------------------------------------------------------------

void operatorDialog::insReadout()
{
	int insMode = ( gimReadouts.insStatus & 3 ) ;

	int GPSHeading = (gimReadouts.insStatus & 0x200)?(1):(0);
	int GPSHeadingINS = (gimReadouts.insStatus & 0x100)?(1):(0);
	int GPSFix = (gimReadouts.insStatus & 0x4)?(1):(0);

	const char *insModeS[4] = {"NoTrack","Aligning","Tracking","NoGPSFix"};

	QString ss;

	ss.sprintf("INS=%s VN300=%i GPSFix=%i GPSHeading=%i GPSHeadingINS=%i ",
		insModeS[insMode],
		gimReadouts.vnFreq,
		GPSFix,
		GPSHeading,
		GPSHeadingINS
	   );
	insText.setPlainText(ss);

	insText.setVisible(insreadoutup);

	int alarm = 0;
	if((!GPSFix)||(gimReadouts.vnFreq<100)){
		alarm = 1;
	}else if((insMode!=2)||(!GPSHeading)||(!GPSHeadingINS)){
		alarm = 2;
	}
	insButton->setAlarm(alarm);
}

//	----------------------------------------------------------------------------
//	initSim
//	----------------------------------------------------------------------------

void initSim()
{
	double alt1 = 50;
	double dalt = 100;

	double waypoints[2][3]={
		{43.30606244-0.0050, -80.14241557-0.0050, targetsX[0][2] + alt1 + dalt },
		{43.31286724+0.0016, -80.12940089-0.0116, targetsX[7][2] + alt1 },
	};

	for(int i=0;i<3;i++){
		steeringOut.sim.lla1[i] = waypoints[0][i];
		steeringOut.sim.lla2[i] = waypoints[1][i];
	}
	steeringOut.sim.speed = 21;

	steeringOut.sim.mode = SIM_MODE_OFF;
}

//	----------------------------------------------------------------------------
//	makeTargetMenu
//	----------------------------------------------------------------------------

void operatorDialog::makeTargetMenu(float xmenu, float ymenu)
{
	int group = MG_tmenu;
	QFont font  = QFont("Calibri", 30, QFont::Black);
	float wmenub = 230*fixX;
	float hmenu = 40*fixY;	// button height
	float mh = 10*fixY;		// margin height
	float mw = 25*fixY;		// margin width
	int nb = 4;
	float hmenub = (1+nb)*mh + nb*hmenu;
	menuTBox = makeButton3(MB_menu,xmenu,ymenu,hmenub,wmenub,M_white,B_menuTBox_,"","","",font,group);
	ymenu += mh;
	xmenu += mw;
	float wmenu = wmenub - 2*mw;
	menuTButton01 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuTButton_01_,	"Load",		"",		"",font,group);ymenu += mh + hmenu;
	menuTButton02 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuTButton_02_,	"Save",		"",		"",font,group);ymenu += mh + hmenu;
	menuTButton03 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,		B_menuTButton_03_,	"Reset",	"",		"",font,group);ymenu += mh + hmenu;
	menuTButton04 = makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_green,		B_menuTButton_04_,	"Cancel",	"",		"",font,group);ymenu += mh + hmenu;
}

//	----------------------------------------------------------------------------
//	makeModeMenu
//	----------------------------------------------------------------------------

void operatorDialog::makeModeMenu(float xmenu, float ymenu)
{
	int group = MG_mode;
	QFont font  = QFont("Calibri", 30, QFont::Black);
	float wmenub = 230*fixX;
	float hmenu = 40*fixY;		// button height
	float mh = 10*fixY;			// margin height
	float mw = 25*fixY;			// margin width
	float hmenub = 7*mh + 6*hmenu;
	modemenuBox = makeButton3(MB_menuX,xmenu,ymenu,hmenub,wmenub,M_white,B_modemenuBox_,"","","",font,group);
	ymenu += mh;
	xmenu += mw;
	float wmenu = wmenub - 2*mw;
	modemenuButton_Rate		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Rate_,		"RATE",		"",	 "",font,group);ymenu += mh + hmenu;
	modemenuButton_Slave	= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Slave_,	"SLAVE",	"",	 "",font,group);ymenu += mh + hmenu;
	modemenuButton_Geo		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Geo_,		"GEO",		"",	 "",font,group);ymenu += mh + hmenu;
	modemenuButton_Outer	= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Outer_,	"OUTER",	"",	 "",font,group);ymenu += mh + hmenu;
	modemenuButton_Stow		= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Stow_,		"STOW",		"",	 "",font,group);ymenu += mh + hmenu;
	modemenuButton_Standby	= makeButton3(MB_menuItem,xmenu,ymenu,hmenu,wmenu,M_orange,	B_modemenuButton_Standby_,	"STANDBY",	"",	 "",font,group);ymenu += mh + hmenu;
}

//	----------------------------------------------------------------------------
//	makeSliderKnob
//	----------------------------------------------------------------------------

void operatorDialog::makeSliderKnob()
{
	int r3 = 34*fixY;
	QPainterPath path1;
	path1.addEllipse(-r3,-r3,2*r3,2*r3);
	sliderKnob = new mPathItem(path1);
	QPen pen1;
	pen1.setWidthF(1);
	QBrush brush1;
	brush1.setColor(mcolor(M_pink));
	brush1.setStyle(Qt::SolidPattern);
	sliderKnob->setPen(pen1);
	sliderKnob->setBrush(brush1);
	graphScene->addItem(sliderKnob);
	sliderKnob->moveBy(300*fixX,250*fixY);
	sliderKnob->setVisible(false);
}

//	----------------------------------------------------------------------------
//	~operatorDialog
//	----------------------------------------------------------------------------

operatorDialog::~operatorDialog()
{
	delete ui;
}

//	----------------------------------------------------------------------------
//	popMenu1
//	----------------------------------------------------------------------------

void operatorDialog::popMenu1(int id)
{
	if(menuup){
		menuup = 0;
	}else{
		menuup = 1;
		menuid = id;
	}
	menuBox->ep->clicked = 0;
	greyOut();
}

//	----------------------------------------------------------------------------
//	shutdownPi
//	----------------------------------------------------------------------------

void shutdownPi()
{
#ifdef Q_OS_LINUX
	system("shutdown -P now");
#endif
}

//	----------------------------------------------------------------------------
//	setSlavePos
//	----------------------------------------------------------------------------

void setSlavePos()
{
	float pan = gimReadouts.losPan;
	const float pmax0 = 180;
	const float pmax1 = 180 + 360;
	const float pmin0 = -pmax0;
	const float pmin1 = -pmax1;
	if(pan>pmax1){
								pan -= 720;
		}else if(pan>pmax0){	pan -= 360;
		}else if(pan<pmin1){	pan += 720;
		}else if(pan<pmin0){	pan += 360;
	}
	steeringOut.axis[0].desiredPos = pan;
	steeringOut.axis[1].desiredPos = gimReadouts.losTilt;
	cmdSteering = 1;
}

//	----------------------------------------------------------------------------
//	setTarget
//	----------------------------------------------------------------------------

void setTarget(double lat, double lon, double alt)
{
	steeringOut.latitude = lat;
	steeringOut.longitude = lon;
	steeringOut.altitude = alt;
	targetReq = 5;
}

//	----------------------------------------------------------------------------
//	setTarget
//	----------------------------------------------------------------------------

void setTarget(double (*lla)[3])
{
	setTarget((*lla)[0],(*lla)[1],(*lla)[2]);
}

//	----------------------------------------------------------------------------
//	setAltitude
//	----------------------------------------------------------------------------

void setAltitude(double alt)
{
	steeringOut.altitude = alt;
	altReq = 3;
}

//	----------------------------------------------------------------------------
//	setL1
//	----------------------------------------------------------------------------

void setL1()
{
	position1[0] = gimReadouts.lat;
	position1[1] = gimReadouts.lon;
	position1[2] = gimReadouts.alt;
	los1D[0] = gimReadouts.ypr[0];
	los1D[1] = gimReadouts.ypr[1];
	los1D[2] = gimReadouts.ypr[2];
	position2[0] = 0;
	positiont[0] = 0;
}

//	----------------------------------------------------------------------------
//	setL2
//	----------------------------------------------------------------------------

void setL2()
{
	position2[0] = gimReadouts.lat;
	position2[1] = gimReadouts.lon;
	position2[2] = gimReadouts.alt;
	los2D[0] = gimReadouts.ypr[0];
	los2D[1] = gimReadouts.ypr[1];
	los2D[2] = gimReadouts.ypr[2];
	double p[3];
	int res = triangulateTarget(&position1,&los1D,&position2,&los2D,&p);
	if(res){
	//	positionh3[0] = positiont[0];
	//	positionh3[1] = positiont[1];
	//	positionh3[2] = positiont[3];
		positiont[0] = p[0];
		positiont[1] = p[1];
		positiont[2] = p[2];
		setTarget(&positiont);
	}
}

//	----------------------------------------------------------------------------
//	exitMenu
//	----------------------------------------------------------------------------

void operatorDialog::exitMenu()
{
	geomenuup = 0;
	modemenuup = 0;
	shutdownmenuup = 0;
	sliderup = 0;
	tmenuup = 0;
	menuup = 0;
	mapselectup = 0;
	greyOut();
}

//	----------------------------------------------------------------------------
//	settingsFromButtons
//	----------------------------------------------------------------------------

void operatorDialog::settingsFromButtons()
{
	operSettings.llaS1[0] = geoTargetSButton_01->lat; operSettings.llaS1[1] = geoTargetSButton_01->lon; operSettings.llaS1[2] = geoTargetSButton_01->alt;
	operSettings.llaS2[0] = geoTargetSButton_02->lat; operSettings.llaS2[1] = geoTargetSButton_02->lon;	operSettings.llaS2[2] = geoTargetSButton_02->alt;
	operSettings.llaS3[0] = geoTargetSButton_03->lat; operSettings.llaS3[1] = geoTargetSButton_03->lon; operSettings.llaS3[2] = geoTargetSButton_03->alt;
	operSettings.llaS4[0] = geoTargetSButton_04->lat; operSettings.llaS4[1] = geoTargetSButton_04->lon; operSettings.llaS4[2] = geoTargetSButton_04->alt;
	operSettings.llaS5[0] = geoTargetSButton_05->lat; operSettings.llaS5[1] = geoTargetSButton_05->lon; operSettings.llaS5[2] = geoTargetSButton_05->alt;
}

//	----------------------------------------------------------------------------
//	popMenu
//	----------------------------------------------------------------------------

void operatorDialog::popMenu(int index)
{
	int id = buttonsX[index]->id;

	switch(id){

	case B_exitButton_:
		exitMenu();
		break;
	case B_menuButton_:
		popMenu1(id);
		break;
	case B_geoTargetSButton_01_:
	case B_geoTargetSButton_02_:
	case B_geoTargetSButton_03_:
	case B_geoTargetSButton_04_:
	case B_geoTargetSButton_05_:
		tmenuid = id;
		tmenuup = 1;
		tmenuindex = index;
		break;
	case B_menuTButton_01_:{	// load
			buttonX *b = NULL;
			switch(tmenuid){
				case B_geoTargetSButton_01_:		b = geoTargetSButton_01;		break;
				case B_geoTargetSButton_02_:		b = geoTargetSButton_02;		break;
				case B_geoTargetSButton_03_:		b = geoTargetSButton_03;		break;
				case B_geoTargetSButton_04_:		b = geoTargetSButton_04;		break;
				case B_geoTargetSButton_05_:		b = geoTargetSButton_05;		break;
			}
			if(b != NULL){
				setTarget(b->lat,b->lon,b->alt);
				operSettings.geoAlt = b->alt;
				newOperatorSettings = 1;
				mapReadout();
			}
		}
		exitMenu();
		break;
	case B_menuTButton_02_:{	// save
			buttonX *b = NULL;
			switch(tmenuid){
				case B_geoTargetSButton_01_:		b = geoTargetSButton_01;		break;
				case B_geoTargetSButton_02_:		b = geoTargetSButton_02;		break;
				case B_geoTargetSButton_03_:		b = geoTargetSButton_03;		break;
				case B_geoTargetSButton_04_:		b = geoTargetSButton_04;		break;
				case B_geoTargetSButton_05_:		b = geoTargetSButton_05;		break;
			}
			if(b != NULL){

				b->setLL(gimReadouts.targetLat,gimReadouts.targetLon,gimReadouts.targetAlt);
				newOperatorSettings = 1;
				mapReadout();
				switch(tmenuid){
					case B_geoTargetSButton_01_:
						operSettings.llaS1[0] = b->lat;
						operSettings.llaS1[1] = b->lon;
						operSettings.llaS1[2] = b->alt;
						break;
					case B_geoTargetSButton_02_:
						operSettings.llaS2[0] = b->lat;
						operSettings.llaS2[1] = b->lon;
						operSettings.llaS2[2] = b->alt;
						break;
					case B_geoTargetSButton_03_:
						operSettings.llaS3[0] = b->lat;
						operSettings.llaS3[1] = b->lon;
						operSettings.llaS3[2] = b->alt;
						break;
					case B_geoTargetSButton_04_:
						operSettings.llaS4[0] = b->lat;
						operSettings.llaS4[1] = b->lon;
						operSettings.llaS4[2] = b->alt;
						break;
					case B_geoTargetSButton_05_:
						operSettings.llaS5[0] = b->lat;
						operSettings.llaS5[1] = b->lon;
						operSettings.llaS5[2] = b->alt;
						break;
				}
			}
		}
		exitMenu();
		break;
	case B_menuTButton_03_:		// reset
		buttonsX[tmenuindex]->setLL(0,0,0);
		settingsFromButtons();
		newOperatorSettings = 1;
		exitMenu();
		break;
	case B_menuTButton_04_:		// cancel
		exitMenu();
		break;
	case B_alarmButton_:
		setWindowState(windowState() ^ Qt::WindowFullScreen);
		break;
	case B_insButton_:
		insreadoutup = (insreadoutup)?(0):(1);
		break;
	case B_camButton_:
		camRecordReq = 20;
		break;
	case B_aidButton_:
	case B_mapButton_Aid_:
		operSettings.geoAid = (operSettings.geoAid)?(0):(1);
		newOperatorSettings = 1;
		break;
	case B_dynButton_:
	case B_panButton_:
	case B_tiltButton_:
	case B_rollButton_:
	case B_zoomButton_:
	case B_focusButton_:
	case B_zmulButton_:
	case B_geoAltButton_:
	case B_mapButton_Alt_:
		if(sliderButton->gp->isVisible()){
			sliderKnob->setVisible(false);
			slideractive = 0;
			sliderup = 0;
		}else{
			sliderKnob->clicked = 0;
			slideractive = 0;
			sliderup = 1;
			sliderid = id;
			sliderindex = index;
			sliderKnob->setVisible(true);
			newOperatorSettings = 1;
			qreal v = 0;
			switch(id){
				case B_dynButton_:		v = operSettings.dynGain;  break;
				case B_panButton_:		v = operSettings.panGain;  break;
				case B_tiltButton_:		v = operSettings.tiltGain; break;
				case B_rollButton_:		v = operSettings.rollGain; break;
				case B_zoomButton_:		v = operSettings.zoomGain; break;
				case B_focusButton_:	v = operSettings.focusGain;break;
				case B_zmulButton_:		v = operSettings.zmultGain;break;
				case B_geoAltButton_:
				case B_mapButton_Alt_:
					v = 0.5;
					geoAltBase = operSettings.geoAlt;
					break;
			}
			qreal m = sliderButton->lbound+sliderButton->r1;
			qreal s = sliderButton->rbound-sliderButton->lbound-2*sliderButton->r1;
			qreal x = v*s + m;
			qreal y = (sliderButton->tbound+sliderButton->bbound)/2;
			sliderKnob->setPos(x,y);
			greyOut();
		}
		break;
	case B_irisButton_:
		operSettings.irisRev = (operSettings.irisRev )?(0):(1);
		newOperatorSettings = 1;
		break;
	case B_xyzButton_:
		switch(operSettings.encFunction){
			case XYZ_Camera:	operSettings.encFunction = XYZ_Altitude; break;
			case XYZ_Altitude:	operSettings.encFunction = XYZ_Camera;	break;
		}
		newOperatorSettings = 1;
		break;
	case B_menuButton_01_:
		menuup = 0;
		shutdownmenuup = 1;
		greyOut();
		break;
	case B_mapButton_Mode_:
	case B_modemenuButton_:
		modemenuup = (modemenuup)?(0):(1);
		modemenuindex = index;
		greyOut();
		break;
	case B_mapButton_:
		mapup = 1;
		greyOut();
		break;
	case B_mapButton_Sat_:
		mapsat = (mapsat<(numpixmaps-1))?(mapsat+1):(0);
		newOperatorSettings = 1;
		greyOut();
		break;
	case B_mapButton_Select_:
		mapselectup = (mapselectup)?(0):(1);
		mapReadout();
		break;
	case B_mapButton_Hist_:
		maphistoryup = (maphistoryup)?(0):(1);
		mapReadout();
		break;
	case B_wrapButton_:
	case B_wrapMapButton_:{
			int res = 0;
			float yaw = gimReadouts.yaw;
			if(yaw >180){
				res = 2;
				while(yaw > 180)yaw -= 360;
			}else if(yaw < -180){
				res = 1;
				while(yaw < -180)yaw += 360;
			}
			if(res){
				unwrapReq = res;
				unwrapTarg = yaw;
			}
			unwrapMode = gimReadouts.modeX;
			unwrapLLA[0] = gimReadouts.targetLat;
			unwrapLLA[1] = gimReadouts.targetLon;
			unwrapLLA[2] = gimReadouts.targetAlt;
			setModeX(MODEX_RATE);
		}
		break;

	case B_stowButton_:
		setModeX(MODEX_STOW);
		break;

	case B_menuButton_02_:
		setWindowState(windowState() ^ Qt::WindowFullScreen);
		exitMenu();
		break;

	case B_menuButton_03_:
		operSettings.jzFunction =(operSettings.jzFunction == JSZ_Altitude)?(JSZ_Roll):(JSZ_Altitude);
		newOperatorSettings = 1;
		exitMenu();
		break;

	case B_menuButton_04_:
		setSlavePos();
		exitMenu();
		break;

	case B_menuButton_05_:
		exitMenu();
		break;

	case B_shutdownButton_01_: shutdownRedReq = 20; shutdownStandbyReq =  0; shutdownPiReq = 0; exitMenu(); break;
	case B_shutdownButton_02_: shutdownRedReq =  0; shutdownStandbyReq = 20; shutdownPiReq = 1; exitMenu(); break;;
	case B_shutdownButton_03_: shutdownRedReq = 20; shutdownStandbyReq = 20; shutdownPiReq = 1; exitMenu(); break;
	case B_shutdownButton_04_:																	exitMenu(); break;

	case B_modemenuButton_Rate_:	setModeX( MODEX_RATE	); exitMenu(); break;
	case B_modemenuButton_Slave_:	setModeX( MODEX_SLAVE	); exitMenu(); break;
	case B_modemenuButton_Geo_:		setModeX( MODEX_GEO		); exitMenu(); break;
	case B_modemenuButton_Outer_:	setModeX( MODEX_OUTER	); exitMenu(); break;
	case B_modemenuButton_Stow_:	setModeX( MODEX_STOW	); exitMenu(); break;
	case B_modemenuButton_Standby_:	setModeX( MODEX_STANDBY	); exitMenu(); break;

	case B_geomenuButton_Targets_:	mapup = 1; exitMenu(); break;
	case B_mapButton_Exit_:			mapup = 0; greyOut(); break;
	case B_mapButton_Load_:{
			buttonX *b = NULL;
			switch(selectedtargetid){
				case B_geoTargetButton_01_:		b = geoTargetButton_01;		break;
				case B_geoTargetButton_02_:		b = geoTargetButton_02;		break;
				case B_geoTargetButton_03_:		b = geoTargetButton_03;		break;
				case B_geoTargetButton_04_:		b = geoTargetButton_04;		break;
				case B_geoTargetButton_05_:		b = geoTargetButton_05;		break;
				case B_geoTargetButton_06_:		b = geoTargetButton_06;		break;
				case B_geoTargetButton_07_:		b = geoTargetButton_07;		break;
				case B_geoTargetButton_08_:		b = geoTargetButton_08;		break;
				case B_geoTargetButton_09_:		b = geoTargetButton_09;		break;
				case B_geoTargetButton_10_:		b = geoTargetButton_10;		break;
				case B_geoTargetButton_11_:		b = geoTargetButton_11;		break;
				case B_geoTargetButton_12_:		b = geoTargetButton_12;		break;
				case B_geoTargetButton_13_:		b = geoTargetButton_13;		break;
				case B_geoTargetButton_14_:		b = geoTargetButton_14;		break;
				case B_geoTargetButton_15_:		b = geoTargetButton_15;		break;
				case B_geoTargetButton_16_:		b = geoTargetButton_16;		break;
				case B_acposButton_:			b = acposButton;			break;
				case B_acpos1Button_:			b = acpos1Button;			break;
				case B_acpos2Button_:			b = acpos2Button;			break;
				case B_geoTwoPointButton_:		b = geoTwoPointButton;		break;
				case B_geoTarget2Button_01_:	b = geoTarget2Button_01;	break;
				case B_geoTarget2Button_02_:	b = geoTarget2Button_02;	break;
				case B_geoTarget2Button_03_:	b = geoTarget2Button_03;	break;
				case B_geoTarget2Button_04_:	b = geoTarget2Button_04;	break;
				case B_geoTarget2Button_05_:	b = geoTarget2Button_05;	break;
				case B_geoTargetSButton_01_:	b = geoTargetSButton_01;	break;
				case B_geoTargetSButton_02_:	b = geoTargetSButton_02;	break;
				case B_geoTargetSButton_03_:	b = geoTargetSButton_03;	break;
				case B_geoTargetSButton_04_:	b = geoTargetSButton_04;	break;
				case B_geoTargetSButton_05_:	b = geoTargetSButton_05;	break;
			}
			mapselectup = 0;
			if(b != NULL){
				setTarget(b->lat,b->lon,b->alt);
				operSettings.geoAlt = b->alt;
				newOperatorSettings = 1;
				mapReadout();
			}
		}
		break;
	case B_geoTarget2Button_01_:
	case B_geoTarget2Button_02_:
	case B_geoTarget2Button_03_:
	case B_geoTarget2Button_04_:
	case B_geoTarget2Button_05_:
	case B_geoTargetButton_01_:
	case B_geoTargetButton_02_:
	case B_geoTargetButton_03_:
	case B_geoTargetButton_04_:
	case B_geoTargetButton_05_:
	case B_geoTargetButton_06_:
	case B_geoTargetButton_07_:
	case B_geoTargetButton_08_:
	case B_geoTargetButton_09_:
	case B_geoTargetButton_10_:
	case B_geoTargetButton_11_:
	case B_geoTargetButton_12_:
	case B_geoTargetButton_13_:
	case B_geoTargetButton_14_:
	case B_geoTargetButton_15_:
	case B_geoTargetButton_16_:
	case B_acposButton_:
	case B_acpos1Button_:
	case B_acpos2Button_:
	case B_geoTwoPointButton_:
		selectedtargetid = id;
		break;
	}
}

//	----------------------------------------------------------------------------
//	isViz
//	----------------------------------------------------------------------------

int operatorDialog::isViz(int id)
{
	if(mapselectup){
		switch(id){

			case B_geoTarget2Button_01_:	return (geoTarget2Button_01->lat)?(MS_normal):(MS_grey);
			case B_geoTarget2Button_02_:	return (geoTarget2Button_02->lat)?(MS_normal):(MS_grey);
			case B_geoTarget2Button_03_:	return (geoTarget2Button_03->lat)?(MS_normal):(MS_grey);
			case B_geoTarget2Button_04_:	return (geoTarget2Button_04->lat)?(MS_normal):(MS_grey);
			case B_geoTarget2Button_05_:	return (geoTarget2Button_05->lat)?(MS_normal):(MS_grey);

			case B_geoTargetSButton_01_:	return MS_normal;
			case B_geoTargetSButton_02_:	return MS_normal;
			case B_geoTargetSButton_03_:	return MS_normal;
			case B_geoTargetSButton_04_:	return MS_normal;
			case B_geoTargetSButton_05_:	return MS_normal;

			case B_geoTwoPointButton_:		return (geoTwoPointButton->lat)?(MS_normal):(MS_grey);

			case B_acpos1Button_:			return MS_invisible;
			case B_acpos2Button_:			return MS_invisible;
			case B_acposButton_:			return MS_invisible;
			case B_geoTargetButton_:		return MS_invisible;
			case B_geoPassiveButton_:		return MS_invisible;
			case B_simWaypoint1Button_:		return MS_invisible;
			case B_simWaypoint2Button_:		return MS_invisible;

			case B_wrapMapButton_:			return MS_invisible;
			case B_wrapMapPoly01_:			return MS_invisible;
			case B_wrapMapPoly02_:			return MS_invisible;
		}
	}else{
		switch(id){

			case B_geoTarget2Button_01_:	return ( (maphistoryup) && (geoTarget2Button_01->lat) )?(MS_normal):(MS_invisible);
			case B_geoTarget2Button_02_:	return ( (maphistoryup) && (geoTarget2Button_02->lat) )?(MS_normal):(MS_invisible);
			case B_geoTarget2Button_03_:	return ( (maphistoryup) && (geoTarget2Button_03->lat) )?(MS_normal):(MS_invisible);
			case B_geoTarget2Button_04_:	return ( (maphistoryup) && (geoTarget2Button_04->lat) )?(MS_normal):(MS_invisible);
			case B_geoTarget2Button_05_:	return ( (maphistoryup) && (geoTarget2Button_05->lat) )?(MS_normal):(MS_invisible);

			case B_geoTargetSButton_01_:	return (geoTargetSButton_01->lat)?(MS_normal):(MS_invisible);
			case B_geoTargetSButton_02_:	return (geoTargetSButton_02->lat)?(MS_normal):(MS_invisible);
			case B_geoTargetSButton_03_:	return (geoTargetSButton_03->lat)?(MS_normal):(MS_invisible);
			case B_geoTargetSButton_04_:	return (geoTargetSButton_04->lat)?(MS_normal):(MS_invisible);
			case B_geoTargetSButton_05_:	return (geoTargetSButton_05->lat)?(MS_normal):(MS_invisible);

			case B_acpos1Button_:			return (acpos1Button->lat)?(MS_normal):(MS_invisible);
			case B_acpos2Button_:			return (acpos2Button->lat)?(MS_normal):(MS_invisible);
			case B_geoTwoPointButton_:		return (positiont[0])?(MS_normal):(MS_invisible);

			case B_acposButton_:			return (acposButton->lat)?(MS_normal):(MS_invisible);
			case B_geoTargetButton_:		return (geoTargetButton->lat)?(MS_normal):(MS_invisible);
			case B_geoPassiveButton_:		return (geoPassiveButton->lat)?(MS_normal):(MS_invisible);
			case B_simWaypoint1Button_:		return (gimReadouts.simOn)?(MS_normal):(MS_invisible);
			case B_simWaypoint2Button_:		return (gimReadouts.simOn)?(MS_normal):(MS_invisible);
		}
	}

	return MS_normal;
}

//	----------------------------------------------------------------------------
//	greyOut
//	----------------------------------------------------------------------------

void operatorDialog::greyOut()
{
	if(sliderup){
		for(int i=0;i<nbuttonX;i++){
			int id = buttonsX[i]->id;
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_main:		s = (mapup)?(MS_invisible):(MS_grey);	break;
				case MG_map:		s = (mapup)?(MS_grey):(MS_invisible);	break;
				case MG_slider:		s = MS_normal;							break;
				default:			s = MS_invisible;						break;
			}
			if(id==sliderid)s = MS_highlight;
			buttonsX[i]->setState(s);
		}
	}else if(shutdownmenuup){
		for(int i=0;i<nbuttonX;i++){
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_main:		s = MS_grey;		break;
				case MG_shutdown:	s = MS_normal;		break;
				default:			s = MS_invisible;	break;
			}
			buttonsX[i]->setState(s);
		}
	}else if(geomenuup){
		for(int i=0;i<nbuttonX;i++){
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_main:		s = MS_grey;		break;
				case MG_geo:		s = MS_normal;		break;
				default:			s = MS_invisible;	break;
			}
			buttonsX[i]->setState(s);
		}
	}else if(modemenuup){
		for(int i=0;i<nbuttonX;i++){
			int s;
			switch(buttonsX[i]->group){
				case MG_main:		s = ( mapup)?(MS_invisible):(MS_grey);		break;
				case MG_map:		s = (!mapup)?(MS_invisible):(MS_grey);		break;
				case MG_smarties:	s = MS_grey;								break;
				case MG_mode:		s = MS_normal;								break;
				case MG_exit:		s = MS_normal;								break;
				default:			s = MS_invisible;							break;
			}
			int id = buttonsX[i]->id;
			int idmodex = id - B_modemenuButton_Rate_;
			if((i==modemenuindex)||(gimReadouts.modeX == idmodex)){
				s = MS_highlight;
			}
			buttonsX[i]->setState(s);
		}
	}else if(tmenuup){
		for(int i=0;i<nbuttonX;i++){
			int id = buttonsX[i]->id;
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_map:			s = MS_grey;		break;
				case MG_tmenu:			s = MS_normal;		break;
				default:				s = MS_invisible;	break;
			}
			if(id==tmenuid)s = MS_normal;
			if((s==MS_normal)&&(buttonsX[i]->hover)&&(hoverTime))s=MS_highlight;
			buttonsX[i]->setState(s);
		}
	}else if(menuup){
		for(int i=0;i<nbuttonX;i++){
			int id = buttonsX[i]->id;
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_main:			s = MS_grey;		break;
				case MG_menu:			s = MS_normal;		break;
				default:				s = MS_invisible;	break;
			}
			if(id==menuid)s = MS_highlight;
			if((s==MS_normal)&&(buttonsX[i]->hover)&&(hoverTime))s=MS_highlight;
			buttonsX[i]->setState(s);
		}
	}else if(mapup){
		for(int i=0;i<nbuttonX;i++){
			int id = buttonsX[i]->id;
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:		s = MS_normal;		break;
				case MG_map:			s = isViz(id);		break;
				default:				s = MS_invisible;	break;
			}
			if(id == selectedtargetid)s = MS_highlight;
//			if((s==MS_normal)&&(buttonsX[i]->hover))s=MS_highlight;
			buttonsX[i]->setState(s);
		}
	}else{
		for(int i=0;i<nbuttonX;i++){
			int s;
			switch(buttonsX[i]->group){
				case MG_smarties:
				case MG_main:			s = MS_normal;		break;
				default:				s = MS_invisible;	break;
			}
			if((s==MS_normal)&&(buttonsX[i]->hover)&&(hoverTime))s=MS_highlight;
			buttonsX[i]->setState(s);
		}
	}

	if(mapup){
		for(int i=0;i<numpixmaps;i++){
			(*pixmaps[i])->setVisible((i==mapsat)?(1):(0));
		}
		c1->setVisible(false);
		c2->setVisible(false);
	}else{
		for(int i=0;i<numpixmaps;i++){
			(*pixmaps[i])->setVisible(0);
		}
		c1->setVisible(true);
		c2->setVisible(true);
	}

	mapLos->setVisible(mapup && (!mapselectup) );
	mapL1->setVisible( (mapup) && (position1[0]) && (!mapselectup));
	mapL2->setVisible( (mapup) && (position2[0]) && (!mapselectup));

	if(sliderup || menuup || tmenuup || modemenuup || shutdownmenuup || geomenuup ){
		exitButton->setState(MS_normal);
	}else{
		exitButton->setState(MS_invisible);
	}

	if(sliderup){
		sliderKnob->setVisible(true);
	}else{
		sliderKnob->setVisible(false);
	}

}

//	----------------------------------------------------------------------------
//	readouts
//	----------------------------------------------------------------------------

void operatorDialog::readouts()
{
	QString s;

	static float oldAltT = 0.1;
	if(gimReadouts.targetAlt != oldAltT){
		s.sprintf("%1.0f",		gimReadouts.targetAlt);	centerText(&geoAltButton->t2,s);
		s.sprintf("%1.0f",		gimReadouts.targetAlt);	centerText(&mapButton_Alt->t2,s,100);
		oldAltT = gimReadouts.targetAlt;
	}

	int alt = (int)gimReadouts.alt;
	static int oldAlt = -100000;
	if(alt != oldAlt){
		s.sprintf("%i",		alt);	centerText(&geoAltButton->t3,s);
		oldAlt = alt;
	}

	int rangeP = (int)gimReadouts.rangeP;
	if(gimReadouts.targetAlt > gimReadouts.alt){
		if(gimReadouts.ypr[1]<0){
			rangeP = -1;
		}
	}else{
		if(gimReadouts.ypr[1]>0){
			rangeP = -1;
		}
	}
	static int oldRangeP = -2;
	if(rangeP != oldRangeP){
		if(rangeP<0){
			s.sprintf("---");
		}else{
			s.sprintf("%i",		rangeP);
		}
		centerText(&aidButton->t3,s);
		oldRangeP = rangeP;
	}

	if(newOperatorSettings){
		s.sprintf("%1.0f%%",100*operSettings.panGain);	centerText(&panButton->t2,s);
		s.sprintf("%1.0f%%",100*operSettings.tiltGain);	centerText(&tiltButton->t2,s);
		s.sprintf("%1.0f%%",100*operSettings.rollGain);	centerText(&rollButton->t2,s);
		s.sprintf("%1.0f%%",100*operSettings.dynGain);	centerText(&dynButton->t2,s);
		s.sprintf("%1.0f%%",100*operSettings.zmultGain);centerText(&zmulButton->t2,s);
		s.sprintf("%1.0f",		gimReadouts.targetAlt);	centerText(&geoAltButton->t2,s);
		s.sprintf("%1.0f",		gimReadouts.targetAlt);	centerText(&mapButton_Alt->t2,s,100);
		s.sprintf("%1.0f%%",100*operSettings.zoomGain);	centerText(&zoomButton->t2,s);
		s.sprintf("%1.0f%%",100*operSettings.focusGain);centerText(&focusButton->t2,s);
		centerText(&panButton->t3,		(operSettings.panRev  )?("REV"):("NRM"));
		centerText(&tiltButton->t3,		(operSettings.tiltRev )?("REV"):("NRM"));
		centerText(&rollButton->t3,		(operSettings.rollRev )?("REV"):("NRM"));
		centerText(&dynButton->t3,		(operSettings.dynOn	  )?( "ON"):("OFF"));
		centerText(&zmulButton->t3,		(operSettings.zmultOn )?( "ON"):("OFF"));
		centerText(&zoomButton->t3,		(operSettings.zoomRev )?("REV"):("NRM"));
		centerText(&focusButton->t3,	(operSettings.focusRev)?("REV"):("NRM"));
		centerText(&irisButton->t2,		(operSettings.irisRev )?("REV"):("NRM"));
		if(gimReadouts.modeX == MODEX_RATE){
		centerText(&mapButton_Aid->t2,	(operSettings.geoAid  )?( "ON"):("OFF"),100);
			centerText(&aidButton->t2,		(operSettings.geoAid  )?( "ON"):("OFF"));
		}else{
			centerText(&mapButton_Aid->t2,	(operSettings.geoAid  )?( "on"):("off"),100);
			centerText(&aidButton->t2,		(operSettings.geoAid  )?( "on"):("off"));
		}
		centerText(&xyzButton->t2,		(operSettings.encFunction == XYZ_Altitude)?("ALT"):("CAM"));
		if(sliderButton01->state != MS_invisible){
			float dx = 50*fixX;
			centerText(&sliderButton01->t1,(buttonsX[sliderindex]->t1.toPlainText()),dx);
			centerText(&sliderButton01->t2,(buttonsX[sliderindex]->t2.toPlainText()),dx);
			centerText(&sliderButton01->t3,(buttonsX[sliderindex]->t3.toPlainText()),dx);
		}
		if(newOperatorSettings == 1){
			saveSettings();
		}
		newOperatorSettings = 0;
		newOperatorSettingsX = 1;

		menuButton03->t1.setPlainText((operSettings.jzFunction == JSZ_Altitude)?( "JS.Z = Alt"):("JS.Z = Roll"));
	}

	static int modex = -1;
	if(gimReadouts.modeX != modex){
		modex = gimReadouts.modeX;
		extern const char *DL_ModeX[];
		centerText(&modeButton->t2,DL_ModeX[modex]);
		centerText(&mapButton_Mode->t2,DL_ModeX[modex],100);
		newOperatorSettings = 1;
	}
}

//	----------------------------------------------------------------------------
//	timed
//	----------------------------------------------------------------------------

void operatorDialog::timed()
{
	extern QElapsedTimer mainTime;
	qint64 tStart = mainTime.elapsed();
	QString s;
	QBrush brush;
	brush.setStyle(Qt::SolidPattern);

	static int oldOpMinimize = opMinimize;
	if(opMinimize != oldOpMinimize){
		if(opMinimize & 1){
			setWindowState(Qt::WindowMinimized);
		}else{
			setWindowState(Qt::WindowFullScreen);
		}
		oldOpMinimize = opMinimize;
	}

	if(hoverTime){
		hoverTime--;
//		if(!hoverTime){
//			newOperatorSettings = 1;
//		}
	}

	float rmax = 159;
	static int iball = 0;
	const int ncount = 11;
	int yaw = (int)gimReadouts.yaw;
	int pitch = (int)gimReadouts.pitch;
	int zoom = (int)(100*gimReadouts.zoom);
	int lastiball = iball;

	static int fpass = 1;
	if(fpass){
		fpass = 0;
		setWindowState(Qt::WindowActive);
		setWindowState(windowState() | Qt::WindowFullScreen);
//		raise();
	}

	readouts();
	insReadout();
	if(mapup)mapReadout();


	if(sliderup){
		if(sliderKnob->wasClicked()){
			slideractive = 1;
			mousereleased = 0;
		}
		if(sliderButton01->ep->wasClicked()){
			int res = 0;
			switch(sliderid){
				case B_panButton_:  operSettings.panRev  = (operSettings.panRev  )?(0):(1); res = 1; break;
				case B_tiltButton_: operSettings.tiltRev = (operSettings.tiltRev )?(0):(1); res = 1; break;
				case B_rollButton_: operSettings.rollRev = (operSettings.rollRev )?(0):(1); res = 1; break;
				case B_dynButton_:	operSettings.dynOn =   (operSettings.dynOn	 )?(0):(1); res = 1; break;
				case B_focusButton_:operSettings.focusRev =(operSettings.focusRev)?(0):(1); res = 1; break;
				case B_zoomButton_: operSettings.zoomRev = (operSettings.zoomRev )?(0):(1); res = 1; break;
				case B_zmulButton_: operSettings.zmultOn = (operSettings.zmultOn )?(0):(1); res = 1; break;
			}
			if(res){
				newOperatorSettings = 1;
				sliderup = 0;
				sliderKnob->setVisible(false);
			}
			mousereleased = 0;
		}
	}
	if(sliderup){
		if(slideractive){
			if(mousereleased){
				mousereleased = 0;
				slideractive = 0;
				sliderup = 0;
				newOperatorSettings = 1;
				sliderKnob->setVisible(false);
			}
		}
	}
	if(sliderup){
		if(slideractive){
			if(mousemoved){
				qreal r = sliderButton->r1;
				qreal xmin = sliderButton->lbound +r;
				qreal xmax = sliderButton->rbound -r;
				qreal x = _min(xmax,_max(xmin,mousepoint.rx()));
				qreal y = (sliderButton->tbound+sliderButton->bbound)/2;
				sliderKnob->setPos(x,y);

				qreal m = xmin;
				qreal s = xmax - xmin;
				qreal v = (x-m)/s;
				switch(sliderid){
					case B_panButton_:		operSettings.panGain   = v;	break;
					case B_tiltButton_:		operSettings.tiltGain  = v;	break;
					case B_rollButton_:		operSettings.rollGain  = v;	break;
					case B_dynButton_:		operSettings.dynGain   = v;	break;
					case B_zoomButton_:		operSettings.zoomGain  = v;	break;
					case B_focusButton_:	operSettings.focusGain = v;	break;
					case B_zmulButton_:		operSettings.zmultGain = v;	break;
					case B_geoAltButton_:
					case B_mapButton_Alt_:
						v = 2.0f*(v-0.5f);
						operSettings.geoAlt = geoAltBase + 1000.0f*v*fabs(v);
						setAltitude(operSettings.geoAlt);
						break;
				}
				newOperatorSettings = 1;
			}
		}
	}

	for(int i=0;i<ncount;i++){
		lastiball = iball;
		int done = 0;
		switch(iball){
			case 0:{
					static int oldYaw1 = 0;
					if(yaw != oldYaw1){
						s.sprintf("%i",yaw);
						centerText(&losPanButton->t2,s);
						centerText(&wrapMapButton->t2,s);
						losPanPoly->ball->setRotation(gimReadouts.yaw);
						drawGeoPoly();
						done = 1;
						oldYaw1 = yaw;
					}
				}
				iball++;
				break;
			case 1:
				iball++;
				break;
			case 2:{
					static int oldYaw = 0;
					if(yaw != oldYaw){
						float ab = _max(-rmax,_min(rmax,gimReadouts.yaw*rmax/650));
						wrapPoly02->ball->setRotation(ab);
						wrapMapPoly02->ball->setRotation(ab);
						done = 1;
						oldYaw = yaw;
					}
				}
				iball++;
				break;

			case 3:{
					static int oldZoom = 0;
					if(zoom != oldZoom){
						zoomPoly->ball->setRotation(-rmax + 2*rmax*gimReadouts.zoom);
						done = 1;
						oldZoom = zoom;
					}
				}
				iball++;
				break;
			case 4:{
					static int oldPitch = 0;
					if(pitch != oldPitch){
						s.sprintf("%i",pitch);
						centerText(&losTiltButton->t2,s);
						done = 1;
						oldPitch = pitch;
					}
				}
				iball++;
				break;
			case 5:{
					static int oldPitch = 0;
					if(pitch != oldPitch){
						losTiltPoly->ball->setRotation(-gimReadouts.pitch + 90);
						done = 1;
						oldPitch = pitch;
					}
				}
				iball++;
				break;
			case 6:
//			if(mapup)mapReadout();
				iball++;
				break;

			default:
				iball = 0;
				break;
		}
		if(done)break;
	}

//	int pop = (sliderup) || (menuup);
//	static int oldpop = 0;
//	if((pop)&&(!oldpop)){
		greyOut();
//	}
//	oldpop = pop;

//	if(mousemoved){
	if(1){

		int hand = 0;

		for(int i=nbuttonX-1;i>=0;i--){
			int clicked = buttonsX[i]->ep->wasClicked();
			int hover = buttonsX[i]->isHover(mousepoint.rx(),mousepoint.ry());
			if(hand)hover = 0;
//			if(!pop){
//				brush.setColor(mcolor((hover)?(M_lightblue):(buttonsX[i]->color)));
//				buttonsX[i]->ep->setBrush(brush);
//			}
			buttonsX[i]->hover = hover;
			hoverId = (hover)?(buttonsX[i]->id):(-1);
			if(clicked&&hover){
				popMenu(i);
			}
			hand |= hover;
		}
		ui->graphicsView->setCursor(QCursor(hand?(Qt::PointingHandCursor):(Qt::ArrowCursor)));
		s.sprintf("%1.0f,%1.0f",mousepoint.x(),mousepoint.y());
		centerText(&camButton->t3,s);
	}


	util.guiTimed = mainTime.elapsed() - tStart;
	static int statscount = 0;
	if(statscount>15){
		statscount = 0;
		util.guiTimedMax = util.guiTimed;
		util.guiTimedIball = lastiball;
	}else if(util.guiTimedMax < util.guiTimed){
		util.guiTimedMax = util.guiTimed;
		util.guiTimedIball = lastiball;
	}
	statscount++;

	if(util.rxConsInputFrame == 0){
		if(!alarmButton->alarm){
			alarmButton->setAlarm(1);
			centerText(&alarmButton->t2,"INPUT");
		}
	}else if(fabs(gimReadouts.angleDif[0]) > 5.0f){
		if(!alarmButton->alarm){
			alarmButton->setAlarm(1);
			centerText(&alarmButton->t2,"DOM ENC");
		}
	}else if(fabs(gimReadouts.angleDif[1]) > 5.0f){
		if(!alarmButton->alarm){
			alarmButton->setAlarm(1);
			centerText(&alarmButton->t2,"VSW ENC");
		}
	}else{
		if(alarmButton->alarm){
			alarmButton->setAlarm(0);
			centerText(&alarmButton->t2,"NONE");
		}
	}
}

//	----------------------------------------------------------------------------
//	centerText
//	----------------------------------------------------------------------------

void operatorDialog::centerText(QGraphicsTextItem *g,QString s)
{
	g->setPlainText(s);
	float w = g->boundingRect().width();
//	g->pos().setX(-w/2);
	g->setX(-w/2);
//	g->setPos(,0);
}

//	----------------------------------------------------------------------------
//	centerText
//	----------------------------------------------------------------------------

void operatorDialog::centerText(QGraphicsTextItem *g,QString s,float offset)
{
	g->setPlainText(s);
	float w = g->boundingRect().width();
	g->setX(-w/2+offset);
}

//	----------------------------------------------------------------------------
//	init
//	----------------------------------------------------------------------------

void operatorDialog::init()
{
	hoverId = -1;

	graphScene = NULL;
	pixI1a = NULL;
	pixI1b = NULL;
	pixI2a = NULL;
	pixI2b = NULL;

	wrapPoly01 = NULL;
	wrapPoly02 = NULL;
	wrapMapPoly01 = NULL;
	wrapMapPoly02 = NULL;
	losPanPoly = NULL;
	losTiltPoly = NULL;
	zoomPoly = NULL;

	focusButton = NULL;
	menuButton = NULL;
	alarmButton = NULL;
	stowButton = NULL;
	zoomButton = NULL;
	zmulButton = NULL;
	wrapButton = NULL;
	wrapMapButton = NULL;
	dynButton = NULL;
	losPanButton = NULL;
	camButton = NULL;
	panButton = NULL;
	tiltButton = NULL;
	rollButton = NULL;
	losTiltButton = NULL;

	operSettings.panGain		= 1;
	operSettings.tiltGain		= 1;
	operSettings.rollGain		= 1;
	operSettings.dynGain		= 0.03f;
	operSettings.zoomGain		= 1;
	operSettings.focusGain		= 1;
	operSettings.zmultGain		= 1;
	operSettings.geoAlt			= 1000;
	operSettings.panRev			= 0;
	operSettings.tiltRev		= 0;
	operSettings.rollRev		= 0;
	operSettings.zoomRev		= 0;
	operSettings.irisRev		= 0;
	operSettings.focusRev		= 0;
	operSettings.dynOn			= 1;
	operSettings.zmultOn		= 1;
	operSettings.mode			= MODE_RATE;
	operSettings.encFunction	= XYZ_Altitude;
	operSettings.jzFunction		= JSZ_Roll;
	operSettings.geoAid			= 0;

	memset(operSettings.llaS1,0,sizeof(operSettings.llaS1));
	memset(operSettings.llaS2,0,sizeof(operSettings.llaS2));
	memset(operSettings.llaS3,0,sizeof(operSettings.llaS3));
	memset(operSettings.llaS4,0,sizeof(operSettings.llaS4));
	memset(operSettings.llaS5,0,sizeof(operSettings.llaS5));

}

//	----------------------------------------------------------------------------
//	initLabels
//	----------------------------------------------------------------------------

void operatorDialog::initLabels()
{
}

//	----------------------------------------------------------------------------
//	makeButton
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeButton(
		float x,
		float y,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(MB_button,x,y,0,0,color,id,t1,t2,t3,f,graphScene,group);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	makeButton2
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeButton2(
		float x,
		float y,
		float h,
		float w,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(MB_button2,x,y,h,w,color,id,t1,t2,t3,f,graphScene,group);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	makeButton3
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeButton3(
		int type,
		float x,
		float y,
		float h,
		float w,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(type,x,y,h,w,color,id,t1,t2,t3,f,graphScene,group);
//	b->gp->setVisible(visible);
	b->gp->setVisible(1);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	makeSlider
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeSlider(
		float x,
		float y,
		float h,
		float w,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(MB_slider,x,y,h,w,color,id,t1,t2,t3,f,graphScene,group);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	makeArc
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeArc(
		float x,
		float y,
		float r1,
		float r2,
		float a1,
		float a2,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(MB_arc,x,y,r1,r2,color,id,t1,t2,t3,f,graphScene,group,a1,a2);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	makeRing
//	----------------------------------------------------------------------------

buttonX *operatorDialog::makeRing(
		float x,
		float y,
		float r1,
		float r2,
		int color,
		int id,
		QString t1,
		QString t2,
		QString t3,
		QFont f,
		int group
		)
{
	buttonX *b = new buttonX;
	b->makeButtonX(MB_ring,x,y,r1,r2,color,id,t1,t2,t3,f,graphScene,group);

	if(nbuttonX<(NBUTTONX-1)){
		buttonsX[nbuttonX] = b;
		nbuttonX++;
	}
	return b;
}

//	----------------------------------------------------------------------------
//	mapReadout
//	----------------------------------------------------------------------------

void operatorDialog::mapReadout()
{
	static int oldmode = gimReadouts.mode;
	if(gimReadouts.mode != MODE_GEO){
		if(oldmode == MODE_GEO){
			pushHistory();
		}
	}
	oldmode = gimReadouts.mode;

	if(mapselectup){
		const float x0 = 50*fixX;
		const float dx = 100*fixX;
		const float y0 = 100*fixY;
		const float dy = 80*fixY;

		float x = x0;
		float y = y0;
		geoTargetButton_01->setPos(x,y); x+= dx;
		geoTargetButton_02->setPos(x,y); x+= dx;
		geoTargetButton_03->setPos(x,y); x+= dx;
		geoTargetButton_04->setPos(x,y); x+= dx;
		geoTargetButton_05->setPos(x,y); x+= dx;
		geoTargetButton_06->setPos(x,y); x+= dx;
		geoTargetButton_07->setPos(x,y); x+= dx;
		geoTargetButton_08->setPos(x,y); x+= dx;

		x = x0;
		y += dy;
		geoTargetButton_09->setPos(x,y); x+= dx;
		geoTargetButton_10->setPos(x,y); x+= dx;
		geoTargetButton_11->setPos(x,y); x+= dx;
		geoTargetButton_12->setPos(x,y); x+= dx;
		geoTargetButton_13->setPos(x,y); x+= dx;
		geoTargetButton_14->setPos(x,y); x+= dx;
		geoTargetButton_15->setPos(x,y); x+= dx;
		geoTargetButton_16->setPos(x,y); x+= dx;

		x = x0;
		y += dy;
		geoTarget2Button_01->setPos(x,y); x+= dx;
		geoTarget2Button_02->setPos(x,y); x+= dx;
		geoTarget2Button_03->setPos(x,y); x+= dx;
		geoTarget2Button_04->setPos(x,y); x+= dx;
		geoTarget2Button_05->setPos(x,y); x+= dx;

		x = x0;
		y += dy;
		geoTargetSButton_01->setPos(x,y); x+= dx;
		geoTargetSButton_02->setPos(x,y); x+= dx;
		geoTargetSButton_03->setPos(x,y); x+= dx;
		geoTargetSButton_04->setPos(x,y); x+= dx;
		geoTargetSButton_05->setPos(x,y); x+= dx;

		x = x0;
		y += dy;
		geoTwoPointButton->setPos(x,y); x+= dx;

	}else{

		int i = 0;
		geoTargetButton_01->setLL(targetsX[i]);i++;
		geoTargetButton_02->setLL(targetsX[i]);i++;
		geoTargetButton_03->setLL(targetsX[i]);i++;
		geoTargetButton_04->setLL(targetsX[i]);i++;
		geoTargetButton_05->setLL(targetsX[i]);i++;
		geoTargetButton_06->setLL(targetsX[i]);i++;
		geoTargetButton_07->setLL(targetsX[i]);i++;
		geoTargetButton_08->setLL(targetsX[i]);i++;
		geoTargetButton_09->setLL(targetsX[i]);i++;
		geoTargetButton_10->setLL(targetsX[i]);i++;
		geoTargetButton_11->setLL(targetsX[i]);i++;
		geoTargetButton_12->setLL(targetsX[i]);i++;
		geoTargetButton_13->setLL(targetsX[i]);i++;
		geoTargetButton_14->setLL(targetsX[i]);i++;
		geoTargetButton_15->setLL(targetsX[i]);i++;
		geoTargetButton_16->setLL(targetsX[i]);i++;

		geoTarget2Button_01->showLL();
		geoTarget2Button_02->showLL();
		geoTarget2Button_03->showLL();
		geoTarget2Button_04->showLL();
		geoTarget2Button_05->showLL();

		geoTargetSButton_01->showLL();
		geoTargetSButton_02->showLL();
		geoTargetSButton_03->showLL();
		geoTargetSButton_04->showLL();
		geoTargetSButton_05->showLL();

		if(position1[0])acpos1Button->setLL(		position1);
		if(position2[0])acpos2Button->setLL(		position2);
		if(positiont[0])geoTwoPointButton->setLL(	positiont);

		acposButton->setLL(			gimReadouts.lat,		gimReadouts.lon,		gimReadouts.alt);
		geoTargetButton->setLL(		gimReadouts.targetLat,	gimReadouts.targetLon,	gimReadouts.targetAlt);
		geoPassiveButton->setLL(	gimReadouts.passiveLat,	gimReadouts.passiveLon,	gimReadouts.alt);

		simWaypoint1Button->setLL(	steeringOut.sim.lla1);
		simWaypoint2Button->setLL(	steeringOut.sim.lla2);

		acposButton->gp->setRotation(gimReadouts.yprA[0]);
	}
	drawMapLos();
	drawMapL1();
	drawMapL2();
}

//  ---------------------------------------------------------------------------------
//  sinD cosD
//  ---------------------------------------------------------------------------------

	float cosD(float ang){ return cosf( (float)(toRad*ang) ); }
	float sinD(float ang){ return sinf( (float)(toRad*ang) ); }

//	----------------------------------------------------------------------------
//	metersToPix
//	----------------------------------------------------------------------------

void metersToPix(float x,float y,float *xP,float *yP)
{
	switch(mapsat){
	case 0:
	default:{
		const float googxscale = 58.0f/200;
		const float xScale =  googxscale*(788.0f/794.0f);
		const float yScale = -googxscale*(173.0f/175.0f);
		*xP = x * xScale + 500 + 64;
		*yP = y * yScale + 500 - 61;
		}
		break;
	case 1:{
		const float googxscale = 72.0f/2000;
		const float xScale =  googxscale*(1012.0f/1012.0f);
		const float yScale = -googxscale*(34.0f/34.0f);
		*xP = x * xScale + 50 + 313;
		*yP = y * yScale + 50 + 365;
		}
		break;
	case 2:{
		const float googxscale = 77.0f/20000;
		const float xScale =  googxscale;
		const float yScale = -googxscale;
		*xP = x * xScale + 50 + 313 + 533 - 28;
		*yP = y * yScale + 50 + 365 + 565 + 10;
		}
		break;
	case 3:{
		const float googxscale = 115.0f/50000;
		const float xScale =  googxscale;
		const float yScale = -googxscale;
		*xP = x * xScale + 50 + 313 + 533 + 10;
		*yP = y * yScale + 50 + 365 + 565 - 163;
		}
		break;
	}
}

//	----------------------------------------------------------------------------
//	latLonToPix
//	----------------------------------------------------------------------------

void latLonToPix(double lat,double lon,float *xP,float *yP)
{
	float x;
	float y;
	latLonToMeters(lat,lon,&x,&y);
	metersToPix(x,y,xP,yP);
}

//	----------------------------------------------------------------------------
//	metersToLatlon
//	----------------------------------------------------------------------------

void metersToLatlon(double x,double y, double *latP, double *lonP)
{
	static double oldLat0 = 0;
	static double kx = 0;
	static double ky = 0;

	if(latitude0 != oldLat0){
		const double Req = 6378137.0;
		const double F = 0.0033528107;
		oldLat0 = latitude0;
		double cosLat = cos(toRad*latitude0);
		double sinLat = sin(toRad*latitude0);
		double sin2Lat = sinLat * sinLat;
		double rNS = Req * (1 + F * (3.0 * sin2Lat - 2));
		double rEW = Req * (1 + F * sin2Lat);
		kx = toDeg/(rEW*cosLat);
		ky = toDeg/rNS;
	}
	*lonP = longitude0 + kx*x;
	*latP = latitude0  + ky*y;
}

//	----------------------------------------------------------------------------
//	latLonToMeters
//	----------------------------------------------------------------------------

int latLonToMeters(double lat,double lon,float *xP,float *yP)
{
	const double Req = 6378137.0;
	const double F = 0.0033528107;
	static double oldLat0 = 0;
	static double cosLat;
	static double sinLat;
	static double sin2Lat;
	static double rNS;
	static double rEW;

	if(latitude0 != oldLat0){
		oldLat0 = latitude0;
		cosLat = cos(latitude0*(PI/180.0));
		sinLat = sin(latitude0*(PI/180.0));
		sin2Lat = sinLat * sinLat;
		rNS = Req * (1 + F * (3.0 * sin2Lat - 2));
		rEW = Req * (1 + F * sin2Lat);
	}
	if(abs(lon-longitude0)>5)return 0;
	if(abs(lat-latitude0)>5)return 0;
	*xP = (float)(rEW*(lon-longitude0)*cosLat*(PI/180.0));
	*yP = (float)(rNS*(lat-latitude0)*(PI/180.0));
	return 1;
}

//	----------------------------------------------------------------------------
//	makeGeoPoly
//	----------------------------------------------------------------------------

void operatorDialog::makeGeoPoly()
{
	return;

	geoPanPoly = new QGraphicsPolygonItem;
	QPen pen;
	pen.setWidthF(6);
	pen.setColor(mcolor(M_yellow));
	geoPanPoly->setPen(pen);
//	geoPanPoly->setVisible(false);
	graphScene->addItem(geoPanPoly);

	geoTiltPoly = new QGraphicsPolygonItem;
	geoTiltPoly->setPen(pen);
	geoTiltPoly->setVisible(false);
	graphScene->addItem(geoTiltPoly);
}

//	----------------------------------------------------------------------------
//	drawGeoPoly
//	----------------------------------------------------------------------------

//buttonX *losPanPoly;
//buttonX *losTiltPoly;

void operatorDialog::drawGeoPoly()
{
	return;

	float x = losPanPoly->x;
	float y = losPanPoly->y;
	QPointF p1(x,y);
	x += gimReadouts.dxTarget;
	y += gimReadouts.dyTarget;
	QPointF p2(x,y);
	QPolygonF poly;
	poly.append(p1);
	poly.append(p2);
	geoPanPoly->setPolygon(poly);
}

//	----------------------------------------------------------------------------
//	makeMapLos
//	----------------------------------------------------------------------------

void operatorDialog::makeMapLos()
{
	mapLos = new QGraphicsPolygonItem;
	QPen pen;
	pen.setWidthF(3);
	pen.setColor(mcolor(M_pink));
	mapLos->setPen(pen);
	mapLos->setVisible(false);
	graphScene->addItem(mapLos);
}

//	----------------------------------------------------------------------------
//	drawMapLos
//	----------------------------------------------------------------------------

void operatorDialog::drawMapLos()
{
	float x = acposButton->x;
	float y = acposButton->y;
	QPointF p1(x,y);

	if(gimReadouts.rangeP > 0){
		x = geoPassiveButton->x;
		y = geoPassiveButton->y;
	}else{
		float r = 200;
		double heading = toRad*gimReadouts.ypr[0];
		x += r*sin(heading);
		y -= r*cos(heading);
	}
	QPointF p2(x,y);
	QPolygonF poly;
	poly.append(p1);
	poly.append(p2);
	mapLos->setPolygon(poly);
}

//	----------------------------------------------------------------------------
//	makeMapL1
//	----------------------------------------------------------------------------

void operatorDialog::makeMapL1()
{
	mapL1 = new QGraphicsPolygonItem;
	QPen pen;
	pen.setWidthF(3);
	pen.setColor(mcolor(M_blue));
	mapL1->setPen(pen);
	mapL1->setVisible(false);
	graphScene->addItem(mapL1);
}

//	----------------------------------------------------------------------------
//	makeMapL2
//	----------------------------------------------------------------------------

void operatorDialog::makeMapL2()
{
	mapL2 = new QGraphicsPolygonItem;
	QPen pen;
	pen.setWidthF(3);
	pen.setColor(mcolor(M_blue));
	mapL2->setPen(pen);
	mapL2->setVisible(false);
	graphScene->addItem(mapL2);
}

//	----------------------------------------------------------------------------
//	drawMapL1
//	----------------------------------------------------------------------------

void operatorDialog::drawMapL1()
{
	if(position1[0] == 0){
		mapL1->setVisible(false);
		return;
	}
	float rm = 1000;
	float xm1;
	float ym1;
	latLonToMeters(position1[0], position1[1], &xm1, &ym1 );
	float losY = los1D[0];
	float xm2 = xm1 + rm*sinD(losY);
	float ym2 = ym1 + rm*cosD(losY);
	float xp;
	float yp;

	metersToPix(xm1,ym1,&xp,&yp);
	QPointF p1(xp,yp);

	if(positiont[0]){
		latLonToPix(positiont[0], positiont[1], &xp, &yp );
	}else{
		metersToPix(xm2,ym2,&xp,&yp);
	}
	QPointF p2(xp,yp);

	QPolygonF poly;
	poly.append(p1);
	poly.append(p2);
	mapL1->setPolygon(poly);
}

//	----------------------------------------------------------------------------
//	drawMapL2
//	----------------------------------------------------------------------------

void operatorDialog::drawMapL2()
{
	if(position2[0] == 0){
		mapL2->setVisible(false);
		return;
	}

	float rm = 1000;
	float xm1;
	float ym1;
	latLonToMeters(position2[0], position2[1], &xm1, &ym1 );
	float losY = los2D[0];
	float xm2 = xm1 + rm*sinD(losY);
	float ym2 = ym1 + rm*cosD(losY);
	float xp;
	float yp;
	metersToPix(xm1,ym1,&xp,&yp);
	QPointF p1(xp,yp);
	metersToPix(xm2,ym2,&xp,&yp);

	if(positiont[0]){
		latLonToPix(positiont[0], positiont[1], &xp, &yp );
	}else{
		metersToPix(xm2,ym2,&xp,&yp);
	}
	QPointF p2(xp,yp);



	QPolygonF poly;
	poly.append(p1);
	poly.append(p2);
	mapL2->setPolygon(poly);
}

//	----------------------------------------------------------------------------
//  rangeFromPit
//	----------------------------------------------------------------------------

double rangeFromPit(geoFix_t *fix)
{
	const double eps = .001*toRad;
	double vehAlt = (double)(fix->p0.alt);
	double h = gimReadouts.alt - vehAlt;
	double pit = toRad*fix->ypr[1];
	if(pit>0){
		pit = _max(pit,eps);
	}else{
		pit = _min(pit,-eps);
	}
	return h/tan(pit);
}

//	----------------------------------------------------------------------------
//  computeFix
//	----------------------------------------------------------------------------

void computeFix(geoFix_t *fix)
{
	if( (fix->state) == (FIX_Empty) )return;

	double lat = fix->p0.lat;
	double lon = fix->p0.lon;
	float x;
	float y;
	latLonToMeters( lat, lon, &x, &y );

	float *pixX =  &(fix->p0.pixX);
	float *pixY =  &(fix->p0.pixY);
	fix->p0.x = x;
	fix->p0.y = y;
	metersToPix( x, y, pixX, pixY );

	double xyRange = rangeFromPit(fix);
	if(xyRange<0){
		(fix->state) = FIX_NoSolution;
		return;
	}else{
		(fix->state) = FIX_OK;
		(fix->xyRange) = xyRange;
	}
	double yaw = toRad*fix->ypr[0];
	double sinyaw = sin(yaw);
	double cosyaw = cos(yaw);
	x += xyRange*sinyaw;
	y += xyRange*cosyaw;

	fix->p1.x = x;
	fix->p1.y = y;
	pixX = &(fix->p1.pixX);
	pixY = &(fix->p1.pixY);
	metersToPix( x, y, pixX, pixY );

	fix->p1.alt = fix->targetAlt;
	double *lat1 = &(fix->p1.lat);
	double *lon1 = &(fix->p1.lon);
	metersToLatlon( x, y, lat1, lon1);

	float dx = (fix->p1.x)   -  (fix->p0.x);
	float dy = (fix->p1.y)   -  (fix->p0.y);
	float dz = (fix->p1.alt) -  (fix->p0.alt);
	fix->range = sqrtf(dx*dx + dy*dy + dz*dz);
}

//	----------------------------------------------------------------------------
//  geoFix_t
//	----------------------------------------------------------------------------

void takeFix(geoFix_t *fix)
{
	fix->state = FIX_Taken;
	fix->p0.lat = gimReadouts.lat;
	fix->p0.lon = gimReadouts.lon;
	fix->p0.alt = gimReadouts.alt;
	fix->ypr[0] = gimReadouts.ypr[0];
	fix->ypr[1] = gimReadouts.ypr[1];
	fix->ypr[2] = gimReadouts.ypr[2];
	fix->targetAlt = gimReadouts.alt;
	computeFix(fix);
}

//	----------------------------------------------------------------------------
//	pushHistory
//	----------------------------------------------------------------------------

void operatorDialog::pushHistory()
{
	if(	(targetHistory[0]->lat == gimReadouts.targetLat)&&
		(targetHistory[0]->lon == gimReadouts.targetLon)&&
		(targetHistory[0]->alt == gimReadouts.targetAlt))
	{
		return;
	}

	int n = _max(0,nTargetHistory - 1);
	int j = n;
	for(int i=0;i<n;i++){
		targetHistory[j]->setLL(targetHistory[j-1]->lat,targetHistory[j-1]->lon,targetHistory[j-1]->alt);
		j--;
	}
	targetHistory[0]->setLL(gimReadouts.targetLat,gimReadouts.targetLon,gimReadouts.targetAlt);
}
