summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2018-12-18 00:56:37 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-05-02 04:10:35 +0000
commit355ecfb11c838b4c9facc9a631e04a52531a2127 (patch)
tree5b59ec8df2690601b8e5d33252f8ba2dc45a534c /src
parent0df30ff22e50aa301791fc72f106ab15ce385a6a (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/gui/text/qtextmarkdownwriter.cpp31
-rw-r--r--src/gui/text/qtextmarkdownwriter_p.h2
2 files changed, 33 insertions, 0 deletions
diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp
index c91248757a..fed4a7b766 100644
--- a/src/gui/text/qtextmarkdownwriter.cpp
+++ b/src/gui/text/qtextmarkdownwriter.cpp
@@ -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);
diff --git a/src/gui/text/qtextmarkdownwriter_p.h b/src/gui/text/qtextmarkdownwriter_p.h
index 9845355259..2a9388ca2d 100644
--- a/src/gui/text/qtextmarkdownwriter_p.h
+++ b/src/gui/text/qtextmarkdownwriter_p.h
@@ -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);