Skip to content

Commit 3d973a2

Browse files
committed
#1756 Add normalized CS support for table borders coordinates management.
Much more robust than absolute coordinates (Heavilly WIP). Signed-off-by: cneben <[email protected]>
1 parent ed4427f commit 3d973a2

File tree

4 files changed

+83
-59
lines changed

4 files changed

+83
-59
lines changed

src/qanTableBorder.cpp

+28-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
//-----------------------------------------------------------------------------
3434

3535
// Std headers
36-
#include <sstream>
3736
#include <limits>
3837
#include <algorithm>
3938

@@ -71,6 +70,20 @@ void TableBorder::setTableGroup(qan::TableGroup* tableGroup)
7170
qreal TableBorder::verticalCenter() const { return x() + (width() / 2.); }
7271
qreal TableBorder::horizontalCenter() const { return y() + (height() / 2.); }
7372

73+
qreal TableBorder::getSx() const { return _sx; }
74+
void TableBorder::setSx(qreal sx)
75+
{
76+
_sx = sx;
77+
emit sxChanged();
78+
}
79+
80+
qreal TableBorder::getSy() const { return _sy; }
81+
void TableBorder::setSy(qreal sy)
82+
{
83+
_sy = sy;
84+
emit syChanged();
85+
}
86+
7487
bool TableBorder::setOrientation(Qt::Orientation orientation)
7588
{
7689
if (orientation != _orientation) {
@@ -254,6 +267,10 @@ void TableBorder::mouseMoveEvent(QMouseEvent* event)
254267
const auto borderWidth2 = _borderWidth / 2.;
255268
const auto tableGroupItem = _tableGroup ? qobject_cast<const qan::TableGroupItem*>(_tableGroup->getGroupItem()) :
256269
nullptr;
270+
if (tableGroupItem == nullptr ||
271+
tableGroupItem->getContainer() == nullptr)
272+
return;
273+
const auto tableSize = tableGroupItem->getContainer()->size();
257274

258275
const auto padding = _tableGroup ? _tableGroup->getTablePadding() :
259276
2.;
@@ -271,14 +288,15 @@ void TableBorder::mouseMoveEvent(QMouseEvent* event)
271288
}
272289

273290
auto maxX = std::numeric_limits<qreal>::lowest();
274-
if (_nextBorder == nullptr && // Do not drag outside (on right) table group
275-
tableGroupItem != nullptr)
276-
maxX = tableGroupItem->width() - padding - spacing2 - borderWidth2;
291+
if (_nextBorder == nullptr) // Do not drag outside (on right) table group
292+
maxX = tableSize.width() - padding - spacing2 - borderWidth2;
277293
else { // Do not drag after next border
278294
if (_nextBorder != nullptr)
279295
maxX = std::max(maxX, _nextBorder->verticalCenter() - spacing - borderWidth2);
280296
}
281-
setX(qBound(minX, position.x(), maxX));
297+
const auto x = qBound(minX, position.x(), maxX);
298+
setX(x);
299+
setSx(x / tableSize.width());
282300
xModified = true;
283301
}
284302
else if (getOrientation() == Qt::Horizontal) {
@@ -291,14 +309,15 @@ void TableBorder::mouseMoveEvent(QMouseEvent* event)
291309
}
292310

293311
auto maxY = std::numeric_limits<qreal>::lowest();
294-
if (_nextBorder == nullptr && // Do not drag outside (past/bottom) table group
295-
tableGroupItem != nullptr)
296-
maxY = tableGroupItem->height() - padding - spacing2 - borderWidth2;
312+
if (_nextBorder == nullptr) // Do not drag outside (past/bottom) table group
313+
maxY = tableSize.height() - padding - spacing2 - borderWidth2;
297314
else { // Do not drag after/under next border
298315
if (_nextBorder != nullptr)
299316
maxY = std::max(maxY, _nextBorder->horizontalCenter() - spacing - borderWidth2);
300317
}
301-
setY(qBound(minY, position.y(), maxY));
318+
const auto y = qBound(minY, position.y(), maxY);
319+
setY(y);
320+
setSy(y / tableSize.height());
302321
yModified = true;
303322
}
304323
event->setAccepted(true);

src/qanTableBorder.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ class TableBorder : public QQuickItem
7878
//! Utility, return border horizontal center, ie y() + height() / 2.
7979
qreal horizontalCenter() const;
8080

