/***********************************************************************
**
**   lineelement.cpp
**
**   This file is part of KFLog.
**
************************************************************************
**
**   Copyright (c):  2000 by Heiner Lamprecht, Florian Ehinger
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: lineelement.cpp,v 1.16 2004/06/18 22:02:26 axel Exp $
**
***********************************************************************/
#include "targets.h"
#include <qcolor.h>
#include <qglobal.h>

#include "lineelement.h"

LineElement::LineElement(QString name, BaseMapElement::objectType t, QPointArray pA, bool isV, unsigned int secID)
  : BaseMapElement(name, t, secID),
    projPointArray(pA), bBox(pA.boundingRect()), valley(isV), closed(false)
{
  if(typeID == BaseMapElement::Lake || typeID == BaseMapElement::City ||
      typeID == BaseMapElement::Forest)
      closed = true;
}

LineElement::~LineElement()
{

}

void LineElement::drawMapElement(QPainter* targetP, QPainter* )
{
  //
  // If the element-type should not be drawn in the actual scale, or if the
  // element is not visible, return.
  //

  if(!glConfig->isBorder(typeID) || !__isVisible()) return;

  QPen drawP(glConfig->getDrawPen(typeID));

  QPointArray pA( glMapMatrix->map(projPointArray) );

  if(typeID == BaseMapElement::City)
    {
      //
      // We do not draw the outline of the city directly, because otherwise
      // we will get into trouble with cities lying at the edge of a
      // map-section. So we use a thicker draw a line into the mask-painter.
      //
      QBrush drawB( glConfig->getDrawBrush(typeID) );
      targetP->setPen(QPen(Qt::black, 1, Qt::SolidLine));
      targetP->setBrush(drawB);
      targetP->drawPolygon(pA);
      return;
    }

  targetP->setPen(drawP);

  if(closed)
    {
      //
      // Lakes do not have a brush, because they are devided into normal
      // sections and we do not want to see section-borders in a lake ...
      //
      if(typeID == BaseMapElement::Lake)
          targetP->setBrush(QBrush(drawP.color(), QBrush::SolidPattern));
      else
          targetP->setBrush(glConfig->getDrawBrush(typeID));
      targetP->drawPolygon(pA);
    }
  else
    {
      targetP->drawPolyline(pA);
      if(typeID == Highway && drawP.width() > 4)
        {
          // draw the white line in the middle
          targetP->setPen(QPen(Qt::white, 1));
          targetP->drawPolyline(pA);
        }
    }
}

bool LineElement::__isVisible() const
{
  return glMapMatrix->isVisible(bBox, getTypeID());
}

bool LineElement::isValley() const  {  return valley;  }
