Add addValueTable method. Add flexibility to formatter ragarding end of row and end of table text

This commit is contained in:
Bjørn Erik Jensen
2018-10-08 15:52:33 +02:00
parent 2a59d86975
commit 83b93e1a1e
2 changed files with 76 additions and 2 deletions

View File

@@ -69,7 +69,7 @@ void RifEclipseDataTableFormatter::setTableRowLineAppendText(const QString& text
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::outputBuffer()
{
if (!m_columns.empty())
if (!m_columns.empty() && !isAllHeadersEmpty(m_columns))
{
m_out << "-- ";
for (RifEclipseOutputTableColumn& column : m_columns)
@@ -98,7 +98,7 @@ void RifEclipseDataTableFormatter::outputBuffer()
m_out << formatColumn(line.data[i], m_columns[i]);
}
m_out << m_tableRowAppendText << "\n";
m_out << (line.appendTextSet ? line.appendText : m_tableRowAppendText) << "\n";
}
}
m_columns.clear();
@@ -139,6 +139,18 @@ void RifEclipseDataTableFormatter::outputHorizontalLine(RifEclipseOutputTableLin
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifEclipseDataTableFormatter::isAllHeadersEmpty(const std::vector<RifEclipseOutputTableColumn>& headers)
{
for (auto& header : headers)
{
if (!header.title.isEmpty()) return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -150,6 +162,43 @@ void RifEclipseDataTableFormatter::tableCompleted()
m_out << m_tableRowPrependText << m_tableRowAppendText << "\n";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::tableCompleted(const QString& appendText, bool appendNewline)
{
outputBuffer();
// Output an "empty" line after a finished table
if (!appendText.isEmpty() || appendNewline)
{
m_out << m_tableRowPrependText << appendText << (appendNewline ? "\n" : "");
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::addValueTable(QTextStream& stream, const QString& name, size_t columns, const std::vector<double>& values)
{
RifEclipseDataTableFormatter subFormatter(stream);
std::vector<RifEclipseOutputTableColumn> cols(columns, RifEclipseOutputTableColumn(""));
subFormatter.setTableRowPrependText("");
subFormatter.keyword(name);
subFormatter.header(cols);
int colCount = 0;
for (int i = 0; i < values.size(); i++)
{
subFormatter.add(values[i]);
if (++colCount % columns == 0 && i < values.size() - 1) subFormatter.rowCompleted("");
}
subFormatter.rowCompleted();
subFormatter.tableCompleted("", false);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -183,6 +232,7 @@ RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::comment(const QStrin
RifEclipseOutputTableLine line;
line.data.push_back(comment);
line.lineType = COMMENT;
line.appendTextSet = false;
if (m_columns.empty())
{
outputComment(line);
@@ -204,6 +254,7 @@ RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::addHorizontalLine(co
data += character;
line.data.push_back(data);
line.lineType = HORIZONTAL_LINE;
line.appendTextSet = false;
if (m_columns.empty())
{
outputComment(line);
@@ -295,10 +346,25 @@ RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::addValueOrDefaultMar
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::rowCompleted()
{
RifEclipseOutputTableLine line;
line.data = m_lineBuffer;
line.lineType = CONTENTS;
line.appendTextSet = false;
m_buffer.push_back(line);
m_lineBuffer.clear();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::rowCompleted(const QString& appendText)
{
RifEclipseOutputTableLine line;
line.data = m_lineBuffer;
line.lineType = CONTENTS;
line.appendTextSet = true;
line.appendText = appendText;
m_buffer.push_back(line);
m_lineBuffer.clear();
}