qtvplugin_geomarker.h 10.1 KB
Newer Older
1 2 3 4 5 6
#ifndef QTVPLUGIN_GEOMARKER_H
#define QTVPLUGIN_GEOMARKER_H

#include <QWidget>
#include <QTranslator>
#include <QStandardItemModel>
7 8
#include <QMap>
#include <QXmlStreamAttributes>
9
#include <functional>
10 11 12
#include "geographicsscene.h"
#include "../qtviewer_planetosm/osmtiles/layer_interface.h"
#include "../qtviewer_planetosm/osmtiles/viewer_interface.h"
13
#include "geographicspixmapitem.h"
14 15 16 17 18
#include "geoitembase.h"
namespace Ui {
	class qtvplugin_geomarker;
}
using namespace QTVOSM;
19 20
/*!
 \brief qtvplugin_geomarker introduces QGraphicesView system, established a common approach for geo marking.
21 22 23
 GEO marker is a vector symbol that will be displayed on the background OSM raster map. there are 3 different mark types supported by this plugin.
 1. Point Marks. Include ellipse and rect style mark. these type of mark stand for a single point on earth, with a specified lat, lon .the width and height
 for rect / ellipse circumrect can be specified by user at runtime, in PIXEL. width and height will stay still during map zoom.
24 25 26 27

 2. Line Mark.	Line mark is a beeline on map. ATTENTION, in Mercator Projection system, the geo shortest path between 2 points on earth is NOT a beeline, that means
 beeline on a map is just for display performance and accessibility. the real path is a curve , which has 2 point of intersections exactly at start position and end position.

28
 3. Polygon (Polygon) Mark. Polygon mark is a polygon on map. borders of a polygon is painted with lines, for a same reason above, the geo shortest path between 2 points on earth is NOT a beeline either.
29 30 31 32 33 34 35 36 37 38 39

 Marks above shares a same style system provided by Qt painter system. pen, brush , font can be setted for each mark.

 Each mark has a Uinque ID called "name", and a type enum called "type". It can also contain several user-defined properties, with a key-value  style storage.
 Especial, a user-defined property called "LABEL"(Upper case) is different agains others. the value of LABEL will be displayed as text items on map all the time,
 but other properties will only visible when user click the mark.

 When the mark is clicked, or double clicked, a event will be fired, so that all plugins and OCX containers will be noticed that a mark (with ID) is clicked.

 \class qtvplugin_geomarker qtvplugin_geomarker.h "qtvplugin_geomarker/qtvplugin_geomarker.h"
*/
40 41 42 43 44
class qtvplugin_geomarker : public QWidget,public layer_interface
{
	Q_OBJECT
	Q_PLUGIN_METADATA(IID OSMLayerInterface_iid )
	Q_INTERFACES(QTVOSM::layer_interface)
45
	typedef QMap<QString, QVariant> (tpfunc_call)(const QMap<QString, QVariant> &);
46 47 48 49 50 51 52 53
private:
	struct tag_xml_mark{
		QString name;
		int		type;
		QPolygonF geoPoints;
		QMap<QString, QString> styles;
		QMap<QString, QString> props;
	};
丁劲犇's avatar
丁劲犇 已提交
54
	//The global icon map
55
	QMap<QString,QTVP_GEOMARKER::tag_icon> m_map_icons;
56

57 58 59 60
public:
	qtvplugin_geomarker(QWidget *parent = 0);
	~qtvplugin_geomarker();
	QWidget *	get_propWindow() {return this;}
61
	QString		get_name();
62 63 64 65 66 67 68 69 70
	/*! Get graphicalScence, the scence is a World coord in current level.
	 * in plugin system and draw tasks, user should divide class from  QGraphicsItem
	 * and maintain lat, lon coords to world coords according to current level
	 * (through  CV_LLA2World for convenience).
	*/
	QTVP_GEOMARKER::geoGraphicsScene * scene(){return m_pScene;}
private:
	int m_nInstance;

71
	QTVP_GEOMARKER::geoGraphicsScene * m_pScene;	//! the graphics scene object pointer.
72 73
	QTranslator pluginTranslator;
	Ui::qtvplugin_geomarker *ui;
74
	viewer_interface * m_pVi;						//! viewer_interface interface provides coordinats convertions.
75 76 77 78
	bool m_bVisible;
	QString m_SLLibName;
	QString m_SLLibPath;
private:
79 80 81
	/*! a timer provides timing ui-refresh , instead of immediately refresh when item has been updated.
	 * This timer is just affects UI widgets, Map will be updated immediately otherwise.
	*/
82
	int m_nTimerID_refreshUI;
丁劲犇's avatar
丁劲犇 已提交
83
	int m_nTimerID_refreshMap;
84
	int m_nTimerID_levelQueue;
85
	quint64 m_nDivideTimer;
86
	bool m_bNeedRefresh;
丁劲犇's avatar
丁劲犇 已提交
87 88
	bool m_bNeedUpdateView;

89
	QStandardItemModel * m_pLineStyleModel;
丁劲犇's avatar
丁劲犇 已提交
90
	QStandardItemModel * m_pFillStyleModel;
91 92
	QStandardItemModel * m_pGeoItemModel;
	QStandardItemModel * m_pGeoPropModel;
丁劲犇's avatar
丁劲犇 已提交
93
	QStandardItemModel * m_pIconsModel;
94 95

