]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Fix tick drawing logic
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 10 Oct 2014 15:32:20 +0000 (17:32 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 10 Oct 2014 15:32:20 +0000 (17:32 +0200)
signalcontroller.cpp
signaldrawer.cpp
signaldrawer.h

index 87913bc242630a787cc2c0ced957fe28ac7a01cc..da3e5c39b9ef220a3cb1607886fb626caf280e11 100644 (file)
@@ -192,11 +192,16 @@ RulerDrawData SignalController::getRulerDrawData(const double from, const double
   diffAbs = toAbs - fromAbs;
   step = 1 / pow(10, floor(log10(1 / (diffAbs))) + 1); // Magic - you want to love it but you better not touch!
   relStep = (diffRel * step) / (diffAbs);
-  //qDebug() << __QFUNC__ << "Step" << step << "RelStep" << relStep << "relDiff" << diffRel;
+  //qDebug() << __QFUNC__ << "Step" << step << "RelStep" << relStep << "relDiff" << diffRel << fromAbs;
 
   /* Calculate position of the first major tick */
-  firstTickAbs = ceil(fromAbs / step) * step;
-  firstTickRel = (diffRel / diffAbs) * (firstTickAbs - fromAbs) + from;
+  if (from == 0.0) {
+    firstTickAbs = fromAbs;
+    firstTickRel = from;
+  } else {
+    firstTickAbs = fromAbs + (step - fmod(fromAbs, step));
+    firstTickRel = from + (relStep - fmod(from, relStep));
+  }
   //qDebug() << __QFUNC__ << "First tick Abs:" << firstTickAbs << "Rel:" << firstTickRel;
 
   return RulerDrawData(firstTickAbs, firstTickRel, step, relStep);
index a68ee0cb27d08189f9ba92c7d6b002fbe78200b3..f74546a93131f9767fa782d690bc538a774d44ee 100644 (file)
@@ -229,10 +229,10 @@ bool SignalDrawer::drawPeaks(QPixmap* const target)
 void SignalDrawer::drawLeadingSubticks(const RulerDrawData& rd, std::function<void (const double, const double, const TickType)> drawFunc,
                                       const double relMin)
 {
-  double subRelMin = rd.firstTickRel;
-  double subAbsMin = rd.firstTickAbs;
-  while ((subRelMin -= rd.relStep / 10) >= relMin)
-    subAbsMin -= rd.step / 10;
+  const double subRelStep = subStep(rd.relStep);
+  const double subRelRem = fmod(relMin, subRelStep);
+  const double subRelMin = relMin + (subRelStep - subRelRem);
+  const double subAbsMin = (rd.firstTickRel - subRelMin) * (subStep(rd.step)) / subRelStep;
 
   drawScaleBySubticks(rd, drawFunc, subRelMin, subAbsMin, rd.firstTickRel);
 }
@@ -240,17 +240,17 @@ void SignalDrawer::drawLeadingSubticks(const RulerDrawData& rd, std::function<vo
 void SignalDrawer::drawScaleBySubticks(const RulerDrawData& rd, std::function<void (const double, const double, const TickType)> drawFunc, const double fromSubRel,
                                        const double fromAbsVal, const double toSubRel)
 {
-  const double subRelStep = rd.relStep / 10;
-  const double subAbsStep = rd.step / 10;
+  const double subRelStep = subStep(rd.relStep);
+  const double subAbsStep = subStep(rd.step);
   double subRel = fromSubRel;
   double absVal = fromAbsVal;
 
-  int ctr = 11 - ((toSubRel - fromSubRel) / subRelStep);
+  int ctr = 10 - ((toSubRel - fromSubRel) / subRelStep);
 
   while (subRel < toSubRel) {
+    drawFunc(subRel, absVal, (ctr++ == 5) ? TickType::TICK : TickType::SUBTICK);
     subRel += subRelStep;
     absVal += subAbsStep;
-    drawFunc(subRel, absVal, (ctr++ == 5) ? TickType::TICK : TickType::SUBTICK);
   }
 }
 
@@ -262,7 +262,7 @@ void SignalDrawer::drawScaleByTicks(const RulerDrawData& rd, std::function<void
 
     /* Draw subticks ticks */
     if (drawSubTicks)
-      drawScaleBySubticks(rd, drawFunc, rel, absVal, rel + rd.relStep);
+      drawScaleBySubticks(rd, drawFunc, rel + subStep(rd.relStep), absVal, rel + rd.relStep);
 
     absVal += rd.step;
   }
@@ -721,6 +721,11 @@ void SignalDrawer::setNewRelativeConstraints(const double fromX, const double fr
   }
 }
 
+double SignalDrawer::subStep(const double step)
+{
+  return step / 10;
+}
+
 SignalDrawer::~SignalDrawer()
 {
   delete m_pixmap;
index 77c3b3c77f2e170456f7e6e9f15e64c50db6fedf..a7eba19ee1a2359636c3d2283b1211782a5c102f 100644 (file)
@@ -111,6 +111,7 @@ private:
   double linesIntersection(const double k1, const double q1, const double k2, const double q2);
   void restoreRelativeConstraints();
   void setNewRelativeConstraints(const double fromX, const double fromY, const double toX, const double toY);
+  double subStep(const double step);
 
   int m_height;
   int m_width;