summaryrefslogtreecommitdiffstats
path: root/kgantt/kgantt/KGanttItem.cpp
blob: f032ede037b005eb68c42e6b99ccf8e80e597c81 (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//
//  file    : KGanttItem.C
//  date    : 26 oct 2000
//  changed : 11 jan 2001
//  author  : jh
//


#include "KGanttItem.h"


TQBrush KGanttItem::_selectBrush(TQColor(255,0,0));


KGanttItem::KGanttItem(KGanttItem* parentItem, const TQString& text, 
	 const TQDateTime& start, const TQDateTime& end)
  : TQObject()
////////////////////////////////////////////////////////
{
  init(parentItem,text, start,end);
}



KGanttItem::KGanttItem(KGanttItem* parentItem, const TQString& text, 
	 const TQDateTime& start, long durationMin)
  : TQObject()
////////////////////////////////////////////////////////
{  
  init(parentItem, text, start, start.addSecs( durationMin * 60));
}



void 
KGanttItem::init(KGanttItem* parentItem, const TQString& text,
	     const TQDateTime& start, const TQDateTime& end) 
///////////////////////////////////////////////////////////////
{
  _style = DrawAll - DrawHandle;
  _open = true;
  _selected = false;
  _editable = true;

  _mode = Normal;

  _brush   = TQBrush(TQColor(140,140,255));
  _pen     = TQPen(TQColor(100,100,100));
  _textPen = TQPen(TQColor(black));

  _height = 24;

  _text = text;

  _start = start; _minDateTime = start;
  _end = end; _maxDateTime = end;

  _parentItem = parentItem;

  if(_parentItem)
    _parentItem->registerItem(this);
  
}



KGanttItem::~KGanttItem()
/////////////////
{
#ifdef _DEBUG_
  printf("-> delete %s \n", getText().latin1() );
#endif

  if(_parentItem)
    _parentItem->unregisterItem(this);

  _subitems.setAutoDelete(true);
  _subitems.clear();

  emit destroyed(this);

#ifdef _DEBUG_
  printf("<- delete %s \n", getText().latin1() );
#endif
}



KGanttRelation* 
KGanttItem::addRelation(KGanttItem* from, KGanttItem* to,
			const TQString& text)
{
  if(_subitems.containsRef(from) > 0 && _subitems.containsRef(to) >0) {
    KGanttRelation* rel = new KGanttRelation(from,to,text);
    _relations.append(rel);

    connect(rel, TQT_SIGNAL(destroyed(KGanttRelation*)),
	    this, TQT_SLOT(removeRelation(KGanttRelation*)));

    emit changed(this, RelationAdded);
    return rel;
  }	   
  else
    return NULL;
}



void
KGanttItem::removeRelation(KGanttRelation* rel)
{
  if( _relations.removeRef(rel) )
    emit changed(this, RelationRemoved);
}



void 
KGanttItem::endTransaction()
///////////////////////////
{
  blockSignals(false);
  emit changed(this, Unknown);
}



void 
KGanttItem::registerItem(KGanttItem* item)
{
  _subitems.append(item);
  
  connect(item, TQT_SIGNAL(changed(KGanttItem*, KGanttItem::Change)),
	  this, TQT_SLOT(subItemChanged(KGanttItem*, KGanttItem::Change)) );

  bool minChanged = false;
  bool maxChanged = false;

  // update min/man

  if(_subitems.count() == 1) {

    _minDateTime = item->getStart();
    _maxDateTime = item->getEnd();
    
    minChanged = true;
    maxChanged = true;

  }
  else {

    if(item->getEnd() > _maxDateTime) {
      _maxDateTime = item->getEnd();
      maxChanged = true;
    }
    
    if(_minDateTime > item->getStart()) {
      _minDateTime = item->getStart();
      minChanged = true;
    }
    
  } // else


  //  increase start/end if necessary
  Change change = adjustStartEnd();

  if(_mode == Rubberband) {
    if(minChanged && !(change & StartChanged))
      change = (Change) (change + StartChanged);
    if(maxChanged && !(change & EndChanged))
      change = (Change) (change + EndChanged);
  }

  if( isOpen() ) {
    if(!(change & TotalHeightChanged))
      change = (Change) (change + TotalHeightChanged);
  }

  if(change != NoChange)
    emit changed(this,change);

}



void 
KGanttItem::unregisterItem(KGanttItem* item)
{
  _subitems.removeRef(item);
  disconnect(item);

  Change change = adjustMinMax();

  if( isOpen() ) {
    if(!(change & TotalHeightChanged))
      change = (Change) (change + TotalHeightChanged);
  }

  if(change != NoChange)
    emit changed(this,change);

}



TQDateTime 
KGanttItem::getStart()
{ 
  if(_mode == Rubberband && _subitems.count()>0)
    return _minDateTime;
  else
    return _start; 
}




TQDateTime 
KGanttItem::getEnd()
/////////////////
{
  if(_mode == Rubberband && _subitems.count()>0)
    return _maxDateTime;
  else
    return _end; 
}



void 
KGanttItem::setStart(const TQDateTime& start) 
{
  if(!_editable) return;

  //  if there are no subitems, just set _start and _minDateTime
  if(_subitems.count()==0) {

    if(_start != start) {
      _start = start;
      _minDateTime = _start;
      emit changed(this,StartChanged);
    }
    
  }
  else {

    //  if there are subitems, just change start if
    //  mode is not 'rubberband' and start is less than _minDateTime

    if(_mode != Rubberband) {

      if(start < _minDateTime)
	_start = start;
      else
	_start = _minDateTime;
    
      emit changed(this,StartChanged);

    }
      
  }
  
}



void 
KGanttItem::setEnd(const TQDateTime& end) 
{ 
  if(!_editable) return;

  //  if there are no subitems, just set _end and _maxDateTime
  if(_subitems.count()==0) {

    if(_end != end) {
      _end = end;
      _maxDateTime = _end;
      emit changed(this,EndChanged);
    }

  }
  else {

    //  if there are subitems, just change end if
    //  mode is not 'rubberband' and end is greater than _maxDateTime

    if(_mode != Rubberband) {

      if(end > _maxDateTime)
	_end = end;
      else
	_end = _maxDateTime;

      emit changed(this,EndChanged);

    }
    
  }
  
}




KGanttItem::Change
KGanttItem::adjustStartEnd()
//////////////////////////
{
  //  first update _min and _max of subitems

  Change c = adjustMinMax();

  if(_start > _minDateTime) {
    _start = _minDateTime;
    if(!(c & StartChanged))
      c = (Change) (c + StartChanged);
  }
  
  if(_end < _maxDateTime) {
    _end = _maxDateTime;
    if(!(c & EndChanged))
      c = (Change) (c + EndChanged);
  }  
  
  return c;
  
}



KGanttItem::Change 
KGanttItem::adjustMinMax()
//////////////////////////
{
  //
  //  calculate _min and _max by 
  //  traversing the subitems. if there are no subitems
  //  _min = start and _max = end.
  //

  TQDateTime min = _minDateTime;
  TQDateTime max = _maxDateTime;
  Change c = NoChange;

  if(_subitems.count()==0) {

    _minDateTime = _start;
    _maxDateTime = _end;

    if(min != _minDateTime) c  = MinChanged;
    if(max != _maxDateTime) c = (Change) (c + MaxChanged);

  }
  else {
    
    // get min/max date and time
    
    KGanttItem* item = _subitems.first();
    
    _minDateTime = item->getStart();
    _maxDateTime = item->getEnd();
    
    item = _subitems.next();
    
    for(; item != 0; item = _subitems.next() ) {
      
      if(_minDateTime > item->getStart()) {
	_minDateTime = item->getStart();
      }
      
      if(item->getEnd() > _maxDateTime) {
	_maxDateTime = item->getEnd();
      }
      
    } // for()
    
    
    if(min != _minDateTime) c  = MinChanged;
    if(max != _maxDateTime) c = (Change) (c + MaxChanged);
  
  }

  return c;

}



void 
KGanttItem::subItemChanged(KGanttItem* /*item*/, Change change)
/////////////////////////////////////////////////////
{  
  if(change & StyleChanged)
    emit changed(this, change);
  
  if( (change & Opened) || (change & Closed) || 
      (change & TotalHeightChanged) || (change & HeightChanged) )
    emit changed(this, TotalHeightChanged);

  if( (change & StartChanged) || 
      (change & EndChanged) ) {

    Change c = adjustStartEnd();
    
    if(_mode == Rubberband) {
      if(c & MinChanged && !(c & StartChanged)) 
	c = (Change) (c + StartChanged);
      if(c & MaxChanged && !(c & EndChanged))
	c = (Change) ( c +EndChanged);
    }

    if(c != NoChange)
      emit changed(this, c);
   
  }
}



void 
KGanttItem::setText(const TQString& text) 
///////////////////////////////////////
{ 
  if(!_editable) return;
  if(text != _text) {
    _text = text; 
    emit changed(this,TextChanged);
  }
}



void 
KGanttItem::open(bool f)
//////////////////////
{
  if(f != _open) {
    _open = f;
    if(_open)
      emit changed(this, Opened);
    else
      emit changed(this, Closed);
  }
}



void
KGanttItem::select(bool f)
///////////////////////
{
  if(!_editable) return;
  if(f != _selected) {
    _selected = f;
    if(_selected)
      emit changed(this, Selected);
    else
      emit changed(this, Unselected);
  }
}



void 
KGanttItem::setMode(Mode flag) 
////////////////////////////
{
  if(!_editable) return;
  if(_mode != flag) {
    _mode = flag;
    emit changed(this,ModeChanged);
  }
  
}



void
KGanttItem::setStyle(int flag, bool includeSubItems) 
///////////////////////////////////////////////
{
  if(!_editable) return;
  if(_style != flag) {

    _style = flag;
 
    if(includeSubItems)
      for(KGanttItem* item = _subitems.first(); 
	  item != 0; 
	  item = _subitems.next() )
	item->setStyle(flag,true);

    emit changed(this,StyleChanged);

  }

}



void
KGanttItem::setBrush(const TQBrush& brush)
///////////////////////////////////////
{
  _brush = brush;
}



void
KGanttItem::setPen(const TQPen& pen)
///////////////////////////////
{
  _pen = pen;
}



void
KGanttItem::setHeight(int h)
/////////////////////////
{
  if(!_editable) return;
  if(_height != h) {
    _height = h;
    emit changed(this,HeightChanged);
  }
}



int 
KGanttItem::getTotalHeight()
////////////////////////////////////////
{
  int h = _height;

  if( isOpen() ) {
    for(KGanttItem* item = _subitems.first(); item != 0; item = _subitems.next() ) {
      h += item->getTotalHeight();
    }
  }
  return h;
}



int
KGanttItem::getWidth()
//////////////////
{
  //  int width = _start.secsTo(_end)/60;

  int width = getStart().secsTo(getEnd())/60;
 
  // printf("width[%s] = %d \n", (const char*) getID(), width );

  return width;
}



void 
KGanttItem::dump(TQTextOStream& cout, const TQString& pre) 
////////////////////////////////////////////////////
{
  cout << pre << "<Item. text = [" << _text << "]>\n";
  cout << pre << "|  start : " << getStart().toString() << "  (" 
       <<_start.toString() << ")" << endl;
  cout << pre << "|  end :   " << getEnd().toString() << "  (" 
       <<_end.toString() << ")" << endl;
  if(_editable)
    cout << pre << "|    - editable " << endl;
  else
    cout << pre << "|    - not editable " << endl;
  if(_mode == Rubberband)
    cout << pre << "|  mode = 'rubberband'" << endl;
  else
    cout << pre << "|  mode = 'normal'" << endl;

  cout << pre << "|  min date/time : " << _minDateTime.toString() << endl;
  cout << pre << "|  max date/time : " << _maxDateTime.toString() << endl;
  
  for(KGanttItem* item = _subitems.first(); item != 0; item = _subitems.next() )
    item->dump(cout, pre + "|   ");
  
  for(KGanttRelation* rel = _relations.first(); 
      rel != 0; 
      rel = _relations.next() )
    rel->dump(cout, pre + "|   ");

  cout << pre << "</Item>\n";

}


TQString
KGanttItem::ChangeAsString(Change c)
//////////////////////////////////
{
  TQString ret;

  if(c & StartChanged)       ret += "StartChanged, ";
  if(c & EndChanged)         ret += "EndChanged,  ";
  if(c & HeightChanged)      ret += "HeightChanged,  ";
  if(c & TotalHeightChanged) ret += "TotalHeightChanged,  ";
  if(c & StyleChanged)       ret += "StyleChanged,  ";
  if(c & TextChanged)        ret += "TextChanged,  ";
  if(c & ModeChanged)        ret += "ModeChanged,  ";
  if(c & MinChanged)         ret += "MinChanged,  ";
  if(c & MaxChanged)         ret += "MaxChanged,  ";
  if(c & Opened)             ret += "Opened,  ";
  if(c & Closed)             ret += "Closed,  ";
  if(c & Selected)           ret += "Selected, ";
  if(c & Unselected)         ret += "Unselected, ";
  if(c & Unknown)            ret += "Unknown, ";
  return ret;

}
#include "KGanttItem.moc"