kgantt

KGanttItem.cpp
1 //
2 // file : KGanttItem.C
3 // date : 26 oct 2000
4 // changed : 11 jan 2001
5 // author : jh
6 //
7 
8 
9 #include "KGanttItem.h"
10 
11 
12 TQBrush KGanttItem::_selectBrush(TQColor(255,0,0));
13 
14 
15 KGanttItem::KGanttItem(KGanttItem* parentItem, const TQString& text,
16  const TQDateTime& start, const TQDateTime& end)
17  : TQObject()
19 {
20  init(parentItem,text, start,end);
21 }
22 
23 
24 
25 KGanttItem::KGanttItem(KGanttItem* parentItem, const TQString& text,
26  const TQDateTime& start, long durationMin)
27  : TQObject()
29 {
30  init(parentItem, text, start, start.addSecs( durationMin * 60));
31 }
32 
33 
34 
35 void
36 KGanttItem::init(KGanttItem* parentItem, const TQString& text,
37  const TQDateTime& start, const TQDateTime& end)
38 {
40  _style = DrawAll - DrawHandle;
41  _open = true;
42  _selected = false;
43  _editable = true;
44 
45  _mode = Normal;
46 
47  _brush = TQBrush(TQColor(140,140,255));
48  _pen = TQPen(TQColor(100,100,100));
49  _textPen = TQPen(TQColor(black));
50 
51  _height = 24;
52 
53  _text = text;
54 
55  _start = start; _minDateTime = start;
56  _end = end; _maxDateTime = end;
57 
58  _parentItem = parentItem;
59 
60  if(_parentItem)
61  _parentItem->registerItem(this);
62 
63 }
64 
65 
66 
69 {
70 #ifdef _DEBUG_
71  printf("-> delete %s \n", getText().latin1() );
72 #endif
73 
74  if(_parentItem)
75  _parentItem->unregisterItem(this);
76 
77  _subitems.setAutoDelete(true);
78  _subitems.clear();
79 
80  emit destroyed(this);
81 
82 #ifdef _DEBUG_
83  printf("<- delete %s \n", getText().latin1() );
84 #endif
85 }
86 
87 
88 
91  const TQString& text)
92 {
93  if(_subitems.containsRef(from) > 0 && _subitems.containsRef(to) >0) {
94  KGanttRelation* rel = new KGanttRelation(from,to,text);
95  _relations.append(rel);
96 
97  connect(rel, TQ_SIGNAL(destroyed(KGanttRelation*)),
98  this, TQ_SLOT(removeRelation(KGanttRelation*)));
99 
100  emit changed(this, RelationAdded);
101  return rel;
102  }
103  else
104  return NULL;
105 }
106 
107 
108 
109 void
110 KGanttItem::removeRelation(KGanttRelation* rel)
111 {
112  if( _relations.removeRef(rel) )
113  emit changed(this, RelationRemoved);
114 }
115 
116 
117 
118 void
121 {
122  blockSignals(false);
123  emit changed(this, Unknown);
124 }
125 
126 
127 
128 void
129 KGanttItem::registerItem(KGanttItem* item)
130 {
131  _subitems.append(item);
132 
133  connect(item, TQ_SIGNAL(changed(KGanttItem*, KGanttItem::Change)),
134  this, TQ_SLOT(subItemChanged(KGanttItem*, KGanttItem::Change)) );
135 
136  bool minChanged = false;
137  bool maxChanged = false;
138 
139  // update min/man
140 
141  if(_subitems.count() == 1) {
142 
143  _minDateTime = item->getStart();
144  _maxDateTime = item->getEnd();
145 
146  minChanged = true;
147  maxChanged = true;
148 
149  }
150  else {
151 
152  if(item->getEnd() > _maxDateTime) {
153  _maxDateTime = item->getEnd();
154  maxChanged = true;
155  }
156 
157  if(_minDateTime > item->getStart()) {
158  _minDateTime = item->getStart();
159  minChanged = true;
160  }
161 
162  } // else
163 
164 
165  // increase start/end if necessary
166  Change change = adjustStartEnd();
167 
168  if(_mode == Rubberband) {
169  if(minChanged && !(change & StartChanged))
170  change = (Change) (change + StartChanged);
171  if(maxChanged && !(change & EndChanged))
172  change = (Change) (change + EndChanged);
173  }
174 
175  if( isOpen() ) {
176  if(!(change & TotalHeightChanged))
177  change = (Change) (change + TotalHeightChanged);
178  }
179 
180  if(change != NoChange)
181  emit changed(this,change);
182 
183 }
184 
185 
186 
187 void
188 KGanttItem::unregisterItem(KGanttItem* item)
189 {
190  _subitems.removeRef(item);
191  disconnect(item);
192 
193  Change change = adjustMinMax();
194 
195  if( isOpen() ) {
196  if(!(change & TotalHeightChanged))
197  change = (Change) (change + TotalHeightChanged);
198  }
199 
200  if(change != NoChange)
201  emit changed(this,change);
202 
203 }
204 
205 
206 
207 TQDateTime
209 {
210  if(_mode == Rubberband && _subitems.count()>0)
211  return _minDateTime;
212  else
213  return _start;
214 }
215 
216 
217 
218 
219 TQDateTime
222 {
223  if(_mode == Rubberband && _subitems.count()>0)
224  return _maxDateTime;
225  else
226  return _end;
227 }
228 
229 
230 
231 void
232 KGanttItem::setStart(const TQDateTime& start)
233 {
234  if(!_editable) return;
235 
236  // if there are no subitems, just set _start and _minDateTime
237  if(_subitems.count()==0) {
238 
239  if(_start != start) {
240  _start = start;
241  _minDateTime = _start;
242  emit changed(this,StartChanged);
243  }
244 
245  }
246  else {
247 
248  // if there are subitems, just change start if
249  // mode is not 'rubberband' and start is less than _minDateTime
250 
251  if(_mode != Rubberband) {
252 
253  if(start < _minDateTime)
254  _start = start;
255  else
256  _start = _minDateTime;
257 
258  emit changed(this,StartChanged);
259 
260  }
261 
262  }
263 
264 }
265 
266 
267 
268 void
269 KGanttItem::setEnd(const TQDateTime& end)
270 {
271  if(!_editable) return;
272 
273  // if there are no subitems, just set _end and _maxDateTime
274  if(_subitems.count()==0) {
275 
276  if(_end != end) {
277  _end = end;
278  _maxDateTime = _end;
279  emit changed(this,EndChanged);
280  }
281 
282  }
283  else {
284 
285  // if there are subitems, just change end if
286  // mode is not 'rubberband' and end is greater than _maxDateTime
287 
288  if(_mode != Rubberband) {
289 
290  if(end > _maxDateTime)
291  _end = end;
292  else
293  _end = _maxDateTime;
294 
295  emit changed(this,EndChanged);
296 
297  }
298 
299  }
300 
301 }
302 
303 
304 
305 
307 KGanttItem::adjustStartEnd()
309 {
310  // first update _min and _max of subitems
311 
312  Change c = adjustMinMax();
313 
314  if(_start > _minDateTime) {
315  _start = _minDateTime;
316  if(!(c & StartChanged))
317  c = (Change) (c + StartChanged);
318  }
319 
320  if(_end < _maxDateTime) {
321  _end = _maxDateTime;
322  if(!(c & EndChanged))
323  c = (Change) (c + EndChanged);
324  }
325 
326  return c;
327 
328 }
329 
330 
331 
333 KGanttItem::adjustMinMax()
335 {
336  //
337  // calculate _min and _max by
338  // traversing the subitems. if there are no subitems
339  // _min = start and _max = end.
340  //
341 
342  TQDateTime min = _minDateTime;
343  TQDateTime max = _maxDateTime;
344  Change c = NoChange;
345 
346  if(_subitems.count()==0) {
347 
348  _minDateTime = _start;
349  _maxDateTime = _end;
350 
351  if(min != _minDateTime) c = MinChanged;
352  if(max != _maxDateTime) c = (Change) (c + MaxChanged);
353 
354  }
355  else {
356 
357  // get min/max date and time
358 
359  KGanttItem* item = _subitems.first();
360 
361  _minDateTime = item->getStart();
362  _maxDateTime = item->getEnd();
363 
364  item = _subitems.next();
365 
366  for(; item != 0; item = _subitems.next() ) {
367 
368  if(_minDateTime > item->getStart()) {
369  _minDateTime = item->getStart();
370  }
371 
372  if(item->getEnd() > _maxDateTime) {
373  _maxDateTime = item->getEnd();
374  }
375 
376  } // for()
377 
378 
379  if(min != _minDateTime) c = MinChanged;
380  if(max != _maxDateTime) c = (Change) (c + MaxChanged);
381 
382  }
383 
384  return c;
385 
386 }
387 
388 
389 
390 void
391 KGanttItem::subItemChanged(KGanttItem* /*item*/, Change change)
393 {
394  if(change & StyleChanged)
395  emit changed(this, change);
396 
397  if( (change & Opened) || (change & Closed) ||
398  (change & TotalHeightChanged) || (change & HeightChanged) )
399  emit changed(this, TotalHeightChanged);
400 
401  if( (change & StartChanged) ||
402  (change & EndChanged) ) {
403 
404  Change c = adjustStartEnd();
405 
406  if(_mode == Rubberband) {
407  if(c & MinChanged && !(c & StartChanged))
408  c = (Change) (c + StartChanged);
409  if(c & MaxChanged && !(c & EndChanged))
410  c = (Change) ( c +EndChanged);
411  }
412 
413  if(c != NoChange)
414  emit changed(this, c);
415 
416  }
417 }
418 
419 
420 
421 void
422 KGanttItem::setText(const TQString& text)
423 {
425  if(!_editable) return;
426  if(text != _text) {
427  _text = text;
428  emit changed(this,TextChanged);
429  }
430 }
431 
432 
433 
434 void
437 {
438  if(f != _open) {
439  _open = f;
440  if(_open)
441  emit changed(this, Opened);
442  else
443  emit changed(this, Closed);
444  }
445 }
446 
447 
448 
449 void
452 {
453  if(!_editable) return;
454  if(f != _selected) {
455  _selected = f;
456  if(_selected)
457  emit changed(this, Selected);
458  else
459  emit changed(this, Unselected);
460  }
461 }
462 
463 
464 
465 void
467 {
469  if(!_editable) return;
470  if(_mode != flag) {
471  _mode = flag;
472  emit changed(this,ModeChanged);
473  }
474 
475 }
476 
477 
478 
479 void
480 KGanttItem::setStyle(int flag, bool includeSubItems)
481 {
483  if(!_editable) return;
484  if(_style != flag) {
485 
486  _style = flag;
487 
488  if(includeSubItems)
489  for(KGanttItem* item = _subitems.first();
490  item != 0;
491  item = _subitems.next() )
492  item->setStyle(flag,true);
493 
494  emit changed(this,StyleChanged);
495 
496  }
497 
498 }
499 
500 
501 
502 void
503 KGanttItem::setBrush(const TQBrush& brush)
505 {
506  _brush = brush;
507 }
508 
509 
510 
511 void
512 KGanttItem::setPen(const TQPen& pen)
514 {
515  _pen = pen;
516 }
517 
518 
519 
520 void
523 {
524  if(!_editable) return;
525  if(_height != h) {
526  _height = h;
527  emit changed(this,HeightChanged);
528  }
529 }
530 
531 
532 
533 int
536 {
537  int h = _height;
538 
539  if( isOpen() ) {
540  for(KGanttItem* item = _subitems.first(); item != 0; item = _subitems.next() ) {
541  h += item->getTotalHeight();
542  }
543  }
544  return h;
545 }
546 
547 
548 
549 int
552 {
553  // int width = _start.secsTo(_end)/60;
554 
555  int width = getStart().secsTo(getEnd())/60;
556 
557  // printf("width[%s] = %d \n", (const char*) getID(), width );
558 
559  return width;
560 }
561 
562 
563 
564 void
565 KGanttItem::dump(TQTextOStream& cout, const TQString& pre)
566 {
568  cout << pre << "<Item. text = [" << _text << "]>\n";
569  cout << pre << "| start : " << getStart().toString() << " ("
570  <<_start.toString() << ")" << endl;
571  cout << pre << "| end : " << getEnd().toString() << " ("
572  <<_end.toString() << ")" << endl;
573  if(_editable)
574  cout << pre << "| - editable " << endl;
575  else
576  cout << pre << "| - not editable " << endl;
577  if(_mode == Rubberband)
578  cout << pre << "| mode = 'rubberband'" << endl;
579  else
580  cout << pre << "| mode = 'normal'" << endl;
581 
582  cout << pre << "| min date/time : " << _minDateTime.toString() << endl;
583  cout << pre << "| max date/time : " << _maxDateTime.toString() << endl;
584 
585  for(KGanttItem* item = _subitems.first(); item != 0; item = _subitems.next() )
586  item->dump(cout, pre + "| ");
587 
588  for(KGanttRelation* rel = _relations.first();
589  rel != 0;
590  rel = _relations.next() )
591  rel->dump(cout, pre + "| ");
592 
593  cout << pre << "</Item>\n";
594 
595 }
596 
597 
598 TQString
601 {
602  TQString ret;
603 
604  if(c & StartChanged) ret += "StartChanged, ";
605  if(c & EndChanged) ret += "EndChanged, ";
606  if(c & HeightChanged) ret += "HeightChanged, ";
607  if(c & TotalHeightChanged) ret += "TotalHeightChanged, ";
608  if(c & StyleChanged) ret += "StyleChanged, ";
609  if(c & TextChanged) ret += "TextChanged, ";
610  if(c & ModeChanged) ret += "ModeChanged, ";
611  if(c & MinChanged) ret += "MinChanged, ";
612  if(c & MaxChanged) ret += "MaxChanged, ";
613  if(c & Opened) ret += "Opened, ";
614  if(c & Closed) ret += "Closed, ";
615  if(c & Selected) ret += "Selected, ";
616  if(c & Unselected) ret += "Unselected, ";
617  if(c & Unknown) ret += "Unknown, ";
618  return ret;
619 
620 }
621 #include "KGanttItem.moc"
KGanttItem.
Definition: KGanttItem.h:56
TQDateTime getStart()
Get date of starting.
Definition: KGanttItem.cpp:208
static TQString ChangeAsString(Change c)
Return a given change as a string.
Definition: KGanttItem.cpp:599
void setEnd(const TQDateTime &end)
Set time/date of end.
Definition: KGanttItem.cpp:269
void setBrush(const TQBrush &brush)
Set brush for filling.
Definition: KGanttItem.cpp:503
void destroyed(KGanttItem *)
Item will be deleted.
~KGanttItem()
Destructor.
Definition: KGanttItem.cpp:67
void select(bool f)
Select/unselect item.
Definition: KGanttItem.cpp:450
bool isOpen()
Returns true if item is open (subitems has to be drawn)
Definition: KGanttItem.h:178
KGanttItem(KGanttItem *parentItem, const TQString &text, const TQDateTime &start, const TQDateTime &end)
Constructor.
Definition: KGanttItem.cpp:15
TQDateTime getEnd()
Get date of ending.
Definition: KGanttItem.cpp:220
void endTransaction()
End a transaction.
Definition: KGanttItem.cpp:119
void setText(const TQString &text)
Set text.
Definition: KGanttItem.cpp:422
TQString getText()
Get text.
Definition: KGanttItem.h:340
int getWidth()
Get width in minutes.
Definition: KGanttItem.cpp:550
@ HeightChanged
Height for this item has changed.
Definition: KGanttItem.h:71
@ StyleChanged
Style for drawing has changed.
Definition: KGanttItem.h:80
@ Unknown
Changes may occurred but the types are unknown.
Definition: KGanttItem.h:99
@ Selected
Item has been selected.
Definition: KGanttItem.h:93
@ Unselected
Item has been unselected.
Definition: KGanttItem.h:96
@ RelationRemoved
Relation between two subitems has been removed.
Definition: KGanttItem.h:105
@ Closed
Draw item without subitems.
Definition: KGanttItem.h:90
@ RelationAdded
Relation between two subitems has been added.
Definition: KGanttItem.h:102
@ TotalHeightChanged
Total height has changed.
Definition: KGanttItem.h:77
@ Opened
Draw item including subitems.
Definition: KGanttItem.h:87
void changed(KGanttItem *, KGanttItem::Change)
Item has changed.
void dump(TQTextOStream &cout, const TQString &pre)
Dump to cout.
Definition: KGanttItem.cpp:565
KGanttRelation * addRelation(KGanttItem *from, KGanttItem *to, const TQString &text)
Add relation between two subitems.
Definition: KGanttItem.cpp:90
void setMode(Mode flag)
Set mode.
Definition: KGanttItem.cpp:466
void setStyle(int flag, bool includeSubitems=false)
Set drawing style.
Definition: KGanttItem.cpp:480
void setHeight(int h)
Set height.
Definition: KGanttItem.cpp:521
void setPen(const TQPen &pen)
Set pen for border.
Definition: KGanttItem.cpp:512
void setStart(const TQDateTime &start)
Set time/date of start.
Definition: KGanttItem.cpp:232
void open(bool f)
Open / Close item.
Definition: KGanttItem.cpp:435
int getTotalHeight()
Get total height.
Definition: KGanttItem.cpp:534
KGanttRelation.
void dump(TQTextOStream &cout, const TQString &pre)
Dump to cout.