tilesviewer.cpp 36.2 KB
Newer Older
1 2 3 4 5 6 7
#include "tilesviewer.h"
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>
#include <math.h>
#include <QPixmap>
丁劲犇's avatar
丁劲犇 已提交
8 9
#include <QFileInfo>
#include <QDir>
10
#include "layer_interface.h"
丁劲犇's avatar
丁劲犇 已提交
11
#include "interface_utils.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
namespace QTVOSM{
	tilesviewer::tilesviewer(QWidget *parent) :
		QWidget(parent)
	{
		QTVOSM_DEBUG("The tilesviewer class is constructing.");
		this->m_dCenterX = this->m_dCenterY = 0;
		this->m_nLevel = 0;
		this->setMouseTracking(true);
		connect(this,&tilesviewer::_inside_do_next_evts,this,&tilesviewer::_do_next_evts,Qt::QueuedConnection);
		QTVOSM_DEBUG("The tilesviewer class constructed.");
	}

	tilesviewer::~tilesviewer()
	{
		//Call layers to draw images
		QVector<layer_interface * > slayers = layers();
		foreach (layer_interface * pItem, slayers)
			removeLayer(pItem);
	}

	bool tilesviewer::addLayer(layer_interface * ba)
	{
		if (m_setLayers.contains(ba)==true)
			return false;
		m_listLayers.push_back(ba);
		m_setLayers.insert(ba);
		return true;
	}
	void tilesviewer::removeLayer(layer_interface * ba )
	{
		if (m_setLayers.contains(ba)==false)
			return;
		m_listLayers.removeOne(ba);
		m_setLayers.remove(ba);
	}
	layer_interface * tilesviewer::layer(const QString & name)
	{
		layer_interface * res = 0;
		foreach (layer_interface * pItem, m_listLayers)
		{
			if (name ==pItem->get_name())
			{
				res = pItem;
				break;
			}
		}
		return res;
	}
	bool tilesviewer::adjust_layers(layer_interface * la )
	{
		QVector <layer_interface *> vec_layers = layers();
		bool activeb = la->is_active();
		bool tarExclusive = false;
		for (int i=0;i<vec_layers.size();++i)
		{
			//It's exclusive, there should be at most only one layer_tiles active
			if (vec_layers[i] == la )
				tarExclusive = vec_layers[i]->is_exclusive();
		}
		if (tarExclusive && activeb)
		{
			for (int i=0;i<vec_layers.size();++i)
			{
				//It's exclusive, there should be only one layer_tiles active
				if (vec_layers[i] != la  && vec_layers[i]->is_exclusive()==true)
					vec_layers[i]->set_active(false);
			}
		}

		return true;
	}

	QVector<layer_interface * > tilesviewer::layers()
	{
		QVector<layer_interface * > vec_layers;
		//Call layers to draw images
		foreach (layer_interface * pItem, m_listLayers)
			vec_layers.push_back(pItem);
		return vec_layers;
	}

	QVector<QString> tilesviewer::layerNames()
	{
		QVector<QString> vec_layers;
		//Call layers to draw images
		foreach (layer_interface * pItem, m_listLayers)
			vec_layers.push_back(pItem->get_name());
		return vec_layers;
	}

	QVector<bool> tilesviewer::layerVisibilities()
	{
		QVector<bool> vec_layers;
		//Call layers to draw images
		foreach (layer_interface * pItem, m_listLayers)
			vec_layers.push_back(pItem->is_visible());
		return vec_layers;
	}

	QVector<bool> tilesviewer::layerActivities()
	{
		QVector<bool> vec_layers;
		//Call layers to draw images
		foreach (layer_interface * pItem, m_listLayers)
			vec_layers.push_back(pItem->is_active());
		return vec_layers;
	}

	void tilesviewer::moveLayerUp(layer_interface * ly)
	{
		int n = m_listLayers.indexOf(ly);
		if (n>0 && n<m_listLayers.size())
		{
			m_listLayers.removeAt(n);
			m_listLayers.insert(n-1,ly);
			//! 1. source=MAIN_MAP,  destin = ALL, msg = LAYER_MOVED_UP
			if (this->isEnabled())
			{
				QMap<QString, QVariant> map_evt;
				map_evt["source"] = "MAIN_MAP";
				map_evt["destin"] = "ALL";
				map_evt["name"] = "LAYER_MOVED_UP";
				map_evt["layerName"] = ly->get_name();
				post_event(map_evt);
			}
		}

	}

	void tilesviewer::moveLayerDown(layer_interface * ly)
	{
		int n = m_listLayers.indexOf(ly);
		if (n>=0 && n + 1 < m_listLayers.size())
		{
			m_listLayers.removeAt(n);
			m_listLayers.insert(n+1,ly);
			//! 1. source=MAIN_MAP,  destin = ALL, msg = LAYER_MOVED_DOWN
			if (this->isEnabled())
			{
				QMap<QString, QVariant> map_evt;
				map_evt["source"] = "MAIN_MAP";
				map_evt["destin"] = "ALL";
				map_evt["name"] = "LAYER_MOVED_DOWN";
				map_evt["layerName"] = ly->get_name();
				post_event(map_evt);
			}
		}

	}

