kdgantt Library API Documentation

KDGanttViewSubwidgets.cpp

00001 /* -*- Mode: C++ -*-
00002    $Id: KDGanttViewSubwidgets.cpp 397992 2005-03-16 07:14:57Z danders $
00003    KDGantt - a multi-platform charting engine
00004 */
00005 
00006 /****************************************************************************
00007  ** Copyright (C)  2002-2004 Klar�vdalens 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 Qt, and distribute the resulting executable,
00031  ** without including the source code for Qt 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 <qlabel.h>
00045 #include <qheader.h>
00046 #include <qpainter.h>
00047 #include <qrect.h>
00048 #include <qtooltip.h>
00049 #include <qapplication.h>
00050 #include <qdrawutil.h>
00051 #include <qpalette.h>
00052 #include <qdragobject.h>
00053 #include <qptrlist.h>
00054 
00055 #include <kglobal.h>
00056 #include <klocale.h>
00057 #include <kcalendarsystem.h>
00058 #include <kdebug.h>
00059 
00060 KDTimeTableWidget:: KDTimeTableWidget( QWidget* parent,KDGanttView* myGantt):QCanvas (parent)
00061 {
00062     myGanttView = myGantt;
00063     taskLinksVisible = true;
00064     flag_blockUpdating = false;
00065     int_blockUpdating = 0;
00066     gridPen.setStyle(Qt::DotLine);
00067     gridPen.setColor(QColor(100,100,100));
00068     maximumComputedGridHeight = 0;
00069     denseLineCount = 0;
00070     denseLineBrush = QBrush( QColor ( 240,240,240 ));
00071     noInfoLineBrush = QBrush(  QColor ( 100,100,100 ), Qt::FDiagPattern );
00072     pendingHeight = 0;
00073     pendingWidth = 0;
00074     retune(256);
00075     resize(1,1);
00076 }
00077 
00078 QPtrList<KDGanttViewTaskLink> KDTimeTableWidget::taskLinks()
00079 {
00080     return myTaskLinkList;
00081 }
00082 
00083 void KDTimeTableWidget::clearTaskLinks()
00084 {
00085     // cannot use clear() here, as tasklinks will remove themselves from my list when deleted!
00086     QPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
00087     while (it.current()) {
00088         delete it.current();
00089     }
00090         
00091 }
00092 
00093 void KDTimeTableWidget::resetWidth( int wid )
00094 {
00095     if ( wid == width() ) {
00096         if (pendingHeight)
00097             pendingWidth = wid;
00098         else
00099             pendingWidth = 0;
00100         return;
00101     }
00102     if ( ! pendingHeight )
00103         pendingHeight = height();
00104     pendingWidth = wid;
00105     updateMyContent();
00106 }
00107 
00108 void KDTimeTableWidget::checkHeight( int hei )
00109 {
00110     if( hei < height() )
00111         return;
00112     if ( pendingHeight < hei+100)
00113         pendingHeight = hei+100;
00114     if ( !  pendingWidth )
00115         pendingWidth = width();
00116     maximumComputedGridHeight = 0; //force recomputing all
00117     updateMyContent();
00118 }
00119 
00120 
00121 void KDTimeTableWidget::setNoInformationBrush( const QBrush& brush )
00122 {
00123     noInfoLineBrush = brush;
00124     updateMyContent();
00125 }
00126 QBrush KDTimeTableWidget::noInformationBrush() const
00127 {
00128     return noInfoLineBrush;
00129 }
00130 
00131 void KDTimeTableWidget::removeItemFromTasklinks( KDGanttViewItem* item)
00132 {
00133     QPtrListIterator<KDGanttViewTaskLink> it((myTaskLinkList));
00134     for ( ; it.current(); ++it ) {
00135         it.current()->removeItemFromList( item );
00136     }
00137 }
00138 
00139 void KDTimeTableWidget::expandItem( QListViewItem * item)
00140 {
00141     item->invalidateHeight () ;
00142     //qApp->processEvents();
00143     updateMyContent();
00144 }
00145 void KDTimeTableWidget::collapseItem( QListViewItem * item)
00146 {
00147     item->invalidateHeight () ;
00148     //qApp->processEvents();
00149     updateMyContent();
00150 }
00151 
00152 void KDTimeTableWidget::highlightItem( QListViewItem * item )
00153 {
00154     static bool itemwashighlighted;
00155     static KDGanttViewItem* highlightedItem = 0;
00156     if (highlightedItem)
00157         highlightedItem->setHighlight(itemwashighlighted);
00158     highlightedItem = ( KDGanttViewItem*)item;
00159     itemwashighlighted = highlightedItem->highlight();
00160     highlightedItem->setHighlight(true);
00161     item->invalidateHeight () ;
00162     myGanttView->myListView->contentsY();
00163     updateMyContent();
00164 }
00165 int  KDTimeTableWidget::computeHeight()
00166 {
00167     // compute height of ListView
00168     // show only items shown in ListView
00169     int hei = 0;
00170     KDGanttViewItem* temp;
00171     temp = myGanttView->firstChild();
00172     while (temp) {
00173         hei += temp->computeHeight();
00174         temp = temp->nextSibling();
00175     }
00176     // set hei  to 1 to avoid canavs to be a null pixmap
00177     if (hei == 0) {
00178         hei = 1;
00179     }
00180     //qDebug("COMPUTED HEI %d ", hei);
00181     emit heightComputed( hei );
00182     return hei;
00183 }
00184 void KDTimeTableWidget::computeVerticalGrid()
00185 {
00186     // recompute the vertical grid
00187     // compute the vertical grid
00188     // if we have too much lines, hide them
00189     //qDebug("computeVerticalGrid() ");
00190     int cw =  myGanttView->myTimeHeader->myGridMinorWidth;
00191     int i = 0;
00192     int h ;
00193     if (pendingHeight > height() )
00194         h = pendingHeight;
00195     else
00196         h = height();
00197     int wid;
00198     if ( pendingWidth )
00199         wid = pendingWidth;
00200     else
00201         wid = width();
00202     KDCanvasLine* templine;
00203     KDCanvasRectangle* temprect;
00204     QColor colcol;
00205     QPen colPen;
00206     bool colorIterator = true;
00207 
00208 
00209     if (myGanttView->showMinorTicks()){//minor
00210         colPen.setWidth(cw);
00211         QPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00212         QPtrListIterator<KDCanvasLine> itgrid(verGridList);
00213         for ( ; itgrid.current(); ++itgrid ) {
00214             if (i < wid) {
00215                 itgrid.current()->setPoints(i,0,i,h);
00216                 itgrid.current()->show();
00217 
00218                 if (myGanttView->myTimeHeader->getColumnColor(colcol,i,i+cw))
00219                     {
00220 
00221                         colPen.setColor(colcol);
00222                         if (colorIterator)
00223                             colorIterator = itcol.current();
00224                         if (colorIterator)
00225                             {/*
00226                                itcol.current()->setPen(colPen);
00227                                itcol.current()->setPoints(i+(cw/2),0,i+(cw/2),h);
00228                              */
00229 
00230                                 itcol.current()->setPen( QPen::NoPen );
00231                                 itcol.current()->setBrush( QBrush( colcol, SolidPattern) );
00232                                 itcol.current()->setSize(cw ,h );
00233                                 itcol.current()->move( i, 0 );
00234                                 itcol.current()->show();
00235                                 ++itcol;
00236                             } else {
00237 
00238                                 /*
00239                                   templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00240                                   templine->setPen(colPen);
00241                                   templine->setPoints(i+(cw/2),0,i+(cw/2),h);
00242                                 */
00243                                 temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00244                                 temprect->setPen( QPen::NoPen );
00245                                 temprect->setBrush( QBrush( colcol, SolidPattern) );
00246                                 temprect->setSize(cw ,h );
00247                                 temprect->move( i, 0 );
00248                                 temprect->setZ(-20);
00249                                 temprect->show();
00250                                 columnColorList.append(temprect);
00251                             }
00252                     }
00253                 i += cw;
00254             } else {
00255                 itgrid.current()->hide();
00256             }
00257         }
00258         // create additional Lines for vertical grid
00259         for ( ;i < wid;i += cw) {
00260             templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00261             templine->setPen(gridPen);
00262             templine->setPoints(i,0,i,h);
00263             templine->setZ(0);
00264             templine->show();
00265             verGridList.append(templine);
00266             if (myGanttView->myTimeHeader->getColumnColor(colcol,i,i+cw))
00267                 {
00268                     colPen.setColor(colcol);
00269                     if (colorIterator)
00270                         colorIterator = itcol.current();
00271                     if (colorIterator)
00272                         {/*
00273                            itcol.current()->setPen(colPen);
00274                            itcol.current()->setPoints(i+(cw/2),0,i+(cw/2),h);
00275                          */
00276                             itcol.current()->setPen( QPen::NoPen );
00277                             itcol.current()->setBrush( QBrush( colcol, SolidPattern) );
00278                             itcol.current()->setSize(cw ,h );
00279                             itcol.current()->move( i, 0 );
00280                             itcol.current()->show();
00281                             ++itcol;
00282                         } else {
00283                             temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00284                             temprect->setPen( QPen::NoPen );
00285                             temprect->setBrush( QBrush( colcol, SolidPattern) );
00286                             temprect->setSize(cw ,h );
00287                             temprect->move( i, 0 );
00288                             temprect->setZ(-20);
00289                             temprect->show();
00290                             columnColorList.append(temprect);
00291                             /*
00292                               templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00293                               templine->setPen(colPen);
00294                               templine->setPoints(i+(cw/2),0,i+(cw/2),h);
00295                               templine->setZ(-20);
00296                               templine->show();
00297                               columnColorList.append(templine);
00298                             */
00299                         }
00300                 }
00301         }
00302         if (colorIterator)
00303             for ( ; itcol.current(); ++itcol )
00304                 itcol.current()->hide();
00305     } else {//major
00306         if (myGanttView->showMajorTicks()) {
00307             QValueList<int>::iterator intIt = myGanttView->myTimeHeader->majorTicks.begin();
00308             QValueList<int>::iterator intItEnd = myGanttView->myTimeHeader->majorTicks.end();
00309             QPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00310             QPtrListIterator<KDCanvasLine> itgrid(verGridList);
00311             int left = 0;
00312             for ( ; itgrid.current(); ++itgrid ) {
00313                 if (intIt != intItEnd) {
00314                     left = (*intIt);
00315                     ++intIt;
00316                     itgrid.current()->setPoints(left,0,left,h);
00317                     itgrid.current()->show();
00318                     //int right = (*intIt);
00319                     if ((*intIt))
00320                         if (myGanttView->myTimeHeader->getColumnColor(colcol,left,(*intIt) ))
00321                             {
00322                                 int mid = (-left+(*intIt));
00323                                 colPen.setColor(colcol);
00324                                 colPen.setWidth((*intIt)-left);
00325                                 if (colorIterator)
00326                                     colorIterator = itcol.current();
00327                                 if (colorIterator)
00328                                     {/*
00329                                        itcol.current()->setPen(colPen);
00330                                        itcol.current()->setPoints(i+mid,0,mid,h);
00331                                      */
00332                                         itcol.current()->setPen( QPen::NoPen );
00333                                         itcol.current()->setBrush( QBrush( colcol, SolidPattern) );
00334                                         itcol.current()->setSize(mid ,h );
00335                                         itcol.current()->move( left, 0 );
00336                                         itcol.current()->show();
00337                                         ++itcol;
00338                                     } else {
00339                                         temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00340                                         temprect->setPen( QPen::NoPen );
00341                                         temprect->setBrush( QBrush( colcol, SolidPattern) );
00342                                         temprect->setSize(mid,h );
00343                                         temprect->move( left, 0 );
00344                                         temprect->setZ(-20);
00345                                         temprect->show();
00346                                         columnColorList.append(temprect);
00347                                         /*
00348                                           templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00349                                           templine->setPen(colPen);
00350                                           templine->setPoints(mid,0,i+mid,h);
00351                                           templine->setZ(-20);
00352                                           templine->show();
00353                                           columnColorList.append(templine);
00354                                         */
00355 
00356                                     }
00357                             }
00358 
00359                 } else {
00360                     itgrid.current()->hide();
00361                 }
00362             }
00363             KDCanvasLine* templine;
00364             // create additional Lines for vertical grid
00365             for ( ;intIt != intItEnd  ;++intIt) {
00366 
00367                 templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00368                 templine->setPen(gridPen);
00369                 templine->setPoints((*intIt),0,(*intIt),h);
00370                 templine->setZ(0);
00371                 templine->show();
00372                 verGridList.append(templine);
00373                 if ((*intIt))
00374                     if (myGanttView->myTimeHeader->getColumnColor(colcol,left,(*intIt)))
00375                         {
00376                             int mid = (-left+(*intIt));
00377                             colPen.setColor(colcol);
00378                             colPen.setWidth((*intIt)-left);
00379                             if (colorIterator)
00380                                 colorIterator = itcol.current();
00381                             if (colorIterator)
00382                                 {/*
00383                                    itcol.current()->setPen(colPen);
00384                                    itcol.current()->setPoints(i+mid,0,mid,h);
00385                                  */
00386                                     itcol.current()->setPen( QPen::NoPen );
00387                                     itcol.current()->setBrush( QBrush( colcol, SolidPattern) );
00388                                     itcol.current()->setSize(mid ,h );
00389                                     itcol.current()->move( left, 0 );
00390                                     itcol.current()->show();
00391                                     ++itcol;
00392                                 } else {
00393                                     temprect = new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00394                                     temprect->setPen( QPen::NoPen );
00395                                     temprect->setBrush( QBrush( colcol, SolidPattern) );
00396                                     temprect->setSize(mid ,h );
00397                                     temprect->move( left, 0 );
00398                                     temprect->setZ(-20);
00399                                     temprect->show();
00400                                     columnColorList.append(temprect);
00401                                     /*
00402                                       templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00403                                       templine->setPen(colPen);
00404                                       templine->setPoints(mid,0,i+mid,h);
00405                                       templine->setZ(-20);
00406                                       templine->show();
00407                                       columnColorList.append(templine);
00408                                     */
00409                                 }
00410                         }
00411                 left = (*intIt);
00412             }
00413             if (colorIterator)
00414                 for ( ; itcol.current(); ++itcol ) {
00415                     itcol.current()->hide();
00416                 }
00417 
00418         }
00419         else {
00420             //hideall
00421             QPtrListIterator<KDCanvasLine> itgrid(verGridList);
00422             for ( ; itgrid.current(); ++itgrid ) {
00423                 itgrid.current()->hide();
00424             }
00425             QPtrListIterator<KDCanvasRectangle> itcol(columnColorList);
00426             for ( ; itcol.current(); ++itcol ) {
00427                 itcol.current()->hide();
00428             }
00429         }
00430     }
00431 }
00432 void KDTimeTableWidget::computeHorizontalGrid()
00433 {
00434     // compute  horizontal grid
00435     //qDebug("computeHorizontalGrid() ");
00436     KDGanttViewItem* temp = myGanttView->firstChild();
00437     int wid;
00438     if ( pendingWidth )
00439         wid = pendingWidth;
00440     else
00441         wid = width();
00442     KDCanvasLine* templine;
00443     QPtrListIterator<KDCanvasLine> ithor(horGridList);
00444     if ( ithor.current() ) {
00445         templine = ithor.current();
00446         ++ithor;
00447     } else {
00448         templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00449         templine->setPen(gridPen);
00450         templine->setZ(0);
00451         horGridList.append(templine);
00452     }
00453     templine->setPoints(0,0,wid,0);
00454     templine->show();
00455     int posY;
00456     while ( temp ) {
00457         posY = temp->itemPos() + temp->height();
00458         if ( ithor.current() ) {
00459             templine = ithor.current();
00460             ++ithor;
00461         } else {
00462             //new vertical grid line
00463             templine = new KDCanvasLine(this,0,Type_is_KDGanttGridItem);
00464             templine->setPen(gridPen);
00465             templine->setZ(0);
00466             horGridList.append(templine);
00467         }
00468         if ( templine->endPoint() != QPoint(wid,posY ))
00469             templine->setPoints(0,posY,wid,posY );
00470         if ( !templine->isVisible() )
00471             templine->show();
00472         //QString ts = "asGroup";
00473         //if (!temp->displaySubitemsAsGroup() )
00474         //  ts = " NOT asGroup";
00475         //qDebug("temp name %s %s", temp->listViewText(0).latin1(), ts.latin1());
00476 
00477         temp = temp->itemBelow ();
00478     }
00479     while ( ithor.current() ) {
00480         if ( ithor.current()->isVisible() )
00481             ithor.current()->hide();
00482         ++ithor;
00483     }
00484 }
00485 
00486 void KDTimeTableWidget::computeDenseLines()
00487 {
00488     KDGanttViewItem* temp = myGanttView->firstChild();
00489     int wid;
00490     if ( pendingWidth )
00491         wid = pendingWidth;
00492     else
00493         wid = width();
00494     QPtrListIterator<KDCanvasRectangle> ithordense(horDenseList);
00495     KDCanvasRectangle* denseLine;
00496     int tempDenseLineCount = 0;
00497     while ( temp ) {
00498         if ( temp->isVisible() ) {
00499             ++tempDenseLineCount;
00500             if ( tempDenseLineCount == denseLineCount ) {
00501                 tempDenseLineCount = 0;
00502                 if ( ithordense.current() ) {
00503                     denseLine = ithordense.current();
00504                     ++ithordense;
00505                 } else {
00506                     denseLine =new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00507                     denseLine->setZ(-2);
00508                     horDenseList.append( denseLine );
00509                 }
00510                 if ( denseLine->rect() != QRect(0, temp->itemPos(),wid, temp->height()) ) {
00511                     denseLine->move( 0, temp->itemPos() );
00512                     denseLine->setSize( wid, temp->height());
00513                 }
00514                 if (denseLine->brush() != denseLineBrush ) {
00515                     denseLine->setPen( QPen(  Qt::NoPen ) );
00516                     denseLine->setBrush( denseLineBrush);
00517                 }
00518                 if (!denseLine->isVisible() )
00519                     denseLine->show();
00520 
00521             } else {
00522                 ;
00523             }
00524         }
00525         temp = temp->itemBelow ();
00526     }
00527     while ( ithordense.current() ) {
00528         if ( ithordense.current()->isVisible() ) {
00529             ithordense.current()->hide();
00530         }
00531         ++ithordense;
00532     }
00533 }
00534 void KDTimeTableWidget::computeShowNoInformation()
00535 {
00536     KDGanttViewItem* temp = myGanttView->firstChild();
00537     int wid;
00538     if ( pendingWidth )
00539         wid = pendingWidth;
00540     else
00541         wid = width();
00542     QPtrListIterator<KDCanvasRectangle> itnoinfo(showNoInfoList);
00543     KDCanvasRectangle* noInfoLine;
00544     while ( temp ) {
00545         if ( temp->showNoInformation() ) {
00546             if ( itnoinfo.current() ) {
00547                 noInfoLine = itnoinfo.current();
00548                 ++itnoinfo;
00549             } else {
00550                 noInfoLine =new KDCanvasRectangle(this,0,Type_is_KDGanttGridItem);
00551                 showNoInfoList.append( noInfoLine );
00552                 noInfoLine->setZ(-1);
00553             }
00554             noInfoLine->move( 0, temp->itemPos() );
00555             noInfoLine->setSize( wid, temp->height());
00556             noInfoLine->setPen( QPen(  Qt::NoPen ) );
00557             noInfoLine->setBrush( noInfoLineBrush);
00558             noInfoLine->show();
00559         }
00560         temp = temp->itemBelow ();
00561     }
00562     while ( itnoinfo.current() ) {
00563         itnoinfo.current()->hide();
00564         ++itnoinfo;
00565     }
00566 
00567 }
00568 
00569 void KDTimeTableWidget::computeTaskLinks()
00570 {
00571     //compute and show tasklinks
00572     QPtrListIterator<KDGanttViewTaskLink> it((myTaskLinkList));
00573     for ( ; it.current(); ++it ) {
00574         if (it.current()->isVisible())
00575             it.current()->showMe(true);
00576         else
00577             it.current()->showMe(false);
00578     }
00579 }
00580 
00581 // updateMyContent() can be blocked by blockUpdating( true ) or inc_blockUpdating()
00582 // updateMyContent() is blocked, if the GanttView is hidden after startup
00583 
00584 void KDTimeTableWidget::updateMyContent()
00585 {
00586     if ( flag_blockUpdating || int_blockUpdating ) {
00587         // qDebug("KDTimeTableWidget::updateMyContent() blocked! ");
00588         return;
00589     }
00590     //qDebug("KDTimeTableWidget::updateMyContent() ********************************* ");
00591     /*
00592     // debug output
00593     KDGanttViewItem* temp =  myGanttView->firstChild();
00594     while (temp != 0) {
00595     temp->printinfo("  " );
00596     temp = temp->nextSibling();
00597     }
00598     */
00599     int hei = computeHeight();
00600     minimumHeight = hei;
00601     int viewport_hei = myGanttView->myCanvasView->viewport()->height();
00602     if ( viewport_hei > hei )
00603         hei = viewport_hei + 100;
00604     if ( myGanttView->myTimeHeader->registerStartTime() )
00605         return; // try again via timeheader computeTicks();
00606     if ( myGanttView->myTimeHeader->registerEndTime() )
00607         return; // try again via timeheader computeTicks();
00608     if ( hei > height() ) {
00609         if ( !  pendingWidth )
00610             pendingWidth = width();
00611         if ( pendingHeight < hei )
00612             pendingHeight = hei;
00613     }
00614     if (pendingHeight > hei )
00615         hei =  pendingHeight;
00616     if (hei > maximumComputedGridHeight)
00617         {
00618             maximumComputedGridHeight = hei;
00619             // compute the background interval lines
00620             myGanttView->myTimeHeader->computeIntervals( hei );
00621             //compute VerticalGrid and column color
00622             computeVerticalGrid();
00623         }
00624     computeTaskLinks();
00625     computeHorizontalGrid();
00626     computeDenseLines();
00627     computeShowNoInformation();
00628     //setAllChanged();
00629     update();
00630     if (pendingWidth && pendingHeight ) {
00631         resize( pendingWidth, pendingHeight );
00632         pendingWidth = 0;
00633         pendingHeight = 0;
00634         emit heightComputed( 0 );
00635 
00636     }
00637     pendingWidth = 0;
00638     pendingHeight = 0;
00639     //qDebug("KDTimeTableWidget::updateMyContent() -------------------------");
00640 }
00641 // used for blocking recursive methods
00642 // e.g. KDGanttViewItem::setHighlight() and  displaySubitemsAsGroup() == true
00643 
00644 void KDTimeTableWidget::inc_blockUpdating( )
00645 {
00646     ++ int_blockUpdating;
00647 }
00648 // used for blocking recursive methods
00649 void KDTimeTableWidget::dec_blockUpdating( )
00650 {
00651     -- int_blockUpdating;
00652 }
00653 // if false(i.e. unblock), sets int_blockUpdating to 0
00654 void KDTimeTableWidget::setBlockUpdating( bool block )
00655 {
00656     if ( !block )
00657         int_blockUpdating = 0;
00658     flag_blockUpdating = block;
00659 }
00660 bool KDTimeTableWidget::blockUpdating()
00661 {
00662     return flag_blockUpdating;
00663 }
00664 
00665 void KDTimeTableWidget::setShowTaskLinks( bool show )
00666 {
00667     taskLinksVisible = show;
00668     updateMyContent();
00669 }
00670 bool KDTimeTableWidget::showTaskLinks()
00671 {
00672     return taskLinksVisible;
00673 }
00674 void KDTimeTableWidget::setHorBackgroundLines( int count,  QBrush brush )
00675 {
00676     denseLineBrush = brush;
00677     denseLineCount = 0;
00678     if ( count > 1 )
00679         denseLineCount = count;
00680 }
00681 
00682 
00683 int KDTimeTableWidget::horBackgroundLines(  QBrush& brush )
00684 {
00685     brush =  denseLineBrush;
00686     return denseLineCount;
00687 }
00688 
00689 int KDTimeTableWidget::getCoordX( QDateTime dt ) {
00690     return myGanttView->myTimeHeader->getCoordX(dt);
00691 }
00692 
00693 /* ***************************************************************
00694    KDTimeHeaderWidget:: KDTimeHeaderWidget
00695    ***************************************************************** */
00696 KDTimeHeaderWidget:: KDTimeHeaderWidget( QWidget* parent,KDGanttView* gant )
00697     : QWidget (parent)
00698 {
00699     myToolTip = new KDTimeHeaderToolTip(this,this);
00700     mySizeHint = 0;
00701     myGanttView = gant;
00702     flagDoNotRecomputeAfterChange = true;
00703     QDateTime start = (QDateTime::currentDateTime ()).addSecs(-3600);
00704     setHorizonStart(start);
00705     setHorizonEnd( start.addSecs(3600*2));
00706     flagStartTimeSet = false;
00707     flagEndTimeSet = false;
00708     myCenterDateTime = QDateTime::currentDateTime ();
00709     setScale(KDGanttView::Auto);
00710     //setScale(KDGanttView::Hour);
00711     myMaxScale = KDGanttView::Month;
00712     myMinScale = KDGanttView::Minute;
00713     myAutoScaleMinorTickcount = 100;
00714     setMajorScaleCount( 1 );
00715     setMinorScaleCount( 1);
00716     setMinimumColumnWidth( 5 );
00717     setYearFormat(KDGanttView::FourDigit );
00718     setHourFormat( KDGanttView::Hour_12 );
00719     myZoomFactor = 1.0;
00720     setWeekendBackgroundColor(QColor(220,220,220) );
00721     setWeekendDays( 6, 7 );
00722     myGridMinorWidth = 0;
00723     myPopupMenu = new QPopupMenu(this);
00724     QPopupMenu * zoomPopupMenu = new QPopupMenu(this);
00725     myPopupMenu->insertItem (i18n("Zoom"),zoomPopupMenu, 1);
00726     zoomPopupMenu->insertItem( i18n("Zoom to 100%"),this, SLOT(setSettings(int)),0 ,21,21 );
00727     zoomPopupMenu->insertItem( i18n("Zoom to Fit"),this, SLOT(setSettings(int)),0 ,20,20 );
00728     zoomPopupMenu->insertItem( i18n("Zoom In (x 2)"),this, SLOT(setSettings(int)),0 ,22,22 );
00729     zoomPopupMenu->insertItem( i18n("Zoom In (x 6)"),this, SLOT(setSettings(int)),0 ,24,24 );
00730     zoomPopupMenu->insertItem( i18n("Zoom In (x 12)"),this, SLOT(setSettings(int)),0 ,26,26 );
00731     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/2)"),this, SLOT(setSettings(int)),0 ,23,23 );
00732     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/6)"),this, SLOT(setSettings(int)),0 ,25,25 );
00733     zoomPopupMenu->insertItem( i18n("Zoom Out (x 1/12)"),this, SLOT(setSettings(int)),0 ,27,27 );
00734     scalePopupMenu = new QPopupMenu(this);
00735     myPopupMenu->insertItem (i18n("Scale"),scalePopupMenu, 2);
00736     scalePopupMenu->insertItem( i18n("Minute"),this, SLOT(setSettings(int)),0 ,1,1 );
00737     scalePopupMenu->insertItem( i18n("Hour"),this, SLOT(setSettings(int)),0 ,2,2 );
00738     scalePopupMenu->insertItem( i18n("Day"),this, SLOT(setSettings(int)),0 ,3,3 );
00739     scalePopupMenu->insertItem( i18n("Week"),this, SLOT(setSettings(int)),0 ,4,4 );
00740     scalePopupMenu->insertItem( i18n("Month"),this, SLOT(setSettings(int)),0 ,5,5 );
00741     scalePopupMenu->insertItem( i18n("Auto"),this, SLOT(setSettings(int)),0 ,6,6 );
00742     scalePopupMenu->setCheckable ( true );
00743     timePopupMenu = new QPopupMenu(this);
00744     myPopupMenu->insertItem (i18n("Time Format"),timePopupMenu, 3);
00745     timePopupMenu->insertItem( i18n("24 Hour"),this, SLOT(setSettings(int)),0 ,40,40 );
00746     timePopupMenu->insertItem( i18n("12 PM Hour"),this, SLOT(setSettings(int)),0 ,41,41 );
00747     timePopupMenu->insertItem( i18n("24:00 Hour"),this, SLOT(setSettings(int)),0 ,42,42 );
00748     yearPopupMenu = new QPopupMenu(this);
00749     myPopupMenu->insertItem (i18n("Year Format"),yearPopupMenu, 4);
00750     yearPopupMenu->insertItem( i18n("Four Digit"),this, SLOT(setSettings(int)),0 ,50,50 );
00751     yearPopupMenu->insertItem( i18n("Two Digit"),this, SLOT(setSettings(int)),0 ,51,51 );
00752     yearPopupMenu->insertItem( i18n("Two Digit Apostrophe"),this, SLOT(setSettings(int)),0 ,52,52 );
00753     yearPopupMenu->insertItem( i18n("No Date on Minute/Hour Scale"),this, SLOT(setSettings(int)),0 ,53,53 );
00754 
00755     gridPopupMenu = new QPopupMenu(this);
00756     myPopupMenu->insertItem (i18n("Grid"),gridPopupMenu,5);
00757     gridPopupMenu->insertItem( i18n("Show Minor Grid"),this, SLOT(setSettings(int)),0 ,10,10 );
00758     gridPopupMenu->insertItem( i18n("Show Major Grid"),this, SLOT(setSettings(int)),0 ,11,11 );
00759     gridPopupMenu->insertItem( i18n("Show No Grid"),this, SLOT(setSettings(int)),0 ,12,12 );
00760     myPopupMenu->insertItem( i18n("Print"),this, SLOT(setSettings(int)),0 ,30,30 );
00761     connect(myPopupMenu, SIGNAL (  aboutToShow () ) , this, SLOT( preparePopupMenu() )) ;
00762     flagZoomToFit = false;
00763     setShowMinorTicks( true );
00764     myRealEnd =  myHorizonEnd;
00765     myRealStart = myHorizonStart;
00766     autoComputeTimeLine = true;
00767     flagDoNotRecomputeAfterChange = false;
00768     flagDoNotRepaintAfterChange = false;
00769     setShowPopupMenu(false,false,false,false,false,false,false);
00770     for (int j =1;j<8;++j)
00771         weekdayColor[j] = Qt::white;
00772     myMinimumWidth = 0;
00773     mouseDown = false;
00774     beginMouseDown = 0;
00775     endMouseDown = 0;
00776 }
00777 
00778 KDTimeHeaderWidget::~KDTimeHeaderWidget()
00779 {
00780     delete myToolTip;
00781 }
00782 void  KDTimeHeaderWidget::preparePopupMenu()
00783 {
00784     myPopupMenu->setItemVisible ( 1, flagShowZoom  );
00785     myPopupMenu->setItemVisible ( 2, flagShowScale );
00786     myPopupMenu->setItemVisible ( 3, flagShowTime );
00787     myPopupMenu->setItemVisible ( 4, flagShowYear );
00788     myPopupMenu->setItemVisible ( 5, flagShowGrid);
00789     myPopupMenu->setItemVisible ( 30, flagShowPrint );
00790     if (flagZoomToFit)
00791         myPopupMenu->changeItem( 1, i18n("Zoom (Fit)"));
00792     else
00793         myPopupMenu->changeItem( 1, i18n("Zoom (%1)").arg( QString::number( zoomFactor(), 'f',3) ) );
00794     int i = 0;
00795     int id;
00796     while ( ( id = scalePopupMenu->idAt( i++ )) >= 0 ) {
00797         scalePopupMenu->setItemChecked ( id, false );
00798     }
00799     scalePopupMenu->setItemChecked ( scalePopupMenu->idAt ( (int)( scale()) ), true );
00800     i = 0;
00801     while ( ( id = timePopupMenu->idAt( i++ )) >= 0 ) {
00802         timePopupMenu->setItemChecked ( id, false );
00803     }
00804     timePopupMenu->setItemChecked ( timePopupMenu->idAt ( (int)( hourFormat()) ), true );
00805     i = 0;
00806     while ( ( id = yearPopupMenu->idAt( i++ )) >= 0 ) {
00807         yearPopupMenu->setItemChecked ( id, false );
00808     }
00809     yearPopupMenu->setItemChecked ( yearPopupMenu->idAt ( (int)( yearFormat()) ), true );
00810     i = 0;
00811     while ( ( id = gridPopupMenu->idAt( i++ )) >= 0 ) {
00812         gridPopupMenu->setItemChecked ( id, false );
00813     }
00814 
00815     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 0 ), showMinorTicks() );
00816     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 1 ), showMajorTicks() );
00817     gridPopupMenu->setItemChecked ( gridPopupMenu->idAt ( 2 ),
00818                                     !(showMajorTicks() || showMinorTicks()) );
00819 
00820 
00821 }
00822 
00823 QString  KDTimeHeaderWidget::getToolTipText(QPoint p)
00824 {
00825     return KGlobal::locale()->formatDateTime(getDateTimeForIndex(p.x()));
00826 }
00827 void KDTimeHeaderWidget::addTickRight( int num )
00828 {
00829     int secs = ((num*getTickTime())-30);
00830     setHorizonEnd(getDateTimeForIndex(width()).addSecs(secs));
00831     //qApp->processEvents();
00832 }
00833 
00834 void KDTimeHeaderWidget::addTickLeft( int num )
00835 {
00836     int secs = ((num*getTickTime())-30);
00837     setHorizonStart(getDateTimeForIndex(0).addSecs(-secs));
00838     //qApp->processEvents();
00839 }
00840 // the time in secs of one minor grid tick
00841 int KDTimeHeaderWidget::getTickTime()
00842 {
00843     return getDateTimeForIndex(0).secsTo(getDateTimeForIndex(myGridMinorWidth));
00844 }
00845 
00846 
00847 void KDTimeHeaderWidget::checkWidth( int wid )
00848 {
00849     // we have to set the minimum width one pixel higher than the
00850     // viewport width of the canvas view in  order to
00851     // avoid that the horiz. scrollbar of the canvasview is hidden
00852     myMinimumWidth = wid + 1;
00853     if ( myMinimumWidth  > width() ||
00854          ( myMinimumWidth > mySizeHint &&
00855            myMinimumWidth < (width() - myGridMinorWidth  )) )
00856         computeTicks();
00857 }
00858 
00859 bool KDTimeHeaderWidget::registerStartTime()
00860 {
00861 
00862     QListViewItemIterator it( myGanttView->myListView );
00863     if (!flagStartTimeSet) {
00864         QDateTime temp , time;
00865         KDGanttViewItem* item;
00866         bool setNewTime = false;
00867         item = (KDGanttViewItem*)myGanttView->myListView->firstChild();
00868         if ( item ) {
00869             temp = item->startTime();
00870             time = temp;
00871             //  while ( item != 0)
00872             for ( ; it.current(); ++it ) {
00873                 item = ( KDGanttViewItem* )it.current();
00874                 if (item->isVisibleInGanttView) {
00875                     if ( !setNewTime )
00876                         temp = item->startTime();
00877                     switch( item->type() ) {
00878                     case KDGanttViewItem::Event:
00879                         time = ((KDGanttViewEventItem*)item)->leadTime();
00880                         setNewTime = true;
00881                         break;
00882                     case KDGanttViewItem::Summary:
00883                     case KDGanttViewItem::Task:
00884                         time = item->startTime();
00885                         setNewTime = true;
00886                         break;
00887                     default:
00888                         time = temp;
00889                     }
00890                     if ( time < temp) {
00891                         temp = time ;
00892                     }
00893                 }
00894             }
00895             if ( setNewTime )
00896                 if ( myHorizonStart != temp) {
00897                     myHorizonStart = temp;
00898                     computeTicks();
00899                     return true;
00900                 }
00901         }
00902     }
00903     return false;
00904 }
00905 
00906 
00907 bool KDTimeHeaderWidget::registerEndTime()
00908 {
00909     if (!flagEndTimeSet) {
00910         QDateTime temp , time;
00911         KDGanttViewItem* item;
00912         bool setNewTime = false;
00913         item = (KDGanttViewItem*)myGanttView->myListView->firstChild();
00914         if ( item ) {
00915             temp = item->startTime();
00916             time = temp;
00917             QListViewItemIterator it( myGanttView->myListView );
00918             for ( ; it.current(); ++it ) {
00919                 item = ( KDGanttViewItem* )it.current();
00920                 if (item->isVisibleInGanttView) {
00921                     if ( !setNewTime )
00922                         temp = item->startTime();
00923                     switch( item->type() ) {
00924                     case KDGanttViewItem::Event:
00925                         time = ((KDGanttViewEventItem*)item)->startTime();
00926                         setNewTime = true;
00927                         break;
00928                     case KDGanttViewItem::Summary:
00929                         time = item->endTime();
00930                         if ( time < ((KDGanttViewSummaryItem*)item)->actualEndTime())
00931                             time = ((KDGanttViewSummaryItem*)item)->actualEndTime();
00932                         setNewTime = true;
00933                         break;
00934                     case KDGanttViewItem::Task:
00935                         time = item->endTime();
00936                         setNewTime = true;
00937                         break;
00938                     default:
00939                         time = temp;
00940                     }
00941                     if ( time > temp)
00942                         temp = time ;
00943                 }
00944             }
00945 
00946             if ( setNewTime )
00947                 if (myHorizonEnd != temp ) {
00948                     myHorizonEnd = temp;
00949                     computeTicks();
00950                     return true;
00951                 }
00952         }
00953     }
00954     return false;
00955 }
00956 
00957 
00958 void KDTimeHeaderWidget::setShowPopupMenu( bool show,
00959                                            bool showZoom,
00960                                            bool showScale,
00961                                            bool showTime,
00962                                            bool showYear,
00963                                            bool showGrid,
00964                                            bool showPrint)
00965 {
00966     flagShowPopupMenu = show;
00967     flagShowZoom = showZoom;
00968     flagShowScale  = showScale;
00969     flagShowTime  = showTime;
00970     flagShowYear = showYear;
00971     flagShowGrid  = showGrid;
00972     flagShowPrint = showPrint;
00973 }
00974 
00975 
00976 bool KDTimeHeaderWidget::showPopupMenu() const
00977 {
00978     return flagShowPopupMenu;
00979 }
00980 
00981 void KDTimeHeaderWidget::setSettings(int i)
00982 {
00983 
00984     switch (i) {
00985     case 1:
00986         setScale(KDGanttView::Minute );
00987         break;
00988     case 2:
00989         setScale(KDGanttView::Hour );
00990         break;
00991     case 3:
00992         setScale(KDGanttView::Day );
00993         break;
00994     case 4:
00995         setScale(KDGanttView::Week );
00996         break;
00997     case 5:
00998         setScale(KDGanttView::Month );
00999         break;
01000     case 6:
01001         setScale(KDGanttView::Auto );
01002         break;
01003     case 10:
01004         setShowMinorTicks( true );
01005         break;
01006     case 11:
01007         setShowMajorTicks( true );{
01008 
01009         }
01010         break;
01011     case 12:
01012         setShowMajorTicks( false );
01013         setShowMinorTicks( false);
01014         break;
01015     case 20:
01016         zoomToFit();
01017         break;
01018     case 21:
01019         zoom(1.0);
01020         break;
01021     case 22:
01022         zoom(2.0,false);
01023         break;
01024     case 23:
01025         zoom(0.5,false);
01026         break;
01027     case 24:
01028         zoom(6.0,false);
01029         break;
01030     case 25:
01031         zoom(0.16666,false);
01032         break;
01033     case 26:
01034         zoom(12.0,false);
01035         break;
01036     case 27:
01037         zoom(0.08333,false);
01038         break;
01039     case 30:
01040         myGanttView->print();
01041         break;
01042     case 40:
01043     case 41:
01044     case 42:
01045         setHourFormat( (KDGanttView::HourFormat) (i - 40) );
01046         break;
01047     case 50:
01048     case 51:
01049     case 52:
01050     case 53:
01051         setYearFormat( (KDGanttView::YearFormat) ( i - 50) );
01052         break;
01053 
01054     case 60:
01055 
01056         break;
01057 
01058     case 61:
01059 
01060         break;
01061 
01062     case 62:
01063 
01064         break;
01065 
01066     case 63:
01067 
01068         break;
01069 
01070     case 64:
01071 
01072         break;
01073     }
01074     // myGanttView->myTimeTable->updateMyContent();
01075 }
01076 void KDTimeHeaderWidget::zoomToFit()
01077 {
01078     flagZoomToFit = true;
01079     computeTicks();
01080 }
01081 double KDTimeHeaderWidget::zoomFactor()
01082 {
01083     return myZoomFactor;
01084 }
01085 double KDTimeHeaderWidget::secsFromTo( QDateTime begin, QDateTime end )
01086 {
01087     QDateTime temp;
01088     double secs, days;
01089     days = begin.daysTo(end);
01090     temp = begin.addDays((int) days);
01091     secs = temp.secsTo(end);
01092     secs += days * 86400.0;
01093     return secs;
01094 }
01095 
01096 
01097 void KDTimeHeaderWidget::zoomToSelection( QDateTime start, QDateTime end)
01098 {
01099     if (start < myHorizonStart) {
01100         myHorizonStart = start;
01101         flagStartTimeSet = true;
01102         //qDebug("myHorizonStart reset");
01103     }
01104     if (end > myHorizonEnd) {
01105         myHorizonEnd = end;
01106         flagEndTimeSet = true;
01107         //qDebug("myHorizonEnd reset ");
01108     }
01109     flagDoNotRepaintAfterChange = true;//avoid flicker
01110     zoom(1.0); // set to 100%
01111     int viewWid = myGanttView->myCanvasView->viewport()->width();
01112     int timeWid =  getCoordX(end)-getCoordX(start);
01113     double fac;
01114     fac  = ( (double)viewWid)/((double) timeWid  );
01115     zoom (fac);
01116     timeWid =  getCoordX(end)-getCoordX(start);
01117     int count = 0;
01118     int lastScaleCount = 0;
01119     while (timeWid >viewWid || ( ( myRealMinorScaleCount != lastScaleCount)  && timeWid*2 < viewWid ) ) {
01120         lastScaleCount = myRealMinorScaleCount;
01121         fac = (fac * (double)viewWid)/(double)timeWid;
01122         zoom (fac);
01123         timeWid =  getCoordX(end)-getCoordX(start);
01124         if ( count++ > 10 ) {
01125             //qDebug("Exiting while loop in zoomToSelection ");
01126             break;
01127         }
01128     }
01129     flagDoNotRepaintAfterChange = false;
01130     updateTimeTable();
01131     repaint();
01132     moveTimeLineTo((getCoordX(start)-(viewWid-timeWid)/2));
01133 }
01134 void KDTimeHeaderWidget::moveTimeLineTo(int X)
01135 {
01136     int Y = myGanttView->myCanvasView->contentsY ();
01137     myGanttView->myCanvasView->setContentsPos (X, Y );
01138 }
01139 
01140 void KDTimeHeaderWidget::zoom(double factor, bool absolute)
01141 {
01142     flagZoomToFit = false;
01143     if ( factor < 0.000001 ) {
01144         qDebug("KDGanttView::zoom() : Zoom factor to low. Nothing zoomed. ");
01145         return;
01146     }
01147     double newZoom;
01148     if (absolute)
01149         newZoom = factor;
01150     else
01151         newZoom = myZoomFactor * factor;
01152     double relativeZoom;
01153     relativeZoom = newZoom / myZoomFactor;
01154 
01155     //qDebug("zooming relative %f ", relativeZoom);
01156     //qDebug("zooming absolute %f ", newZoom);
01157     int viewWid = myGanttView->myCanvasView->viewport()->width();
01158     if ( width() * relativeZoom < viewWid && ( newZoom > 1.01 || newZoom < 0.99 ) ) {
01159         qDebug("KDGanttView::zoom() : Zoom factor to low for current horizon. ");
01160         // qDebug("zooming relative %f, zooming absolute %f, viewWidth %d width %d ", relativeZoom,  newZoom, viewWid, width() );
01161         return;
01162     }
01163     myZoomFactor = newZoom;
01164     computeTicks();
01165 }
01166 
01174 void KDTimeHeaderWidget::setHorizonStart( const QDateTime& start )
01175 {
01176     myHorizonStart = start;
01177     flagStartTimeSet = true;
01178     computeTicks();
01179 }
01180 
01181 
01188 QDateTime KDTimeHeaderWidget::horizonStart() const
01189 {
01190     return myHorizonStart;
01191 }
01192 
01193 
01201 void KDTimeHeaderWidget::setHorizonEnd( const QDateTime& start )
01202 {
01203     myHorizonEnd = start;
01204     flagEndTimeSet = true;
01205     computeTicks();
01206 
01207 }
01208 
01209 
01216 QDateTime KDTimeHeaderWidget::horizonEnd() const
01217 {
01218     return myHorizonEnd;
01219 }
01220 
01221 
01230 void KDTimeHeaderWidget::setScale(Scale unit )
01231 {
01232     myScale = unit;
01233     myZoomFactor = 1.0;
01234     computeTicks();
01235 
01236 
01237 }
01238 
01239 
01246 KDTimeHeaderWidget::Scale KDTimeHeaderWidget::scale() const
01247 {
01248     return myScale;
01249 }
01250 
01251 
01258 void KDTimeHeaderWidget::setMaximumScale( Scale unit )
01259 {
01260     myMaxScale = unit;
01261     computeTicks();
01262 }
01263 
01264 
01271 KDTimeHeaderWidget::Scale  KDTimeHeaderWidget::maximumScale() const
01272 {
01273     return myMaxScale;
01274 }
01275 
01276 
01283 void  KDTimeHeaderWidget::setMinimumScale( Scale unit )
01284 {
01285     myMinScale = unit;
01286     computeTicks();
01287 }
01288 
01289 
01296 KDTimeHeaderWidget::Scale  KDTimeHeaderWidget::minimumScale() const
01297 {
01298     return myMinScale;
01299 }
01300 
01301 
01310 void KDTimeHeaderWidget::setMinimumColumnWidth( int width )
01311 {
01312     myMinimumColumWidth =  width;
01313     computeTicks();
01314 }
01315 
01316 
01323 int KDTimeHeaderWidget::minimumColumnWidth() const
01324 {
01325     return myMinimumColumWidth;
01326 }
01327 
01328 
01336 void KDTimeHeaderWidget::setYearFormat( YearFormat format )
01337 {
01338     myYearFormat =  format;
01339     computeTicks();
01340 }
01341 
01342 
01349 KDTimeHeaderWidget::YearFormat KDTimeHeaderWidget::yearFormat() const
01350 {
01351     return  myYearFormat;
01352 }
01353 
01354 
01362 void KDTimeHeaderWidget::setHourFormat( HourFormat format )
01363 {
01364     myHourFormat = format;
01365     computeTicks();
01366 }
01367 
01368 
01375 KDTimeHeaderWidget::HourFormat KDTimeHeaderWidget::hourFormat() const
01376 {
01377     return myHourFormat;
01378 }
01379 
01380 
01387 void KDTimeHeaderWidget::setShowMajorTicks( bool show )
01388 {
01389     flagShowMajorTicks = show;
01390     if (show) {
01391         setShowMinorTicks(false);
01392     }
01393     updateTimeTable();
01394 }
01395 
01396 
01403 bool KDTimeHeaderWidget::showMajorTicks() const
01404 {
01405     return flagShowMajorTicks;
01406 }
01407 
01408 
01415 void KDTimeHeaderWidget::setShowMinorTicks( bool show )
01416 {
01417     flagShowMinorTicks = show;
01418     if (show)
01419         setShowMajorTicks(false );
01420     //repaintMe();
01421     updateTimeTable();
01422 }
01423 
01424 
01431 bool KDTimeHeaderWidget::showMinorTicks() const
01432 {
01433     return flagShowMinorTicks;
01434 }
01435 
01436 
01445 void KDTimeHeaderWidget::setColumnBackgroundColor( const QDateTime& column,
01446                                                    const QColor& color,
01447                                                    Scale mini, Scale maxi )
01448 {
01449     ColumnColorList::iterator it;
01450     for ( it = ccList.begin(); it != ccList.end(); ++it ) {
01451         if ((*it).datetime == column) {
01452             (*it).color = color;
01453             (*it).minScaleView = mini;
01454             (*it).maxScaleView = maxi;
01455             return;
01456         }
01457     }
01458     DateTimeColor newItem;
01459     newItem.datetime = column;
01460     newItem.color = color;
01461     newItem.minScaleView = mini;
01462     newItem.maxScaleView = maxi;
01463     ccList.append(newItem);
01464     updateTimeTable();
01465 }
01466 
01467 void KDTimeHeaderWidget::computeIntervals( int height )
01468 {
01469 
01470     IntervalColorList::iterator it;
01471     int left, right;
01472     for ( it = icList.begin(); it != icList.end(); ++it ) {
01473         if ( (*it).minScaleView <= myRealScale && (*it).maxScaleView >= myRealScale ) {
01474             left = getCoordX((*it).datetime);
01475             right = getCoordX((*it).end);
01476             if ( right == left )
01477                 ++right;
01478             (*it).canvasRect->setPen( QPen::NoPen );
01479             (*it).canvasRect->setBrush( QBrush( (*it).color, SolidPattern) );
01480             (*it).canvasRect->setSize( right - left ,height );
01481             (*it).canvasRect->move( left,0 );
01482             (*it).canvasRect->show();
01483         } else {
01484             (*it).canvasRect->hide();
01485             /*
01486               (*it).canvasLine->setPen( QPen( (*it).color, right - left ) );
01487               (*it).canvasLine->setPoints( mid ,0 ,mid ,height );
01488               (*it).canvasLine->show();
01489               } else {
01490               (*it).canvasLine->hide();
01491             */
01492         }
01493     }
01494 }
01495 bool KDTimeHeaderWidget::changeBackgroundInterval( const QDateTime& oldstart,
01496                                                    const QDateTime& oldend,
01497                                                    const QDateTime& newstart,
01498                                                    const QDateTime& newend )
01499 {
01500     IntervalColorList::iterator it;
01501     for ( it = icList.begin(); it != icList.end(); ++it ) {
01502         if ((*it).datetime == oldstart && (*it).end == oldend ) {
01503             IntervalColorList::iterator it2;
01504             for ( it2 = icList.begin(); it2 != icList.end(); ++it2 ) {
01505                 if ((*it2).datetime == newstart && (*it2).end == newend )
01506                     return false;
01507             }
01508             (*it).datetime = newstart;
01509             (*it).end = newend;
01510             updateTimeTable();
01511             return true;
01512         }
01513     }
01514     return false;
01515 }
01516 bool KDTimeHeaderWidget::deleteBackgroundInterval( const QDateTime& start,
01517                                                    const QDateTime& end)
01518 {
01519     IntervalColorList::iterator it;
01520     for ( it = icList.begin(); it != icList.end(); ++it ) {
01521         if ((*it).datetime == start && (*it).end == end ) {
01522             //delete  (*it).canvasLine;
01523             delete  (*it).canvasRect;
01524             icList.remove(it);
01525             updateTimeTable();
01526             return true;
01527         }
01528     }
01529     return false;
01530 }
01531 
01532 void KDTimeHeaderWidget::setIntervalBackgroundColor( const QDateTime& start,
01533                                                      const QDateTime& end,
01534                                                      const QColor& color,
01535                                                      Scale mini ,
01536                                                      Scale maxi )
01537 {
01538 
01539     IntervalColorList::iterator it;
01540     for ( it = icList.begin(); it != icList.end(); ++it ) {
01541         if ((*it).datetime == start && (*it).end == end ) {
01542             (*it).color = color;
01543             (*it).minScaleView = mini;
01544             (*it).maxScaleView = maxi;
01545             return;
01546         }
01547     }
01548     DateTimeColor newItem;
01549     if ( start <= end ) {
01550         newItem.datetime = start;
01551         newItem.end = end;
01552     } else {
01553         newItem.datetime = end;
01554         newItem.end = start;
01555     }
01556     newItem.color = color;
01557     newItem.minScaleView = mini;
01558     newItem.maxScaleView = maxi;
01559     //newItem.canvasLine = new KDCanvasLine(myGanttView->myTimeTable,0,Type_is_KDGanttGridItem);
01560     newItem.canvasRect = new KDCanvasRectangle(myGanttView->myTimeTable,0,Type_is_KDGanttGridItem);
01561     newItem.canvasRect->setZ(-19);
01562     icList.append(newItem);
01563     updateTimeTable();
01564 
01565 }
01566 void KDTimeHeaderWidget::clearBackgroundColor()
01567 {
01568 
01569     IntervalColorList::iterator itic;
01570     for ( itic = icList.begin(); itic != icList.end(); ++itic ) {
01571         delete  (*itic).canvasRect;
01572     }
01573     ccList.clear();
01574     icList.clear();
01575     updateTimeTable();
01576 }
01577 QDateTime KDTimeHeaderWidget::getDateTimeForIndex(int X, bool local )
01578 {
01579     int coordX = X;
01580     if ( !local ) {
01581         QPoint p = QPoint ( X, 1 );
01582         coordX = myGanttView->myTimeHeaderScroll->viewportToContents(myGanttView->myTimeHeaderScroll->mapFromGlobal( p )).x();
01583 
01584     }
01585     double secs = (secsFromTo( myRealStart, myRealEnd ) * ((double)coordX))/(double)width();
01586     double days = secs/86400.0;
01587     secs = secs - ( ((int) days) *86400.0 );
01588     return (myRealStart.addDays ( (int) days )).addSecs( (int) secs);
01589 }
01590 
01591 //FIXME: This doesn't work quite intuitively (imho) when scale is day
01592 //       and each column containes more than 1 day:
01593 //       1) If a column includes a weekend day, the whole column gets weekend color,
01594 //       2) If a column includes 7 days, either *all* columns get weekend color, or
01595 //          *none* get weekend color (haven't figured out why)
01596 //       Proposal: Only use weekend color if the whole column is a weekend.
01597 //       Alt: Color the area that actually is the weekend.
01598 bool KDTimeHeaderWidget::getColumnColor(QColor& col,int coordLow, int coordHigh)
01599 {
01600     if (!flagShowMajorTicks && !flagShowMinorTicks)
01601         return false;
01602     QDateTime start,end;
01603     start = getDateTimeForIndex(coordLow);
01604     end = getDateTimeForIndex(coordHigh).addSecs(-1);
01605     Scale tempScale = myRealScale;
01606     if (flagShowMajorTicks)
01607         switch (myRealScale)
01608             {
01609             case KDGanttView::Minute: tempScale = KDGanttView::Hour;  break;
01610             case KDGanttView::Hour: tempScale = KDGanttView::Day   ;  break;
01611             case KDGanttView::Day: tempScale = KDGanttView::Week   ;  break;
01612             case KDGanttView::Week: tempScale = KDGanttView::Month  ;  break;
01613             case KDGanttView::Month: return false   ;  break;
01614             case KDGanttView::Auto: return false   ;  break;
01615             }
01616     //check defined column color
01617     ColumnColorList::iterator it;
01618     for ( it = ccList.begin(); it != ccList.end(); ++it ) {
01619         if ((*it).datetime  >= start && (*it).datetime  <= end) {
01620             if (tempScale >= (*it).minScaleView &&   tempScale <= (*it).maxScaleView    ) {
01621                 col = (*it).color;
01622                 return true;
01623             }
01624         }
01625     }
01626 
01627     if (tempScale > KDGanttView::Day) return false;
01628 
01629     start = getDateTimeForIndex((coordLow+coordHigh)/2);
01630     int day = start.date().dayOfWeek ();
01631     //checkweekdaycolor
01632     if (weekdayColor[day] != Qt::white) {
01633         col = weekdayColor[day];
01634         return true;
01635     }
01636     //checkweekendcolor
01637     int endday = myWeekendDaysEnd;
01638     col = myWeekendBackgroundColor;
01639     if (myWeekendDaysStart > myWeekendDaysEnd)
01640         endday +=7;
01641     if (day >= myWeekendDaysStart && day <= endday) {
01642         return true;
01643     } else {
01644         if (day+7 >= myWeekendDaysStart && day+7 <= endday) {
01645             return true;
01646         }
01647     }
01648     return false;
01649 }
01650 
01659 QColor KDTimeHeaderWidget::columnBackgroundColor( const QDateTime& column ) const
01660 {
01661     QColor c;
01662     c = white;
01663     ColumnColorList::const_iterator ite;
01664     for ( ite = ccList.begin(); ite != ccList.end(); ++ite ) {
01665         if ((*ite).datetime == column) {
01666             c = (*ite).color;
01667         }
01668     }
01669     return c;
01670 }
01671 
01672 
01681 void KDTimeHeaderWidget::setWeekendBackgroundColor( const QColor& color )
01682 {
01683     myWeekendBackgroundColor = color ;
01684     updateTimeTable();
01685 }
01686 
01687 
01694 QColor KDTimeHeaderWidget::weekendBackgroundColor() const
01695 {
01696     return myWeekendBackgroundColor;
01697 }
01698 
01709 void KDTimeHeaderWidget::setWeekdayBackgroundColor( const QColor& color, int  weekday )
01710 {
01711     weekdayColor[weekday] = color;
01712     updateTimeTable();
01713 }
01714 
01715 
01723 QColor KDTimeHeaderWidget::weekdayBackgroundColor(int weekday) const
01724 {
01725     return weekdayColor[weekday];
01726 }
01727 
01728 
01739 void KDTimeHeaderWidget::setWeekendDays( int start, int end )
01740 {
01741     myWeekendDaysStart = start;
01742     myWeekendDaysEnd = end;
01743     updateTimeTable();
01744 }
01745 
01746 
01754 void KDTimeHeaderWidget::weekendDays( int& start, int& end ) const
01755 {
01756     start = myWeekendDaysStart;
01757     end = myWeekendDaysEnd ;
01758 }
01759 
01760 
01761 
01768 void KDTimeHeaderWidget::setMajorScaleCount( int count )
01769 {
01770     myMajorScaleCount=count;
01771     computeTicks();
01772 }
01773 
01774 
01781 int KDTimeHeaderWidget::majorScaleCount() const
01782 {
01783     return myMajorScaleCount;
01784 }
01785 
01786 
01793 void KDTimeHeaderWidget::setMinorScaleCount( int count )
01794 {
01795     myMinorScaleCount = count;
01796     computeTicks();
01797 }
01798 
01799 
01806 int KDTimeHeaderWidget::minorScaleCount() const
01807 {
01808     return myMinorScaleCount ;
01809 
01810 }
01811 
01812 
01813 void KDTimeHeaderWidget::resizeEvent ( QResizeEvent * )
01814 {
01815     // qDebug("KDTimeHeaderWidget:: resizeEvent ");
01816     paintPix.resize( 800, height () );
01817 }
01818 
01819 
01820 void KDTimeHeaderWidget::updateTimeTable()
01821 {
01822     //qDebug("KDTimeHeaderWidget::updateTimeTable() ");
01823     if (flagDoNotRecomputeAfterChange) return;
01824     // setting the scrolling steps
01825     int scrollLineStep = myGridMinorWidth;
01826     if (showMajorTicks()) {
01827         QValueList<int>::iterator intIt = majorTicks.begin();
01828         scrollLineStep = 5 * myGridMinorWidth;
01829         if (intIt != majorTicks.end()) {
01830             int left = *intIt;
01831             ++intIt;
01832             if (intIt != majorTicks.end()) {
01833                 scrollLineStep = *intIt-left;
01834             }
01835         }
01836     }
01837     myGanttView->myCanvasView->horizontalScrollBar()->setLineStep(scrollLineStep);
01838     myGanttView->myTimeTable->maximumComputedGridHeight = 0;
01839     myGanttView->myTimeTable->updateMyContent();
01840 }
01841 
01842 
01843 void KDTimeHeaderWidget::setAutoScaleMinorTickCount( int count )
01844 {
01845     myAutoScaleMinorTickcount = count;
01846     computeTicks();
01847 
01848 }
01849 
01850 
01851 int KDTimeHeaderWidget::autoScaleMinorTickCount()
01852 {
01853     return myAutoScaleMinorTickcount;
01854 }
01855 
01856 
01857 void KDTimeHeaderWidget::repaintMe(int left,int paintwid, QPainter* painter)
01858 {
01859     if (flagDoNotRecomputeAfterChange) return;
01860     QColorGroup qcg =QColorGroup( white, black,white, darkGray,black,gray,gray) ;
01861     QPainter* p;
01862     int offsetLeft = 0;
01863     if ( paintwid > paintPix.width()-100 )
01864         paintPix.resize( paintwid+100, height () );
01865     if ( painter )
01866         p = painter;
01867     else {
01868         p = new QPainter( &paintPix );
01869         offsetLeft = left-50;
01870     }
01871     if ( mouseDown ) {
01872         p->fillRect( left-offsetLeft, 0, paintwid, height(), QBrush(paletteBackgroundColor()) );
01873         int start ;
01874         int wid;
01875         if ( beginMouseDown < endMouseDown ) {
01876             start = beginMouseDown ;
01877             wid = endMouseDown - beginMouseDown ;
01878         } else {
01879             start = endMouseDown ;
01880             wid = -endMouseDown + beginMouseDown ;
01881         }
01882         p->fillRect( start-offsetLeft, 0, wid, height(), QBrush(paletteBackgroundColor().dark()) );
01883     } else {
01884         if (! painter )
01885             p->fillRect( left-offsetLeft, 0, paintwid, height(), QBrush(paletteBackgroundColor()) );
01886     }
01887     p->setPen(QColor(40,40,40));
01888     QFont tempFont = p->font();
01889     tempFont.setWeight(63);
01890     p->setFont(tempFont);
01891     int hei1 = myMajorGridHeight,
01892         hei2 = height(),
01893         wid1 = myGridMinorWidth;
01894     int xCoord;
01895     int lwid = 1;
01896 
01897     QValueList<QString>::iterator it;
01898     QValueList<int>::iterator intIt = majorTicks.begin();
01899     for ( it =  majorText.begin(); it !=  majorText.end(); ++it ) {
01900         xCoord  = (*intIt++);
01901         if (((*intIt)>= left && xCoord <= left+paintwid)) {
01902             qDrawShadeLine ( p,xCoord-offsetLeft ,hei1+1, xCoord-offsetLeft, -2, qcg, true, lwid, 1 );
01903             p->drawText(xCoord+4-offsetLeft,hei1-4,(*it));
01904         }
01905     }
01906     qDrawShadeLine ( p,left-offsetLeft  ,hei1, left+paintwid-offsetLeft, hei1, qcg, true, lwid, 1 );
01907     int i = 0;
01908     for ( it =  minorText.begin(); it !=  minorText.end(); ++it ) {
01909         if (i*wid1 >= left-wid1 && i*wid1 <= left+paintwid) {
01910             qDrawShadeLine ( p,i*wid1-offsetLeft ,hei1-1, i*wid1-offsetLeft, hei2, qcg, true, lwid, 1 );
01911             p->drawText(i*wid1+1-offsetLeft,hei1+1,wid1-1,hei2-hei1,Qt::AlignCenter,(*it));
01912         }
01913         ++i;
01914     }
01915     p->setPen(black);
01916     p->drawLine(left-offsetLeft,hei1,left+paintwid-offsetLeft,hei1);
01917     qDrawShadeLine ( p,left-offsetLeft  ,hei2-1, left+paintwid-offsetLeft, hei2-1, qcg, true, lwid, 1 );
01918     p->drawLine(left-offsetLeft,hei2-1,left+paintwid-offsetLeft,hei2-1);
01919     if ( !painter ) {
01920         p->end();
01921         delete p;
01922         bitBlt ( this, left, 0, &paintPix, 50, 0, paintwid, height() );
01923     }
01924 }
01925 
01926 // cuts the secs in the DateTime if scale is Minute ,
01927 // the minutes and secs if scale is Hour and so on
01928 
01929 QDateTime KDTimeHeaderWidget::getEvenTimeDate(QDateTime tempdatetime ,Scale sc)
01930 {
01931     QDate tempdate;
01932     int min, hour;
01933     int tempMinorScaleCount = myRealMinorScaleCount;
01934     switch (sc)
01935         {
01936         case KDGanttView::Month:
01937             tempdate = tempdatetime.date();
01938             while (tempdate.day ()!= 1 )
01939                 tempdate = tempdate.addDays(-1);
01940             //while (tempdate.month ()!= 1 )
01941             //tempdate = tempdate.addMonths(-1);
01942             tempdatetime = QDateTime (tempdate, QTime (0,0));
01943             break;
01944         case KDGanttView::Week:
01945             tempdate = tempdatetime.date();
01946             while (tempdate.dayOfWeek ()!= KGlobal::locale()->weekStartDay())
01947                 tempdate = tempdate.addDays(-1);
01948             //tempdate = tempdate.addDays(-7);
01949             tempdatetime = QDateTime (tempdate, QTime (0,0));
01950             break;
01951         case KDGanttView::Day:
01952             tempdatetime = QDateTime (tempdatetime.date(), QTime ( 0,0 ) );
01953             break;
01954         case KDGanttView::Hour:
01955             hour = tempdatetime.time().hour();
01956             while (24%tempMinorScaleCount > 0 && 24%tempMinorScaleCount < 24)
01957                 ++tempMinorScaleCount;
01958             hour = ( hour /tempMinorScaleCount)*tempMinorScaleCount;
01959             tempdatetime = QDateTime (tempdatetime.date(), QTime (hour, 0 ));
01960             break;
01961         case KDGanttView::Minute:
01962             min = tempdatetime.time().minute();
01963             while (60%tempMinorScaleCount > 0 && 60%tempMinorScaleCount < 60)
01964                 ++tempMinorScaleCount;
01965             // qDebug("myMinorScaleCount %d %d %d",myMinorScaleCount, myRealMinorScaleCount, tempMinorScaleCount);
01966             min = (min /tempMinorScaleCount)*tempMinorScaleCount;
01967             tempdatetime = QDateTime (tempdatetime.date(), QTime (tempdatetime.time().hour(),min ));
01968 
01969             break;
01970         case KDGanttView::Auto:
01971             break;
01972         }
01973     return tempdatetime;
01974 }
01975 
01976 
01977 void KDTimeHeaderWidget::computeRealScale(QDateTime start)
01978 {
01979 
01980     if (myScale ==KDGanttView::Auto) {
01981         //qDebug("Autoscale ");
01982         //double secsPerMinor = (((double)start.daysTo(myHorizonEnd))* 86400.00)/((double)myAutoScaleMinorTickcount);
01983         double secsPerMinor = (((double)start.secsTo(myHorizonEnd)))/((double)myAutoScaleMinorTickcount);
01984         secsPerMinor /= myZoomFactor;
01985         if (secsPerMinor <= 1800) {
01986             myRealScale =  KDGanttView::Minute;
01987             myRealMinorScaleCount = (int) secsPerMinor/60;
01988         } else {
01989             if (secsPerMinor <= 12*3600) {
01990                 myRealScale =  KDGanttView::Hour;
01991                 myRealMinorScaleCount = (int)  secsPerMinor/3600;
01992             } else {
01993                 if (secsPerMinor <= 24*3600*3) {
01994                     myRealScale =  KDGanttView::Day;
01995                     myRealMinorScaleCount = (int)  secsPerMinor/(3600*24);
01996                 } else {
01997                     if (secsPerMinor <= 24*3600*14) {
01998                         myRealScale =  KDGanttView::Week;
01999                         myRealMinorScaleCount =  (int) secsPerMinor/(3600*24*7);
02000                     } else {
02001                         myRealScale =  KDGanttView::Month;
02002                         myRealMinorScaleCount =  (int) secsPerMinor/(3600*24*30);
02003 
02004                     }
02005                 }
02006             }
02007         }
02008         if(myRealMinorScaleCount == 0)
02009             myRealMinorScaleCount = 1;
02010         myRealMajorScaleCount = 1;
02011     }
02012     else {
02013         //qDebug("Fixed scale ");
02014         myRealScale = myScale;
02015         if (myRealScale > myMaxScale)
02016             myRealScale = myMaxScale;
02017         if (myRealScale < myMinScale)
02018             myRealScale = myMinScale;
02019         myRealMinorScaleCount = (int) ( ((double)myMinorScaleCount) /myZoomFactor );
02020         double tempZoom = myZoomFactor;
02021         myRealMajorScaleCount =  myMajorScaleCount;
02022         while (myRealMinorScaleCount == 0) {
02023             if (myRealScale  == myMinScale) {
02024                 myRealMinorScaleCount = 1;
02025                 break;
02026             }
02027             switch (myRealScale)
02028                 {
02029                 case KDGanttView::Minute:
02030                     myRealMinorScaleCount = 1;
02031                     return;
02032                     break;
02033                 case KDGanttView::Hour:
02034                     myRealScale = KDGanttView::Minute;
02035                     tempZoom = tempZoom/60;
02036                     break;
02037                 case KDGanttView::Day:
02038                     myRealScale = KDGanttView::Hour;
02039                     tempZoom = tempZoom/24;
02040                     break;
02041                 case KDGanttView::Week:
02042                     myRealScale = KDGanttView::Day;
02043                     tempZoom = tempZoom/7;
02044                     break;
02045                 case KDGanttView::Month:
02046                     myRealScale =  KDGanttView::Week ;
02047                     tempZoom = tempZoom*7/30;
02048                     break;
02049                 case KDGanttView::Auto:
02050                     break;
02051                 }
02052             myRealMinorScaleCount =  (int) ( myMinorScaleCount /tempZoom );
02053         }
02054     }
02055 }
02056 
02057 
02058 void KDTimeHeaderWidget::computeTicks(bool doNotComputeRealScale)
02059 {
02060     if (flagDoNotRecomputeAfterChange) return;
02061     bool block = myGanttView->myTimeTable->blockUpdating();
02062     myGanttView->myTimeTable->setBlockUpdating( true );
02063     //qDebug("computeticks ");
02064     majorTicks.clear();
02065     minorText.clear();
02066     majorText.clear();
02067     if ( !doNotComputeRealScale )
02068         saveCenterDateTime();
02069     if (!doNotComputeRealScale)
02070         computeRealScale(myHorizonStart);
02071     myRealStart = getEvenTimeDate(myHorizonStart ,myRealScale);
02072     if (!doNotComputeRealScale)
02073         computeRealScale(myRealStart);
02074     int tempMinorScaleCount = myRealMinorScaleCount,
02075         tempMajorScaleCount = myRealMajorScaleCount;
02076     int minorItems,minorPerMajor = 1;
02077     minorItems = (int)  (secsFromTo( myRealStart, myHorizonEnd)/60.0);
02078     //qDebug("tempMinorScaleCount %d ", tempMinorScaleCount);
02079     QPainter p(this);
02080     int Width, Height;
02081     QString testTextMinor,testTextMajor, tempStr;
02082     QRect itemRectMinor, itemRectMajor;
02083     QDate tempDate = myRealStart.date();
02084     myRealEnd = myRealStart;
02085     // preparing the testtext for the differennt scales
02086     switch (myRealScale)
02087         {
02088             // the x in testTextMajor is added to reserve a little bit more space
02089         case KDGanttView::Minute:
02090             testTextMinor = "60";
02091             if (myHourFormat == KDGanttView::Hour_12)
02092                 testTextMajor = "Mon Aug 30, 12 AMx";
02093             else
02094                 testTextMajor = "Mon Aug 30, 24:00x";
02095             minorPerMajor = 6000;
02096             break;
02097         case KDGanttView::Hour:
02098             minorItems = minorItems/60;
02099             if (myHourFormat == KDGanttView::Hour_24)
02100                 testTextMinor = "24x";
02101             else
02102                 testTextMinor = "12 AM";
02103             testTextMajor = "Mon Aug 30, x";
02104             if ( yearFormat() != KDGanttView::NoDate )
02105                 testTextMajor += getYear(QDate::currentDate());
02106             minorPerMajor = 2400;
02107             break;
02108         case KDGanttView::Day:
02109             minorItems = minorItems/(60*24);
02110             testTextMinor = "88";
02111             testTextMajor = "Aug 30, x"+getYear(QDate::currentDate());
02112             minorPerMajor = 700;
02113             break;
02114         case KDGanttView::Week:
02115             minorItems = minorItems/(60*24*7);
02116             testTextMinor = "88";
02117             testTextMajor = "Aug x"+getYear(QDate::currentDate());
02118             minorPerMajor = 435; // 435 = 365days/12months/7days * 100
02119             break;
02120         case KDGanttView::Month:
02121             minorItems = (minorItems*12)/(60*24*365);
02122             testTextMinor = "M";
02123             testTextMajor = "x"+getYear(QDate::currentDate());
02124             minorPerMajor = 1200;
02125             break;
02126         case KDGanttView::Auto:
02127             qDebug("KDGanttView::Internal Error in KDTimeHeaderWidget::computeTicks() ");
02128             qDebug("             RealScale == Auto : This may not be! ");
02129             break;
02130         }
02131     itemRectMinor = p.boundingRect ( 10, 10, 2, 2, Qt::AlignLeft,testTextMinor);
02132     itemRectMajor = p.boundingRect ( 10, 10, 2, 2, Qt::AlignLeft,testTextMajor);
02133     p.end();
02134     //qDebug("     tempMinorScaleCount %d ", tempMinorScaleCount);
02135     Height = itemRectMinor.height()+itemRectMajor.height()+11;
02136     Width = (itemRectMinor.width()+5);
02137     if (Width < minimumColumnWidth()) Width = minimumColumnWidth();
02138     // if the desired width is greater than the maximum width of this widget
02139     // increase the minorscalecount
02140     int maxWid = myGanttView->myCanvasView->viewport()->width();
02141     if (!flagZoomToFit)
02142         maxWid = maximumWidth();
02143     while((minorItems/tempMinorScaleCount+1)*Width > maxWid)
02144         ++tempMinorScaleCount;
02145     //qDebug("             tempMinorScaleCount %d ", tempMinorScaleCount);
02146     mySizeHint = (minorItems/tempMinorScaleCount+1)*Width;
02147     switch (myRealScale)
02148         {
02149         case KDGanttView::Minute:
02150             if (tempMinorScaleCount < 60)
02151                 while (60%tempMinorScaleCount > 0 && 60%tempMinorScaleCount < 60)
02152                     ++tempMinorScaleCount;
02153             if (tempMinorScaleCount >= 60) {
02154                 myRealScale = KDGanttView::Hour;
02155                 myRealMinorScaleCount = tempMinorScaleCount/ 60;
02156                 // myRealMinorScaleCount = 1;
02157                 myRealMajorScaleCount = 1;
02158                 qDebug("KDGantt::Overzoom:Rescaling from Minute to Hour");
02159                 myGanttView->myTimeTable->setBlockUpdating( block );
02160                 emit myGanttView->rescaling( KDGanttView::Hour );
02161                 computeTicks(true);
02162                 return;
02163             }
02164             break;
02165         case KDGanttView::Hour:
02166             while (24%tempMinorScaleCount > 0 && 24%tempMinorScaleCount < 24)
02167                 ++tempMinorScaleCount;
02168             if (tempMinorScaleCount >= 24) {
02169                 myRealScale = KDGanttView::Day;
02170                 myRealMinorScaleCount = tempMinorScaleCount/ 24;
02171                 //myRealMinorScaleCount = 1;
02172                 myRealMajorScaleCount = 1;
02173                 qDebug("KDGantt::Overzoom:Rescaling from Hour to Day");
02174                 myGanttView->myTimeTable->setBlockUpdating( block );
02175                 emit myGanttView->rescaling( KDGanttView::Day );
02176                 computeTicks(true);
02177                 return;
02178             }
02179             break;
02180         default:
02181             break;
02182         }
02183     //flagZoomToFit = false;
02184     while((minorItems/tempMinorScaleCount+1)*Width < myMinimumWidth ) {
02185         ++minorItems;
02186     }
02187     minorItems = (minorItems/tempMinorScaleCount)+1;
02188     // if not enough space for the text of the major scale, increase majorscalecount
02189     minorPerMajor = (minorPerMajor*tempMajorScaleCount)/tempMinorScaleCount;
02190     // checking, if enough space for majorscale
02191     // if not, increasing MajorScaleCount
02192 
02193     while ((minorPerMajor*Width)/100 < itemRectMajor.width()) {
02194         minorPerMajor = minorPerMajor/tempMajorScaleCount;
02195         ++tempMajorScaleCount;
02196         minorPerMajor = minorPerMajor*tempMajorScaleCount;
02197 
02198     }
02199     // now we have the fixed  width of the minorscale computed
02200     myGridMinorWidth = Width;
02201     // the width of this widget is the gridwidth * the amount of items
02202     Width *= minorItems;
02203     // if size changed, reset geometry
02204     if (width() != Width   || height() != Height  )
02205         {
02206             resize( Width, Height );
02207             emit  sizeChanged( Width );
02208         }
02209     myMajorGridHeight = itemRectMajor.height()+5;
02210     QTime tempTime = myRealStart.time();
02211     QDateTime tempDateTime;
02212     int i;
02213     const KCalendarSystem * calendar = KGlobal::locale()->calendar();
02214     switch (myRealScale)
02215         {
02216         case KDGanttView::Minute:
02217             myRealEnd = myRealEnd.addSecs((minorItems)*tempMinorScaleCount*60);
02218             for ( i = 0; i < minorItems;++i) {
02219                 tempStr.setNum(tempTime.minute());
02220                 minorText.append(tempStr);
02221                 tempTime = tempTime.addSecs(60*tempMinorScaleCount);
02222             }
02223             tempDateTime = myRealStart;
02224             while (tempDateTime.time().minute() != 0)
02225                 tempDateTime = tempDateTime.addSecs(60);
02226             while (tempDateTime < myRealEnd) {
02227                 majorTicks.append( getCoordX(tempDateTime));
02228                 tempStr.setNum(tempDateTime.date().day());
02229                 if ( yearFormat() == KDGanttView::NoDate ) {
02230                     tempStr = calendar->weekDayName( tempDateTime.date() )+", "
02231                         +getHour(tempDateTime.time());
02232                 } else {
02233                     tempStr = calendar->weekDayName( tempDateTime.date(), true )+" "+
02234                         calendar->monthName( tempDateTime.date().month(), tempDateTime.date().year(), true)+ " "+
02235                         tempStr+", "+getHour(tempDateTime.time());
02236                 }
02237 
02238                 majorText.append(tempStr);
02239                 tempDateTime = tempDateTime.addSecs(3600*tempMajorScaleCount);
02240             }
02241             majorTicks.append( getCoordX(tempDateTime));
02242             break;
02243 
02244         case KDGanttView::Hour:
02245             myRealEnd = myRealEnd.addSecs(minorItems*tempMinorScaleCount*60*60);
02246 
02247             for ( i = 0; i < minorItems;++i) {
02248                 tempStr = getHour(tempTime);
02249                 minorText.append(tempStr);
02250                 tempTime = tempTime.addSecs(3600*tempMinorScaleCount);
02251             }
02252             tempDateTime = myRealStart;
02253             while (tempDateTime.time().hour() != 0)
02254                 tempDateTime = tempDateTime.addSecs(3600);
02255             while (tempDateTime < myRealEnd) {
02256                 majorTicks.append( getCoordX(tempDateTime));
02257                 tempStr.setNum(tempDateTime.date().day());
02258                 if ( yearFormat() == KDGanttView::NoDate ) {
02259                     tempStr = calendar->weekDayName( tempDateTime.date() );
02260                 } else {
02261                     tempStr = calendar->weekDayName( tempDateTime.date(), true )+" "+
02262                         calendar->monthName( tempDateTime.date().month(), tempDateTime.date().year(), true)+ " "+
02263                         tempStr+", "+getYear(tempDateTime.date());
02264                 }
02265                 majorText.append(tempStr);
02266                 tempDateTime = tempDateTime.addDays(tempMajorScaleCount);
02267             }
02268             majorTicks.append( getCoordX(tempDateTime));
02269             break;
02270         case KDGanttView::Day:
02271             myRealEnd = myRealEnd.addDays(minorItems*tempMinorScaleCount);
02272             for ( i = 0; i < minorItems;++i) {
02273                 if (tempMinorScaleCount == 1)
02274                     minorText.append((calendar->weekDayName(tempDate, true)).left(1)); //TODO: BIDI
02275                 else
02276                     minorText.append(QString::number(tempDate.day()));
02277                 tempDate = tempDate.addDays(tempMinorScaleCount);
02278             }
02279             tempDate = myRealStart.date();
02280             while (tempDate.dayOfWeek() != KGlobal::locale()->weekStartDay())
02281                 tempDate = tempDate.addDays(1);
02282             while (tempDate < myRealEnd.date()) {
02283                 majorTicks.append( getCoordX(tempDate));
02284                 tempStr.setNum(tempDate.day());
02285                 tempStr = calendar->monthName(tempDate.month(), tempDate.year(), true)+ " "+
02286                     tempStr+", "+getYear(tempDate);
02287                 majorText.append(tempStr);
02288                 tempDate = tempDate.addDays(7*tempMajorScaleCount);
02289             }
02290             majorTicks.append( getCoordX(tempDate));
02291             break;
02292         case KDGanttView::Week:
02293             myRealEnd = myRealEnd.addDays(minorItems*tempMinorScaleCount*7);
02294             for ( i = 0; i < minorItems;++i) {
02295                 tempStr.setNum(tempDate.day());
02296                 minorText.append(tempStr);
02297                 tempDate = tempDate.addDays(7*tempMinorScaleCount);
02298             }
02299             tempDate = myRealStart.date();
02300             while (tempDate.day() != KGlobal::locale()->weekStartDay())
02301                 tempDate = tempDate.addDays(1);
02302             while (tempDate < myRealEnd.date()) {
02303                 majorTicks.append( getCoordX(tempDate));
02304                 tempStr = calendar->monthName(tempDate.month(), tempDate.year(), true)+ " "+getYear(tempDate);
02305                 majorText.append(tempStr);
02306                 tempDate = tempDate.addMonths(tempMajorScaleCount);
02307             }
02308             majorTicks.append( getCoordX(tempDate));
02309             break;
02310         case KDGanttView::Month:
02311             myRealEnd = myRealEnd.addMonths(minorItems*tempMinorScaleCount);
02312             for ( i = 0; i < minorItems;++i) {
02313                 minorText.append((calendar->monthName(tempDate.month(), tempDate.year(), true)).left(1)); //TODO: BIDI
02314                 tempDate = tempDate.addMonths(tempMinorScaleCount);
02315             }
02316             tempDate = myRealStart.date();
02317             while (tempDate.month() != 1)
02318                 tempDate = tempDate.addMonths(1);
02319             while (tempDate < myRealEnd.date()) {
02320                 majorTicks.append( getCoordX(tempDate));
02321                 tempStr = getYear(tempDate);
02322                 majorText.append(tempStr);
02323                 tempDate = tempDate.addYears(tempMajorScaleCount);
02324             }
02325             majorTicks.append( getCoordX(tempDate));
02326             break;
02327         case KDGanttView::Auto:
02328             break;
02329         }
02330 
02331     if (flagDoNotRepaintAfterChange) {
02332         myGanttView->myTimeTable->setBlockUpdating( block );
02333         return;
02334     }
02335     //qDebug("KDTimeHeaderWidget width %d, viewport width %d  ",width (), myGanttView->myCanvasView->viewport()->width());
02336     myGanttView->myTimeTable->setBlockUpdating( block );
02337     updateTimeTable();
02338     centerDateTime(myCenterDateTime);
02339     repaint();
02340 }
02341 
02342 
02343 void KDTimeHeaderWidget::saveCenterDateTime()
02344 {
02345     double wid = width();
02346     double allsecs = secsFromTo( myRealStart, myRealEnd );
02347     double center = myGanttView->myCanvasView->viewport()->width();
02348     center = center / 2;
02349     center = center + myGanttView->myCanvasView->contentsX();
02350     double secs = (allsecs*center)/wid;
02351     double days = secs/86400.0;
02352     secs = secs - ( (int) days *86400.0 );
02353     myCenterDateTime =  (myRealStart.addDays ( (int) days )).addSecs( (int) secs);
02354 }
02355 
02356 
02357 void KDTimeHeaderWidget::centerDateTime( const QDateTime& center )
02358 {
02359      moveTimeLineTo(getCoordX( center )-(myGanttView->myCanvasView->viewport()->width() /2));
02360     //  qDebug("centerDateTime %s %d %d", center.toString().latin1(),getCoordX( center ),(myGanttView->myCanvasView->viewport()->width() /2) );
02361    
02362 }
02363 
02364 
02365 void KDTimeHeaderWidget::paintEvent(QPaintEvent *p)
02366 {
02367     repaintMe(p->rect().x(),p->rect().width());
02368 }
02369 
02370 
02371 int KDTimeHeaderWidget::getCoordX(QDate date)
02372 {
02373     int wid = width();
02374     int daysAll = myRealStart.daysTo(myRealEnd);
02375     if (daysAll == 0) return 0;
02376     int days = myRealStart.daysTo(QDateTime(date));
02377     return (wid *days) /daysAll;
02378 }
02379 
02380 
02381 int KDTimeHeaderWidget::getCoordX(QDateTime datetime)
02382 {
02383     double wid = width();
02384     double secsAll = secsFromTo( myRealStart, myRealEnd );
02385     if (secsAll == 0.0) return 0;
02386     double secs = secsFromTo( myRealStart, datetime);
02387     return ((int)((wid *(secs /secsAll))+0.5));
02388 }
02389 
02390 
02391 QString KDTimeHeaderWidget::getYear(QDate date)
02392 {
02393     QString ret;
02394     ret.setNum(date.year());
02395     switch (yearFormat()) {
02396     case KDGanttView::FourDigit:
02397         // nothing to do
02398         break;
02399     case KDGanttView::TwoDigit:
02400         ret = ret.right(2);
02401         break;
02402     case KDGanttView::TwoDigitApostrophe:
02403         ret = "'"+ret.right(2);
02404         break;
02405     case KDGanttView::NoDate:
02406         // nothing to do
02407         break;
02408     }
02409     return ret;
02410 }
02411 
02412 
02413 QString KDTimeHeaderWidget::getHour(QTime time)
02414 {
02415     QString ret;
02416     int hour = time.hour();
02417     if (myHourFormat == KDGanttView::Hour_12) {
02418         if (hour >= 12) {
02419             if (hour > 12) hour -=12;
02420             ret.setNum(hour);
02421             ret = ret +" PM";
02422         } else {
02423             if (hour == 0) hour = 12;
02424             ret.setNum(hour);
02425             ret = ret +" AM";
02426         }
02427     } else {
02428         if (myHourFormat == KDGanttView::Hour_24)
02429             ret.setNum(hour);
02430         else {
02431             ret.setNum(hour);
02432             ret += ":00";
02433         }
02434     }
02435     return ret;
02436 }
02437 
02438 
02439 void KDTimeHeaderWidget::mousePressEvent ( QMouseEvent * e )
02440 {
02441     mouseDown = false;
02442     switch ( e->button() ) {
02443     case LeftButton:
02444         mouseDown = true;
02445         beginMouseDown = e->pos().x();
02446         endMouseDown = e->pos().x();
02447         break;
02448     case RightButton:
02449         if (flagShowPopupMenu)
02450             myPopupMenu->popup(e->globalPos());
02451         break;
02452     case MidButton:
02453         break;
02454     default:
02455         break;
02456     }
02457 
02458 }
02459 
02460 
02461 void KDTimeHeaderWidget::mouseReleaseEvent ( QMouseEvent *  )
02462 {
02463     if ( mouseDown ) {
02464         mouseDown = false;
02465         // zoom to selection getDateTimeForIndex(
02466         int start, end;
02467         if ( beginMouseDown < endMouseDown ) {
02468             start = beginMouseDown;
02469             end = endMouseDown;
02470         } else {
02471             start = endMouseDown;
02472             end = beginMouseDown;
02473         }
02474         if (start < 0 )
02475             start = 0;
02476         if ( end > width() )
02477             end = width();
02478         //qDebug("start %s ",getDateTimeForIndex(start).toString().latin1() );
02479         //qDebug("end %s ",getDateTimeForIndex(end).toString().latin1() );
02480         emit myGanttView->timeIntervalSelected( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02481         emit myGanttView->timeIntervallSelected( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02482         //zoomToSelection( getDateTimeForIndex(start),getDateTimeForIndex(end) );
02483     }
02484     mouseDown = false;
02485     repaint();
02486 }
02487 
02488 
02489 void KDTimeHeaderWidget::mouseDoubleClickEvent ( QMouseEvent *  )
02490 {
02491 
02492 }
02493 
02494 
02495 void KDTimeHeaderWidget::mouseMoveEvent ( QMouseEvent * e )
02496 {
02497     if ( mouseDown ) {
02498         if ( e->pos().y() < -height() || e->pos().y() > 2* height() ) {
02499             mouseDown = false;
02500             repaint();
02501             return;
02502         }
02503         endMouseDown = e->pos().x();
02504         //repaint;
02505         int val = -1;
02506         if (endMouseDown <  -x() ) {
02507             val = myGanttView->myCanvasView->horizontalScrollBar()->value() -
02508                 myGanttView->myCanvasView->horizontalScrollBar()->lineStep();
02509             if ( val < 0 ) {
02510                 val = 0;
02511             }
02512         }
02513         if (endMouseDown >  -x() +parentWidget()->width() ) {
02514             val = myGanttView->myCanvasView->horizontalScrollBar()->value() +
02515                 myGanttView->myCanvasView->horizontalScrollBar()->lineStep();
02516 
02517         }
02518         repaintMe(-x(),parentWidget()->width());
02519         if ( val > -1 ) {
02520             if ( val > myGanttView->myCanvasView->horizontalScrollBar()->maxValue() ) {
02521                 val = myGanttView->myCanvasView->horizontalScrollBar()->maxValue();
02522             }
02523             myGanttView->myCanvasView->horizontalScrollBar()->setValue( val );
02524         }
02525         //qDebug("mousemove %d %d %d %d",endMouseDown, -x(),parentWidget()->width() , e->pos().y());
02526     }
02527 }
02528 
02529 
02530 /* ***************************************************************
02531    KDLegendWidget:: KDLegendWidget
02532    ***************************************************************** */
02533 KDLegendWidget:: KDLegendWidget( QWidget* parent,
02534                                  KDGanttMinimizeSplitter* legendParent ) :
02535     KDGanttSemiSizingControl ( KDGanttSemiSizingControl::Before, Vertical,
02536                                parent)
02537 {
02538     myLegendParent = legendParent;
02539     dock = 0;
02540     scroll = new QScrollView( legendParent );
02541     setMaximizedWidget( scroll );
02542 
02543     setMinimizedWidget( myLabel = new QLabel( i18n( " Legend is hidden" ), this) );
02544     setGeometry( 0, 0, 50, 50 );
02545     myLegend = 0;
02546     clearLegend();
02547     showMe ( false );
02548 }
02549 void KDLegendWidget::setAsDockwindow( bool dockwin )
02550 {
02551     if ( (dock == 0 && !dockwin) || ( dock && dockwin  ) )
02552         return;
02553     if ( dockwin )
02554         {
02555             setMaximizedWidget( 0 );
02556             showMe ( false );
02557             if ( dock ) delete dock;
02558             dock = new QDockWindow(QDockWindow:: OutsideDock,0 );
02559             dock->resize( 200, 100 );
02560             dock->setHorizontallyStretchable( true );
02561             dock->setVerticallyStretchable( true );
02562             dock->setCaption(i18n("Legend: ") );
02563             dock->setResizeEnabled (true );
02564             delete myLegend;
02565             myLegend = 0;
02566             delete scroll;
02567             scroll = new QScrollView( dock );
02568             clearLegend();
02569             dock->setWidget(scroll);
02570             setMaximizedWidget( dock );
02571             showMe ( false );
02572 
02573         } else {
02574             setMaximizedWidget( 0 );
02575             showMe ( false );
02576             delete myLegend;
02577             myLegend = 0;
02578             delete scroll;
02579             delete dock;
02580             dock = 0;
02581             scroll = new QScrollView( myLegendParent );
02582             clearLegend();
02583             setMaximizedWidget( scroll );
02584             showMe ( false );
02585         }
02586 
02587 }
02588 
02589 
02590 bool KDLegendWidget::asDockwindow( )
02591 {
02592     if ( dock )
02593         return true;
02594     return false;
02595 }
02596 
02597 
02598 QDockWindow* KDLegendWidget::dockwindow( )
02599 {
02600     return dock;
02601 }
02602 
02603 
02604 void KDLegendWidget::setFont( QFont font)
02605 {
02606     myLegend->setFont( font);
02607     myLabel->setFont( font);
02608     QWidget::setFont( font );
02609 }
02610 
02611 
02612 void KDLegendWidget::drawToPainter( QPainter *p )
02613 {
02614     p->drawPixmap( 0, 0, QPixmap::grabWidget( myLegend ) );
02615 }
02616 
02617 
02618 QSize KDLegendWidget::legendSize()
02619 {
02620     return myLegend->size();
02621 }
02622 
02623 
02624 QSize KDLegendWidget::legendSizeHint()
02625 {
02626 
02627     qApp->processEvents();
02628     return QSize( myLegend->sizeHint().width(), myLegend->sizeHint().height()+scroll->horizontalScrollBar()->height());
02629 }
02630 
02631 
02632 void KDLegendWidget::showMe ( bool show )
02633 {
02634     minimize( !show );
02635 }
02636 
02637 
02638 void KDLegendWidget::clearLegend ( )
02639 {
02640     if ( myLegend ) delete myLegend;
02641     if ( dock )
02642         myLegend = new QGroupBox( 1, Qt::Horizontal, scroll->viewport() );
02643     else
02644         myLegend = new QGroupBox( 1, Qt::Horizontal, i18n( "Legend" ), scroll->viewport() );
02645     myLegend->setBackgroundColor( Qt::white );
02646     myLegend->setFont( font() );
02647     scroll->addChild(  myLegend );
02648     scroll->setResizePolicy( QScrollView::AutoOneFit );
02649     myLegend->layout()->setMargin( 11 );
02650     myLegend->setFrameStyle( QFrame::NoFrame );
02651     if ( dock )
02652         scroll->setMaximumHeight( 32000 );
02653     else
02654         scroll->setMaximumHeight( legendSizeHint().height() );
02655 }
02656 
02657 
02658 void KDLegendWidget::addLegendItem( KDGanttViewItem::Shape shape, const QColor& shapeColor, const QString& text )
02659 {
02660     QLabel * temp;
02661     QPixmap p = KDGanttView::getPixmap( shape,  shapeColor, Qt::white, 10);
02662     QWidget *w = new QWidget( myLegend );
02663     w->setBackgroundColor( Qt::white );
02664     QHBoxLayout *lay = new QHBoxLayout( w ,0, 6);
02665     temp = new QLabel ( w );
02666     lay->addWidget( temp, 0, Qt:: AlignRight);
02667     temp->setPixmap(p);
02668     temp = new QLabel ( text, w );
02669     temp->setBackgroundColor( Qt::white );
02670     lay->addWidget( temp, 0, Qt:: AlignLeft);
02671     lay->addStretch();
02672     if ( dock )
02673         scroll->setMaximumHeight( 32000 );
02674     else
02675         scroll->setMaximumHeight( legendSizeHint().height() );
02676 }
02677 
02678 
02679 bool KDLegendWidget::isShown ( )
02680 {
02681     return !isMinimized();
02682 }
02683 
02684 
02685 KDListView::KDListView(QWidget* parent, KDGanttView* gantView):QListView (parent)
02686 {
02687     myGanttView = gantView;
02688     setAcceptDrops(true);
02689     new KDListViewWhatsThis(viewport(),this);
02690     setRootIsDecorated( true );
02691     setAllColumnsShowFocus( true );
02692     addColumn( i18n( "Task Name" ) );
02693     setSorting( -1 );
02694     //setVScrollBarMode (QScrollView::AlwaysOn );
02695     setHScrollBarMode (QScrollView::AlwaysOn );
02696     setDefaultRenameAction(QListView::Accept);
02697     setColumnWidthMode ( 0,Maximum  );
02698     _calendarMode = false;
02699     // QObject::connect(this, SIGNAL (  pressed ( QListViewItem * )) , this, SLOT( dragItem( QListViewItem *))) ;
02700 }
02701 
02702 
02703 void  KDListView::dragItem( QListViewItem *  )
02704 {
02705     // qDebug("drag ");
02706     // startDrag();
02707 }
02708 QString KDListView::getWhatsThisText(QPoint p)
02709 {
02710     KDGanttViewItem* item = ( KDGanttViewItem* ) itemAt( p );
02711     if ( item )
02712         return item->whatsThisText();
02713     return i18n( "No item Found" );
02714 }
02715 
02716 void  KDListView::setCalendarMode( bool mode )
02717 {
02718     _calendarMode = mode;
02719     // setRootIsDecorated ( ! mode );
02720 }
02721 
02722 void  KDListView::setOpen(QListViewItem * item, bool open )
02723 {
02724     if (! _calendarMode || ! open ) {
02725         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( false );
02726         QListView::setOpen ( item, open );
02727         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( true );
02728         return;
02729     }
02730     // we are in calendarmode
02731     // in calendarmode only items can be opened which have subitems which have subitems
02732 
02733     QListViewItem* temp;
02734     temp = item->firstChild();
02735     bool openItem = false;
02736     while (temp) {
02737         if ( (( KDGanttViewItem*)temp)->displaySubitemsAsGroup() ) {
02738             temp->setVisible( true );
02739             openItem = true;
02740         }
02741         else {
02742             temp->setVisible( false );
02743             //qDebug(" temp->setVisible( false );");
02744         }
02745         temp = temp->nextSibling();
02746     }
02747     if ( openItem ) {
02748         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( false );
02749         QListView::setOpen ( item, open );
02750         (( KDGanttViewItem*)item)->setCallListViewOnSetOpen( true );
02751     }
02752 }
02753 
02754 
02755 void  KDListView::contentsMouseDoubleClickEvent ( QMouseEvent * e )
02756 {
02757     QListView::contentsMouseDoubleClickEvent ( e );
02758     //if ( ! _calendarMode )
02759     // QListView::contentsMouseDoubleClickEvent ( e );
02760     // else
02761     {
02762 
02763         emit myGanttView->lvItemDoubleClicked ( (KDGanttViewItem*) itemAt(e->pos() ) );
02764         emit myGanttView->itemDoubleClicked ( (KDGanttViewItem*) itemAt(e->pos() ) );
02765     }
02766 
02767 }
02768 
02769 
02770 void  KDListView::drawToPainter ( QPainter * p )
02771 {
02772     drawContentsOffset ( p, 0, 0, 0, 0, contentsWidth(), contentsHeight() );
02773 }
02774 
02775 
02776 void KDListView::resizeEvent(QResizeEvent *)
02777 {
02778     triggerUpdate ();
02779 }
02780 void KDListView::dragEnterEvent ( QDragEnterEvent * e)
02781 {
02782     if ( !myGanttView->dropEnabled() ) {
02783         e->accept( false );
02784         return;
02785     }
02786     myGanttView->lvDragEnterEvent(e);
02787     //e->accept(KDGanttViewItemDrag::canDecode(e) );
02788 }
02789 
02790 void KDListView::dragMoveEvent ( QDragMoveEvent * e)
02791 {
02792     if ( !myGanttView->dropEnabled() ) {
02793         e->accept( false );
02794         return;
02795     }
02796     KDGanttViewItem* draggedItem = 0;
02797     KDGanttViewItem* gItem = (KDGanttViewItem*)itemAt( e->pos()) ;
02798     setCurrentItem( gItem );
02799     if (  e->source() == myGanttView )
02800         draggedItem = myGanttView->myCanvasView->lastClickedItem;
02801     // execute user defined dragMoveEvent handling
02802     if (myGanttView->lvDragMoveEvent ( e , draggedItem, gItem ) )
02803         return;
02804     if ( !KDGanttViewItemDrag::canDecode(e) ) {
02805         e->accept( false );
02806         return;
02807     }
02808     if ( e->source() == myGanttView && gItem ){
02809         // internal drag - do not allow to drag the item to a subitem of itself
02810         KDGanttViewItem* pItem = gItem->parent();
02811         while ( pItem ) {
02812             if ( pItem == myGanttView->myCanvasView->lastClickedItem ) {
02813                 e->accept( false );
02814                 return;
02815             }
02816             pItem = pItem->parent();
02817         }
02818         if ( gItem == myGanttView->myCanvasView->lastClickedItem ) {
02819             e->accept( false );
02820             return;
02821         }
02822     }
02823     e->accept( true );
02824 }
02825 
02826 void KDListView::dragLeaveEvent ( QDragLeaveEvent * )
02827 {
02828     //qDebug("contentsDragLeaveEvent ");
02829 }
02830 void KDListView::dropEvent ( QDropEvent *e )
02831 {
02832     if ( !myGanttView->dropEnabled() ) {
02833         e->accept( false );
02834         return;
02835     }
02836     KDGanttViewItem* gItem = (KDGanttViewItem*)itemAt( e->pos()) ;
02837     KDGanttViewItem* draggedItem = 0;
02838     if (  e->source() == myGanttView )
02839         draggedItem = myGanttView->myCanvasView->lastClickedItem;
02840     if (myGanttView->lvDropEvent ( e, draggedItem, gItem ))
02841         return;
02842     QString string;
02843     KDGanttViewItemDrag::decode( e, string );
02844     KDGanttViewItem* newItem = 0;
02845 
02846     if ( gItem == myGanttView->myCanvasView->lastClickedItem && gItem != 0 ) {
02847         qDebug("KDGanttView::Possible bug in drag&drop code ");
02848         return;
02849     }
02850 
02851     QDomDocument doc( "GanttView" );
02852     doc.setContent( string );
02853     QDomElement docRoot = doc.documentElement(); // ChartParams element
02854     QDomNode node = docRoot.firstChild();
02855     bool enable = myGanttView->myTimeTable->blockUpdating( );
02856     myGanttView->myTimeTable->setBlockUpdating( true );
02857     while( !node.isNull() ) {
02858         QDomElement element = node.toElement();
02859         if( !element.isNull() ) { // was really an element
02860             QString tagName = element.tagName();
02861             if( tagName == "Items" ) {
02862                 QDomNode node = element.firstChild();
02863                 while( !node.isNull() ) {
02864                     QDomElement element = node.toElement();
02865                     if( !element.isNull() ) { // was really an element
02866                         QString tagName = element.tagName();
02867                         if( tagName == "Item" ) {
02868                             if (  gItem )
02869                                 newItem = KDGanttViewItem::createFromDomElement( gItem,
02870                                                                                  element );
02871                             else
02872                                 newItem = KDGanttViewItem::createFromDomElement( myGanttView,
02873                                                                                  element );
02874                         } else {
02875                             qDebug( "Unrecognized tag name: %s", tagName.latin1() );
02876                             Q_ASSERT( false );
02877                         }
02878                     }
02879                     //qDebug("next node1 ");
02880                     node = node.nextSibling();
02881                 }
02882             }
02883         }
02884         //qDebug("next node2 ");
02885         node = node.nextSibling();
02886     }
02887     newItem->setDisplaySubitemsAsGroup(myGanttView->displaySubitemsAsGroup());
02888     newItem->resetSubitemVisibility();
02889     myGanttView->slot_lvDropped(e, draggedItem, gItem);
02890     myGanttView->myTimeTable->setBlockUpdating( enable );
02891     myGanttView->myTimeTable->updateMyContent();
02892     return;
02893 }
02894 
02895 QDragObject * KDListView::dragObject ()
02896 {
02897     return QListView::dragObject ();
02898 }
02899 
02900 void KDListView::startDrag ()
02901 {
02902     if ( ! myGanttView->dragEnabled() )
02903         return;
02904     KDGanttViewItem* cItem = (KDGanttViewItem*) currentItem ();
02905     myGanttView->myCanvasView->lastClickedItem = cItem;
02906     myGanttView->lvStartDrag (cItem);
02907 }
02908 
02909 KDCanvasText::KDCanvasText( KDTimeTableWidget* canvas,
02910                             void* parentItem,
02911                             int type ) :
02912     QCanvasText(canvas)
02913 {
02914     myParentType = type;
02915     myParentItem = parentItem;
02916 }
02917 
02918 
02919 KDCanvasLine::KDCanvasLine( KDTimeTableWidget* canvas,
02920                             void* parentItem,
02921                             int type ) :
02922     QCanvasLine(canvas)
02923 {
02924     myParentType = type;
02925     myParentItem = parentItem;
02926 }
02927 
02928 
02929 KDCanvasPolygonItem::KDCanvasPolygonItem( KDTimeTableWidget* canvas,
02930                                           void* parentItem,
02931                                           int type ) :
02932     QCanvasPolygonalItem( canvas )
02933 {
02934     myParentType = type;
02935     myParentItem = parentItem;
02936 }
02937 
02938 
02939 KDCanvasPolygon::KDCanvasPolygon( KDTimeTableWidget* canvas,
02940                                   void* parentItem,
02941                                   int type ) :
02942     QCanvasPolygon( canvas )
02943 {
02944     myParentType = type;
02945     myParentItem = parentItem;
02946 }
02947 
02948 
02949 KDCanvasEllipse::KDCanvasEllipse( KDTimeTableWidget* canvas,
02950                                   void* parentItem,
02951                                   int type ) :
02952     QCanvasEllipse( canvas )
02953 {
02954     myParentType = type;
02955     myParentItem = parentItem;
02956 }
02957 
02958 
02959 KDCanvasRectangle::KDCanvasRectangle( KDTimeTableWidget* canvas,
02960                                       void* parentItem,
02961                                       int type ) :
02962     QCanvasRectangle( canvas )
02963 {
02964     myParentType = type;
02965     myParentItem = parentItem;
02966 }
02967 
02968 
02969 
02970 
02971 KDGanttCanvasView::KDGanttCanvasView( KDGanttView* sender,QCanvas* canvas, QWidget* parent,  const char* name ) : QCanvasView ( canvas, parent, name )
02972 {
02973     setHScrollBarMode (QScrollView::AlwaysOn );
02974     setVScrollBarMode( QScrollView::AlwaysOn );
02975     myToolTip = new KDCanvasToolTip(viewport(),this);
02976     mySignalSender =  sender;
02977     currentItem = 0;
02978     currentLink = 0;
02979     cuttedItem = 0;
02980     fromItem = 0;
02981     fromArea = 0;
02982     linkItemsEnabled = false;
02983     linkLine = new QCanvasLine(canvas);
02984     linkLine->hide();
02985     linkLine->setZ(1000);
02986     //set_Mouse_Tracking(true);
02987     new KDCanvasWhatsThis(viewport(),this);
02988     onItem = new QPopupMenu( this );
02989     QPopupMenu * newMenu = new QPopupMenu( this );
02990     QPopupMenu * onView = new QPopupMenu( this );
02991     onView->insertItem( i18n( "Summary" ), this,
02992                         SLOT ( newRootItem( int ) ), 0, 0 );
02993     onView->insertItem( i18n( "Event" ), this,
02994                         SLOT ( newRootItem( int ) ), 0, 1);
02995     onView->insertItem( i18n( "Task" ), this,
02996                         SLOT ( newRootItem( int ) ), 0, 2 );
02997 
02998     onItem->insertItem( i18n( "New Root" ), onView );
02999     newMenu->insertItem( i18n( "Summary" ),
03000                          this, SLOT ( newChildItem(  int) ), 0, 0 );
03001     newMenu->insertItem( i18n( "Event" ),
03002                          this, SLOT ( newChildItem( int ) ), 0, 1 );
03003     newMenu->insertItem( i18n( "Task" ),
03004                          this, SLOT ( newChildItem( int ) ), 0, 2 );
03005 
03006     onItem->insertItem( i18n( "New Child" ), newMenu );
03007     QPopupMenu * afterMenu = new QPopupMenu( this );
03008     afterMenu->insertItem( i18n( "Summary" ),
03009                            this, SLOT ( newChildItem(  int) ), 0, 0+4 );
03010     afterMenu->insertItem( i18n( "Event" ),
03011                            this, SLOT ( newChildItem( int ) ), 0, 1+4 );
03012     afterMenu->insertItem( i18n( "Task" ),
03013                            this, SLOT ( newChildItem( int ) ), 0, 2+4 );
03014     onItem->insertItem( i18n( "New After" ), afterMenu );
03015     QPopupMenu *pasteMenu = new QPopupMenu( this );
03016     pasteMenu->insertItem( i18n( "As Root" ),
03017                            this, SLOT ( pasteItem( int ) ), 0, 0 );
03018     pasteMenu->insertItem( i18n( "As Child" ),
03019                            this, SLOT ( pasteItem( int ) ), 0, 1 );
03020     pasteMenu->insertItem( i18n( "After" ),
03021                            this, SLOT ( pasteItem( int ) ), 0, 2 );
03022     onItem->insertItem( i18n( "Paste" ), pasteMenu, 3 );
03023     onItem->insertItem( i18n( "Cut Item" ), this, SLOT ( cutItem() ) );
03024     onItem->setItemEnabled( 3, false );
03025     myMyContentsHeight = 0;
03026     _showItemAddPopupMenu = false;
03027     
03028     myScrollTimer = new QTimer( this );
03029     connect( myScrollTimer, SIGNAL( timeout() ), SLOT( slotScrollTimer() ) );
03030     autoScrollEnabled = false;
03031 }
03032 
03033 
03034 KDGanttCanvasView::~KDGanttCanvasView()
03035 {
03036     delete myToolTip;
03037 }
03038 
03039 
03040 void KDGanttCanvasView::setShowPopupMenu( bool show )
03041 {
03042     _showItemAddPopupMenu = show;
03043 }
03044 bool KDGanttCanvasView::showPopupMenu()
03045 {
03046     return _showItemAddPopupMenu;
03047 }
03048 
03049 
03050 void KDGanttCanvasView::moveMyContent( int, int y)
03051 {
03052     setContentsPos(contentsX(), y);
03053 }
03054 
03055 void KDGanttCanvasView::resizeEvent ( QResizeEvent * e )
03056 {
03057     int ho = e->oldSize().height();
03058     int wo = e->oldSize().width();
03059     int hi = height();
03060     int wi = width();
03061     //QScrollView::blockSignals( true );
03062 
03063     verticalScrollBar()->setUpdatesEnabled( false );
03064     QScrollView::resizeEvent ( e ) ;
03065     if ( ho != hi )
03066         emit heightResized( viewport()->height());
03067     if ( wo != wi )
03068         emit widthResized( viewport()->width() );
03069     setMyContentsHeight( 0 );
03070     //QScrollView::blockSignals( false );
03071 }
03072 void KDGanttCanvasView::setMyContentsHeight( int hei )
03073 {
03074     mySignalSender->closingBlocked = true;
03075     qApp->processEvents();
03076     mySignalSender->closingBlocked = false;
03077     //qDebug("setMyContentsHeight %d %d ", hei,  myMyContentsHeight);
03078     if ( hei > 0 )
03079         myMyContentsHeight = hei;
03080     verticalScrollBar()->setUpdatesEnabled( true );
03081     if ( viewport()->height() <= myMyContentsHeight )
03082         verticalScrollBar()->setRange( 0, myMyContentsHeight- viewport()->height()+1);
03083     else
03084         verticalScrollBar()->setRange( 0,0 );
03085     // testing for unmatching ScrollBar values of timeheader and timetable
03086     // may happen after external resizing
03087     if ( horizontalScrollBar()->value() != mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->value() ) {
03088         // I am the Boss!
03089         mySignalSender->myTimeHeaderScroll->horizontalScrollBar()->setValue(horizontalScrollBar()->value()  );
03090 
03091     }
03092 
03093 }
03094 void  KDGanttCanvasView::cutItem( KDGanttViewItem* item )
03095 {
03096     lastClickedItem = item;
03097     cutItem();
03098 }
03099 void  KDGanttCanvasView::insertItemAsRoot( KDGanttViewItem* item )
03100 {
03101     mySignalSender->myListView->insertItem( item  );
03102     if ( item == cuttedItem )
03103         cuttedItem = 0;
03104 }
03105 void  KDGanttCanvasView::insertItemAsChild( KDGanttViewItem* parent, KDGanttViewItem* item )
03106 {
03107     parent->insertItem( cuttedItem );
03108     if ( item == cuttedItem )
03109         cuttedItem = 0;
03110 }
03111 void  KDGanttCanvasView::insertItemAfter( KDGanttViewItem* parent , KDGanttViewItem* item )
03112 {
03113     if ( parent->parent() ) {
03114         parent->parent()->insertItem( item );
03115     }
03116     else
03117         mySignalSender->myListView->insertItem( item );
03118     item->moveItem( parent );
03119     if ( item == cuttedItem )
03120         cuttedItem = 0;
03121 }
03122 
03123 void  KDGanttCanvasView::cutItem()
03124 {
03125     lastClickedItem->hideSubtree();
03126     //qDebug("last clicked %d parent %d ", lastClickedItem  , lastClickedItem->parent());
03127     if ( lastClickedItem->parent() )
03128         lastClickedItem->parent()->takeItem(lastClickedItem);
03129     else
03130         mySignalSender->myListView->takeItem( lastClickedItem );
03131     mySignalSender->myTimeTable->updateMyContent();
03132     if ( cuttedItem )
03133         delete cuttedItem;
03134     cuttedItem = lastClickedItem;
03135     onItem->setItemEnabled( 3, true );
03136 
03137 }
03138 // called from the destructor in KDGanttViewItem or KDGanttView
03139 
03140 void  KDGanttCanvasView::resetCutPaste( KDGanttViewItem* item )
03141 {
03142     if ( item == 0 && cuttedItem ) {
03143         delete cuttedItem;
03144         cuttedItem = 0;
03145     }
03146     if (item == cuttedItem) {
03147         onItem->setItemEnabled( 3, false );
03148         cuttedItem = 0;
03149     }
03150 }
03151 
03152 void  KDGanttCanvasView::pasteItem( int type )
03153 {
03154     if ( !cuttedItem )
03155         return;
03156     switch( type ) {
03157     case 0://root
03158         mySignalSender->myListView->insertItem( cuttedItem );
03159         break;
03160     case 1://child
03161         lastClickedItem->insertItem( cuttedItem );
03162         break;
03163     case 2://after
03164         if ( lastClickedItem->parent() ) {
03165             lastClickedItem->parent()->insertItem( cuttedItem );
03166         }
03167         else
03168             mySignalSender->myListView->insertItem( cuttedItem );
03169         cuttedItem->moveItem( lastClickedItem );
03170         break;
03171     default:
03172         ;
03173     }
03174     cuttedItem = 0;
03175     onItem->setItemEnabled( 3, false );
03176     mySignalSender->myTimeTable->updateMyContent();
03177 }
03178 void  KDGanttCanvasView::newRootItem(int type)
03179 {
03180     KDGanttViewItem* temp = 0;
03181     switch( type ) {
03182     case 1:
03183         temp = new KDGanttViewEventItem( mySignalSender, i18n( "New Event" ) );
03184         break;
03185     case 0:
03186         temp = new KDGanttViewSummaryItem( mySignalSender, i18n( "New Summary" ) );
03187         break;
03188     case 2:
03189         temp = new KDGanttViewTaskItem( mySignalSender, i18n( "New Task" ) );
03190         break;
03191     default:
03192         ;
03193     }
03194     if ( temp )
03195         mySignalSender->editItem( temp );
03196 }
03197 
03198 void  KDGanttCanvasView::newChildItem( int type )
03199 {
03200     KDGanttViewItem* temp = 0;
03201     switch( type ) {
03202     case 1:
03203         temp = new KDGanttViewEventItem( lastClickedItem, i18n( "New Event" ) );
03204         break;
03205     case 0:
03206         temp = new KDGanttViewSummaryItem( lastClickedItem, i18n( "New Summary" ) );
03207         break;
03208     case 2:
03209         temp = new KDGanttViewTaskItem( lastClickedItem, i18n( "New Task" ) );
03210         break;
03211     case 5:
03212         if ( lastClickedItem->parent() )
03213             temp = new KDGanttViewEventItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Event" ) );
03214         else
03215             temp = new KDGanttViewEventItem( mySignalSender, lastClickedItem, i18n( "New Event" ) );
03216         break;
03217     case 4:
03218         if ( lastClickedItem->parent() )
03219             temp = new KDGanttViewSummaryItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Summary" ) );
03220         else
03221             temp = new KDGanttViewSummaryItem( mySignalSender, lastClickedItem, i18n( "New Summary" ) );
03222         break;
03223     case 6:
03224         if ( lastClickedItem->parent() )
03225             temp = new KDGanttViewTaskItem( lastClickedItem->parent(), lastClickedItem, i18n( "New Task" ) );
03226         else
03227             temp = new KDGanttViewTaskItem( mySignalSender, lastClickedItem, i18n( "New Task" ) );
03228         break;
03229 
03230 
03231     default:
03232         ;
03233     }
03234     if ( temp )
03235         mySignalSender->editItem( temp );
03236 }
03237 
03238 void  KDGanttCanvasView::drawToPainter ( QPainter * p )
03239 {
03240     drawContents ( p, 0, 0, canvas()->width(), canvas()->height() );
03241 }
03242 QString  KDGanttCanvasView::getToolTipText(QPoint p)
03243 {
03244     QCanvasItemList il = canvas()->collisions ( viewportToContents( p ));
03245     QCanvasItemList::Iterator it;
03246     for ( it = il.begin(); it != il.end(); ++it ) {
03247         switch (getType(*it)) {
03248         case Type_is_KDGanttViewItem:
03249             return (getItem(*it))->tooltipText();
03250             break;
03251         case Type_is_KDGanttTaskLink:
03252             return (getLink(*it))->tooltipText();
03253             break;
03254         default:
03255             break;
03256         }
03257     }
03258     return "";
03259 }
03260 
03261 QString  KDGanttCanvasView::getWhatsThisText(QPoint p)
03262 {
03263     QCanvasItemList il = canvas() ->collisions (viewportToContents( p ));
03264     QCanvasItemList::Iterator it;
03265     for ( it = il.begin(); it != il.end(); ++it ) {
03266         switch (getType(*it)) {
03267         case Type_is_KDGanttViewItem:
03268             return (getItem(*it))->whatsThisText();
03269             break;
03270         case Type_is_KDGanttTaskLink:
03271             return (getLink(*it))->whatsThisText();
03272             break;
03273         default:
03274             break;
03275         }
03276     }
03277     return "";
03278 }
03279 
03280 
03288 void KDGanttCanvasView::contentsMousePressEvent ( QMouseEvent * e )
03289 {
03290     //qDebug("mousepress! %d ", this);
03291     //qDebug("focus %d ",qApp->focusWidget());
03292     setFocus();
03293     currentLink = 0;
03294     currentItem = 0;
03295     if (e->button() == RightButton && mySignalSender->editable()) {
03296         lastClickedItem = (KDGanttViewItem*) mySignalSender->myListView->itemAt( QPoint(2,e->pos().y()));
03297         if ( lastClickedItem ) {
03298             if ( lastClickedItem->displaySubitemsAsGroup() && ! lastClickedItem->isOpen() ) {
03299                 // findSub subitem
03300                 QCanvasItemList il = canvas() ->collisions ( e->pos() );
03301                 QCanvasItemList::Iterator it;
03302                 for ( it = il.begin(); it != il.end(); ++it ) {
03303                     if ( getType(*it) == Type_is_KDGanttViewItem ) {
03304                         lastClickedItem = getItem(*it);
03305                     }
03306                 }
03307             }
03308             if ( _showItemAddPopupMenu  )
03309                 onItem->popup(e->globalPos());
03310         }
03311     }
03312     QCanvasItemList il = canvas() ->collisions ( e->pos() );
03313     QCanvasItemList::Iterator it;
03314     for ( it = il.begin(); it != il.end(); ++it ) {
03315         switch ( e->button() ) {
03316         case LeftButton:
03317             switch (getType(*it)) {
03318             case Type_is_KDGanttViewItem:
03319                 currentItem = getItem(*it);
03320                 if (! currentItem->enabled() ) {
03321                     currentItem = 0;
03322                 } else if (linkItemsEnabled && 
03323                            !currentItem->isMyTextCanvas(*it)) {
03324                     fromArea = getItemArea(currentItem, e->pos().x());
03325                     if (fromArea > 0) {
03326                         fromItem = currentItem;
03327                         linkLine->setPoints(e->pos().x(), e->pos().y(), e->pos().x(), e->pos().y());
03328                         linkLine->show();
03329                     }
03330                 }
03331                 break;
03332             case Type_is_KDGanttTaskLink:
03333                 currentLink = getLink(*it);
03334                 break;
03335             default:
03336                 break;
03337             }
03338             break;
03339         case RightButton:
03340             switch (getType(*it)) {
03341             case Type_is_KDGanttViewItem:
03342                 currentItem = getItem(*it);
03343                 if (! currentItem->enabled() )
03344                     currentItem = 0;
03345                 break;
03346             case Type_is_KDGanttTaskLink:
03347                 currentLink = getLink(*it);
03348                 break;
03349             }
03350             break;
03351         case MidButton:
03352             switch (getType(*it)) {
03353             case Type_is_KDGanttViewItem:
03354                 currentItem = getItem(*it);
03355                 if (! currentItem->enabled() )
03356                     currentItem = 0;
03357                 break;
03358             case Type_is_KDGanttTaskLink:
03359                 currentLink = getLink(*it);
03360                 break;
03361             }
03362             break;
03363         default:
03364             break;
03365         }
03366     }
03367     if (e->button() == RightButton ) {
03368         mySignalSender->gvContextMenuRequested( currentItem, e->globalPos() );
03369     }
03370     if (autoScrollEnabled && e->button() == LeftButton) {
03371         myScrollTimer->start(50);
03372     }
03373 }
03381 void KDGanttCanvasView::contentsMouseReleaseEvent ( QMouseEvent * e )
03382 {
03383     static KDGanttViewItem* lastClicked = 0;
03384     mySignalSender->gvMouseButtonClicked( e->button(), currentItem ,  e->globalPos() );
03385     //qDebug("datetime %s ",mySignalSender->getDateTimeForCoordX(e->globalPos().x(), true ).toString().latin1() );
03386     //qDebug("mousepos %d %d ",e->pos().x(),e->pos().y() );
03387     //qDebug("mouseup ");
03388     // if ( currentLink || currentItem )
03389     {
03390         switch ( e->button() ) {
03391         case LeftButton:
03392             myScrollTimer->stop();
03393             {
03394                 mySignalSender->itemLeftClicked( currentItem );
03395                 mySignalSender->gvItemLeftClicked( currentItem );
03396             }
03397             if ( currentLink )
03398                 mySignalSender->taskLinkLeftClicked( currentLink );
03399             if (linkItemsEnabled && fromItem) {
03400                 linkLine->hide();
03401                 canvas()->update();
03402                 QCanvasItemList il = canvas() ->collisions ( e->pos() );
03403                 QCanvasItemList::Iterator it;
03404                 for ( it = il.begin(); it != il.end(); ++it ) {
03405                     if (getType(*it) == Type_is_KDGanttViewItem) {
03406                         KDGanttViewItem *toItem = getItem(*it);
03407                         if (!toItem->isMyTextCanvas(*it)) {
03408                             int toArea = getItemArea(toItem, e->pos().x());
03409                             if (toArea > 0 && toItem && fromItem != toItem) {
03410                                 mySignalSender->linkItems(fromItem, toItem, getLinkType(fromArea, toArea));
03411                             }
03412                         }
03413                         break;
03414                     }
03415                 }
03416             }
03417             fromItem = 0;
03418             break;
03419         case RightButton:
03420             {
03421                 mySignalSender->itemRightClicked( currentItem );
03422                 mySignalSender->gvItemRightClicked( currentItem );
03423 
03424             }
03425             if ( currentLink )
03426                 mySignalSender->taskLinkRightClicked( currentLink );
03427             break;
03428         case MidButton:
03429             {
03430                 mySignalSender->itemMidClicked( currentItem );
03431                 mySignalSender->gvItemMidClicked( currentItem );
03432             }
03433             if ( currentLink )
03434                 mySignalSender->taskLinkRightClicked( currentLink );
03435             break;
03436         default:
03437             break;
03438         }
03439     }
03440     if ( lastClicked != currentItem )
03441         mySignalSender->gvCurrentChanged( currentItem );
03442     lastClicked = currentItem;
03443     currentLink = 0;
03444     currentItem = 0;
03445 }
03453 void KDGanttCanvasView::contentsMouseDoubleClickEvent ( QMouseEvent * e )
03454 {
03455     QCanvasItemList il = canvas() ->collisions ( e->pos() );
03456     QCanvasItemList::Iterator it;
03457     for ( it = il.begin(); it != il.end(); ++it ) {
03458         switch ( e->button() ) {
03459         case LeftButton:
03460             switch (getType(*it)) {
03461             case Type_is_KDGanttViewItem:
03462                 if ( getItem(*it)->enabled() )
03463                     mySignalSender->itemDoubleClicked(getItem(*it));
03464                 mySignalSender->gvItemDoubleClicked(getItem(*it));
03465                 return;
03466                 break;
03467             case Type_is_KDGanttTaskLink:
03468                 mySignalSender->taskLinkDoubleClicked(getLink(*it));
03469                 return;
03470                 break;
03471             default:
03472                 break;
03473             }
03474             break;
03475             /*
03476               case RightButton:
03477               switch (getType(*it)) {
03478               case Type_is_KDGanttViewItem:
03479               mySignalSender->itemRightClicked(getItem(*it));
03480               return;
03481               break;
03482               case Type_is_KDGanttTaskLink:
03483               mySignalSender->taskLinkRightClicked(getLink(*it));
03484               return;
03485               break;
03486               }
03487               break;
03488               case MidButton:
03489               switch (getType(*it)) {
03490               case Type_is_KDGanttViewItem:
03491               mySignalSender->itemMidClicked(getItem(*it));
03492               return;
03493               break;
03494               case Type_is_KDGanttTaskLink:
03495               mySignalSender->taskLinkMidClicked(getLink(*it));
03496               return;
03497               break;
03498               }
03499               break;
03500             */
03501         default:
03502             break;
03503         }
03504     }
03505 }
03513 void KDGanttCanvasView::contentsMouseMoveEvent ( QMouseEvent *e )
03514 {
03515     //qDebug("mousemove! ");
03516     static int moves = 0;
03517     if ( (currentLink || currentItem) && (moves < 3) ) {
03518         ++moves;
03519 
03520     } else {
03521         moves = 0;
03522         currentLink = 0;
03523         currentItem = 0;
03524     }
03525     if (autoScrollEnabled)
03526         mousePos = e->pos()- QPoint(contentsX(),contentsY()); // make mousePos relative 0
03527     if (fromItem) {
03528         //qDebug("mousemove: linking %s: %d,%d ",fromItem->listViewText().latin1(), e->pos().x(), e->pos().y());
03529         linkLine->setPoints(linkLine->startPoint().x(), linkLine->startPoint().y(), e->pos().x(), e->pos().y());
03530         canvas()->update();
03531     }
03532     // no action implemented
03533     //qDebug("mousemove ");
03534     //QToolTip::setGloballyEnabled (false);
03535     //QToolTip::remove(viewport());
03536     // QToolTip::add(viewport(), "hello");
03537     // QToolTip::setGloballyEnabled (true);
03538     /*
03539       QCanvasItemList il = canvas() ->collisions ( e->pos() );
03540 
03541       QCanvasItemList::Iterator it;
03542       KDGanttItem* mouseover = 0;
03543       for ( it = il.begin(); it != il.end(); ++it ) {
03544 
03545       }
03546 
03547     */
03548 }
03549 void KDGanttCanvasView::viewportPaintEvent ( QPaintEvent * pe )
03550 {
03551     QCanvasView::viewportPaintEvent ( pe );
03552 }
03553 void KDGanttCanvasView::set_Mouse_Tracking(bool on)
03554 {
03555     viewport()->setMouseTracking(on);
03556 }
03557 int  KDGanttCanvasView::getType(QCanvasItem* it)
03558 {
03559     switch (it->rtti()) {
03560     case QCanvasItem::Rtti_Line: return ((KDCanvasLine*)it)->myParentType;
03561     case QCanvasItem::Rtti_Ellipse: return ((KDCanvasEllipse *)it)->myParentType;
03562     case QCanvasItem::Rtti_Text: return ((KDCanvasText *)it)->myParentType;
03563     case QCanvasItem::Rtti_Polygon: return ((KDCanvasPolygon *)it)->myParentType;
03564     case QCanvasItem::Rtti_Rectangle: return ((KDCanvasRectangle *)it)->myParentType;
03565     }
03566     return -1;
03567 }
03568 KDGanttViewItem*  KDGanttCanvasView::getItem(QCanvasItem* it)
03569 {
03570     switch (it->rtti()) {
03571     case QCanvasItem::Rtti_Line: return (KDGanttViewItem*)  ((KDCanvasLine*)it)->myParentItem;
03572     case QCanvasItem::Rtti_Ellipse: return (KDGanttViewItem*)  ((KDCanvasEllipse *)it)->myParentItem;
03573     case QCanvasItem::Rtti_Text: return (KDGanttViewItem*) ((KDCanvasText *)it)->myParentItem;
03574     case QCanvasItem::Rtti_Polygon: return (KDGanttViewItem*) ((KDCanvasPolygon *)it)->myParentItem;
03575     case QCanvasItem::Rtti_Rectangle: return (KDGanttViewItem*) ((KDCanvasRectangle *)it)->myParentItem;
03576 
03577     }
03578     return 0;
03579 }
03580 KDGanttViewTaskLink*  KDGanttCanvasView::getLink(QCanvasItem* it)
03581 {
03582     switch (it->rtti()) {
03583     case QCanvasItem::Rtti_Line: return (KDGanttViewTaskLink*)  ((KDCanvasLine*)it)->myParentItem;
03584     case QCanvasItem::Rtti_Ellipse: return (KDGanttViewTaskLink*)  ((KDCanvasEllipse *)it)->myParentItem;
03585     case QCanvasItem::Rtti_Text: return (KDGanttViewTaskLink*) ((KDCanvasText *)it)->myParentItem;
03586     case QCanvasItem::Rtti_Polygon: return (KDGanttViewTaskLink*) ((KDCanvasPolygon *)it)->myParentItem;
03587     }
03588     return 0;
03589 }
03590 
03591 void KDGanttCanvasView::slotScrollTimer() {
03592     int mx = mousePos.x(); 
03593     int my = mousePos.y();
03594     int dx = 0;
03595     int dy = 0;
03596     if (mx < 0)
03597         dx = -5;
03598     else if (mx > visibleWidth())
03599         dx = 5;
03600     if (my < 0)
03601         dy = -5;
03602     else if (my > visibleHeight())
03603         dy = 5;
03604     if (dx != 0 || dy != 0)
03605         scrollBy(dx, dy);
03606 }
03607 
03608 int KDGanttCanvasView::getItemArea(KDGanttViewItem *item, int x) {
03609     // area can be: no area = 0, Start = 1, Finish = 2
03610     // TODO: middle (move, dnd), front, back (resize)
03611     KDTimeTableWidget *tt = dynamic_cast<KDTimeTableWidget *>(canvas());
03612     if (!tt) {
03613         qWarning("Cannot cast canvas to KDTimeTableWidget");
03614         return 0;
03615     }
03616     int area = 0;
03617     int start = tt->getCoordX(item->startTime());
03618     int end = start;
03619     if (item->type() == KDGanttViewItem::Event) {
03620         x > start ? area = 2 : area = 1;
03621     } else {
03622         end = tt->getCoordX(item->endTime());
03623         if ((end - start)/2 > (x - start))
03624             area = 1;
03625         else
03626             area = 2;
03627     }
03628     return area;
03629 }
03630 
03631 int KDGanttCanvasView::getLinkType(int from, int to) {
03632     // from, to should be Start = 1 or Finish = 2
03633     if ((from == 1) && (to == 1)) {
03634         return KDGanttViewTaskLink::StartStart;
03635     }
03636     if ((from == 1) && (to == 2)) {
03637         return KDGanttViewTaskLink::StartFinish;
03638     }
03639     if ((from == 2) && (to == 1)) {
03640         return KDGanttViewTaskLink::FinishStart;
03641     }
03642     if ((from == 2) && (to == 2)) {
03643         return KDGanttViewTaskLink::FinishFinish;
03644     }
03645     return KDGanttViewTaskLink::None;
03646 }
KDE Logo
This file is part of the documentation for kdgantt Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:42:27 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003