CalendarSupport

cellitem.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5*/
6
7#include <QSet>
8
9#include "cellitem.h"
10
11#include "calendarsupport_debug.h"
12#include <KLocalizedString>
13
14using namespace CalendarSupport;
15
16void CellItem::setSubCells(int v)
17{
18 mSubCells = v;
19}
20
21int CellItem::subCells() const
22{
23 return mSubCells;
24}
25
26void CellItem::setSubCell(int v)
27{
28 mSubCell = v;
29}
30
31int CellItem::subCell() const
32{
33 return mSubCell;
34}
35
36QString CellItem::label() const
37{
38 return xi18n("<placeholder>undefined</placeholder>");
39}
40
41QList<CellItem *> CellItem::placeItem(const QList<CellItem *> &cells, CellItem *placeItem)
42{
43 int maxSubCells = 0;
44 QSet<int> subCellsInUse;
45
46 // Find all items that overlap placeItem, the items that overlaps them, and so on.
47 QList<CellItem *> overlappingItems{placeItem};
48 for (int i = 0; i < overlappingItems.count(); i++) {
49 const auto checkItem = overlappingItems.at(i);
50 for (const auto item : cells) {
51 if (item->overlaps(checkItem) && !overlappingItems.contains(item)) {
52 qCDebug(CALENDARSUPPORT_LOG) << item->label() << "overlaps" << checkItem->label();
53 overlappingItems.append(item);
54 if (item->subCell() >= maxSubCells) {
55 maxSubCells = item->subCells();
56 }
57 if (checkItem == placeItem) {
58 subCellsInUse.insert(item->subCell());
59 }
60 }
61 }
62 }
63
64 if (overlappingItems.count() > 1) {
65 // Look for an unused subcell in placeItem's cells. If all are used,
66 // all overlapping items have to squeeze over.
67 int i;
68 for (i = 0; i < maxSubCells; ++i) {
69 if (!subCellsInUse.contains(i)) {
70 break;
71 }
72 }
73 placeItem->setSubCell(i);
74 if (i == maxSubCells) {
75 maxSubCells += 1;
76 for (auto item : overlappingItems) {
77 item->setSubCells(maxSubCells);
78 }
79 }
80 placeItem->setSubCells(maxSubCells);
81 qCDebug(CALENDARSUPPORT_LOG) << "use subcell" << i << "of" << maxSubCells;
82 } else {
83 // Nothing overlapped placeItem, so:
84 overlappingItems.clear();
85 placeItem->setSubCell(0);
86 placeItem->setSubCells(1);
87 }
88
89 return overlappingItems;
90}
QString xi18n(const char *text, const TYPE &arg...)
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.