summaryrefslogtreecommitdiffstats
path: root/kdgantt/KDGanttViewTaskItem.cpp
blob: a89f23603f6830712253550507644aa8e1a059f0 (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
/* -*- Mode: C++ -*-
   $Id$
   KDGantt - a multi-platform charting engine
*/

/****************************************************************************
 ** Copyright (C)  2002-2004 Klarälvdalens Datakonsult AB.  All rights reserved.
 **
 ** This file is part of the KDGantt library.
 **
 ** This file may be distributed and/or modified under the terms of the
 ** GNU General Public License version 2 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.
 **
 ** Licensees holding valid commercial KDGantt licenses may use this file in
 ** accordance with the KDGantt Commercial License Agreement provided with
 ** the Software.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** See http://www.klaralvdalens-datakonsult.se/Public/products/ for
 **   information about KDGantt Commercial License Agreements.
 **
 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
 ** licensing are not clear to you.
 **
 ** As a special exception, permission is given to link this program
 ** with any edition of TQt, and distribute the resulting executable,
 ** without including the source code for TQt in the source distribution.
 **
 **********************************************************************/


#include "KDGanttViewTaskItem.h"
#include "KDGanttViewSubwidgets.h"

#include "itemAttributeDialog.h"

/*!
  \class KDGanttViewTaskItem KDGanttViewTaskItem.h

  This class represents calendar items in Gantt charts.

  A calendar item in a Gantt chart has no start/end tqshape,
  it is displayed as a rectangle.
  You can set the colors as usual, where only the first argument of
  setColors( col, col, col )
  is important.
  If the start time is equal to the end time, the item is displayed as
  ø, showing that there is no time interval set.

  For a KDGanttViewTaskItem, the text, setted by \a setText(),
  is shown in the item itself and the text is truncated automatically
  to fit in the item.
  For all other item types, the text is shown right of the item.
*/


/*!
  Constructs an empty Gantt item of type event.

  \param view the Gantt view to insert this item into
  \param lvtext the text to show in the listview
  \param name the name by which the item can be identified. If no name
  is specified, a unique name will be generated
*/
KDGanttViewTaskItem::KDGanttViewTaskItem( KDGanttView* view,
                                          const TQString& lvtext,
                                          const TQString& name ) :
    KDGanttViewItem( Task, view, lvtext, name )
{

  initItem();
}


/*!
  Constructs an empty Gantt item of type event.

  \param parent a parent item under which this one goes
  \param lvtext the text to show in the list view
  \param name the name by which the item can be identified. If no name
  is specified, a unique name will be generated
*/
KDGanttViewTaskItem::KDGanttViewTaskItem( KDGanttViewItem* parent,
                                          const TQString& lvtext,
                                          const TQString& name ) :
    KDGanttViewItem( Task, parent, lvtext, name )
{
  initItem();
}


/*!
  Constructs an empty Gantt item of type event.

  \param view the Gantt view to insert this item into
  \param after another item at the same level behind which this one should go
  \param lvtext the text to show in the list view
  \param name the name by which the item can be identified. If no name
  is specified, a unique name will be generated
*/
KDGanttViewTaskItem::KDGanttViewTaskItem( KDGanttView* view,
                                                KDGanttViewItem* after,
                                                const TQString& lvtext,
                                                const TQString& name ) :
    KDGanttViewItem( Task, view, after, lvtext, name )
{
  initItem();
}


/*!
  Constructs an empty Gantt item of type event.

  \param parent a parent item under which this one goes
  \param after another item at the same level behind which this one should go
  \param lvtext the text to show in the listview
  \param name the name by which the item can be identified. If no name
  is specified, a unique name will be generated
*/
KDGanttViewTaskItem::KDGanttViewTaskItem( KDGanttViewItem* parent,
                                          KDGanttViewItem* after,
                                          const TQString& lvtext,
                                          const TQString& name ) :
    KDGanttViewItem( Task, parent, after, lvtext, name )
{
  initItem();
}


/*!
  The destructor.
*/
KDGanttViewTaskItem::~KDGanttViewTaskItem()
{

}


/*!
  Specifies the end time of this item. The parameter must be valid
  and non-null. If the parameter is invalid or null, no value is set.
  If the end time is less the start time,
  the start time is set to this end time automatically.

  \param end the end time
  \sa setStartTime(), startTime(), endTime()
*/
void KDGanttViewTaskItem::setEndTime( const TQDateTime& end )
{
  myEndTime = end;
  if ( myEndTime < startTime() )
      setStartTime( myEndTime );
  else
    updateCanvasItems();
}


/*!
  Specifies the start time of this item. The parameter must be valid
  and non-null. If the parameter is invalid or null, no value is set.
  If the start time is greater than the end time,
  the end time is set to this start time automatically.

  \param start the start time
  \sa startTime(), setEndTime(), endTime()
*/
void KDGanttViewTaskItem::setStartTime( const TQDateTime& start )
{
  if (! start.isValid() ) {
    qDebug("KDGanttViewTaskItem::setStartTime():Invalid parameter-no time set");
    return;
  }
    myStartTime = start;
    if ( myStartTime > endTime() )
      setEndTime( myStartTime );
    else
      updateCanvasItems();
}


/*!
  Hides all canvas items of this Gantt item
  \sa showItem()
*/
void KDGanttViewTaskItem::hideMe()
{
    startShape->hide();
    progressShape->hide();
    textCanvas->hide();
    floatStartShape->hide();
    floatEndShape->hide();
}


void KDGanttViewTaskItem::showItem(bool show, int coordY)
{

  //qDebug("KDGanttViewTaskItem::showItem() %d %s ", (int) show, listViewText().latin1());
  isVisibleInGanttView = show;
  invalidateHeight () ;
  if (!show) {
    hideMe();
    return;
  }
  bool takedefaultHeight = true ; // pending: make configureable
  float prio = ((float) ( priority() - 100 )) / 100.0;
  startShape->setZ( prio );
  progressShape->setZ(startShape->z()+0.002); // less than textCanvas
  progressShape->hide();
  floatStartShape->setZ(startShape->z()+0.003); // less than textCanvas
  floatStartShape->hide();
  floatEndShape->setZ(startShape->z()+0.003); // less than textCanvas
  floatEndShape->hide();
  textCanvas->setZ( prio + 0.005 );
  if ( displaySubitemsAsGroup() && !parent() && !isOpen() ) {
    hideMe();
    return;
  }
  if ( displaySubitemsAsGroup()  && ( firstChild() || myGanttView->calendarMode() )  ) {
    hideMe();//new
    return;//new
    myStartTime = myChildStartTime();
    myEndTime = myChildEndTime();
  }
  //setExpandable(false);
  KDCanvasRectangle* temp = (KDCanvasRectangle*) startShape;
  KDCanvasRectangle* progtemp = (KDCanvasRectangle*) progressShape;
  int startX, endX, midX = 0,allY, progX=0;
   if ( coordY )
    allY = coordY;
  else
    allY = getCoordY();
  startX = myGanttView->myTimeHeader->getCoordX(myStartTime);
  endX = myGanttView->myTimeHeader->getCoordX(myEndTime);
  midX = endX;
  if (myProgress > 0) {
    progX = (endX - startX) * myProgress / 100;
  }
  int hei = height();
  if ( ! isVisible() ) {
    KDGanttViewItem * par = parent();
    while ( par != 0 && !par->isVisible() )
      par = par->parent();
    if ( par )
      hei = par->height();
  }
  if (myGanttView->myListView->itemAt( TQPoint(2, allY)))
     hei =  myGanttView->myListView->itemAt( TQPoint(2, allY))->height();
  if ( takedefaultHeight )
    hei = 16;
  if ( myStartTime == myEndTime ) {
    textCanvas->hide();
    if ( showNoInformation() ) {
      startShape->hide();
    } else {
      startShape->setZ( 1.01 );
      if (myGanttView->displayEmptyTasksAsLine() ) {
	hei = myGanttView->myTimeTable->height();
	if (hei  < myGanttView->myTimeTable->pendingHeight )
	  hei = myGanttView->myTimeTable->pendingHeight;
	temp->setSize(5,  hei  );
	temp->move(startX, 0);
	temp->show();
      } else {
	temp->setSize( 1,  hei -3 );
	temp->move(startX, allY-hei/2 +2);
	temp->show();
      }
    }
    return;
  }
  if ( startX +3 >= endX )
    temp->setSize( 3,  hei-3 );
  else
    temp->setSize(endX-startX,  hei-3 );
  temp->move(startX, allY-hei/2 +2);
  temp->show();
  if (progX > 0) {
    // FIXME: For now, just use inverted color for progress
    TQColor c = temp->brush().color();
    int h, s, v;
    c.getHsv(&h, &s, &v);
    h > 359/2 ? h -= 359/2 : h += 359/2;
    c.setHsv(h, s, v);
    progtemp->setBrush(TQBrush(c));
    
    progtemp->setSize(progX, hei-3);
    progtemp->move(temp->x(), temp->y());
    progtemp->show();
  }
  if (myFloatStartTime.isValid()) {
    KDCanvasRectangle* floatStartTemp = (KDCanvasRectangle*) floatStartShape;
    int floatStartX = myGanttView->myTimeHeader->getCoordX(myFloatStartTime);
    // FIXME: Configurable colors
    TQBrush b(temp->brush().color(), Dense4Pattern);
    floatStartTemp->setBrush(b);
    floatStartTemp->setPen(TQPen(gray));
    if (floatStartX < startX) {
        floatStartTemp->setSize(startX - floatStartX, temp->size().height()/2);
        floatStartTemp->move(floatStartX, temp->y() + temp->size().height()/4);
    } else {
        floatStartTemp->setSize(floatStartX - startX, temp->size().height()/2);
        floatStartTemp->move(startX, temp->y() + temp->size().height()/4);
    }
    floatStartTemp->show();    
  }
  if (myFloatEndTime.isValid()) {
    KDCanvasRectangle* floatEndTemp = (KDCanvasRectangle*) floatEndShape;
    int floatEndX = myGanttView->myTimeHeader->getCoordX(myFloatEndTime);
    // FIXME: Configurable colors
    TQBrush b(temp->brush().color(), Dense4Pattern);
    floatEndTemp->setBrush(b);
    floatEndTemp->setPen(TQPen(gray));
    int ex = startX + temp->size().width();
    if (floatEndX > ex) {
        floatEndTemp->setSize(floatEndX - ex, temp->size().height()/2);
        floatEndTemp->move(ex, temp->y() + temp->size().height()/4);
    } else {
        floatEndTemp->setSize(ex - floatEndX, temp->size().height()/2);
        floatEndTemp->move(floatEndX, temp->y() + temp->size().height()/4);
    }
    floatEndTemp->show();    
  }
  
  int wid = endX-startX - 4;
  if ( !displaySubitemsAsGroup() && !myGanttView->calendarMode()) {
    moveTextCanvas(endX,allY);
    textCanvas->show();
  } else {
    if ( textCanvasText.isEmpty()  || wid < 5)
      textCanvas->hide();
    else {
      textCanvas->move(startX+3, allY-textCanvas->boundingRect().height()/2);
      TQString temp = textCanvasText;
      textCanvas->setText(temp);
      int len =  temp.length();
      while ( textCanvas->boundingRect().width() > wid ) {
	temp.truncate(--len);
	textCanvas->setText(temp);
      }
      if ( temp.isEmpty())
	textCanvas->hide();
      else {
	textCanvas->show();
      }
    }
  }
}


void KDGanttViewTaskItem::initItem()
{
  isVisibleInGanttView = false;

  if ( myGanttView->calendarMode() && parent() ) {
    setVisible( false );
    parent()->setVisible( true );
  } else
    showItem(true);
  //qDebug("initItem  %s %s", listViewText().latin1(),startShape->brush().color().name().latin1() );
  myGanttView->myTimeTable->updateMyContent();
  setDragEnabled( myGanttView->dragEnabled() );
  setDropEnabled( myGanttView->dropEnabled() );
}