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"
25 #warning "!!! Clean out all references to RS_GraphicView here and in header !!!"
29 Grid::Grid(GraphicView * gv): graphicView(gv), pt(NULL), number(0),
30 metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
50 * Updates the grid point array.
54 DEBUG->print("Grid::update");
56 if (!graphicView->isGridOn())
59 Drawing * graphic = graphicView->getGraphic();
62 settings.beginGroup("Appearance");
63 bool scaleGrid = settings.value("ScaleGrid", true).toBool();
64 int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
71 userGrid = graphic->getVariableVector("$GRIDUNIT", Vector(-1.0, -1.0));
96 DEBUG->print("Grid::update: 001");
99 RS2::Unit unit = RS2::None;
100 RS2::LinearFormat format = RS2::Decimal;
104 unit = graphic->getUnit();
105 format = graphic->getLinearFormat();
109 Vector metaGridWidth;
111 DEBUG->print("Grid::update: 002");
113 // init grid spacing:
115 if (Units::isMetric(unit) || unit == RS2::None
116 || format == RS2::Decimal || format == RS2::Engineering)
118 if (userGrid.x > 0.0)
119 gridWidth.x = userGrid.x;
121 gridWidth.x = 0.000001;
123 if (userGrid.y > 0.0)
124 gridWidth.y = userGrid.y;
126 gridWidth.y = 0.000001;
128 DEBUG->print("Grid::update: 003");
133 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
136 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
140 metaGridWidth.x = gridWidth.x * 10;
141 metaGridWidth.y = gridWidth.y * 10;
143 DEBUG->print("Grid::update: 004");
148 DEBUG->print("Grid::update: 005");
150 if (userGrid.x > 0.0)
151 gridWidth.x = userGrid.x;
153 gridWidth.x = 1.0 / 1024.0;
155 if (userGrid.y > 0.0)
156 gridWidth.y = userGrid.y;
158 gridWidth.y = 1.0 / 1024.0;
160 DEBUG->print("Grid::update: 006");
162 if (unit == RS2::Inch)
164 DEBUG->print("Grid::update: 007");
169 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
171 if (Math::round(gridWidth.x) >= 36)
175 else if (Math::round(gridWidth.x) >= 12)
179 else if (Math::round(gridWidth.x) >= 4)
183 else if (Math::round(gridWidth.x) >= 1)
193 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
195 if (Math::round(gridWidth.y) >= 36)
199 else if (Math::round(gridWidth.y) >= 12)
203 else if (Math::round(gridWidth.y) >= 4)
207 else if (Math::round(gridWidth.y) >= 1)
218 DEBUG->print("Grid::update: 008");
220 // metagrid X shows inches..
221 metaGridWidth.x = 1.0;
223 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
226 metaGridWidth.x = 12.0;
229 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
231 metaGridWidth.x = 36.0;
233 // .. or miles (not really..)
234 //if (graphicView->toGuiDX(metaGridWidth.x)<20) {
235 // metaGridWidth.x = 63360.0;
239 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
240 metaGridWidth.x = -1.0;
245 DEBUG->print("Grid::update: 009");
247 // metagrid Y shows inches..
248 metaGridWidth.y = 1.0;
250 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
253 metaGridWidth.y = 12.0;
256 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
258 metaGridWidth.y = 36.0;
260 // .. or miles (not really..)
261 //if (graphicView->toGuiDY(metaGridWidth.y)<20) {
262 // metaGridWidth.y = 63360.0;
266 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
268 metaGridWidth.y = -1.0;
274 DEBUG->print("Grid::update: 010");
278 DEBUG->print("Grid::update: 011");
282 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
287 metaGridWidth.x = -1.0;
289 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
294 metaGridWidth.y = -1.0;
296 DEBUG->print("Grid::update: 012");
298 //gridWidth.y = gridWidth.x;
299 //metaGridWidth.y = metaGridWidth.x;
302 DEBUG->print("Grid::update: 013");
305 spacing = gridWidth.x;
306 metaSpacing = metaGridWidth.x;
308 if (gridWidth.x > 1.0e-6 && gridWidth.y > 1.0e-6
309 && graphicView->toGuiDX(gridWidth.x) > 2
310 && graphicView->toGuiDY(gridWidth.y) > 2)
312 // find grid boundaries
313 double left = (int)(graphicView->toGraphX(0) / gridWidth.x)
315 double right = (int)(graphicView->toGraphX(graphicView->getWidth())
316 / gridWidth.x) * gridWidth.x;
317 double top = (int)(graphicView->toGraphY(0)
318 / gridWidth.y) * gridWidth.y;
320 (int)(graphicView->toGraphY(graphicView->getHeight())
321 / gridWidth.y) * gridWidth.y;
325 right += gridWidth.x;
327 bottom -= gridWidth.y;
329 // calculate number of visible grid points
330 int numberX = (Math::round((right-left) / gridWidth.x) + 1);
331 int numberY = (Math::round((top-bottom) / gridWidth.y) + 1);
332 number = numberX * numberY;
334 DEBUG->print("Grid::update: 014");
336 // create grid array:
337 if (number > 0 && number < 1000000)
339 pt = new Vector[number];
342 for(int y=0; y<numberY; ++y)
344 for(int x=0; x<numberX; ++x)
346 pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
356 DEBUG->print("Grid::update: 015");
359 // find meta grid boundaries
360 if (metaGridWidth.x > 1.0e-6 && metaGridWidth.y > 1.0e-6
361 && graphicView->toGuiDX(metaGridWidth.x) > 2
362 && graphicView->toGuiDY(metaGridWidth.y) > 2)
364 double mleft = (int)(graphicView->toGraphX(0)
365 / metaGridWidth.x) * metaGridWidth.x;
366 double mright = (int)(graphicView->toGraphX(graphicView->getWidth())
367 / metaGridWidth.x) * metaGridWidth.x;
368 double mtop = (int)(graphicView->toGraphY(0)
369 / metaGridWidth.y) * metaGridWidth.y;
370 double mbottom = (int)(graphicView->toGraphY(graphicView->getHeight())
371 / metaGridWidth.y) * metaGridWidth.y;
373 mleft -= metaGridWidth.x;
374 mright += metaGridWidth.x;
375 mtop += metaGridWidth.y;
376 mbottom -= metaGridWidth.y;
378 // calculate number of visible meta grid lines:
379 numMetaX = (Math::round((mright - mleft) / metaGridWidth.x) + 1);
380 numMetaY = (Math::round((mtop - mbottom) / metaGridWidth.y) + 1);
382 if (numMetaX > 0 && numMetaY > 0)
384 // create meta grid arrays:
385 metaX = new double[numMetaX];
386 metaY = new double[numMetaY];
390 for(int x=0; x<numMetaX; ++x)
391 metaX[i++] = mleft + x * metaGridWidth.x;
395 for(int y=0; y<numMetaY; ++y)
396 metaY[i++] = mbottom + y * metaGridWidth.y;
407 DEBUG->print("Grid::update: OK");
411 * @return Array of all visible grid points.
413 Vector * Grid::getPoints()
419 * @return Number of visible grid points.
427 * @return Grid info for status widget.
429 QString Grid::getInfo()
431 return QString("%1 / %2").arg(spacing).arg(metaSpacing);
435 * @return Meta grid positions in X.
437 double * Grid::getMetaX()
443 * @return Number of visible meta grid lines in X.
445 int Grid::countMetaX()
451 * @return Meta grid positions in Y.
453 double * Grid::getMetaY()
459 * @return Number of visible meta grid lines in Y.
461 int Grid::countMetaY()