geographicspixmapitem.cpp 5.7 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
#include "geographicspixmapitem.h"
2 3 4 5 6 7 8 9
#include "../qtviewer_planetosm/osmtiles/viewer_interface.h"
#include <assert.h>
#include <math.h>
#include <QGraphicsSceneMouseEvent>
#include <QMessageBox>
#include "geographicsscene.h"
#include "qtvplugin_geomarker.h"
namespace QTVP_GEOMARKER{
10 11 12 13
	geoGraphicsPixmapItem::geoGraphicsPixmapItem(QString name,QTVOSM::viewer_interface * pVi
												 ,const tag_icon * pIcon,
												 qreal lat/* = 90*/,
												 qreal lon/* = 0*/)
14 15 16 17
		:QGraphicsPixmapItem (0)
		,geoItemBase(name,ITEAMTYPE_PIXMAP,pVi)
		,m_lat(lat)
		,m_lon(lon)
18
		,m_pIcon(pIcon)
19 20 21

	{
		assert(vi()!=0);
丁劲犇's avatar
丁劲犇 已提交
22
		this->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
23 24
		double px,py;
		vi()->CV_LLA2World(m_lat,m_lon,&px,&py);
25 26
		setOffset(px - m_pIcon->centerx, py - m_pIcon->centery);
		QGraphicsPixmapItem::setPixmap(m_pIcon->icon);
27 28
		QGraphicsPixmapItem::setTransformOriginPoint(px,py);
		QGraphicsPixmapItem::setTransformationMode(Qt::FastTransformation );
29 30 31 32 33 34 35 36 37 38 39
	}
	void geoGraphicsPixmapItem::adjust_coords(int ncurrLevel)
	{
		if (vi() && ncurrLevel != level())
		{
			/** Since the map is zooming from level() to current level,
			 * the map size zoom ratio can be calculated using pow below.
			 * We can get new coord for current zoom level by multiplicative.
			*/
			double ratio = pow(2.0,(ncurrLevel - level()));
			QPointF offset = this->offset();
40 41 42
			double oldlefttop_x = offset.x() + m_pIcon->centerx;
			double oldlefttop_y = offset.y() + m_pIcon->centery;
			setOffset(oldlefttop_x*ratio - m_pIcon->centerx, oldlefttop_y*ratio - m_pIcon->centery);
43
			QGraphicsPixmapItem::setTransformOriginPoint(oldlefttop_x*ratio, oldlefttop_y*ratio );
44 45
		}
	}
46
	void geoGraphicsPixmapItem::setPixmap(const tag_icon &icon)
47
	{
48 49 50
		this->m_pIcon = &icon;
		QGraphicsPixmapItem::setPixmap(icon.icon);

51 52
		double px,py;
		vi()->CV_LLA2World(m_lat,m_lon,&px,&py);
53
		setOffset(px - m_pIcon->centerx, py - m_pIcon->centery);
54
		QGraphicsPixmapItem::setTransformOriginPoint(px, py);
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
	void geoGraphicsPixmapItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
	{
		QGraphicsPixmapItem::hoverEnterEvent(event);
		//this->show_props(true);
		//post enent
		QMap<QString, QVariant > map_evt;
		geoGraphicsScene * pscene = dynamic_cast<geoGraphicsScene *>(this->scene());
		if (pscene)
		{
			QObject * pPlg = pscene->parent();
			if (pPlg)
			{
				qtvplugin_geomarker * pMarker = dynamic_cast<qtvplugin_geomarker *>(pPlg) ;
				if (pMarker)
				{
					map_evt["source"] = pMarker->get_name();
					map_evt["destin"] = "ALL";
					map_evt["name"] = "ITEM_MOUSE_ENTER";
					map_evt["id"] = this->item_name();
					vi()->post_event(map_evt);
				}
			}

		}
	}

	void geoGraphicsPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
	{
		QGraphicsPixmapItem::hoverLeaveEvent(event);
		//this->show_props(false);
		//post enent
		QMap<QString, QVariant > map_evt;
		geoGraphicsScene * pscene = dynamic_cast<geoGraphicsScene *>(this->scene());
		if (pscene)
		{
			QObject * pPlg = pscene->parent();
			if (pPlg)
			{
				qtvplugin_geomarker * pMarker = dynamic_cast<qtvplugin_geomarker *>(pPlg) ;
				if (pMarker)
				{
					map_evt["source"] = pMarker->get_name();
					map_evt["destin"] = "ALL";
					map_evt["name"] = "ITEM_MOUSE_LEAVE";
					map_evt["id"] = this->item_name();
					vi()->post_event(map_evt);
				}
			}
104

105 106
		}
	}
107 108 109 110 111 112
	void geoGraphicsPixmapItem::setGeo(qreal cent_lat,qreal cent_lon)
	{
		m_lat = cent_lat;
		m_lon = cent_lon;
		double px,py;
		vi()->CV_LLA2World(cent_lat,cent_lon,&px,&py);
113
		setOffset(px - m_pIcon->centerx, py - m_pIcon->centery);
114
		QGraphicsPixmapItem::setTransformOriginPoint(px, py);
115 116 117 118 119
		adjustLabelPos();
	}
	void geoGraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
	{
		QGraphicsPixmapItem ::mousePressEvent(event);
120 121 122 123 124
		//if (wantMouseHoverEvent()==false)
		{
			bool bshow = this->props_visible();
			this->show_props(!bshow);
		}
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
		//post enent
		QMap<QString, QVariant > map_evt;
		geoGraphicsScene * pscene = dynamic_cast<geoGraphicsScene *>(this->scene());
		if (pscene)
		{
			QObject * pPlg = pscene->parent();
			if (pPlg)
			{
				qtvplugin_geomarker * pMarker = dynamic_cast<qtvplugin_geomarker *>(pPlg) ;
				if (pMarker)
				{
					map_evt["source"] = pMarker->get_name();
					map_evt["destin"] = "ALL";
					if (event->buttons() & Qt::LeftButton)
						map_evt["name"] = "ITEM_LBUTTON_CLICKED";
					else if (event->buttons() & Qt::RightButton)
						map_evt["name"] = "ITEM_RBUTTON_CLICKED";
					else if (event->buttons() & Qt::MidButton)
						map_evt["name"] = "ITEM_MBUTTON_CLICKED";
					else
						map_evt["name"] = "ITEM_BUTTON_CLICKED";
					map_evt["id"] = this->item_name();
					vi()->post_event(map_evt);
				}
			}

		}
	}

	void geoGraphicsPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
	{
		QGraphicsPixmapItem ::mouseDoubleClickEvent(event);
		//post enent
		QMap<QString, QVariant > map_evt;
		geoGraphicsScene * pscene = dynamic_cast<geoGraphicsScene *>(this->scene());
		if (pscene)
		{
			QObject * pPlg = pscene->parent();
			if (pPlg)
			{
				qtvplugin_geomarker * pMarker = dynamic_cast<qtvplugin_geomarker *>(pPlg) ;
				if (pMarker)
				{
					map_evt["source"] = pMarker->get_name();
					map_evt["destin"] = "ALL";
					if (event->buttons() & Qt::LeftButton)
						map_evt["name"] = "ITEM_LBUTTON_DBLCLICKED";
					else if (event->buttons() & Qt::RightButton)
						map_evt["name"] = "ITEM_RBUTTON_DBLCLICKED";
					else if (event->buttons() & Qt::MidButton)
						map_evt["name"] = "ITEM_MBUTTON_DBLCLICKED";
					else
						map_evt["name"] = "ITEM_BUTTON_DBLCLICKED";
					map_evt["id"] = this->item_name();
					vi()->post_event(map_evt);
				}
			}

		}
	}



	QPointF geoGraphicsPixmapItem::label_pos()
	{
		return QPointF(offset().x()+this->boundingRect().width(),offset().y()-8);
	}
}