geographicsscene.cpp 3.5 KB
Newer Older
1 2 3
#include "geographicsscene.h"
#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 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
	}
	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)
	{

	}
	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);
				}
			}
			else
			{
				this->addItem(pg);
				this->m_map_items[namec] = item;
			}
			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);
				delete item;
			}
		}

	}

67 68 69 70 71 72 73
	/**
	 * @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.
	 */
74 75
	void geoGraphicsScene::adjust_item_coords(int newLevel)
	{
76 77
		currentNewLevel = newLevel;
		if (total_items()<1024)
78
		{
79
			for (QMap<QString, geoItemBase * >::iterator p = m_map_items.begin();p!=m_map_items.end();++p)
80
			{
81
				geoItemBase * base = p.value();
82 83
				//when this function is called, base->level() is the old level from which
				//  current map is zoomed.
84
				base->adjust_coords(newLevel);
85 86
				//After adjust_coords above, the item "base" is considered to
				// have a valid coord corresponds to current  newLevel
87 88 89
				base->setLevel(newLevel);
			}
		}
90 91 92 93
		else
			m_queue_level_change = geo_items();


94
	}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	bool geoGraphicsScene::deal_level_queue()
	{
		int bneedref = 0;
		for (int i=0;i<1024 && m_queue_level_change.empty()==false;++i)
		{
			++bneedref;
			geoItemBase * base = m_queue_level_change.first();
			m_queue_level_change.pop_front();
			//when this function is called, base->level() is the old level from which
			//  current map is zoomed.
			base->adjust_coords(currentNewLevel);
			//After adjust_coords above, the item "base" is considered to
			// have a valid coord corresponds to current  newLevel
			base->setLevel(currentNewLevel);
		}
		if (m_queue_level_change.empty()==true && bneedref)
			return true;
		return false;
	}

115 116 117 118 119 120 121 122 123 124 125
	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()
	{
126 127 128 129 130
		return std::move(m_map_items.values());
	}
	QList<QString> geoGraphicsScene::geo_item_names()
	{
		return std::move(m_map_items.keys());
131 132
	}
}