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 "rs_graphic.h"
24 RS_Grid::RS_Grid(RS_GraphicView * gv): graphicView(gv), pt(NULL), number(0),
25 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
45 * Updates the grid point array.
47 void RS_Grid::update()
49 RS_DEBUG->print("RS_Grid::update");
51 if (!graphicView->isGridOn())
54 RS_Graphic * graphic = graphicView->getGraphic();
57 settings.beginGroup("Appearance");
58 bool scaleGrid = settings.value("ScaleGrid", true).toBool();
59 int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
66 userGrid = graphic->getVariableVector("$GRIDUNIT", Vector(-1.0, -1.0));
91 RS_DEBUG->print("RS_Grid::update: 001");
94 RS2::Unit unit = RS2::None;
95 RS2::LinearFormat format = RS2::Decimal;
99 unit = graphic->getUnit();
100 format = graphic->getLinearFormat();
104 Vector metaGridWidth;
106 RS_DEBUG->print("RS_Grid::update: 002");
108 // init grid spacing:
110 if (RS_Units::isMetric(unit) || unit == RS2::None
111 || format == RS2::Decimal || format == RS2::Engineering)
113 if (userGrid.x > 0.0)
114 gridWidth.x = userGrid.x;
116 gridWidth.x = 0.000001;
118 if (userGrid.y > 0.0)
119 gridWidth.y = userGrid.y;
121 gridWidth.y = 0.000001;
123 RS_DEBUG->print("RS_Grid::update: 003");
128 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
131 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
135 metaGridWidth.x = gridWidth.x * 10;
136 metaGridWidth.y = gridWidth.y * 10;
138 RS_DEBUG->print("RS_Grid::update: 004");
143 RS_DEBUG->print("RS_Grid::update: 005");
145 if (userGrid.x > 0.0)
146 gridWidth.x = userGrid.x;
148 gridWidth.x = 1.0 / 1024.0;
150 if (userGrid.y > 0.0)
151 gridWidth.y = userGrid.y;
153 gridWidth.y = 1.0 / 1024.0;
155 RS_DEBUG->print("RS_Grid::update: 006");
157 if (unit == RS2::Inch)
159 RS_DEBUG->print("RS_Grid::update: 007");
164 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
166 if (RS_Math::round(gridWidth.x) >= 36)
170 else if (RS_Math::round(gridWidth.x) >= 12)
174 else if (RS_Math::round(gridWidth.x) >= 4)
178 else if (RS_Math::round(gridWidth.x) >= 1)
188 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
190 if (RS_Math::round(gridWidth.y) >= 36)
194 else if (RS_Math::round(gridWidth.y) >= 12)
198 else if (RS_Math::round(gridWidth.y) >= 4)
202 else if (RS_Math::round(gridWidth.y) >= 1)
213 RS_DEBUG->print("RS_Grid::update: 008");
215 // metagrid X shows inches..
216 metaGridWidth.x = 1.0;
218 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
221 metaGridWidth.x = 12.0;
224 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
226 metaGridWidth.x = 36.0;
228 // .. or miles (not really..)
229 //if (graphicView->toGuiDX(metaGridWidth.x)<20) {
230 // metaGridWidth.x = 63360.0;
234 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
235 metaGridWidth.x = -1.0;
240 RS_DEBUG->print("RS_Grid::update: 009");
242 // metagrid Y shows inches..
243 metaGridWidth.y = 1.0;
245 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
248 metaGridWidth.y = 12.0;
251 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
253 metaGridWidth.y = 36.0;
255 // .. or miles (not really..)
256 //if (graphicView->toGuiDY(metaGridWidth.y)<20) {
257 // metaGridWidth.y = 63360.0;
261 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
263 metaGridWidth.y = -1.0;
269 RS_DEBUG->print("RS_Grid::update: 010");
273 RS_DEBUG->print("RS_Grid::update: 011");
277 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
282 metaGridWidth.x = -1.0;
284 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
289 metaGridWidth.y = -1.0;
291 RS_DEBUG->print("RS_Grid::update: 012");
293 //gridWidth.y = gridWidth.x;
294 //metaGridWidth.y = metaGridWidth.x;
297 RS_DEBUG->print("RS_Grid::update: 013");
300 spacing = gridWidth.x;
301 metaSpacing = metaGridWidth.x;
303 if (gridWidth.x > 1.0e-6 && gridWidth.y > 1.0e-6
304 && graphicView->toGuiDX(gridWidth.x) > 2
305 && graphicView->toGuiDY(gridWidth.y) > 2)
307 // find grid boundaries
308 double left = (int)(graphicView->toGraphX(0) / gridWidth.x)
310 double right = (int)(graphicView->toGraphX(graphicView->getWidth())
311 / gridWidth.x) * gridWidth.x;
312 double top = (int)(graphicView->toGraphY(0)
313 / gridWidth.y) * gridWidth.y;
315 (int)(graphicView->toGraphY(graphicView->getHeight())
316 / gridWidth.y) * gridWidth.y;
320 right += gridWidth.x;
322 bottom -= gridWidth.y;
324 // calculate number of visible grid points
325 int numberX = (RS_Math::round((right-left) / gridWidth.x) + 1);
326 int numberY = (RS_Math::round((top-bottom) / gridWidth.y) + 1);
327 number = numberX * numberY;
329 RS_DEBUG->print("RS_Grid::update: 014");
331 // create grid array:
332 if (number > 0 && number < 1000000)
334 pt = new Vector[number];
337 for(int y=0; y<numberY; ++y)
339 for(int x=0; x<numberX; ++x)
341 pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
351 RS_DEBUG->print("RS_Grid::update: 015");
354 // find meta grid boundaries
355 if (metaGridWidth.x > 1.0e-6 && metaGridWidth.y > 1.0e-6
356 && graphicView->toGuiDX(metaGridWidth.x) > 2
357 && graphicView->toGuiDY(metaGridWidth.y) > 2)
359 double mleft = (int)(graphicView->toGraphX(0)
360 / metaGridWidth.x) * metaGridWidth.x;
361 double mright = (int)(graphicView->toGraphX(graphicView->getWidth())
362 / metaGridWidth.x) * metaGridWidth.x;
363 double mtop = (int)(graphicView->toGraphY(0)
364 / metaGridWidth.y) * metaGridWidth.y;
365 double mbottom = (int)(graphicView->toGraphY(graphicView->getHeight())
366 / metaGridWidth.y) * metaGridWidth.y;
368 mleft -= metaGridWidth.x;
369 mright += metaGridWidth.x;
370 mtop += metaGridWidth.y;
371 mbottom -= metaGridWidth.y;
373 // calculate number of visible meta grid lines:
374 numMetaX = (RS_Math::round((mright - mleft) / metaGridWidth.x) + 1);
375 numMetaY = (RS_Math::round((mtop - mbottom) / metaGridWidth.y) + 1);
377 if (numMetaX > 0 && numMetaY > 0)
379 // create meta grid arrays:
380 metaX = new double[numMetaX];
381 metaY = new double[numMetaY];
385 for(int x=0; x<numMetaX; ++x)
386 metaX[i++] = mleft + x * metaGridWidth.x;
390 for(int y=0; y<numMetaY; ++y)
391 metaY[i++] = mbottom + y * metaGridWidth.y;
402 RS_DEBUG->print("RS_Grid::update: OK");
406 * @return Array of all visible grid points.
408 Vector * RS_Grid::getPoints()
414 * @return Number of visible grid points.
422 * @return Grid info for status widget.
424 QString RS_Grid::getInfo()
426 return QString("%1 / %2").arg(spacing).arg(metaSpacing);
430 * @return Meta grid positions in X.
432 double * RS_Grid::getMetaX()
438 * @return Number of visible meta grid lines in X.
440 int RS_Grid::countMetaX()
446 * @return Meta grid positions in Y.
448 double * RS_Grid::getMetaY()
454 * @return Number of visible meta grid lines in Y.
456 int RS_Grid::countMetaY()