	void tilesviewer::moveLayerTop(layer_interface * ly)
	{
		int n = m_listLayers.indexOf(ly);
		if (n>0 && n<m_listLayers.size())
		{
			m_listLayers.removeAt(n);
			m_listLayers.push_front(ly);
			//! 1. source=MAIN_MAP,  destin = ALL, msg = LAYER_MOVED_TOP
			if (this->isEnabled())
			{
				QMap<QString, QVariant> map_evt;
				map_evt["source"] = "MAIN_MAP";
				map_evt["destin"] = "ALL";
				map_evt["name"] = "LAYER_MOVED_TOP";
				map_evt["layerName"] = ly->get_name();

				post_event(map_evt);
			}
		}
	}

	void tilesviewer::moveLayerBottom(layer_interface *ly)
	{
		int n = m_listLayers.indexOf(ly);
		if (n>=0 && n+1<m_listLayers.size())
		{
			m_listLayers.removeAt(n);
			m_listLayers.push_back(ly);
			//! 1. source=MAIN_MAP,  destin = ALL, msg = LAYER_MOVED_BOTTOM
			if (this->isEnabled())
			{
				QMap<QString, QVariant> map_evt;
				map_evt["source"] = "MAIN_MAP";
				map_evt["destin"] = "ALL";
				map_evt["name"] = "LAYER_MOVED_BOTTOM";
				map_evt["layerName"] = ly->get_name();
				post_event(map_evt);
			}
		}
	}
	void tilesviewer::updateLayerGridView()
	{
		emit cmd_update_layer_box();
	}

	void tilesviewer::paintEvent( QPaintEvent * /*event*/ )
	{
		QPainter painter(this);
		QBrush br_back(QColor(192,192,192),Qt::Dense5Pattern);
		painter.setBackground(br_back);
		painter.eraseRect(this->rect());

		//Call layers to draw images
		foreach (layer_interface * pItem, m_listLayers)
			pItem->cb_paintEvent(&painter);

		//Draw center mark
		QPen pen(Qt::DotLine);
		pen.setColor(QColor(0,0,255,128));
		painter.setPen(pen);
		painter.drawLine(
					width()/2+.5,height()/2+.5-32,
					width()/2+.5,height()/2+.5+32
					);
		painter.drawLine(
					width()/2+.5-32,height()/2+.5,
					width()/2+.5+32,height()/2+.5
					);


	}


