summaryrefslogtreecommitdiff
path: root/src/pmview/barobj.cpp
blob: da3e621f5edf6a355ce395341a52e3bbe90bb444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * Copyright (c) 1997 Silicon Graphics, Inc.  All Rights Reserved.
 * Copyright (c) 2009 Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoScale.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoTranslation.h>
#include <Inventor/nodes/SoTransform.h>

#include "barobj.h"
#include "colorlist.h"
#include "yscalemod.h"
#include "colormod.h"
#include "colorscalemod.h"
#include "text.h"
#include "defaultobj.h"

#include <iostream>
using namespace std;

BarObj::~BarObj()
{
}

BarObj::BarObj(ViewObj::Shape shape,
	       BarMod::Direction dir,
	       BarMod::Modulation mod,
	       BarMod::Grouping group,
	       bool baseFlag, 
	       const DefaultObj &defaults,
	       int x, int y, 
	       int cols, int rows, 
	       BaseObj::Alignment align)
: ModObj(baseFlag, defaults, x, y, cols, rows, align),
  _shape(shape),
  _dir(dir),
  _mod(mod),
  _group(group),
  _width(0),
  _depth(0),
  _xSpace(defaults.barSpaceX()),
  _zSpace(defaults.barSpaceZ()),
  _labelSpace(defaults.barSpaceLabel()),
  _bars(0),
  _metDir(towards),
  _metLabels(new QStringList),
  _instDir(away),
  _instLabels(new QStringList)
{
    _objtype |= BAROBJ;

    int	i;
    for (i = 0; i < numSides; i++)
	_margins[i] = 0.0;
    _labelColor[0] = defaults.labelColor(0);
    _labelColor[1] = defaults.labelColor(1);
    _labelColor[2] = defaults.labelColor(2);
}

void
BarObj::finishedAdd()
{
    const ColorSpec	*colSpec = NULL;
    SoNode		*object = ViewObj::object(_shape);
    SoSeparator		*labelSep = NULL;
    SoSeparator		*metricSep = NULL;
    SoSeparator		*instSep = NULL;
    SoSeparator		*barSep = new SoSeparator;
    SoSeparator		*baseSep = new SoSeparator;
    SoTranslation	*objTran = new SoTranslation;
    SoTranslation	*barTran = new SoTranslation;
    SoTranslation	*baseTran = new SoTranslation;
    SoTranslation	*modTran = new SoTranslation;
    ColorScale		*colScale = NULL;
    LabelSide		metSide = left;
    LabelSide		instSide = left;
    Text		**metText = NULL;
    Text		**instText = NULL;
    int			i;
    int			max = 0;
    int			numMetLabels = 0;
    int			numInstLabels = 0;

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0)
	cerr << "BarObj::finishedAdd:" << endl;
