summaryrefslogtreecommitdiffstats
path: root/korganizer/timelineitem.cpp
blob: 649bcfa867fbef0f1b25c905670fcdc39a2670ad (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
/*
    Copyright (c) 2007 Volker Krause <[email protected]>

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "timelineitem.h"

#include "kohelper.h"

#define protected public
#include <kdgantt/KDGanttViewSubwidgets.h>
#undef public

#include <libkcal/calendar.h>
#include <libkcal/incidenceformatter.h>
#include <libkcal/resourcecalendar.h>

using namespace KOrg;
using namespace KCal;

TimelineItem::TimelineItem( const TQString &label, KCal::Calendar *calendar, KDGanttView * tqparent) :
    KDGanttViewTaskItem( tqparent ), mCalendar( calendar )
{
  setListViewText( 0, label );
  setDisplaySubitemsAsGroup( true );
  if ( listView() )
    listView()->setRootIsDecorated( false );
}

void TimelineItem::insertIncidence(KCal::Incidence * incidence, const TQDateTime & _start, const TQDateTime & _end)
{
  TQDateTime start = incidence->dtStart(), end = incidence->dtEnd();
  if ( _start.isValid() )
    start = _start;
  if ( _end.isValid() )
    end = _end;
  if ( incidence->doesFloat() )
    end = end.addDays( 1 );

  typedef TQValueList<TimelineSubItem*> ItemList;
  ItemList list = mItemMap[incidence];
  for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it )
    if ( (*it)->startTime() == start && (*it)->endTime() == end )
      return;

  TimelineSubItem * item = new TimelineSubItem( mCalendar, incidence, this );
  TQColor c1, c2, c3;
  colors( c1, c2, c3 );
  item->setColors( c1, c2, c3 );

  item->setStartTime( start );
  item->setOriginalStart( start );
  item->setEndTime( end );

  mItemMap[incidence].append( item );
}

void TimelineItem::removeIncidence(KCal::Incidence * incidence)
{
  typedef TQValueList<TimelineSubItem*> ItemList;
  ItemList list = mItemMap[incidence];
  for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it )
    delete *it;
  mItemMap.remove( incidence );
}

void TimelineItem::moveItems(KCal::Incidence * incidence, int delta, int duration)
{
  typedef TQValueList<TimelineSubItem*> ItemList;
  ItemList list = mItemMap[incidence];
  for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) {
    TQDateTime start = (*it)->originalStart();
    start = start.addSecs( delta );
    (*it)->setStartTime( start );
    (*it)->setOriginalStart( start );
    (*it)->setEndTime( start.addSecs( duration ) );
  }
}


TimelineSubItem::TimelineSubItem( KCal::Calendar *calendar,
                                  KCal::Incidence *incidence, TimelineItem *tqparent) :
    KDGanttViewTaskItem( tqparent ),
    mIncidence( incidence ),
    mLeft( 0 ),
    mRight( 0 ),
    mMarkerWidth( 0 )
{
  setTooltipText( IncidenceFormatter::toolTipStr( calendar, incidence,
                                                  originalStart().date(), true ) );
  if ( !incidence->isReadOnly() ) {
    setMoveable( true );
    setResizeable( true );
  }
}

TimelineSubItem::~TimelineSubItem()
{
  delete mLeft;
  delete mRight;
}

void TimelineSubItem::showItem(bool show, int coordY)
{
  KDGanttViewTaskItem::showItem( show, coordY );
  int y;
  if ( coordY != 0 )
    y = coordY;
  else
    y = getCoordY();
  int startX = myGanttView->timeHeaderWidget()->getCoordX(myStartTime);
  int endX = myGanttView->timeHeaderWidget()->getCoordX(myEndTime);

  const int mw = TQMAX( 1, TQMIN( 4, endX - startX ) );
  if ( !mLeft || mw != mMarkerWidth ) {
    if ( !mLeft ) {
      mLeft = new KDCanvasPolygon( myGanttView->timeTableWidget(), this, Type_is_KDGanttViewItem );
      mLeft->setBrush( TQt::black );
    }
    TQPointArray a = TQPointArray( 4 );
    a.setPoint( 0, 0, -mw -myItemSize/2 - 2 );
    a.setPoint( 1, mw, -myItemSize/2 - 2 );
    a.setPoint( 2, mw, myItemSize/2 + 2 );
    a.setPoint( 3, 0, myItemSize/2 + mw + 2 );
    mLeft->setPoints( a );
  }
  if ( !mRight || mw != mMarkerWidth ) {
    if ( !mRight ) {
      mRight = new KDCanvasPolygon( myGanttView->timeTableWidget(), this, Type_is_KDGanttViewItem );
      mRight->setBrush( TQt::black );
    }
    TQPointArray a = TQPointArray( 4 );
    a.setPoint( 0, -mw, -myItemSize/2 - 2 );
    a.setPoint( 1, 0, -myItemSize/2 - mw - 2 );
    a.setPoint( 2, 0, myItemSize/2 + mw + 2 );
    a.setPoint( 3, -mw, myItemSize/2 + 2 );
    mRight->setPoints( a );
  }
  mMarkerWidth = mw;
  mLeft->setX( startX );
  mLeft->setY( y );
  mLeft->setZ( startShape->z() - 1 );
  mLeft->show();
  mRight->setX( endX );
  mRight->setY( y );
  mRight->setZ( startShape->z() - 1 );
  mRight->show();
}