3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/21/2010 Added this text. :-)
18 #include "graphicview.h"
19 //#include "rs_graphicview.h"
24 #warning "!!! Clean out all references to RS_GraphicView here and in header !!!"
29 RS_Grid::RS_Grid(RS_GraphicView * gv): graphicView(gv), pt(NULL), number(0),
30 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
38 RS_Grid::RS_Grid(GraphicView * gv): graphicView(gv), pt(NULL), number(0),
39 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
59 * Updates the grid point array.
61 void RS_Grid::update()
63 RS_DEBUG->print("RS_Grid::update");
65 if (!graphicView->isGridOn())
68 Drawing * graphic = graphicView->getGraphic();
71 settings.beginGroup("Appearance");
72 bool scaleGrid = settings.value("ScaleGrid", true).toBool();
73 int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
80 userGrid = graphic->getVariableVector("$GRIDUNIT", Vector(-1.0, -1.0));
105 RS_DEBUG->print("RS_Grid::update: 001");
108 RS2::Unit unit = RS2::None;
109 RS2::LinearFormat format = RS2::Decimal;
113 unit = graphic->getUnit();
114 format = graphic->getLinearFormat();
118 Vector metaGridWidth;
120 RS_DEBUG->print("RS_Grid::update: 002");
122 // init grid spacing:
124 if (RS_Units::isMetric(unit) || unit == RS2::None
125 || format == RS2::Decimal || format == RS2::Engineering)
127 if (userGrid.x > 0.0)
128 gridWidth.x = userGrid.x;
130 gridWidth.x = 0.000001;
132 if (userGrid.y > 0.0)
133 gridWidth.y = userGrid.y;
135 gridWidth.y = 0.000001;
137 RS_DEBUG->print("RS_Grid::update: 003");
142 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
145 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
149 metaGridWidth.x = gridWidth.x * 10;
150 metaGridWidth.y = gridWidth.y * 10;
152 RS_DEBUG->print("RS_Grid::update: 004");
157 RS_DEBUG->print("RS_Grid::update: 005");
159 if (userGrid.x > 0.0)
160 gridWidth.x = userGrid.x;
162 gridWidth.x = 1.0 / 1024.0;
164 if (userGrid.y > 0.0)
165 gridWidth.y = userGrid.y;
167 gridWidth.y = 1.0 / 1024.0;
169 RS_DEBUG->print("RS_Grid::update: 006");
171 if (unit == RS2::Inch)
173 RS_DEBUG->print("RS_Grid::update: 007");
178 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
180 if (RS_Math::round(gridWidth.x) >= 36)
184 else if (RS_Math::round(gridWidth.x) >= 12)
188 else if (RS_Math::round(gridWidth.x) >= 4)
192 else if (RS_Math::round(gridWidth.x) >= 1)
202 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
204 if (RS_Math::round(gridWidth.y) >= 36)
208 else if (RS_Math::round(gridWidth.y) >= 12)
212 else if (RS_Math::round(gridWidth.y) >= 4)
216 else if (RS_Math::round(gridWidth.y) >= 1)
227 RS_DEBUG->print("RS_Grid::update: 008");
229 // metagrid X shows inches..
230 metaGridWidth.x = 1.0;
232 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
235 metaGridWidth.x = 12.0;
238 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
240 metaGridWidth.x = 36.0;
242 // .. or miles (not really..)
243 //if (graphicView->toGuiDX(metaGridWidth.x)<20) {
244 // metaGridWidth.x = 63360.0;
248 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
249 metaGridWidth.x = -1.0;
254 RS_DEBUG->print("RS_Grid::update: 009");
256 // metagrid Y shows inches..
257 metaGridWidth.y = 1.0;
259 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
262 metaGridWidth.y = 12.0;
265 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
267 metaGridWidth.y = 36.0;
269 // .. or miles (not really..)
270 //if (graphicView->toGuiDY(metaGridWidth.y)<20) {
271 // metaGridWidth.y = 63360.0;
275 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
277 metaGridWidth.y = -1.0;
283 RS_DEBUG->print("RS_Grid::update: 010");
287 RS_DEBUG->print("RS_Grid::update: 011");
291 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
296 metaGridWidth.x = -1.0;
298 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
303 metaGridWidth.y = -1.0;
305 RS_DEBUG->print("RS_Grid::update: 012");
307 //gridWidth.y = gridWidth.x;
308 //metaGridWidth.y = metaGridWidth.x;
311 RS_DEBUG->print("RS_Grid::update: 013");
314 spacing = gridWidth.x;
315 metaSpacing = metaGridWidth.x;
317 if (gridWidth.x > 1.0e-6 && gridWidth.y > 1.0e-6
318 && graphicView->toGuiDX(gridWidth.x) > 2
319 && graphicView->toGuiDY(gridWidth.y) > 2)
321 // find grid boundaries
322 double left = (int)(graphicView->toGraphX(0) / gridWidth.x)
324 double right = (int)(graphicView->toGraphX(graphicView->getWidth())
325 / gridWidth.x) * gridWidth.x;
326 double top = (int)(graphicView->toGraphY(0)
327 / gridWidth.y) * gridWidth.y;
329 (int)(graphicView->toGraphY(graphicView->getHeight())
330 / gridWidth.y) * gridWidth.y;
334 right += gridWidth.x;
336 bottom -= gridWidth.y;
338 // calculate number of visible grid points
339 int numberX = (RS_Math::round((right-left) / gridWidth.x) + 1);
340 int numberY = (RS_Math::round((top-bottom) / gridWidth.y) + 1);
341 number = numberX * numberY;
343 RS_DEBUG->print("RS_Grid::update: 014");
345 // create grid array:
346 if (number > 0 && number < 1000000)
348 pt = new Vector[number];
351 for(int y=0; y<numberY; ++y)
353 for(int x=0; x<numberX; ++x)
355 pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
365 RS_DEBUG->print("RS_Grid::update: 015");
368 // find meta grid boundaries
369 if (metaGridWidth.x > 1.0e-6 && metaGridWidth.y > 1.0e-6
370 && graphicView->toGuiDX(metaGridWidth.x) > 2
371 && graphicView->toGuiDY(metaGridWidth.y) > 2)
373 double mleft = (int)(graphicView->toGraphX(0)
374 / metaGridWidth.x) * metaGridWidth.x;
375 double mright = (int)(graphicView->toGraphX(graphicView->getWidth())
376 / metaGridWidth.x) * metaGridWidth.x;
377 double mtop = (int)(graphicView->toGraphY(0)
378 / metaGridWidth.y) * metaGridWidth.y;
379 double mbottom = (int)(graphicView->toGraphY(graphicView->getHeight())
380 / metaGridWidth.y) * metaGridWidth.y;
382 mleft -= metaGridWidth.x;
383 mright += metaGridWidth.x;
384 mtop += metaGridWidth.y;
385 mbottom -= metaGridWidth.y;
387 // calculate number of visible meta grid lines:
388 numMetaX = (RS_Math::round((mright - mleft) / metaGridWidth.x) + 1);
389 numMetaY = (RS_Math::round((mtop - mbottom) / metaGridWidth.y) + 1);
391 if (numMetaX > 0 && numMetaY > 0)
393 // create meta grid arrays:
394 metaX = new double[numMetaX];
395 metaY = new double[numMetaY];
399 for(int x=0; x<numMetaX; ++x)
400 metaX[i++] = mleft + x * metaGridWidth.x;
404 for(int y=0; y<numMetaY; ++y)
405 metaY[i++] = mbottom + y * metaGridWidth.y;
416 RS_DEBUG->print("RS_Grid::update: OK");
420 * @return Array of all visible grid points.
422 Vector * RS_Grid::getPoints()
428 * @return Number of visible grid points.
436 * @return Grid info for status widget.
438 QString RS_Grid::getInfo()
440 return QString("%1 / %2").arg(spacing).arg(metaSpacing);
444 * @return Meta grid positions in X.
446 double * RS_Grid::getMetaX()
452 * @return Number of visible meta grid lines in X.
454 int RS_Grid::countMetaX()
460 * @return Meta grid positions in Y.
462 double * RS_Grid::getMetaY()
468 * @return Number of visible meta grid lines in Y.
470 int RS_Grid::countMetaY()