📄 zonemap.cpp
字号:
// Now decide where to move the label, x & y can be reused int tmpx, tmpy, x, y; zoneToWin( m_last.lon(), m_last.lat(), tmpx, tmpy ); contentsToViewport(tmpx, tmpy, x, y); // // Put default position for the city label in the "above left" // area. This avoids obscuring the popup for quite a bit of // the map. Use the "below right" position for the border cases. // x -= iLABELOFFSET + lblCity->width(); if (x < 0) { x += 2*iLABELOFFSET + lblCity->width(); } y -= iLABELOFFSET + lblCity->height(); if (y < 0) { y += 2*iLABELOFFSET + lblCity->height(); } // draw in the city and the label if ( m_repaint.isValid()) { int repx, repy; zoneToWin( m_repaint.lon(), m_repaint.lat(), repx, repy ); updateContents( repx - iCITYOFFSET, repy - iCITYOFFSET, iCITYSIZE, iCITYSIZE ); } updateContents( tmpx - iCITYOFFSET, tmpy - iCITYOFFSET, iCITYSIZE, iCITYSIZE ); m_repaint = m_last; lblCity->move( x, y ); lblCity->show();}void ZoneMap::resizeEvent( QResizeEvent *e ){ // // Disable zooming when resizing. // if (bZoom) { bZoom = FALSE; cmdZoom->setOn(FALSE); } // keep the zoom button down in the corner QSize _size = e->size(); cmdZoom->move( _size.width() - cmdZoom->width(), _size.height() - cmdZoom->height() ); if ( !bZoom ) { drawableW = width() - 2 * frameWidth(); drawableH = height() - 2 * frameWidth(); makeMap( drawableW, drawableH ); resizeContents( drawableW, drawableH ); } else { resizeContents( wImg, hImg ); }}void ZoneMap::drawCities( QPainter *p ){ int x,y; // draw in the cities // for testing only as when you put it // on the small screen it looks awful and not to mention useless p->setPen( red ); TimeZone curZone; const char *zoneID; QStrListIterator it( TimeZone::ids() ); for (; it.current(); ++it) { zoneID = it.current(); curZone = TimeZone( zoneID ); zoneToWin( curZone.lat(), curZone.lon(), x, y ); if ( x > wImg ) x = x - wImg; p->drawRect( x - iCITYOFFSET, y - iCITYOFFSET, iCITYSIZE, iCITYSIZE); }}static void dayNight(QImage *pImage){ // create a mask the functions from sun.h double dJulian, dSunRad, dSunDecl, dSunRadius, dSunLong; int wImage = pImage->width(), hImage = pImage->height(), iStart, iStop, iMid, relw, i; short * wtab = new short [ wImage ]; time_t tCurrent; struct tm *pTm; // get the position of the sun bassed on our current time... tCurrent = time( NULL ); pTm = gmtime( &tCurrent ); dJulian = jtime( pTm ); sunpos( dJulian, 0, &dSunRad, &dSunDecl, &dSunRadius, &dSunLong ); // now get the projected illumination projillum( wtab, wImage, hImage, dSunDecl ); relw = wImage - int( wImage * 0.0275 ); // draw the map, keeping in mind that we may go too far off the map... iMid = ( relw * ( 24*60 - pTm->tm_hour * 60 - pTm->tm_min ) ) / ( 24*60 ); for ( i = 0; i < hImage; i++ ) { if ( wtab[i] > 0 ) { iStart = iMid - wtab[i]; iStop = iMid + wtab[i]; if ( iStart < 0 ) { darken( pImage, iStop, wImage + iStart, i ); } else if ( iStop > wImage ) { darken( pImage, iStop - wImage, iStart, i ); } else { darken( pImage, 0, iStart, i ); darken( pImage, iStop, wImage, i ); } } else { darken( pImage, 0, wImage, i ); } } delete [] wtab;}static inline void darken( QImage *pImage, int start, int stop, int row ){ // Always clip stop parameter to ensure our preconditions if ( stop >= pImage->width() ) stop = pImage->width() - 1; // Assume that the image is 32bpp as we should have converted previously to that... QRgb *p = (QRgb *)pImage->scanLine( row ); for ( int j = start; j <= stop; j++ ) { QRgb rgb = p[j]; p[j] = qRgb( 2 * qRed( rgb ) / 3, 2 * qGreen( rgb ) / 3, 2 * qBlue( rgb ) / 3 ); }}void ZoneMap::makeMap( int w, int h ){ QImage imgOrig = Resource::loadImage( strMAP ); if ( imgOrig.isNull() ) { QMessageBox::warning( this, tr( "Couldn't Find Map" ), tr( "<p>Couldn't load map: %1, exiting") .arg( strMAP ) ); exit(-1); } // set up the color table for darkening... imgOrig = imgOrig.convertDepth( 32 ); // else go one with making the map... if ( bIllum ) { // do a daylight mask dayNight(&imgOrig); } // redo the width and height wImg = w; hImg = h; ox = ( wImg / 2 ) - int( wImg * 0.0275 ); oy = hImg / 2; pixCurr->convertFromImage( imgOrig.smoothScale(w, h), QPixmap::ThresholdDither );}void ZoneMap::drawCity( QPainter *p, const TimeZone &city ){ int x, y; p->setPen( red ); zoneToWin( city.lon(), city.lat(), x, y ); p->drawRect( x - iCITYOFFSET, y - iCITYOFFSET, iCITYSIZE, iCITYSIZE );}void ZoneMap::drawContents( QPainter *p, int cx, int cy, int cw, int ch ){ // if there is a need to resize, then do it... // get our drawable area drawableW = width() - 2 * frameWidth(); drawableH = height() - 2 * frameWidth(); int pixmapW = pixCurr->width(), pixmapH = pixCurr->height(); if ( !bZoom && ( ( pixmapW != drawableW ) || ( pixmapH != drawableH) ) ) { makeMap( drawableW, drawableH ); } // taken from the scrollview example... int rowheight = pixCurr->height(); int toprow = cy / rowheight; int bottomrow = ( cy + ch + rowheight - 1 ) / rowheight; int colwidth = pixCurr->width(); int leftcol= cx / colwidth; int rightcol= ( cx + cw + colwidth - 1 ) / colwidth; for ( int r = toprow; r <= bottomrow; r++ ) { int py = r * rowheight; for ( int c = leftcol; c <= rightcol; c++ ) { int px = c * colwidth; p->drawPixmap( px, py, *pixCurr ); } } // Draw that city! if ( m_last.isValid() ) drawCity( p, m_last );}void ZoneMap::slotZoom( bool setZoom ){ bZoom = setZoom; if ( bZoom ) { makeMap( 2 * wImg , 2 * hImg ); resizeContents( wImg, hImg ); } else { makeMap( drawableW, drawableH ); resizeContents( drawableW, drawableH ); }}void ZoneMap::slotIllum( bool setIllum ){ bIllum = !setIllum; // make the map... makeMap( pixCurr->width(), pixCurr->height() ); updateContents( 0, 0, wImg, hImg );}void ZoneMap::slotUpdate( void ){ // recalculate the light, most people will never see this, // but it is good to be complete makeMap ( pixCurr->width(), pixCurr->height() ); updateContents( contentsX(), contentsY(), drawableW, drawableH );}void ZoneMap::slotRedraw( void ){ // paint over that pesky city... int x, y; if ( m_repaint.isValid() ) { m_last = TimeZone(); zoneToWin(m_repaint.lon(), m_repaint.lat(), x, y); updateContents( x - iCITYOFFSET, y - iCITYOFFSET, iCITYSIZE, iCITYSIZE); m_repaint = TimeZone(); }}void ZoneMap::initCities(){ // Contructing TimeZone::TimeZone( city ) is hideously expensive - // preload the position of each city. if ( citiesInit ) return; QStrList list = TimeZone::ids() ; QStrListIterator it( list ); int count = 0; cities.resize( list.count() ); for (; it.current(); ++it) { QCString zoneID = it.current(); TimeZone curZone( zoneID ); if ( !curZone.isValid() ){ qDebug("ZoneMap::slotFindCity Invalid zoneID %s", zoneID.data() ); continue; } CityPos *cp = new CityPos; cp->lat = curZone.lat(); cp->lon = curZone.lon(); cp->id = zoneID.copy(); cities.insert( count++, cp ); } cities.resize(count); citiesInit = TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -