Add QTextMarkdownWriter::writeTable(QAbstractTableModel)
This provides the ability to write live data from a table to Markdown, which can be useful to load it into a text document or a wiki. But so far QTextMarkdownWriter is still a private class, intended to be used experimentally from QtQuick and perhaps later exposed in other ways. Change-Id: I0de4673987e4172178604e49b5a024a05d4486ba Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
0df30ff22e
commit
355ecfb11c
@ -64,6 +64,37 @@ bool QTextMarkdownWriter::writeAll(const QTextDocument &document)
|
||||
return true;
|
||||
}
|
||||
|
||||
void QTextMarkdownWriter::writeTable(const QAbstractTableModel &table)
|
||||
{
|
||||
QVector<int> tableColumnWidths(table.columnCount());
|
||||
for (int col = 0; col < table.columnCount(); ++col) {
|
||||
tableColumnWidths[col] = table.headerData(col, Qt::Horizontal).toString().length();
|
||||
for (int row = 0; row < table.rowCount(); ++row) {
|
||||
tableColumnWidths[col] = qMax(tableColumnWidths[col],
|
||||
table.data(table.index(row, col)).toString().length());
|
||||
}
|
||||
}
|
||||
|
||||
// write the header and separator
|
||||
for (int col = 0; col < table.columnCount(); ++col) {
|
||||
QString s = table.headerData(col, Qt::Horizontal).toString();
|
||||
m_stream << "|" << s << QString(tableColumnWidths[col] - s.length(), Space);
|
||||
}
|
||||
m_stream << "|" << endl;
|
||||
for (int col = 0; col < tableColumnWidths.length(); ++col)
|
||||
m_stream << '|' << QString(tableColumnWidths[col], QLatin1Char('-'));
|
||||
m_stream << '|'<< endl;
|
||||
|
||||
// write the body
|
||||
for (int row = 0; row < table.rowCount(); ++row) {
|
||||
for (int col = 0; col < table.columnCount(); ++col) {
|
||||
QString s = table.data(table.index(row, col)).toString();
|
||||
m_stream << "|" << s << QString(tableColumnWidths[col] - s.length(), Space);
|
||||
}
|
||||
m_stream << '|'<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
void QTextMarkdownWriter::writeFrame(const QTextFrame *frame)
|
||||
{
|
||||
Q_ASSERT(frame);
|
||||
|
@ -56,6 +56,7 @@
|
||||
|
||||
#include "qtextdocument_p.h"
|
||||
#include "qtextdocumentwriter.h"
|
||||
#include "QAbstractTableModel"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
@ -64,6 +65,7 @@ class Q_GUI_EXPORT QTextMarkdownWriter
|
||||
public:
|
||||
QTextMarkdownWriter(QTextStream &stream, QTextDocument::MarkdownFeatures features);
|
||||
bool writeAll(const QTextDocument &document);
|
||||
void writeTable(const QAbstractTableModel &table);
|
||||
|
||||
int writeBlock(const QTextBlock &block, bool table, bool ignoreFormat);
|
||||
void writeFrame(const QTextFrame *frame);
|
||||
|
@ -32,6 +32,9 @@
|
||||
#include <private/qtablewidget_p.h>
|
||||
#include <QtTest/QtTest>
|
||||
#include "private/qapplication_p.h"
|
||||
#if QT_CONFIG(textmarkdownwriter)
|
||||
#include "private/qtextmarkdownwriter_p.h"
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
@ -196,6 +199,10 @@ private slots:
|
||||
void viewOptions();
|
||||
|
||||
void taskQTBUG_7232_AllowUserToControlSingleStep();
|
||||
|
||||
#if QT_CONFIG(textmarkdownwriter)
|
||||
void markdownWriter();
|
||||
#endif
|
||||
};
|
||||
|
||||
// Testing get/set functions
|
||||
@ -4560,5 +4567,33 @@ void tst_QTableView::taskQTBUG_50171_selectRowAfterSwapColumns()
|
||||
}
|
||||
}
|
||||
|
||||
// This has nothing to do with QTableView, but it's convenient to reuse the QtTestTableModel
|
||||
#if QT_CONFIG(textmarkdownwriter)
|
||||
|
||||
// #define DEBUG_WRITE_OUTPUT
|
||||
|
||||
void tst_QTableView::markdownWriter()
|
||||
{
|
||||
QtTestTableModel model(2, 3);
|
||||
QString md;
|
||||
{
|
||||
QTextStream stream(&md);
|
||||
QTextMarkdownWriter writer(stream, QTextDocument::MarkdownDialectGitHub);
|
||||
writer.writeTable(model);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_WRITE_OUTPUT
|
||||
{
|
||||
QFile out("/tmp/table.md");
|
||||
out.open(QFile::WriteOnly);
|
||||
out.write(md.toUtf8());
|
||||
out.close();
|
||||
}
|
||||
#endif
|
||||
|
||||
QCOMPARE(md, QString::fromLatin1("|1 |2 |3 |\n|-------|-------|-------|\n|[0,0,0]|[0,1,0]|[0,2,0]|\n|[1,0,0]|[1,1,0]|[1,2,0]|\n"));
|
||||
}
|
||||
#endif
|
||||
|
||||
QTEST_MAIN(tst_QTableView)
|
||||
#include "tst_qtableview.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user