korganizer

cellitem.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "cellitem.h"
00026 
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 
00030 #include <qintdict.h>
00031 
00032 using namespace KOrg;
00033 
00034 QString CellItem::label() const
00035 {
00036   return i18n("<undefined>");
00037 }
00038 
00039 QPtrList<CellItem> CellItem::placeItem( QPtrList<CellItem> cells,
00040                                         CellItem *placeItem )
00041 {
00042   kdDebug(5855) << "Placing " << placeItem->label() << endl;
00043 
00044   QPtrList<KOrg::CellItem> conflictItems;
00045   int maxSubCells = 0;
00046   QIntDict<KOrg::CellItem> subCellDict;
00047 
00048   // Find all items which are in same cell
00049   QPtrListIterator<KOrg::CellItem> it2( cells );
00050   for( it2.toFirst(); it2.current(); ++it2 ) {
00051     KOrg::CellItem *item = it2.current();
00052     if ( item == placeItem ) continue;
00053 
00054     if ( item->overlaps( placeItem ) ) {
00055       kdDebug(5855) << "  Overlaps: " << item->label() << endl;
00056 
00057       conflictItems.append( item );
00058       if ( item->subCells() > maxSubCells ) maxSubCells = item->subCells();
00059       subCellDict.insert( item->subCell(), item );
00060     }
00061   }
00062 
00063   if ( conflictItems.count() > 0 ) {
00064     // Look for unused sub cell and insert item
00065     int i;
00066     for( i = 0; i < maxSubCells; ++i ) {
00067       kdDebug(5855) << "  Trying subcell " << i << endl;
00068       if ( !subCellDict.find( i ) ) {
00069         kdDebug(5855) << "  Use subcell " << i << endl;
00070         placeItem->setSubCell( i );
00071         break;
00072       }
00073     }
00074     if ( i == maxSubCells ) {
00075       kdDebug(5855) << "  New subcell " << i << endl;
00076       placeItem->setSubCell( maxSubCells );
00077       maxSubCells++;  // add new item to number of sub cells
00078     }
00079 
00080     kdDebug(5855) << "  Sub cells: " << maxSubCells << endl;
00081 
00082     // Write results to item to be placed
00083     conflictItems.append( placeItem );
00084     placeItem->setSubCells( maxSubCells );
00085 
00086     QPtrListIterator<KOrg::CellItem> it3( conflictItems );
00087     for( it3.toFirst(); it3.current(); ++it3 ) {
00088       (*it3)->setSubCells( maxSubCells );
00089     }
00090     // Todo: Adapt subCells of items conflicting with conflicting items
00091   } else {
00092     kdDebug(5855) << "  no conflicts" << endl;
00093     placeItem->setSubCell( 0 );
00094     placeItem->setSubCells( 1 );
00095   }
00096   
00097   return conflictItems;
00098 }
KDE Home | KDE Accessibility Home | Description of Access Keys