1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "MarbleGraphicsGridLayout.h"

// Qt includes

#include <QHash>
#include <QRectF>
#include <QSizeF>
#include <QVector>

// Local includes

#include "ScreenGraphicsItem.h"
#include "digikam_debug.h"

namespace Marble
{

class Q_DECL_HIDDEN MarbleGraphicsGridLayoutPrivate
{
public:
    MarbleGraphicsGridLayoutPrivate(int rows, int columns)
        : m_rows(rows),
          m_columns(columns),
          m_spacing(0),
          m_alignment(Qt::AlignLeft | Qt::AlignTop)
    {
        m_items = new ScreenGraphicsItem** [rows];<--- Class 'MarbleGraphicsGridLayoutPrivate' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'MarbleGraphicsGridLayoutPrivate' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).

        for (int i = 0; i < rows; ++i)
        {
            m_items[i] = new ScreenGraphicsItem *[columns];
        }

        for (int row = 0; row < rows; row++)
        {
            for (int column = 0; column < columns; column++)
            {
                m_items[row][column] = nullptr;
            }
        }
    }

    ~MarbleGraphicsGridLayoutPrivate()
    {
        for (int i = 0; i < m_rows; ++i)
        {
            delete[] m_items[i];
        }

        delete[] m_items;
    }

    // A two dimensional array of pointers to ScreenGraphicsItems
    ScreenGraphicsItem*** m_items;
    int m_rows;
    int m_columns;
    int m_spacing;
    Qt::Alignment m_alignment;
    QHash<ScreenGraphicsItem*, Qt::Alignment> m_itemAlignment;
};

MarbleGraphicsGridLayout::MarbleGraphicsGridLayout(int rows, int columns)
    : d(new MarbleGraphicsGridLayoutPrivate(rows, columns))<--- Class 'MarbleGraphicsGridLayout' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'MarbleGraphicsGridLayout' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
{
}

MarbleGraphicsGridLayout::~MarbleGraphicsGridLayout()
{
    delete d;
}

void MarbleGraphicsGridLayout::addItem(ScreenGraphicsItem* item, int row, int column)
{
    if (row < d->m_rows
        && column < d->m_columns)
    {
        d->m_items[row][column] = item;
    }
}

void MarbleGraphicsGridLayout::updatePositions(MarbleGraphicsItem* parent)
{
    // Initialize with 0.0
    QVector<double> maxWidth(d->m_columns, 0.0);
    QVector<double> maxHeight(d->m_rows, 0.0);

    // Determining the cell sizes
    for (int row = 0; row < d->m_rows; row++)
    {
        for (int column = 0; column < d->m_columns; column++)
        {
            if (d->m_items[row][column] == nullptr)
            {
                continue;
            }

            QSizeF size = d->m_items[row][column]->size();
            double width = size.width();
            double height = size.height();

            if (width > maxWidth[column])
            {
                maxWidth[column] = width;
            }

            if (height > maxHeight[row])
            {
                maxHeight[row] = height;
            }
        }
    }

    QVector<double> startX(d->m_columns);
    QVector<double> endX(d->m_columns);
    QVector<double> startY(d->m_rows);
    QVector<double> endY(d->m_rows);
    QRectF contentRect = parent->contentRect();

    for (int i = 0; i < d->m_columns; i++)
    {
        if (i == 0)
        {
            startX[0] = contentRect.left();
        }

        else if (maxWidth[i] == 0)
        {
            startX[i] = endX[i - 1];
        }

        else
        {
            startX[i] = endX[i - 1] + d->m_spacing;
        }

        endX[i] = startX[i] + maxWidth[i];
    }

    for (int i = 0; i < d->m_rows; i++)
    {
        if (i == 0)
        {
            startY[0] = contentRect.left();
        }

        else if (maxHeight[i] == 0)
        {
            startY[i] = endY[i - 1];
        }

        else
        {
            startY[i] = endY[i - 1] + d->m_spacing;
        }

        endY[i] = startY[i] + maxHeight[i];
    }

    // Setting the positions
    for (int row = 0; row < d->m_rows; row++)
    {
        for (int column = 0; column < d->m_columns; column++)
        {
            if (d->m_items[row][column] == nullptr)
            {
                continue;
            }

            double xPos, yPos;

            Qt::Alignment align = alignment(d->m_items[row][column]);

            if (align & Qt::AlignRight)
            {
                xPos = endX[column] - d->m_items[row][column]->size().width();
            }

            else if (align & Qt::AlignHCenter)
            {
                xPos = startX[column]
                       + (maxWidth[column] - d->m_items[row][column]->size().width()) / 2.0;
            }

            else
            {
                xPos = startX[column];
            }

            if (align & Qt::AlignBottom)
            {
                yPos = endY[row] - d->m_items[row][column]->size().height();
            }

            else if (align & Qt::AlignVCenter)
            {
                yPos = startY[row]
                       + (maxHeight[row] - d->m_items[row][column]->size().height()) / 2.0;
            }

            else
            {
                yPos = startY[row];
            }

            d->m_items[row][column]->setPosition(QPointF(xPos, yPos));
        }
    }

    parent->setContentSize(QSizeF(endX[d->m_columns - 1] - contentRect.left(),
                                  endY[d->m_rows - 1] - contentRect.top()));
}

Qt::Alignment MarbleGraphicsGridLayout::alignment() const
{
    return d->m_alignment;
}

Qt::Alignment MarbleGraphicsGridLayout::alignment(ScreenGraphicsItem* item) const<--- Parameter 'item' can be declared as pointer to const
{
    return d->m_itemAlignment.value(item, d->m_alignment);
}

void MarbleGraphicsGridLayout::setAlignment(Qt::Alignment align)
{
    d->m_alignment = align;
}

void MarbleGraphicsGridLayout::setAlignment(ScreenGraphicsItem* item, Qt::Alignment align)<--- Parameter 'item' can be declared as pointer to const
{
    d->m_itemAlignment.insert(item, align);
}

int MarbleGraphicsGridLayout::spacing() const
{
    return d->m_spacing;
}

void MarbleGraphicsGridLayout::setSpacing(int spacing)
{
    d->m_spacing = spacing;
}

} // namespace Marble