📄 gps.cpp
字号:
load(mymapinfo->name.cstring()); if(mymap.width() != mymapinfo->pixwidth || mymap.height() != mymapinfo->pixheight){ printwhinge("Map %s, database gives width %d height %d but image has width %d height %d",mymapinfo->name.cstring(), mymapinfo->pixwidth, mymapinfo->pixheight, mymap.width(), mymap.height()); } // Now figure out where the offset should be to put these coords // under the centre of the picture. QPoint p = pos_to_mappixels(cpr); // printf("positionto xcoord %d ycoord %d\n", p.x(), p.y()); if(picoffset != p){ picoffset = p; repaint(true); // arg specifies whether to erase background or not } return true; // got a map } // No map found. So that plotting can continue, we produce a fake map // one pixel wide and one high, with its corner in just the right place // so that we don't have to move anything and some suitable (ish) scaling // to draw the track. mydummymapinfo.longmin = mydummymapinfo.longmax = cpr.longit; mydummymapinfo.latmin = mydummymapinfo.latmax = cpr.latit; mydummymapinfo.osproj = 0; const double scale = 1e3; mydummymapinfo.pixperdegwidth = mydummymapinfo.pixperdegheight = scale; mymapinfo = &mydummymapinfo; myGpsp->mapscales.clear(); myGpsp->mapscales += scale; mydummymapinfo.scalefamily = scale; myGpsp->showmapscales(&mydummymapinfo); if(verbose) printf("Using dummy map when no real map found\n"); picoffset.setX(0); picoffset.setY(0); repaint(true); return false;}void Mapwindow::home(const highlight &hr){ if(verbose)printf("Mapwindow home called for lat %f long %f\n", hr.pos.latit, hr.pos.longit); positionto(hr.pos);}bool GPS::event(QEvent *ep){ if(ep->type() == Event_KeyPress){ int key = Q_KEY_EVENT(ep)->ascii(); if(key == ' '){ const n = scalebuttonptrs.element_count(); for(int i = 0; i < n; i++){ if(scalebuttonptrs[i]->isChecked()){ int nextbutton = (i+1)%n; // printf("Next button to check is %d\n", nextbutton); scalegrp->setButton(nextbutton); slotMaptoggle(nextbutton); break; } } } } return QWidget::event(ep);}void GPS::resizeEvent(QResizeEvent *rep){ mymap->resize(rep->size().width()-mymap_x, rep->size().height() - menubar->size().height());}void GPS::showmapscales(const mapinfo *cmp){ // First clear any existing ones while(scalebuttonptrs.element_count()){ QRadioButton *p = scalebuttonptrs.last_element(); scalegrp->remove(p); delete p; scalebuttonptrs.delete_last_element(); } for(int i = 0; i < mapscales.element_count(); i++){ String s; s.sprintf("%.2f", mapscales[i]); QRadioButton *p = new QRadioButton(s.cstring(), this); CHECK_PTR(p); if(mapscales[i] == cmp->scalefamily) p->setChecked(true); scalebuttonptrs += p; scalegrp->insert(p, i); p->setGeometry(scales_x, mbht+scales_y+(i+1)*(lhlabels_h+lhlabelsspacing), leftbarwidth, lhlabels_h); p->show(); }}void GPS::slotMaptoggle(int i){ double newscale = mapscales[i]; mymap->setpreferredmapscale(newscale);}void GPS::stopplotreplay(){// printf("Stop plot/replay\n"); delete replaytimer; replaytimer = 0; closelogfile(); filemenu->changeItem("&Replay", replayid); filemenu->changeItem("&Plot", plotid); filemenu->setItemEnabled(plotid, true); filemenu->setItemEnabled(replayid, true); utc->hide(); speed->hide(); hdng->hide(); plotting = false;}void GPS::slotAbout(){ QMessageBox::information(this, "About GPS", "This is version 1.0 of the GPS application");}void GPS::slotPlot(){// printf("In slotPlot\n"); // Going already? if(replaytimer){ stopplotreplay(); return; } if(!openlogfile("logfile", true)){ printwhinge("Can't open the logfile for writing: %s", strerror(errno)); } String s = String("New Journey: "); time_t now = time(0); s += ctime(&now); putlogfpstring(s.cstring()); asyncfdes = open("/dev/cua0", O_RDWR | O_NDELAY); if(asyncfdes < 0){ printwhinge("Can't open the serial port: %s", strerror(errno)); return; } struct termios asyncterm; if (tcgetattr (asyncfdes, &asyncterm) < 0) { printwhinge ("Problem getting async term params: %s", strerror(errno)); return; } cfmakeraw (&asyncterm); // asyncterm.c_cflag = CSTOPB | CREAD | CS8 | CLOCAL | PARENB | PARODD; asyncterm.c_cflag = CREAD | CS8 | CLOCAL; asyncterm.c_lflag = ICANON; asyncterm.c_iflag = IGNCR; cfsetospeed (&asyncterm, B4800); cfsetispeed (&asyncterm, B4800); if (tcsetattr (asyncfdes, TCSANOW, &asyncterm) < 0) { printwhinge ("Problem setting async term params: %s", strerror(errno)); perror (""); return; } plotting = true; filemenu->setItemEnabled(replayid, false); filemenu->changeItem("Stop &Plot", plotid); startplotreplay();}void GPS::slotReplay(){// printf("In slotReplay\n"); plotting = false; // Going already? if(replaytimer){ stopplotreplay(); return; } if(!openlogfile("logfile")){ printwhinge("Can't open logfile, reason %s", strerror(errno)); return; } printf("logfile opened\n"); filemenu->setItemEnabled(plotid, false); filemenu->changeItem("Stop &Replay", replayid); startplotreplay();}void GPS::startplotreplay(){ mymap->cleartrack(); printf("track cleared\n"); replaytimer = new QTimer( this ); if(plotting){ connect( replaytimer, SIGNAL(timeout()), this, SLOT(slotPlottick()) ); replaytimer->start( 1000, FALSE ); }else{ connect( replaytimer, SIGNAL(timeout()), this, SLOT(slotReplaytick()) ); replaytimer->start( 100, FALSE ); } utc->setText(""); utc->show(); speed->setText(""); speed->show(); hdng->setText(""); hdng->show();}void GPS::slotPlottick(){ // printf("Plot timer ticked\n"); char buff[512]; if(asyncfdes < 0){ printf("Plottick: asyncfdes not open!\n"); return; } int res; while((res = read(asyncfdes, buff, 512)) > 0){ // printf("Plottick read %d bytes\n", res); if(res > 512) res = 512; buff[res-1] = 0; if(verbose) printf("Read \"%s\"\n", buff); double latit, longit, utctime; if(rcvplotstr(buff, latit, longit, utctime)){ mymap->trackto(latit, longit, utctime, 0); } }}void GPS::slotReplaytick(){ double latit, longit; double utctime; if(getlogll(latit, longit, utctime)){ // printf("Replay, read lat %f long %f\n", latit, longit); if(latit < -90 || latit > 90 || longit < -180 || longit > 180){ printwhinge("Duff logfile data suspected and ignored, lat %f long %f\n", latit, longit); }else{ mymap->trackto(latit, longit, utctime, 30); // Not needed surely ... qApp->processEvents(); } }else{ closelogfile(); delete replaytimer; replaytimer = 0; filemenu->changeItem("&Replay", replayid); }}GPS::GPS():plotting(false){ // Menubar stuff filemenu = new QPopupMenu(); CHECK_PTR(filemenu); replayid = filemenu->insertItem("&Replay", this, SLOT(slotReplay())); plotid = filemenu->insertItem("&Plot", this, SLOT(slotPlot())); filemenu->insertItem("E&xit", qApp, SLOT(quit())); helpmenu = new QPopupMenu(); CHECK_PTR(helpmenu); helpmenu->insertItem("&About", this, SLOT(slotAbout())); menubar = new QMenuBar(this); CHECK_PTR(menubar); menubar->insertItem("&File", filemenu); menubar->insertSeparator(); menubar->setSeparator(QMenuBar::InWindowsStyle); menubar->insertItem("&Help", helpmenu); mbht = menubar->size().height(); mymousex = new QLabel(this); CHECK_PTR(mymousex); mymousex->setGeometry(mymousex_x, mymousex_y+mbht, mymousex_w, mymousex_h); mymousex->setFrameStyle( QFrame::Panel | QFrame::Sunken ); mymousey = new QLabel(this); CHECK_PTR(mymousey); mymousey->setGeometry(mymousey_x, mymousey_y+mbht, mymousey_w, mymousey_h); mymousey->setFrameStyle( QFrame::Panel | QFrame::Sunken ); altpos = new QLabel(this); CHECK_PTR(altpos); altpos->setGeometry(altpos_x, altpos_y+mbht, altpos_w, altpos_h); altpos->setFrameStyle( QFrame::Panel | QFrame::Sunken ); // altpos not necessarily visible altpos->hide(); utc = new QLabel(this); CHECK_PTR(utc); utc->setGeometry(utc_x, utc_y+mbht, utc_w, utc_h); utc->setFrameStyle( QFrame::Panel | QFrame::Sunken ); utc->hide(); speed = new QLabel(this); CHECK_PTR(utc); speed->setGeometry(speed_x, speed_y+mbht, speed_w, utc_h); speed->setFrameStyle( QFrame::Panel | QFrame::Sunken ); speed->hide(); hdng = new QLabel(this); CHECK_PTR(utc); hdng->setGeometry(hdng_x, hdng_y+mbht, hdng_w, utc_h); hdng->setFrameStyle( QFrame::Panel | QFrame::Sunken ); hdng->hide(); mymap = new Mapwindow(this); CHECK_PTR(mymap); mymap->setGeometry(mymap_x, mymap_y+mbht, mymap_w, mymap_h); mymap->setFrameStyle(QFrame::Box | QFrame::Plain); mymap->setLineWidth(1); scalegrp = new QButtonGroup(this); CHECK_PTR(scalegrp); connect(scalegrp, SIGNAL(clicked(int)), SLOT(slotMaptoggle(int)) ); scalegrp->hide(); scale = new QLabel(this); CHECK_PTR(scale); scale->setFrameStyle( QFrame::Panel | QFrame::Raised ); scale->setText(" Map Scales"); scale->setGeometry(scales_x, mbht+scales_y, leftbarwidth, lhlabels_h); // scale->show(); mymap->home(home); resize(startwidth,startheight);}const char *wrkdir = "./maps";const char *landmarkfile = "highlts.dat";const char *maplist = "maps.dat";highlight home;int loadhighlights(){ // Read the highlights if(!openlandmarkfile(landmarkfile)){ printwhinge("Can't open landmark file %s, reason %s", landmarkfile, strerror(errno)); return 0; } highlight h; const bufflen = 512; char buff1[bufflen], buff2[bufflen]; while(getlandmark( &h.pos.latit, &h.pos.longit, buff1, bufflen, buff2, bufflen)){ h.description = buff1; if(h.description == "home"){ if(verbose)printf("Read home landmark, latit %f, longit %f, description %s, type %s\n", h.pos.latit, h.pos.longit, buff1, buff2); home = h; } h.type = buff2; highlightlist += h; } closelandmarkfile(); return 1;}int main(int argc, char **argv){ try{ QApplication myapp(argc, argv); if(argc > 1){ if(strchr(argv[1], 'v')) verbose = 1; } if(chdir(wrkdir)){ printwhinge("Can't chdir to %s, reason %s", wrkdir, strerror(errno)); return 1; } if(!loadhighlights() || !initmaplist(maplist)){ return 1; } Gps = new GPS(); CHECK_PTR(Gps); myapp.setMainWidget(Gps); Gps->show(); return myapp.exec(); } catch(...){ printf("Caught exception in main!!\n"); } return 1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -