From 8f051fc6123543001a5897551ffb752c7c682c6f Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 24 Jun 2021 19:46:18 +0200 Subject: [PATCH] Remove table useless qetgraphicstableitem when model is reseted When the model of a qetgraphicstableitem is reseted (for exemple when the sql query is modified) we check if there is useless tables (table with 0 row displayed) and remove it. --- .../ViewItem/qetgraphicstableitem.cpp | 68 +++++++++++++++---- .../ViewItem/qetgraphicstableitem.h | 4 +- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp index e9fb6d198..9cc7e63fa 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp @@ -280,8 +280,8 @@ void QetGraphicsTableItem::paint( if (m_previous_table) //Remove the number of row already displayed by previous tables row_count -= m_previous_table->displayNRowOffset(); - if (m_number_of_displayed_row > 0) //User override the number of row to display - row_count = std::min(row_count, m_number_of_displayed_row); + if (m_number_of_row_to_display > 0) //User override the number of row to display + row_count = std::min(row_count, m_number_of_row_to_display); //Draw horizontal lines auto cell_height = static_cast(m_current_size.height())/static_cast(row_count); @@ -374,8 +374,8 @@ QSize QetGraphicsTableItem::minimumSize() const row_count -= m_previous_table->displayNRowOffset(); } - if (m_number_of_displayed_row > 0) - row_count = std::min(row_count, m_number_of_displayed_row); + if (m_number_of_row_to_display > 0) + row_count = std::min(row_count, m_number_of_row_to_display); //m_minimum_column_width already take in count the minimum size of header @@ -399,7 +399,7 @@ QSize QetGraphicsTableItem::minimumSize() const */ void QetGraphicsTableItem::setDisplayNRow(const int &number) { - m_number_of_displayed_row = number; + m_number_of_row_to_display = number; setToMinimumHeight(); if (m_next_table) m_next_table->previousTableDisplayRowChanged(); @@ -412,7 +412,7 @@ void QetGraphicsTableItem::setDisplayNRow(const int &number) */ int QetGraphicsTableItem::displayNRow() const { - return m_number_of_displayed_row; + return m_number_of_row_to_display; } /** @@ -499,13 +499,31 @@ QString QetGraphicsTableItem::tableName() const */ int QetGraphicsTableItem::displayNRowOffset() const { - auto offset_ = m_number_of_displayed_row; + auto offset_ = m_number_of_row_to_display; if(m_previous_table) offset_ += m_previous_table->displayNRowOffset(); return offset_; } +/** + * @brief QetGraphicsTableItem::displayedRowCount + * @return the number of row displayed by this table + */ +int QetGraphicsTableItem::displayedRowCount() const +{ + //Calcule the number of rows to display. + auto row_count = m_model->rowCount(); + + if (m_previous_table) //Remove the number of row already displayed by previous tables + row_count -= m_previous_table->displayNRowOffset(); + + if (m_number_of_row_to_display > 0) //User override the number of row to display + row_count = std::min(row_count, m_number_of_row_to_display); + + return row_count; +} + QetGraphicsTableItem *QetGraphicsTableItem::previousTable() const { return m_previous_table; @@ -565,7 +583,7 @@ QDomElement QetGraphicsTableItem::toXml(QDomDocument &dom_document) const dom_table.setAttribute("height", QString::number(m_current_size.height())); dom_table.setAttribute("uuid", m_uuid.toString()); dom_table.setAttribute("name", m_name); - dom_table.setAttribute("display_n_row", QString::number(m_number_of_displayed_row)); + dom_table.setAttribute("display_n_row", QString::number(m_number_of_row_to_display)); //Add the header xml dom_table.appendChild(m_header_item->toXml(dom_document)); @@ -623,7 +641,7 @@ void QetGraphicsTableItem::fromXml(const QDomElement &dom_element) "uuid", QUuid::createUuid().toString())); m_name = dom_element.attribute("name"); - m_number_of_displayed_row = dom_element.attribute( + m_number_of_row_to_display = dom_element.attribute( "display_n_row", QString::number(0)).toInt(); @@ -688,8 +706,8 @@ bool QetGraphicsTableItem::toDXF(const QString &filepath) if (m_previous_table) //Remove the number of row already displayed by previous tables row_count -= m_previous_table->displayNRowOffset(); - if (m_number_of_displayed_row > 0) //User override the number of row to display - row_count = std::min(row_count, m_number_of_displayed_row); + if (m_number_of_row_to_display > 0) //User override the number of row to display + row_count = std::min(row_count, m_number_of_row_to_display); //Draw horizontal lines auto cell_height = static_cast(m_current_size.height())/static_cast(row_count); @@ -816,13 +834,18 @@ QVariant QetGraphicsTableItem::itemChange( return QetGraphicsItem::itemChange(change, value); } +/** + * @brief QetGraphicsTableItem::modelReseted + */ void QetGraphicsTableItem::modelReseted() { dataChanged(m_model->index(0,0), m_model->index(0,0), QVector()); setToMinimumHeight(); - if (!previousTable()) { //this is the head table + if (!previousTable()) //this is the head table + { checkInsufficientRowsCount(this); + removeUselessNextTable(); } } @@ -1056,3 +1079,24 @@ void QetGraphicsTableItem::previousTableDisplayRowChanged() m_next_table->previousTableDisplayRowChanged(); } } + +/** + * @brief QetGraphicsTableItem::removeUselessNextTable + * Remove next table if useless, an useless table with 0 row displayed. + * If \p recursive is true check and remove for all sub next table. + * @param recursive + */ +void QetGraphicsTableItem::removeUselessNextTable(bool recursive) +{ + if (!m_next_table) { + return; + } + + if (recursive) { + m_next_table->removeUselessNextTable(); + } + if (m_next_table->displayedRowCount() <= 0) { + delete m_next_table; + m_next_table = nullptr; + } +} diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h index b7c5202f4..c7cf58473 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h @@ -79,6 +79,7 @@ class QetGraphicsTableItem : public QetGraphicsItem void setTableName(const QString &name); QString tableName() const; int displayNRowOffset() const; + int displayedRowCount() const; QetGraphicsTableItem *previousTable() const; QetGraphicsTableItem *nextTable() const; void setToMinimumHeight(); @@ -120,13 +121,14 @@ class QetGraphicsTableItem : public QetGraphicsItem void headerSectionResized(); void adjustSize(); void previousTableDisplayRowChanged(); + void removeUselessNextTable(bool recursive = true); QAbstractItemModel *m_model= nullptr; QVector m_minimum_column_width; int m_minimum_row_height, - m_number_of_displayed_row = 0, + m_number_of_row_to_display = 0, m_br_margin = 10; QSize