summaryrefslogtreecommitdiffstats
path: root/examples/widgets/richtext/textedit
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2017-12-18 11:28:33 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-05-02 04:10:10 +0000
commitdeae75ae093d11714dd2f05a40dcfcdb6bb8e10e (patch)
tree4e1a63940008133fcfcba0e4cf56cf6feccd61c4 /examples/widgets/richtext/textedit
parentb32b61f17eb6f816d854d6177e70df9c9e8fb895 (diff)
TextEdit example: add Markdown as a supported format
Also use QT_CONFIG to disable features gracefully if Qt is configured without them. Change-Id: I38e92bf5fd70f77fc4d5158769d590619be8905f Reviewed-by: Gatis Paeglis <gatis.paeglis@qt.io>
Diffstat (limited to 'examples/widgets/richtext/textedit')
-rw-r--r--examples/widgets/richtext/textedit/example.md96
-rw-r--r--examples/widgets/richtext/textedit/textedit.cpp33
2 files changed, 125 insertions, 4 deletions
diff --git a/examples/widgets/richtext/textedit/example.md b/examples/widgets/richtext/textedit/example.md
new file mode 100644
index 0000000000..70aeaf7649
--- /dev/null
+++ b/examples/widgets/richtext/textedit/example.md
@@ -0,0 +1,96 @@
+# QTextEdit
+
+The QTextEdit widget is an advanced editor that supports formatted rich text.
+It can be used to display HTML and other rich document formats. Internally,
+QTextEdit uses the QTextDocument class to describe both the high-level
+structure of each document and the low-level formatting of paragraphs.
+
+If you are viewing this document in the textedit example, you can edit this
+document to explore Qt's rich text editing features. We have included some
+comments in each of the following sections to encourage you to experiment.
+
+## Font and Paragraph Styles
+
+QTextEdit supports **bold**, *italic*, &amp; ~~strikethrough~~ font styles, and
+can display <span style="font-size:10pt; font-weight:600;
+color:#00007f;">multicolored</span> text. Font families such as <span
+style="font-family:Times New Roman">Times New Roman</span> and `Courier`
+can also be used directly. *If you place the cursor in a region of styled text,
+the controls in the tool bars will change to reflect the current style.*
+
+Paragraphs can be formatted so that the text is left-aligned, right-aligned,
+centered, or fully justified.
+
+*Try changing the alignment of some text and resize the editor to see how the
+text layout changes.*
+
+## Lists
+
+Different kinds of lists can be included in rich text documents. Standard
+bullet lists can be nested, using different symbols for each level of the list:
+
+- Disc symbols are typically used for top-level list items.
+ * Circle symbols can be used to distinguish between items in lower-level
+ lists.
+ + Square symbols provide a reasonable alternative to discs and circles.
+
+Ordered lists can be created that can be used for tables of contents. Different
+characters can be used to enumerate items, and we can use both Roman and Arabic
+numerals in the same list structure:
+
+1. Introduction
+2. Qt Tools
+ 1) Qt Assistant
+ 2) Qt Designer
+ 1. Form Editor
+ 2. Component Architecture
+ 3) Qt Linguist
+
+The list will automatically be renumbered if you add or remove items. *Try
+adding new sections to the above list or removing existing item to see the
+numbers change.*
+
+## Images
+
+Inline images are treated like ordinary ranges of characters in the text
+editor, so they flow with the surrounding text. Images can also be selected in
+the same way as text, making it easy to cut, copy, and paste them.
+
+![logo](images/logo32.png "logo") *Try to select this image by clicking and
+dragging over it with the mouse, or use the text cursor to select it by holding
+down Shift and using the arrow keys. You can then cut or copy it, and paste it
+into different parts of this document.*
+
+## Tables
+
+QTextEdit can arrange and format tables, supporting features such as row and
+column spans, text formatting within cells, and size constraints for columns.
+
+| | Development Tools | Programming Techniques | Graphical User Interfaces |
+| ------------: | ----------------- | ---------------------- | ------------------------- |
+| 9:00 - 11:00 | Introduction to Qt |||
+| 11:00 - 13:00 | Using qmake | Object-oriented Programming | Layouts in Qt |
+| 13:00 - 15:00 | Qt Designer Tutorial | Extreme Programming | Writing Custom Styles |
+| 15:00 - 17:00 | Qt Linguist and Internationalization | &nbsp; | &nbsp; |
+
+*Try adding text to the cells in the table and experiment with the alignment of
+the paragraphs.*
+
+## Hyperlinks
+
+QTextEdit is designed to support hyperlinks between documents, and this feature
+is used extensively in
+[Qt Assistant](http://doc.qt.io/qt-5/qtassistant-index.html). Hyperlinks are
+automatically created when an HTML file is imported into an editor. Since the
+rich text framework supports hyperlinks natively, they can also be created
+programatically.
+
+## Undo and Redo
+
+Full support for undo and redo operations is built into QTextEdit and the
+underlying rich text framework. Operations on a document can be packaged
+together to make editing a more comfortable experience for the user.
+
+*Try making changes to this document and press `Ctrl+Z` to undo them. You can
+always recover the original contents of the document.*
+
diff --git a/examples/widgets/richtext/textedit/textedit.cpp b/examples/widgets/richtext/textedit/textedit.cpp
index 3ad9f48b67..7b8c242cfc 100644
--- a/examples/widgets/richtext/textedit/textedit.cpp
+++ b/examples/widgets/richtext/textedit/textedit.cpp
@@ -71,6 +71,7 @@
#include <QCloseEvent>
#include <QMessageBox>
#include <QMimeData>
+#include <QMimeDatabase>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
@@ -395,11 +396,18 @@ bool TextEdit::load(const QString &f)
QByteArray data = file.readAll();
QTextCodec *codec = Qt::codecForHtml(data);
QString str = codec->toUnicode(data);
+ QUrl baseUrl = (f.front() == QLatin1Char(':') ? QUrl(f) : QUrl::fromLocalFile(f)).adjusted(QUrl::RemoveFilename);
+ textEdit->document()->setBaseUrl(baseUrl);
if (Qt::mightBeRichText(str)) {
textEdit->setHtml(str);
} else {
- str = QString::fromLocal8Bit(data);
- textEdit->setPlainText(str);
+#if QT_CONFIG(textmarkdownreader)
+ QMimeDatabase db;
+ if (db.mimeTypeForFileNameAndData(f, data).name() == QLatin1String("text/markdown"))
+ textEdit->setMarkdown(str);
+ else
+#endif
+ textEdit->setPlainText(QString::fromLocal8Bit(data));
}
setCurrentFileName(f);
@@ -451,7 +459,15 @@ void TextEdit::fileOpen()
QFileDialog fileDialog(this, tr("Open File..."));
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setFileMode(QFileDialog::ExistingFile);
- fileDialog.setMimeTypeFilters(QStringList() << "text/html" << "text/plain");
+ fileDialog.setMimeTypeFilters(QStringList()
+#if QT_CONFIG(texthtmlparser)
+ << "text/html"
+#endif
+#if QT_CONFIG(textmarkdownreader)
+
+ << "text/markdown"
+#endif
+ << "text/plain");
if (fileDialog.exec() != QDialog::Accepted)
return;
const QString fn = fileDialog.selectedFiles().first();
@@ -485,9 +501,18 @@ bool TextEdit::fileSaveAs()
QFileDialog fileDialog(this, tr("Save as..."));
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList mimeTypes;
- mimeTypes << "application/vnd.oasis.opendocument.text" << "text/html" << "text/plain";
+ mimeTypes << "text/plain"
+#if QT_CONFIG(textodfwriter)
+ << "application/vnd.oasis.opendocument.text"
+#endif
+#if QT_CONFIG(textmarkdownwriter)
+ << "text/markdown"
+#endif
+ << "text/html";
fileDialog.setMimeTypeFilters(mimeTypes);
+#if QT_CONFIG(textodfwriter)
fileDialog.setDefaultSuffix("odt");
+#endif
if (fileDialog.exec() != QDialog::Accepted)
return false;
const QString fn = fileDialog.selectedFiles().first();