1 /******************************************************************************************************
2  * (C) 2019 markummitchell@github.com. This file is part of Engauge Digitizer, which is released      *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission.     *
5  ******************************************************************************************************/
6 
7 #include "CallbackGatherXThetasInGridLines.h"
8 #include "Document.h"
9 #include "EngaugeAssert.h"
10 #include "ExportAlignLinear.h"
11 #include "ExportAlignLog.h"
12 #include "ExportLayoutFunctions.h"
13 #include "ExportPointsSelectionFunctions.h"
14 #include "GridLineLimiter.h"
15 #include "Logger.h"
16 #include "MainWindowModel.h"
17 #include "Point.h"
18 #include <qmath.h>
19 
20 const bool NOT_FIRST_CURVE_ONLY = false;
21 
CallbackGatherXThetasInGridLines(const MainWindowModel & modelMainWindow,const DocumentModelExportFormat & modelExport,const QStringList & curvesIncluded,const Transformation & transformation,const Document & document)22 CallbackGatherXThetasInGridLines::CallbackGatherXThetasInGridLines(const MainWindowModel &modelMainWindow,
23                                                                    const DocumentModelExportFormat &modelExport,
24                                                                    const QStringList &curvesIncluded,
25                                                                    const Transformation &transformation,
26                                                                    const Document &document) :
27   CallbackGatherXThetasAbstractBase (NOT_FIRST_CURVE_ONLY,
28                                      modelExport.extrapolateOutsideEndpoints (),
29                                      curvesIncluded,
30                                      transformation)
31 {
32   addGridLines (modelMainWindow,
33                 transformation,
34                 document);
35 }
36 
addGridLines(const MainWindowModel & modelMainWindow,const Transformation & transformation,const Document & document)37 void CallbackGatherXThetasInGridLines::addGridLines (const MainWindowModel &modelMainWindow,
38                                                      const Transformation &transformation,
39                                                      const Document &document)
40 {
41   DocumentModelGridDisplay gridLines = document.modelGridDisplay();
42 
43   // Prevent overflow
44   GridLineLimiter gridLineLimiter;
45   double startX = document.modelGridDisplay().startX();
46   double stepX = document.modelGridDisplay().stepX();
47   double stopX = document.modelGridDisplay().stopX();
48   gridLineLimiter.limitForXTheta (document,
49                                   transformation,
50                                   document.modelCoords(),
51                                   modelMainWindow,
52                                   document.modelGridDisplay(),
53                                   startX,
54                                   stepX,
55                                   stopX);
56   if (document.modelCoords().coordScaleXTheta() == COORD_SCALE_LINEAR) {
57     // Linear
58     int countX = qFloor (0.5 + 1 + (stopX - startX) / stepX);
59     for (int i = 0; i < countX; i++) {
60       double x = startX + i * stepX;
61       addGraphX (x);
62     }
63   } else {
64     // Log
65     int countX = qFloor (1.0 + (qLn (stopX) - qLn (startX)) / qLn (stepX));
66     for (int i = 0; i < countX; i++) {
67       double x = startX * qPow (stepX, i);
68       addGraphX (x);
69     }
70   }
71 }
72 
callback(const QString & curveName,const Point & point)73 CallbackSearchReturn CallbackGatherXThetasInGridLines::callback (const QString &curveName,
74                                                                  const Point &point)
75 {
76   LOG4CPP_DEBUG_S ((*mainCat)) << "CallbackGatherXThetasInGridLines::callback"
77                                << " curveName=" << curveName.toLatin1().data()
78                                << " point=" << point.identifier().toLatin1().data();
79 
80   updateMinMax (curveName,
81                 point);
82 
83   return CALLBACK_SEARCH_RETURN_CONTINUE;
84 }
85