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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/21/2010 Added this text. :-)
20 #include "graphicview.h"
21 //#include "rs_graphicview.h"
26 #warning "!!! Clean out all references to RS_GraphicView here and in header !!!"
31 RS_Grid::RS_Grid(RS_GraphicView * gv): graphicView(gv), pt(NULL), number(0),
32 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
40 RS_Grid::RS_Grid(GraphicView * gv): graphicView(gv), pt(NULL), number(0),
41 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
61 * Updates the grid point array.
63 void RS_Grid::update()
65 RS_DEBUG->print("RS_Grid::update");
67 if (!graphicView->isGridOn())
70 Drawing * graphic = graphicView->getGraphic();
73 settings.beginGroup("Appearance");
74 bool scaleGrid = settings.value("ScaleGrid", true).toBool();
75 int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
82 userGrid = graphic->getVariableVector("$GRIDUNIT", Vector(-1.0, -1.0));
107 RS_DEBUG->print("RS_Grid::update: 001");
110 RS2::Unit unit = RS2::None;
111 RS2::LinearFormat format = RS2::Decimal;
115 unit = graphic->getUnit();
116 format = graphic->getLinearFormat();
120 Vector metaGridWidth;
122 RS_DEBUG->print("RS_Grid::update: 002");
124 // init grid spacing:
126 if (RS_Units::isMetric(unit) || unit == RS2::None
127 || format == RS2::Decimal || format == RS2::Engineering)
129 if (userGrid.x > 0.0)
130 gridWidth.x = userGrid.x;
132 gridWidth.x = 0.000001;
134 if (userGrid.y > 0.0)
135 gridWidth.y = userGrid.y;
137 gridWidth.y = 0.000001;
139 RS_DEBUG->print("RS_Grid::update: 003");
144 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
147 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
151 metaGridWidth.x = gridWidth.x * 10;
152 metaGridWidth.y = gridWidth.y * 10;
154 RS_DEBUG->print("RS_Grid::update: 004");
159 RS_DEBUG->print("RS_Grid::update: 005");
161 if (userGrid.x > 0.0)
162 gridWidth.x = userGrid.x;
164 gridWidth.x = 1.0 / 1024.0;
166 if (userGrid.y > 0.0)
167 gridWidth.y = userGrid.y;
169 gridWidth.y = 1.0 / 1024.0;
171 RS_DEBUG->print("RS_Grid::update: 006");
173 if (unit == RS2::Inch)
175 RS_DEBUG->print("RS_Grid::update: 007");
180 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
182 if (RS_Math::round(gridWidth.x) >= 36)
186 else if (RS_Math::round(gridWidth.x) >= 12)
190 else if (RS_Math::round(gridWidth.x) >= 4)
194 else if (RS_Math::round(gridWidth.x) >= 1)
204 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
206 if (RS_Math::round(gridWidth.y) >= 36)
210 else if (RS_Math::round(gridWidth.y) >= 12)
214 else if (RS_Math::round(gridWidth.y) >= 4)
218 else if (RS_Math::round(gridWidth.y) >= 1)
229 RS_DEBUG->print("RS_Grid::update: 008");
231 // metagrid X shows inches..
232 metaGridWidth.x = 1.0;
234 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
237 metaGridWidth.x = 12.0;
240 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
242 metaGridWidth.x = 36.0;
244 // .. or miles (not really..)
245 //if (graphicView->toGuiDX(metaGridWidth.x)<20) {
246 // metaGridWidth.x = 63360.0;
250 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
251 metaGridWidth.x = -1.0;
256 RS_DEBUG->print("RS_Grid::update: 009");
258 // metagrid Y shows inches..
259 metaGridWidth.y = 1.0;
261 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
264 metaGridWidth.y = 12.0;
267 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
269 metaGridWidth.y = 36.0;
271 // .. or miles (not really..)
272 //if (graphicView->toGuiDY(metaGridWidth.y)<20) {
273 // metaGridWidth.y = 63360.0;
277 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
279 metaGridWidth.y = -1.0;
285 RS_DEBUG->print("RS_Grid::update: 010");
289 RS_DEBUG->print("RS_Grid::update: 011");
293 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
298 metaGridWidth.x = -1.0;
300 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
305 metaGridWidth.y = -1.0;
307 RS_DEBUG->print("RS_Grid::update: 012");
309 //gridWidth.y = gridWidth.x;
310 //metaGridWidth.y = metaGridWidth.x;
313 RS_DEBUG->print("RS_Grid::update: 013");
316 spacing = gridWidth.x;
317 metaSpacing = metaGridWidth.x;
319 if (gridWidth.x > 1.0e-6 && gridWidth.y > 1.0e-6
320 && graphicView->toGuiDX(gridWidth.x) > 2
321 && graphicView->toGuiDY(gridWidth.y) > 2)
323 // find grid boundaries
324 double left = (int)(graphicView->toGraphX(0) / gridWidth.x)
326 double right = (int)(graphicView->toGraphX(graphicView->getWidth())
327 / gridWidth.x) * gridWidth.x;
328 double top = (int)(graphicView->toGraphY(0)
329 / gridWidth.y) * gridWidth.y;
331 (int)(graphicView->toGraphY(graphicView->getHeight())
332 / gridWidth.y) * gridWidth.y;
336 right += gridWidth.x;
338 bottom -= gridWidth.y;
340 // calculate number of visible grid points
341 int numberX = (RS_Math::round((right-left) / gridWidth.x) + 1);
342 int numberY = (RS_Math::round((top-bottom) / gridWidth.y) + 1);
343 number = numberX * numberY;
345 RS_DEBUG->print("RS_Grid::update: 014");
347 // create grid array:
348 if (number > 0 && number < 1000000)
350 pt = new Vector[number];
353 for(int y=0; y<numberY; ++y)
355 for(int x=0; x<numberX; ++x)
357 pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
367 RS_DEBUG->print("RS_Grid::update: 015");
370 // find meta grid boundaries
371 if (metaGridWidth.x > 1.0e-6 && metaGridWidth.y > 1.0e-6
372 && graphicView->toGuiDX(metaGridWidth.x) > 2
373 && graphicView->toGuiDY(metaGridWidth.y) > 2)
375 double mleft = (int)(graphicView->toGraphX(0)
376 / metaGridWidth.x) * metaGridWidth.x;
377 double mright = (int)(graphicView->toGraphX(graphicView->getWidth())
378 / metaGridWidth.x) * metaGridWidth.x;
379 double mtop = (int)(graphicView->toGraphY(0)
380 / metaGridWidth.y) * metaGridWidth.y;
381 double mbottom = (int)(graphicView->toGraphY(graphicView->getHeight())
382 / metaGridWidth.y) * metaGridWidth.y;
384 mleft -= metaGridWidth.x;
385 mright += metaGridWidth.x;
386 mtop += metaGridWidth.y;
387 mbottom -= metaGridWidth.y;
389 // calculate number of visible meta grid lines:
390 numMetaX = (RS_Math::round((mright - mleft) / metaGridWidth.x) + 1);
391 numMetaY = (RS_Math::round((mtop - mbottom) / metaGridWidth.y) + 1);
393 if (numMetaX > 0 && numMetaY > 0)
395 // create meta grid arrays:
396 metaX = new double[numMetaX];
397 metaY = new double[numMetaY];
401 for(int x=0; x<numMetaX; ++x)
402 metaX[i++] = mleft + x * metaGridWidth.x;
406 for(int y=0; y<numMetaY; ++y)
407 metaY[i++] = mbottom + y * metaGridWidth.y;
418 RS_DEBUG->print("RS_Grid::update: OK");
422 * @return Array of all visible grid points.
424 Vector * RS_Grid::getPoints()
430 * @return Number of visible grid points.
438 * @return Grid info for status widget.
440 QString RS_Grid::getInfo()
442 return QString("%1 / %2").arg(spacing).arg(metaSpacing);
446 * @return Meta grid positions in X.
448 double * RS_Grid::getMetaX()
454 * @return Number of visible meta grid lines in X.
456 int RS_Grid::countMetaX()
462 * @return Meta grid positions in Y.
464 double * RS_Grid::getMetaY()
470 * @return Number of visible meta grid lines in Y.
472 int RS_Grid::countMetaY()