	//persistent functions
96
private:
97 98 99 100 101 102 103 104 105 106
	QString		ini_file();
	void		ini_save();
	void		ini_load();
	bool		xml_save		(QString xml);
	bool		xml_load		(QString xml);
	bool		xml_readMark	(QXmlStreamReader & reader, tag_xml_mark & mark,QString & errMsg);
	bool		xml_readGeo		(QXmlStreamReader & reader, tag_xml_mark & mark,QString & errMsg);
	bool		xml_readStyle	(QXmlStreamReader & reader, tag_xml_mark & mark,QString & errMsg);
	bool		xml_readProps	(QXmlStreamReader & reader, tag_xml_mark & mark,QString & errMsg);
	QMap<QString,QString> xml_attribs_map(const QXmlStreamAttributes & ats);
107

丁劲犇's avatar
丁劲犇 已提交
108 109 110
	bool		xml_icon_save	(QString xml);
	bool		xml_icon_load	(QString xml);

111
	//UI refreshing functions
112
private:
113 114 115 116 117 118 119 120 121 122
	QList<QString> m_items_to_insert;
	void		scheduleRefreshMarks();
	void		scheduleUpdateMap();
	void		refreshItemUI(QString markname);
	void		refreshProps(QTVP_GEOMARKER::geoItemBase * itm);
	QColor		string2color(const QString & s);
	QString		color2string(const QColor & c);
	void		refreshIconModel();
	void		loadTranslations();
	bool		too_many_items();
123 124
	//Geo mark updating functions
private:
125
	//update methopd for UI
126
	template <class T>
127 128
	QTVP_GEOMARKER::geoItemBase *	update_point		(const QString & name,double lat, double lon, int width, int height, QPen pen, QBrush brush);
	QTVP_GEOMARKER::geoItemBase *	update_line			(const QString & name,double lat1, double lon1,double lat2, double lon2, QPen pen);
129
	QTVP_GEOMARKER::geoItemBase *	update_polygon		(const QString & name,const QPolygonF latlons, QPen pen, QBrush brush, bool tp);
130
	QTVP_GEOMARKER::geoItemBase *	update_icon			(const QString & name,double lat, double lon,qreal scale, qreal rotate,int smooth, QString id);
131 132 133 134 135 136
	//update method for XML
	bool							xml_update_mark		(tag_xml_mark & mark);
	//update method for plugin function calls
	QMap<QString, QVariant>			func_update_point	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_update_line	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_update_polygon	(const QMap<QString, QVariant> &);
137
	QMap<QString, QVariant>			func_update_icon	(const QMap<QString, QVariant> &);
138
	QMap<QString, QVariant>			func_update_props	(const QMap<QString, QVariant> &);
139
	//other method for plugin function calls
140 141
private:
	void initialBindPluginFuntions();
142
	QMap<QString,std::function <tpfunc_call> > m_map_pluginFunctions;
丁劲犇's avatar
丁劲犇 已提交
143 144 145 146 147
	QMap<QString, QVariant>			func_exists			(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_delete_marks	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_delete_props	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_mark_names		(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_mark			(const QMap<QString, QVariant> &);
148
	QMap<QString, QVariant>			func_props			(const QMap<QString, QVariant> &);
149 150
	QMap<QString, QVariant>			func_save_xml		(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_load_xml		(const QMap<QString, QVariant> &);
151
	//icon and its manage
152 153 154
	QMap<QString, QVariant>			func_add_resource	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_save_resources	(const QMap<QString, QVariant> &);
	QMap<QString, QVariant>			func_load_resources	(const QMap<QString, QVariant> &);
155
	//overloaded virtual funtions
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
protected:
	layer_interface *		load_initial_plugin(QString strSLibPath,viewer_interface  * ptrviewer);
	QWidget *				load_prop_window();
	void					load_retranslate_UI();

	bool		is_visible();
	void		set_visible(bool vb);
	void		set_name(QString vb);

	void		cb_paintEvent( QPainter * pImage );
	void		cb_levelChanged(int);
	bool		cb_mousePressEvent(QMouseEvent *);
	bool		cb_mouseDoubleClickEvent(QMouseEvent *);
	bool		cb_event(const QMap<QString, QVariant>);

171
	void					timerEvent(QTimerEvent * e);
172
	QMap<QString, QVariant> call_func(const  QMap<QString, QVariant> & /*paras*/);
173
	//ui slots
174 175 176 177 178
protected slots:
	void on_pushButton_update_clicked();
	void on_radioButton_tool_point_toggled(bool);
	void on_radioButton_tool_line_toggled(bool);
	void on_radioButton_tool_polygon_toggled(bool);
179
	void on_radioButton_tool_icons_toggled(bool);
丁劲犇's avatar
丁劲犇 已提交
180 181
	void on_toolButton_selColorPen_clicked();
	void on_toolButton_selColorFill_clicked();
182
	void on_toolButton_selColorText_clicked();
183 184 185 186 187 188
	void on_tableView_marks_doubleClicked(const QModelIndex & index);
	void on_pushButton_prop_update_clicked();
	void on_pushButton_prop_delete_clicked();
	void on_pushButton_del_clicked();
	void on_pushButton_pickToLine1_clicked();
	void on_pushButton_pickToLine2_clicked();
189
	void on_pushButton_getPolygon_clicked();
190 191
	void on_pushButton_save_clicked();
	void on_pushButton_load_clicked();
丁劲犇's avatar
丁劲犇 已提交
192
	void on_pushButton_import_icon_clicked();
丁劲犇's avatar
丁劲犇 已提交
193 194
	void on_pushButton_load_icons_clicked();
	void on_pushButton_save_icons_clicked();
195
	void on_pushButton_refresh_list_clicked();
196 197 198
};

template <class T>
199
QTVP_GEOMARKER::geoItemBase * qtvplugin_geomarker::update_point(const QString & name,double lat, double lon, int width, int height, QPen pen, QBrush brush)
200
{
201
	QTVP_GEOMARKER::geoItemBase * res = 0;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	//Get raw Item by name
	QTVP_GEOMARKER::geoItemBase * base = m_pScene->geoitem_by_name(name);
	//Get Props
	QStringList propNames;
	QVariantList propValues;
	if (base)
	{
		propNames = base->prop_names();
		propValues = base->prop_values();
	}
	//type convertion to T
	T * pitem = base?dynamic_cast<T  *>(base):0;
	if (!pitem)
		pitem	= new T(name,
						this->m_pVi,
						lat,lon,
						width,height);
丁劲犇's avatar
丁劲犇 已提交
219 220
	pitem->setPen(pen);
	pitem->setBrush(brush);
221

222 223
	if (base == pitem)
	{
224
		pitem->setGeo(lat,lon);
225
		pitem->setSize(width,height);
226
		res = pitem;
227
	}
228
	else if (false==this->m_pScene->addItem(pitem,0))
229 230 231 232 233 234 235 236 237 238 239 240 241
	{
		if (base != pitem)
			delete pitem;
	}
	else
	{
		int cs = propNames.size();
		for (int i=0;i<cs && base != pitem;++i)
		{
			pitem->set_prop_data(propNames.first(), propValues.first());
			propNames.pop_front();
			propValues.pop_front();
		}
242
		res = pitem;
243 244
	}

245 246
	return res;

247 248 249 250 251
}

#endif // QTVPLUGIN_GEOMARKER_H