Esempio n. 1
0
void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
{
  mSettings.setUnits( units );
  if ( mMap )
  {
    double upperMagnitudeMultiplier = 1.0;
    double widthInSelectedUnits = mapWidth();
    double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
    mSettings.setUnitsPerSegment( initialUnitsPerSegment );

    setUnitLabel( QgsUnitTypes::toAbbreviatedString( units ) );
    upperMagnitudeMultiplier = 1;

    double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
    int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
    double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
    double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;

    if ( multiplier > 0 )
    {
      unitsPerSegment = unitsPerSegment * multiplier;
    }
    mSettings.setUnitsPerSegment( unitsPerSegment );
    mSettings.setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );

    mSettings.setNumberOfSegments( 4 );
    mSettings.setNumberOfSegmentsLeft( 2 );
  }

  refreshSegmentMillimeters();
  resizeToMinimumWidth();
  emit changed();
}
Esempio n. 2
0
void QgsComposerScaleBar::applyDefaultSize()
{
  if ( mComposerMap )
  {
    setUnits( Meters );
    double widthMeter = mapWidth();
    int nUnitsPerSegment =  widthMeter / 10.0; //default scalebar width equals half the map width
    setNumUnitsPerSegment( nUnitsPerSegment );

    if ( nUnitsPerSegment > 1000 )
    {
      setNumUnitsPerSegment(( int )( numUnitsPerSegment() / 1000.0 + 0.5 ) * 1000 );
      setUnitLabeling( tr( "km" ) );
      setNumMapUnitsPerScaleBarUnit( 1000 );
    }
    else
    {
      setUnitLabeling( tr( "m" ) );
    }

    setNumSegments( 4 );
    setNumSegmentsLeft( 2 );
  }

  refreshSegmentMillimeters();
  adjustBoxSize();
  emit itemChanged();
}
Esempio n. 3
0
void QgsMapToPixel::updateMatrix()
{
  double rotation = mapRotation();

#if 0 // debugging
  QgsDebugMsg( QString( "XXX %7 -- xCent:%1 yCent:%2 mWidth:%3 mHeight:%4 uPP:%5 rot:%6" )
               .arg( xCenter ).arg( yCenter ).arg( mWidth ).arg( mHeight )
               .arg( mMapUnitsPerPixel ).arg( rotation ).arg(( quintptr )this, QT_POINTER_SIZE *2, 15, QChar( '0' ) ) );
#endif

  // NOTE: operations are done in the reverse order in which
  //       they are configured, so translation to geographical
  //       center happens first, then scaling, then rotation
  //       and finally translation to output viewport center

  if ( qgsDoubleNear( rotation, 0.0 ) )
  {
    //no rotation, return a simplified matrix
    mMatrix = QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
              .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
    return;
  }

  double cy = mapHeight() / 2.0;
  double cx = mapWidth() / 2.0;
  mMatrix = QTransform::fromTranslate( cx, cy )
            .rotate( rotation )
            .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
            .translate( -xCenter, -yCenter )
            ;
}
Esempio n. 4
0
void QgsComposerScaleBar::refreshSegmentMillimeters()
{
  if ( mComposerMap )
  {
    //get mm dimension of composer map
    QRectF composerItemRect = mComposerMap->rect();

    if ( mSegmentSizeMode == SegmentSizeFixed )
    {
      //calculate size depending on mNumUnitsPerSegment
      mSegmentMillimeters = composerItemRect.width() / mapWidth() * mNumUnitsPerSegment;
    }
    else /*if(mSegmentSizeMode == SegmentSizeFitWidth)*/
    {
      if ( mMaxBarWidth < mMinBarWidth )
      {
        mSegmentMillimeters = 0;
      }
      else
      {
        double nSegments = ( mNumSegmentsLeft != 0 ) + mNumSegments;
        // unitsPerSegments which fit minBarWidth resp. maxBarWidth
        double minUnitsPerSeg = ( mMinBarWidth * mapWidth() ) / ( nSegments * composerItemRect.width() );
        double maxUnitsPerSeg = ( mMaxBarWidth * mapWidth() ) / ( nSegments * composerItemRect.width() );

        // Start with coarsest "nice" number closest to minUnitsPerSeg resp
        // maxUnitsPerSeg, then proceed to finer numbers as long as neither
        // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are are in
        // [minUnitsPerSeg, maxUnitsPerSeg]
        double lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg );
        double upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg );

        double d = 1;
        while ( lowerNiceUnitsPerSeg > maxUnitsPerSeg && upperNiceUnitsPerSeg < minUnitsPerSeg )
        {
          d *= 10;
          lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg, d );
          upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg, d );
        }

        // Pick mNumUnitsPerSegment from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
        mNumUnitsPerSegment = upperNiceUnitsPerSeg < minUnitsPerSeg ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg;
        mSegmentMillimeters = composerItemRect.width() / mapWidth() * mNumUnitsPerSegment;
      }
    }
  }
}
void QgsComposerScaleBar::refreshSegmentMillimeters()
{
  if ( mComposerMap )
  {
    //get extent of composer map
    QgsRectangle composerMapRect = *( mComposerMap->currentMapExtent() );

    //get mm dimension of composer map
    QRectF composerItemRect = mComposerMap->rect();

    //calculate size depending on mNumUnitsPerSegment
    mSegmentMillimeters = composerItemRect.width() / mapWidth() * mNumUnitsPerSegment;
  }
}
Esempio n. 6
0
QgsUnitTypes::DistanceUnit QgsLayoutItemScaleBar::guessUnits() const
{
  if ( !mMap )
    return QgsUnitTypes::DistanceMeters;

  QgsCoordinateReferenceSystem crs = mMap->crs();
  // start with crs units
  QgsUnitTypes::DistanceUnit unit = crs.mapUnits();
  if ( unit == QgsUnitTypes::DistanceDegrees || unit == QgsUnitTypes::DistanceUnknownUnit )
  {
    // geographic CRS, use metric units
    unit = QgsUnitTypes::DistanceMeters;
  }

  // try to pick reasonable choice between metric / imperial units
  double widthInSelectedUnits = mapWidth();
  double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
  switch ( unit )
  {
    case QgsUnitTypes::DistanceMeters:
    {
      if ( initialUnitsPerSegment > 1000.0 )
      {
        unit = QgsUnitTypes::DistanceKilometers;
      }
      break;
    }
    case QgsUnitTypes::DistanceFeet:
    {
      if ( initialUnitsPerSegment > 5419.95 )
      {
        unit = QgsUnitTypes::DistanceMiles;
      }
      break;
    }
    default:
      break;
  }

  return unit;
}
Esempio n. 7
0
QTransform QgsMapToPixel::transform() const
{
  // NOTE: operations are done in the reverse order in which
  //       they are configured, so translation to geographical
  //       center happens first, then scaling, then rotation
  //       and finally translation to output viewport center

  double rotation = mapRotation();
  if ( qgsDoubleNear( rotation, 0.0 ) )
  {
    //no rotation, return a simplified matrix
    return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
           .translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
  }
  else
  {
    double cy = mapHeight() / 2.0;
    double cx = mapWidth() / 2.0;
    return QTransform::fromTranslate( cx, cy )
           .rotate( rotation )
           .scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
           .translate( -xCenter, -yCenter );
  }
}
Esempio n. 8
0
void QgsComposerScaleBar::applyDefaultSize( QgsComposerScaleBar::ScaleBarUnits u )
{
  if ( mComposerMap )
  {
    setUnits( u );
    double upperMagnitudeMultiplier = 1.0;
    double widthInSelectedUnits = mapWidth();
    double initialUnitsPerSegment =  widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
    setNumUnitsPerSegment( initialUnitsPerSegment );

    switch ( mUnits )
    {
      case MapUnits:
      {
        upperMagnitudeMultiplier = 1.0;
        setUnitLabeling( tr( "units" ) );
        break;
      }
      case Meters:
      {
        if ( initialUnitsPerSegment > 1000.0 )
        {
          upperMagnitudeMultiplier = 1000.0;
          setUnitLabeling( tr( "km" ) );
        }
        else
        {
          upperMagnitudeMultiplier = 1.0;
          setUnitLabeling( tr( "m" ) );
        }
        break;
      }
      case Feet:
      {
        if ( initialUnitsPerSegment > 5419.95 )
        {
          upperMagnitudeMultiplier = 5419.95;
          setUnitLabeling( tr( "miles" ) );
        }
        else
        {
          upperMagnitudeMultiplier = 1.0;
          setUnitLabeling( tr( "ft" ) );
        }
        break;
      }
      case NauticalMiles:
      {
        upperMagnitudeMultiplier = 1;
        setUnitLabeling( tr( "Nm" ) );
        break;
      }
    }

    double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
    int segmentMagnitude = floor( log10( segmentWidth ) );
    double unitsPerSegment = upperMagnitudeMultiplier * ( qPow( 10.0, segmentMagnitude ) );
    double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;

    if ( multiplier > 0 )
    {
      unitsPerSegment = unitsPerSegment * multiplier;
    }
    setNumUnitsPerSegment( unitsPerSegment );
    setNumMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );

    setNumSegments( 4 );
    setNumSegmentsLeft( 2 );
  }

  refreshSegmentMillimeters();
  adjustBoxSize();
  emit itemChanged();
}
Esempio n. 9
0
//This function is called each time it is your turn.
//Return true to end your turn, return false to ask the server for updated information.
bool AI::run()
{
  Fish* coolFish = getFish(0, 0, fishes);
  if(coolFish != NULL)
  {
    coolFish->carryingWeight();
  }
  for(int i = 0;i < tiles.size();i++)
  {
    if(tiles[i].owner() == playerID() &&
       getFish(tiles[i].x(), tiles[i].y(), fishes) == NULL)
    {
      for(int p = 0;p<species.size(); p++)
      {
        if(species[p].season() == currentSeason())
        {
          if(players[playerID()].spawnFood() >= species[p].cost() &&
             tiles[i].hasEgg() == false)
          {
            species[p].spawn(tiles[i].x(), tiles[i].y());
          }
        }
      }
    }
  }
  for(int i = 0;i < fishes.size();i++)
  {
    if(fishes[i].owner() == playerID())
    {
      int x = fishes[i].x();
      int y = fishes[i].y();
      if(fishes[i].x() >= 1)
      {
        if(getTile(x - 1, y, mapHeight(), tiles).trashAmount() > 0 &&
           fishes[i].carryingWeight() + 1 <= fishes[i].carryCap())
        {
          fishes[i].pickUp(x - 1, y, 1);
        }
      }
      if(fishes[i].carryingWeight() > 0)
      {
        if(fishes[i].x() < mapWidth()/2 - boundLength() - 1)
        {
          if(fishes[i].y() != 0)
          {
            if(getTile(x,y - 1,mapHeight(),tiles).owner() == 2 &&
               getFish(x,y + 1,fishes) == NULL)
            {
              fishes[i].drop(x, y - 1, fishes[i].carryingWeight());
            }
          }
          else
          {
            if(getTile(x,y + 1,mapHeight(),tiles).owner() == 2 &&
               getFish(x,y + 1,fishes) == NULL)
            {
              fishes[i].drop(x, y + 1, fishes[i].carryingWeight());
            }
          }
        }
      }
      if(fishes[i].x() >= 1)
      {
        if(getTile(x - 1,y,mapHeight(),tiles).owner() != enemyAI_ID &&
           getTile(x - 1,y,mapHeight(),tiles).trashAmount() == 0)
        {
          if(getFish(x - 1, y, fishes) == NULL &&
             getTile(x - 1, y, mapHeight(), tiles).hasEgg() == false)
          {
            fishes[i].move(x - 1,y);
          }
        }
      }
    }
  }
  return true;
}
Esempio n. 10
0
//This function is called each time it is your turn.
//Return true to end your turn, return false to ask the server for updated information.
bool AI::run()
{
  //loop through all of the tiles
  for(int i = 0;i < tiles.size();i++)
  {
    //if this tile is one of my coves and does not have a fish on it
    if(tiles[i].owner() == playerID() &&
       getFishIndex(tiles[i].x(), tiles[i].y()) == -1)
    {
       if(getSpecies(SEA_STAR).spawn(tiles[i].x(),tiles[i].y()))
          std::cout<<"I am fish"<<std::endl;
       /*
      //loop through all of the species
      for(int p = 0;p<species.size(); p++)
      {
        //if the current species is in season
        if(species[p].season() == currentSeason())
        {
          //if I have enough food to spawn the fish in and there is not
          //an egg on that tile already
          if(players[playerID()].spawnFood() >= species[p].cost() &&
             tiles[i].hasEgg() == false)
          {
            //spawn the fish on that tile
            species[p].spawn(tiles[i].x(), tiles[i].y());
          }
        }
      }*/
    }
  }

  //loop through all of the fish
  for(int i = 0;i < fishes.size();i++)
  {
    //if this is my fish
    if(fishes[i].owner() == playerID())
    {
      int x = fishes[i].x();
      int y = fishes[i].y();
      if(fishes[i].x() >= 1)
      {
        //if the tile to the left has trash and the current fish can
        //carry at least one more trash
        if(getTile(x - 1, y).trashAmount() > 0 &&
           fishes[i].carryingWeight() + 1 <= fishes[i].carryCap())
        {
          //if the fish has enought health to pick up trash
          if(1 * trashDamage() < fishes[i].currentHealth())
          {
            //pick up trash to the left of the fish
            fishes[i].pickUp(x - 1, y, 1);
          }
        }
      }
      //if the fish carrying any trash
      if(fishes[i].carryingWeight() > 0)
      {
        //if the fish in the enemy's side
        if((fishes[i].x() < mapWidth()/2 - boundLength() - 1 && enemyAI_ID == 0) ||
           (fishes[i].x() > mapWidth()/2 + boundLength() + 1 && enemyAI_ID == 1))
        {
          if(fishes[i].y() != 0)
          {
            //if the tile above the fish is not a cove and has no fish
            if(getTile(x, y - 1).owner() == 2 && getFishIndex(x, y + 1) == -1)
            {
              //drop all of the trash the fish is carrying
              fishes[i].drop(x, y - 1, fishes[i].carryingWeight());
            }
          }
          else if(fishes[i].y() != mapHeight() - 1)
          {
            //if the tile below the fish is not a cove and has no fish
            if(getTile(x,y + 1).owner() == 2 && getFishIndex(x,y + 1) == -1)
            {
              //drop all of the trash the fish is carrying
              fishes[i].drop(x, y + 1, fishes[i].carryingWeight());
            }
          }
        }
      }
      if(fishes[i].x() >= 1)
      {
        //if the tile to the left is not an enemy cove and there is no trash
        if(getTile(x - 1,y).owner() != enemyAI_ID &&
           getTile(x - 1,y).trashAmount() == 0)
        {
          //if the tile to the left does not have a fish and does not have an
          //egg
          if(getFishIndex(x - 1, y) == -1 &&
             getTile(x - 1, y).hasEgg() == false)
          {
            //move to the left
            fishes[i].move(x - 1, y);
          }
        }
        //get the fish to the left
        int target = getFishIndex(x - 1, y);
        //if there is a fish to the left and the fish has attacks left
        if(target != -1 && fishes[i].attacksLeft() > 0)
        {
          //if the fish is not a cleaner shrimp
          if(fishes[i].species() != CLEANER_SHRIMP)
          {
            //if the fish to the left is an enemy fish
            if(fishes[target].owner() == enemyAI_ID)
            {
               //attack the fish
               fishes[i].attack(fishes[target]);
            }
          }
          else
          {
             //this is if the fish is a cleaner shrimp

             //if the fish to the left is a friendly fish
             if(fishes[target].owner() != enemyAI_ID)
             {
                //heal the fish
                fishes[i].attack(fishes[target]);
             }
          }
        }
      }
    }
  }
  return true;
}