⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapdisp.cpp

📁 给予QT的qps开源最新源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      if ( ManualWp ) {
         wp = dest;
      } else {
         wp = wpPos;
      }
      // get waypoint in display coordinates
      mapSelector->currentMap()->calcxy(&xwp, &ywp, wp);
      QPoint screenPos = mapToScreen(QPoint((int) rint(xwp), 
         (int) rint(ywp)));
      tx = screenPos.x();
      ty = screenPos.y();
   }
   
   QPen p = painter->pen();
   p.setWidth(2);
   p.setColor(mapDispOptions.bearColor);
   painter->setPen(p);

   if( !mapSelector->isMap()
         || ((tx > (mapdisp->width() - halflgth))
         || (tx < halflgth)
         || (ty > mapdisp->height() - halflgth)
         || (ty < halflgth))) {
      // outside the screen
      double BearingAngle;
      if ( !ManualWp ) {
         BearingAngle = gpsData->fix.bearing.val;
      } else {
         BearingAngle = calcAngle(dest, currentPos);
      }
      if (mapDispOptions.rotateMap)
         BearingAngle += mapOrientation;
      BearingAngle = deg2rad(BearingAngle);
      tx = xcenter + (int)rint((double)(sin(BearingAngle)
               * (double)lgth * 3.0));
      ty = ycenter - (int)rint((double)(cos(BearingAngle)
               * (double)lgth * 3.0));

      painter->drawLine(xcenter, ycenter, tx, ty);
   } else {
      painter->drawLine(xcenter, ycenter, tx, ty);
      painter->drawRect(tx - lgth / 4, ty - lgth / 4, halflgth, halflgth);
   }
}

void MapDisp::DrawScaleLine(QPainter *painter)
{
	double	dlg,x,y,x1,y1;			/* Draw scale line */
	QString	sc;

	if ((cos(deg2rad(currentPos.lat)) != 0) && mapSelector->isMap())
	{
		MapBase *map = mapSelector->currentMap();
      dlg = 360.0 * (map->scale / 100000.0)
         / (40074.16 * cos(deg2rad(currentPos.lat)));
      if ( dlg != 0 ) {
         if (mapDispOptions.zoom2x)
            dlg *= 2;
         QPen pen = painter->pen();
         Position tmp(currentPos.lat, currentPos.lon + dlg);
         map->calcxy(&x, &y, tmp);
         map->calcxy(&x1, &y1, currentPos);
         x = fabs(x - x1);
         //painter.save();
         pen.setColor(mapDispOptions.scaleColor);
         pen.setWidth(3);
         painter->setPen(pen);
			QFont f= painter->font();
			f.setPixelSize(f.pixelSize()-2);
			f.setPointSize(16);
			painter->setFont(f);
			int ty= mapdisp->height()-19;
			painter->drawLine(6, ty, (int)ceil(x)+8, ty);
         QString sc = tr(" %1 km.").arg(map->scale / 100000.0, 0, 'f', 2);
         painter->drawText(mapdisp->rect(), AlignBottom | AlignLeft, sc);
         /* End of Drawing scale line  */
		}
	}
}

void MapDisp::DrawPlaces(QPainter *painter)
{
   if (!mapSelector->isMap() || places->count() == 0)
      return;

   QFont f= painter->font();
   f.setPointSize(placesOptions.placesTextSize);
   f.setBold(true);
   painter->setFont(f);
   
   QPen pen=painter->pen();
   pen.setColor(placesOptions.placesColor);

   // calculate limits
   int mx, my;
   if (mapDispOptions.rotateMap && !mapDispOptions.mode) {
      mx = my = (int) sqrt(mapdisp->width()*mapdisp->width() +
         mapdisp->height()*mapdisp->height());
   } else {
      mx = mapdisp->width()/2;
      my = mapdisp->height()/2;
   }
   if (mapDispOptions.zoom2x) {
      mx = mx/2;
      my = my/2;
   }
   Position nw;
   Position se;
   mapSelector->currentMap()->calcPos(nw, (double) centerX-mx, 
      (double) centerY-my);
   mapSelector->currentMap()->calcPos(se, (double) centerX+mx, 
      (double) centerY+my);

   double xwp, ywp;
   // find the first place 
   Place *place = placesIt.current();
   while (place && place->pos.lat < nw.lat) {
      place = --placesIt;
   }
   if (place == NULL) {
      // at the start
      placesIt.toFirst();
   }
   ++placesIt;
   place = placesIt.current();
   while (place && !place->pos.lat > nw.lat) {
      ++placesIt;
      place = placesIt.current();
   }
   int nPlaces = 0;
   while (place && place->pos.lat > se.lat) {
      if (place->pos.lon > nw.lon && place->pos.lon < se.lon) {
         mapSelector->currentMap()->calcxy(&xwp, &ywp, place->pos);
         QPoint scrPos = mapToScreen(QPoint((int) rint(xwp), 
            (int) rint(ywp)));
         if (mapdisp->rect().contains(scrPos)) {
            // draw the place
            pen.setWidth(4);
            painter->setPen(pen);
            painter->drawRect(scrPos.x()-1, scrPos.y()-1, 2, 2);
            
            pen.setWidth(3);
            painter->setPen(pen);
            painter->drawText(scrPos.x()+3, scrPos.y(), place->name);
         }
      }

      nPlaces++;
      ++placesIt;
      place = placesIt.current();
   }
   // points the iterator to the first Place drawn
   placesIt -= nPlaces;
}

void MapDisp::paintEvent(QPaintEvent * )
{
   createMap();
   bitBlt(this, 0, 0, mapdisp);
}

void MapDisp::timerDone()
{
   if ( mapDispOptions.mode ) {
      if ( mapSelector->isMap() ) {
         MapDispMesure(mouseEventX, mouseEventY);
      }
   } else { 
      if ( mapSelector->isMap() ) 
         MapDispDist(mouseEventX, mouseEventY);  
   }
}

void MapDisp::mousePressEvent(QMouseEvent *e)
{
   noDBLC=true;
   //if (FirstKey){    emit mouseClick(this);    emit mouseClick(this); FirstKey=false;}

   if ( timer->isActive() ) {
      timer->stop();
      noDBLC=false;
      swichMode();
   } else if (e->button() == Qt::RightButton && noMouseMoved ) {
      // display the context menu
      if (mapDispOptions.mode)
         ContextMenuManual(e->pos());
      else
         ContextMenuGps(e->pos());
      noDBLC=false;
   }

   mouseEventX=e->x();
   mouseEventY=e->y();
   noMouseMoved = true;
}

void MapDisp::mouseMoveEvent(QMouseEvent *e)
{
   if (abs(mouseEventX-e->x()) + abs(mouseEventY-e->y()) > 8)
      noMouseMoved = false;
}

void MapDisp::mouseReleaseEvent(QMouseEvent *e)
{
   if (e->x() == -1 && e->y() == -1)
      return;
   if (!noMouseMoved) {
      if ( timer->isActive() ) {
         timer->stop();
      }
      if ( mapDispOptions.mode && mapSelector->isMap() ) {
         Position pos;
         int incX = e->x() - mouseEventX;
         int incY = e->y() - mouseEventY;
         if (mapDispOptions.zoom2x) {
            incX /= 2;
            incY /= 2;
         }   
         if ( mapSelector->currentMap()->calcPos(pos, 
               centerX - incX, centerY - incY) ){
            currentPos.lon = pos.lon;
            currentPos.lat = pos.lat;

            update();
         }
      }
   } else if ( noDBLC ) {
      timer->start(200, true);
      noDBLC = true;
   }
}

void MapDisp::swichMode()
{
   if ( mapDispOptions.mode ) {
//      *gpsData->statusStr = "";
      currentPos.lat = gpsData->fix.position.lat;
      currentPos.lon = gpsData->fix.position.lon;
      mapDispOptions.mode = MapDispOptions::GpsMode;
   } else {
//      *gpsData->statusStr = "X";
      mapDispOptions.mode = MapDispOptions::ManualMode;
   }
   update();
}


//void MapDisp::mouseDoubleClickEvent(QMouseEvent *) {timer->stop();swichMode(); }

void MapDisp::MapDispDist(int mouseEventX, int mouseEventY)
{
   QPoint mapPos = screenToMap(QPoint(mouseEventX, mouseEventY));
   Position pos;
   if ( mapSelector->isMap() &&
         mapSelector->currentMap()->calcPos(pos, (double) 
         mapPos.x(), (double) mapPos.y()) ) {
      QString pos2InfoStr = tr("Lat=%1, Lon=%2")
         .arg(Position::string(currentPos.lat), Position::Lat)
         .arg(Position::string(currentPos.lon), Position::Lon);
      QString posInfoStr = tr("Lat=%1, Lon=%2")
         .arg(Position::string(pos.lat, Position::Lat))
         .arg(Position::string(pos.lon, Position::Lon));
      double clatr = deg2rad(currentPos.lat);
      double clonr = deg2rad(currentPos.lon);
      double latr = deg2rad(pos.lat);
      double lonr = deg2rad(pos.lon);
      double dist = acos(
            cos(clatr) * cos(clonr) * cos(latr) * cos(lonr)
            + cos(clatr) * sin(clonr) * cos(latr) * sin(lonr)
            + sin(clatr) * sin(latr)
            ) * 6378;
      QString DistStr = tr("\n\n Distance (aprox.)= %1")
         .arg(dist, 0, 'f', 2); //.arg(dist1, 0, 'f' ,3);

      switch ( QMessageBox::information(this, "Pont coordinates",
               "Current position: " + pos2InfoStr +
               "\nClicked point: " + posInfoStr+DistStr + "km." +
               "\nPress:\n \"Cancel\" - Quit,\n \"Destination\" / \"OK\""
               + "- Set clicked point as distination,\n"
               + "\"Save\" - save place.\n\nSave? or Set Destination?"
               ,"Destination", "Save", "Cancel", 0, 2 ) )
      {
         case 0: /* Set Destination */
            dest.lat = rad2deg(latr);
            dest.lon = rad2deg(lonr);
            ManualWp = true;
            break;
         case 1: /* Save  */
            MapDispAddPos(pos, 0);
            break;
         case 2: /* Cancel */
            break;
      }
   }
   //    setFocus();
}