	//public slots for resolution changed events
	void tilesviewer::setLevel(int n)
	{
		bool needUpdate = m_nLevel==n?false:true;
		if (needUpdate==false)
			return;

		this->m_nLevel = n;

		//Call layers
		foreach (layer_interface * pItem, m_listLayers)
			pItem->cb_levelChanged(n);
		update();
		emit evt_level_changed(m_nLevel);

		//! 1. source=MAIN_MAP,  destin = ALL, msg = LEVEL_CHANGED
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "ALL";
			map_evt["name"] = "LEVEL_CHANGED";
			map_evt["nLevel"] = n;
			post_event(map_evt);
		}
	}


	//public slots for resolution changed events
	void tilesviewer::setBrLevel(int n)
	{
		this->m_nLevel = n - 6;
		if (m_nLevel < 0)
			m_nLevel = 0;
		else
		{
			//Call layers
			foreach (layer_interface * pItem, m_listLayers)
				pItem->cb_levelChanged(n);
			update();
		}

	}

	void tilesviewer::UpdateWindow()
	{
		update();
	}

	void tilesviewer::mousePressEvent ( QMouseEvent * event )
	{
		//Call layers
		int needUpdate = 0;
		foreach (layer_interface * pItem, m_listLayers)
			needUpdate +=  pItem->cb_mousePressEvent(event)==true?1:0;
		if (needUpdate>0)
			this->update();

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MOUSE_LBUTTON_DOWN
		//! 2. source=MAIN_MAP,  destin = ALL, msg = MOUSE_RBUTTON_DOWN
		//! 3. source=MAIN_MAP,  destin = ALL, msg = MOUSE_MBUTTON_DOWN
		//! 4. source=MAIN_MAP,  destin = ALL, msg = MOUSE_BUTTON_DOWN
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			if (event->buttons() & Qt::LeftButton)
				map_evt["name"] = "MOUSE_LBUTTON_DOWN";
			else if (event->buttons() & Qt::RightButton)
				map_evt["name"] = "MOUSE_RBUTTON_DOWN";
			else if (event->buttons() & Qt::MidButton)
				map_evt["name"] = "MOUSE_MBUTTON_DOWN";
			else
				map_evt["name"] = "MOUSE_BUTTON_DOWN";
			double tlat, tlon;
			CV_DP2LLA(event->pos().x(),event->pos().y(),&tlat,&tlon);
丁劲犇's avatar
丁劲犇 已提交
312 313
			map_evt["lat"] = tlat;
			map_evt["lon"] = tlon;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
			map_evt["nLevel"] = m_nLevel;
			post_event(map_evt);

			//test coord cnt
			/**/
			if (false)
			{
				QString strMsg;
				int dpx = event->pos().x(), dpy = event->pos().y();
				strMsg += QString("DP(%1,%2)").arg(dpx).arg(dpy);
				double wdx,wdy;
				CV_DP2World(dpx,dpy,&wdx,&wdy);
				strMsg += QString("->WD(%1,%2)").arg(wdx).arg(wdy);
				CV_World2DP(wdx,wdy,&dpx,&dpy);
				strMsg += QString("->DP(%1,%2)").arg(dpx).arg(dpy);
				double lat,lon;
				CV_World2LLA(wdx,wdy,&lat,&lon);
				strMsg += QString("->LLA(%1,%2)").arg(lat).arg(lon);
				CV_LLA2World(lat,lon,&wdx,&wdy);
				strMsg += QString("->WD(%1,%2)").arg(wdx).arg(wdy);

				CV_DP2LLA(dpx,dpy,&lat,&lon);
				strMsg += QString("->LLA(%1,%2)").arg(lat).arg(lon);
				CV_LLA2DP(lat,lon,&dpx,&dpy);
				strMsg += QString("->DP(%1,%2)").arg(dpx).arg(dpy);
				double mkx,mky;
				CV_LLA2MK(lat,lon,&mkx,&mky);
				strMsg += QString("->MK(%1,%2)").arg(mkx).arg(mky);
				CV_MK2LLA(mkx,mky,&lat,&lon);
				strMsg += QString("->LLA(%1,%2)").arg(lat).arg(lon);

				CV_MK2World(mkx,mky,&wdx,&wdy);
				strMsg += QString("->WD(%1,%2)").arg(wdx).arg(wdy);
				CV_World2MK(wdx,wdy,&mkx,&mky);
				strMsg += QString("->MK(%1,%2)").arg(mkx).arg(mky);

				double pctx,pcty;
				CV_World2Pct(wdx,wdy,&pctx,&pcty);
				strMsg += QString("->PCT(%1,%2)").arg(pctx).arg(pcty);
				CV_Pct2World(pctx,pcty,&wdx,&wdy);
				strMsg += QString("->WD(%1,%2)").arg(wdx).arg(wdy);

				qDebug()<<strMsg;
			}
		}

		QWidget::mousePressEvent(event);

	}
	void tilesviewer::resizeEvent ( QResizeEvent * event )
	{
		//Call layers
		foreach (layer_interface * pItem, m_listLayers)
			pItem->cb_resizeEvent(event);
		update();

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MAP_RESIZED
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			map_evt["name"] = "MAP_RESIZED";
			map_evt["width"] = event->size().width();
			map_evt["height"] = event->size().height();
			post_event(map_evt);
		}

		QWidget::resizeEvent(event);
	}

	void tilesviewer::mouseReleaseEvent ( QMouseEvent * event )
	{
		//Call layers
		int needUpdate = 0;
		foreach (layer_interface * pItem, m_listLayers)
			needUpdate += pItem->cb_mouseReleaseEvent(event)==true?1:0;
		if (needUpdate>0)
			this->update();

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MOUSE_LBUTTON_UP
		//! 2. source=MAIN_MAP,  destin = ALL, msg = MOUSE_RBUTTON_UP
		//! 3. source=MAIN_MAP,  destin = ALL, msg = MOUSE_MBUTTON_UP
		//! 4. source=MAIN_MAP,  destin = ALL, msg = MOUSE_BUTTON_UP
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			if (event->buttons() & Qt::LeftButton)
				map_evt["name"] = "MOUSE_LBUTTON_UP";
			else if (event->buttons() & Qt::RightButton)
				map_evt["name"] = "MOUSE_RBUTTON_UP";
			else if (event->buttons() & Qt::MidButton)
				map_evt["name"] = "MOUSE_MBUTTON_UP";
			else
				map_evt["name"] = "MOUSE_BUTTON_UP";
			double tlat, tlon;
			CV_DP2LLA(event->pos().x(),event->pos().y(),&tlat,&tlon);
丁劲犇's avatar
丁劲犇 已提交
413 414
			map_evt["lat"] = tlat;
			map_evt["lon"] = tlon;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
			map_evt["nLevel"] = m_nLevel;
			post_event(map_evt);
		}


		QWidget::mouseReleaseEvent(event);
	}

	void tilesviewer::mouseDoubleClickEvent(QMouseEvent * event)
	{
		//Call layers
		int needUpdate = false;
		foreach (layer_interface * pItem, m_listLayers)
			needUpdate += pItem->cb_mouseDoubleClickEvent(event)==true?1:0;
		if (needUpdate>0)
			this->update();

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MOUSE_LBUTTON_DBLCLK
		//! 2. source=MAIN_MAP,  destin = ALL, msg = MOUSE_RBUTTON_BLCLK
		//! 3. source=MAIN_MAP,  destin = ALL, msg = MOUSE_MBUTTON_BLCLK
		//! 4. source=MAIN_MAP,  destin = ALL, msg = MOUSE_BUTTON_BLCLK
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			if (event->buttons() & Qt::LeftButton)
				map_evt["name"] = "MOUSE_LBUTTON_DBLCLK";
			else if (event->buttons() & Qt::RightButton)
444
				map_evt["name"] = "MOUSE_RBUTTON_DBLCLK";
445
			else if (event->buttons() & Qt::MidButton)
446
				map_evt["name"] = "MOUSE_MBUTTON_DBLCLK";
447
			else
448
				map_evt["name"] = "MOUSE_BUTTON_DBLCLK";
449 450
			double tlat, tlon;
			CV_DP2LLA(event->pos().x(),event->pos().y(),&tlat,&tlon);
丁劲犇's avatar
丁劲犇 已提交
451 452
			map_evt["lat"] = tlat;
			map_evt["lon"] = tlon;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
			map_evt["nLevel"] = m_nLevel;
			post_event(map_evt);
		}


		QWidget::mouseDoubleClickEvent(event);
	}

	void tilesviewer::mouseMoveEvent(QMouseEvent * event)
	{
		//Call layers
		int needUpdate = 0;
		foreach (layer_interface * pItem, m_listLayers)
			needUpdate += pItem->cb_mouseMoveEvent(event)==true?1:0;
		if (needUpdate)
			this->update();
		double lat,lon;
		CV_DP2LLA(event->x(),event->y(),&lat,&lon);

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MOUSE_MOVE
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			map_evt["name"] = "MOUSE_MOVE";
			double tlat, tlon;
			CV_DP2LLA(event->pos().x(),event->pos().y(),&tlat,&tlon);
丁劲犇's avatar
丁劲犇 已提交
481 482
			map_evt["lat"] = tlat;
			map_evt["lon"] = tlon;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
			map_evt["nLevel"] = m_nLevel;
			map_evt["mask"] = (quint32)event->buttons();
			post_event(map_evt);
		}

		QWidget::mouseMoveEvent(event);
	}

	void tilesviewer::wheelEvent ( QWheelEvent * event )
	{
		//Call layers
		int needUpdate = 0;
		foreach (layer_interface * pItem, m_listLayers)
			needUpdate += pItem->cb_wheelEvent(event);
		if (needUpdate)
			this->update();

		//! 1. source=MAIN_MAP,  destin = ALL, msg = MOUSE_WHELL
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			map_evt["name"] = "MOUSE_WHELL";
			double tlat, tlon;
			CV_DP2LLA(event->pos().x(),event->pos().y(),&tlat,&tlon);
丁劲犇's avatar
丁劲犇 已提交
509 510
			map_evt["lat"] = tlat;
			map_evt["lon"] = tlon;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
			map_evt["nLevel"] = m_nLevel;
			map_evt["pDeltaX"] = event->pixelDelta().x();
			map_evt["pDeltaY"] = event->pixelDelta().y();
			map_evt["aDeltaX"] = event->angleDelta().x();
			map_evt["aDeltaY"] = event->angleDelta().y();
			post_event(map_evt);
		}


		QWidget::wheelEvent(event);
	}

	void tilesviewer::DragView(int nOffsetX,int nOffsetY)
	{
		if (nOffsetX==0 && nOffsetY == 0)
			return;

		int sz_whole_idx = 1<<m_nLevel;
		int sz_whole_size = sz_whole_idx*256;

		double dx = nOffsetX*1.0/sz_whole_size;
		double dy = nOffsetY*1.0/sz_whole_size;

		this->m_dCenterX -= dx;
		this->m_dCenterY -= dy;

		while (m_dCenterX<-0.5)
			m_dCenterX += 1;
		while (m_dCenterX>0.5)
			m_dCenterX -= 1;
		if (m_dCenterY<-0.5)
			m_dCenterY = -0.5;
		if (m_dCenterY>0.5)
			m_dCenterY = 0.5;

		double lat,lon;
		CV_DP2LLA(width()/2,height()/2,&lat,&lon);
		emit evt_center_changed(lat,lon);

		//! 1. source=MAIN_MAP,  destin = ALL, msg = CENTER_CHANGED
		if (this->isEnabled())
		{
			QMap<QString, QVariant> map_evt;
			map_evt["source"] = "MAIN_MAP";
			map_evt["destin"] = "OUTER";
			map_evt["name"] = "CENTER_CHANGED";
丁劲犇's avatar
丁劲犇 已提交
557 558
			map_evt["lat"] = lat;
			map_evt["lon"] = lon;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
			map_evt["nLevel"] = m_nLevel;
			post_event(map_evt);
		}


	}
	//set center LLA
	void tilesviewer::setCenterLLA(double lat, double lon)
	{
		//To Mercator Projection
		double dMx = cProjectionMercator(lat,lon).ToMercator().m_x;
		double dMy = cProjectionMercator(lat,lon).ToMercator().m_y;

		//Calculat the percentage pos (-0.5-0.5) of the Mercator point
		double dCtX = dMx/(cProjectionMercator::pi*cProjectionMercator::R*2);
		double dCtY =  -dMy/(cProjectionMercator::pi*cProjectionMercator::R*2);


		while (dCtX<-0.5)
			dCtX += 1;
		while (dCtX>0.5)
			dCtX -= 1;
		if (dCtY<-0.5)
			dCtY = -0.5;
		if (dCtY>0.5)
			dCtY = 0.5;

		if (m_dCenterX != dCtX || m_dCenterY != dCtY)
		{
			this->m_dCenterX = dCtX;
			this->m_dCenterY = dCtY;
			//Call layers
			update();
			emit evt_center_changed(lat,lon);
			//! 1. source=MAIN_MAP,  destin = ALL, msg = CENTER_CHANGED
			if (this->isEnabled())
			{
				QMap<QString, QVariant> map_evt;
				map_evt["source"] = "MAIN_MAP";
				map_evt["destin"] = "OUTER";
				map_evt["name"] = "CENTER_CHANGED";
				centerLLA(&lat,&lon);
丁劲犇's avatar
丁劲犇 已提交
601 602
				map_evt["lat"] = lat;
				map_evt["lon"] = lon;
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
				map_evt["nLevel"] = m_nLevel;
				post_event(map_evt);
			}
		}
	}
	//set center LLA, not emit centerChanged
	void tilesviewer::setBrCenterLLA(double lat, double lon)
	{
		//To Mercator Projection
		double dMx = cProjectionMercator(lat,lon).ToMercator().m_x;
		double dMy = cProjectionMercator(lat,lon).ToMercator().m_y;

		//Calculat the percentage pos (-0.5-0.5) of the Mercator point
		double dCtX = dMx/(cProjectionMercator::pi*cProjectionMercator::R*2);
		double dCtY =  -dMy/(cProjectionMercator::pi*cProjectionMercator::R*2);


		while (dCtX<-0.5)
			dCtX += 1;
		while (dCtX>0.5)
			dCtX -= 1;
		if (dCtY<-0.5)
			dCtY = -0.5;
		if (dCtY>0.5)
			dCtY = 0.5;

		if (m_dCenterX != dCtX || m_dCenterY != dCtY)
		{
			this->m_dCenterX = dCtX;
			this->m_dCenterY = dCtY;
			update();
		}
	}

	/*!
	 \brief convert LLA to Device Points. Device Points is according to current viewport,
	 point(0,0) stay at the top-left, point (width-1,height-1) in bottom-right
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_LLA2DP
	 \param lat	latitude in degree
	 \param lon	longitude in degree
	 \param pX	the pointer to hold X cood of DP
	 \param pY	the pointer to hold Y cood of DP
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_LLA2DP(double lat,double lon,qint32 * pX,qint32 *pY)
	{
		if (!pX||!pY)			return false;

		//!1.To Mercator Projection
		double dMx = cProjectionMercator(lat,lon).ToMercator().m_x;
		double dMy = cProjectionMercator(lat,lon).ToMercator().m_y;

		//!2.Calculat the percentage pos (-0.5-0.5) of the Mercator point
		double dperx = dMx/(cProjectionMercator::pi*cProjectionMercator::R*2);
		double dpery = -dMy/(cProjectionMercator::pi*cProjectionMercator::R*2);

661
		int nCurrImgSize = (1<<m_nLevel)*256;
662
		//!3.Calculat the World pixel coordinats
663 664
		double dTarX = dperx * nCurrImgSize + nCurrImgSize/2;
		double dTarY = dpery * nCurrImgSize + nCurrImgSize/2;
665
		//!4.Calculat the World pixel coordinats of current view-center point
666 667
		double dCurrX = nCurrImgSize*m_dCenterX+nCurrImgSize/2;
		double dCurrY = nCurrImgSize*m_dCenterY+nCurrImgSize/2;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		//!5.Calculat the World pixel coordinats of current view-left-top point
		double nOffsetLT_x = (dCurrX-width()/2.0);
		double nOffsetLT_y = (dCurrY-height()/2.0);

		//!6.Turn world coord to DP coord
		*pX = dTarX - nOffsetLT_x+.5;
		*pY = dTarY - nOffsetLT_y+.5;
		return true;
	}
	/*!
	 \brief convert  Device Points to LLA. Device Points is according to current viewport,
	 point(0,0) stay at the top-left, point (width-1,height-1) in bottom-right
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_DP2LLA
	 \param X	 X cood of DP
	 \param Y	 Y cood of DP
	 \param plat	the pointer to hold lat in degree
	 \param plon	the pointer to hold lon in degree
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_DP2LLA(qint32 X,qint32 Y,double  * plat,double * plon)
	{
		if (!plat||!plon)
			return false;

		//!1.Current World Pixel Size, connected to nLevel
695
		int nCurrImgSize = (1<<m_nLevel)*256;
696 697 698 699
		//!2.current DP according to center
		double dx = X-(width()/2.0);
		double dy = Y-(height()/2.0);
		//!3.Percentage -0.5 ~ 0.5 coord
700 701
		double dImgX = dx/nCurrImgSize+m_dCenterX;
		double dImgY = dy/nCurrImgSize+m_dCenterY;
702 703 704 705 706 707 708 709 710
		//!4.to Mercator
		double Mercator_x = cProjectionMercator::pi*cProjectionMercator::R*2*dImgX;
		double Mercator_y = -cProjectionMercator::pi*cProjectionMercator::R*2*dImgY;
		*plat = cProjectionMercator(Mercator_y,Mercator_x).ToLatLon().m_lat;
		*plon = cProjectionMercator(Mercator_y,Mercator_x).ToLatLon().m_lon;
		return true;
	}

	/*!
丁劲犇's avatar
丁劲犇 已提交
711
	 \brief convert Mercator to LLA.Mercator coord is a projection with ID 3857 .
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	 this method is NOT Level releated.

	 \fn tilesviewer::CV_MK2LLA
	 \param mx	Mercator x coord in meters
	 \param my	Mercator y coord in meters
	 \param plat	the pointer to hold lat in degree
	 \param plon	the pointer to hold lon in degree
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_MK2LLA(double mx, double my, double * plat, double * plon)
	{
		if (!plat||!plon)			return false;
		*plat  = cProjectionMercator(my,mx).ToLatLon().m_lat;
		*plon  = cProjectionMercator(my,mx).ToLatLon().m_lon;
		return true;
	}

	/*!
丁劲犇's avatar
丁劲犇 已提交
730
	 \brief convert LLA to Mercator .Mercator coord is a projection with ID 3857 .
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	 this method is NOT Level releated.

	 \fn tilesviewer::CV_LLA2MK
	 \param lat	latitude in degree
	 \param lon	longitude in degree
	 \param pmx	the pointer to hold Mercator x coord in meters
	 \param pmy	the pointer to hold Mercator y coord in meters
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_LLA2MK(double lat,double lon, double * pmx, double * pmy)
	{
		if (!pmx||!pmy)			return false;
		//To Mercator Projection
		*pmx = cProjectionMercator(lat,lon).ToMercator().m_x;
		*pmy = cProjectionMercator(lat,lon).ToMercator().m_y;
		return true;
	}


	/*!
	 \brief convert Mercator to World. World Points is according to current level,
	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right
A
archosm 已提交
753
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_MK2World
	 \param mx	Mercator x in meters
	 \param my	Mercator y in meters
	 \param px	pointer to hold world x in pixel
	 \param py	pointer to hold world y in pixel
	 \return bool will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_MK2World(double mx, double my, double * px, double * py)
	{
		if (!px||!py)			return false;
		//!1.Calculat the percentage pos (-0.5-0.5) of the Mercator point
		double dperx = mx/(cProjectionMercator::pi*cProjectionMercator::R*2);
		double dpery = -my/(cProjectionMercator::pi*cProjectionMercator::R*2);

770
		int nCurrImgSize = (1<<m_nLevel)*256;
771 772

		//!2.Calculat the World pixel coordinats
773 774
		*px = dperx * nCurrImgSize + nCurrImgSize/2;
		*py = dpery * nCurrImgSize + nCurrImgSize/2;
775 776 777 778 779 780 781

		return true;
	}

	/*!
	 \brief convert World to  Mercator. World Points is according to current level,
	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right
A
archosm 已提交
782
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
783 784 785 786 787 788 789 790 791 792 793 794 795
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_World2MK
	 \param x world x in pixel
	 \param y world y in pixel
	 \param pmx pointer to hold Mercator x in meters
	 \param pmy pointer to hold Mercator y in meters
	 \return bool  will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_World2MK(double x,double y, double * pmx, double * pmy)
	{
		if (!pmx||!pmy)			return false;
		//!1.Current World Pixel Size, connected to nLevel
796
		int nCurrImgSize = (1<<m_nLevel)*256;
797
		//!2.Percentage -0.5 ~ 0.5 coord
798 799
		double dImgX = x/nCurrImgSize - .5;
		double dImgY = y/nCurrImgSize - .5;
800 801 802 803 804 805 806 807 808 809
		//!3.to Mercator
		*pmx = cProjectionMercator::pi*cProjectionMercator::R*2*dImgX;
		*pmy = -cProjectionMercator::pi*cProjectionMercator::R*2*dImgY;
		return true;
	}


	/*!
	 \brief  convert LLA to world. World Points is according to current level,
	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right
A
archosm 已提交
810
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_LLA2World
	 \param lat		the latitude in meter
	 \param lon		the longitude in meter
	 \param px	pointer to hold world x in pixel
	 \param py	pointer to hold world y in pixel
	 \return bool	 will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_LLA2World(double lat, double lon, double * px, double * py)
	{
		if (!px||!py)			return false;
		//!1.To Mercator Projection
		double dMx = cProjectionMercator(lat,lon).ToMercator().m_x;
		double dMy = cProjectionMercator(lat,lon).ToMercator().m_y;

		//!2.Calculat the percentage pos (-0.5-0.5) of the Mercator point
		double dperx = dMx/(cProjectionMercator::pi*cProjectionMercator::R*2);
		double dpery = -dMy/(cProjectionMercator::pi*cProjectionMercator::R*2);

831
		int nCurrImgSize = (1<<m_nLevel)*256;
832
		//!3.Calculat the World pixel coordinats
833 834
		*px = dperx * nCurrImgSize + nCurrImgSize/2;
		*py = dpery * nCurrImgSize + nCurrImgSize/2;
835 836 837 838 839 840
		return true;
	}

	/*!
	 \brief  convert world to LLA. World Points is according to current level,
	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right
A
archosm 已提交
841
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
842 843 844 845 846 847 848 849 850 851 852 853 854
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_World2LLA
	 \param x	world x coord in pixels
	 \param y	world y coord in pixels
	 \param plat	pointer to hold latitude in degree
	 \param plon	pointer to hold longitude in degree
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_World2LLA(double x,double y, double * plat, double * plon)
	{
		if (!plat||!plon)			return false;
		//!1.Current World Pixel Size, connected to nLevel
855
		int nCurrImgSize = (1<<m_nLevel)*256;
856
		//!2.Percentage -0.5 ~ 0.5 coord
857 858
		double dImgX = x/nCurrImgSize - .5;
		double dImgY = y/nCurrImgSize - .5;
859 860 861 862 863 864 865 866 867 868 869 870 871 872
		//!3.to Mercator
		double mkx = cProjectionMercator::pi*cProjectionMercator::R*2*dImgX;
		double mky = -cProjectionMercator::pi*cProjectionMercator::R*2*dImgY;
		//!4.to LLA
		*plat = cProjectionMercator(mky,mkx).ToLatLon().m_lat;
		*plon = cProjectionMercator(mky,mkx).ToLatLon().m_lon;

		return true;
	}

	/*!
	 \brief	 convert  Device Points to World. Device Points is according to current viewport,
	 point(0,0) stay at the top-left, point (width-1,height-1) in bottom-right. World Points is according
	 to current level,	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right,
A
archosm 已提交
873
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
874 875 876 877 878 879 880 881 882 883 884 885 886
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_DP2World
	 \param dX	Device point X in pixel
	 \param dY	Device point Y in pixel
	 \param px	pointer to hold world x in pixel
	 \param py	pointer to hold world y in pixel
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_DP2World(qint32 dX, qint32 dY, double * px, double * py)
	{
		if (!px||!py)			return false;
		//!1.Current World Pixel Size, connected to nLevel
887
		int nCurrImgSize = (1<<m_nLevel)*256;
888 889 890 891
		//!2.current DP according to center
		double dx = dX-(width()/2.0);
		double dy = dY-(height()/2.0);
		//!3.Percentage -0.5 ~ 0.5 coord
892 893
		double dImgX = dx/nCurrImgSize+m_dCenterX;
		double dImgY = dy/nCurrImgSize+m_dCenterY;
894
		//!4.Calculat the World pixel coordinats
895 896
		*px = dImgX * nCurrImgSize + nCurrImgSize/2;
		*py = dImgY * nCurrImgSize + nCurrImgSize/2;
897 898 899 900 901 902 903 904
		return true;

	}

	/*!
	 \brief	 convert  World to Device Points. Device Points is according to current viewport,
	 point(0,0) stay at the top-left, point (width-1,height-1) in bottom-right. World Points is according
	 to current level,	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right,
A
archosm 已提交
905
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20
906 907 908 909 910 911 912 913 914 915 916 917 918 919
	 This approach is devided into several steps, and it is LEVEL RELATED!

	 \fn tilesviewer::CV_World2DP
	 \param x	world x in pixel
	 \param y	world y in pixel
	 \param pdX	pointer to hold Device point X in pixel
	 \param pdY	pointer to hold Device point Y in pixel
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_World2DP(double x,double y, qint32 * pdX, qint32 * pdY)
	{
		if (!pdX||!pdY)			return false;

		//!1.Current World Pixel Size, connected to nLevel
920
		int nCurrImgSize = (1<<m_nLevel)*256;
921 922

		//!2.Calculat the World pixel coordinats of current view-center point
923 924
		double dCurrX = nCurrImgSize*m_dCenterX+nCurrImgSize/2;
		double dCurrY = nCurrImgSize*m_dCenterY+nCurrImgSize/2;
925 926 927 928 929 930 931 932 933 934 935 936 937
		//!3.Calculat the World pixel coordinats of current view-left-top point
		double nOffsetLT_x = (dCurrX-width()/2.0);
		double nOffsetLT_y = (dCurrY-height()/2.0);

		//!4.Turn world coord to DP coord
		*pdX = x - nOffsetLT_x+.5;
		*pdY = y - nOffsetLT_y+.5;
		return true;

	}
	/*!
	 \brief	 convert  percentage coord to world. World Points is according
	 to current level,	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right,
A
archosm 已提交
938
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20. Percentage coord is a
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	 level-unretated coord, take a range -0.5~0.5, the world center in 0,0, -0.5.-0.5
	 at top-left, 0.5,0.5 at bottom-right
	 This approach is LEVEL RELATED!

	 \fn tilesviewer::CV_Pct2World
	 \param px	percentage coord x
	 \param py	percentage coord y
	 \param nx	pointer to hold world x in pixel
	 \param ny	pointer to hold world y in pixel
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool tilesviewer::CV_Pct2World(double px,double py,double * nx,double * ny)
	{
		if (!nx || !ny)
			return false;
		//first, determine whole map size in current level
		int sz_whole_idx = 1<<m_nLevel;
		int sz_whole_size = sz_whole_idx*256;
		//calculate pix coodinats
		*nx = px * sz_whole_size+sz_whole_size/2;
		*ny = py * sz_whole_size+sz_whole_size/2;
		return true;
	}
	/*!
	 \brief	 convert world to   percentage coord. World Points is according
	 to current level,	 point(0,0) stay at the left-top, point (SZ,SZ) in bottom-right,
A
archosm 已提交
965
	 the pixel size is 2^m_nLevel*256, m_nLevel between 0 and 20. Percentage coord is a
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	 level-unretated coord, take a range -0.5~0.5, the world center in 0,0, -0.5.-0.5
	 at top-left, 0.5,0.5 at bottom-right
	 This approach is LEVEL RELATED!

	 \fn tilesviewer::CV_World2Pct
	 \param nx	world x in pixel
	 \param ny	world y in pixel
	 \param px	pointer to hold percentage coord x
	 \param py	pointer to hold percentage coord y
	 \return bool	will always return true except for the pointer is NULL
	*/
	bool  tilesviewer::CV_World2Pct(double nx,double ny,double * px,double * py)
	{
		if (!px || !py)
			return false;
		//Current World Pixel Size, connected to nLevel
982
		int nCurrImgSize = (1<<m_nLevel)*256;
983
		//Percentage -0.5 ~ 0.5 coord
984 985
		*px = nx/nCurrImgSize - .5;
		*py = ny/nCurrImgSize - .5;
986 987 988 989 990 991

		return true;
	}

	bool tilesviewer::saveToImage(const QString & strFm)
	{
丁劲犇's avatar
丁劲犇 已提交
992 993 994
		QFileInfo info(strFm);
		QDir dir;
		dir.mkpath(info.absolutePath());
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
		QPixmap m = this->grab();
		return m.save(strFm);
	}

	void tilesviewer::_do_next_evts()
	{
		QMap<QString, QVariant> event;
		bool empty = true;
		m_mutex_events.lock();
		if ( m_list_pendingEvts.empty()==false)
		{
			event = m_list_pendingEvts.first();
			m_list_pendingEvts.pop_front();
			empty =  (m_list_pendingEvts.empty());
		}
		m_mutex_events.unlock();

		int st = 0;
		QString strDestin = event["destin"].toString();
		/*!
		 * In event/Message system,  Send or Post messages between different parts are enabled.the messages will be sent/posted from source to destin.
		 * The destin property setted to OUTER Message means this message/event is sent for OCX containers outer this instance.
		 * The destin property setted to MAIN_MAP Message means this message/event is sent for this MAP instance.
		 * The destin property setted to ALL Message means this message/event is sent for ALL possible recievers.
		 * Other destin will be treated as plugin DLL names ret by get_name()
		 */
		if (strDestin!=QString("OUTER") && strDestin!=QString("MAIN_MAP"))
		{
			int needUpdate = 0;
			foreach (layer_interface * pItem, m_listLayers)
			{
				if (pItem->get_name()==strDestin || strDestin==QString("ALL"))
				{
					needUpdate += pItem->cb_event(event)==true?1:0;
					++st;
				}
			}
			if (needUpdate>0)
				this->update();
		}
		/*! There are 3 ways dealing with messages.
		 * 1.cb_event callback mechanism is designed for DLL plugin system, that is Public for Qtvplugin extentions, and only take effect when destin=ALL, get_name()
		 * 2.A C++11 functinal mechanism called "send-listen-recievt" is built for OCX internal usage, and only take effect when destin=ALL, OUTER
		 * 3.All messages with destin setted to "MAINMAP" will be dealing in main map directly.
		*/
		if (strDestin==QString("OUTER") || strDestin==QString("ALL"))
		{
			m_mutex_listeners.lock();
			foreach (auto p, m_map_listeners)
				p(event);
			st += m_map_listeners.size();
			if (strDestin==QString("OUTER"))
				++st;
			m_mutex_listeners.unlock();
		}
		//This message/event is for this MAP itself
		else if (strDestin==QString("MAIN_MAP"))
		{
			++st;
		}

		if (st==0)
		{
			qWarning()<<"EVENT/MESSAGE "<<event["name"].toString() <<"'s' DESTIN "<<strDestin << " does not hit any recievers.";
			QString warmsg;
			foreach (layer_interface * pItem, m_listLayers)
				warmsg += pItem->get_name() + ";";
			qWarning()<<"Valid recievers:ALL;OUTER;MAIN_MAP;"<<warmsg;
		}

		if (empty==false)
			emit _inside_do_next_evts();
	}

	bool tilesviewer::post_event(const QMap<QString, QVariant> event)
	{
		if (event.contains("destin")==false ||event.contains("source")==false)
			return false;
		bool needEmit = false;
		m_mutex_events.lock();
		m_list_pendingEvts.append(event);
		if (m_list_pendingEvts.size()==1)
			needEmit = true;
		m_mutex_events.unlock();
		if (needEmit)
			emit _inside_do_next_evts();
		return true;
	}

	bool tilesviewer::send_event(const QMap<QString, QVariant> event)
	{
		if (event.contains("destin")==false ||event.contains("source")==false)
			return false;
		QString strDestin = event["destin"].toString();
		int st = 0;
		/*!
		 * In event/Message system,  Send or Post messages between different parts are enabled.The messages will be sent/posted from source to destin.
		 * The destin property setted to OUTER Message means this message/event is sent for OCX containers outer this instance.
		 * The destin property setted to MAIN_MAP Message means this message/event is sent for this MAP instance.
		 * The destin property setted to ALL Message means this message/event is sent for ALL possible recievers.
		 * Other destin will be treated as plugin DLL names ret by get_name()
		 */

		if (strDestin!=QString("OUTER") && strDestin!=QString("MAIN_MAP"))
		{
			int needUpdate = 0;
			foreach (layer_interface * pItem, m_listLayers)
			{
				if (pItem->get_name()==strDestin||strDestin==QString("ALL"))
				{
					++st;
					needUpdate += pItem->cb_event(event)==true?1:0;
				}
			}
			if (needUpdate>0)
				this->update();
		}
		/*! There are 3 ways dealing with messages.
		 * 1.cb_event callback mechanism is designed for DLL plugin system, that is Public for Qtvplugin extentions, and only take effect when destin=ALL, get_name()
		 * 2.A C++11 functinal mechanism called "send-listen-recievt" is built for OCX internal usage, and only take effect when destin=ALL, OUTER
		 * 3.All messages with destin setted to "MAINMAP" will be dealing in main map directly.
		*/

		if (strDestin==QString("OUTER") || strDestin==QString("ALL"))
		{
			foreach (auto p, m_map_listeners)
				p(event);
			st += m_map_listeners.size();
			if (strDestin==QString("OUTER"))
				++st;
		}
		//This message/event is for this MAP itself
		else if (strDestin==QString("MAIN_MAP"))
		{
			++st;
		}

		if (st==0)
		{
			qWarning()<<"EVENT/MESSAGE DESTIN "<<strDestin << " does not hit any recievers.";
			QString warmsg;
			foreach (layer_interface * pItem, m_listLayers)
				warmsg += pItem->get_name() + ";";
			qWarning()<<"Valid recievers:ALL;OUTER;MAIN_MAP;"<<warmsg;
		}
		return true;
	}
	void tilesviewer::listen_event(QString name,std::function<void (const QMap<QString, QVariant>)> functor)
	{
		m_mutex_listeners.lock();
		m_map_listeners[name] = (functor);
		m_mutex_listeners.unlock();
	}

	void tilesviewer::unlisten_event(QString name)
	{
		m_mutex_listeners.lock();
		m_map_listeners.remove(name);
		m_mutex_listeners.unlock();

	}
}