Skip to content

Commit

Permalink
#1756 Add normalized CS support for table borders coordinates managem…
Browse files Browse the repository at this point in the history
…ent.

Much more robust than absolute coordinates (Heavilly WIP).

Signed-off-by: cneben <[email protected]>
  • Loading branch information
cneben committed Sep 5, 2024
1 parent ed4427f commit 3d973a2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
37 changes: 28 additions & 9 deletions src/qanTableBorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
//-----------------------------------------------------------------------------

// Std headers
#include <sstream>
#include <limits>
#include <algorithm>

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

qreal TableBorder::getSx() const { return _sx; }
void TableBorder::setSx(qreal sx)
{
_sx = sx;
emit sxChanged();
}

qreal TableBorder::getSy() const { return _sy; }
void TableBorder::setSy(qreal sy)
{
_sy = sy;
emit syChanged();
}

bool TableBorder::setOrientation(Qt::Orientation orientation)
{
if (orientation != _orientation) {
Expand Down Expand Up @@ -254,6 +267,10 @@ void TableBorder::mouseMoveEvent(QMouseEvent* event)
const auto borderWidth2 = _borderWidth / 2.;
const auto tableGroupItem = _tableGroup ? qobject_cast<const qan::TableGroupItem*>(_tableGroup->getGroupItem()) :
nullptr;
if (tableGroupItem == nullptr ||
tableGroupItem->getContainer() == nullptr)
return;
const auto tableSize = tableGroupItem->getContainer()->size();

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

auto maxX = std::numeric_limits<qreal>::lowest();
if (_nextBorder == nullptr && // Do not drag outside (on right) table group
tableGroupItem != nullptr)
maxX = tableGroupItem->width() - padding - spacing2 - borderWidth2;
if (_nextBorder == nullptr) // Do not drag outside (on right) table group
maxX = tableSize.width() - padding - spacing2 - borderWidth2;
else { // Do not drag after next border
if (_nextBorder != nullptr)
maxX = std::max(maxX, _nextBorder->verticalCenter() - spacing - borderWidth2);
}
setX(qBound(minX, position.x(), maxX));
const auto x = qBound(minX, position.x(), maxX);
setX(x);
setSx(x / tableSize.width());
xModified = true;
}
else if (getOrientation() == Qt::Horizontal) {
Expand All @@ -291,14 +309,15 @@ void TableBorder::mouseMoveEvent(QMouseEvent* event)
}

auto maxY = std::numeric_limits<qreal>::lowest();
if (_nextBorder == nullptr && // Do not drag outside (past/bottom) table group
tableGroupItem != nullptr)
maxY = tableGroupItem->height() - padding - spacing2 - borderWidth2;
if (_nextBorder == nullptr) // Do not drag outside (past/bottom) table group
maxY = tableSize.height() - padding - spacing2 - borderWidth2;
else { // Do not drag after/under next border
if (_nextBorder != nullptr)
maxY = std::max(maxY, _nextBorder->horizontalCenter() - spacing - borderWidth2);
}
setY(qBound(minY, position.y(), maxY));
const auto y = qBound(minY, position.y(), maxY);
setY(y);
setSy(y / tableSize.height());
yModified = true;
}
event->setAccepted(true);
Expand Down
20 changes: 19 additions & 1 deletion src/qanTableBorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ class TableBorder : public QQuickItem
//! Utility, return border horizontal center, ie y() + height() / 2.
qreal horizontalCenter() const;

public:
Q_PROPERTY(qreal sx READ getSx WRITE setSx NOTIFY sxChanged FINAL)
qreal getSx() const;
void setSx(qreal sX);
protected:
qreal _sx = 0.;
signals:
void sxChanged();

public:
Q_PROPERTY(qreal sy READ getSy WRITE setSy NOTIFY syChanged FINAL)
qreal getSy() const;
void setSy(qreal sY);
protected:
qreal _sy = 0.;
signals:
void syChanged();

public:
Q_PROPERTY(Qt::Orientation orientation READ getOrientation WRITE setOrientation NOTIFY orientationChanged FINAL)
Qt::Orientation getOrientation() const { return _orientation; }
Expand All @@ -88,7 +106,7 @@ class TableBorder : public QQuickItem
void orientationChanged();

public:
/*! \brief FIXME
/*! \brief Border line color.
*/
Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor NOTIFY borderColorChanged FINAL)
void setBorderColor(QColor borderColor);
Expand Down
83 changes: 36 additions & 47 deletions src/qanTableGroupItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ bool TableGroupItem::setContainer(QQuickItem* container) noexcept
if (cell != nullptr)
cell->setParentItem(container);

// Note 20240830: React to size modifications, usually table size
// is fully initialized at this point, to prevent spurious reaction
// set setEnabled(false).
const auto container = getContainer();
if (container != nullptr) {
connect(container, &QQuickItem::widthChanged,
this, &TableGroupItem::layoutTable);
connect(container, &QQuickItem::heightChanged,
this, &TableGroupItem::layoutTable);
}

return true;
}
return false;
Expand Down Expand Up @@ -298,7 +309,6 @@ auto TableGroupItem::createFromComponent(QQmlComponent& component) -> QQuickItem

