Table: use the length of header row as the length of the table

This commit is contained in:
Le Tan 2018-11-29 21:26:19 +08:00
parent 8d95553e82
commit 05a42bd2bb
2 changed files with 33 additions and 13 deletions

View File

@ -208,6 +208,8 @@ void VTable::format()
}
int nrCols = calculateColumnCount();
pruneColumns(nrCols);
for (int i = 0; i < nrCols; ++i) {
formatOneColumn(i, curRowIdx, curPib);
}
@ -215,16 +217,9 @@ void VTable::format()
int VTable::calculateColumnCount() const
{
int nr = 0;
// Find the longest row.
for (const auto & row : m_rows) {
if (row.m_cells.size() > nr) {
nr = row.m_cells.size();
}
}
return nr;
// We use the width of the header as the width of the table.
// With this, we could add or remove one column by just changing the header row.
return header()->m_cells.size();
}
VTable::Row *VTable::header() const
@ -354,7 +349,7 @@ void VTable::fetchCellInfoOfColumn(int p_idx,
// Get the info of this cell.
const auto & cell = row.m_cells[p_idx];
int first = 1, last = cell.m_length - 2;
int first = 1, last = cell.m_length - 1;
for (; first <= last; ++first) {
if (cell.m_text[first] != ' ') {
// Found the core content.
@ -589,7 +584,7 @@ void VTable::writeExist()
for (auto & row : m_rows) {
bool needChange = false;
for (const auto & cell : row.m_cells) {
if (!cell.m_formattedText.isEmpty()) {
if (!cell.m_formattedText.isEmpty() || cell.m_deleted) {
needChange = true;
break;
}
@ -610,6 +605,10 @@ void VTable::writeExist()
// Construct the block text.
QString newBlockText(row.m_preText);
for (auto & cell : row.m_cells) {
if (cell.m_deleted) {
continue;
}
int pos = newBlockText.size();
if (cell.m_formattedText.isEmpty()) {
newBlockText += cell.m_text;
@ -658,6 +657,10 @@ void VTable::writeNonExist()
const auto & row = m_rows[rowIdx];
tableText += row.m_preText;
for (auto & cell : row.m_cells) {
if (cell.m_deleted) {
continue;
}
tableText += cell.m_text;
}
@ -676,3 +679,12 @@ void VTable::writeNonExist()
cursor.setPosition(pos);
m_editor->setTextCursorW(cursor);
}
void VTable::pruneColumns(int p_nrCols)
{
for (auto & row : m_rows) {
for (int i = p_nrCols; i < row.m_cells.size(); ++i) {
row.m_cells[i].m_deleted = true;
}
}
}

View File

@ -34,7 +34,8 @@ private:
Cell()
: m_offset(-1),
m_length(0),
m_cursorCoreOffset(-1)
m_cursorCoreOffset(-1),
m_deleted(false)
{
}
@ -45,6 +46,7 @@ private:
m_text.clear();
m_formattedText.clear();
m_cursorCoreOffset = -1;
m_deleted = false;
}
// Start offset within block, including the starting border |.
@ -62,6 +64,9 @@ private:
// If cursor is within this cell, this will not be -1.
int m_cursorCoreOffset;
// Whether this cell need to be deleted.
bool m_deleted;
};
struct Row
@ -169,6 +174,9 @@ private:
void writeNonExist();
// Prune unwanted columns beyond the header row.
void pruneColumns(int p_nrCols);
VTable::Row *header() const;
VTable::Row *delimiter() const;