void MapDisp::MapDispMesure(int mouseEventX,int mouseEventY)
{
   QPoint mapPos = screenToMap(QPoint(mouseEventX, mouseEventY));
   Position pos;
   if ( mapSelector->isMap() &&
      mapSelector->currentMap()->calcPos(pos, (double) 
      mapPos.x(), (double) mapPos.y()) ) {
      QString posInfoStr = tr("Lat=%1, Lon=%2")
         .arg(Position::string(pos.lat, Position::Lat))
         .arg(Position::string(pos.lon, Position::Lon));
      if ( mPos.lat || mPos.lon ) {
         QString pos2InfoStr = tr("Lat=%1, Lon=%2")
            .arg(Position::string(mPos.lat, Position::Lat))
            .arg(Position::string(mPos.lon, Position::Lon));
         double latr = deg2rad(pos.lat);
         double lonr = deg2rad(pos.lon);
         double mlatr = deg2rad(mPos.lat);
         double mlonr = deg2rad(mPos.lon);
         double dist = acos(
               cos(mlatr) * cos(mlonr) * cos(latr) * cos(lonr)
               + cos(mlatr) * sin(mlonr) * cos(latr) * sin(lonr)
               + sin(mlatr) * sin(latr)) * 6378;
         QString DistStr;
         if ( accDist == 0 ) 
            DistStr = tr("\n\nDistance (aprox.)= %1").arg(dist, 0, 'f', 2); //.arg(dist1, 0, 'f', 3)
         else
            DistStr = tr("\n\nDistance (aprox.) = %1 + %2 = %3")
            .arg(accDist, 0, 'f', 2).arg(dist, 0, 'f', 2)
            .arg(dist + accDist, 0, 'f', 2); // .arg(dist1, 0, 'f', 3);

         switch ( QMessageBox::information(this, "Mesuring result",
                  "Prev. point: " + pos2InfoStr
                  + "\nLast point : "+ posInfoStr+DistStr + "km."
                  + "\nPress:\n \"Cancel\" - Quit,\n \"Measure\""
                  + "/ \"OK\" - measure summary distance to next point,\n"
                  + "\"Save&Measure\" - save place and measure distance.\n\n"
                  + "Save&Measure? or Measure (distance)?",
                  "Measure", "Save&&Measure", "Cancel", 0, 2) )
         {
            case 0: /* Measure */
               mPos = pos;
               accDist = accDist + dist;
               break;
            case 1: /* Save & Measure  */
               MapDispAddPos(pos, 0);
               mPos = pos;
               accDist = accDist + dist;
               break;
            case 2: /* Cancel */
               accDist = 0;
               mPos.lat = 0;
               mPos.lon = 0;
               break;
         }
      } else {
         switch ( QMessageBox::information(this, "Coordinates", posInfoStr
                  + "\nPress:\n \"Cancel\" - Quit,\n \"Save\""
                  + "/ \"OK\" - save place,\n \"Measure\" - measure distance."
                  + "\n\nSave (place)? or Measure (distance)?",
                  "Save", "Measure", "Cancel", 0, 2) )
         {
            case 0:
               MapDispAddPos(pos, 0);
               break;
            case 1:
               mPos = pos;
               break;
            case 2:
               break;
         }
      }
   }
   //    setFocus();
}

void MapDisp::keyPressEvent(QKeyEvent * e)
{
//   if ( FirstKey ) {
//      emit mouseClick(this);
//      emit mouseClick(this);
//      FirstKey = false;
//   }

   if ( mapDispOptions.mode ) { 
      switch ( e->key() ) {
         case Qt::Key_F33:
         case Qt::Key_Space: // Select
         case Qt::Key_Escape: // Select
            emit mouseClick(this);
            break;
         case Qt::Key_Return:
            if ( e->state() && ShiftButton) {
               MapDispAddPos(currentPos, currentAltitude.val);
            } else {
               // cursor at center of screen
               ContextMenuManual(QPoint(mapdisp->width() / 2, 
                  mapdisp->height() / 2));
            }
            break;
         case Qt::Key_Down:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -