Skip to content

Commit

Permalink
Merge pull request #199 from cneben/f/#190-table-support
Browse files Browse the repository at this point in the history
F/#190 table support
  • Loading branch information
cneben authored Feb 6, 2023
2 parents 48f34c6 + 8ea44cc commit 3f66901
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 25 deletions.
12 changes: 10 additions & 2 deletions src/qanTableBorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ void TableBorder::layoutCells()

// Layout prev/next cells position and size
for (auto prevCell: _prevCells) {
if (prevCell == nullptr)
continue;
prevCell->setWidth(vCenter -
prevCell->x() - spacing2);
if (!_prevBorder && // For first column, set cell x too (not prev border will do it).
Expand All @@ -156,10 +158,12 @@ void TableBorder::layoutCells()
}
}
for (auto nextCell: _nextCells) {
if (nextCell == nullptr)
continue;
nextCell->setX(vCenter + spacing2);
if (_nextBorder &&
_nextBorder->verticalCenter() > (x() + spacing)) // nextBorder might still not be initialized...
nextCell->setWidth(_nextBorder->verticalCenter() - x() - spacing); // FIXME #190 secure that
nextCell->setWidth(_nextBorder->verticalCenter() - x() - spacing);

if (!_nextBorder && // For last column, set cell witdh too (no next border will do it).
_tableGroup && tableGroupItem != nullptr) {
Expand All @@ -172,6 +176,8 @@ void TableBorder::layoutCells()

// Layout prev/next cells position and size
for (auto prevCell: _prevCells) {
if (prevCell == nullptr)
continue;
prevCell->setHeight(hCenter -
prevCell->y() - spacing2);
if (!_prevBorder && // For first column, set cell x too (not prev border will do it).
Expand All @@ -180,10 +186,12 @@ void TableBorder::layoutCells()
}
}
for (auto nextCell: _nextCells) {
if (nextCell == nullptr)
continue;
nextCell->setY(hCenter + spacing2);
if (_nextBorder &&
_nextBorder->horizontalCenter() > (y() + spacing)) // nextBorder might still not be initialized...
nextCell->setHeight(_nextBorder->horizontalCenter() - y() - spacing); // FIXME #190 secure that
nextCell->setHeight(_nextBorder->horizontalCenter() - y() - spacing);

if (!_nextBorder && // For last column, set cell witdh too (no next border will do it).
_tableGroup && tableGroupItem != nullptr) {
Expand Down
78 changes: 63 additions & 15 deletions src/qanTableGroupItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ TableGroupItem::~TableGroupItem()
clearLayout();
}

void TableGroupItem::componentComplete() { /* Nil */ }

void TableGroupItem::classBegin() { }

bool TableGroupItem::setContainer(QQuickItem* container) noexcept
{
qWarning() << "qan::TableGroupItem::setContainer(): container=" << container;
Expand Down Expand Up @@ -164,6 +160,8 @@ void TableGroupItem::initialize(int cols, int rows)
}

borderComponent->deleteLater();

initializeTableLayout();
}

void TableGroupItem::createCells(int cellsCount)
Expand Down Expand Up @@ -300,9 +298,9 @@ auto TableGroupItem::createFromComponent(QQmlComponent& component) -> QQuickItem
return item;
};

void TableGroupItem::layoutTable()
void TableGroupItem::initializeTableLayout()
{
qWarning() << "qan::TableGroupItem::layoutTable()";
qWarning() << "qan::TableGroupItem::initializeTableLayout()";
const auto tableGroup = getTableGroup();
if (tableGroup == nullptr)
return;
Expand All @@ -314,11 +312,11 @@ void TableGroupItem::layoutTable()
2.;

if (cols <= 0 || rows <= 0) {
qWarning() << "qan::TableGroupItem::layoutTable(): Error, rows and columns count can't be <= 0.";
qWarning() << "qan::TableGroupItem::initializeTableLayout(): Error, rows and columns count can't be <= 0.";
return;
}
if (spacing < 0 || padding < 0) {
qWarning() << "qan::TableGroupItem::layoutTable(): Error, padding and spacing can't be < 0.";
qWarning() << "qan::TableGroupItem::initializeTableLayout(): Error, padding and spacing can't be < 0.";
return;
}

Expand All @@ -335,7 +333,7 @@ void TableGroupItem::layoutTable()
//qWarning() << "cellHeight=" << cellHeight;

if (cellWidth < 0. || cellHeight < 0.) {
qWarning() << "qan::TableGroupItem::layoutTable(): Error, invalid cell width/height.";
qWarning() << "qan::TableGroupItem::initializeTableLayout(): Error, invalid cell width/height.";
return;
}
// Note: cells are laid out by their borders, do not set their geometry
Expand All @@ -360,7 +358,7 @@ void TableGroupItem::layoutTable()
verticalBorder->setHeight(height());
}
} else
qWarning() << "qan::TableGoupItem::layoutTable(): Invalid vertical border count.";
qWarning() << "qan::TableGoupItem::initializeTableLayout(): Invalid vertical border count.";

// Layout horizontal borders
if (static_cast<int>(_horizontalBorders.size()) == rows - 1) {
Expand All @@ -378,12 +376,59 @@ void TableGroupItem::layoutTable()
horizontalBorder->setHeight(borderHeight);
}
} else
qWarning() << "qan::TableGoupItem::layoutTable(): Invalid horizontal border count.";
qWarning() << "qan::TableGoupItem::initializeTableLayout(): Invalid horizontal border count.";

// Note: There is no need to manually call borders layoutCells() method
// it will be called automatically when border are moved.
}

void TableGroupItem::layoutTable()
{
// Adapt to a new (width, height), keep previous rows/columns ratio
// New position is:
// x = previousX * (actualWidth / previousWidth)
// y = previousY * (actualHeight / previousHeight)
if (_previousSize.isNull()) {
qWarning() << "qan::TableGroupItem::layoutTable(): Invalid initial size.";
_previousSize = size();
return;
}
if (_previousSize == size())
return;

for (const auto verticalBorder: _verticalBorders) {
if (verticalBorder == nullptr)
continue;
const auto previousX = verticalBorder->x();
verticalBorder->setX(qRound(previousX * width() / _previousSize.width()));
verticalBorder->setY(0.);
verticalBorder->setHeight(height());
}

for (const auto horizontalBorder: _horizontalBorders) {
if (horizontalBorder == nullptr)
continue;
const auto previousY = horizontalBorder->y();
horizontalBorder->setX(0.);
horizontalBorder->setY(qRound(previousY * height() / _previousSize.height()));
horizontalBorder->setWidth(width());
}

_previousSize = size();
}

void TableGroupItem::layoutCells()
{
for (const auto verticalBorder: _verticalBorders) {
if (verticalBorder != nullptr)
verticalBorder->layoutCells();
}
for (const auto horizontalBorder: _horizontalBorders) {
if (horizontalBorder != nullptr)
horizontalBorder->layoutCells();
}
}

bool TableGroupItem::setGroup(qan::Group* group) noexcept
{
if (qan::GroupItem::setGroup(group)) {
Expand All @@ -402,11 +447,11 @@ bool TableGroupItem::setGroup(qan::Group* group) noexcept
if (border)
border->setTableGroup(tableGroup);
connect(tableGroup, &qan::TableGroup::cellSpacingChanged,
this, &qan::TableGroupItem::layoutTable);
this, &qan::TableGroupItem::layoutCells);
connect(tableGroup, &qan::TableGroup::cellMinimumSizeChanged,
this, &qan::TableGroupItem::layoutTable);
this, &qan::TableGroupItem::layoutCells);
connect(tableGroup, &qan::TableGroup::tablePaddingChanged,
this, &qan::TableGroupItem::layoutTable);
this, &qan::TableGroupItem::layoutCells);

// Set cell reference to group
for (auto cell: _cells)
Expand Down Expand Up @@ -449,6 +494,8 @@ void TableGroupItem::groupNodeItem(qan::NodeItem* nodeItem, qan::TableCell* g
} else {
// Find cell at groupPos and attach node to cell
for (const auto& cell: _cells) {
if (cell == nullptr)
continue;
const auto cellBr = cell->boundingRect().translated(cell->position());
if (cellBr.contains(groupPos)) {
cell->setItem(nodeItem);
Expand All @@ -465,7 +512,7 @@ void TableGroupItem::ungroupNodeItem(qan::NodeItem* nodeItem, bool transform)
{
if (nodeItem == nullptr) // A container must have configured in concrete QML group component
return;
if (getGraph() &&
if (getGraph() != nullptr &&
getGraph()->getContainerItem() != nullptr) {
auto nodeCell = nodeItem->getNode()->getCell();
if (nodeCell != nullptr) {
Expand All @@ -474,6 +521,7 @@ void TableGroupItem::ungroupNodeItem(qan::NodeItem* nodeItem, bool transform)

nodeCell->restoreCache(nodeItem);
nodeCell->setItem(nullptr);
nodeCell->setUserProp(QVariant{});
nodeItem->setParentItem(getGraph()->getContainerItem());
if (transform)
nodeItem->setPosition(nodeGlobalPos + QPointF{10., 10.}); // A delta to visualize ungroup
Expand Down
19 changes: 11 additions & 8 deletions src/qanTableGroupItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Graph;
class TableGroup;
class TableCell;

/*! \brief FIXME #190
/*! \brief Visual delegate for qan::TableGroup, manage table layout, borders and cells.
*
* \nosubgrouping
*/
Expand All @@ -59,23 +59,18 @@ class TableGroupItem : public qan::GroupItem
/*! \name TableGroupItem Object Management *///----------------------------
//@{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
public:
explicit TableGroupItem(QQuickItem* parent = nullptr);
virtual ~TableGroupItem() override;
TableGroupItem(const TableGroupItem&) = delete;

public:
//! QQmlParserStatus Component.onCompleted() overload to initialize default graph delegate in a valid QQmlEngine.
virtual void componentComplete() override;

virtual void classBegin() override;

//! Override qan::GroupItem::setContainer()
virtual bool setContainer(QQuickItem* container) noexcept override;

signals:
void modified();
//! Emitted when the table geometry has change (border modified for example).
void modified();
//@}
//-------------------------------------------------------------------------

Expand All @@ -91,13 +86,21 @@ class TableGroupItem : public qan::GroupItem

void createCells(int cellsCount);
void createBorders(int verticalBordersCount, int horizontalBordersCount);

protected:
auto createFromComponent(QQmlComponent& component) -> QQuickItem*;

public:
void initializeTableLayout();
public slots:
//! Layout current cell after a table geometry change.
void layoutTable();

//! 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;
protected:
Expand Down

0 comments on commit 3f66901

Please sign in to comment.