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 * drawing = graphicView->GetDrawing();
62 settings.beginGroup("Appearance");
63 bool scaleGrid = settings.value("ScaleGrid", true).toBool();
64 int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
71 userGrid = drawing->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 = drawing->getUnit();
105 format = drawing->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) * gridWidth.x;
314 double right = (int)(graphicView->toGraphX(graphicView->getWidth())
315 / gridWidth.x) * gridWidth.x;
316 double top = (int)(graphicView->toGraphY(0) / gridWidth.y) * gridWidth.y;
317 double bottom = (int)(graphicView->toGraphY(graphicView->getHeight())
318 / gridWidth.y) * gridWidth.y;
321 right += gridWidth.x;
323 bottom -= gridWidth.y;
325 // calculate number of visible grid points
326 int numberX = (Math::round((right-left) / gridWidth.x) + 1);
327 int numberY = (Math::round((top-bottom) / gridWidth.y) + 1);
328 number = numberX * numberY;
330 DEBUG->print("Grid::update: 014");
332 // create grid array:
333 if (number > 0 && number < 1000000)
335 pt = new Vector[number];
338 for(int y=0; y<numberY; ++y)
340 for(int x=0; x<numberX; ++x)
342 pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
352 DEBUG->print("Grid::update: 015");
355 // find meta grid boundaries
356 if (metaGridWidth.x > 1.0e-6 && metaGridWidth.y > 1.0e-6
357 && graphicView->toGuiDX(metaGridWidth.x) > 2
358 && graphicView->toGuiDY(metaGridWidth.y) > 2)
360 double mleft = (int)(graphicView->toGraphX(0)
361 / metaGridWidth.x) * metaGridWidth.x;
362 double mright = (int)(graphicView->toGraphX(graphicView->getWidth())
363 / metaGridWidth.x) * metaGridWidth.x;
364 double mtop = (int)(graphicView->toGraphY(0)
365 / metaGridWidth.y) * metaGridWidth.y;
366 double mbottom = (int)(graphicView->toGraphY(graphicView->getHeight())
367 / metaGridWidth.y) * metaGridWidth.y;
369 mleft -= metaGridWidth.x;
370 mright += metaGridWidth.x;
371 mtop += metaGridWidth.y;
372 mbottom -= metaGridWidth.y;
374 // calculate number of visible meta grid lines:
375 numMetaX = (Math::round((mright - mleft) / metaGridWidth.x) + 1);
376 numMetaY = (Math::round((mtop - mbottom) / metaGridWidth.y) + 1);
378 if (numMetaX > 0 && numMetaY > 0)
380 // create meta grid arrays:
381 metaX = new double[numMetaX];
382 metaY = new double[numMetaY];
386 for(int x=0; x<numMetaX; ++x)
387 metaX[i++] = mleft + x * metaGridWidth.x;
391 for(int y=0; y<numMetaY; ++y)
392 metaY[i++] = mbottom + y * metaGridWidth.y;
403 DEBUG->print("Grid::update: OK");
407 * @return Array of all visible grid points.
409 Vector * Grid::getPoints()
415 * @return Number of visible grid points.
423 * @return Grid info for status widget.
425 QString Grid::getInfo()
427 return QString("%1 / %2").arg(spacing).arg(metaSpacing);
431 * @return Meta grid positions in X.
433 double * Grid::getMetaX()
439 * @return Number of visible meta grid lines in X.
441 int Grid::countMetaX()
447 * @return Meta grid positions in Y.
449 double * Grid::getMetaY()
455 * @return Number of visible meta grid lines in Y.
457 int Grid::countMetaY()