]> Shamusworld >> Repos - architektonas/blob - src/base/grid.cpp
Initial removal of unnecessary rs_ prefixes from files.
[architektonas] / src / base / grid.cpp
1 // grid.cpp
2 //
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
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/21/2010  Added this text. :-)
15 //
16
17 #include "grid.h"
18
19 #include "drawing.h"
20 #include "graphicview.h"
21 //#include "graphicview.h"
22 #include "settings.h"
23 #include "units.h"
24 #include "vector.h"
25
26 #warning "!!! Clean out all references to RS_GraphicView here and in header !!!"
27 #if 0
28 /**
29  * Constructor.
30  */
31 RS_Grid::RS_Grid(RS_GraphicView * gv): graphicView(gv), pt(NULL), number(0),
32         metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
33 {
34 }
35 #endif
36
37 /**
38  * Constructor II.
39  */
40 RS_Grid::RS_Grid(GraphicView * gv): graphicView(gv), pt(NULL), number(0),
41         metaX(NULL), numMetaX(0), metaY(NULL), numMetaY(0)
42 {
43 }
44
45 /**
46  * Destructor.
47  */
48 RS_Grid::~RS_Grid()
49 {
50         if (pt != NULL)
51                 delete[] pt;
52
53         if (metaX != NULL)
54                 delete[] metaX;
55
56         if (metaY != NULL)
57                 delete[] metaY;
58 }
59
60 /**
61  * Updates the grid point array.
62  */
63 void RS_Grid::update()
64 {
65         RS_DEBUG->print("RS_Grid::update");
66
67         if (!graphicView->isGridOn())
68                 return;
69
70         Drawing * graphic = graphicView->getGraphic();
71
72         // auto scale grid?
73         settings.beginGroup("Appearance");
74         bool scaleGrid = settings.value("ScaleGrid", true).toBool();
75         int minGridSpacing = settings.value("MinGridSpacing", 10).toInt();
76         settings.endGroup();
77
78         // get grid setting
79         Vector userGrid;
80
81         if (graphic != NULL)
82                 userGrid = graphic->getVariableVector("$GRIDUNIT", Vector(-1.0, -1.0));
83
84         // delete old grid:
85         if (pt != NULL)
86         {
87                 delete[] pt;
88                 pt = NULL;
89         }
90
91         if (metaX != NULL)
92         {
93                 delete[] metaX;
94                 metaX = NULL;
95         }
96
97         if (metaY != NULL)
98         {
99                 delete[] metaY;
100                 metaY = NULL;
101         }
102
103         number = 0;
104         numMetaX = 0;
105         numMetaY = 0;
106
107         RS_DEBUG->print("RS_Grid::update: 001");
108
109         // find out unit:
110         RS2::Unit unit = RS2::None;
111         RS2::LinearFormat format = RS2::Decimal;
112
113         if (graphic != NULL)
114         {
115                 unit = graphic->getUnit();
116                 format = graphic->getLinearFormat();
117         }
118
119         Vector gridWidth;
120         Vector metaGridWidth;
121
122         RS_DEBUG->print("RS_Grid::update: 002");
123
124         // init grid spacing:
125         // metric grid:
126         if (RS_Units::isMetric(unit) || unit == RS2::None
127                 || format == RS2::Decimal || format == RS2::Engineering)
128         {
129                 if (userGrid.x > 0.0)
130                         gridWidth.x = userGrid.x;
131                 else
132                         gridWidth.x = 0.000001;
133
134                 if (userGrid.y > 0.0)
135                         gridWidth.y = userGrid.y;
136                 else
137                         gridWidth.y = 0.000001;
138
139                 RS_DEBUG->print("RS_Grid::update: 003");
140
141                 // auto scale grid
142                 if (scaleGrid)
143                 {
144                         while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
145                                 gridWidth.x *= 10;
146
147                         while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
148                                 gridWidth.y *= 10;
149                 }
150
151                 metaGridWidth.x = gridWidth.x * 10;
152                 metaGridWidth.y = gridWidth.y * 10;
153
154                 RS_DEBUG->print("RS_Grid::update: 004");
155         }
156         // imperial grid:
157         else
158         {
159                 RS_DEBUG->print("RS_Grid::update: 005");
160
161                 if (userGrid.x > 0.0)
162                         gridWidth.x = userGrid.x;
163                 else
164                         gridWidth.x = 1.0 / 1024.0;
165
166                 if (userGrid.y > 0.0)
167                         gridWidth.y = userGrid.y;
168                 else
169                         gridWidth.y = 1.0 / 1024.0;
170
171                 RS_DEBUG->print("RS_Grid::update: 006");
172
173                 if (unit == RS2::Inch)
174                 {
175                         RS_DEBUG->print("RS_Grid::update: 007");
176
177                         // auto scale grid
178                         if (scaleGrid)
179                         {
180                                 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
181                                 {
182                                         if (RS_Math::round(gridWidth.x) >= 36)
183                                         {
184                                                 gridWidth.x *= 2;
185                                         }
186                                         else if (RS_Math::round(gridWidth.x) >= 12)
187                                         {
188                                                 gridWidth.x *= 3;
189                                         }
190                                         else if (RS_Math::round(gridWidth.x) >= 4)
191                                         {
192                                                 gridWidth.x *= 3;
193                                         }
194                                         else if (RS_Math::round(gridWidth.x) >= 1)
195                                         {
196                                                 gridWidth.x *= 2;
197                                         }
198                                         else
199                                         {
200                                                 gridWidth.x*=2;
201                                         }
202                                 }
203
204                                 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
205                                 {
206                                         if (RS_Math::round(gridWidth.y) >= 36)
207                                         {
208                                                 gridWidth.y *= 2;
209                                         }
210                                         else if (RS_Math::round(gridWidth.y) >= 12)
211                                         {
212                                                 gridWidth.y *= 3;
213                                         }
214                                         else if (RS_Math::round(gridWidth.y) >= 4)
215                                         {
216                                                 gridWidth.y *= 3;
217                                         }
218                                         else if (RS_Math::round(gridWidth.y) >= 1)
219                                         {
220                                                 gridWidth.y *= 2;
221                                         }
222                                         else
223                                         {
224                                                 gridWidth.y *= 2;
225                                         }
226                                 }
227                         }
228
229                         RS_DEBUG->print("RS_Grid::update: 008");
230
231                         // metagrid X shows inches..
232                         metaGridWidth.x = 1.0;
233
234                         if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
235                         {
236                                 // .. or feet
237                                 metaGridWidth.x = 12.0;
238
239                                 // .. or yards
240                                 if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
241                                 {
242                                         metaGridWidth.x = 36.0;
243
244                                         // .. or miles (not really..)
245                                         //if (graphicView->toGuiDX(metaGridWidth.x)<20) {
246                                         //    metaGridWidth.x = 63360.0;
247                                         //}
248
249                                         // .. or nothing
250                                         if (graphicView->toGuiDX(metaGridWidth.x) < minGridSpacing * 2)
251                                                 metaGridWidth.x = -1.0;
252
253                                 }
254                         }
255
256                         RS_DEBUG->print("RS_Grid::update: 009");
257
258                         // metagrid Y shows inches..
259                         metaGridWidth.y = 1.0;
260
261                         if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
262                         {
263                                 // .. or feet
264                                 metaGridWidth.y = 12.0;
265
266                                 // .. or yards
267                                 if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
268                                 {
269                                         metaGridWidth.y = 36.0;
270
271                                         // .. or miles (not really..)
272                                         //if (graphicView->toGuiDY(metaGridWidth.y)<20) {
273                                         //    metaGridWidth.y = 63360.0;
274                                         //}
275
276                                         // .. or nothing
277                                         if (graphicView->toGuiDY(metaGridWidth.y) < minGridSpacing * 2)
278                                         {
279                                                 metaGridWidth.y = -1.0;
280                                         }
281
282                                 }
283                         }
284
285                         RS_DEBUG->print("RS_Grid::update: 010");
286                 }
287                 else
288                 {
289                         RS_DEBUG->print("RS_Grid::update: 011");
290
291                         if (scaleGrid)
292                         {
293                                 while (graphicView->toGuiDX(gridWidth.x) < minGridSpacing)
294                                 {
295                                         gridWidth.x *= 2;
296                                 }
297
298                                 metaGridWidth.x = -1.0;
299
300                                 while (graphicView->toGuiDY(gridWidth.y) < minGridSpacing)
301                                 {
302                                         gridWidth.y *= 2;
303                                 }
304
305                                 metaGridWidth.y = -1.0;
306                         }
307                         RS_DEBUG->print("RS_Grid::update: 012");
308                 }
309                 //gridWidth.y = gridWidth.x;
310                 //metaGridWidth.y = metaGridWidth.x;
311         }
312
313         RS_DEBUG->print("RS_Grid::update: 013");
314
315         // for grid info:
316         spacing = gridWidth.x;
317         metaSpacing = metaGridWidth.x;
318
319         if (gridWidth.x > 1.0e-6 && gridWidth.y > 1.0e-6
320                 && graphicView->toGuiDX(gridWidth.x) > 2
321                 && graphicView->toGuiDY(gridWidth.y) > 2)
322         {
323                 // find grid boundaries
324                 double left = (int)(graphicView->toGraphX(0) / gridWidth.x)
325                         * 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;
330                 double bottom =
331                         (int)(graphicView->toGraphY(graphicView->getHeight())
332                         / gridWidth.y) * gridWidth.y;
333
334
335                 left -= gridWidth.x;
336                 right += gridWidth.x;
337                 top += gridWidth.y;
338                 bottom -= gridWidth.y;
339
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;
344
345                 RS_DEBUG->print("RS_Grid::update: 014");
346
347                 // create grid array:
348                 if (number > 0 && number < 1000000)
349                 {
350                         pt = new Vector[number];
351
352                         int i=0;
353                         for(int y=0; y<numberY; ++y)
354                         {
355                                 for(int x=0; x<numberX; ++x)
356                                 {
357                                         pt[i++] = Vector(left + x * gridWidth.x, bottom + y * gridWidth.y);
358                                 }
359                         }
360                 }
361                 else
362                 {
363                         number = 0;
364                         pt = NULL;
365                 }
366
367                 RS_DEBUG->print("RS_Grid::update: 015");
368         }
369
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)
374         {
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;
383
384                 mleft -= metaGridWidth.x;
385                 mright += metaGridWidth.x;
386                 mtop += metaGridWidth.y;
387                 mbottom -= metaGridWidth.y;
388
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);
392
393                 if (numMetaX > 0 && numMetaY > 0)
394                 {
395                         // create meta grid arrays:
396                         metaX = new double[numMetaX];
397                         metaY = new double[numMetaY];
398
399                         int i = 0;
400
401                         for(int x=0; x<numMetaX; ++x)
402                                 metaX[i++] = mleft + x * metaGridWidth.x;
403
404                         i = 0;
405
406                         for(int y=0; y<numMetaY; ++y)
407                                 metaY[i++] = mbottom + y * metaGridWidth.y;
408                 }
409                 else
410                 {
411                         numMetaX = 0;
412                         metaX = NULL;
413                         numMetaY = 0;
414                         metaY = NULL;
415                 }
416         }
417
418         RS_DEBUG->print("RS_Grid::update: OK");
419 }
420
421 /**
422  * @return Array of all visible grid points.
423  */
424 Vector * RS_Grid::getPoints()
425 {
426         return pt;
427 }
428
429 /**
430  * @return Number of visible grid points.
431  */
432 int RS_Grid::count()
433 {
434         return number;
435 }
436
437 /**
438  * @return Grid info for status widget.
439  */
440 QString RS_Grid::getInfo()
441 {
442         return QString("%1 / %2").arg(spacing).arg(metaSpacing);
443 }
444
445 /**
446  * @return Meta grid positions in X.
447  */
448 double * RS_Grid::getMetaX()
449 {
450         return metaX;
451 }
452
453 /**
454  * @return Number of visible meta grid lines in X.
455  */
456 int RS_Grid::countMetaX()
457 {
458         return numMetaX;
459 }
460
461 /**
462 * @return Meta grid positions in Y.
463 */
464 double * RS_Grid::getMetaY()
465 {
466         return metaY;
467 }
468
469 /**
470 * @return Number of visible meta grid lines in Y.
471 */
472 int RS_Grid::countMetaY()
473 {
474         return numMetaY;
475 }