#endif

    if (_metrics.numMetrics() == 0) {
	BaseObj::addBase(_root);
	pmprintf("%s: Error: Bar object has no metrics\n",
		 pmProgname);
	_length = 0;
	_width = baseWidth();
	_depth = baseDepth();
	return;
    }

    _root->addChild(objTran);

    if (_metLabels->size() || _instLabels->size()) {
	labelSep = new SoSeparator;
	_root->addChild(labelSep);
	SoBaseColor *base = new SoBaseColor;
	base->rgb.setValue(_labelColor[0], _labelColor[1], _labelColor[2]);
	labelSep->addChild(base);
    }
    _root->addChild(barSep);
    barSep->addChild(barTran);
    barSep->addChild(baseSep);
    baseSep->addChild(baseTran);
    barSep->addChild(modTran);
    BaseObj::addBase(baseSep);

    // Determine color mapping

    if (_colors.size())
	colSpec = theColorLists.list((const char *)_colors.toAscii());

    if (colSpec != NULL) {
        if (colSpec->_scale) {
	    if (_mod == BarMod::yScale) {
		pmprintf("%s: Warning: Color scale ignored for Y-Scale Bar object.\n",
			 pmProgname);
	    }
	    else {
		if (colSpec->_list.size() == 0)
		    colScale = new ColorScale(0.0, 0.0, 1.0);
		else {
		    colScale = new ColorScale(*(colSpec->_list[0]));
		    for (i = 1; i < colSpec->_list.size(); i++)
			colScale->add(new ColorStep(*(colSpec->_list[i]),
							colSpec->_max[i]));
		}
	    }
	}
        else if (_mod == BarMod::color || _mod == BarMod::colYScale) {
	    pmprintf("%s: Warning: Expected color scale for color modulated Bar object.\n",
		     pmProgname);

	    if (colSpec->_list.size() == 0)
		colScale = new ColorScale(0.0, 0.0, 1.0);
	    else
		colScale = new ColorScale(*(colSpec->_list[0]));
	}
    }
    else {
        pmprintf("%s: Warning: No colours specified for Bar objects, defaulting to blue.\n",
                 pmProgname);

	if (_mod == BarMod::color || _mod == BarMod::colYScale)
	    colScale = new ColorScale(0.0, 0.0, 1.0);
    }

    if (_mod == BarMod::yScale) {
	if (colSpec != NULL)
	    for (i = 0; i < colSpec->_list.size(); i++)
		_metrics.add(*(colSpec->_list)[i]);
	_metrics.resolveColors(MetricList::perMetric);
    }

    // Generate Bar Modulate Object
    if (_mod == BarMod::yScale)
	_bars = new BarMod(&_metrics, object, _dir, _group,
			   (float)_length, (float)_maxHeight, (float)_length,
			   (float)_xSpace, (float)_zSpace);
    else {
	_bars = new BarMod(&_metrics, *colScale, object, _dir, _mod, _group,
			   (float)_length, (float)_maxHeight, (float)_length,
			   (float)_xSpace, (float)_zSpace);	
    }

    barSep->addChild(_bars->root());
    BaseObj::add(_bars);

    // Generate Labels

    if (_metLabels->size()) {
	if (_dir == BarMod::instPerRow)
	    if (_metDir == away)
		metSide = below;
	    else
		metSide = above;
	else
	    if (_metDir == away)
		metSide = right;
	    else
		metSide = left;

	metricSep = new SoSeparator;
	labelSep->addChild(metricSep);

	if (_metLabels->size() < _metrics.numMetrics())
	    numMetLabels = _metLabels->size();
	else
	    numMetLabels = _metrics.numMetrics();

	metText = calcLabels(*_metLabels, metSide, numMetLabels);
    }

    if (_instLabels->size()) {
	if (_dir == BarMod::instPerCol) {
	    max = _bars->cols();
	    if (_instDir == away)
		instSide = below;
	    else
		instSide = above;
	}
	else {
	    max = _bars->rows();
	    if (_instDir == away)
		instSide = right;
	    else
		instSide = left;
	}

	instSep = new SoSeparator;
	labelSep->addChild(instSep);

	if (_instLabels->size() < max)
	    numInstLabels = _instLabels->size();
	else
	    numInstLabels = max;

	instText = calcLabels(*_instLabels, instSide, numInstLabels);
    }

    // Width and depth of bars only, effects of labels added later

    _width = _bars->width();
    _depth = _bars->depth();

    // Insert the labels

    if (numMetLabels)
	metricSep->addChild(doLabels(metText, metSide, numMetLabels));

    if (numInstLabels)
	instSep->addChild(doLabels(instText, instSide, numInstLabels));

    // Work out where the bars live

    _bars->regenerate(_length, _length, _xSpace, _zSpace);
    _width = _bars->width();
    _depth = _bars->depth();

    baseTran->translation.setValue(_width / 2.0, 0.0, _depth / 2.0);
    
    _width += (u_int32_t)(baseWidth() + _margins[left] + _margins[right]+0.5);
    _depth += (u_int32_t)(baseDepth() + _margins[above] + _margins[below]+0.5);

    objTran->translation.setValue((_width / -2.0), 0.0, (_depth / -2.0));

    barTran->translation.setValue(_margins[left] + borderX(), 0.0,
				  _margins[above] + borderZ());


    modTran->translation.setValue(0.0, 
				  (BaseObj::state() ? baseHeight() : 0.0),
				  0.0);

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0)
	cerr << "BarObj::finishedAdd: metric list = " << endl
	     << _metrics << endl;
#endif

    if (_metrics.numMetrics())
	ViewObj::theNumModObjects++;

    // Cleanup

    if (colScale)
	delete colScale;
    delete _metLabels;
    delete _instLabels;
}

void
BarObj::setTran(float xTran, float zTran, int setWidth, int setDepth)
{
    BaseObj::setBaseSize(width() - _margins[left] - _margins[right],
			 depth() - _margins[above] - _margins[below]);
    BaseObj::setTran(xTran + (width() / 2.0),
		     zTran + (depth() / 2.0),
		     setWidth, setDepth);
}

QTextStream&
operator<<(QTextStream& os, BarObj const& rhs)
{
    rhs.display(os);
    return os;
}

void
BarObj::display(QTextStream& os) const
{
    BaseObj::display(os);

    if (_bars == NULL) {
	os << "No valid metrics" << endl; 
	return;
    }

    os << ", dir = " 
       << (_dir == BarMod::instPerCol ? "instPerCol" : "instPerRow")
       << ", length = " << _length << ", xSpace = " << _xSpace << ", zSpace = "
       << _zSpace << ", labelSpace = " << _labelSpace << ", rows = " << _rows 
       << ", cols = " << _cols << ", num bars = " << _bars->numBars()
       << ", shape = ";
    ViewObj::dumpShape(os, _shape);
    os << ", margins: left = " << _margins[left] << ", right = "
       << _margins[right] << ", above = " << _margins[above]
       << ", below = " << _margins[below];
}

