Skip to content

Commit

Permalink
Changed simplifyMatrix() (giowck#120)
Browse files Browse the repository at this point in the history
Only empty edges and columns will be pruned by simplifyMatrix(). This is to make it easier to compose larger form layouts and give more control to the user.
  • Loading branch information
jtMUMT committed Jan 29, 2020
1 parent 484d296 commit 067f9ee
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions components/formlayoutmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,36 +284,46 @@ bool FormLayoutMatrix::findFormWidgetIndex(AbstractFormWidget *fw, int &row, int
void FormLayoutMatrix::simplifyMatrix()
{
bool rowRemoved = false;

//check for empty rows
for (int i = 0; i < m_rows; i++) {
bool markedRow = false; //current row removal flag
QList<AbstractFormWidget*>& currentColumn = m_matrix[i];

if (currentColumn.isEmpty()) {
markedRow = true;
} else {
markedRow = true; //init value
//check if all elements of the row are empty
for (int c = 0; c < m_columns; c++) {
AbstractFormWidget* p = currentColumn.at(c);
//markedRow is only true if all previous and current cell is empty
markedRow = markedRow && ((p == NULL) || (p == (void*)NO_FORM_WIDGET));
bool scanningEdge = true;

//This used to prune out all empty rows/columns
//New functionality only prunes away extra rows/columns
// ie., those on the edges of the layout.

//check for empty rows, from outside in
for (int i = m_rows-1; i >= 0; i--) {
if (scanningEdge == true) { //only test for removal if we're on the edge.
bool markedRow = false; //current row removal flag
QList<AbstractFormWidget*>& currentColumn = m_matrix[i];

if (currentColumn.isEmpty()) {
markedRow = true;
} else {
markedRow = true; //init value
//check if all elements of the row are empty
for (int c = 0; c < m_columns; c++) {
AbstractFormWidget* p = currentColumn.at(c);
//markedRow is only true if all previous and current cell is empty
markedRow = markedRow && ((p == NULL) || (p == (void*)NO_FORM_WIDGET));
}
}
}

if (markedRow) {
removeRow(i);
rowRemoved = true;
simplifyMatrix(); //repeat for all rows but now with one row less
break; //break cycle because after removal the indexes are not coherent anymore
//if we find an empty row, we remove it and then re-try with the next-to-last row.
if (markedRow) {
removeRow(i);
//removing an edge row will not affect the indexing of subsequent loops
} else {
//this row is not empty, then we have found all the empty edges, no more removals.
scanningEdge = false;
}
}
}

//if no row was deleted the indexes are still valid, so we can check columns
if (!rowRemoved) {
//check for empty columns
for (int i = 0; i < m_columns; i++) {
//check for empty columns
scanningEdge = true;
for (int i = m_columns-1; i >= 0; i--) {
if (scanningEdge == true) {
bool markedColumn = true; //current column removal flag
int j = 0;
while (j < m_rows) {
Expand All @@ -328,8 +338,10 @@ void FormLayoutMatrix::simplifyMatrix()

if (markedColumn) {
removeColumn(i);
simplifyMatrix(); //repeat for all columns but now with one column less
break; //break cycle because after removal the indexes are not coherent anymore
//removing an edge column will not affect the indexing of subsequent loops
} else {
//this row is not empty, then we have found all the empty edges, no more removals.
scanningEdge = false;
}
}
}
Expand Down

0 comments on commit 067f9ee

Please sign in to comment.