2 * Copyright (c) 1997 Silicon Graphics, Inc. All Rights Reserved.
3 * Copyright (c) 2009 Aconex. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 #include <Inventor/nodes/SoSeparator.h>
16 #include <Inventor/nodes/SoBaseColor.h>
17 #include <Inventor/nodes/SoCube.h>
19 #include "defaultobj.h"
28 GridObj::GridObj(bool onFlag
,
29 const DefaultObj
&defaults
,
32 GridObj::Alignment align
)
33 : BaseObj(onFlag
, defaults
, x
, y
, cols
, rows
, align
),
34 _minDepth(defaults
.gridMinDepth()),
35 _minWidth(defaults
.gridMinWidth()),
36 _width(_minWidth
+ baseWidth()),
37 _depth(_minDepth
+ baseDepth()),
39 _rowDepth(1, _minDepth
),
40 _colWidth(1, _minWidth
),
48 GridObj::setTran(float xTran
, float zTran
, int setWidth
, int setDepth
)
53 float xShift
= width() / 2.0;
54 float zShift
= depth() / 2.0;
55 QVector
<int> rowPos(rows());
56 QVector
<int> colPos(cols());
58 assert(_finished
== true);
61 if (pmDebug
& DBG_TRACE_APPL1
)
62 cerr
<< "\nGridObj(" << width() << "x" << depth() << ")@"
63 << col() << "," << row()
65 << xTran
<< ", " << zTran
<< ", "
66 << setWidth
<< ", " << setDepth
<< ")" << endl
;
69 BaseObj::setBaseSize(width(), depth());
70 BaseObj::setTran(xTran
+ xShift
, zTran
+ zShift
, setWidth
, setDepth
);
73 for (i
= 1; i
< cols(); i
++) {
74 colPos
[i
] = colPos
[i
-1] + _colWidth
[i
-1];
77 for (i
= 1; i
< rows(); i
++) {
78 rowPos
[i
] = rowPos
[i
-1] + _rowDepth
[i
-1];
81 for (i
= 0; i
< _list
.size(); i
++) {
82 GridItem
&item
= _list
[i
];
83 totalWidth
= totalDepth
= 0;
84 for (j
= item
._col
; j
< item
._col
+ item
._item
->cols(); j
++)
85 totalWidth
+= _colWidth
[j
];
86 for (j
= item
._row
; j
< item
._row
+ item
._item
->rows(); j
++)
87 totalDepth
+= _rowDepth
[j
];
90 if (pmDebug
& DBG_TRACE_APPL1
)
91 cerr
<< "GridObj::setTran: [" << i
<< "] at " << item
._col
92 << ',' << item
._row
<< ": ";
95 item
._item
->setTran(colPos
[item
._col
] - xShift
+ borderX(),
96 rowPos
[item
._row
] - zShift
+ borderZ(),
97 totalWidth
, totalDepth
);
102 addRowCols(QVector
<int> & ivec
, int nsize
, int min
)
106 if (nsize
> ivec
.size()) {
107 extra
= min
* (nsize
- ivec
.size());
116 GridObj::addObj(ViewObj
*obj
, int col
, int row
)
121 for (i
= 0; i
< _list
.size(); i
++) {
122 const GridItem
&item
= _list
[i
];
123 if ((row
>= item
._row
&& row
< (item
._row
+ item
._item
->rows())) &&
124 (col
>= item
._col
&& col
< (item
._col
+ item
._item
->cols()))) {
125 pmprintf("%s: %s at %d,%d (%dx%d) collides with %s at %d,%d (%dx%d)\nThe later object will be ignored\n",
126 pmProgname
, item
._item
->name(), item
._col
,
127 item
._row
, item
._item
->cols(), item
._item
->rows(),
128 obj
->name(), col
, row
, obj
->cols(), obj
->rows());
134 if (pmDebug
& DBG_TRACE_APPL0
)
135 cerr
<< "GridObj::addObj: Adding item " << _list
.length() << ": "
136 << obj
->name() << ", size = " << obj
->width() << 'x'
137 << obj
->depth() << endl
;
143 _list
.append(newItem
);
145 // Add extra cols & rows as necessary
146 _width
+= addRowCols (_colWidth
, col
+ obj
->cols(), _minWidth
);
147 _depth
+= addRowCols (_rowDepth
, row
+ obj
->rows(), _minDepth
);
150 // Fasttrack size adjustments for simple objects
151 if (obj
->cols() == 1 && _colWidth
[col
] < obj
->width()) {
153 if (pmDebug
& DBG_TRACE_APPL0
)
154 cerr
<< "GridObj::addObj: increasing col[" << col
<< "] from "
155 << _colWidth
[col
] << " to " << obj
->width() << endl
;
157 _width
+= obj
->width() - _colWidth
[col
];
158 _colWidth
[col
] = obj
->width();
161 if (obj
->rows() == 1 && _rowDepth
[row
] < obj
->depth()) {
163 if (pmDebug
& DBG_TRACE_APPL0
)
164 cerr
<< "GridObj::addObj: increasing row[" << row
<< "] from "
165 << _rowDepth
[row
] << " to " << obj
->depth() << endl
;
167 _depth
+= obj
->depth() - _rowDepth
[row
];
168 _rowDepth
[row
] = obj
->depth();
173 GridObj::finishedAdd()
180 BaseObj::addBase(_root
);
182 for (i
= 0; i
< _list
.size(); i
++) {
183 const GridItem
&item
= _list
[i
];
184 _root
->addChild(item
._item
->root());
185 if (item
._item
->modObj() != NULL
) {
186 BaseObj::add(item
._item
->modObj());
189 if (item
._item
->cols() > 1) {
190 size
= item
._item
->width();
191 for (j
= item
._col
, current
= 0;
192 j
< item
._col
+ item
._item
->cols();
194 current
+= _colWidth
[j
];
197 if (current
< size
) {
199 adjust
= (int)((size
/ (float)item
._item
->cols()) + 0.5);
201 j
< item
._col
+ item
._item
->cols() && adjust
> 0; j
++) {
204 if (pmDebug
& DBG_TRACE_APPL0
)
205 cerr
<< "GridObj::finishedAdd: increasing col["
206 << j
<< "] from " << _colWidth
[j
] << " to "
207 << _colWidth
[j
] + size
<< endl
;
209 _colWidth
[j
] += size
;
215 if (pmDebug
& DBG_TRACE_APPL0
)
216 cerr
<< "GridObj::finishedAdd: increasing col["
217 << j
<< "] from " << _colWidth
[j
] << " to "
218 << _colWidth
[j
] + adjust
<< endl
;
220 _colWidth
[j
] += adjust
;
228 if (item
._item
->rows() > 1) {
229 size
= item
._item
->depth();
230 for (j
= item
._row
, current
= 0;
231 j
< item
._row
+ item
._item
->rows();
233 current
+= _rowDepth
[j
];
236 if (current
< size
) {
238 adjust
= (int)((size
/ (float)item
._item
->rows()) + 0.5);
240 j
< item
._row
+ item
._item
->rows() && adjust
> 0; j
++) {
243 if (pmDebug
& DBG_TRACE_APPL0
)
244 cerr
<< "GridObj::finishedAdd: increasing row["
245 << j
<< "] from " << _rowDepth
[j
] << " to "
246 << _rowDepth
[j
] + size
<< endl
;
248 _rowDepth
[j
] += size
;
254 if (pmDebug
& DBG_TRACE_APPL0
)
255 cerr
<< "GridObj::finishedAdd: increasing row["
256 << j
<< "] from " << _rowDepth
[j
] << " to "
257 << _rowDepth
[j
] + adjust
<< endl
;
259 _rowDepth
[j
] += adjust
;
271 if (pmDebug
& DBG_TRACE_APPL0
)
272 cerr
<< "GridObj::finishedAdd: " << *this << endl
;
277 operator<<(QTextStream
& os
, GridObj
const& rhs
)
284 GridObj::display(QTextStream
& os
) const
289 BaseObj::display(os
);
290 os
<< ", minRowDepth = " << _minDepth
<< ", minColWidth = "
291 << _minWidth
<< ", rows = " << rows() << ", cols = " << cols()
292 << ", finishedAdd = " << (_finished
== true ? "true" : "false")
295 os
<< "Column widths: ";
296 for (i
= 0, sum
= 0; i
< _colWidth
.size(); i
++) {
297 os
<< _colWidth
[i
] << ' ';
301 assert(_width
== sum
+ baseWidth());
303 os
<< "Row depths: ";
304 for (i
= 0, sum
= 0; i
< _rowDepth
.size(); i
++) {
305 os
<< _rowDepth
[i
] << ' ';
309 assert(_depth
== sum
+ baseDepth());
311 for (i
= 0; i
< _list
.size(); i
++)
312 os
<< '[' << i
<< "] at " << _list
[i
]._col
<< ',' << _list
[i
]._row
313 << ": " << *(_list
[i
]._item
) << endl
;