#3129 Table Formatter : Add support for creating tables prefixed with comment string

This commit is contained in:
Magne Sjaastad 2018-06-29 11:21:31 +02:00
parent bb91a79515
commit 07530534ef
2 changed files with 113 additions and 18 deletions

View File

@ -26,6 +26,8 @@
RifEclipseDataTableFormatter::RifEclipseDataTableFormatter(QTextStream& out)
: m_out(out)
, m_colSpacing(5)
, m_tableRowPrependText(" ")
, m_tableRowAppendText(" /")
{
}
@ -46,6 +48,22 @@ void RifEclipseDataTableFormatter::setColumnSpacing(int spacing)
m_colSpacing = spacing;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::setTableRowPrependText(const QString& text)
{
m_tableRowPrependText = text;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::setTableRowLineAppendText(const QString& text)
{
m_tableRowAppendText = text;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -67,15 +85,20 @@ void RifEclipseDataTableFormatter::outputBuffer()
{
outputComment(line);
}
else if (line.lineType == HORIZONTAL_LINE)
{
outputHorizontalLine(line);
}
else if (line.lineType == CONTENTS)
{
m_out << " ";
m_out << m_tableRowPrependText;
for (size_t i = 0; i < line.data.size(); ++i)
{
m_out << formatColumn(line.data[i], m_columns[i]);
}
m_out << " /"
<< "\n";
m_out << m_tableRowAppendText << "\n";
}
}
m_columns.clear();
@ -91,19 +114,46 @@ void RifEclipseDataTableFormatter::outputComment(RifEclipseOutputTableLine& comm
}
//--------------------------------------------------------------------------------------------------
///
///
//--------------------------------------------------------------------------------------------------
void RifEclipseDataTableFormatter::tableCompleted()
void RifEclipseDataTableFormatter::outputHorizontalLine(RifEclipseOutputTableLine& comment)
{
outputBuffer();
// Output an "empty" line after a finished table
m_out << "/\n";
if (comment.lineType == HORIZONTAL_LINE)
{
int charCount = tableWidth();
QChar fillChar = ' ';
if (!comment.data.empty())
{
QString firstString = comment.data[0];
if (!firstString.isEmpty())
{
fillChar = firstString[0];
}
}
QString str;
str.fill(fillChar, charCount);
m_out << "--" << str << "\n";
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::keyword(const QString keyword)
void RifEclipseDataTableFormatter::tableCompleted()
{
outputBuffer();
// Output an "empty" line after a finished table
m_out << m_tableRowPrependText << m_tableRowAppendText << "\n";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::keyword(const QString& keyword)
{
CVF_ASSERT(m_buffer.empty());
CVF_ASSERT(m_columns.empty());
@ -128,7 +178,7 @@ RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::header(const std::ve
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::comment(const QString comment)
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::comment(const QString& comment)
{
RifEclipseOutputTableLine line;
line.data.push_back(comment);
@ -147,7 +197,28 @@ RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::comment(const QStrin
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::add(const QString str)
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::addHorizontalLine(const QChar& character)
{
RifEclipseOutputTableLine line;
QString data;
data += character;
line.data.push_back(data);
line.lineType = HORIZONTAL_LINE;
if (m_columns.empty())
{
outputComment(line);
}
else
{
m_buffer.push_back(line);
}
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifEclipseDataTableFormatter& RifEclipseDataTableFormatter::add(const QString& str)
{
size_t column = m_lineBuffer.size();
CVF_ASSERT(column < m_columns.size());
@ -252,6 +323,21 @@ int RifEclipseDataTableFormatter::measure(size_t num)
return format(num).length();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RifEclipseDataTableFormatter::tableWidth() const
{
int characterCount = 0;
for (const auto& col : m_columns)
{
characterCount += formatColumn(" ", col).size();
}
return characterCount;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -287,7 +373,7 @@ QString RifEclipseDataTableFormatter::format(size_t num)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RifEclipseDataTableFormatter::formatColumn(const QString str, RifEclipseOutputTableColumn column)
QString RifEclipseDataTableFormatter::formatColumn(const QString str, RifEclipseOutputTableColumn column) const
{
if (column.alignment == LEFT)
{
@ -295,6 +381,6 @@ QString RifEclipseDataTableFormatter::formatColumn(const QString str, RifEclipse
}
else
{
return str.rightJustified(column.width, ' ').leftJustified(m_colSpacing, ' ');
return str.rightJustified(column.width + m_colSpacing, ' ');
}
}

View File

@ -29,7 +29,8 @@
enum RifEclipseOutputTableLineType
{
COMMENT,
CONTENTS
CONTENTS,
HORIZONTAL_LINE
};
//==================================================================================================
@ -106,15 +107,18 @@ public:
virtual ~RifEclipseDataTableFormatter();
void setColumnSpacing(int spacing);
void setTableRowPrependText(const QString& text);
void setTableRowLineAppendText(const QString& text);
RifEclipseDataTableFormatter& keyword(const QString keyword);
RifEclipseDataTableFormatter& keyword(const QString& keyword);
RifEclipseDataTableFormatter& header(std::vector<RifEclipseOutputTableColumn> tableHeader);
RifEclipseDataTableFormatter& add(const QString str);
RifEclipseDataTableFormatter& add(const QString& str);
RifEclipseDataTableFormatter& add(double num);
RifEclipseDataTableFormatter& add(int num);
RifEclipseDataTableFormatter& add(size_t num);
RifEclipseDataTableFormatter& addZeroBasedCellIndex(size_t index);
RifEclipseDataTableFormatter& comment(const QString str);
RifEclipseDataTableFormatter& comment(const QString& str);
RifEclipseDataTableFormatter& addHorizontalLine(const QChar& str);
void rowCompleted();
void tableCompleted();
@ -124,13 +128,16 @@ private:
int measure(int num);
int measure(size_t num);
int tableWidth() const;
QString format(double num, RifEclipseOutputTableDoubleFormatting doubleFormat);
QString format(int num);
QString format(size_t num);
QString formatColumn(const QString str, RifEclipseOutputTableColumn column);
QString formatColumn(const QString str, RifEclipseOutputTableColumn column) const;
void outputBuffer();
void outputComment(RifEclipseOutputTableLine& comment);
void outputHorizontalLine(RifEclipseOutputTableLine& comment);
private:
std::vector<RifEclipseOutputTableColumn> m_columns;
@ -138,4 +145,6 @@ private:
std::vector<QString> m_lineBuffer;
QTextStream& m_out;
int m_colSpacing;
QString m_tableRowPrependText;
QString m_tableRowAppendText;
};