build: update parfait spec with a parfait-agent package
[pcp.git] / src / pmview / gridobj.cpp
blobcd0f762ca146a456c869fc0dc7727b06d6d5f870
1 /*
2 * Copyright (c) 1997 Silicon Graphics, Inc. All Rights Reserved.
3 * Copyright (c) 2009 Aconex. All Rights Reserved.
4 *
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.
9 *
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
13 * for more details.
15 #include <Inventor/nodes/SoSeparator.h>
16 #include <Inventor/nodes/SoBaseColor.h>
17 #include <Inventor/nodes/SoCube.h>
18 #include "gridobj.h"
19 #include "defaultobj.h"
21 #include <iostream>
22 using namespace std;
24 GridObj::~GridObj()
28 GridObj::GridObj(bool onFlag,
29 const DefaultObj &defaults,
30 int x, int y,
31 int cols, int rows,
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()),
38 _list(),
39 _rowDepth(1, _minDepth),
40 _colWidth(1, _minWidth),
41 _finished(false)
43 _objtype |= GRIDOBJ;
44 _defs = defaults;
47 void
48 GridObj::setTran(float xTran, float zTran, int setWidth, int setDepth)
50 int i, j;
51 int totalWidth;
52 int totalDepth;
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);
60 #ifdef PCP_DEBUG
61 if (pmDebug & DBG_TRACE_APPL1)
62 cerr << "\nGridObj(" << width() << "x" << depth() << ")@"
63 << col() << "," << row()
64 << "::setTran ("
65 << xTran << ", " << zTran << ", "
66 << setWidth << ", " << setDepth << ")" << endl;
67 #endif
69 BaseObj::setBaseSize(width(), depth());
70 BaseObj::setTran(xTran + xShift, zTran + zShift, setWidth, setDepth);
72 colPos[0] = 0;
73 for (i = 1; i < cols(); i++) {
74 colPos[i] = colPos[i-1] + _colWidth[i-1];
76 rowPos[0] = 0;
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];
89 #ifdef PCP_DEBUG
90 if (pmDebug & DBG_TRACE_APPL1)
91 cerr << "GridObj::setTran: [" << i << "] at " << item._col
92 << ',' << item._row << ": ";
93 #endif
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)
104 int extra = 0;
106 if (nsize > ivec.size()) {
107 extra = min * (nsize - ivec.size());
109 ivec.resize(nsize);
112 return extra;
115 void
116 GridObj::addObj(ViewObj *obj, int col, int row)
118 int i;
119 GridItem newItem;
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());
129 return;
133 #ifdef PCP_DEBUG
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;
138 #endif
140 newItem._item = obj;
141 newItem._row = row;
142 newItem._col = col;
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()) {
152 #ifdef PCP_DEBUG
153 if (pmDebug & DBG_TRACE_APPL0)
154 cerr << "GridObj::addObj: increasing col[" << col << "] from "
155 << _colWidth[col] << " to " << obj->width() << endl;
156 #endif
157 _width += obj->width() - _colWidth[col];
158 _colWidth[col] = obj->width();
161 if (obj->rows() == 1 && _rowDepth[row] < obj->depth()) {
162 #ifdef PCP_DEBUG
163 if (pmDebug & DBG_TRACE_APPL0)
164 cerr << "GridObj::addObj: increasing row[" << row << "] from "
165 << _rowDepth[row] << " to " << obj->depth() << endl;
166 #endif
167 _depth += obj->depth() - _rowDepth[row];
168 _rowDepth[row] = obj->depth();
172 void
173 GridObj::finishedAdd()
175 int i, j;
176 int size;
177 int current;
178 int adjust;
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();
193 j++) {
194 current += _colWidth[j];
197 if (current < size) {
198 size -= current;
199 adjust = (int)((size / (float)item._item->cols()) + 0.5);
200 for (j = item._col;
201 j < item._col + item._item->cols() && adjust > 0; j++) {
202 if (adjust > size) {
203 #ifdef PCP_DEBUG
204 if (pmDebug & DBG_TRACE_APPL0)
205 cerr << "GridObj::finishedAdd: increasing col["
206 << j << "] from " << _colWidth[j] << " to "
207 << _colWidth[j] + size << endl;
208 #endif
209 _colWidth[j] += size;
210 _width+= size;
211 adjust = 0;
213 else {
214 #ifdef PCP_DEBUG
215 if (pmDebug & DBG_TRACE_APPL0)
216 cerr << "GridObj::finishedAdd: increasing col["
217 << j << "] from " << _colWidth[j] << " to "
218 << _colWidth[j] + adjust << endl;
219 #endif
220 _colWidth[j] += adjust;
221 _width += adjust;
222 size -= 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();
232 j++) {
233 current += _rowDepth[j];
236 if (current < size) {
237 size -= current;
238 adjust = (int)((size / (float)item._item->rows()) + 0.5);
239 for (j = item._row;
240 j < item._row + item._item->rows() && adjust > 0; j++) {
241 if (adjust > size) {
242 #ifdef PCP_DEBUG
243 if (pmDebug & DBG_TRACE_APPL0)
244 cerr << "GridObj::finishedAdd: increasing row["
245 << j << "] from " << _rowDepth[j] << " to "
246 << _rowDepth[j] + size << endl;
247 #endif
248 _rowDepth[j] += size;
249 _depth+= size;
250 adjust = 0;
252 else {
253 #ifdef PCP_DEBUG
254 if (pmDebug & DBG_TRACE_APPL0)
255 cerr << "GridObj::finishedAdd: increasing row["
256 << j << "] from " << _rowDepth[j] << " to "
257 << _rowDepth[j] + adjust << endl;
258 #endif
259 _rowDepth[j] += adjust;
260 _depth += adjust;
261 size -= adjust;
268 _finished = true;
270 #ifdef PCP_DEBUG
271 if (pmDebug & DBG_TRACE_APPL0)
272 cerr << "GridObj::finishedAdd: " << *this << endl;
273 #endif
276 QTextStream&
277 operator<<(QTextStream& os, GridObj const& rhs)
279 rhs.display(os);
280 return os;
283 void
284 GridObj::display(QTextStream& os) const
286 int i;
287 int sum;
289 BaseObj::display(os);
290 os << ", minRowDepth = " << _minDepth << ", minColWidth = "
291 << _minWidth << ", rows = " << rows() << ", cols = " << cols()
292 << ", finishedAdd = " << (_finished == true ? "true" : "false")
293 << endl;
295 os << "Column widths: ";
296 for (i = 0, sum = 0; i < _colWidth.size(); i++) {
297 os << _colWidth[i] << ' ';
298 sum += _colWidth[i];
300 os << endl;
301 assert(_width == sum + baseWidth());
303 os << "Row depths: ";
304 for (i = 0, sum = 0; i < _rowDepth.size(); i++) {
305 os << _rowDepth[i] << ' ';
306 sum += _rowDepth[i];
308 os << endl;
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;