geographicsscene.cpp 2.9 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1
#include "geographicsscene.h"
2 3
#include "geoitembase.h"
#include <QGraphicsItem>
4
#include <algorithm>
5 6 7 8
namespace QTVP_GEOMARKER{
	geoGraphicsScene::geoGraphicsScene(QObject *parent) :
		QGraphicsScene(parent)
	{
9
		this->setItemIndexMethod(QGraphicsScene::NoIndex);
10
		currentNewLevel = 0;
11 12 13 14 15 16 17 18 19 20 21
	}
	geoGraphicsScene::geoGraphicsScene(const QRectF &sceneRect, QObject *parent )
		:QGraphicsScene(sceneRect,parent)
	{

	}

	geoGraphicsScene::geoGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent )
		:QGraphicsScene(x,y,width,height,parent)
	{

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	}

	/**
	 * @brief  sequentially call virtual function geoItemBase::adjust_coords for every geoItemBase object.
	 *
	 * Since the  scene coord will be zoomed in / out together with level change, all graphics items' coords should
	 * be recalculated in time. the method adjust_item_coords will do this automatically,
	 * @param newLevel the level to which current map is zoomed.
	 */
	void geoGraphicsScene::adjust_item_coords(int newLevel)
	{
		currentNewLevel = newLevel;
		for (QSet<geoItemBase * >::iterator p = m_set_iconitems.begin();p!=m_set_iconitems.end();++p)
		{
			geoItemBase * base = *p;
			base->adjust_coords(newLevel);
			//After adjust_coords above, the item "base" is considered to
			// have a valid coord corresponds to current  newLevel
			base->setLevel(newLevel);
		}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	}
	bool geoGraphicsScene::addItem(geoItemBase * item,int /*reserved*/)
	{
		bool res = false;
		QGraphicsItem * pg = dynamic_cast<QGraphicsItem *>(item);
		if (pg)
		{
			const QString & namec = item->item_name();
			if (m_map_items.contains(namec))
			{
				geoItemBase * oldItem = m_map_items[namec];
				if (oldItem != item)
				{
					this->removeItem(oldItem,0);
					this->m_map_items[namec] = item;
					this->addItem(pg);
59 60
					if (item->item_type()==ITEAMTYPE_PIXMAP)
						m_set_iconitems.insert(item);
61 62 63 64 65 66
				}
			}
			else
			{
				this->addItem(pg);
				this->m_map_items[namec] = item;
67 68
				if (item->item_type()==ITEAMTYPE_PIXMAP)
					m_set_iconitems.insert(item);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
			}
			res = true;
		}

		return res;
	}

	void geoGraphicsScene::removeItem(geoItemBase * item, int /*reserved*/)
	{
		QGraphicsItem * pg = dynamic_cast<QGraphicsItem *>(item);
		if (pg)
		{
			QString c_name = item->item_name();
			if (m_map_items.contains(c_name))
			{
				this->removeItem(pg);
				m_map_items.remove(c_name);
86 87
				if (item->item_type()==ITEAMTYPE_PIXMAP)
					m_set_iconitems.remove(item);
88 89 90 91 92 93
				delete item;
			}
		}

	}

94

95 96 97 98 99 100 101 102 103 104 105
	geoItemBase * geoGraphicsScene::geoitem_by_name(const QString & name)
	{
		geoItemBase * it = 0;
		if (m_map_items.contains(name))
			it = m_map_items[name];
		return it;

	}

	QList<geoItemBase *> geoGraphicsScene::geo_items()
	{
106
		return /*std::move*/(m_map_items.values());
107 108 109
	}
	QList<QString> geoGraphicsScene::geo_item_names()
	{
110
		return /*std::move*/(m_map_items.keys());
111 112
	}
}