bool RangeProfilePlotManager::mouseMoveEvent(PlotView* pView, QMouseEvent* pEvent)
{
   bool rval = false;
   if (!mMouseStart.isNull())
   {
      double dataX, startY, curY;
      pView->translateScreenToData(0.0, mMouseStart.y(), dataX, startY);
      pView->translateScreenToData(0.0, pEvent->y(), dataX, curY);
      double shift = curY - startY;

      std::list<PlotObject*> selected;
      pView->getSelectedObjects(selected, true);
      for (std::list<PlotObject*>::iterator obj = selected.begin(); obj != selected.end(); ++obj)
      {
         PointSet* pSet = dynamic_cast<PointSet*>(*obj);
         if (pSet != NULL)
         {
            rval = true;
            std::vector<Point*> points = pSet->getPoints();
            for (std::vector<Point*>::iterator point = points.begin(); point != points.end(); ++point)
            {
               LocationType loc = (*point)->getLocation();
               loc.mY -= shift;
               (*point)->setLocation(loc);
            }
         }
      }
      mMouseStart = pEvent->pos();
      pView->refresh();
   }
   return rval;
}
bool RangeProfilePlotManager::wheelEvent(PlotView* pView, QWheelEvent* pEvent)
{
   double scaleAdjust = pEvent->delta() / 1440.0; // yup, a magic number, started with one full rotation to double
                                                  // then experimented until I got something that looked good
   scaleAdjust = (scaleAdjust < 0.0) ? (-1.0 - scaleAdjust) : (1.0 - scaleAdjust);
   std::list<PlotObject*> selected;
   pView->getSelectedObjects(selected, true);
   for (std::list<PlotObject*>::iterator obj = selected.begin(); obj != selected.end(); ++obj)
   {
      PointSet* pSet = dynamic_cast<PointSet*>(*obj);
      if (pSet != NULL)
      {
         double minX, minY, maxX, maxY;
         pSet->getExtents(minX, minY, maxX, maxY);
         double shift = minY;
         std::vector<Point*> points = pSet->getPoints();
         for (std::vector<Point*>::iterator point = points.begin(); point != points.end(); ++point)
         {
            LocationType loc = (*point)->getLocation();
            loc.mY -= shift;
            if (scaleAdjust < 0.0)
            {
               loc.mY *= fabs(scaleAdjust);
            }
            else
            {
               loc.mY /= scaleAdjust;
            }
            loc.mY += shift;
            (*point)->setLocation(loc);
         }
      }
   }
   pView->refresh();
   return false;
}
void RangeProfilePlotManager::calculateDifferences()
{
   // ensure we have only two objects selected
   VERIFYNRV(mpView);
   std::list<PlotObject*> selected;
   mpView->getSelectedObjects(selected, true);
   std::list<PlotObject*>::iterator selIt = selected.begin();
   PointSet* pFirst = (selIt == selected.end()) ? NULL : dynamic_cast<PointSet*>(*(selIt++));
   PointSet* pSecond = (selIt == selected.end()) ? NULL : dynamic_cast<PointSet*>(*(selIt++));
   if (pFirst == NULL || pSecond == NULL)
   {
      return;
   }

   // locate the Difference point set
   std::list<PlotObject*> allObjects;
   mpView->getObjects(POINT_SET, allObjects);
   PointSet* pDiffSet = NULL;
   for (std::list<PlotObject*>::iterator obj = allObjects.begin(); obj != allObjects.end(); ++obj)
   {
      PointSet* pSet = static_cast<PointSet*>(*obj);
      std::string name;
      pSet->getObjectName(name);
      if (name == "Difference")
      {
         pDiffSet = pSet;
         pDiffSet->clear(true);
         break;
      }
   }
   if (pDiffSet == NULL)
   {
      pDiffSet = static_cast<PointSet*>(mpView->addObject(POINT_SET, true));
      pDiffSet->setObjectName("Difference");
   }

   // calculate the differences and errors
   std::vector<Point*> aPoints = pFirst->getPoints();
   std::vector<Point*> bPoints = pSecond->getPoints();
   if (aPoints.size() < 2 || bPoints.size() < 2)
   {
      return;
   }
   double mae = 0.0;
   double mse1 = 0.0;
   double mse2 = 0.0;

   for (size_t aIdx = 0; aIdx < aPoints.size(); ++aIdx)
   {
      Point* pA = aPoints[aIdx];
      VERIFYNRV(pA);
      LocationType aVal = pA->getLocation();
      LocationType newVal;

      // locate the associated spot in b
      for (size_t bIdx = 0; bIdx < bPoints.size(); ++bIdx)
      {
         Point* pB = bPoints[bIdx];
         VERIFYNRV(pB);
         LocationType bVal = pB->getLocation();
         double diff = aVal.mX - bVal.mX;
         if (fabs(diff) < 0.0000001) // a == b   use the exact value
         {
            newVal.mX = aVal.mX;
            newVal.mY = bVal.mY - aVal.mY;
            break;
         }
         else if (diff < 0.0) // a < b   found the upper point, interpolate
         {
            newVal.mX = aVal.mX;
            LocationType secondBVal;
            if (bIdx == 0) // we are at the start so continue the segment from the right
            {
               Point* pSecondB = bPoints[1];
               VERIFYNRV(pSecondB);
               secondBVal = pSecondB->getLocation();
            }
            else // grab the previous point for interpolation
            {
               Point* pSecondB = bPoints[bIdx-1];
               VERIFYNRV(pSecondB);
               secondBVal = pSecondB->getLocation();
            }

            // calculate slope-intercept
            double m = (bVal.mY - secondBVal.mY) / (bVal.mX - secondBVal.mX);
            double b = bVal.mY - m * bVal.mX;

            // find the y corresponding to the interpolated x
            newVal.mY = m * newVal.mX + b;
            newVal.mY -= aVal.mY;
            break;
         }
      }
      mae += fabs(newVal.mY);
      mse1 += newVal.mY * newVal.mY;
      mse2 += (newVal.mY * newVal.mY) / (aVal.mY * aVal.mY);
      pDiffSet->addPoint(newVal.mX, newVal.mY);
   }
   pDiffSet->setLineColor(ColorType(200, 0, 0));
   mpView->refresh();
   mae /= aPoints.size();
   mse1 /= aPoints.size();
   mse2 /= aPoints.size();
   QMessageBox::information(mpView->getWidget(), "Comparison metrics",
      QString("Mean squared error (method 1): %1\n"
              "Mean squared error (method 2): %2\n"
              "Mean absolute error:           %3").arg(mse1).arg(mse2).arg(mae), QMessageBox::Close);
}