81+
public:
82+
Q_PROPERTY(qreal sx READ getSx WRITE setSx NOTIFY sxChanged FINAL)
83+
qreal getSx() const;
84+
void setSx(qreal sX);
85+
protected:
86+
qreal _sx = 0.;
87+
signals:
88+
void sxChanged();
89+
90+
public:
91+
Q_PROPERTY(qreal sy READ getSy WRITE setSy NOTIFY syChanged FINAL)
92+
qreal getSy() const;
93+
void setSy(qreal sY);
94+
protected:
95+
qreal _sy = 0.;
96+
signals:
97+
void syChanged();
98+
8199
public:
82100
Q_PROPERTY(Qt::Orientation orientation READ getOrientation WRITE setOrientation NOTIFY orientationChanged FINAL)
83101
Qt::Orientation getOrientation() const { return _orientation; }
@@ -88,7 +106,7 @@ class TableBorder : public QQuickItem
88106
void orientationChanged();
89107

90108
public:
91-
/*! \brief FIXME
109+
/*! \brief Border line color.
92110
*/
93111
Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor NOTIFY borderColorChanged FINAL)
94112
void setBorderColor(QColor borderColor);

src/qanTableGroupItem.cpp

+36-47
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ bool TableGroupItem::setContainer(QQuickItem* container) noexcept
7070
if (cell != nullptr)
7171
cell->setParentItem(container);
7272

73+
// Note 20240830: React to size modifications, usually table size
74+
// is fully initialized at this point, to prevent spurious reaction
75+
// set setEnabled(false).
76+
const auto container = getContainer();
77+
if (container != nullptr) {
78+
connect(container, &QQuickItem::widthChanged,
79+
this, &TableGroupItem::layoutTable);
80+
connect(container, &QQuickItem::heightChanged,
81+
this, &TableGroupItem::layoutTable);
82+
}
83+
7384
return true;
7485
}
7586
return false;
@@ -298,7 +309,6 @@ auto TableGroupItem::createFromComponent(QQmlComponent& component) -> QQuickItem
298309

299310
void TableGroupItem::initializeTableLayout()
300311
{
301-
//qWarning() << "qan::TableGroupItem::initializeTableLayout()";
302312
const auto tableGroup = getTableGroup();
303313
if (tableGroup == nullptr)
304314
return;
@@ -308,6 +318,7 @@ void TableGroupItem::initializeTableLayout()
308318
const auto tableWidth = tableContainer->width();
309319
const auto tableHeight = tableContainer->height();
310320
const auto tableSize = tableContainer->size();
321+
qWarning() << "qan::TableGroupItem::initializeTableLayout(): tableSize=" << tableSize;
311322
if (qRound(tableWidth) <= 0 || qRound(tableHeight) <= 0)
312323
return;
313324

@@ -358,7 +369,8 @@ void TableGroupItem::initializeTableLayout()
358369
((c - 1) * spacing) +
359370
(c * cellWidth) +
360371
(spacing / 2.);
361-
verticalBorder->setX(x - borderWidth2);
372+
const auto borderX = x - borderWidth2;
373+
verticalBorder->setSx(borderX / tableWidth);
362374
verticalBorder->setY(0.);
363375
verticalBorder->setWidth(borderWidth);
364376
verticalBorder->setHeight(tableHeight);
@@ -377,7 +389,10 @@ void TableGroupItem::initializeTableLayout()
377389
(r * cellHeight) +
378390
(spacing / 2.);
379391
horizontalBorder->setX(0.);
380-
horizontalBorder->setY(y - borderHeight2);
392+
// FIXME #1756 BTW, ce serait peut-être bien aussi de normaliser
393+
// width et heght en prevision merge...
394+
const auto borderY = y - borderHeight2;
395+
horizontalBorder->setSy(borderY / tableHeight);
381396
horizontalBorder->setWidth(tableWidth);
382397
horizontalBorder->setHeight(borderHeight);
383398
}
@@ -388,20 +403,9 @@ void TableGroupItem::initializeTableLayout()
388403
// it will be called automatically when border are moved.
389404
// Note 20230406: In fact calling layout cell is necessary for rows==1, cols==1
390405
// table that need special handling to dimension cells since there is no horiz/vert borders.
391-
layoutCells();
392-
393-
_previousSize = tableSize; // Set a correct initial size
394-
395-
// Note 20240830: React to size modifications, usually table size
396-
// is fully initialized at this point, to prevent spurious reaction
397-
// set setEnabled(false).
398-
const auto container = getContainer();
399-
if (container != nullptr) {
400-
connect(container, &QQuickItem::widthChanged,
401-
this, &TableGroupItem::layoutTable);
402-
connect(container, &QQuickItem::heightChanged,
403-
this, &TableGroupItem::layoutTable);
404-
}
406+
// FIXME #1756
407+
layoutTable();
408+
//layoutCells();
405409
}
406410