void TableGroupItem::initializeTableLayout()
{
//qWarning() << "qan::TableGroupItem::initializeTableLayout()";
const auto tableGroup = getTableGroup();
if (tableGroup == nullptr)
return;
Expand All @@ -308,6 +318,7 @@ void TableGroupItem::initializeTableLayout()
const auto tableWidth = tableContainer->width();
const auto tableHeight = tableContainer->height();
const auto tableSize = tableContainer->size();
qWarning() << "qan::TableGroupItem::initializeTableLayout(): tableSize=" << tableSize;
if (qRound(tableWidth) <= 0 || qRound(tableHeight) <= 0)
return;

Expand Down Expand Up @@ -358,7 +369,8 @@ void TableGroupItem::initializeTableLayout()
((c - 1) * spacing) +
(c * cellWidth) +
(spacing / 2.);
verticalBorder->setX(x - borderWidth2);
const auto borderX = x - borderWidth2;
verticalBorder->setSx(borderX / tableWidth);
verticalBorder->setY(0.);
verticalBorder->setWidth(borderWidth);
verticalBorder->setHeight(tableHeight);
Expand All @@ -377,7 +389,10 @@ void TableGroupItem::initializeTableLayout()
(r * cellHeight) +
(spacing / 2.);
horizontalBorder->setX(0.);
horizontalBorder->setY(y - borderHeight2);
// FIXME #1756 BTW, ce serait peut-être bien aussi de normaliser
// width et heght en prevision merge...
const auto borderY = y - borderHeight2;
horizontalBorder->setSy(borderY / tableHeight);
horizontalBorder->setWidth(tableWidth);
horizontalBorder->setHeight(borderHeight);
}
Expand All @@ -388,20 +403,9 @@ void TableGroupItem::initializeTableLayout()
// it will be called automatically when border are moved.
// Note 20230406: In fact calling layout cell is necessary for rows==1, cols==1
// table that need special handling to dimension cells since there is no horiz/vert borders.
layoutCells();

_previousSize = tableSize; // Set a correct initial size

// Note 20240830: React to size modifications, usually table size
// is fully initialized at this point, to prevent spurious reaction
// set setEnabled(false).
const auto container = getContainer();
if (container != nullptr) {
connect(container, &QQuickItem::widthChanged,
this, &TableGroupItem::layoutTable);
connect(container, &QQuickItem::heightChanged,
this, &TableGroupItem::layoutTable);
}
// FIXME #1756
layoutTable();
//layoutCells();
}

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

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

// qWarning() << "TableGroupItem::layoutTable(): " << getGroup()->getLabel() <<
// " tableWidth=" << tableWidth << "tableHeight=" << tableHeight <<
// " _previousSize=" << _previousSize;
// qWarning() << " groupSize" << size();
// qWarning() << " _previousSize.isNull()=" << _previousSize.isNull();
// qWarning() << " _previousSize.isValid()=" << _previousSize.isValid();
// qWarning() << " _previousSize.isEmpty()=" << _previousSize.isEmpty();
if (_previousSize.isNull()) {
if (!tableSize.isEmpty())
_previousSize = tableSize;
return;
}
if (_previousSize == tableSize)
return;
qWarning() << "TableGroupItem::layoutTable(): " << getGroup()->getLabel() <<
" tableWidth=" << tableWidth << "tableHeight=" << tableHeight;

for (const auto verticalBorder: _verticalBorders) {
if (verticalBorder == nullptr)
continue;
const auto previousX = verticalBorder->x();
verticalBorder->setX((previousX / _previousSize.width()) * tableWidth);
// FIXME #1756
//const auto previousX = verticalBorder->x();
//verticalBorder->setX((previousX / _previousSize.width()) * tableWidth);
verticalBorder->setX(verticalBorder->getSx() * tableWidth);
verticalBorder->setY(0.);
verticalBorder->setHeight(tableHeight);
}

for (const auto horizontalBorder: _horizontalBorders) {
if (horizontalBorder == nullptr)
continue;
const auto previousY = horizontalBorder->y();
horizontalBorder->setX(0.);
horizontalBorder->setY((previousY / _previousSize.height()) * tableHeight);
// FIXME #1756
//const auto previousY = horizontalBorder->y();
//horizontalBorder->setY((previousY / _previousSize.height()) * tableHeight);
horizontalBorder->setY(horizontalBorder->getSy() * tableHeight);
horizontalBorder->setWidth(tableWidth);
}

_previousSize = tableSize;
layoutCells();
}

void TableGroupItem::polishTable()
{
// FIXME #1756 no longer necessary ????

if (!isEnabled())
return; // Note 20240830: prevent spurious layouts during serialization (see hlg::TableGroup::serializeFromJson()).
const auto tableContainer = getContainer();
if (tableContainer == nullptr)
return;
layoutCells();
const auto container = getContainer();
if (container != nullptr) {
connect(container, &QQuickItem::widthChanged,
this, &TableGroupItem::layoutTable);
connect(container, &QQuickItem::heightChanged,
this, &TableGroupItem::layoutTable);
}
}

void TableGroupItem::layoutCells()
Expand Down Expand Up @@ -522,7 +512,6 @@ void TableGroupItem::layoutCells()
cell->setY(padding);
}
}
//_previousSize = tableSize;
}

bool TableGroupItem::setGroup(qan::Group* group) noexcept
Expand Down
2 changes: 0 additions & 2 deletions src/qanTableGroupItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ public slots:

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

public:
virtual bool setGroup(qan::Group* group) noexcept override;
Expand Down

0 comments on commit 3d973a2

Please sign in to comment.