timelineitem.cpp
00001 /* 00002 Copyright (c) 2007 Volker Krause <vkrause@kde.org> 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "timelineitem.h" 00020 00021 #include "kohelper.h" 00022 00023 #define protected public 00024 #include <kdgantt/KDGanttViewSubwidgets.h> 00025 #undef public 00026 00027 #include <libkcal/calendar.h> 00028 #include <libkcal/incidenceformatter.h> 00029 #include <libkcal/resourcecalendar.h> 00030 00031 using namespace KOrg; 00032 using namespace KCal; 00033 00034 TimelineItem::TimelineItem( const TQString &label, KCal::Calendar *calendar, KDGanttView * parent) : 00035 KDGanttViewTaskItem( parent ), mCalendar( calendar ) 00036 { 00037 setListViewText( 0, label ); 00038 setDisplaySubitemsAsGroup( true ); 00039 if ( listView() ) 00040 listView()->setRootIsDecorated( false ); 00041 } 00042 00043 void TimelineItem::insertIncidence(KCal::Incidence * incidence, const TQDateTime & _start, const TQDateTime & _end) 00044 { 00045 TQDateTime start = incidence->dtStart(), end = incidence->dtEnd(); 00046 if ( _start.isValid() ) 00047 start = _start; 00048 if ( _end.isValid() ) 00049 end = _end; 00050 if ( incidence->doesFloat() ) 00051 end = end.addDays( 1 ); 00052 00053 typedef TQValueList<TimelineSubItem*> ItemList; 00054 ItemList list = mItemMap[incidence]; 00055 for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) 00056 if ( (*it)->startTime() == start && (*it)->endTime() == end ) 00057 return; 00058 00059 TimelineSubItem * item = new TimelineSubItem( mCalendar, incidence, this ); 00060 TQColor c1, c2, c3; 00061 colors( c1, c2, c3 ); 00062 item->setColors( c1, c2, c3 ); 00063 00064 item->setStartTime( start ); 00065 item->setOriginalStart( start ); 00066 item->setEndTime( end ); 00067 00068 mItemMap[incidence].append( item ); 00069 } 00070 00071 void TimelineItem::removeIncidence(KCal::Incidence * incidence) 00072 { 00073 typedef TQValueList<TimelineSubItem*> ItemList; 00074 ItemList list = mItemMap[incidence]; 00075 for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) 00076 delete *it; 00077 mItemMap.remove( incidence ); 00078 } 00079 00080 void TimelineItem::moveItems(KCal::Incidence * incidence, int delta, int duration) 00081 { 00082 typedef TQValueList<TimelineSubItem*> ItemList; 00083 ItemList list = mItemMap[incidence]; 00084 for ( ItemList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) { 00085 TQDateTime start = (*it)->originalStart(); 00086 start = start.addSecs( delta ); 00087 (*it)->setStartTime( start ); 00088 (*it)->setOriginalStart( start ); 00089 (*it)->setEndTime( start.addSecs( duration ) ); 00090 } 00091 } 00092 00093 00094 TimelineSubItem::TimelineSubItem( KCal::Calendar *calendar, 00095 KCal::Incidence *incidence, TimelineItem *parent) : 00096 KDGanttViewTaskItem( parent ), 00097 mIncidence( incidence ), 00098 mLeft( 0 ), 00099 mRight( 0 ), 00100 mMarkerWidth( 0 ) 00101 { 00102 setTooltipText( IncidenceFormatter::toolTipStr( calendar, incidence, 00103 originalStart().date(), true ) ); 00104 if ( !incidence->isReadOnly() ) { 00105 setMoveable( true ); 00106 setResizeable( true ); 00107 } 00108 } 00109 00110 TimelineSubItem::~TimelineSubItem() 00111 { 00112 delete mLeft; 00113 delete mRight; 00114 } 00115 00116 void TimelineSubItem::showItem(bool show, int coordY) 00117 { 00118 KDGanttViewTaskItem::showItem( show, coordY ); 00119 int y; 00120 if ( coordY != 0 ) 00121 y = coordY; 00122 else 00123 y = getCoordY(); 00124 int startX = myGanttView->timeHeaderWidget()->getCoordX(myStartTime); 00125 int endX = myGanttView->timeHeaderWidget()->getCoordX(myEndTime); 00126 00127 const int mw = TQMAX( 1, TQMIN( 4, endX - startX ) ); 00128 if ( !mLeft || mw != mMarkerWidth ) { 00129 if ( !mLeft ) { 00130 mLeft = new KDCanvasPolygon( myGanttView->timeTableWidget(), this, Type_is_KDGanttViewItem ); 00131 mLeft->setBrush( TQt::black ); 00132 } 00133 TQPointArray a = TQPointArray( 4 ); 00134 a.setPoint( 0, 0, -mw -myItemSize/2 - 2 ); 00135 a.setPoint( 1, mw, -myItemSize/2 - 2 ); 00136 a.setPoint( 2, mw, myItemSize/2 + 2 ); 00137 a.setPoint( 3, 0, myItemSize/2 + mw + 2 ); 00138 mLeft->setPoints( a ); 00139 } 00140 if ( !mRight || mw != mMarkerWidth ) { 00141 if ( !mRight ) { 00142 mRight = new KDCanvasPolygon( myGanttView->timeTableWidget(), this, Type_is_KDGanttViewItem ); 00143 mRight->setBrush( TQt::black ); 00144 } 00145 TQPointArray a = TQPointArray( 4 ); 00146 a.setPoint( 0, -mw, -myItemSize/2 - 2 ); 00147 a.setPoint( 1, 0, -myItemSize/2 - mw - 2 ); 00148 a.setPoint( 2, 0, myItemSize/2 + mw + 2 ); 00149 a.setPoint( 3, -mw, myItemSize/2 + 2 ); 00150 mRight->setPoints( a ); 00151 } 00152 mMarkerWidth = mw; 00153 mLeft->setX( startX ); 00154 mLeft->setY( y ); 00155 mLeft->setZ( startShape->z() - 1 ); 00156 mLeft->show(); 00157 mRight->setX( endX ); 00158 mRight->setY( y ); 00159 mRight->setZ( startShape->z() - 1 ); 00160 mRight->show(); 00161 }