File indexing completed on 2024-05-12 05:13:14

0001 /*
0002   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005 */
0006 
0007 #include <QSet>
0008 
0009 #include "cellitem.h"
0010 
0011 #include "calendarsupport_debug.h"
0012 #include <KLocalizedString>
0013 
0014 using namespace CalendarSupport;
0015 
0016 void CellItem::setSubCells(int v)
0017 {
0018     mSubCells = v;
0019 }
0020 
0021 int CellItem::subCells() const
0022 {
0023     return mSubCells;
0024 }
0025 
0026 void CellItem::setSubCell(int v)
0027 {
0028     mSubCell = v;
0029 }
0030 
0031 int CellItem::subCell() const
0032 {
0033     return mSubCell;
0034 }
0035 
0036 QString CellItem::label() const
0037 {
0038     return xi18n("<placeholder>undefined</placeholder>");
0039 }
0040 
0041 QList<CellItem *> CellItem::placeItem(const QList<CellItem *> &cells, CellItem *placeItem)
0042 {
0043     int maxSubCells = 0;
0044     QSet<int> subCellsInUse;
0045 
0046     // Find all items that overlap placeItem, the items that overlaps them, and so on.
0047     QList<CellItem *> overlappingItems{placeItem};
0048     for (int i = 0; i < overlappingItems.count(); i++) {
0049         const auto checkItem = overlappingItems.at(i);
0050         for (const auto item : cells) {
0051             if (item->overlaps(checkItem) && !overlappingItems.contains(item)) {
0052                 qCDebug(CALENDARSUPPORT_LOG) << item->label() << "overlaps" << checkItem->label();
0053                 overlappingItems.append(item);
0054                 if (item->subCell() >= maxSubCells) {
0055                     maxSubCells = item->subCells();
0056                 }
0057                 if (checkItem == placeItem) {
0058                     subCellsInUse.insert(item->subCell());
0059                 }
0060             }
0061         }
0062     }
0063 
0064     if (overlappingItems.count() > 1) {
0065         // Look for an unused subcell in placeItem's cells.  If all are used,
0066         // all overlapping items have to squeeze over.
0067         int i;
0068         for (i = 0; i < maxSubCells; ++i) {
0069             if (!subCellsInUse.contains(i)) {
0070                 break;
0071             }
0072         }
0073         placeItem->setSubCell(i);
0074         if (i == maxSubCells) {
0075             maxSubCells += 1;
0076             for (auto item : overlappingItems) {
0077                 item->setSubCells(maxSubCells);
0078             }
0079         }
0080         placeItem->setSubCells(maxSubCells);
0081         qCDebug(CALENDARSUPPORT_LOG) << "use subcell" << i << "of" << maxSubCells;
0082     } else {
0083         // Nothing overlapped placeItem, so:
0084         overlappingItems.clear();
0085         placeItem->setSubCell(0);
0086         placeItem->setSubCells(1);
0087     }
0088 
0089     return overlappingItems;
0090 }