diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-12-18 11:28:33 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-05-02 04:10:10 +0000 |
commit | deae75ae093d11714dd2f05a40dcfcdb6bb8e10e (patch) | |
tree | 4e1a63940008133fcfcba0e4cf56cf6feccd61c4 /examples/widgets/richtext/textedit/textedit.cpp | |
parent | b32b61f17eb6f816d854d6177e70df9c9e8fb895 (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/textedit.cpp')
-rw-r--r-- | examples/widgets/richtext/textedit/textedit.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
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(); |