kdgantt

KDGanttViewSubwidgets.cpp

00001 /* -*- Mode: C++ -*-
00002    $Id$
00003    KDGantt - a multi-platform charting engine
00004 */
00005 
00006 /****************************************************************************
00007  ** Copyright (C)  2002-2004 Klarälvdalens Datakonsult AB.  All rights reserved.
00008  **
00009  ** This file is part of the KDGantt library.
00010  **
00011  ** This file may be distributed and/or modified under the terms of the
00012  ** GNU General Public License version 2 as published by the Free Software
00013  ** Foundation and appearing in the file LICENSE.GPL included in the
00014  ** packaging of this file.
00015  **
00016  ** Licensees holding valid commercial KDGantt licenses may use this file in
00017  ** accordance with the KDGantt Commercial License Agreement provided with
00018  ** the Software.
00019  **
00020  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00021  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00022  **
00023  ** See http://www.klaralvdalens-datakonsult.se/Public/products/ for
00024  **   information about KDGantt Commercial License Agreements.
00025  **
00026  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00027  ** licensing are not clear to you.
00028  **
00029  ** As a special exception, permission is given to link this program
00030  ** with any edition of TQt, and distribute the resulting executable,
00031  ** without including the source code for TQt in the source distribution.
00032  **
00033  **********************************************************************/
00034 
00035 
00036 #include "KDGanttViewSubwidgets.h"
00037 #include "KDGanttViewEventItem.h"
00038 #include "KDGanttViewSummaryItem.h"
00039 #include "KDGanttViewTaskItem.h"
00040 #ifndef KDGANTT_MASTER_CVS
00041 #include "KDGanttViewSubwidgets.moc"
00042 #endif
00043 
00044 #include <tqlabel.h>
00045 #include <tqheader.h>
00046 #include <tqpainter.h>
00047 #include <tqrect.h>
00048 #include <tqtooltip.h>
00049 #include <tqapplication.h>
00050 #include <tqdrawutil.h>
00051 #include <tqpalette.h>
00052 #include <tqdragobject.h>
00053 #include <tqptrlist.h>
00054 #include <tqpen.h>
00055 
00056 #include <tdeglobal.h>
00057 #include <tdelocale.h>
00058 #include <kcalendarsystem.h>
00059 #include <kdebug.h>
00060 
00061 KDTimeTableWidget:: KDTimeTableWidget( TQWidget* parent,KDGanttView* myGantt)
00062     : TQCanvas (TQT_TQOBJECT(parent))
00063 {
00064     myGanttView = myGantt;
00065     taskLinksVisible = true;
00066     flag_blockUpdating = false;
00067     int_blockUpdating = 0;
00068     gridPen.setStyle(Qt::DotLine);
00069     gridPen.setColor(TQColor(100,100,100));
00070     maximumComputedGridHeight = 0;
00071     denseLineCount = 0;
00072     denseLineBrush = TQBrush( TQColor ( 240,240,240 ));
00073     noInfoLineBrush = TQBrush(  TQColor ( 100,100,100 ), TQt::FDiagPattern );
00074     pendingHeight = 0;
00075     pendingWidth = 0;
00076     retune(256);
00077     resize(1,1);
00078 }
00079 
00080 TQPtrList<KDGanttViewTaskLink> KDTimeTableWidget::taskLinks()
00081 {
00082     return myTaskLinkList;
00083 }
00084 
00085 void KDTimeTableWidget::clearTaskLinks()
00086 {
00087     // cannot use clear() here, as tasklinks will remove themselves from my list when deleted!
00088     TQPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
00089     while (it.current()) {
00090         delete it.current();
00091     }
00092 
00093 }
00094 
00095 void KDTimeTableWidget::resetWidth( int wid )
00096 {
00097     if ( wid == width() ) {
00098         if (pendingHeight)
00099             pendingWidth = wid;
00100         else
00101             pendingWidth = 0;
00102         return;
00103     }
00104     if ( ! pendingHeight )
00105         pendingHeight = height();
00106     pendingWidth = wid;
00107     updateMyContent();
00108 }
00109 
00110 void KDTimeTableWidget::checkHeight( int hei )
00111 {
00112     if( hei < height() )
00113         return;
00114     if ( pendingHeight < hei+100)
00115         pendingHeight = hei+100;
00116     if ( !  pendingWidth )
00117         pendingWidth = width();
00118     maximumComputedGridHeight = 0; //force recomputing all
00119     updateMyContent();
00120 }
00121 
00122 
00123 void KDTimeTableWidget::setNoInformationBrush( const TQBrush& brush )
00124 {
00125     noInfoLineBrush = brush;
00126     updateMyContent();
00127 }
00128 TQBrush KDTimeTableWidget::noInformationBrush() const
00129 {
00130     return noInfoLineBrush;
00131 }
00132 
00133 void KDTimeTableWidget::removeItemFromTasklinks( KDGanttViewItem* item)
00134 {
00135     TQPtrListIterator<KDGanttViewTaskLink> it((myTaskLinkList));
00136     for ( ; it.current(); ++it ) {
00137         it.current()->removeItemFromList( item );
00138     }
00139 }
00140 
00141 void KDTimeTableWidget::expandItem( TQListViewItem * item)
00142 {
00143     item->invalidateHeight () ;
00144     //tqApp->processEvents();
00145     updateMyContent();
00146 }
00147 void KDTimeTableWidget::collapseItem( TQListViewItem * item)
00148 {
00149     item->invalidateHeight () ;
00150     //tqApp->processEvents();
00151     updateMyContent();
00152 }
00153 
00154 void KDTimeTableWidget::highlightItem( TQListViewItem * item )
00155 {
00156     static bool itemwashighlighted;
00157     static KDGanttViewItem* highlightedItem = 0;
00158     if (highlightedItem)
00159         highlightedItem->setHighlight(itemwashighlighted);
00160     highlightedItem = ( KDGanttViewItem*)item;
00161     itemwashighlighted = highlightedItem->highlight();
00162     highlightedItem->setHighlight(true);
00163     item->invalidateHeight () ;
00164     myGanttView->myListView->contentsY();
00165     updateMyContent();
00166 }
00167 int  KDTimeTableWidget::computeHeight()
00168 {
00169     // compute height of ListView
00170     // show only items shown in ListView
00171     int hei = 0;
00172     KDGanttViewItem* temp;
00173     temp = myGanttView->firstChild();
00174     while (temp) {
00175         hei += temp->computeHeight();
00176         temp = temp->nextSibling();
00177     }
00178     // set hei  to 1 to avoid canavs to be a null pixmap
00179     if (hei == 0) {
00180         hei = 1;
00181     }
00182     //tqDebug("COMPUTED HEI %d ", hei);
00183     emit heightComputed( hei );
00184     return hei;
00185 }
00186 void KDTimeTableWidget::computeVerticalGrid()
00187 {
00188     // recompute the vertical grid
00189     // compute the vertical grid
00190     // if we have too much lines, hide them
00191     //tqDebug("computeVerticalGrid() ");
00192     int cw =  myGanttView->myTimeHeader->myGridMinorWidth;
00193     int i = 0;
00194     int h ;
00195     if (pendingHeight > height() )
00196         h = pendingHeight;
00197     else
00198         h = height();
00199     int wid;
00200     if ( pendingWidth )
00201         wid = pendingWidth;
00202     else
00203         wid = width();
00204     KDCanvasLine* templine;
00205     KDCanvasRectangle* temprect;
00206     TQColor colcol;
00207     TQPen colPen;
00208     bool colorIterator = true;
00209 
00210 
00211     if (myGanttView->showMinorTicks()){//minor
00212         colPen.setWidth(cw);
00213         TQPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00214         TQPtrListIterator<KDCanvasLine> itgrid(verGridList);
00215         for ( ; itgrid.current(); ++itgrid ) {
00216             if (i < wid) {
00217                 itgrid.current()->setPoints(i,0,i,h);
00218                 itgrid.current()->show();
00219 
00220                 if (myGanttView->myTimeHeader->getColumnColor(colcol,i,i+cw))
00221                     {
00222 
00223                         colPen.setColor(colcol);
00224                         if (colorIterator)
00225                             colorIterator = itcol.current();
00226                         if (colorIterator)
00227                             {/*
00228                                itcol.current()->setPen(colPen);
00229                                itcol.current()->setPoints(i+(cw/2),0,i+(cw/2),h);
00230                              */
00231 
00232                                 itcol.current()->setPen( TQPen(TQPen::NoPen) );
00233                                 itcol.current()->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00234                                 itcol.current()->setSize(cw ,h );
00235                                 itcol.current()->move( i, 0 );
00236                                 itcol.current()->show();
00237                                 ++itcol;
00238                             } else {
00239 
00240                                 /*
00241                                   templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00242                                   templine->setPen(colPen);
00243                                   templine->setPoints(i+(cw/2),0,i+(cw/2),h);
00244                                 */
00245                                 temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00246                                 temprect->setPen( TQPen(TQPen::NoPen) );
00247                                 temprect->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00248                                 temprect->setSize(cw ,h );
00249                                 temprect->move( i, 0 );
00250                                 temprect->setZ(-20);
00251                                 temprect->show();
00252                                 columnColorList.append(temprect);
00253                             }
00254                     }
00255                 i += cw;
00256             } else {
00257                 itgrid.current()->hide();
00258             }
00259         }
00260         // create additional Lines for vertical grid
00261         for ( ;i < wid;i += cw) {
00262             templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00263             templine->setPen(gridPen);
00264             templine->setPoints(i,0,i,h);
00265             templine->setZ(0);
00266             templine->show();
00267             verGridList.append(templine);
00268             if (myGanttView->myTimeHeader->getColumnColor(colcol,i,i+cw))
00269                 {
00270                     colPen.setColor(colcol);
00271                     if (colorIterator)
00272                         colorIterator = itcol.current();
00273                     if (colorIterator)
00274                         {/*
00275                            itcol.current()->setPen(colPen);
00276                            itcol.current()->setPoints(i+(cw/2),0,i+(cw/2),h);
00277                          */
00278                             itcol.current()->setPen( TQPen(TQPen::NoPen) );
00279                             itcol.current()->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00280                             itcol.current()->setSize(cw ,h );
00281                             itcol.current()->move( i, 0 );
00282                             itcol.current()->show();
00283                             ++itcol;
00284                         } else {
00285                             temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00286                             temprect->setPen( TQPen(TQPen::NoPen) );
00287                             temprect->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00288                             temprect->setSize(cw ,h );
00289                             temprect->move( i, 0 );
00290                             temprect->setZ(-20);
00291                             temprect->show();
00292                             columnColorList.append(temprect);
00293                             /*
00294                               templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00295                               templine->setPen(colPen);
00296                               templine->setPoints(i+(cw/2),0,i+(cw/2),h);
00297                               templine->setZ(-20);
00298                               templine->show();
00299                               columnColorList.append(templine);
00300                             */
00301                         }
00302                 }
00303         }
00304         if (colorIterator)
00305             for ( ; itcol.current(); ++itcol )
00306                 itcol.current()->hide();
00307     } else {//major
00308         if (myGanttView->showMajorTicks()) {
00309             TQValueList<int>::iterator intIt = myGanttView->myTimeHeader->majorTicks.begin();
00310             TQValueList<int>::iterator intItEnd = myGanttView->myTimeHeader->majorTicks.end();
00311             TQPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00312             TQPtrListIterator<KDCanvasLine> itgrid(verGridList);
00313             int left = 0;
00314             for ( ; itgrid.current(); ++itgrid ) {
00315                 if (intIt != intItEnd) {
00316                     left = (*intIt);
00317                     ++intIt;
00318                     itgrid.current()->setPoints(left,0,left,h);
00319                     itgrid.current()->show();
00320                     //int right = (*intIt);
00321                     if ((*intIt))
00322                         if (myGanttView->myTimeHeader->getColumnColor(colcol,left,(*intIt) ))
00323                             {
00324                                 int mid = (-left+(*intIt));
00325                                 colPen.setColor(colcol);
00326                                 colPen.setWidth((*intIt)-left);
00327                                 if (colorIterator)
00328                                     colorIterator = itcol.current();
00329                                 if (colorIterator)
00330                                     {/*
00331                                        itcol.current()->setPen(colPen);
00332                                        itcol.current()->setPoints(i+mid,0,mid,h);
00333                                      */
00334                                         itcol.current()->setPen( TQPen(TQPen::NoPen) );
00335                                         itcol.current()->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00336                                         itcol.current()->setSize(mid ,h );
00337                                         itcol.current()->move( left, 0 );
00338                                         itcol.current()->show();
00339                                         ++itcol;
00340                                     } else {
00341                                         temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00342                                         temprect->setPen( TQPen(TQPen::NoPen) );
00343                                         temprect->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00344                                         temprect->setSize(mid,h );
00345                                         temprect->move( left, 0 );
00346                                         temprect->setZ(-20);
00347                                         temprect->show();
00348                                         columnColorList.append(temprect);
00349                                         /*
00350                                           templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00351                                           templine->setPen(colPen);
00352                                           templine->setPoints(mid,0,i+mid,h);
00353                                           templine->setZ(-20);
00354                                           templine->show();
00355                                           columnColorList.append(templine);
00356                                         */
00357 
00358                                     }
00359                             }
00360 
00361                 } else {
00362                     itgrid.current()->hide();
00363                 }
00364             }
00365             KDCanvasLine* templine;
00366             // create additional Lines for vertical grid
00367             for ( ;intIt != intItEnd  ;++intIt) {
00368 
00369                 templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00370                 templine->setPen(gridPen);
00371                 templine->setPoints((*intIt),0,(*intIt),h);
00372                 templine->setZ(0);
00373                 templine->show();
00374                 verGridList.append(templine);
00375                 if ((*intIt))
00376                     if (myGanttView->myTimeHeader->getColumnColor(colcol,left,(*intIt)))
00377                         {
00378                             int mid = (-left+(*intIt));
00379                             colPen.setColor(colcol);
00380                             colPen.setWidth((*intIt)-left);
00381                             if (colorIterator)
00382                                 colorIterator = itcol.current();
00383                             if (colorIterator)
00384                                 {/*
00385                                    itcol.current()->setPen(colPen);
00386                                    itcol.current()->setPoints(i+mid,0,mid,h);
00387                                  */
00388                                     itcol.current()->setPen( TQPen(TQPen::NoPen) );
00389                                     itcol.current()->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00390                                     itcol.current()->setSize(mid ,h );
00391                                     itcol.current()->move( left, 0 );
00392                                     itcol.current()->show();
00393                                     ++itcol;
00394                                 } else {
00395                                     temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00396                                     temprect->setPen( TQPen(TQPen::NoPen) );
00397                                     temprect->setBrush( TQBrush( colcol, Qt::SolidPattern) );
00398                                     temprect->setSize(mid ,h );
00399                                     temprect->move( left, 0 );
00400                                     temprect->setZ(-20);
00401                                     temprect->show();
00402                                     columnColorList.append(temprect);
00403                                     /*
00404                                       templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00405                                       templine->setPen(colPen);
00406                                       templine->setPoints(mid,0,i+mid,h);
00407                                       templine->setZ(-20);
00408                                       templine->show();
00409                                       columnColorList.append(templine);
00410                                     */
00411                                 }
00412                         }
00413                 left = (*intIt);
00414             }
00415             if (colorIterator)
00416                 for ( ; itcol.current(); ++itcol ) {
00417                     itcol.current()->hide();
00418                 }
00419 
00420         }
00421         else {
00422             //hideall
00423             TQPtrListIterator<KDCanvasLine> itgrid(verGridList);
00424             for ( ; itgrid.current(); ++itgrid ) {
00425                 itgrid.current()->hide();
00426             }
00427             TQPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00428             for ( ; itcol.current(); ++itcol ) {
00429                 itcol.current()->hide();
00430             }
00431         }
00432     }
00433 }
00434 void KDTimeTableWidget::computeHorizontalGrid()
00435 {
00436     // compute  horizontal grid
00437     //tqDebug("computeHorizontalGrid() ");
00438     KDGanttViewItem* temp = myGanttView->firstChild();
00439     int wid;
00440     if ( pendingWidth )
00441         wid = pendingWidth;
00442     else
00443         wid = width();
00444     KDCanvasLine* templine;
00445     TQPtrListIterator<KDCanvasLine> ithor(horGridList);
00446     if ( ithor.current() ) {
00447         templine = ithor.current();
00448         ++ithor;
00449     } else {
00450         templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00451         templine->setPen(gridPen);
00452         templine->setZ(0);
00453         horGridList.append(templine);
00454     }
00455     templine->setPoints(0,0,wid,0);
00456     templine->show();
00457     int posY;
00458     while ( temp ) {
00459         posY = temp->itemPos() + temp->height();
00460         if ( ithor.current() ) {
00461             templine = ithor.current();
00462             ++ithor;
00463         } else {
00464             //new vertical grid line
00465             templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00466             templine->setPen(gridPen);
00467             templine->setZ(0);
00468             horGridList.append(templine);
00469         }
00470         if ( templine->endPoint() != TQPoint(wid,posY ))
00471             templine->setPoints(0,posY,wid,posY );
00472         if ( !templine->isVisible() )
00473             templine->show();
00474         //TQString ts = "asGroup";
00475         //if (!temp->displaySubitemsAsGroup() )
00476         //  ts = " NOT asGroup";
00477         //tqDebug("temp name %s %s", temp->listViewText(0).latin1(), ts.latin1());
00478 
00479         temp = temp->itemBelow ();
00480     }
00481     while ( ithor.current() ) {
00482         if ( ithor.current()->isVisible() )
00483             ithor.current()->hide();
00484         ++ithor;
00485     }
00486 }
00487 
00488 void KDTimeTableWidget::computeDenseLines()
00489 {
00490     KDGanttViewItem* temp = myGanttView->firstChild();
00491     int wid;
00492     if ( pendingWidth )
00493         wid = pendingWidth;
00494     else
00495         wid = width();
00496     TQPtrListIterator<KDCanvasRectangle> ithordense(horDenseList);
00497     KDCanvasRectangle* denseLine;
00498     int tempDenseLineCount = 0;
00499     while ( temp ) {
00500         if ( temp->isVisible() ) {
00501             ++tempDenseLineCount;
00502             if ( tempDenseLineCount == denseLineCount ) {
00503                 tempDenseLineCount = 0;
00504                 if ( ithordense.current() ) {
00505                     denseLine = ithordense.current();
00506                     ++ithordense;
00507                 } else {
00508                     denseLine =new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00509                     denseLine->setZ(-2);
00510                     horDenseList.append( denseLine );
00511                 }
00512                 if ( denseLine->rect() != TQRect(0, temp->itemPos(),wid, temp->height()) ) {
00513                     denseLine->move( 0, temp->itemPos() );
00514                     denseLine->setSize( wid, temp->height());
00515                 }
00516                 if (denseLine->brush() != denseLineBrush ) {
00517                     denseLine->setPen( TQPen(  TQt::NoPen ) );
00518                     denseLine->setBrush( denseLineBrush);
00519                 }
00520                 if (!denseLine->isVisible() )
00521                     denseLine->show();
00522 
00523             } else {
00524                 ;
00525             }
00526         }
00527         temp = temp->itemBelow ();
00528     }
00529     while ( ithordense.current() ) {
00530         if ( ithordense.current()->isVisible() ) {
00531             ithordense.current()->hide();
00532         }
00533         ++ithordense;
00534     }
00535 }
00536 void KDTimeTableWidget::computeShowNoInformation()
00537 {
00538     KDGanttViewItem* temp = myGanttView->firstChild();
00539     int wid;
00540     if ( pendingWidth )
00541         wid = pendingWidth;
00542     else
00543         wid = width();
00544     TQPtrListIterator<KDCanvasRectangle> itnoinfo(showNoInfoList);
00545     KDCanvasRectangle* noInfoLine;
00546     while ( temp ) {
00547         if ( temp->showNoInformation() ) {
00548             if ( itnoinfo.current() ) {
00549                 noInfoLine = itnoinfo.current();
00550                 ++itnoinfo;
00551             } else {
00552                 noInfoLine =new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00553                 showNoInfoList.append( noInfoLine );
00554                 noInfoLine->setZ(-1);
00555             }
00556             noInfoLine->move( 0, temp->itemPos() );
00557             noInfoLine->setSize( wid, temp->height());
00558             noInfoLine->setPen( TQPen(  TQt::NoPen ) );
00559             noInfoLine->setBrush( noInfoLineBrush);
00560             noInfoLine->show();
00561         }
00562         temp = temp->itemBelow ();
00563     }
00564     while ( itnoinfo.current() ) {
00565         itnoinfo.current()->hide();
00566         ++itnoinfo;
00567     }
00568 
00569 }
00570 
00571 void KDTimeTableWidget::computeTaskLinks()
00572 {
00573     //compute and show tasklinks
00574     TQPtrListIterator<KDGanttViewTaskLink> it((myTaskLinkList));
00575     for ( ; it.current(); ++it ) {
00576         if (it.current()->isVisible())
00577             it.current()->showMe(true);
00578         else
00579             it.current()->showMe(false);
00580     }
00581 }
00582 
00583 // updateMyContent() can be blocked by blockUpdating( true ) or inc_blockUpdating()
00584 // updateMyContent() is blocked, if the GanttView is hidden after startup
00585 
00586 void KDTimeTableWidget::updateMyContent()
00587 {
00588     if ( flag_blockUpdating || int_blockUpdating ) {
00589         // tqDebug("KDTimeTableWidget::updateMyContent() blocked! ");
00590         return;
00591     }
00592     //tqDebug("KDTimeTableWidget::updateMyContent() ********************************* ");
00593     /*
00594     // debug output
00595     KDGanttViewItem* temp =  myGanttView->firstChild();
00596     while (temp != 0) {
00597     temp->printinfo("  " );
00598     temp = temp->nextSibling();
00599     }
00600     */
00601     int hei = computeHeight();
00602     minimumHeight = hei;
00603     int viewport_hei = myGanttView->myCanvasView->viewport()->height();
00604     if ( viewport_hei > hei )
00605         hei = viewport_hei + 100;
00606     if ( myGanttView->myTimeHeader->registerStartTime() )
00607         return; // try again via timeheader computeTicks();
00608     if ( myGanttView->myTimeHeader->registerEndTime() )
00609         return; // try again via timeheader computeTicks();
00610     if ( hei > height() ) {
00611         if ( !  pendingWidth )
00612             pendingWidth = width();
00613         if ( pendingHeight < hei )
00614             pendingHeight = hei;
00615     }
00616     if (pendingHeight > hei )
00617         hei =  pendingHeight;
00618     if (hei > maximumComputedGridHeight)
00619         {
00620             maximumComputedGridHeight = hei;
00621             // compute the background interval lines
00622             myGanttView->myTimeHeader->computeIntervals( hei );
00623             //compute VerticalGrid and column color
00624             computeVerticalGrid();
00625         }
00626     computeTaskLinks();
00627     computeHorizontalGrid();
00628     computeDenseLines();
00629     computeShowNoInformation();
00630     //setAllChanged();
00631     update();
00632     if (pendingWidth && pendingHeight ) {
00633         resize( pendingWidth, pendingHeight );
00634         pendingWidth = 0;
00635         pendingHeight = 0;
00636         emit heightComputed( 0 );
00637 
00638     }
00639     pendingWidth = 0;
00640     pendingHeight = 0;
00641     //tqDebug("KDTimeTableWidget::updateMyContent() -------------------------");
00642 }
00643 // used for blocking recursive methods
00644 // e.g. KDGanttViewItem::setHighlight() and  displaySubitemsAsGroup() == true
00645 
00646 void KDTimeTableWidget::inc_blockUpdating( )
00647 {
00648     ++ int_blockUpdating;
00649 }
00650 // used for blocking recursive methods
00651 void KDTimeTableWidget::dec_blockUpdating( )
00652 {
00653     -- int_blockUpdating;
00654 }
00655 // if false(i.e. unblock), sets int_blockUpdating to 0
00656 void KDTimeTableWidget::setBlockUpdating( bool block )
00657 {
00658     if ( !block )
00659         int_blockUpdating = 0;
00660     flag_blockUpdating = block;
00661 }
00662 bool KDTimeTableWidget::blockUpdating()
00663 {
00664     return flag_blockUpdating;
00665 }
00666 
00667 void KDTimeTableWidget::setShowTaskLinks( bool show )
00668 {
00669     taskLinksVisible = show;
00670     updateMyContent();
00671 }
00672 bool KDTimeTableWidget::showTaskLinks()
00673 {
00674     return taskLinksVisible;
00675 }
00676 void KDTimeTableWidget::setHorBackgroundLines( int count,  TQBrush brush )
00677 {
00678     denseLineBrush = brush;
00679     denseLineCount = 0;
00680     if ( count > 1 )
00681         denseLineCount = count;
00682 }
00683 
00684 
00685 int KDTimeTableWidget::horBackgroundLines(  TQBrush& brush )
00686 {
00687     brush =  denseLineBrush;
00688     return denseLineCount;
00689 }
00690 
00691 int KDTimeTableWidget::getCoordX( TQDateTime dt ) {
00692     return myGanttView->myTimeHeader->getCoordX(dt);
00693 }
00694 
00695 /* ***************************************************************
00696    KDTimeHeaderWidget:: KDTimeHeaderWidget
00697    ***************************************************************** */
00698 KDTimeHeaderWidget:: KDTimeHeaderWidget( TQWidget* parent,KDGanttView* gant )
00699     : TQWidget (parent)
00700 {
00701     myToolTip = new KDTimeHeaderToolTip(this,this);
00702     mySizeHint = 0;
00703     myGanttView = gant;
00704     flagDoNotRecomputeAfterChange = true;
00705     TQDateTime start = (TQDateTime::currentDateTime ()).addSecs(-3600);
00706     setHorizonStart(start);
00707     setHorizonEnd( start.addSecs(3600*2));
00708     flagStartTimeSet = false;
00709     flagEndTimeSet = false;
00710     myCenterDateTime = TQDateTime::currentDateTime ();
00711     setScale(KDGanttView::Auto);
00712     //setScale(KDGanttView::Hour);
00713     myMaxScale = KDGanttView::Month;
00714     myMinScale = KDGanttView::Minute;
00715     myAutoScaleMinorTickcount = 100;
00716     setMajorScaleCount( 1 );
00717     setMinorScaleCount( 1);
00718     setMinimumColumnWidth( 5 );
00719     setYearFormat(KDGanttView::FourDigit );
00720     setHourFormat( KDGanttView::Hour_12 );
00721     myZoomFactor = 1.0;
00722     setWeekendBackgroundColor(TQColor(220,220,220) );
00723     setWeekendDays( 6, 7 );
00724     myGridMinorWidth = 0;
00725     myPopupMenu = new TQPopupMenu(this);
00726     TQPopupMenu * zoomPopupMenu = new TQPopupMenu(this);
00727     myPopupMenu->insertItem (i18n("Zoom"),zoomPopupMenu, 1);
00728     zoomPopupMenu->insertItem( i18n("Zoom to 100%"),this, TQT_SLOT(setSettings(int)),0 ,21,21 );
00729     zoomPopupMenu->insertItem( i18n("Zoom to Fit"),this, TQT_SLOT(setSettings(int)),0 ,20,20 );
00730     zoomPopupMenu->insertItem( i18n("Zoom In (x 2)"),this, TQT_SLOT(setSettings(int)),0 ,22,22 );
00731     zoomPopupMenu->insertItem( i18n("Zoom In (x 6)"),this, TQT_SLOT(setSettings(int)),0 ,24,24 );
00732     zoomPopupMenu->insertItem( i18n("Zoom In (x 12)"),this, TQT_SLOT(setSettings(int)),0 ,26,26 );
00733     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/2)"),this, TQT_SLOT(setSettings(int)),0 ,23,23 );
00734     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/6)"),this, TQT_SLOT(setSettings(int)),0 ,25,25 );
00735     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/12)"),this, TQT_SLOT(setSettings(int)),0 ,27,27 );
00736     scalePopupMenu = new TQPopupMenu(this);
00737     myPopupMenu->insertItem (i18n("Scale"),scalePopupMenu, 2);
00738     scalePopupMenu->insertItem( i18n("Minute"),this, TQT_SLOT(setSettings(int)),0 ,1,1 );
00739     scalePopupMenu->insertItem( i18n("Hour"),this, TQT_SLOT(setSettings(int)),0 ,2,2 );
00740     scalePopupMenu->insertItem( i18n("Day"),this, TQT_SLOT(setSettings(int)),0 ,3,3 );
00741     scalePopupMenu->insertItem( i18n("Week"),this, TQT_SLOT(setSettings(int)),0 ,4,4 );
00742     scalePopupMenu->insertItem( i18n("Month"),this, TQT_SLOT(setSettings(int)),0 ,5,5 );
00743     scalePopupMenu->insertItem( i18n("Auto"),this, TQT_SLOT(setSettings(int)),0 ,6,6 );
00744     scalePopupMenu->setCheckable ( true );
00745     timePopupMenu = new TQPopupMenu(this);
00746     myPopupMenu->insertItem (i18n("Time Format"),timePopupMenu, 3);
00747     timePopupMenu->insertItem( i18n("24 Hour"),this, TQT_SLOT(setSettings(int)),0 ,40,40 );
00748     timePopupMenu->insertItem( i18n("12 PM Hour"),this, TQT_SLOT(setSettings(int)),0 ,41,41 );
00749     timePopupMenu->insertItem( i18n("24:00 Hour"),this, TQT_SLOT(setSettings(int)),0 ,42,42 );
00750     yearPopupMenu = new TQPopupMenu(this);
00751     myPopupMenu->insertItem (i18n("Year Format"),yearPopupMenu, 4);
00752     yearPopupMenu->insertItem( i18n("Four Digit"),this, TQT_SLOT(setSettings(int)),0 ,50,50 );
00753     yearPopupMenu->insertItem( i18n("Two Digit"),this, TQT_SLOT(setSettings(int)),0 ,51,51 );
00754     yearPopupMenu->insertItem( i18n("Two Digit Apostrophe"),this, TQT_SLOT(setSettings(int)),0 ,52,52 );
00755     yearPopupMenu->insertItem( i18n("No Date on Minute/Hour Scale"),this, TQT_SLOT(setSettings(int)),0 ,53,53 );
00756 
00757     gridPopupMenu = new TQPopupMenu(this);
00758     myPopupMenu->insertItem (i18n("Grid"),gridPopupMenu,5);
00759     gridPopupMenu->insertItem( i18n("Show Minor Grid"),this, TQT_SLOT(setSettings(int)),0 ,10,10 );
00760     gridPopupMenu->insertItem( i18n("Show Major Grid"),this, TQT_SLOT(setSettings(int)),0 ,11,11 );
00761     gridPopupMenu->insertItem( i18n("Show No Grid"),this, TQT_SLOT(setSettings(int)),0 ,12,12 );
00762     myPopupMenu->insertItem( i18n("Print"),this, TQT_SLOT(setSettings(int)),0 ,30,30 );
00763     connect(myPopupMenu, TQT_SIGNAL (  aboutToShow () ) , this, TQT_SLOT( preparePopupMenu() )) ;
00764     flagZoomToFit = false;
00765     setShowMinorTicks( true );
00766     myRealEnd =  myHorizonEnd;
00767     myRealStart = myHorizonStart;
00768     autoComputeTimeLine = true;
00769     flagDoNotRecomputeAfterChange = false;
00770     flagDoNotRepaintAfterChange = false;
00771     setShowPopupMenu(false,false,false,false,false,false,false);
00772     for (int j =1;j<8;++j)
00773         weekdayColor[j] = TQt::white;
00774     myMinimumWidth = 0;
00775     mouseDown = false;
00776     beginMouseDown = 0;
00777     endMouseDown = 0;
00778 }
00779 
00780 KDTimeHeaderWidget::~KDTimeHeaderWidget()
00781 {
00782     delete myToolTip;
00783 }
00784 void  KDTimeHeaderWidget::preparePopupMenu()
00785 {
00786     myPopupMenu->setItemVisible ( 1, flagShowZoom  );
00787     myPopupMenu->setItemVisible ( 2, flagShowScale );
00788     myPopupMenu->setItemVisible ( 3, flagShowTime );
00789     myPopupMenu->setItemVisible ( 4, flagShowYear );
00790     myPopupMenu->setItemVisible ( 5, flagShowGrid);
00791     myPopupMenu->setItemVisible ( 30, flagShowPrint );
00792     if (flagZoomToFit)
00793         myPopupMenu->changeItem( 1, i18n("Zoom (Fit)"));
00794     else
00795         myPopupMenu->changeItem( 1, i18n("Zoom (%1)").arg( TQString::number( zoomFactor(), 'f',3) ) );
00796     int i = 0;
00797     int id;
00798     while ( ( id = scalePopupMenu->idAt( i++ )) >= 0 ) {
00799         scalePopupMenu->setItemChecked ( id, false );
00800     }
00801     scalePopupMenu->setItemChecked ( scalePopupMenu->idAt ( (int)( scale()) ), true );
00802     i = 0;
00803     while ( ( id = timePopupMenu->idAt( i++ )) >= 0 ) {
00804         timePopupMenu->setItemChecked ( id, false );
00805     }
00806     timePopupMenu->setItemChecked ( timePopupMenu->idAt ( (int)( hourFormat()) ), true );
00807     i = 0;
00808     while ( ( id = yearPopupMenu->idAt( i++ )) >= 0 ) {
00809         yearPopupMenu->setItemChecked ( id, false );
00810     }
00811     yearPopupMenu->setItemChecked ( yearPopupMenu->idAt ( (int)( yearFormat()) ), true );
00812     i = 0;
00813     while ( ( id = gridPopupMenu->idAt( i++ )) >= 0 ) {
00814         gridPopupMenu->setItemChecked ( id, false );
00815     }
00816 
00817     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 0 ), showMinorTicks() );
00818     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 1 ), showMajorTicks() );
00819     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 2 ),
00820                                     !(showMajorTicks() || showMinorTicks()) );
00821 
00822 
00823 }
00824 
00825 TQString  KDTimeHeaderWidget::getToolTipText(TQPoint p)
00826 {
00827     return TDEGlobal::locale()->formatDateTime(getDateTimeForIndex(p.x()));
00828 }
00829 void KDTimeHeaderWidget::addTickRight( int num )
00830 {
00831     int secs = ((num*getTickTime())-30);
00832     setHorizonEnd(getDateTimeForIndex(width()).addSecs(secs));
00833     //tqApp->processEvents();
00834 }
00835 
00836 void KDTimeHeaderWidget::addTickLeft( int num )
00837 {
00838     int secs = ((num*getTickTime())-30);
00839     setHorizonStart(getDateTimeForIndex(0).addSecs(-secs));
00840     //tqApp->processEvents();
00841 }
00842 // the time in secs of one minor grid tick
00843 int KDTimeHeaderWidget::getTickTime()
00844 {
00845     return getDateTimeForIndex(0).secsTo(getDateTimeForIndex(myGridMinorWidth));
00846 }
00847 
00848 
00849 void KDTimeHeaderWidget::checkWidth( int wid )
00850 {
00851     // we have to set the minimum width one pixel higher than the
00852     // viewport width of the canvas view in  order to
00853     // avoid that the horiz. scrollbar of the canvasview is hidden
00854     myMinimumWidth = wid + 1;
00855     if ( myMinimumWidth  > width() ||
00856          ( myMinimumWidth > mySizeHint &&
00857            myMinimumWidth < (width() - myGridMinorWidth  )) )
00858         computeTicks();
00859 
00860     // Update (horizontal) scrollbar,
00861     // We probably come from an external resize and then we must
00862     // calculate on basis of myCanvasView.
00863     // (NOTE: we have disconnected the auto TQScrollView scrollbar update)
00864     if (myGanttView && myGanttView->myCanvasView)
00865         myGanttView->myCanvasView->updateScrollBars();
00866 }
00867 
00868 bool KDTimeHeaderWidget::registerStartTime()
00869 {
00870 
00871     TQListViewItemIterator it( myGanttView->myListView );
00872     if (!flagStartTimeSet) {
00873         TQDateTime temp , time;
00874         KDGanttViewItem* item;
00875         bool setNewTime = false;
00876         item = (KDGanttViewItem*)myGanttView->myListView->firstChild();
00877         if ( item ) {
00878             temp = item->startTime();
00879             time = temp;
00880             //  while ( item != 0)
00881             for ( ; it.current(); ++it ) {
00882                 item = ( KDGanttViewItem* )it.current();
00883                 if (item->isVisibleInGanttView) {
00884                     if ( !setNewTime )
00885                         temp = item->startTime();
00886                     switch( item->type() ) {
00887                     case KDGanttViewItem::Event:
00888                         time = ((KDGanttViewEventItem*)item)->leadTime();
00889                         setNewTime = true;
00890                         break;
00891                     case KDGanttViewItem::Summary:
00892                     case KDGanttViewItem::Task:
00893                         time = item->startTime();
00894                         setNewTime = true;
00895                         break;
00896                     default:
00897                         time = temp;
00898                     }
00899                     if ( time < temp) {
00900                         temp = time ;
00901                     }
00902                 }
00903             }
00904             if ( setNewTime )
00905                 if ( myHorizonStart != temp) {
00906                     myHorizonStart = temp;
00907                     computeTicks();
00908                     return true;
00909                 }
00910         }
00911     }
00912     return false;
00913 }
00914 
00915 
00916 bool KDTimeHeaderWidget::registerEndTime()
00917 {
00918     if (!flagEndTimeSet) {
00919         TQDateTime temp , time;
00920         KDGanttViewItem* item;
00921         bool setNewTime = false;
00922         item = (KDGanttViewItem*)myGanttView->myListView->firstChild();
00923         if ( item ) {
00924             temp = item->startTime();
00925             time = temp;
00926             TQListViewItemIterator it( myGanttView->myListView );
00927             for ( ; it.current(); ++it ) {
00928                 item = ( KDGanttViewItem* )it.current();
00929                 if (item->isVisibleInGanttView) {
00930                     if ( !setNewTime )
00931                         temp = item->startTime();
00932                     switch( item->type() ) {
00933                     case KDGanttViewItem::Event:
00934                         time = ((KDGanttViewEventItem*)item)->startTime();
00935                         setNewTime = true;
00936                         break;
00937                     case KDGanttViewItem::Summary:
00938                         time = item->endTime();
00939                         if ( time < ((KDGanttViewSummaryItem*)item)->actualEndTime())
00940                             time = ((KDGanttViewSummaryItem*)item)->actualEndTime();
00941                         setNewTime = true;
00942                         break;
00943                     case KDGanttViewItem::Task:
00944                         time = item->endTime();
00945                         setNewTime = true;
00946                         break;
00947                     default:
00948                         time = temp;
00949                     }
00950                     if ( time > temp)
00951                         temp = time ;
00952                 }
00953             }
00954 
00955             if ( setNewTime )
00956                 if (myHorizonEnd != temp ) {
00957                     myHorizonEnd = temp;
00958                     computeTicks();
00959                     return true;
00960                 }
00961         }
00962     }
00963     return false;
00964 }
00965 
00966 
00967 void KDTimeHeaderWidget::setShowPopupMenu( bool show,
00968                                            bool showZoom,
00969                                            bool showScale,
00970                                            bool showTime,
00971                                            bool showYear,
00972                                            bool showGrid,
00973                                            bool showPrint)
00974 {
00975     flagShowPopupMenu = show;
00976     flagShowZoom = showZoom;
00977     flagShowScale  = showScale;
00978     flagShowTime  = showTime;
00979     flagShowYear = showYear;
00980     flagShowGrid  = showGrid;
00981     flagShowPrint = showPrint;
00982 }
00983 
00984 
00985 bool KDTimeHeaderWidget::showPopupMenu() const
00986 {
00987     return flagShowPopupMenu;
00988 }
00989 
00990 void KDTimeHeaderWidget::setSettings(int i)
00991 {
00992 
00993     switch (i) {
00994     case 1:
00995         setScale(KDGanttView::Minute );
00996         break;
00997     case 2:
00998         setScale(KDGanttView::Hour );
00999         break;
01000     case 3:
01001         setScale(KDGanttView::Day );
01002         break;
01003     case 4:
01004         setScale(KDGanttView::Week );
01005         break;
01006     case 5:
01007         setScale(KDGanttView::Month );
01008         break;
01009     case 6:
01010         setScale(KDGanttView::Auto );
01011         break;
01012     case 10:
01013         setShowMinorTicks( true );
01014         break;
01015     case 11:
01016         setShowMajorTicks( true );{
01017 
01018         }
01019         break;
01020     case 12:
01021         setShowMajorTicks( false );
01022         setShowMinorTicks( false);
01023         break;
01024     case 20:
01025         zoomToFit();
01026         break;
01027     case 21:
01028         zoom(1.0);
01029         break;
01030     case 22:
01031         zoom(2.0,false);
01032         break;
01033     case 23:
01034         zoom(0.5,false);
01035         break;
01036     case 24:
01037         zoom(6.0,false);
01038         break;
01039     case 25:
01040         zoom(0.16666,false);
01041         break;
01042     case 26:
01043         zoom(12.0,false);
01044         break;
01045     case 27:
01046         zoom(0.08333,false);
01047         break;
01048     case 30:
01049         myGanttView->print();
01050         break;
01051     case 40:
01052     case 41:
01053     case 42:
01054         setHourFormat( (KDGanttView::HourFormat) (i - 40) );
01055         break;
01056     case 50:
01057     case 51:
01058     case 52:
01059     case 53:
01060         setYearFormat( (KDGanttView::YearFormat) ( i - 50) );
01061         break;
01062 
01063     case 60:
01064 
01065         break;
01066 
01067     case 61:
01068 
01069         break;
01070 
01071     case 62:
01072 
01073         break;
01074 
01075     case 63:
01076 
01077         break;
01078 
01079     case 64:
01080 
01081         break;
01082     }
01083     // myGanttView->myTimeTable->updateMyContent();
01084 }
01085 void KDTimeHeaderWidget::zoomToFit()
01086 {
01087     flagZoomToFit = true;
01088     computeTicks();
01089     // Since we have disconnected autoupdate of scrollbars, we must do it ourselves
01090     if (myGanttView && myGanttView->myCanvasView)
01091         myGanttView->myCanvasView->updateScrollBars();
01092 }
01093 double KDTimeHeaderWidget::zoomFactor()
01094 {
01095     return myZoomFactor;
01096 }
01097 double KDTimeHeaderWidget::secsFromTo( TQDateTime begin, TQDateTime end )
01098 {
01099     TQDateTime temp;
01100     double secs, days;
01101     days = begin.daysTo(end);
01102     temp = begin.addDays((int) days);
01103     secs = temp.secsTo(end);
01104     secs += days * 86400.0;
01105     return secs;
01106 }
01107 
01108 
01109 void KDTimeHeaderWidget::zoomToSelection( TQDateTime start, TQDateTime end)
01110 {
01111     if (start < myHorizonStart) {
01112         myHorizonStart = start;
01113         flagStartTimeSet = true;
01114         //tqDebug("myHorizonStart reset");
01115     }
01116     if (end > myHorizonEnd) {
01117         myHorizonEnd = end;
01118         flagEndTimeSet = true;
01119         //tqDebug("myHorizonEnd reset ");
01120     }
01121     flagDoNotRepaintAfterChange = true;//avoid flicker
01122     zoom(1.0); // set to 100%
01123     int viewWid = myGanttView->myCanvasView->viewport()->width();
01124     int timeWid =  getCoordX(end)-getCoordX(start);
01125     double fac;
01126     fac  = ( (double)viewWid)/((double) timeWid  );
01127     zoom (fac);
01128     timeWid =  getCoordX(end)-getCoordX(start);
01129     int count = 0;
01130     int lastScaleCount = 0;
01131     while (timeWid >viewWid || ( ( myRealMinorScaleCount != lastScaleCount)  && timeWid*2 < viewWid ) ) {
01132         lastScaleCount = myRealMinorScaleCount;
01133         fac = (fac * (double)viewWid)/(double)timeWid;
01134         zoom (fac);
01135         timeWid =  getCoordX(end)-getCoordX(start);
01136         if ( count++ > 10 ) {
01137             //tqDebug("Exiting while loop in zoomToSelection ");
01138             break;
01139         }
01140     }
01141     flagDoNotRepaintAfterChange = false;
01142     updateTimeTable();
01143     repaint();
01144     moveTimeLineTo((getCoordX(start)-(viewWid-timeWid)/2));
01145     // Since we have disconnected autoupdate of scrollbars, we must do it ourselves
01146     if (myGanttView && myGanttView->myCanvasView)
01147         myGanttView->myCanvasView->updateScrollBars();
01148 }
01149 void KDTimeHeaderWidget::moveTimeLineTo(int X)
01150 {
01151     int Y = myGanttView->myCanvasView->contentsY ();
01152     myGanttView->myCanvasView->setContentsPos (X, Y );
01153 }
01154 
01155 void KDTimeHeaderWidget::zoom(double factor, bool absolute)
01156 {
01157     flagZoomToFit = false;
01158     if ( factor < 0.000001 ) {
01159         tqDebug("KDGanttView::zoom() : Zoom factor to low. Nothing zoomed. ");
01160         return;
01161     }
01162     double newZoom;
01163     if (absolute)
01164         newZoom = factor;
01165     else
01166         newZoom = myZoomFactor * factor;
01167     double relativeZoom;
01168     relativeZoom = newZoom / myZoomFactor;
01169 
01170     //tqDebug("zooming relative %f ", relativeZoom);
01171     //tqDebug("zooming absolute %f ", newZoom);
01172     int viewWid = myGanttView->myCanvasView->viewport()->width();
01173     if ( width() * relativeZoom < viewWid && ( newZoom > 1.01 || newZoom < 0.99 ) ) {
01174         tqDebug("KDGanttView::zoom() : Zoom factor to low for current horizon. ");
01175         // tqDebug("zooming relative %f, zooming absolute %f, viewWidth %d width %d ", relativeZoom,  newZoom, viewWid, width() );
01176         return;
01177     }
01178     myZoomFactor = newZoom;
01179     computeTicks();
01180     // Since we have disconnected autoupdate of scrollbars, we must do it ourselves
01181     if (myGanttView && myGanttView->myCanvasView)
01182         myGanttView->myCanvasView->updateScrollBars();
01183 }
01184 
01192 void KDTimeHeaderWidget::setHorizonStart( const TQDateTime& start )
01193 {
01194     myHorizonStart = start;
01195     flagStartTimeSet = true;
01196     computeTicks();
01197 }
01198 
01199 
01206 TQDateTime KDTimeHeaderWidget::horizonStart() const
01207 {
01208     return myHorizonStart;
01209 }
01210 
01211 
01219 void KDTimeHeaderWidget::setHorizonEnd( const TQDateTime& start )
01220 {
01221     myHorizonEnd = start;
01222     flagEndTimeSet = true;
01223     computeTicks();
01224 
01225 }
01226 
01227 
01234 TQDateTime KDTimeHeaderWidget::horizonEnd() const
01235 {
01236     return myHorizonEnd;
01237 }
01238 
01239 
01248 void KDTimeHeaderWidget::setScale(Scale unit )
01249 {
01250     myScale = unit;
01251     myZoomFactor = 1.0;
01252     computeTicks();
01253     // Since we have disconnected autoupdate of scrollbars, we must do it ourselves
01254     if (myGanttView && myGanttView->myCanvasView)
01255         myGanttView->myCanvasView->updateHorScrollBar();
01256 }
01257 
01258 
01265 KDTimeHeaderWidget::Scale KDTimeHeaderWidget::scale() const
01266 {
01267     return myScale;
01268 }
01269 
01270 
01277 void KDTimeHeaderWidget::setMaximumScale( Scale unit )
01278 {
01279     myMaxScale = unit;
01280     computeTicks();
01281 }
01282 
01283 
01290 KDTimeHeaderWidget::Scale  KDTimeHeaderWidget::maximumScale() const
01291 {
01292     return myMaxScale;
01293 }
01294 
01295 
01302 void  KDTimeHeaderWidget::setMinimumScale( Scale unit )
01303 {
01304     myMinScale = unit;
01305     computeTicks();
01306 }
01307 
01308 
01315 KDTimeHeaderWidget::Scale  KDTimeHeaderWidget::minimumScale() const
01316 {
01317     return myMinScale;
01318 }
01319 
01320 
01329 void KDTimeHeaderWidget::setMinimumColumnWidth( int width )
01330 {
01331     myMinimumColumWidth =  width;
01332     computeTicks();
01333 }
01334 
01335 
01342 int KDTimeHeaderWidget::minimumColumnWidth() const
01343 {
01344     return myMinimumColumWidth;
01345 }
01346 
01347 
01355 void KDTimeHeaderWidget::setYearFormat( YearFormat format )
01356 {
01357     myYearFormat =  format;
01358     computeTicks();
01359 }
01360 
01361 
01368 KDTimeHeaderWidget::YearFormat KDTimeHeaderWidget::yearFormat() const
01369 {
01370     return  myYearFormat;
01371 }
01372 
01373 
01381 void KDTimeHeaderWidget::setHourFormat( HourFormat format )
01382 {
01383     myHourFormat = format;
01384     computeTicks();
01385 }
01386 
01387 
01394 KDTimeHeaderWidget::HourFormat KDTimeHeaderWidget::hourFormat() const
01395 {
01396     return myHourFormat;
01397 }
01398 
01399 
01406 void KDTimeHeaderWidget::setShowMajorTicks( bool show )
01407 {
01408     flagShowMajorTicks = show;
01409     if (show) {
01410         setShowMinorTicks(false);
01411     }
01412     updateTimeTable();
01413 }
01414 
01415 
01422 bool KDTimeHeaderWidget::showMajorTicks() const
01423 {
01424     return flagShowMajorTicks;
01425 }
01426 
01427 
01434 void KDTimeHeaderWidget::setShowMinorTicks( bool show )
01435 {
01436     flagShowMinorTicks = show;
01437     if (show)
01438         setShowMajorTicks(false );
01439     //repaintMe();
01440     updateTimeTable();
01441 }
01442 
01443 
01450 bool KDTimeHeaderWidget::showMinorTicks() const
01451 {
01452     return flagShowMinorTicks;
01453 }
01454 
01455 
01464 void KDTimeHeaderWidget::setColumnBackgroundColor( const TQDateTime& column,
01465                                                    const TQColor& color,
01466                                                    Scale mini, Scale maxi )
01467 {
01468     ColumnColorList::iterator it;
01469     for ( it = ccList.begin(); it != ccList.end(); ++it ) {
01470         if ((*it).datetime == column) {
01471             (*it).color = color;
01472             (*it).minScaleView = mini;
01473             (*it).maxScaleView = maxi;
01474             return;
01475         }
01476     }
01477     DateTimeColor newItem;
01478     newItem.datetime = column;
01479     newItem.color = color;
01480     newItem.minScaleView = mini;
01481     newItem.maxScaleView = maxi;
01482     ccList.append(newItem);
01483     updateTimeTable();
01484 }
01485 
01486 void KDTimeHeaderWidget::computeIntervals( int height )
01487 {
01488     IntervalColorList::const_iterator it;
01489     for ( it = icList.begin(); it != icList.end(); ++it ) {
01490       (*it)->layout( this, height );
01491     }
01492 }
01493 
01494 void KDTimeHeaderWidget::addIntervalBackgroundColor( KDIntervalColorRectangle* newItem )
01495 {
01496     icList.append(newItem);
01497     updateTimeTable();
01498 }
01499 
01500 #if 0
01501 bool KDTimeHeaderWidget::changeBackgroundInterval( const TQDateTime& oldstart,
01502                                                    const TQDateTime& oldend,
01503                                                    const TQDateTime& newstart,
01504                                                    const TQDateTime& newend )
01505 {
01506     IntervalColorList::iterator it;
01507     for ( it = icList.begin(); it != icList.end(); ++it ) {
01508         if ((*it).datetime == oldstart && (*it).end == oldend ) {
01509             IntervalColorList::iterator it2;
01510             for ( it2 = icList.begin(); it2 != icList.end(); ++it2 ) {
01511                 if ((*it2).datetime == newstart && (*it2).end == newend )
01512                     return false;
01513             }
01514             (*it).datetime = newstart;
01515             (*it).end = newend;
01516             updateTimeTable();
01517             return true;
01518         }
01519     }
01520     return false;
01521 }
01522 bool KDTimeHeaderWidget::deleteBackgroundInterval( const TQDateTime& start,
01523                                                    const TQDateTime& end)
01524 {
01525     IntervalColorList::iterator it;
01526     for ( it = icList.begin(); it != icList.end(); ++it ) {
01527         if ((*it).datetime == start && (*it).end == end ) {
01528             //delete  (*it).canvasLine;
01529             delete  (*it).canvasRect;
01530             icList.remove(it);
01531             updateTimeTable();
01532             return true;
01533         }
01534     }
01535     return false;
01536 }
01537 
01538 void KDTimeHeaderWidget::setIntervalBackgroundColor( const TQDateTime& start,
01539                                                      const TQDateTime& end,
01540                                                      const TQColor& color,
01541                                                      Scale mini ,
01542                                                      Scale maxi )
01543 {
01544 
01545     IntervalColorList::iterator it;
01546     for ( it = icList.begin(); it != icList.end(); ++it ) {
01547         if ((*it).datetime == start && (*it).end == end ) {
01548             (*it).color = color;
01549             (*it).minScaleView = mini;
01550             (*it).maxScaleView = maxi;
01551             return;
01552         }
01553     }
01554     DateTimeColor newItem;
01555     if ( start <= end ) {
01556         newItem.datetime = start;
01557         newItem.end = end;
01558     } else {
01559         newItem.datetime = end;
01560         newItem.end = start;
01561     }
01562     newItem.color = color;
01563     newItem.minScaleView = mini;
01564     newItem.maxScaleView = maxi;
01565     //newItem.canvasLine = new KDCanvasLine(myGanttView->myTimeTable,0,Type_is_KDGanttGridItem);
01566     newItem.canvasRect = new KDCanvasRectangle(myGanttView->myTimeTable,0,Type_is_KDGanttGridItem);
01567     newItem.canvasRect->setZ(-19);
01568     icList.append(newItem);
01569     updateTimeTable();
01570 }
01571 #endif
01572 
01573 void KDTimeHeaderWidget::clearBackgroundColor()
01574 {
01575 
01576     IntervalColorList::iterator itic;
01577     for ( itic = icList.begin(); itic != icList.end(); ++itic ) {
01578         delete  (*itic);
01579     }
01580     ccList.clear();
01581     icList.clear();
01582     updateTimeTable();
01583 }
01584 TQDateTime KDTimeHeaderWidget::getDateTimeForIndex(int X, bool local )
01585 {
01586     int coordX = X;
01587     if ( !local ) {
01588         TQPoint p = TQPoint ( X, 1 );
01589         coordX = myGanttView->myTimeHeaderScroll->viewportToContents(myGanttView->myTimeHeaderScroll->mapFromGlobal( p )).x();
01590 
01591     }
01592     double secs = (secsFromTo( myRealStart, myRealEnd ) * ((double)coordX))/(double)width();
01593     double days = secs/86400.0;
01594     secs = secs - ( ((int) days) *86400.0 );
01595     return (myRealStart.addDays ( (int) days )).addSecs( (int) secs);
01596 }
01597 
01598 //FIXME: This doesn't work quite intuitively (imho) when scale is day
01599 //       and each column containes more than 1 day:
01600 //       1) If a column includes a weekend day, the whole column gets weekend color,
01601 //       2) If a column includes 7 days, either *all* columns get weekend color, or
01602 //          *none* get weekend color (haven't figured out why)
01603 //       Proposal: Only use weekend color if the whole column is a weekend.
01604 //       Alt: Color the area that actually is the weekend.
01605 bool KDTimeHeaderWidget::getColumnColor(TQColor& col,int coordLow, int coordHigh)
01606 {
01607     if (!flagShowMajorTicks && !flagShowMinorTicks)
01608         return false;
01609     TQDateTime start,end;
01610     start = getDateTimeForIndex(coordLow);
01611     end = getDateTimeForIndex(coordHigh).addSecs(-1);
01612     Scale tempScale = myRealScale;
01613     if (flagShowMajorTicks)
01614         switch (myRealScale)
01615             {
01616             case KDGanttView::Minute: tempScale = KDGanttView::Hour;  break;
01617             case KDGanttView::Hour: tempScale = KDGanttView::Day   ;  break;
01618             case KDGanttView::Day: tempScale = KDGanttView::Week   ;  break;
01619             case KDGanttView::Week: tempScale = KDGanttView::Month  ;  break;
01620             case KDGanttView::Month: return false   ;  break;
01621             case KDGanttView::Auto: return false   ;  break;
01622             }
01623     //check defined column color
01624     ColumnColorList::iterator it;
01625     for ( it = ccList.begin(); it != ccList.end(); ++it ) {
01626         if ((*it).datetime  >= start && (*it).datetime  <= end) {
01627             if (tempScale >= (*it).minScaleView &&   tempScale <= (*it).maxScaleView    ) {
01628                 col = (*it).color;
01629                 return true;
01630             }
01631         }
01632     }
01633 
01634     if (tempScale > KDGanttView::Day) return false;
01635 
01636     start = getDateTimeForIndex((coordLow+coordHigh)/2);
01637     int day = start.date().dayOfWeek ();
01638     //checkweekdaycolor
01639     if (weekdayColor[day] != TQt::white) {
01640         col = weekdayColor[day];
01641         return true;
01642     }
01643     //checkweekendcolor
01644     int endday = myWeekendDaysEnd;
01645     col = myWeekendBackgroundColor;
01646     if (myWeekendDaysStart > myWeekendDaysEnd)
01647         endday +=7;
01648     if (day >= myWeekendDaysStart && day <= endday) {
01649         return true;
01650     } else {
01651         if (day+7 >= myWeekendDaysStart && day+7 <= endday) {
01652             return true;
01653         }
01654     }
01655     return false;
01656 }
01657 
01666 TQColor KDTimeHeaderWidget::columnBackgroundColor( const TQDateTime& column ) const
01667 {
01668     TQColor c;
01669     c = white;
01670     ColumnColorList::const_iterator ite;
01671     for ( ite = ccList.begin(); ite != ccList.end(); ++ite ) {
01672         if ((*ite).datetime == column) {
01673             c = (*ite).color;
01674         }
01675     }
01676     return c;
01677 }
01678 
01679 
01688 void KDTimeHeaderWidget::setWeekendBackgroundColor( const TQColor& color )
01689 {
01690     myWeekendBackgroundColor = color ;
01691     updateTimeTable();
01692 }
01693 
01694 
01701 TQColor KDTimeHeaderWidget::weekendBackgroundColor() const
01702 {
01703     return myWeekendBackgroundColor;
01704 }
01705 
01716 void KDTimeHeaderWidget::setWeekdayBackgroundColor( const TQColor& color, int  weekday )
01717 {
01718     weekdayColor[weekday] = color;
01719     updateTimeTable();
01720 }
01721 
01722 
01730 TQColor KDTimeHeaderWidget::weekdayBackgroundColor(int weekday) const
01731 {
01732     return weekdayColor[weekday];
01733 }
01734 
01735 
01746 void KDTimeHeaderWidget::setWeekendDays( int start, int end )
01747 {
01748     myWeekendDaysStart = start;
01749     myWeekendDaysEnd = end;
01750     updateTimeTable();
01751 }
01752 
01753 
01761 void KDTimeHeaderWidget::weekendDays( int& start, int& end ) const
01762 {
01763     start = myWeekendDaysStart;
01764     end = myWeekendDaysEnd ;
01765 }
01766 
01767 
01768 
01775 void KDTimeHeaderWidget::setMajorScaleCount( int count )
01776 {
01777     myMajorScaleCount=count;
01778     computeTicks();
01779 }
01780 
01781 
01788 int KDTimeHeaderWidget::majorScaleCount() const
01789 {
01790     return myMajorScaleCount;
01791 }
01792 
01793 
01800 void KDTimeHeaderWidget::setMinorScaleCount( int count )
01801 {
01802     myMinorScaleCount = count;
01803     computeTicks();
01804 }
01805 
01806 
01813 int KDTimeHeaderWidget::minorScaleCount() const
01814 {
01815     return myMinorScaleCount ;
01816 
01817 }
01818 
01819 
01820 void KDTimeHeaderWidget::resizeEvent ( TQResizeEvent * )
01821 {
01822     // tqDebug("KDTimeHeaderWidget:: resizeEvent ");
01823     paintPix.resize( 800, height () );
01824 }
01825 
01826 
01827 void KDTimeHeaderWidget::updateTimeTable()
01828 {
01829     //tqDebug("KDTimeHeaderWidget::updateTimeTable() ");
01830     if (flagDoNotRecomputeAfterChange) return;
01831     // setting the scrolling steps
01832     int scrollLineStep = myGridMinorWidth;
01833     if (showMajorTicks()) {
01834         TQValueList<int>::iterator intIt = majorTicks.begin();
01835         scrollLineStep = 5 * myGridMinorWidth;
01836         if (intIt != majorTicks.end()) {
01837             int left = *intIt;
01838             ++intIt;
01839             if (intIt != majorTicks.end()) {
01840                 scrollLineStep = *intIt-left;
01841             }
01842         }
01843     }
01844     myGanttView->myCanvasView->horizontalScrollBar()->setLineStep(scrollLineStep);
01845     myGanttView->myTimeTable->maximumComputedGridHeight = 0;
01846     myGanttView->myTimeTable->updateMyContent();
01847 }
01848 
01849 
01850 void KDTimeHeaderWidget::setAutoScaleMinorTickCount( int count )
01851 {
01852     myAutoScaleMinorTickcount = count;
01853     computeTicks();
01854 
01855 }
01856 
01857 
01858 int KDTimeHeaderWidget::autoScaleMinorTickCount()
01859 {
01860     return myAutoScaleMinorTickcount;
01861 }
01862 
01863 
01864 void KDTimeHeaderWidget::repaintMe(int left,int paintwid, TQPainter* painter)
01865 {
01866     if (flagDoNotRecomputeAfterChange) return;
01867     TQColorGroup qcg =TQColorGroup( white, black,white, darkGray,black,gray,gray) ;
01868     TQPainter* p;
01869     int offsetLeft = 0;
01870     if ( paintwid > paintPix.width()-100 )
01871         paintPix.resize( paintwid+100, height () );
01872     if ( painter )
01873         p = painter;
01874     else {
01875         p = new TQPainter( &paintPix );
01876         offsetLeft = left-50;
01877     }
01878     if ( mouseDown ) {
01879         p->fillRect( left-offsetLeft, 0, paintwid, height(), TQBrush(paletteBackgroundColor()) );
01880         int start ;
01881         int wid;
01882         if ( beginMouseDown < endMouseDown ) {
01883             start = beginMouseDown ;
01884             wid = endMouseDown - beginMouseDown ;
01885         } else {
01886             start = endMouseDown ;
01887             wid = -endMouseDown + beginMouseDown ;
01888         }
01889         p->fillRect( start-offsetLeft, 0, wid, height(), TQBrush(paletteBackgroundColor().dark()) );
01890     } else {
01891         if (! painter )
01892             p->fillRect( left-offsetLeft, 0, paintwid, height(), TQBrush(paletteBackgroundColor()) );
01893     }
01894     p->setPen(TQColor(40,40,40));
01895     TQFont tempFont = p->font();
01896     tempFont.setWeight(63);
01897     p->setFont(tempFont);
01898     int hei1 = myMajorGridHeight,
01899         hei2 = height(),
01900         wid1 = myGridMinorWidth;
01901     int xCoord;
01902     int lwid = 1;
01903 
01904     TQValueList<TQString>::iterator it;
01905     TQValueList<int>::iterator intIt = majorTicks.begin();
01906     for ( it =  majorText.begin(); it !=  majorText.end(); ++it ) {
01907         xCoord  = (*intIt++);
01908         if (((*intIt)>= left && xCoord <= left+paintwid)) {
01909             qDrawShadeLine ( p,xCoord-offsetLeft ,hei1+1, xCoord-offsetLeft, -2, qcg, true, lwid, 1 );
01910             p->drawText(xCoord+4-offsetLeft,hei1-4,(*it));
01911         }
01912     }
01913     qDrawShadeLine ( p,left-offsetLeft  ,hei1, left+paintwid-offsetLeft, hei1, qcg, true, lwid, 1 );
01914     int i = 0;
01915     for ( it =  minorText.begin(); it !=  minorText.end(); ++it ) {
01916         if (i*wid1 >= left-wid1 && i*wid1 <= left+paintwid) {
01917             qDrawShadeLine ( p,i*wid1-offsetLeft ,hei1-1, i*wid1-offsetLeft, hei2, qcg, true, lwid, 1 );
01918             p->drawText(i*wid1+1-offsetLeft,hei1+1,wid1-1,hei2-hei1,TQt::AlignCenter,(*it));
01919         }
01920         ++i;
01921     }
01922     p->setPen(black);
01923     p->drawLine(left-offsetLeft,hei1,left+paintwid-offsetLeft,hei1);
01924     qDrawShadeLine ( p,left-offsetLeft  ,hei2-1, left+paintwid-offsetLeft, hei2-1, qcg, true, lwid, 1 );
01925     p->drawLine(left-offsetLeft,hei2-1,left+paintwid-offsetLeft,hei2-1);
01926     if ( !painter ) {
01927         p->end();
01928         delete p;
01929         bitBlt ( this, left, 0, &paintPix, 50, 0, paintwid, height() );
01930     }
01931 }
01932 
01933 // cuts the secs in the DateTime if scale is Minute ,
01934 // the minutes and secs if scale is Hour and so on
01935 
01936 TQDateTime KDTimeHeaderWidget::getEvenTimeDate(TQDateTime tempdatetime ,Scale sc)
01937 {
01938     TQDate tempdate;
01939     int min, hour;
01940     int tempMinorScaleCount = myRealMinorScaleCount;
01941     switch (sc)
01942         {
01943         case KDGanttView::Month:
01944             tempdate = tempdatetime.date();
01945             while (tempdate.day ()!= 1 )
01946                 tempdate = tempdate.addDays(-1);
01947             //while (tempdate.month ()!= 1 )
01948             //tempdate = tempdate.addMonths(-1);
01949             tempdatetime = TQDateTime (tempdate, TQTime (0,0));
01950             break;
01951         case KDGanttView::Week:
01952             tempdate = tempdatetime.date();
01953             while (tempdate.dayOfWeek ()!= TDEGlobal::locale()->weekStartDay())
01954                 tempdate = tempdate.addDays(-1);
01955             //tempdate = tempdate.addDays(-7);
01956             tempdatetime = TQDateTime (tempdate, TQTime (0,0));
01957             break;
01958         case KDGanttView::Day:
01959             tempdatetime = TQDateTime (tempdatetime.date(), TQTime ( 0,0 ) );
01960             break;
01961         case KDGanttView::Hour:
01962             hour = tempdatetime.time().hour();
01963             while (24%tempMinorScaleCount > 0 && 24%tempMinorScaleCount < 24)
01964                 ++tempMinorScaleCount;
01965             hour = ( hour /tempMinorScaleCount)*tempMinorScaleCount;
01966             tempdatetime = TQDateTime (tempdatetime.date(), TQTime (hour, 0 ));
01967             break;
01968         case KDGanttView::Minute:
01969             min = tempdatetime.time().minute();
01970             while (60%tempMinorScaleCount > 0 && 60%tempMinorScaleCount < 60)
01971                 ++tempMinorScaleCount;
01972             // tqDebug("myMinorScaleCount %d %d %d",myMinorScaleCount, myRealMinorScaleCount, tempMinorScaleCount);
01973             min = (min /tempMinorScaleCount)*tempMinorScaleCount;
01974             tempdatetime = TQDateTime (tempdatetime.date(), TQTime (tempdatetime.time().hour(),min ));
01975 
01976             break;
01977         case KDGanttView::Auto:
01978             break;
01979         }
01980     return tempdatetime;
01981 }
01982 
01983 
01984 void KDTimeHeaderWidget::computeRealScale(TQDateTime start)
01985 {
01986 
01987     if (myScale ==KDGanttView::Auto) {
01988         //tqDebug("Autoscale ");
01989         //double secsPerMinor = (((double)start.daysTo(myHorizonEnd))* 86400.00)/((double)myAutoScaleMinorTickcount);
01990         double secsPerMinor = (((double)start.secsTo(myHorizonEnd)))/((double)myAutoScaleMinorTickcount);
01991         secsPerMinor /= myZoomFactor;
01992         if (secsPerMinor <= 1800) {
01993             myRealScale =  KDGanttView::Minute;
01994             myRealMinorScaleCount = (int) secsPerMinor/60;
01995         } else {
01996             if (secsPerMinor <= 12*3600) {
01997                 myRealScale =  KDGanttView::Hour;
01998                 myRealMinorScaleCount = (int)  secsPerMinor/3600;
01999             } else {
02000                 if (secsPerMinor <= 24*3600*3) {
02001                     myRealScale =  KDGanttView::Day;
02002                     myRealMinorScaleCount = (int)  secsPerMinor/(3600*24);
02003                 } else {
02004                     if (secsPerMinor <= 24*3600*14) {
02005                         myRealScale =  KDGanttView::Week;
02006                         myRealMinorScaleCount =  (int) secsPerMinor/(3600*24*7);
02007                     } else {
02008                         myRealScale =  KDGanttView::Month;
02009                         myRealMinorScaleCount =  (int) secsPerMinor/(3600*24*30);
02010 
02011                     }
02012                 }
02013             }
02014         }
02015         if(myRealMinorScaleCount == 0)
02016             myRealMinorScaleCount = 1;
02017         myRealMajorScaleCount = 1;
02018     }
02019     else {
02020         //tqDebug("Fixed scale ");
02021         myRealScale = myScale;
02022         if (myRealScale > myMaxScale)
02023             myRealScale = myMaxScale;
02024         if (myRealScale < myMinScale)
02025             myRealScale = myMinScale;
02026         myRealMinorScaleCount = (int) ( ((double)myMinorScaleCount) /myZoomFactor );
02027         double tempZoom = myZoomFactor;
02028         myRealMajorScaleCount =  myMajorScaleCount;
02029         while (myRealMinorScaleCount == 0) {
02030             if (myRealScale  == myMinScale) {
02031                 myRealMinorScaleCount = 1;
02032                 break;
02033             }
02034             switch (myRealScale)
02035                 {
02036                 case KDGanttView::Minute:
02037                     myRealMinorScaleCount = 1;
02038                     return;
02039                     break;
02040                 case KDGanttView::Hour:
02041                     myRealScale = KDGanttView::Minute;
02042                     tempZoom = tempZoom/60;
02043                     break;
02044                 case KDGanttView::Day:
02045                     myRealScale = KDGanttView::Hour;
02046                     tempZoom = tempZoom/24;
02047                     break;
02048                 case KDGanttView::Week:
02049                     myRealScale = KDGanttView::Day;
02050                     tempZoom = tempZoom/7;
02051                     break;
02052                 case KDGanttView::Month:
02053                     myRealScale =  KDGanttView::Week ;
02054                     tempZoom = tempZoom*7/30;
02055                     break;
02056                 case KDGanttView::Auto:
02057                     break;
02058                 }
02059             myRealMinorScaleCount =  (int) ( myMinorScaleCount /tempZoom );
02060         }
02061     }
02062 }
02063 
02064 
02065 void KDTimeHeaderWidget::computeTicks(bool doNotComputeRealScale)
02066 {
02067     if (flagDoNotRecomputeAfterChange) return;
02068     bool block = myGanttView->myTimeTable->blockUpdating();
02069     myGanttView->myTimeTable->setBlockUpdating( true );
02070     //tqDebug("computeticks ");
02071     majorTicks.clear();
02072     minorText.clear();
02073     majorText.clear();
02074     if ( !doNotComputeRealScale )
02075         saveCenterDateTime();
02076     if (!doNotComputeRealScale)
02077         computeRealScale(myHorizonStart);
02078     myRealStart = getEvenTimeDate(myHorizonStart ,myRealScale);
02079     if (!doNotComputeRealScale)
02080         computeRealScale(myRealStart);
02081     int tempMinorScaleCount = myRealMinorScaleCount,
02082         tempMajorScaleCount = myRealMajorScaleCount;
02083     int minorItems,minorPerMajor = 1;
02084     minorItems = (int)  (secsFromTo( myRealStart, myHorizonEnd)/60.0);
02085     //tqDebug("tempMinorScaleCount %d ", tempMinorScaleCount);
02086     TQPainter p(this);
02087     int Width, Height;
02088     TQString testTextMinor,testTextMajor, tempStr;
02089     TQRect itemRectMinor, itemRectMajor;
02090     TQDate tempDate = myRealStart.date();
02091     myRealEnd = myRealStart;
02092     // preparing the testtext for the differennt scales
02093     switch (myRealScale)
02094         {
02095             // the x in testTextMajor is added to reserve a little bit more space
02096         case KDGanttView::Minute:
02097             testTextMinor = "60";
02098             if (myHourFormat == KDGanttView::Hour_12)
02099                 testTextMajor = "Mon Aug 30, 12 AMx";
02100             else
02101                 testTextMajor = "Mon Aug 30, 24:00x";
02102             minorPerMajor = 6000;
02103             break;
02104         case KDGanttView::Hour:
02105             minorItems = minorItems/60;
02106             if (myHourFormat == KDGanttView::Hour_24)
02107                 testTextMinor = "24x";
02108             else
02109                 testTextMinor = "12 AM";
02110             testTextMajor = "Mon Aug 30, x";
02111             if ( yearFormat() != KDGanttView::NoDate )
02112                 testTextMajor += getYear(TQDate::currentDate());
02113             minorPerMajor = 2400;
02114             break;
02115         case KDGanttView::Day:
02116             minorItems = minorItems/(60*24);
02117             testTextMinor = "88";
02118             testTextMajor = "Aug 30, x"+getYear(TQDate::currentDate());
02119             minorPerMajor = 700;
02120             break;
02121         case KDGanttView::Week:
02122             minorItems = minorItems/(60*24*7);
02123             testTextMinor = "88";
02124             testTextMajor = "Aug x"+getYear(TQDate::currentDate());
02125             minorPerMajor = 435; // 435 = 365days/12months/7days * 100
02126             break;
02127         case KDGanttView::Month:
02128             minorItems = (minorItems*12)/(60*24*365);
02129             testTextMinor = "M";
02130             testTextMajor = "x"+getYear(TQDate::currentDate());
02131             minorPerMajor = 1200;
02132             break;
02133         case KDGanttView::Auto:
02134             tqDebug("KDGanttView::Internal Error in KDTimeHeaderWidget::computeTicks() ");
02135             tqDebug("             RealScale == Auto : This may not be! ");
02136             break;
02137         }
02138     itemRectMinor = p.boundingRect ( 10, 10, 2, 2, TQt::AlignLeft,testTextMinor);
02139     itemRectMajor = p.boundingRect ( 10, 10, 2, 2, TQt::AlignLeft,testTextMajor);
02140     p.end();
02141     //tqDebug("     tempMinorScaleCount %d ", tempMinorScaleCount);
02142     Height = itemRectMinor.height()+itemRectMajor.height()+11;
02143     Width = (itemRectMinor.width()+5);
02144     if (Width < minimumColumnWidth()) Width = minimumColumnWidth();
02145     // if the desired width is greater than the maximum width of this widget
02146     // increase the minorscalecount
02147     int maxWid = myGanttView->myCanvasView->viewport()->width();
02148     if (!flagZoomToFit)
02149         maxWid = maximumWidth();
02150     while((minorItems/tempMinorScaleCount+1)*Width > maxWid)
02151         ++tempMinorScaleCount;
02152     //tqDebug("             tempMinorScaleCount %d ", tempMinorScaleCount);
02153     mySizeHint = (minorItems/tempMinorScaleCount+1)*Width;
02154     switch (myRealScale)
02155         {
02156         case KDGanttView::Minute:
02157             if (tempMinorScaleCount < 60)
02158                 while (60%tempMinorScaleCount > 0 && 60%tempMinorScaleCount < 60)
02159                     ++tempMinorScaleCount;
02160             if (tempMinorScaleCount >= 60) {
02161                 myRealScale = KDGanttView::Hour;
02162                 myRealMinorScaleCount = tempMinorScaleCount/ 60;
02163                 // myRealMinorScaleCount = 1;
02164                 myRealMajorScaleCount = 1;
02165                 tqDebug("KDGantt::Overzoom:Rescaling from Minute to Hour");
02166                 myGanttView->myTimeTable->setBlockUpdating( block );
02167                 emit myGanttView->rescaling( KDGanttView::Hour );
02168                 computeTicks(true);
02169                 return;
02170             }
02171             break;
02172         case KDGanttView::Hour:
02173             while (24%tempMinorScaleCount > 0 && 24%tempMinorScaleCount < 24)
02174                 ++tempMinorScaleCount;
02175             if (tempMinorScaleCount >= 24) {
02176                 myRealScale = KDGanttView::Day;
02177                 myRealMinorScaleCount = tempMinorScaleCount/ 24;
02178                 //myRealMinorScaleCount = 1;
02179                 myRealMajorScaleCount = 1;
02180                 tqDebug("KDGantt::Overzoom:Rescaling from Hour to Day");
02181                 myGanttView->myTimeTable->setBlockUpdating( block );
02182                 emit myGanttView->rescaling( KDGanttView::Day );
02183                 computeTicks(true);
02184                 return;
02185             }
02186             break;
02187         default:
02188             break;
02189         }
02190     //flagZoomToFit = false;
02191     while((minorItems/tempMinorScaleCount+1)*Width < myMinimumWidth ) {
02192         ++minorItems;
02193     }
02194     minorItems = (minorItems/tempMinorScaleCount)+1;
02195     // if not enough space for the text of the major scale, increase majorscalecount
02196     minorPerMajor = (minorPerMajor*tempMajorScaleCount)/tempMinorScaleCount;
02197     // checking, if enough space for majorscale
02198     // if not, increasing MajorScaleCount
02199 
02200     while ((minorPerMajor*Width)/100 < itemRectMajor.width()) {
02201         minorPerMajor = minorPerMajor/tempMajorScaleCount;
02202         ++tempMajorScaleCount;
02203         minorPerMajor = minorPerMajor*tempMajorScaleCount;
02204 
02205     }
02206     // now we have the fixed  width of the minorscale computed
02207     myGridMinorWidth = Width;
02208     // the width of this widget is the gridwidth * the amount of items
02209     Width *= minorItems;
02210     // if size changed, reset geometry
02211     if (width() != Width   || height() != Height  )
02212         {
02213             resize( Width, Height );
02214             emit  sizeChanged( Width );
02215         }
02216     myMajorGridHeight = itemRectMajor.height()+5;
02217     TQTime tempTime = myRealStart.time();
02218     TQDateTime tempDateTime;
02219     int i;
02220     const KCalendarSystem * calendar = TDEGlobal::locale()->calendar();
02221     switch (myRealScale)
02222         {
02223         case KDGanttView::Minute:
02224             myRealEnd = myRealEnd.addSecs((minorItems)*tempMinorScaleCount*60);
02225             for ( i = 0; i < minorItems;++i) {
02226                 tempStr.setNum(tempTime.minute());
02227                 minorText.append(tempStr);
02228                 tempTime = tempTime.addSecs(60*tempMinorScaleCount);
02229             }
02230             tempDateTime = myRealStart;
02231             while (tempDateTime.time().minute() != 0)
02232                 tempDateTime = tempDateTime.addSecs(60);
02233             while (tempDateTime < myRealEnd) {
02234                 majorTicks.append( getCoordX(tempDateTime));
02235                 tempStr.setNum(tempDateTime.date().day());
02236                 if ( yearFormat() == KDGanttView::NoDate ) {
02237                     tempStr = calendar->weekDayName( tempDateTime.date() )+", "
02238                         +getHour(tempDateTime.time());
02239                 } else {
02240                     tempStr = calendar->weekDayName( tempDateTime.date(), true )+" "+
02241                         calendar->monthName( tempDateTime.date().month(), tempDateTime.date().year(), true)+ " "+
02242                         tempStr+", "+getHour(tempDateTime.time());
02243                 }
02244 
02245                 majorText.append(tempStr);
02246                 tempDateTime = tempDateTime.addSecs(3600*tempMajorScaleCount);
02247             }
02248             majorTicks.append( getCoordX(tempDateTime));
02249             break;
02250 
02251         case KDGanttView::Hour:
02252             myRealEnd = myRealEnd.addSecs(minorItems*tempMinorScaleCount*60*60);
02253 
02254             for ( i = 0; i < minorItems;++i) {
02255                 tempStr = getHour(tempTime);
02256                 minorText.append(tempStr);
02257                 tempTime = tempTime.addSecs(3600*tempMinorScaleCount);
02258             }
02259             tempDateTime = myRealStart;
02260             while (tempDateTime.time().hour() != 0)
02261                 tempDateTime = tempDateTime.addSecs(3600);
02262             while (tempDateTime < myRealEnd) {
02263                 majorTicks.append( getCoordX(tempDateTime));
02264                 tempStr.setNum(tempDateTime.date().day());
02265                 if ( yearFormat() == KDGanttView::NoDate ) {
02266                     tempStr = calendar->weekDayName( tempDateTime.date() );
02267                 } else {
02268                     tempStr = calendar->weekDayName( tempDateTime.date(), true )+" "+
02269                         calendar->monthName( tempDateTime.date().month(), tempDateTime.date().year(), true)+ " "+
02270                         tempStr+", "+getYear(tempDateTime.date());
02271                 }
02272                 majorText.append(tempStr);
02273                 tempDateTime = tempDateTime.addDays(tempMajorScaleCount);
02274             }
02275             majorTicks.append( getCoordX(tempDateTime));
02276             break;
02277         case KDGanttView::Day:
02278             myRealEnd = myRealEnd.addDays(minorItems*tempMinorScaleCount);
02279             for ( i = 0; i < minorItems;++i) {
02280                 if (tempMinorScaleCount == 1)
02281                     minorText.append((calendar->weekDayName(tempDate, true)).left(1)); //TODO: BIDI
02282                 else
02283                     minorText.append(TQString::number(tempDate.day()));
02284                 tempDate = tempDate.addDays(tempMinorScaleCount);
02285             }
02286             tempDate = myRealStart.date();
02287             while (tempDate.dayOfWeek() != TDEGlobal::locale()->weekStartDay())
02288                 tempDate = tempDate.addDays(1);
02289             while (tempDate < myRealEnd.date()) {
02290                 majorTicks.append( getCoordX(tempDate));
02291                 tempStr.setNum(tempDate.day());
02292                 tempStr = calendar->monthName(tempDate.month(), tempDate.year(), true)+ " "+
02293                     tempStr+", "+getYear(tempDate);
02294                 majorText.append(tempStr);
02295                 tempDate = tempDate.addDays(7*tempMajorScaleCount);
02296             }
02297             majorTicks.append( getCoordX(tempDate));
02298             break;
02299         case KDGanttView::Week:
02300             myRealEnd = myRealEnd.addDays(minorItems*tempMinorScaleCount*7);
02301             for ( i = 0; i < minorItems;++i) {
02302                 tempStr.setNum(tempDate.day());
02303                 minorText.append(tempStr);
02304                 tempDate = tempDate.addDays(7*tempMinorScaleCount);
02305             }
02306             tempDate = myRealStart.date();
02307             while (tempDate.day() != TDEGlobal::locale()->weekStartDay())
02308                 tempDate = tempDate.addDays(1);
02309             while (tempDate < myRealEnd.date()) {
02310                 majorTicks.append( getCoordX(tempDate));
02311                 tempStr = calendar->monthName(tempDate.month(), tempDate.year(), true)+ " "+getYear(tempDate);
02312                 majorText.append(tempStr);
02313                 tempDate = tempDate.addMonths(tempMajorScaleCount);
02314             }
02315             majorTicks.append( getCoordX(tempDate));
02316             break;
02317         case KDGanttView::Month:
02318             myRealEnd = myRealEnd.addMonths(minorItems*tempMinorScaleCount);
02319             for ( i = 0; i < minorItems;++i) {
02320                 minorText.append((calendar->monthName(tempDate.month(), tempDate.year(), true)).left(1)); //TODO: BIDI
02321                 tempDate = tempDate.addMonths(tempMinorScaleCount);
02322             }
02323             tempDate = myRealStart.date();
02324             while (tempDate.month() != 1)
02325                 tempDate = tempDate.addMonths(1);
02326             while (tempDate < myRealEnd.date()) {
02327                 majorTicks.append( getCoordX(tempDate));
02328                 tempStr = getYear(tempDate);
02329                 majorText.append(tempStr);
02330                 tempDate = tempDate.addYears(tempMajorScaleCount);
02331             }
02332             majorTicks.append( getCoordX(tempDate));
02333             break;
02334         case KDGanttView::Auto:
02335             break;
02336         }
02337 
02338     if (flagDoNotRepaintAfterChange) {
02339         myGanttView->myTimeTable->setBlockUpdating( block );
02340         return;
02341     }
02342     //tqDebug("KDTimeHeaderWidget width %d, viewport width %d  ",width (), myGanttView->myCanvasView->viewport()->width());
02343     myGanttView->myTimeTable->setBlockUpdating( block );
02344     updateTimeTable();
02345     centerDateTime(myCenterDateTime);
02346     repaint();
02347 }
02348 
02349 
02350 void KDTimeHeaderWidget::saveCenterDateTime()
02351 {
02352     double wid = width();
02353     double allsecs = secsFromTo( myRealStart, myRealEnd );
02354     double center = myGanttView->myCanvasView->viewport()->width();
02355     center = center / 2;
02356     center = center + myGanttView->myCanvasView->contentsX();
02357     double secs = (allsecs*center)/wid;
02358     double days = secs/86400.0;
02359     secs = secs - ( (int) days *86400.0 );
02360     myCenterDateTime =  (myRealStart.addDays ( (int) days )).addSecs( (int) secs);
02361 }
02362 
02363 
02364 void KDTimeHeaderWidget::centerDateTime( const TQDateTime& center )
02365 {
02366      moveTimeLineTo(getCoordX( center )-(myGanttView->myCanvasView->viewport()->width() /2));
02367     //  tqDebug("centerDateTime %s %d %d", center.toString().latin1(),getCoordX( center ),(myGanttView->myCanvasView->viewport()->width() /2) );
02368 
02369 }
02370 
02371 
02372 void KDTimeHeaderWidget::paintEvent(TQPaintEvent *p)
02373 {
02374     repaintMe(p->rect().x(),p->rect().width());
02375 }
02376 
02377 
02378 int KDTimeHeaderWidget::getCoordX(TQDate date)
02379 {
02380     int wid = width();
02381     int daysAll = myRealStart.daysTo(myRealEnd);
02382     if (daysAll == 0) return 0;
02383     int days = myRealStart.daysTo(TQDateTime(date));
02384     return (wid *days) /daysAll;
02385 }
02386 
02387 
02388 int KDTimeHeaderWidget::getCoordX(TQDateTime datetime)
02389 {
02390     double wid = width();
02391     double secsAll = secsFromTo( myRealStart, myRealEnd );
02392     if (secsAll == 0.0) return 0;
02393     double secs = secsFromTo( myRealStart, datetime);
02394     return ((int)((wid *(secs /secsAll))+0.5));
02395 }
02396 
02397 
02398 TQString KDTimeHeaderWidget::getYear(TQDate date)
02399 {
02400     TQString ret;
02401     ret.setNum(date.year());
02402     switch (yearFormat()) {
02403     case KDGanttView::FourDigit:
02404         // nothing to do
02405         break;
02406     case KDGanttView::TwoDigit:
02407         ret = ret.right(2);
02408         break;
02409     case KDGanttView::TwoDigitApostrophe:
02410         ret = "'"+ret.right(2);
02411         break;
02412     case KDGanttView::NoDate:
02413         // nothing to do
02414         break;
02415     }
02416     return ret;
02417 }
02418 
02419 
02420 TQString KDTimeHeaderWidget::getHour(TQTime time)
02421 {
02422     TQString ret;
02423     int hour = time.hour();
02424     if (myHourFormat == KDGanttView::Hour_12) {
02425         if (hour >= 12) {
02426             if (hour > 12) hour -=12;
02427             ret.setNum(hour);
02428             ret = ret +" PM";
02429         } else {
02430             if (hour == 0) hour = 12;
02431             ret.setNum(hour);
02432             ret = ret +" AM";
02433         }
02434     } else {
02435         if (myHourFormat == KDGanttView::Hour_24)
02436             ret.setNum(hour);
02437         else {
02438             ret.setNum(hour);
02439             ret += ":00";
02440         }
02441     }
02442     return ret;
02443 }
02444 
02445 
02446 void KDTimeHeaderWidget::mousePressEvent ( TQMouseEvent * e )
02447 {
02448     mouseDown = false;
02449     switch ( e->button() ) {
02450     case Qt::LeftButton:
02451         mouseDown = true;
02452         beginMouseDown = e->pos().x();
02453         endMouseDown = e->pos().x();
02454         break;
02455     case Qt::RightButton:
02456         if (flagShowPopupMenu)
02457             myPopupMenu->popup(e->globalPos());
02458         break;
02459     case Qt::MidButton:
02460         break;
02461     default:
02462         break;
02463     }
02464 
02465 }
02466 
02467 
02468 void KDTimeHeaderWidget::mouseReleaseEvent ( TQMouseEvent *  )
02469 {
02470     if ( mouseDown ) {
02471         mouseDown = false;
02472         // zoom to selection getDateTimeForIndex(
02473         int start, end;
02474         if ( beginMouseDown < endMouseDown ) {
02475             start = beginMouseDown;
02476             end = endMouseDown;
02477         } else {
02478             start = endMouseDown;
02479             end = beginMouseDown;
02480         }
02481         if (start < 0 )
02482             start = 0;
02483         if ( end > width() )
02484             end = width();
02485         //tqDebug("start %s ",getDateTimeForIndex(start).toString().latin1() );
02486         //tqDebug("end %s ",getDateTimeForIndex(end).toString().latin1() );
02487         emit myGanttView->timeIntervalSelected( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02488         emit myGanttView->timeIntervallSelected( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02489         //zoomToSelection( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02490     }
02491     mouseDown = false;
02492     repaint();
02493 }
02494 
02495 
02496 void KDTimeHeaderWidget::mouseDoubleClickEvent ( TQMouseEvent *  )
02497 {
02498 
02499 }
02500 
02501 
02502 void KDTimeHeaderWidget::mouseMoveEvent ( TQMouseEvent * e )
02503 {
02504     if ( mouseDown ) {
02505         if ( e->pos().y() < -height() || e->pos().y() > 2* height() ) {
02506             mouseDown = false;
02507             repaint();
02508             return;
02509         }
02510         endMouseDown = e->pos().x();
02511         //repaint;
02512         int val = -1;
02513         if (endMouseDown <  -x() ) {
02514             val = myGanttView->myCanvasView->horizontalScrollBar()->value() -
02515                 myGanttView->myCanvasView->horizontalScrollBar()->lineStep();
02516             if ( val < 0 ) {
02517                 val = 0;
02518             }
02519         }
02520         if (endMouseDown >  -x() +parentWidget()->width() ) {
02521             val = myGanttView->myCanvasView->horizontalScrollBar()->value() +
02522                 myGanttView->myCanvasView->horizontalScrollBar()->lineStep();
02523 
02524         }
02525         repaintMe(-x(),parentWidget()->width());
02526         if ( val > -1 ) {
02527             if ( val > myGanttView->myCanvasView->horizontalScrollBar()->maxValue() ) {
02528                 val = myGanttView->myCanvasView->horizontalScrollBar()->maxValue();
02529             }
02530             myGanttView->myCanvasView->horizontalScrollBar()->setValue( val );
02531         }
02532         //tqDebug("mousemove %d %d %d %d",endMouseDown, -x(),parentWidget()->width() , e->pos().y());
02533     }
02534 }
02535 
02536 
02537 /* ***************************************************************
02538    KDLegendWidget:: KDLegendWidget
02539    ***************************************************************** */
02540 KDLegendWidget:: KDLegendWidget( TQWidget* parent,
02541                                  KDGanttMinimizeSplitter* legendParent ) :
02542     KDGanttSemiSizingControl ( KDGanttSemiSizingControl::Before, Qt::Vertical,
02543                                parent)
02544 {
02545     myLegendParent = legendParent;
02546     dock = 0;
02547     scroll = new TQScrollView( legendParent );
02548     setMaximizedWidget( scroll );
02549 
02550     setMinimizedWidget( myLabel = new TQLabel( i18n( " Legend is hidden" ), this) );
02551     setGeometry( 0, 0, 50, 50 );
02552     myLegend = 0;
02553     clearLegend();
02554     showMe ( false );
02555 }
02556 void KDLegendWidget::setAsDoctwindow( bool dockwin )
02557 {
02558     if ( (dock == 0 && !dockwin) || ( dock && dockwin  ) )
02559         return;
02560     if ( dockwin )
02561         {
02562             setMaximizedWidget( 0 );
02563             showMe ( false );
02564             if ( dock ) delete dock;
02565             dock = new TQDockWindow(TQDockWindow:: OutsideDock,0 );
02566             dock->resize( 200, 100 );
02567             dock->setHorizontallyStretchable( true );
02568             dock->setVerticallyStretchable( true );
02569             dock->setCaption(i18n("Legend: ") );
02570             dock->setResizeEnabled (true );
02571             delete myLegend;
02572             myLegend = 0;
02573             delete scroll;
02574             scroll = new TQScrollView( dock );
02575             clearLegend();
02576             dock->setWidget(scroll);
02577             setMaximizedWidget( dock );
02578             showMe ( false );
02579 
02580         } else {
02581             setMaximizedWidget( 0 );
02582             showMe ( false );
02583             delete myLegend;
02584             myLegend = 0;
02585             delete scroll;
02586             delete dock;
02587             dock = 0;
02588             scroll = new TQScrollView( myLegendParent );
02589             clearLegend();
02590             setMaximizedWidget( scroll );
02591             showMe ( false );
02592         }
02593 
02594 }
02595 
02596 
02597 bool KDLegendWidget::asDoctwindow( )
02598 {
02599     if ( dock )
02600         return true;
02601     return false;
02602 }
02603 
02604 
02605 TQDockWindow* KDLegendWidget::dockwindow( )
02606 {
02607     return dock;
02608 }
02609 
02610 
02611 void KDLegendWidget::setFont( TQFont font)
02612 {
02613     myLegend->setFont( font);
02614     myLabel->setFont( font);
02615     TQWidget::setFont( font );
02616 }
02617 
02618 
02619 void KDLegendWidget::drawToPainter( TQPainter *p )
02620 {
02621     p->drawPixmap( 0, 0, TQPixmap::grabWidget( myLegend ) );
02622 }
02623 
02624 
02625 TQSize KDLegendWidget::legendSize()
02626 {
02627     return myLegend->size();
02628 }
02629 
02630 
02631 TQSize KDLegendWidget::legendSizeHint()
02632 {
02633     TQApplication::sendPostedEvents( 0, TQEvent::LayoutHint );
02634     return TQSize( myLegend->sizeHint().width(), myLegend->sizeHint().height()+scroll->horizontalScrollBar()->height());
02635 }
02636 
02637 
02638 void KDLegendWidget::showMe ( bool show )
02639 {
02640     minimize( !show );
02641 }
02642 
02643 
02644 void KDLegendWidget::clearLegend ( )
02645 {
02646     if ( myLegend ) delete myLegend;
02647     if ( dock )
02648         myLegend = new TQGroupBox( 1, Qt::Horizontal, scroll->viewport() );
02649     else
02650         myLegend = new TQGroupBox( 1, Qt::Horizontal, i18n( "Legend" ), scroll->viewport() );
02651     myLegend->setBackgroundColor( TQt::white );
02652     myLegend->setFont( font() );
02653     scroll->addChild(  myLegend );
02654     scroll->setResizePolicy( TQScrollView::AutoOneFit );
02655     myLegend->layout()->setMargin( 11 );
02656     myLegend->setFrameStyle( TQFrame::NoFrame );
02657     if ( dock )
02658         scroll->setMaximumHeight( 32000 );
02659     else
02660         scroll->setMaximumHeight( legendSizeHint().height() );
02661 }
02662 
02663 
02664 void KDLegendWidget::addLegendItem( KDGanttViewItem::Shape shape, const TQColor& shapeColor, const TQString& text )
02665 {
02666     TQLabel * temp;
02667     TQPixmap p = KDGanttView::getPixmap( shape,  shapeColor, TQt::white, 10);
02668     TQWidget *w = new TQWidget( myLegend );
02669     w->setBackgroundColor( TQt::white );
02670     TQHBoxLayout *lay = new TQHBoxLayout( w ,0, 6);
02671     temp = new TQLabel ( w );
02672     lay->addWidget( temp, 0, TQt:: AlignRight);
02673     temp->setPixmap(p);
02674     temp = new TQLabel ( text, w );
02675     temp->setBackgroundColor( TQt::white );
02676     lay->addWidget( temp, 0, TQt:: AlignLeft);
02677     lay->addStretch();
02678     if ( dock )
02679         scroll->setMaximumHeight( 32000 );
02680     else
02681         scroll->setMaximumHeight( legendSizeHint().height() );
02682 }
02683 
02684 
02685 bool KDLegendWidget::isShown ( )
02686 {
02687     return !isMinimized();
02688 }
02689 
02690 
02691 KDListView::KDListView(TQWidget* parent, KDGanttView* gantView):TQListView (parent)
02692 {
02693     myGanttView = gantView;
02694     setAcceptDrops(true);
02695     new KDListViewWhatsThis(viewport(),this);
02696     setRootIsDecorated( true );
02697     setAllColumnsShowFocus( true );
02698     addColumn( i18n( "Task Name" ) );
02699     setSorting( -1 );
02700     //setVScrollBarMode (TQScrollView::AlwaysOn );
02701     setHScrollBarMode (TQScrollView::AlwaysOn );
02702     setDefaultRenameAction(TQListView::Accept);
02703     setColumnWidthMode ( 0,Maximum  );
02704     _calendarMode = false;
02705     // TQObject::connect(this, TQT_SIGNAL (  pressed ( TQListViewItem * )) , this, TQT_SLOT( dragItem( TQListViewItem *))) ;
02706 }
02707 
02708 
02709 void  KDListView::dragItem( TQListViewItem *  )
02710 {
02711     // tqDebug("drag ");
02712     // startDrag();
02713 }
02714 TQString KDListView::getWhatsThisText(TQPoint p)
02715 {
02716     KDGanttViewItem* item = ( KDGanttViewItem* ) itemAt( p );
02717     if ( item )
02718         return item->whatsThisText();
02719     return i18n( "No item Found" );
02720 }
02721 
02722 void  KDListView::setCalendarMode( bool mode )
02723 {
02724     _calendarMode = mode;
02725     // setRootIsDecorated ( ! mode );
02726 }
02727 
02728 void  KDListView::setOpen(TQListViewItem * item, bool open )
02729 {
02730     if (! _calendarMode || ! open ) {
02731         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( false );
02732         TQListView::setOpen ( item, open );
02733         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( true );
02734         return;
02735     }
02736     // we are in calendarmode
02737     // in calendarmode only items can be opened which have subitems which have subitems
02738 
02739     TQListViewItem* temp;
02740     temp = item->firstChild();
02741     bool openItem = false;
02742     while (temp) {
02743         if ( (( KDGanttViewItem*)temp)->displaySubitemsAsGroup() ) {
02744             temp->setVisible( true );
02745             openItem = true;
02746         }
02747         else {
02748             temp->setVisible( false );
02749             //tqDebug(" temp->setVisible( false );");
02750         }
02751         temp = temp->nextSibling();
02752     }
02753     if ( openItem ) {
02754         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( false );
02755         TQListView::setOpen ( item, open );
02756         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( true );
02757     }
02758 }
02759 
02760 
02761 void  KDListView::contentsMouseDoubleClickEvent ( TQMouseEvent * e )
02762 {
02763     TQListView::contentsMouseDoubleClickEvent ( e );
02764     //if ( ! _calendarMode )
02765     // TQListView::contentsMouseDoubleClickEvent ( e );
02766     // else
02767     {
02768 
02769         emit myGanttView->lvItemDoubleClicked ( (KDGanttViewItem*) itemAt(e->pos() ) );
02770         emit myGanttView->itemDoubleClicked ( (KDGanttViewItem*) itemAt(e->pos() ) );
02771     }
02772 
02773 }
02774 
02775 
02776 void  KDListView::drawToPainter ( TQPainter * p, bool drawHeader )
02777 {
02778     // Draw list
02779     drawAllContents ( p, 0, 0, contentsWidth(), contentsHeight() );
02780     if (!drawHeader) {
02781         return;
02782     }
02783     // Draw headers
02784     TQPen pen = TQPen(TQt::lightGray, 1);
02785     p->save();
02786     TQHeader *h = header();
02787     for (int s = 0; s < h->count(); ++s) {
02788         TQRect r = h->sectionRect(s);
02789         if (s==0) {
02790             p->translate(0, -r.height());
02791         }
02792         //kdDebug()<<s<<": "<<h->label(s)<<" "<<r<<endl;
02793         p->drawText(r.x()+2, r.y(), r.width()-2, r.height(), columnAlignment(s)|TQt::AlignVCenter, h->label(s), -1);
02794         p->save();
02795         p->setPen(pen);
02796         p->drawRect(r.x(), r.y()+1, r.width(), r.height()-2);
02797         p->restore();
02798 
02799     }
02800     p->restore();
02801 }
02802 
02803 int KDListView::buildDrawables(TQPtrList<KDListView::DrawableItem> &lst, int level, int ypos, TQListViewItem *item, int ymin, int ymax) const {
02804     int y = ypos;
02805     int ih = item->height();
02806     if (y < ymin && y+ih > ymin) {
02807         y = ymin; // include partial item at top
02808     }
02809     if (y >= ymin && y < ymax) { // include partial item at bottom
02810         KDListView::DrawableItem *dr = new KDListView::DrawableItem(level, y, item);
02811         lst.append(dr);
02812         //kdDebug()<<k_funcinfo<<level<<", "<<y<<" : "<<item->text(0)<<endl;
02813     }
02814     y += ih;
02815     if (item->isOpen()) {
02816         TQListViewItem *child = item->firstChild();
02817         for (; child; child = child->nextSibling()) {
02818             y = buildDrawables(lst, level+1, y, child, ymin, ymax);
02819         }
02820     }
02821     return y;
02822 }
02823 // This is a copy of TQListView::drawContentsOffset(), with a few changes
02824 // because drawContentsOffset() only draws *visible* items,
02825 // we want to draw *all* items.
02826 // FIXME: Haven't got paintBraches() to work, atm live without it.
02827 void KDListView::drawAllContents(TQPainter * p, int cx, int cy, int cw, int ch) {
02828     if ( columns() == 0 ) {
02829         paintEmptyArea( p, TQRect( cx, cy, cw, ch ) );
02830         return;
02831     }
02832     //kdDebug()<<k_funcinfo<<TQRect(cx, cy, cw, ch)<<endl;
02833     TQPtrList<KDListView::DrawableItem> drawables;
02834     drawables.setAutoDelete(true);
02835     TQListViewItem *child = firstChild();
02836     int level = 0;
02837     int ypos = 0;
02838     for (; child; child = child->nextSibling()) {
02839         ypos = buildDrawables(drawables, level, ypos, child, cy, cy+ch);
02840     }
02841 
02842     p->setFont( font() );
02843 
02844     TQPtrListIterator<KDListView::DrawableItem> it(drawables);
02845 
02846     TQRect r;
02847     int fx = -1, x, fc = 0, lc = 0;
02848     int tx = -1;
02849     KDListView::DrawableItem * current;
02850 
02851     while ( (current = it.current()) != 0 ) {
02852         ++it;
02853         int ih = current->i->height();
02854         int ith = current->i->totalHeight();
02855         int c;
02856         int cs;
02857 
02858         // need to paint current?
02859         if ( ih > 0 && current->y < cy+ch && current->y+ih > cy ) {
02860             //kdDebug()<<k_funcinfo<<"Paint: "<<current->i->text(0)<<" y="<<current->y<<endl;
02861             if ( fx < 0 ) {
02862                 // find first interesting column, once
02863                 x = 0;
02864                 c = 0;
02865                 cs = header()->cellSize( 0 );
02866                 while ( x + cs <= cx && c < header()->count() ) {
02867                     x += cs;
02868                     c++;
02869                     if ( c < header()->count() )
02870                         cs = header()->cellSize( c );
02871                 }
02872                 fx = x;
02873                 fc = c;
02874                 while( x < cx + cw && c < header()->count() ) {
02875                     x += cs;
02876                     c++;
02877                     if ( c < header()->count() )
02878                         cs = header()->cellSize( c );
02879                 }
02880                 lc = c;
02881             }
02882 
02883             x = fx;
02884             c = fc;
02885             // draw to last interesting column
02886 
02887             const TQColorGroup &cg = ( palette().inactive() );
02888 
02889             while ( c < lc && !drawables.isEmpty() ) {
02890                 int i = header()->mapToLogical( c );
02891                 cs = header()->cellSize( c );
02892                 r.setRect( x, current->y-cy, cs, ih );
02893                 if ( i == 0 )
02894                     r.setLeft( r.left() + current->l * treeStepSize() );
02895 
02896                 p->save();
02897                 // No need to paint if the cell isn't technically visible
02898                 if ( !( r.width() == 0 || r.height() == 0 ) ) {
02899                     p->translate( r.left(), r.top() );
02900                     int ac = header()->mapToLogical( c );
02901                     // map to Left currently. This should change once we
02902                     // can really reverse the listview.
02903                     int align = columnAlignment( ac );
02904                     if ( align == AlignAuto ) align = AlignLeft;
02905                     bool sel = current->i->isSelected();
02906                     if (sel)
02907                         current->i->setSelected(false);
02908                     current->i->paintCell( p, cg, ac, r.width(), align );
02909                     if (sel)
02910                         current->i->setSelected(sel);
02911                 }
02912                 p->restore();
02913                 x += cs;
02914                 c++;
02915             }
02916 
02917         }
02918 
02919         const int cell = header()->mapToActual( 0 );
02920 
02921         if ( tx < 0 )
02922             tx = header()->cellPos( cell );
02923 
02924         // do any children of current need to be painted?
02925 /* FIXME: painting branches doesn't work for some reason...
02926         if ( ih != ith &&
02927             rootIsDecorated() &&
02928             current->y + ith > cy &&
02929             current->y + ih < cy + ch &&
02930             tx + current->l * treeStepSize() < cx + cw &&
02931             tx + (current->l+1) * treeStepSize() > cx ) {
02932             // compute the clip rectangle the safe way
02933 
02934             int rtop = current->y + ih;
02935             int rbottom = current->y + ith;
02936             int rleft = tx + current->l*treeStepSize();
02937             int rright = rleft + treeStepSize();
02938 
02939             int crtop = TQMAX( rtop, cy );
02940             int crbottom = TQMIN( rbottom, cy+ch );
02941             int crleft = TQMAX( rleft, cx );
02942             int crright = TQMIN( rright, cx+cw );
02943 
02944             r.setRect( crleft, crtop,
02945                     crright-crleft, crbottom-crtop );
02946 
02947             if ( r.isValid() ) {
02948                 p->save();
02949                 p->translate( rleft, crtop );
02950                 //kdDebug()<<k_funcinfo<<"paintBranches: "<<current->i->text(0)<<endl;
02951 
02952                 current->i->paintBranches( p, colorGroup(), treeStepSize(),
02953                                         rtop - crtop, r.height() );
02954                 p->restore();
02955             }
02956         }*/
02957     }
02958 }
02959 
02960 void KDListView::resizeEvent(TQResizeEvent *)
02961 {
02962     triggerUpdate ();
02963 }
02964 void KDListView::dragEnterEvent ( TQDragEnterEvent * e)
02965 {
02966     if ( !myGanttView->dropEnabled() ) {
02967         e->accept( false );
02968         return;
02969     }
02970     myGanttView->lvDragEnterEvent(e);
02971     //e->accept(KDGanttViewItemDrag::canDecode(e) );
02972 }
02973 
02974 void KDListView::dragMoveEvent ( TQDragMoveEvent * e)
02975 {
02976     if ( !myGanttView->dropEnabled() ) {
02977         e->accept( false );
02978         return;
02979     }
02980     KDGanttViewItem* draggedItem = 0;
02981     KDGanttViewItem* gItem = (KDGanttViewItem*)itemAt( e->pos()) ;
02982     setCurrentItem( gItem );
02983     if (  e->source() == myGanttView )
02984         draggedItem = myGanttView->myCanvasView->lastClickedItem;
02985     // execute user defined dragMoveEvent handling
02986     if (myGanttView->lvDragMoveEvent ( e , draggedItem, gItem ) )
02987         return;
02988     if ( !KDGanttViewItemDrag::canDecode(e) ) {
02989         e->accept( false );
02990         return;
02991     }
02992     if ( e->source() == myGanttView && gItem ){
02993         // internal drag - do not allow to drag the item to a subitem of itself
02994         KDGanttViewItem* pItem = gItem->parent();
02995         while ( pItem ) {
02996             if ( pItem == myGanttView->myCanvasView->lastClickedItem ) {
02997                 e->accept( false );
02998                 return;
02999             }
03000             pItem = pItem->parent();
03001         }
03002         if ( gItem == myGanttView->myCanvasView->lastClickedItem ) {
03003             e->accept( false );
03004             return;
03005         }
03006     }
03007     e->accept( true );
03008 }
03009 
03010 void KDListView::dragLeaveEvent ( TQDragLeaveEvent * )
03011 {
03012     //tqDebug("contentsDragLeaveEvent ");
03013 }
03014 void KDListView::dropEvent ( TQDropEvent *e )
03015 {
03016     if ( !myGanttView->dropEnabled() ) {
03017         e->accept( false );
03018         return;
03019     }
03020     KDGanttViewItem* gItem = (KDGanttViewItem*)itemAt( e->pos()) ;
03021     KDGanttViewItem* draggedItem = 0;
03022     if (  e->source() == myGanttView )
03023         draggedItem = myGanttView->myCanvasView->lastClickedItem;
03024     if (myGanttView->lvDropEvent ( e, draggedItem, gItem ))
03025         return;
03026     TQString string;
03027     KDGanttViewItemDrag::decode( e, string );
03028     KDGanttViewItem* newItem = 0;
03029 
03030     if ( gItem == myGanttView->myCanvasView->lastClickedItem && gItem != 0 ) {
03031         tqDebug("KDGanttView::Possible bug in drag&drop code ");
03032         return;
03033     }
03034 
03035     TQDomDocument doc( "GanttView" );
03036     doc.setContent( string );
03037     TQDomElement docRoot = doc.documentElement(); // ChartParams element
03038     TQDomNode node = docRoot.firstChild();
03039     bool enable = myGanttView->myTimeTable->blockUpdating( );
03040     myGanttView->myTimeTable->setBlockUpdating( true );
03041     while( !node.isNull() ) {
03042         TQDomElement element = node.toElement();
03043         if( !element.isNull() ) { // was really an element
03044             TQString tagName = element.tagName();
03045             if( tagName == "Items" ) {
03046                 TQDomNode node = element.firstChild();
03047                 while( !node.isNull() ) {
03048                     TQDomElement element = node.toElement();
03049                     if( !element.isNull() ) { // was really an element
03050                         TQString tagName = element.tagName();
03051                         if( tagName == "Item" ) {
03052                             if (  gItem )
03053                                 newItem = KDGanttViewItem::createFromDomElement( gItem,
03054                                                                                  element );
03055                             else
03056                                 newItem = KDGanttViewItem::createFromDomElement( myGanttView,
03057                                                                                  element );
03058                         } else {
03059                             tqDebug( "Unrecognized tag name: %s", tagName.latin1() );
03060                             Q_ASSERT( false );
03061                         }
03062                     }
03063                     //tqDebug("next node1 ");
03064                     node = node.nextSibling();
03065                 }
03066             }
03067         }
03068         //tqDebug("next node2 ");
03069         node = node.nextSibling();
03070     }
03071     newItem->setDisplaySubitemsAsGroup(myGanttView->displaySubitemsAsGroup());
03072     newItem->resetSubitemVisibility();
03073     myGanttView->slot_lvDropped(e, draggedItem, gItem);
03074     myGanttView->myTimeTable->setBlockUpdating( enable );
03075     myGanttView->myTimeTable->updateMyContent();
03076     return;
03077 }
03078 
03079 TQDragObject * KDListView::dragObject ()
03080 {
03081     return TQListView::dragObject ();
03082 }
03083 
03084 void KDListView::startDrag ()
03085 {
03086     if ( ! myGanttView->dragEnabled() )
03087         return;
03088     KDGanttViewItem* cItem = (KDGanttViewItem*) currentItem ();
03089     myGanttView->myCanvasView->lastClickedItem = cItem;
03090     myGanttView->lvStartDrag (cItem);
03091 }
03092 
03093 KDCanvasText::KDCanvasText( KDTimeTableWidget* canvas,
03094                             void* parentItem,
03095                             int type ) :
03096     TQCanvasText(canvas)
03097 {
03098     myParentType = type;
03099     myParentItem = parentItem;
03100 }
03101 
03102 
03103 KDCanvasLine::KDCanvasLine( KDTimeTableWidget* canvas,
03104                             void* parentItem,
03105                             int type ) :
03106     TQCanvasLine(canvas)
03107 {
03108     myParentType = type;
03109     myParentItem = parentItem;
03110 }
03111 
03112 
03113 KDCanvasPolygonItem::KDCanvasPolygonItem( KDTimeTableWidget* canvas,
03114                                           void* parentItem,
03115                                           int type ) :
03116     TQCanvasPolygonalItem( canvas )
03117 {
03118     myParentType = type;
03119     myParentItem = parentItem;
03120 }
03121 
03122 
03123 KDCanvasPolygon::KDCanvasPolygon( KDTimeTableWidget* canvas,
03124                                   void* parentItem,
03125                                   int type ) :
03126     TQCanvasPolygon( canvas )
03127 {
03128     myParentType = type;
03129     myParentItem = parentItem;
03130 }
03131 
03132 
03133 KDCanvasEllipse::KDCanvasEllipse( KDTimeTableWidget* canvas,
03134                                   void* parentItem,
03135                                   int type ) :
03136     TQCanvasEllipse( canvas )
03137 {
03138     myParentType = type;
03139     myParentItem = parentItem;
03140 }
03141 
03142 
03143 KDCanvasRectangle::KDCanvasRectangle( KDTimeTableWidget* canvas,
03144                                       void* parentItem,
03145                                       int type ) :
03146     TQCanvasRectangle( canvas )
03147 {
03148     myParentType = type;
03149     myParentItem = parentItem;
03150 }
03151 
03152 
03153 
03154 
03155 KDGanttCanvasView::KDGanttCanvasView( KDGanttView* sender,TQCanvas* canvas, TQWidget* parent,  const
03156     char* name ) : TQCanvasView ( canvas, parent, name ),
03157     movingGVItem( 0 ),
03158     scrollBarTimer( 0, "scrollBarTimer" )
03159 {
03160     setHScrollBarMode (TQScrollView::AlwaysOn );
03161     setVScrollBarMode( TQScrollView::AlwaysOn );
03162     myToolTip = new KDCanvasToolTip(viewport(),this);
03163     mySignalSender =  sender;
03164     currentItem = 0;
03165     currentLink = 0;
03166     cuttedItem = 0;
03167     fromItem = 0;
03168     fromArea = 0;
03169     linkItemsEnabled = false;
03170     mouseDown = false;
03171     linkLine = new TQCanvasLine(canvas);
03172     linkLine->hide();
03173     linkLine->setZ(1000);
03174     set_Mouse_Tracking(true); // mouse cursor changes over KDIntervalColorRectangle borders
03175     new KDCanvasWhatsThis(viewport(),this);
03176     onItem = new TQPopupMenu( this );
03177     TQPopupMenu * newMenu = new TQPopupMenu( this );
03178     TQPopupMenu * onView = new TQPopupMenu( this );
03179     onView->insertItem( i18n( "Summary" ), this,
03180                         TQT_SLOT ( newRootItem( int ) ), 0, 0 );
03181     onView->insertItem( i18n( "Event" ), this,
03182                         TQT_SLOT ( newRootItem( int ) ), 0, 1);
03183     onView->insertItem( i18n( "Task" ), this,
03184                         TQT_SLOT ( newRootItem( int ) ), 0, 2 );
03185 
03186     onItem->insertItem( i18n( "New Root" ), onView );
03187     newMenu->insertItem( i18n( "Summary" ),
03188                          this, TQT_SLOT ( newChildItem(  int) ), 0, 0 );
03189     newMenu->insertItem( i18n( "Event" ),
03190                          this, TQT_SLOT ( newChildItem( int ) ), 0, 1 );
03191     newMenu->insertItem( i18n( "Task" ),
03192                          this, TQT_SLOT ( newChildItem( int ) ), 0, 2 );
03193 
03194     onItem->insertItem( i18n( "New Child" ), newMenu );
03195     TQPopupMenu * afterMenu = new TQPopupMenu( this );
03196     afterMenu->insertItem( i18n( "Summary" ),
03197                            this, TQT_SLOT ( newChildItem(  int) ), 0, 0+4 );
03198     afterMenu->insertItem( i18n( "Event" ),
03199                            this, TQT_SLOT ( newChildItem( int ) ), 0, 1+4 );
03200     afterMenu->insertItem( i18n( "Task" ),
03201                            this, TQT_SLOT ( newChildItem( int ) ), 0, 2+4 );
03202     onItem->insertItem( i18n( "New After" ), afterMenu );
03203     TQPopupMenu *pasteMenu = new TQPopupMenu( this );
03204     pasteMenu->insertItem( i18n( "As Root" ),
03205                            this, TQT_SLOT ( pasteItem( int ) ), 0, 0 );
03206     pasteMenu->insertItem( i18n( "As Child" ),
03207                            this, TQT_SLOT ( pasteItem( int ) ), 0, 1 );
03208     pasteMenu->insertItem( i18n( "After" ),
03209                            this, TQT_SLOT ( pasteItem( int ) ), 0, 2 );
03210     onItem->insertItem( i18n( "Paste" ), pasteMenu, 3 );
03211     onItem->insertItem( i18n( "Cut Item" ), this, TQT_SLOT ( cutItem() ) );
03212     onItem->setItemEnabled( 3, false );
03213     myMyContentsHeight = 0;
03214     _showItemAddPopupMenu = false;
03215 
03216     TQObject *scrollViewTimer = child( "scrollview scrollbar timer", TQTIMER_OBJECT_NAME_STRING, false );
03217     Q_ASSERT( scrollViewTimer );
03218     if ( scrollViewTimer ) {
03219         disconnect( scrollViewTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateScrollBars() ) );
03220     }
03221     // If they needed a scrollbar timer in scrollview...
03222     connect( &scrollBarTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(myUpdateScrollBars() ) );
03223 
03224     myScrollTimer = new TQTimer( this, "myScrollTimer" );
03225     connect( myScrollTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotScrollTimer() ) );
03226     autoScrollEnabled = false;
03227 }
03228 
03229 
03230 KDGanttCanvasView::~KDGanttCanvasView()
03231 {
03232     delete myToolTip;
03233 }
03234 
03235 
03236 void KDGanttCanvasView::setShowPopupMenu( bool show )
03237 {
03238     _showItemAddPopupMenu = show;
03239 }
03240 bool KDGanttCanvasView::showPopupMenu()
03241 {
03242     return _showItemAddPopupMenu;
03243 }
03244 
03245 
03246 void KDGanttCanvasView::moveMyContent( int, int y)
03247 {
03248     setContentsPos(contentsX(), y);
03249 }
03250 
03251 void KDGanttCanvasView::resizeEvent ( TQResizeEvent * e )
03252 {
03253     int ho = e->oldSize().height();
03254     int wo = e->oldSize().width();
03255     int hi = height();
03256     int wi = width();
03257     //TQScrollView::blockSignals( true );
03258 
03259     verticalScrollBar()->setUpdatesEnabled( false );
03260     TQScrollView::resizeEvent ( e ) ;
03261     if ( ho != hi )
03262         emit heightResized( viewport()->height());
03263     if ( wo != wi )
03264         emit widthResized( viewport()->width() );
03265     //setMyContentsHeight( 0 ); // via timer
03266     //TQScrollView::blockSignals( false );
03267     scrollBarTimer.start(0, true);
03268 }
03269 
03270 void KDGanttCanvasView::myUpdateScrollBars()
03271 {
03272     setMyContentsHeight( 0 );
03273 }
03274 void KDGanttCanvasView::setMyContentsHeight( int hei )
03275 {
03276     //tqDebug("setMyContentsHeight %d %d ", hei,  myMyContentsHeight);
03277     if ( hei > 0 )
03278         myMyContentsHeight = hei;
03279     verticalScrollBar()->setUpdatesEnabled( true ); // set false in resizeEvent()
03280     if ( viewport()->height() <= myMyContentsHeight )
03281         verticalScrollBar()->setRange( 0, myMyContentsHeight- viewport()->height()+1);
03282     else
03283         verticalScrollBar()->setRange( 0,0 );
03284     // testing for unmatching ScrollBar values of timeheader and timetable
03285     // may happen after external resizing
03286     if ( horizontalScrollBar()->value() != mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->value() ) {
03287         // I am the Boss!
03288         mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->setValue(horizontalScrollBar()->value()  );
03289 
03290     }
03291 
03292 }
03293 
03294 // Call after *internal* resizing (like addTickRight())
03295 // Then the new scrollbar maxValue is in myTimeHeader.
03296 void KDGanttCanvasView::updateHorScrollBar() {
03297     //tqDebug("horizontalScrollBar max=%d, myTimeHeaderScroll=%d", horizontalScrollBar()->maxValue(), mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->value());
03298 
03299     horizontalScrollBar()->setRange(mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->minValue(), mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->maxValue());
03300 
03301 }
03302 
03303 void  KDGanttCanvasView::cutItem( KDGanttViewItem* item )
03304 {
03305     lastClickedItem = item;
03306     cutItem();
03307 }
03308 void  KDGanttCanvasView::insertItemAsRoot( KDGanttViewItem* item )
03309 {
03310     mySignalSender->myListView->insertItem( item  );
03311     if ( item == cuttedItem )
03312         cuttedItem = 0;
03313 }
03314 void  KDGanttCanvasView::insertItemAsChild( KDGanttViewItem* parent, KDGanttViewItem* item )
03315 {
03316     parent->insertItem( cuttedItem );
03317     if ( item == cuttedItem )
03318         cuttedItem = 0;
03319 }
03320 void  KDGanttCanvasView::insertItemAfter( KDGanttViewItem* parent , KDGanttViewItem* item )
03321 {
03322     if ( parent->parent() ) {
03323         parent->parent()->insertItem( item );
03324     }
03325     else
03326         mySignalSender->myListView->insertItem( item );
03327     item->moveItem( parent );
03328     if ( item == cuttedItem )
03329         cuttedItem = 0;
03330 }
03331 
03332 void  KDGanttCanvasView::cutItem()
03333 {
03334     lastClickedItem->hideSubtree();
03335     //tqDebug("last clicked %d parent %d ", lastClickedItem  , lastClickedItem->parent());
03336     if ( lastClickedItem->parent() )
03337         lastClickedItem->parent()->takeItem(lastClickedItem);
03338     else
03339         mySignalSender->myListView->takeItem( lastClickedItem );
03340     mySignalSender->myTimeTable->updateMyContent();
03341     if ( cuttedItem )
03342         delete cuttedItem;
03343     cuttedItem = lastClickedItem;
03344     onItem->setItemEnabled( 3, true );
03345 
03346 }
03347 // called from the destructor in KDGanttViewItem or KDGanttView
03348 
03349 void  KDGanttCanvasView::resetCutPaste( KDGanttViewItem* item )
03350 {
03351     if ( item == 0 && cuttedItem ) {
03352         delete cuttedItem;
03353         cuttedItem = 0;
03354     }
03355     if (item == cuttedItem) {
03356         onItem->setItemEnabled( 3, false );
03357         cuttedItem = 0;
03358     }
03359 }
03360 
03361 void  KDGanttCanvasView::pasteItem( int type )
03362 {
03363     if ( !cuttedItem )
03364         return;
03365     switch( type ) {
03366     case 0://root
03367         mySignalSender->myListView->insertItem( cuttedItem );
03368         break;
03369     case 1://child
03370         lastClickedItem->insertItem( cuttedItem );
03371         break;
03372     case 2://after
03373         if ( lastClickedItem->parent() ) {
03374             lastClickedItem->parent()->insertItem( cuttedItem );
03375         }
03376         else
03377             mySignalSender->myListView->insertItem( cuttedItem );
03378         cuttedItem->moveItem( lastClickedItem );
03379         break;
03380     default:
03381         ;
03382     }
03383     cuttedItem = 0;
03384     onItem->setItemEnabled( 3, false );
03385     mySignalSender->myTimeTable->updateMyContent();
03386 }
03387 void  KDGanttCanvasView::newRootItem(int type)
03388 {
03389     KDGanttViewItem* temp = 0;
03390     switch( type ) {
03391     case 1:
03392         temp = new KDGanttViewEventItem( mySignalSender, i18n( "New Event" ) );
03393         break;
03394     case 0:
03395         temp = new KDGanttViewSummaryItem( mySignalSender, i18n( "New Summary" ) );
03396         break;
03397     case 2:
03398         temp = new KDGanttViewTaskItem( mySignalSender, i18n( "New Task" ) );
03399         break;
03400     default:
03401         ;
03402     }
03403     if ( temp )
03404         mySignalSender->editItem( temp );
03405 }
03406 
03407 void  KDGanttCanvasView::newChildItem( int type )
03408 {
03409     KDGanttViewItem* temp = 0;
03410     switch( type ) {
03411     case 1:
03412         temp = new KDGanttViewEventItem( lastClickedItem, i18n( "New Event" ) );
03413         break;
03414     case 0:
03415         temp = new KDGanttViewSummaryItem( lastClickedItem, i18n( "New Summary" ) );
03416         break;
03417     case 2:
03418         temp = new KDGanttViewTaskItem( lastClickedItem, i18n( "New Task" ) );
03419         break;
03420     case 5:
03421         if ( lastClickedItem->parent() )
03422             temp = new KDGanttViewEventItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Event" ) );
03423         else
03424             temp = new KDGanttViewEventItem( mySignalSender, lastClickedItem, i18n( "New Event" ) );
03425         break;
03426     case 4:
03427         if ( lastClickedItem->parent() )
03428             temp = new KDGanttViewSummaryItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Summary" ) );
03429         else
03430             temp = new KDGanttViewSummaryItem( mySignalSender, lastClickedItem, i18n( "New Summary" ) );
03431         break;
03432     case 6:
03433         if ( lastClickedItem->parent() )
03434             temp = new KDGanttViewTaskItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Task" ) );
03435         else
03436             temp = new KDGanttViewTaskItem( mySignalSender, lastClickedItem, i18n( "New Task" ) );
03437         break;
03438 
03439 
03440     default:
03441         ;
03442     }
03443     if ( temp )
03444         mySignalSender->editItem( temp );
03445 }
03446 
03447 void  KDGanttCanvasView::drawToPainter ( TQPainter * p )
03448 {
03449     drawContents ( p, 0, 0, canvas()->width(), canvas()->height() );
03450 }
03451 TQString  KDGanttCanvasView::getToolTipText(TQPoint p)
03452 {
03453     TQCanvasItemList il = canvas()->collisions ( viewportToContents( p ));
03454     TQCanvasItemList::Iterator it;
03455     for ( it = il.begin(); it != il.end(); ++it ) {
03456         switch (getType(*it)) {
03457         case Type_is_KDGanttViewItem:
03458             return (getItem(*it))->tooltipText();
03459             break;
03460         case Type_is_KDGanttTaskLink:
03461             return (getLink(*it))->tooltipText();
03462             break;
03463         default:
03464             break;
03465         }
03466     }
03467     return "";
03468 }
03469 
03470 TQString  KDGanttCanvasView::getWhatsThisText(TQPoint p)
03471 {
03472     TQCanvasItemList il = canvas() ->collisions (viewportToContents( p ));
03473     TQCanvasItemList::Iterator it;
03474     for ( it = il.begin(); it != il.end(); ++it ) {
03475         switch (getType(*it)) {
03476         case Type_is_KDGanttViewItem:
03477             return (getItem(*it))->whatsThisText();
03478             break;
03479         case Type_is_KDGanttTaskLink:
03480             return (getLink(*it))->whatsThisText();
03481             break;
03482         default:
03483             break;
03484         }
03485     }
03486     return "";
03487 }
03488 
03489 
03490 KDGanttCanvasView::MovingOperation KDGanttCanvasView::gvItemHitTest( KDGanttViewItem *item, KDTimeHeaderWidget* timeHeader, const TQPoint &pos )
03491 {
03492   const int left = timeHeader->getCoordX( item->startTime() );
03493   const int right = timeHeader->getCoordX( item->endTime() );
03494   const int width = right - left + 1;
03495   const int x = pos.x();
03496   if ( x < left + width / 10 )
03497     return KDGanttCanvasView::ResizingLeft;
03498   if ( x > right - width / 10 )
03499     return KDGanttCanvasView::ResizingRight;
03500   return KDGanttCanvasView::Moving;
03501 }
03502 
03510 void KDGanttCanvasView::contentsMousePressEvent ( TQMouseEvent * e )
03511 {
03512     //tqDebug("mousepress! %d ", this);
03513     //tqDebug("focus %d ",tqApp->focusWidget());
03514     setFocus();
03515     currentLink = 0;
03516     currentItem = 0;
03517     movingItem = 0;
03518     mouseDown = true;
03519     if (e->button() == Qt::RightButton && mySignalSender->editable()) {
03520         lastClickedItem = (KDGanttViewItem*) mySignalSender->myListView->itemAt( TQPoint(2,e->pos().y()));
03521         if ( lastClickedItem ) {
03522             if ( lastClickedItem->displaySubitemsAsGroup() && ! lastClickedItem->isOpen() ) {
03523                 // findSub subitem
03524                 TQCanvasItemList il = canvas() ->collisions ( e->pos() );
03525                 TQCanvasItemList::Iterator it;
03526                 for ( it = il.begin(); it != il.end(); ++it ) {
03527                     if ( getType(*it) == Type_is_KDGanttViewItem ) {
03528                         lastClickedItem = getItem(*it);
03529                     }
03530                 }
03531             }
03532             if ( _showItemAddPopupMenu  )
03533                 onItem->popup(e->globalPos());
03534         }
03535     }
03536     TQCanvasItemList il = canvas() ->collisions ( e->pos() );
03537     TQCanvasItemList::Iterator it;
03538     for ( it = il.begin(); it != il.end(); ++it ) {
03539         switch ( e->button() ) {
03540         case Qt::LeftButton:
03541             switch (getType(*it)) {
03542             case Type_is_KDGanttViewItem:
03543                 currentItem = getItem(*it);
03544                 if (! currentItem->enabled() ) {
03545                     currentItem = 0;
03546                 } else if (linkItemsEnabled &&
03547                            !currentItem->isMyTextCanvas(*it)) {
03548                     fromArea = getItemArea(currentItem, e->pos().x());
03549                     if (fromArea > 0) {
03550                         fromItem = currentItem;
03551                         linkLine->setPoints(e->pos().x(), e->pos().y(), e->pos().x(), e->pos().y());
03552                         linkLine->show();
03553                     }
03554                 }
03555                 {
03556                   KDCanvasRectangle *rect = dynamic_cast<KDCanvasRectangle*>( *it );
03557                   if ( rect ) {
03558                     movingGVItem = dynamic_cast<KDGanttViewTaskItem*>( getItem( rect ) );
03559                     if ( movingGVItem ) {
03560                       movingStart = e->pos();
03561                       movingStartDate = movingGVItem->startTime();
03562                       movingOperation = gvItemHitTest( movingGVItem, mySignalSender->myTimeHeader, e->pos() );
03563                       if ( movingOperation == Moving && !movingGVItem->isMoveable() )
03564                         movingGVItem = 0;
03565                       else if ( movingOperation != Moving && !movingGVItem->isResizeable() )
03566                         movingOperation = Moving;
03567                     } else {
03568                       movingGVItem = 0;
03569                     }
03570                   }
03571                 }
03572                 break;
03573             case Type_is_KDGanttTaskLink:
03574                 currentLink = getLink(*it);
03575                 break;
03576             case Type_is_KDGanttGridItem:
03577               if ( (*it)->rtti() == KDIntervalColorRectangle::RTTI ) {
03578                 // Cleaner would be isMovable()/isResizeable() in an interface
03579                 // implemented by all movable objects...
03580                 movingItem = static_cast<TQCanvasRectangle *>(*it);
03581                 movingStart = e->pos();
03582                 KDIntervalColorRectangle* icr = static_cast<KDIntervalColorRectangle *>( movingItem );
03583                 KDIntervalColorRectangle::HitTest hitTest = icr->hitTest( mySignalSender->myTimeHeader, movingStart );
03584                 movingOperation = hitTest == KDIntervalColorRectangle::Start ? ResizingLeft :
03585                                   hitTest == KDIntervalColorRectangle::End ? ResizingRight :
03586                                   Moving;
03587               }
03588               break;
03589             default:
03590                 break;
03591             }
03592             break;
03593         case Qt::RightButton:
03594             switch (getType(*it)) {
03595             case Type_is_KDGanttViewItem:
03596                 currentItem = getItem(*it);
03597                 if (! currentItem->enabled() )
03598                     currentItem = 0;
03599                 break;
03600             case Type_is_KDGanttTaskLink:
03601                 currentLink = getLink(*it);
03602                 break;
03603             }
03604             break;
03605         case Qt::MidButton:
03606             switch (getType(*it)) {
03607             case Type_is_KDGanttViewItem:
03608                 currentItem = getItem(*it);
03609                 if (! currentItem->enabled() )
03610                     currentItem = 0;
03611                 break;
03612             case Type_is_KDGanttTaskLink:
03613                 currentLink = getLink(*it);
03614                 break;
03615             }
03616             break;
03617         default:
03618             break;
03619         }
03620     }
03621     if (e->button() == Qt::RightButton ) {
03622         mySignalSender->gvContextMenuRequested( currentItem, e->globalPos() );
03623     }
03624     if (autoScrollEnabled && e->button() == Qt::LeftButton) {
03625         myScrollTimer->start(50);
03626     }
03627 }
03635 void KDGanttCanvasView::contentsMouseReleaseEvent ( TQMouseEvent * e )
03636 {
03637     mouseDown = false;
03638     static KDGanttViewItem* lastClicked = 0;
03639     mySignalSender->gvMouseButtonClicked( e->button(), currentItem ,  e->globalPos() );
03640     //tqDebug("datetime %s ",mySignalSender->getDateTimeForCoordX(e->globalPos().x(), true ).toString().latin1() );
03641     //tqDebug("mousepos %d %d ",e->pos().x(),e->pos().y() );
03642     //tqDebug("mouseup ");
03643     // if ( currentLink || currentItem )
03644     {
03645         switch ( e->button() ) {
03646         case Qt::LeftButton:
03647             myScrollTimer->stop();
03648             {
03649                 mySignalSender->itemLeftClicked( currentItem );
03650                 mySignalSender->gvItemLeftClicked( currentItem );
03651             }
03652             if ( currentLink )
03653                 mySignalSender->taskLinkLeftClicked( currentLink );
03654             if (linkItemsEnabled && fromItem) {
03655                 linkLine->hide();
03656                 canvas()->update();
03657                 TQCanvasItemList il = canvas() ->collisions ( e->pos() );
03658                 TQCanvasItemList::Iterator it;
03659                 for ( it = il.begin(); it != il.end(); ++it ) {
03660                     if (getType(*it) == Type_is_KDGanttViewItem) {
03661                         KDGanttViewItem *toItem = getItem(*it);
03662                         if (!toItem->isMyTextCanvas(*it)) {
03663                             int toArea = getItemArea(toItem, e->pos().x());
03664                             if (toArea > 0 && toItem && fromItem != toItem) {
03665                                 mySignalSender->linkItems(fromItem, toItem, getLinkType(fromArea, toArea));
03666                             }
03667                         }
03668                         break;
03669                     }
03670                 }
03671             }
03672             fromItem = 0;
03673             if ( movingGVItem ) {
03674               mySignalSender->gvItemMoved( movingGVItem );
03675               movingGVItem = 0;
03676             }
03677             break;
03678         case Qt::RightButton:
03679             {
03680                 mySignalSender->itemRightClicked( currentItem );
03681                 mySignalSender->gvItemRightClicked( currentItem );
03682 
03683             }
03684             if ( currentLink )
03685                 mySignalSender->taskLinkRightClicked( currentLink );
03686             break;
03687         case Qt::MidButton:
03688             {
03689                 mySignalSender->itemMidClicked( currentItem );
03690                 mySignalSender->gvItemMidClicked( currentItem );
03691             }
03692             if ( currentLink )
03693                 mySignalSender->taskLinkRightClicked( currentLink );
03694             break;
03695         default:
03696             break;
03697         }
03698     }
03699     if ( lastClicked != currentItem )
03700         mySignalSender->gvCurrentChanged( currentItem );
03701     lastClicked = currentItem;
03702     currentLink = 0;
03703     currentItem = 0;
03704 }
03712 void KDGanttCanvasView::contentsMouseDoubleClickEvent ( TQMouseEvent * e )
03713 {
03714     TQCanvasItemList il = canvas() ->collisions ( e->pos() );
03715 
03716     if ( il.isEmpty() && e->button() == Qt::LeftButton ) {
03717         //not directly sending a signal here (encapsulation and whatnot)
03718         mySignalSender->emptySpaceDoubleClicked(e);
03719         return;
03720     }
03721 
03722     TQCanvasItemList::Iterator it;
03723     for ( it = il.begin(); it != il.end(); ++it ) {
03724         switch ( e->button() ) {
03725         case Qt::LeftButton:
03726             switch (getType(*it)) {
03727             case Type_is_KDGanttViewItem:
03728                 if ( getItem(*it)->enabled() )
03729                     mySignalSender->itemDoubleClicked(getItem(*it));
03730                 mySignalSender->gvItemDoubleClicked(getItem(*it));
03731                 return;
03732                 break;
03733             case Type_is_KDGanttTaskLink:
03734                 mySignalSender->taskLinkDoubleClicked(getLink(*it));
03735                 return;
03736                 break;
03737             default:
03738                 break;
03739             }
03740             break;
03741             /*
03742               case Qt::RightButton:
03743               switch (getType(*it)) {
03744               case Type_is_KDGanttViewItem:
03745               mySignalSender->itemRightClicked(getItem(*it));
03746               return;
03747               break;
03748               case Type_is_KDGanttTaskLink:
03749               mySignalSender->taskLinkRightClicked(getLink(*it));
03750               return;
03751               break;
03752               }
03753               break;
03754               case Qt::MidButton:
03755               switch (getType(*it)) {
03756               case Type_is_KDGanttViewItem:
03757               mySignalSender->itemMidClicked(getItem(*it));
03758               return;
03759               break;
03760               case Type_is_KDGanttTaskLink:
03761               mySignalSender->taskLinkMidClicked(getLink(*it));
03762               return;
03763               break;
03764               }
03765               break;
03766             */
03767         default:
03768             break;
03769         }
03770     }
03771 }
03779 void KDGanttCanvasView::contentsMouseMoveEvent ( TQMouseEvent *e )
03780 {
03781     if ( !mouseDown ) {
03782       // Update cursor
03783       bool found = false;
03784       TQCanvasItemList il = canvas() ->collisions ( e->pos() );
03785       TQCanvasItemList::Iterator it;
03786       for ( it = il.begin(); it != il.end(); ++it ) {
03787         if ( (*it)->rtti() == KDIntervalColorRectangle::RTTI ) {
03788           found = true;
03789           KDIntervalColorRectangle* icr = static_cast<KDIntervalColorRectangle *>( *it );
03790           KDIntervalColorRectangle::HitTest hitTest = icr->hitTest( mySignalSender->myTimeHeader, e->pos() );
03791           switch ( hitTest ) {
03792           case KDIntervalColorRectangle::Start:
03793           case KDIntervalColorRectangle::End:
03794             setCursor( splitHCursor );
03795             break;
03796           default:
03797             unsetCursor();
03798           }
03799         }
03800         KDGanttViewItem *gvItem = getItem( *it );
03801         if ( dynamic_cast<KDGanttViewTaskItem*>( gvItem ) ) {
03802           found = true;
03803           MovingOperation op = gvItemHitTest( gvItem, mySignalSender->myTimeHeader, e->pos() );
03804           switch ( op ) {
03805             case ResizingLeft:
03806             case ResizingRight:
03807               if ( gvItem->isResizeable() )
03808                 setCursor( splitHCursor );
03809               break;
03810             default:
03811               unsetCursor();
03812           }
03813         }
03814       }
03815       if ( !found )
03816         unsetCursor();
03817       return;
03818     }
03819 
03820     const TQPoint p = e->pos();
03821     if ( movingItem ) {
03822       int x = tqRound( movingItem->x() );
03823       int width = movingItem->width();
03824       switch( movingOperation ) {
03825       case Moving:
03826         x += p.x() - movingStart.x();
03827         break;
03828       case ResizingLeft: {
03829         width = tqRound( movingItem->x() + movingItem->width() - p.x() );
03830         x = p.x();
03831         break;
03832       }
03833       case ResizingRight:
03834         width = p.x() - x;
03835         break;
03836       }
03837       movingStart = p;
03838       if ( movingItem->rtti() == KDIntervalColorRectangle::RTTI ) {
03839         KDIntervalColorRectangle* icr = static_cast<KDIntervalColorRectangle *>(movingItem);
03840         const TQDateTime newStart = mySignalSender->myTimeHeader->getDateTimeForIndex(x);
03841         const TQDateTime newEnd = mySignalSender->myTimeHeader->getDateTimeForIndex(x + width);
03842         icr->setDateTimes( newStart, newEnd );
03843         emit mySignalSender->intervalColorRectangleMoved( newStart, newEnd );
03844         mySignalSender->myTimeHeader->computeIntervals( movingItem->height() );
03845       }
03846       canvas()->update();
03847     }
03848 
03849     if ( movingGVItem ) {
03850       int dx = movingStart.x() - e->pos().x();
03851       int x = movingGVItem->middleLeft().x() - dx;
03852       TQDateTime dt = mySignalSender->getDateTimeForCoordX( x, false );
03853       int duration = movingGVItem->startTime().secsTo( movingGVItem->endTime() );
03854       if ( movingOperation == Moving ) {
03855         movingGVItem->setStartTime( dt );
03856         movingGVItem->setEndTime( dt.addSecs( duration ) );
03857       } else if ( movingOperation == ResizingLeft ) {
03858         movingGVItem->setStartTime( dt );
03859       } else if ( movingOperation == ResizingRight ) {
03860         movingGVItem->setEndTime( dt.addSecs( duration ) );
03861       }
03862       movingStart = e->pos();
03863     }
03864 
03865     static int moves = 0;
03866     if ( (currentLink || currentItem) && (moves < 3) ) {
03867         ++moves;
03868     } else {
03869         moves = 0;
03870         currentLink = 0;
03871         currentItem = 0;
03872     }
03873     if (autoScrollEnabled)
03874         mousePos = e->pos()- TQPoint(contentsX(),contentsY()); // make mousePos relative 0
03875     if (fromItem) {
03876         //tqDebug("mousemove: linking %s: %d,%d ",fromItem->listViewText().latin1(), e->pos().x(), e->pos().y());
03877         linkLine->setPoints(linkLine->startPoint().x(), linkLine->startPoint().y(), e->pos().x(), e->pos().y());
03878         canvas()->update();
03879     }
03880     // no action implemented
03881 }
03882 void KDGanttCanvasView::viewportPaintEvent ( TQPaintEvent * pe )
03883 {
03884     TQCanvasView::viewportPaintEvent ( pe );
03885 }
03886 void KDGanttCanvasView::set_Mouse_Tracking(bool on)
03887 {
03888     viewport()->setMouseTracking(on);
03889 }
03890 int  KDGanttCanvasView::getType(TQCanvasItem* it)
03891 {
03892     switch (it->rtti()) {
03893     case TQCanvasItem::Rtti_Line: return ((KDCanvasLine*)it)->myParentType;
03894     case TQCanvasItem::Rtti_Ellipse: return ((KDCanvasEllipse *)it)->myParentType;
03895     case TQCanvasItem::Rtti_Text: return ((KDCanvasText *)it)->myParentType;
03896     case TQCanvasItem::Rtti_Polygon: return ((KDCanvasPolygon *)it)->myParentType;
03897     case TQCanvasItem::Rtti_Rectangle:
03898     case KDIntervalColorRectangle::RTTI:
03899       return ((KDCanvasRectangle *)it)->myParentType;
03900     }
03901     return -1;
03902 }
03903 KDGanttViewItem*  KDGanttCanvasView::getItem(TQCanvasItem* it)
03904 {
03905     switch (it->rtti()) {
03906     case TQCanvasItem::Rtti_Line: return (KDGanttViewItem*)  ((KDCanvasLine*)it)->myParentItem;
03907     case TQCanvasItem::Rtti_Ellipse: return (KDGanttViewItem*)  ((KDCanvasEllipse *)it)->myParentItem;
03908     case TQCanvasItem::Rtti_Text: return (KDGanttViewItem*) ((KDCanvasText *)it)->myParentItem;
03909     case TQCanvasItem::Rtti_Polygon: return (KDGanttViewItem*) ((KDCanvasPolygon *)it)->myParentItem;
03910     case TQCanvasItem::Rtti_Rectangle: return (KDGanttViewItem*) ((KDCanvasRectangle *)it)->myParentItem;
03911 
03912     }
03913     return 0;
03914 }
03915 KDGanttViewTaskLink*  KDGanttCanvasView::getLink(TQCanvasItem* it)
03916 {
03917     switch (it->rtti()) {
03918     case TQCanvasItem::Rtti_Line: return (KDGanttViewTaskLink*)  ((KDCanvasLine*)it)->myParentItem;
03919     case TQCanvasItem::Rtti_Ellipse: return (KDGanttViewTaskLink*)  ((KDCanvasEllipse *)it)->myParentItem;
03920     case TQCanvasItem::Rtti_Text: return (KDGanttViewTaskLink*) ((KDCanvasText *)it)->myParentItem;
03921     case TQCanvasItem::Rtti_Polygon: return (KDGanttViewTaskLink*) ((KDCanvasPolygon *)it)->myParentItem;
03922     }
03923     return 0;
03924 }
03925 
03926 void KDGanttCanvasView::slotScrollTimer() {
03927     int mx = mousePos.x();
03928     int my = mousePos.y();
03929     int dx = 0;
03930     int dy = 0;
03931     if (mx < 0)
03932         dx = -5;
03933     else if (mx > visibleWidth())
03934         dx = 5;
03935     if (my < 0)
03936         dy = -5;
03937     else if (my > visibleHeight())
03938         dy = TQMIN(5, verticalScrollBar()->maxValue()-verticalScrollBar()->value());
03939 
03940     if (dx != 0 || dy != 0)
03941         scrollBy(dx, dy);
03942 }
03943 
03944 int KDGanttCanvasView::getItemArea(KDGanttViewItem *item, int x) {
03945     // area can be: no area = 0, Start = 1, Finish = 2
03946     // TODO: middle (move, dnd), front, back (resize)
03947     KDTimeTableWidget *tt = dynamic_cast<KDTimeTableWidget *>(canvas());
03948     if (!tt) {
03949         tqWarning("Cannot cast canvas to KDTimeTableWidget");
03950         return 0;
03951     }
03952     int area = 0;
03953     int start = tt->getCoordX(item->startTime());
03954     int end = start;
03955     if (item->type() == KDGanttViewItem::Event) {
03956         x > start ? area = 2 : area = 1;
03957     } else {
03958         end = tt->getCoordX(item->endTime());
03959         if ((end - start)/2 > (x - start))
03960             area = 1;
03961         else
03962             area = 2;
03963     }
03964     return area;
03965 }
03966 
03967 int KDGanttCanvasView::getLinkType(int from, int to) {
03968     // from, to should be Start = 1 or Finish = 2
03969     if ((from == 1) && (to == 1)) {
03970         return KDGanttViewTaskLink::StartStart;
03971     }
03972     if ((from == 1) && (to == 2)) {
03973         return KDGanttViewTaskLink::StartFinish;
03974     }
03975     if ((from == 2) && (to == 1)) {
03976         return KDGanttViewTaskLink::FinishStart;
03977     }
03978     if ((from == 2) && (to == 2)) {
03979         return KDGanttViewTaskLink::FinishFinish;
03980     }
03981     return KDGanttViewTaskLink::None;
03982 }
03983 
03989 KDIntervalColorRectangle::KDIntervalColorRectangle( KDGanttView* view )
03990   : KDCanvasRectangle( view->timeTableWidget(), 0, Type_is_KDGanttGridItem ),
03991   mStart(), mEnd()
03992 {
03993   setZ( -19 );
03994 }
03995 
04000 void KDIntervalColorRectangle::setDateTimes( const TQDateTime& start,
04001                                              const TQDateTime& end )
04002 {
04003   mStart = start;
04004   mEnd = end;
04005   if ( mEnd < mStart )
04006     tqSwap( mStart, mEnd );
04007 }
04008 
04013 void KDIntervalColorRectangle::setColor( const TQColor& color )
04014 {
04015   mColor = color;
04016 }
04017 
04021 void KDIntervalColorRectangle::layout( KDTimeHeaderWidget* timeHeader, int height )
04022 {
04023   int left = timeHeader->getCoordX(mStart);
04024   int right = timeHeader->getCoordX(mEnd);
04025   if ( right == left )
04026     ++right;
04027   setPen( TQPen(TQPen::NoPen) );
04028   setBrush( TQBrush(mColor, Qt::SolidPattern) );
04029   setSize( right - left, height );
04030   move( left, 0 );
04031   show();
04032 }
04033 
04037 KDIntervalColorRectangle::HitTest KDIntervalColorRectangle::hitTest( KDTimeHeaderWidget* timeHeader, const TQPoint& pos ) const
04038 {
04039   const int left = timeHeader->getCoordX(mStart);
04040   const int right = timeHeader->getCoordX(mEnd);
04041   const int width = right - left + 1;
04042   const int x = pos.x();
04043   if ( x < left + width / 10 )
04044     return Start;
04045   if ( x > right - width / 10 )
04046     return End;
04047   return Middle;
04048 }