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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "LegendLayout.h"
#include <cmath>
#include "Chart.h"
#include "ItemBuilder.h"
#include "datasource/ChartDataSource.h"
qreal sizeWithSpacing(int count, qreal size, qreal spacing)
{
return size * count + spacing * (count - 1);
}
LegendLayoutAttached::LegendLayoutAttached(QObject *parent)
: QObject(parent)
{
}
qreal LegendLayoutAttached::minimumWidth() const
{
return m_minimumWidth.value_or(0.0);
}
void LegendLayoutAttached::setMinimumWidth(qreal newMinimumWidth)
{
if (newMinimumWidth == m_minimumWidth) {
return;
}
m_minimumWidth = newMinimumWidth;
Q_EMIT minimumWidthChanged();
}
bool LegendLayoutAttached::isMinimumWidthValid() const
{
return m_minimumWidth.has_value();
}
qreal LegendLayoutAttached::preferredWidth() const
{
return m_preferredWidth.value_or(0.0);
}
void LegendLayoutAttached::setPreferredWidth(qreal newPreferredWidth)
{
if (newPreferredWidth == m_preferredWidth) {
return;
}
m_preferredWidth = newPreferredWidth;
Q_EMIT preferredWidthChanged();
}
bool LegendLayoutAttached::isPreferredWidthValid() const
{
return m_preferredWidth.has_value();
}
qreal LegendLayoutAttached::maximumWidth() const
{
return m_maximumWidth.value_or(0.0);
}
void LegendLayoutAttached::setMaximumWidth(qreal newMaximumWidth)
{
if (newMaximumWidth == m_maximumWidth) {
return;
}
m_maximumWidth = newMaximumWidth;
Q_EMIT maximumWidthChanged();
}
bool LegendLayoutAttached::isMaximumWidthValid() const
{
return m_maximumWidth.has_value();
}
LegendLayout::LegendLayout(QQuickItem *parent)
: QQuickItem(parent)
{
}
qreal LegendLayout::horizontalSpacing() const
{
return m_horizontalSpacing;
}
void LegendLayout::setHorizontalSpacing(qreal newHorizontalSpacing)
{
if (newHorizontalSpacing == m_horizontalSpacing) {
return;
}
m_horizontalSpacing = newHorizontalSpacing;
polish();
Q_EMIT horizontalSpacingChanged();
}
qreal LegendLayout::verticalSpacing() const
{
return m_verticalSpacing;
}
void LegendLayout::setVerticalSpacing(qreal newVerticalSpacing)
{
if (newVerticalSpacing == m_verticalSpacing) {
return;
}
m_verticalSpacing = newVerticalSpacing;
polish();
Q_EMIT verticalSpacingChanged();
}
qreal LegendLayout::preferredWidth() const
{
return m_preferredWidth;
}
void LegendLayout::componentComplete()
{
QQuickItem::componentComplete();
m_completed = true;
polish();
}
void LegendLayout::updatePolish()
{
if (!m_completed || !isVisible()) {
return;
}
int columns = 0;
int rows = 0;
qreal itemWidth = 0.0;
qreal itemHeight = 0.0;
qreal layoutWidth = width();
std::tie(columns, rows, itemWidth, itemHeight) = determineColumns();
auto column = 0;
auto row = 0;
const auto items = childItems();
for (auto item : items) {
if (!item->isVisible() || item->implicitWidth() <= 0 || item->implicitHeight() <= 0) {
continue;
}
auto attached = static_cast<LegendLayoutAttached *>(qmlAttachedPropertiesObject<LegendLayout>(item, true));
auto x = (itemWidth + m_horizontalSpacing) * column;
auto y = (itemHeight + m_verticalSpacing) * row;
item->setPosition(QPointF{x, y});
item->setWidth(std::clamp(itemWidth, attached->minimumWidth(), attached->maximumWidth()));
// If we are in single column mode, we are most likely width constrained.
// In that case, we should make sure items do not exceed our own width,
// so we can trigger things like text eliding.
if (layoutWidth > 0 && item->width() > layoutWidth && columns == 1) {
item->setWidth(layoutWidth);
}
column++;
if (column >= columns) {
row++;
column = 0;
}
}
setImplicitSize(sizeWithSpacing(columns, itemWidth, m_horizontalSpacing), sizeWithSpacing(rows, itemHeight, m_verticalSpacing));
}
void LegendLayout::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
if (newGeometry != oldGeometry) {
polish();
}
QQuickItem::geometryChange(newGeometry, oldGeometry);
}
void LegendLayout::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
{
if (change == QQuickItem::ItemVisibleHasChanged || change == QQuickItem::ItemSceneChange) {
polish();
}
if (change == QQuickItem::ItemChildAddedChange) {
auto item = data.item;
connect(item, &QQuickItem::implicitWidthChanged, this, &LegendLayout::polish);
connect(item, &QQuickItem::implicitHeightChanged, this, &LegendLayout::polish);
connect(item, &QQuickItem::visibleChanged, this, &LegendLayout::polish);
auto attached = static_cast<LegendLayoutAttached *>(qmlAttachedPropertiesObject<LegendLayout>(item, true));
connect(attached, &LegendLayoutAttached::minimumWidthChanged, this, &LegendLayout::polish);
connect(attached, &LegendLayoutAttached::preferredWidthChanged, this, &LegendLayout::polish);
connect(attached, &LegendLayoutAttached::maximumWidthChanged, this, &LegendLayout::polish);
polish();
}
if (change == QQuickItem::ItemChildRemovedChange) {
auto item = data.item;
item->disconnect(this);
auto attached = static_cast<LegendLayoutAttached *>(qmlAttachedPropertiesObject<LegendLayout>(item, false));
if (attached) {
attached->disconnect(this);
}
polish();
}
QQuickItem::itemChange(change, data);
}
// Determine how many columns and rows should be used for placing items and how
// large each item should be.
std::tuple<int, int, qreal, qreal> LegendLayout::determineColumns()
{
auto minWidth = -std::numeric_limits<qreal>::max();
auto preferredWidth = -std::numeric_limits<qreal>::max();
auto maxWidth = std::numeric_limits<qreal>::max();
auto maxHeight = -std::numeric_limits<qreal>::max();
const auto items = childItems();
// Keep track of actual visual and visible items, since childItems() also
// includes stuff like repeaters.
auto itemCount = 0;
// First, we determine the minimum, preferred and maximum width of all
// items. These are determined from the attached object, or implicitWidth
// for minimum size if minimumWidth has not been set.
//
// We also determine the maximum height of items so we do not need to do
// that later.
for (auto item : items) {
if (!item->isVisible() || item->implicitWidth() <= 0 || item->implicitHeight() <= 0) {
continue;
}
auto attached = static_cast<LegendLayoutAttached *>(qmlAttachedPropertiesObject<LegendLayout>(item, true));
if (attached->isMinimumWidthValid()) {
minWidth = std::max(minWidth, attached->minimumWidth());
} else {
minWidth = std::max(minWidth, item->implicitWidth());
}
if (attached->isPreferredWidthValid()) {
preferredWidth = std::max(preferredWidth, attached->preferredWidth());
}
if (attached->isMaximumWidthValid()) {
maxWidth = std::min(maxWidth, attached->maximumWidth());
}
maxHeight = std::max(maxHeight, item->implicitHeight());
itemCount++;
}
if (itemCount == 0) {
return std::make_tuple(0, 0, 0, 0);
}
auto availableWidth = width();
// Check if we have a valid width. If we cannot even fit a horizontalSpacing
// we cannot do anything with the width and most likely did not get a width
// assigned, so come up with some reasonable default width.
//
// For the default, layout everything in a full row, using either maxWidth
// for each item if we have it or minWidth if we do not.
if (availableWidth <= m_horizontalSpacing) {
if (maxWidth <= 0.0) {
availableWidth = sizeWithSpacing(itemCount, minWidth, m_horizontalSpacing);
} else {
availableWidth = sizeWithSpacing(itemCount, maxWidth, m_horizontalSpacing);
}
}
// If none of the items have a maximum width set, default to filling all
// available space.
if (maxWidth <= 0.0 || maxWidth >= std::numeric_limits<qreal>::max()) {
maxWidth = availableWidth;
}
// Ensure we don't try to size things below their minimum size.
if (maxWidth < minWidth) {
maxWidth = minWidth;
}
if (preferredWidth != m_preferredWidth) {
m_preferredWidth = preferredWidth;
Q_EMIT preferredWidthChanged();
}
auto columns = 1;
auto rows = itemCount;
bool fit = true;
// Calculate the actual number of rows and columns by trying to fit items
// until we find the right number.
while (true) {
auto minTotalWidth = sizeWithSpacing(columns, minWidth, m_horizontalSpacing);
auto maxTotalWidth = sizeWithSpacing(columns, maxWidth, m_horizontalSpacing);
// If the minimum width is less than our width, but the maximum is
// larger, we found a correct solution since we can resize the items to
// fit within the provided bounds.
if (minTotalWidth <= availableWidth && maxTotalWidth >= availableWidth) {
break;
}
// As long as we have more space available than the items' max size,
// decrease the number of rows and that way increase the number of
// columns we use to place items - unless that results in no rows, as
// that means we've reached a state where we simply have more space than
// needed.
if (maxTotalWidth < availableWidth) {
rows--;
if (rows >= 1) {
columns = std::ceil(itemCount / float(rows));
} else {
fit = false;
break;
}
}
// In certain cases, we hit a corner case where decreasing the number of
// rows leads to things ending up outside of the item's bounds. If that
// happens, increase the number of rows by one and exit the loop.
if (minTotalWidth > availableWidth) {
rows += 1;
columns = std::ceil(itemCount / float(rows));
break;
}
}
// Calculate item width based on the calculated number of columns.
// If it turns out we have more space than needed, use maxWidth
// instead to avoid awkward gaps.
auto itemWidth = fit ? (availableWidth - m_horizontalSpacing * (columns - 1)) / columns : maxWidth;
// Recalculate the number of rows, otherwise we may end up with "ghost" rows
// since the items wrapped into a new column, but no all of them.
rows = std::ceil(itemCount / float(columns));
return std::make_tuple(columns, rows, itemWidth, maxHeight);
}
#include "moc_LegendLayout.cpp"
|