407411
void TableGroupItem::layoutTable()
@@ -420,63 +424,49 @@ void TableGroupItem::layoutTable()
420424
const auto tableWidth = tableContainer->width();
421425
const auto tableHeight = tableContainer->height();
422426

423-
if (qAbs(tableWidth - width()) > 10) // Note 20240904: Table container size must be _almost_ equal to
424-
return; // table group size otherwise we perhaps are still in a polish loop
425-
if (qAbs(tableHeight - height()) > 70)
427+
// During initial polish loop and since we are binded directly on width/
428+
// height change, table container size might be empty.
429+
if (tableSize.isEmpty() || tableSize.isNull())
426430
return;
427431

428-
// qWarning() << "TableGroupItem::layoutTable(): " << getGroup()->getLabel() <<
429-
// " tableWidth=" << tableWidth << "tableHeight=" << tableHeight <<
430-
// " _previousSize=" << _previousSize;
431-
// qWarning() << " groupSize" << size();
432-
// qWarning() << " _previousSize.isNull()=" << _previousSize.isNull();
433-
// qWarning() << " _previousSize.isValid()=" << _previousSize.isValid();
434-
// qWarning() << " _previousSize.isEmpty()=" << _previousSize.isEmpty();
435-
if (_previousSize.isNull()) {
436-
if (!tableSize.isEmpty())
437-
_previousSize = tableSize;
438-
return;
439-
}
440-
if (_previousSize == tableSize)
441-
return;
432+
qWarning() << "TableGroupItem::layoutTable(): " << getGroup()->getLabel() <<
433+
" tableWidth=" << tableWidth << "tableHeight=" << tableHeight;
442434

443435
for (const auto verticalBorder: _verticalBorders) {
444436
if (verticalBorder == nullptr)
445437
continue;
446-
const auto previousX = verticalBorder->x();
447-
verticalBorder->setX((previousX / _previousSize.width()) * tableWidth);
438+
// FIXME #1756
439+
//const auto previousX = verticalBorder->x();
440+
//verticalBorder->setX((previousX / _previousSize.width()) * tableWidth);
441+
verticalBorder->setX(verticalBorder->getSx() * tableWidth);
448442
verticalBorder->setY(0.);
449443
verticalBorder->setHeight(tableHeight);
450444
}
451445

452446
for (const auto horizontalBorder: _horizontalBorders) {
453447
if (horizontalBorder == nullptr)
454448
continue;
455-
const auto previousY = horizontalBorder->y();
456449
horizontalBorder->setX(0.);
457-
horizontalBorder->setY((previousY / _previousSize.height()) * tableHeight);
450+
// FIXME #1756
451+
//const auto previousY = horizontalBorder->y();
452+
//horizontalBorder->setY((previousY / _previousSize.height()) * tableHeight);
453+
horizontalBorder->setY(horizontalBorder->getSy() * tableHeight);
458454
horizontalBorder->setWidth(tableWidth);
459455
}
460456

461-
_previousSize = tableSize;
462457
layoutCells();
463458
}
464459

465460
void TableGroupItem::polishTable()
466461
{
462+
// FIXME #1756 no longer necessary ????
463+
467464
if (!isEnabled())
468465
return; // Note 20240830: prevent spurious layouts during serialization (see hlg::TableGroup::serializeFromJson()).
469466
const auto tableContainer = getContainer();
470467
if (tableContainer == nullptr)
471468
return;
472469
layoutCells();
473-
const auto container = getContainer();
474-
if (container != nullptr) {
475-
connect(container, &QQuickItem::widthChanged,
476-
this, &TableGroupItem::layoutTable);
477-
connect(container, &QQuickItem::heightChanged,
478-
this, &TableGroupItem::layoutTable);
479-
}
480470
}
481471

482472
void TableGroupItem::layoutCells()
@@ -522,7 +512,6 @@ void TableGroupItem::layoutCells()
522512
cell->setY(padding);
523513
}
524514
}
525-
//_previousSize = tableSize;
526515
}
527516

528517
bool TableGroupItem::setGroup(qan::Group* group) noexcept

src/qanTableGroupItem.h

-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ public slots:
105105

106106
//! Layout table cells, triggered when table style change.
107107
void layoutCells();
108-
protected:
109-
QSizeF _previousSize = QSizeF{0., 0.};
110108

111109
public:
112110
virtual bool setGroup(qan::Group* group) noexcept override;

0 commit comments

Comments
 (0)