Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
GridLineLimiter.cpp
1 /******************************************************************************************************
2  * (C) 2016 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 "CallbackBoundingRects.h"
8 #include "Document.h"
9 #include "DocumentModelCoords.h"
10 #include "DocumentModelGridDisplay.h"
11 #include "DocumentModelGridRemoval.h"
12 #include "GridLineLimiter.h"
13 #include "MainWindowModel.h"
14 #include <qmath.h>
15 #include "Transformation.h"
16 
17 const int DEFAULT_MAXIMUM_GRID_LINES = 100;
18 
20 {
21 }
22 
23 void GridLineLimiter::documentBounds (const Document &document,
24  const Transformation &transformation,
25  QPointF &boundingRectMin,
26  QPointF &boundingRectMax) const
27 {
28  // Get graph coordinate bounds
30  transformation);
31 
32  Functor2wRet<const QString &, const Point &, CallbackSearchReturn> ftorWithCallback = functor_ret (ftor,
34  document.iterateThroughCurvePointsAxes (ftorWithCallback);
35  document.iterateThroughCurvesPointsGraphs (ftorWithCallback);
36 
37  bool isEmpty;
38  boundingRectMin = ftor.boundingRectGraphMin (isEmpty);
39  boundingRectMax = ftor.boundingRectGraphMax (isEmpty);
40 }
41 
43  const Transformation &transformation,
44  const DocumentModelCoords &modelCoords,
45  const MainWindowModel &modelMainWindow,
46  const DocumentModelGridDisplay &modelGrid,
47  double &startX,
48  double &stepX,
49  double &stopX) const
50 {
51  startX = modelGrid.startX();
52  stopX = modelGrid.stopX();
53  stepX = modelGrid.stepX();
54  int countX = signed (modelGrid.countX());
55 
56  bool needReduction = (countX > modelMainWindow.maximumGridLines());
57 
58  if (modelCoords.coordScaleXTheta() == COORD_SCALE_LINEAR) {
59 
60  // Linear
61  if (!needReduction) {
62  if (stepX <= 0) {
63  stepX = 0;
64  needReduction = true;
65  } else {
66  countX = qFloor (1.0 + (stopX - startX) / stepX);
67  needReduction = (countX > modelMainWindow.maximumGridLines());
68  }
69  }
70 
71  if (needReduction) {
72  stopX = startX + stepX * (modelMainWindow.maximumGridLines() - 1);
73  }
74 
75  } else {
76 
77  // Log
78  if (startX <= 0) {
79 
80  // Start value is invalid so override both start and step
81  QPointF boundingRectGraphMin, boundingRectGraphMax;
82  documentBounds (document,
83  transformation,
84  boundingRectGraphMin,
85  boundingRectGraphMax);
86 
87  // Override lower bound
88  startX = boundingRectGraphMin.x ();
89  }
90 
91  if (!needReduction) {
92  if (stepX <= 1) {
93  stepX = 1;
94  needReduction = true;
95  } else {
96  countX = qFloor (1.0 + (qLn (stopX) - qLn (startX)) / qLn (stepX));
97  needReduction = (countX > modelMainWindow.maximumGridLines());
98  }
99  }
100 
101  if (needReduction) {
102  stopX = qExp (qLn (startX) + qLn (stepX) * (modelMainWindow.maximumGridLines() - 1));
103  }
104  }
105 }
106 
108  const Transformation &transformation,
109  const DocumentModelCoords &modelCoords,
110  const MainWindowModel &modelMainWindow,
111  const DocumentModelGridDisplay &modelGrid,
112  double &startY,
113  double &stepY,
114  double &stopY) const
115 {
116  startY = modelGrid.startY();
117  stopY = modelGrid.stopY();
118  stepY = modelGrid.stepY();
119  int countY = signed (modelGrid.countY());
120 
121  bool needReduction = (countY > modelMainWindow.maximumGridLines());
122 
123  if (modelCoords.coordScaleYRadius() == COORD_SCALE_LINEAR) {
124 
125  // Linear
126  if (!needReduction) {
127  if (stepY <= 0) {
128  stepY = 0;
129  needReduction = true;
130  } else {
131  countY = qFloor (1.0 + (stopY - startY) / stepY);
132  needReduction = (countY > modelMainWindow.maximumGridLines());
133  }
134  }
135 
136  if (needReduction) {
137  stopY = startY + stepY * (modelMainWindow.maximumGridLines() - 1);
138  }
139 
140  } else {
141 
142  // Log
143  if (startY <= 0) {
144 
145  // Start value is invalid so override both start and step
146  QPointF boundingRectGraphMin, boundingRectGraphMax;
147  documentBounds (document,
148  transformation,
149  boundingRectGraphMin,
150  boundingRectGraphMax);
151 
152  // Override lower bound
153  startY = boundingRectGraphMin.y ();
154  }
155 
156  if (!needReduction) {
157  if (stepY <= 1) {
158  stepY = 1;
159  needReduction = true;
160  } else {
161  countY = qFloor (1.0 + (qLn (stopY) - qLn (startY)) / qLn (stepY));
162  needReduction = (countY > modelMainWindow.maximumGridLines());
163  }
164  }
165 
166  if (needReduction) {
167  stopY = qExp (qLn (startY) + qLn (stepY) * (modelMainWindow.maximumGridLines() - 1));
168  }
169  }
170 }
double stopX() const
Get method for x grid line upper bound (inclusive).
void limitForXTheta(const Document &document, const Transformation &transformation, const DocumentModelCoords &modelCoords, const MainWindowModel &modelMainWindow, const DocumentModelGridDisplay &modelGrid, double &startX, double &stepX, double &stopX) const
Limit step value for x/theta coordinate. This is a noop if the maximum grid line limit in MainWindowM...
DocumentAxesPointsRequired documentAxesPointsRequired() const
Get method for DocumentAxesPointsRequired.
Definition: Document.cpp:361
CallbackSearchReturn callback(const QString &curveName, const Point &point)
Callback method.
double stepX() const
Get method for x grid line increment.
void limitForYRadius(const Document &document, const Transformation &transformation, const DocumentModelCoords &modelCoords, const MainWindowModel &modelMainWindow, const DocumentModelGridDisplay &modelGrid, double &startY, double &stepY, double &stopY) const
Limit step value for y/range coordinate. This is a noop if the maximum grid line limit in MainWindowM...
Model for DlgSettingsGridDisplay and CmdSettingsGridDisplay.
CoordScale coordScaleYRadius() const
Get method for linear/log scale on y/radius.
void iterateThroughCurvePointsAxes(const Functor2wRet< const QString &, const Point &, CallbackSearchReturn > &ftorWithCallback)
See Curve::iterateThroughCurvePoints, for the axes curve.
Definition: Document.cpp:447
double startX() const
Get method for x grid line lower bound (inclusive).
int maximumGridLines() const
Maximum number of grid lines.
Affine transformation between screen and graph coordinates, based on digitized axis points...
GridLineLimiter()
Single constructor.
CoordScale coordScaleXTheta() const
Get method for linear/log scale on x/theta.
Model for DlgSettingsMainWindow.
Model for DlgSettingsCoords and CmdSettingsCoords.
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
double stopY() const
Get method for y grid line upper bound (inclusive).
double startY() const
Get method for y grid line lower bound (inclusive).
double stepY() const
Get method for y grid line increment.
void iterateThroughCurvesPointsGraphs(const Functor2wRet< const QString &, const Point &, CallbackSearchReturn > &ftorWithCallback)
See Curve::iterateThroughCurvePoints, for all the graphs curves.
Definition: Document.cpp:470
Callback for computing the bounding rectangles of the screen and graph coordinates of the points in t...
unsigned int countX() const
Get method for x grid line count.
unsigned int countY() const
Get method for y grid line count.