void LabelToolPlugin::drawRect(QPointF point_a, QPointF point_b)
{
  clearScene();
  drawImage();

  qreal width = point_b.x() - point_a.x();
  qreal height = point_b.y() - point_a.y();
  QPen pen_line(Qt::red);
  pen_line.setWidth(4);
  scene_->addRect(point_a.x(), point_a.y(), width, height, pen_line);
}
void qtvplugin_grid::cb_paintEvent( QPainter * pImage )
{
	int step_level[10] = {20,20,10,10,5,5,2,1,1,1};
	double dstep_level[10] = {0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005};
	if (!m_pVi || m_bVisible==false)
		return ;
	QRect rect = m_pVi->windowRect();
	//Get current viewport pos, in LLA
	double lat_top,lon_left;
	double lat_bottom,lon_right;
	m_pVi->CV_DP2LLA(
		rect.left(),
		rect.top(),
		&lat_top,&lon_left
		);
	if (lat_top >=85) lat_top = 85;
	m_pVi->CV_DP2LLA(
		rect.right(),
		rect.bottom(),
		&lat_bottom,&lon_right
		);
	if (lat_bottom <=-85) lat_bottom = -85;

	QPen pen_line(QColor(0,0,0,96)), pen_area(QColor(255,0,0,96));
	pen_area.setWidth(4);
	QPen oldpen = pImage->pen();
	pImage->setPen(pen_line);
	char str[1024];

	if (m_pVi->level()<9)
	{
		int step = step_level[m_pVi->level()];
		for (int lat=int(lat_bottom/10.0-1.5)*10;lat<=int(lat_top/10.0+1.5)*10&&m_pVi->level()>0;lat+=step)
		{
			if (lat>=85 || lat<=-85)
				continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat,lon_left,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat,lon_right,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			sprintf(str,"%-3d",lat);
			pImage->drawText(DP_left,DP_top-8,str);
		}
		for (int lon=int(lon_left/10.0-1.5)*10;lon<=(lon_right/10.0+1.5)*10&&m_pVi->level()>0;lon+=step)
		{

			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat_top,lon,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat_bottom,lon,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			//warpping
			int nLon = lon;
			while (nLon<-180)
				nLon+=360;
			while (nLon>180)
				nLon-=360;
			sprintf(str,"%-4d",nLon);
			pImage->drawText(DP_left,DP_bottom-16,str);
		}
	}
	else
	{
		double step = dstep_level[m_pVi->level()-9];
		for (double lat=int(lat_bottom*(1/step)-1.5)*step;
			lat<=int(lat_top*(1/step)+1.5)*step&&m_pVi->level()>0;lat+=step)
		{
			if (lat>=85 || lat<=-85)
				continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat,lon_left,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat,lon_right,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			sprintf(str,"%-8.3f",lat);
			pImage->drawText(DP_left,DP_top-8,str);
		}
		for (double lon=int(lon_left*(1/step)-1.5)*step;lon<=int(lon_right*(1/step)+1.5)*step&&m_pVi->level()>0;lon+=step)
		{
			//if (lon>=180 || lon<=-180)
			//	continue;
			int DP_left,DP_top,DP_right,DP_bottom;
			if (false==m_pVi->CV_LLA2DP(lat_top,lon,&DP_left,&DP_top))
				continue;
			if (false==m_pVi->CV_LLA2DP(lat_bottom,lon,&DP_right,&DP_bottom))
				continue;
			pImage->drawLine(DP_left,DP_top,DP_right,DP_bottom);
			//Warpping
			float dLon = lon;
			while (dLon<-180)
				dLon+=360;
			while (dLon>180)
				dLon-=360;
			sprintf(str,"%-8.3f",(float)dLon);
			pImage->drawText(DP_left,DP_bottom-16,str);
		}

	}



	int x1,y1,x2,y2;
	int sz = m_list_points.size();
	QPen pen_text(QColor(0,0,255));
	for (int i=0;i<sz-1;++i)
	{
		m_pVi->CV_LLA2DP(m_list_points[i].x(),m_list_points[i].y(),&x1,&y1);
		m_pVi->CV_LLA2DP(m_list_points[i+1].x(),m_list_points[i+1].y(),&x2,&y2);
		pImage->setPen(pen_area);
		pImage->drawLine(x1,y1,x2,y2);
		double sita;
		double dis = CalDistance(m_list_points[i].x(),m_list_points[i+1].x(),m_list_points[i].y(),m_list_points[i+1].y(), &sita);
		sprintf(str,"%.2lfkm",dis/1000.0);
		pImage->setPen(pen_text);
		QPointF pos = QLineF(x1,y1,x2,y2).pointAt(i*0.8 / sz+0.1);
		pImage->drawText(pos,str);
	}
	if (bFinished==false && sz>0)
	{
		m_pVi->CV_LLA2DP(m_list_points.last().x(),m_list_points.last().y(),&x1,&y1);
		m_pVi->CV_LLA2DP(m_pt_end.x(),m_pt_end.y(),&x2,&y2);
		pImage->setPen(pen_area);
		pImage->drawLine(x1,y1,x2,y2);
		double sita;
		double dis = CalDistance(m_list_points.last().x(),m_pt_end.x(),m_list_points.last().y(),m_pt_end.y(), &sita);
		sprintf(str,"%.1lfkm",dis/1000.0);
		pImage->setPen(pen_text);
		QPointF pos = QLineF(x1,y1,x2,y2).pointAt(1*0.8 / sz+0.1);
		pImage->drawText(pos,str);
	}
	pImage->setPen(oldpen);
}
void LabelToolPlugin::drawQtPolygon(bool connect)
{
  //delete everything from scene
  clearScene();

  // add img to scene
  if(!img_with_polygons_.empty())
    drawImage();



  if(selected_points_.size() == 1)
  {
    // draw point
    double rad = 1;
    QPen pen(Qt::blue);
    QBrush brush(Qt::SolidPattern);
    scene_->addEllipse(selected_points_[0].x() - rad,
        selected_points_[0].y() - rad ,
        rad * 5.0, rad * 5.0, pen, brush);
  }
  else
  {
    if(!connect)
    {
      QPen pen_point(Qt::blue);
      QBrush brush_point(Qt::SolidPattern);
      QPen pen_line(Qt::red);
      pen_line.setWidth(4);

      double rad = 1;
      for(int idx = 0; idx < (selected_points_.size() - 1); idx++)
      {
        scene_->addEllipse(selected_points_[idx].x() - rad,
                           selected_points_[idx].y() - rad ,
                           rad * 5.0, rad * 5.0, pen_point, brush_point);
        scene_->addLine(selected_points_[idx].x(), selected_points_[idx].y(),
                        selected_points_[idx + 1].x(), selected_points_[idx + 1].y(),
            pen_line);
      }
      scene_->addEllipse(selected_points_[selected_points_.size() - 1].x() - rad,
          selected_points_[selected_points_.size() - 1].y() - rad ,
          rad * 5.0, rad * 5.0, pen_point, brush_point);

    }
    else
    {
      QPen pen_line(Qt::green);
      pen_line.setWidth(4);
      for(int idx = 0; idx < (selected_points_.size() - 1); idx++)
      {
        scene_->addLine(selected_points_[idx].x(), selected_points_[idx].y(),
                        selected_points_[idx + 1].x(), selected_points_[idx + 1].y(),
            pen_line);
      }
      scene_->addLine(selected_points_[0].x(), selected_points_[0].y(),
          selected_points_[selected_points_.size() - 1].x(), selected_points_[selected_points_.size() - 1].y(),
          pen_line);
    }
  }

  ui_.graphicsView->show();
  ui_.graphicsView->viewport()->repaint();
}