// -----------------------------------------------------------------------------
// CTestEdwinSettingPage::DoLayoutRect
// -----------------------------------------------------------------------------
//
void CTestEdwinSettingPage::DoLayoutRect()
    {
    TPoint topLeftPoint( KZero, KZero );
    TPoint bottomRightPoint( KTen, KTen );
    TRect rect( topLeftPoint, bottomRightPoint );
    iEdwinLayoutRect.LayoutRect( rect, KZero, KZero, KZero, KTen, KTen, KTen, KTen );
    iHorizontalShadow.LayoutRect( rect, KZero, KZero, KZero, KTen, KTen, KTen, KTen );
    iVerticalShadow.LayoutRect( rect, KZero, KZero, KZero, KTen, KTen, KTen, KTen );
    iOutlineFrame.LayoutRect( rect, KZero, KZero, KZero, KTen, KTen, KTen, KTen );
    }
void SelectionRectangle::setRect(const QPointF &firstPoint,
                                 const QPointF &secondPoint)
{
    double firstX = std::floor(firstPoint.x()) + 0.5;
    double firstY = std::floor(firstPoint.y()) + 0.5;
    double secondX = std::floor(secondPoint.x()) + 0.5;
    double secondY = std::floor(secondPoint.y()) + 0.5;
    QPointF topLeftPoint(firstX < secondX ? firstX : secondX, firstY < secondY ? firstY : secondY);
    QPointF bottomRightPoint(firstX > secondX ? firstX : secondX, firstY > secondY ? firstY : secondY);

    QRectF rect(topLeftPoint, bottomRightPoint);
    m_controlShape->setRect(rect);
}
/**************************************************************************
* AEarthWindow :: paintWorld - paint a view of Earth from space           *
**************************************************************************/
bool AEarthWindow :: computeWorld()
{
  const float
    xRadiusFactor[4] = {1.45, 1.55, 1.60, 1.65},
    yRadiusFactor[4] = {(0.7 - 1.0/8), (0.7 - 1.0/16),
                        (0.7 - 1.0/32), (0.7 - 1.0/64)};
  const IRectangle
    psRect(rect());

/*------------------------------------------------------------------------|
| Construct the IGRect2D space to form the background of the scene.       |
-------------------------------------------------------------------------*/

  IGPoint2D
     bottomLeftPoint(0, 0),
     bottomRightPoint(psRect.width(), 0),
     topLeftPoint(0, psRect.height()),
     topRightPoint(psRect.width(), psRect.height());

  spaceDimensions.setPoint(0, bottomLeftPoint);
  spaceDimensions.setPoint(1, bottomRightPoint);
  spaceDimensions.setPoint(2, topRightPoint);
  spaceDimensions.setPoint(3, topLeftPoint);

/*------------------------------------------------------------------------|
| Compute the dimensions for the earthArcs.                               |
-------------------------------------------------------------------------*/

  IGPoint2D centerPoint(psRect.width() / 2, -psRect.height() * 0.2);
  int xRad, yRad;
  for(int i=0;i<=atmosphereLayers;i++)
     {
        yRad = yRadiusFactor[i] * psRect.height();
        xRad = xRadiusFactor[i] * yRad;
        IGRect2D arcBoundsRect(0, 2 * yRad, 2 * xRad, 0);
        arcDimensions[i].setBounds(arcBoundsRect);
        arcDimensions[i].setCenter(centerPoint);
     }


/*------------------------------------------------------------------------|
| Call computeStars() to compuet the dimensions for the stars.            |
| Then call paintWorld() that uses all these dimensions to create the     |
| objects to be painted.                                                  |
-------------------------------------------------------------------------*/
  computeStars();
  paintWorld();
  refresh();
  return true;

} /* end AEarthWindow :: paintWorld(..) */
/**
 * This is a quick fix for positioning with limited functionality. This will 
 * be followed by a more permanent fix soon.
 */
void PopupShadowNode::SetAnchorPosition(const winrt::Popup& popup)
{
  auto wkinstance = static_cast<PopupViewManager*>(GetViewManager())->m_wkReactInstance;
  auto instance = wkinstance.lock();

  if (instance == nullptr)
    return;

  // center relative to anchor
  if (m_targetTag > 0)
  {
    auto pNativeUIManagerHost = static_cast<NativeUIManager*>(instance->NativeUIManager())->getHost();
    ShadowNodeBase* pShadowNodeChild = static_cast<ShadowNodeBase*>(pNativeUIManagerHost->FindShadowNodeForTag(m_targetTag));

    if (pShadowNodeChild != nullptr)
    {
      auto targetView = pShadowNodeChild->GetView();
      auto targetElement = targetView.as<winrt::FrameworkElement>();

      auto popupTransform = targetElement.TransformToVisual(popup);
      winrt::Point bottomRightPoint(static_cast<float>(targetElement.Width()), static_cast<float>(targetElement.Height()));
      auto point = popupTransform.TransformPoint(bottomRightPoint);
      popup.HorizontalOffset(point.X + popup.HorizontalOffset());
      popup.VerticalOffset(point.Y + popup.VerticalOffset());
    }
  }
  else // Center relative to app window
  {
    auto appWindow = winrt::Window::Current().Content();
    auto popupToWindow = appWindow.TransformToVisual(popup);
    auto appWindowSize = GetAppWindowSize();
    winrt::Point centerPoint;
    centerPoint.X = static_cast<float>((appWindowSize.Width - popup.ActualWidth()) / 2);
    centerPoint.Y = static_cast<float>((appWindowSize.Height - popup.ActualHeight()) / 2);
    auto point = popupToWindow.TransformPoint(centerPoint);
    popup.HorizontalOffset(point.X);
    popup.VerticalOffset(point.Y);
  }
}
void ThumbnailView::Delegate::paintStackedIndicator( QPainter* painter, const QRect &pixmapRect, const QModelIndex& index ) const
{
    DB::ImageInfoPtr imageInfo = model()->imageAt(index.row()).info();
    if (!imageInfo || !imageInfo->isStacked())
        return;

    const QRect cellRect = widget()->visualRect( index );

    // Calculate the three points for the bottom most/right most lines
    int leftX = cellRect.left();
    int rightX = cellRect.right() + 5; // 5 for the 3D effect

    if ( isFirst( index.row() ) )
        leftX = pixmapRect.left() + pixmapRect.width()/2;

    if ( isLast( index.row() ) )
        rightX = pixmapRect.right();

    QPoint bottomLeftPoint( leftX, pixmapRect.bottom() );
    QPoint bottomRightPoint( rightX, pixmapRect.bottom() );
    QPoint topPoint = isLast( index.row() ) ? QPoint( rightX, pixmapRect.top() + pixmapRect.height()/2 ) : QPoint();

    // Paint the lines.
    painter->save();
    for ( int i=0; i < 8; ++i ) {
        painter->setPen( QPen(i % 2 == 0 ? Qt::black : Qt::white) );

        painter->drawLine(bottomLeftPoint,bottomRightPoint);
        if ( topPoint != QPoint() ) {
            painter->drawLine( bottomRightPoint, topPoint );
            topPoint -= QPoint(1,1);
        }

        bottomLeftPoint -= QPoint( isFirst( index.row()) ? 1 : 0, 1 );
        bottomRightPoint -= QPoint( isLast( index.row()) ? 1 : 0, 1);
    }
    painter->restore();
}