const char*
BarObj::name() const
{
    static QString myName;

    if (myName.size() == 0) {
	if (_bars == NULL)
	    myName = "Invalid bar object";
	else {
	    myName = _bars->modStr();
	    myName.append(" Bar Object (");
	    myName.append(_bars->dirStr());
	    myName.append(QChar(')'));
	}
    }

    return (const char *)myName.toAscii();
}

Text **
BarObj::calcLabels(const QStringList &labels, LabelSide side, int numLabels)
{
    Text		**text = NULL;
    int			i;
    int			maxWidth = 0;
    int			maxDepth = 0;

    text= new Text*[numLabels];

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	cerr << "BarObj::calcLabels: " << numLabels << " labels on the ";
	switch(side) {
	case left:
	    cerr << "left";
	    break;
	case right:
	    cerr << "right";
	    break;
	case above:
	    cerr << "above";
	    break;
	case below:
	    cerr << "below";
	    break;
	}
	cerr << " side" << endl;
    }
#endif

    // Create the text objects so that we know how big they are

    for (i = 0; i < numLabels; i++) {
	if (side == above || side == below)
	    text[i] = new Text(labels[i], Text::down, Text::medium);
	else
	    text[i] = new Text(labels[i], Text::right, Text::medium);

	if (text[i]->width() > maxWidth)
	    maxWidth = text[i]->width();
	if (text[i]->depth() > maxDepth)
	    maxDepth = text[i]->depth();
    }

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0)
	cerr << "BarObj::calcLabels: maxWidth = " << maxWidth
	     << ", maxDepth = " << maxDepth << endl;
#endif

    // Determine if the size of the bars will need to be increased

    if (side == above || side == below) {
	_margins[side] = maxDepth + _labelSpace;
	if (maxWidth > _length) {
	    _length = maxWidth;

#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL0)
		cerr << "BarObj::calcLabels: length (width) increased to "
		     << _length << endl;
#endif

	}
    }
    else {
	_margins[side] = maxWidth + _labelSpace;
	if (maxDepth > _length) {
	    _length = maxDepth;


#ifdef PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL0)
		cerr << "BarObj::calcLabels: length (depth) increased to " 
		     << _length << endl;
#endif
	}
    }
    return text;
}

SoNode *
BarObj::doLabels(Text **text, LabelSide side, int numLabels)
{
    SoSeparator		*sep = new SoSeparator;
    SoTranslation	*tran = new SoTranslation;
    int			i;
    int			maxWidth = 0;
    int			maxDepth = 0;

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	cerr << "BarObj::doLabels: " << numLabels << " labels on the ";
	switch(side) {
	case left:
	    cerr << "left";
	    break;
	case right:
	    cerr << "right";
	    break;
	case above:
	    cerr << "above";
	    break;
	case below:
	    cerr << "below";
	    break;
	}
	cerr << " side" << endl;
    }
#endif

    sep->addChild(tran);

    // Determine the translation to the first label, subsequent labels
    // are translated from the first

    maxWidth = _length + _xSpace;
    maxDepth = _length + _zSpace;

    switch (side) {
    case left:
	tran->translation.setValue(_margins[left] - _labelSpace, 0.0, 
				   _margins[above] + borderZ());
	break;
    case right:
	tran->translation.setValue(
			 _margins[left] + _width + baseWidth() + _labelSpace, 
				   0.0, _margins[above] + borderZ());
	break;
    case above:
	tran->translation.setValue(_margins[left] + borderX(), 0.0, 
				   _margins[above] - _labelSpace);
	break;
    case below:
	tran->translation.setValue(_margins[left] + borderX(), 0.0,
			 _margins[above] + _depth + baseDepth() + _labelSpace);
	break;
    default:
	break;
    }

#ifdef PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL0) {
	float x, y, z;
	tran->translation.getValue().getValue(x, y, z);
	cerr << "BarObj::doLabels: translation set to " << x << ',' << y
	     << ',' << z << endl;
    }
#endif

    // Add each label to the scene graph

    for (i = 0; i < numLabels; i++) {
	SoSeparator *labelSep = new SoSeparator;
	sep->addChild(labelSep);

	SoTranslation *labelTran = new SoTranslation;
	labelSep->addChild(labelTran);

	switch (side) {
	case left:
	    labelTran->translation.setValue(0.0,
					    0.0, 
		  (maxDepth * i) + ((_length - (float)text[i]->depth())/ 2.0));
	    break;
	case right:
	    labelTran->translation.setValue(text[i]->width(), 0.0, 
		  (maxDepth * i) + ((_length - (float)text[i]->depth())/ 2.0));
	    break;
	case above:
	    labelTran->translation.setValue(
		  (maxWidth * i) + ((_length - (float)text[i]->width())/ 2.0),
					    0.0, 0.0);
	    break;
	case below:
	    labelTran->translation.setValue(
		  (maxWidth * i) + ((_length - (float)text[i]->width())/ 2.0),
					    0.0, text[i]->depth());
	    break;
	default:
	    break;
	}

	labelSep->addChild(text[i]->root());
    }

    // Do not delete contents, just the array pointer
    delete [] text;

    return sep;
}