Skip to content

Commit

Permalink
Refactoring: modernize LatexDocVisitor::m_tableStateStack
Browse files Browse the repository at this point in the history
  • Loading branch information
doxygen committed Jan 22, 2021
1 parent 302ea69 commit 2ca0666
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 58 deletions.
43 changes: 18 additions & 25 deletions src/latexdocvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ LatexDocVisitor::LatexDocVisitor(FTextStream &t,LatexCodeGenerator &ci,
m_insideItem(FALSE), m_hide(FALSE), m_hideCaption(FALSE), m_insideTabbing(insideTabbing),
m_langExt(langExt)
{
m_tableStateStack.setAutoDelete(TRUE);
}

//--------------------------------------
Expand Down Expand Up @@ -1144,20 +1143,18 @@ void LatexDocVisitor::visitPost(DocHtmlRow *row)
int c=currentColumn();
while (c<=numCols()) // end of row while inside a row span?
{
uint i;
for (i=0;i<rowSpans().count();i++)
for (const auto &span : rowSpans())
{
ActiveRowSpan *span = rowSpans().at(i);
//printf(" found row span: column=%d rs=%d cs=%d rowIdx=%d cell->rowIdx=%d i=%d c=%d\n",
// span->column, span->rowSpan,span->colSpan,row->rowIndex(),span->cell->rowIndex(),i,c);
if (span->rowSpan>0 && span->column==c && // we are at a cell in a row span
row->rowIndex()>span->cell->rowIndex() // but not the row that started the span
if (span.rowSpan>0 && span.column==c && // we are at a cell in a row span
row->rowIndex()>span.cell->rowIndex() // but not the row that started the span
)
{
m_t << "&";
if (span->colSpan>1) // row span is also part of a column span
if (span.colSpan>1) // row span is also part of a column span
{
m_t << "\\multicolumn{" << span->colSpan << "}{";
m_t << "\\multicolumn{" << span.colSpan << "}{";
m_t << "}|}{}";
}
else // solitary row span
Expand All @@ -1172,23 +1169,21 @@ void LatexDocVisitor::visitPost(DocHtmlRow *row)
m_t << "\\\\";

int col = 1;
uint i;
for (i=0;i<rowSpans().count();i++)
for (auto &span : rowSpans())
{
ActiveRowSpan *span = rowSpans().at(i);
if (span->rowSpan>0) span->rowSpan--;
if (span->rowSpan<=0)
if (span.rowSpan>0) span.rowSpan--;
if (span.rowSpan<=0)
{
// inactive span
}
else if (span->column>col)
else if (span.column>col)
{
m_t << "\\cline{" << col << "-" << (span->column-1) << "}";
col = span->column+span->colSpan;
m_t << "\\cline{" << col << "-" << (span.column-1) << "}";
col = span.column+span.colSpan;
}
else
{
col = span->column+span->colSpan;
col = span.column+span.colSpan;
}
}

Expand Down Expand Up @@ -1229,21 +1224,19 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c)
setCurrentColumn(currentColumn()+1);

//Skip columns that span from above.
uint i;
for (i=0;i<rowSpans().count();i++)
for (const auto &span : rowSpans())
{
ActiveRowSpan *span = rowSpans().at(i);
if (span->rowSpan>0 && span->column==currentColumn())
if (span.rowSpan>0 && span.column==currentColumn())
{
if (row && span->colSpan>1)
if (row && span.colSpan>1)
{
m_t << "\\multicolumn{" << span->colSpan << "}{";
m_t << "\\multicolumn{" << span.colSpan << "}{";
if (currentColumn() /*c->columnIndex()*/==1) // add extra | for first column
{
m_t << "|";
}
m_t << "l|}{" << (c->isHeading()? "\\columncolor{\\tableheadbgcolor}" : "") << "}"; // alignment not relevant, empty column
setCurrentColumn(currentColumn()+span->colSpan);
setCurrentColumn(currentColumn()+span.colSpan);
}
else
{
Expand Down Expand Up @@ -1283,7 +1276,7 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c)
//printf("adding row span: cell={r=%d c=%d rs=%d cs=%d} curCol=%d\n",
// c->rowIndex(),c->columnIndex(),c->rowSpan(),c->colSpan(),
// currentColumn());
addRowSpan(new ActiveRowSpan(c,rs,cs,currentColumn()));
addRowSpan(ActiveRowSpan(c,rs,cs,currentColumn()));
m_t << "\\multirow{" << rs << "}{*}{";
}
if (a==DocHtmlCell::Center)
Expand Down
60 changes: 27 additions & 33 deletions src/latexdocvisitor.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
* Copyright (C) 1997-2021 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
Expand All @@ -19,10 +16,10 @@
#ifndef _LATEXDOCVISITOR_H
#define _LATEXDOCVISITOR_H

#include <stack>

#include "docvisitor.h"
#include <qstack.h>
#include <qcstring.h>
#include <qlist.h>

class FTextStream;
class LatexCodeGenerator;
Expand Down Expand Up @@ -149,7 +146,7 @@ class LatexDocVisitor : public DocVisitor
int column;
};

typedef QList<ActiveRowSpan> RowSpanList;
typedef std::vector<ActiveRowSpan> RowSpanList;

//--------------------------------------
// helper functions
Expand Down Expand Up @@ -191,78 +188,75 @@ class LatexDocVisitor : public DocVisitor

struct TableState
{
TableState() : numCols(0), currentColumn(0), inRowSpan(FALSE),
inColSpan(FALSE), firstRow(FALSE)
{ rowSpans.setAutoDelete(TRUE); }
RowSpanList rowSpans;
int numCols;
int currentColumn;
bool inRowSpan;
bool inColSpan;
bool firstRow;
int numCols = 0;
int currentColumn = 0;
bool inRowSpan = false;
bool inColSpan = false;
bool firstRow = false;
};
QStack<TableState> m_tableStateStack; // needed for nested tables
std::stack<TableState> m_tableStateStack; // needed for nested tables
RowSpanList m_emptyRowSpanList;

void pushTableState()
{
m_tableStateStack.push(new TableState);
m_tableStateStack.push(TableState());
}
void popTableState()
{
delete m_tableStateStack.pop();
m_tableStateStack.pop();
}
int currentColumn() const
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->currentColumn : 0;
return !m_tableStateStack.empty() ? m_tableStateStack.top().currentColumn : 0;
}
void setCurrentColumn(int col)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->currentColumn = col;
if (!m_tableStateStack.empty()) m_tableStateStack.top().currentColumn = col;
}
int numCols() const
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->numCols : 0;
return !m_tableStateStack.empty() ? m_tableStateStack.top().numCols : 0;
}
void setNumCols(int num)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->numCols = num;
if (!m_tableStateStack.empty()) m_tableStateStack.top().numCols = num;
}
bool inRowSpan() const
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->inRowSpan : FALSE;
return !m_tableStateStack.empty() ? m_tableStateStack.top().inRowSpan : FALSE;
}
void setInRowSpan(bool b)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->inRowSpan = b;
if (!m_tableStateStack.empty()) m_tableStateStack.top().inRowSpan = b;
}
bool inColSpan() const
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->inColSpan : FALSE;
return !m_tableStateStack.empty() ? m_tableStateStack.top().inColSpan : FALSE;
}
void setInColSpan(bool b)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->inColSpan = b;
if (!m_tableStateStack.empty()) m_tableStateStack.top().inColSpan = b;
}
bool firstRow() const
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->firstRow : FALSE;
return !m_tableStateStack.empty() ? m_tableStateStack.top().firstRow : FALSE;
}
void setFirstRow(bool b)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->firstRow = b;
if (!m_tableStateStack.empty()) m_tableStateStack.top().firstRow = b;
}
const RowSpanList &rowSpans()
RowSpanList &rowSpans()
{
return !m_tableStateStack.isEmpty() ? m_tableStateStack.top()->rowSpans : m_emptyRowSpanList;
return !m_tableStateStack.empty() ? m_tableStateStack.top().rowSpans : m_emptyRowSpanList;
}
void addRowSpan(ActiveRowSpan *span)
void addRowSpan(ActiveRowSpan &&span)
{
if (!m_tableStateStack.isEmpty()) m_tableStateStack.top()->rowSpans.append(span);
if (!m_tableStateStack.empty()) m_tableStateStack.top().rowSpans.push_back(std::move(span));
}
bool insideTable() const
{
return !m_tableStateStack.isEmpty();
return !m_tableStateStack.empty();
}

};
Expand Down

0 comments on commit 2ca0666

Please sign in to comment.