Doc: Compile text snippets

Fix minor issues (e.g. whitespace, missing semi-colon) in passing.

Change-Id: Ib39bb66a724542dcac4ca70072628b9bfcaf200d
Done-with: Nico Vertriest <nico.vertriest@qt.io>
Task-number: QTBUG-81486
Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
bb10
Paul Wicking 2020-08-27 12:46:53 +02:00
parent ea85f77a07
commit da9a57a1e6
8 changed files with 136 additions and 22 deletions

View File

@ -33,4 +33,11 @@ SOURCES = \
src_gui_painting_qregion.cpp \
src_gui_painting_qregion_unix.cpp \
src_gui_painting_qtransform.cpp \
src_gui_qopenglshaderprogram.cpp
src_gui_qopenglshaderprogram.cpp \
src_gui_text_qfont.cpp \
src_gui_text_qfontmetrics.cpp \
src_gui_text_qsyntaxhighlighter.cpp \
src_gui_text_qtextcursor.cpp \
src_gui_text_qtextdocument.cpp \
src_gui_text_qtextdocumentwriter.cpp \
src_gui_text_qtextlayout.cpp

View File

@ -47,7 +47,13 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QFont>
#include <QFontInfo>
#include <QFontMetrics>
namespace src_gui_text_qfont {
void wrapper0() {
//! [0]
QFont serifFont("Times", 10, QFont::Bold);
QFont sansFont("Helvetica [Cronyx]", 12);
@ -58,7 +64,11 @@ QFont sansFont("Helvetica [Cronyx]", 12);
QFont f("Helvetica");
//! [1]
} // wrapper0
void wrapper1() {
QFont f1;
//! [2]
QFont f("Helvetica [Cronyx]");
//! [2]
@ -75,3 +85,8 @@ QFontMetrics fm(f1);
int textWidthInPixels = fm.horizontalAdvance("How many pixels wide is this text?");
int textHeightInPixels = fm.height();
//! [4]
Q_UNUSED(textWidthInPixels);
Q_UNUSED(textHeightInPixels);
} // wrapper
} // src_gui_text_qfont

View File

@ -47,7 +47,12 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QFont>
#include <QFontMetrics>
namespace src_gui_text_qfontmetrics {
void wrapper0() {
//! [0]
QFont font("times", 24);
QFontMetrics fm(font);
@ -55,10 +60,21 @@ int pixelsWide = fm.horizontalAdvance("What's the width of this text?");
int pixelsHigh = fm.height();
//! [0]
Q_UNUSED(pixelsWide);
Q_UNUSED(pixelsHigh);
} // wrapper0
void wrapper1() {
//! [1]
QFont font("times", 24);
QFontMetricsF fm(font);
qreal pixelsWide = fm.horizontalAdvance("What's the width of this text?");
qreal pixelsHigh = fm.height();
//! [1]
Q_UNUSED(pixelsWide);
Q_UNUSED(pixelsHigh);
} // wrapper1
} //src_gui_text_qfontmetrics

View File

@ -47,6 +47,24 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QChar>
#include <QList>
#include <QRegularExpression>
#include <QSyntaxHighlighter>
#include <QTextBlockUserData>
#include <QTextEdit>
#include <QTextObject>
namespace src_gui_text_qsyntaxhighlighter {
struct MyHighlighter : public QSyntaxHighlighter
{
explicit MyHighlighter(QTextDocument *document) : QSyntaxHighlighter(document) { Q_UNUSED(document); }
void highlightBlock(const QString &text);
void wrapper();
QString text;
};
//! [0]
QTextEdit *editor = new QTextEdit;
@ -63,15 +81,14 @@ void MyHighlighter::highlightBlock(const QString &text)
QRegularExpression expression("\\bMy[A-Za-z]+\\b");
QRegularExpressionMatchIterator i = expression.globalMatch(text);
while (i.hasNext())
{
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(), match.capturedLength(), myClassFormat);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(), match.capturedLength(), myClassFormat);
}
}
//! [1]
void MyHighlighter::wrapper() {
//! [2]
QTextCharFormat multiLineCommentFormat;
multiLineCommentFormat.setForeground(Qt::red);
@ -86,27 +103,28 @@ if (previousBlockState() != 1)
startIndex = text.indexOf(startExpression);
while (startIndex >= 0) {
QRegularExpressionMatch endMatch;
int endIndex = text.indexOf(endExpression, startIndex, &endMatch);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ endMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(startExpression,
startIndex + commentLength);
QRegularExpressionMatch endMatch;
int endIndex = text.indexOf(endExpression, startIndex, &endMatch);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ endMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(startExpression,
startIndex + commentLength);
}
//! [2]
} // MyHighlighter::wrapper
//! [3]
struct ParenthesisInfo
{
QChar char;
QChar character;
int position;
};
@ -115,3 +133,5 @@ struct BlockData : public QTextBlockUserData
QList<ParenthesisInfo> parentheses;
};
//! [3]
} // src_gui_text_qsyntaxhighlighter

View File

@ -47,6 +47,16 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QImage>
#include <QTextCursor>
#include <QTextDocument>
namespace src_gui_text_qtextcursor {
QTextDocument *textDocument = nullptr;
void wrapper0() {
QTextCursor cursor;
//! [0]
cursor.clearSelection();
@ -56,12 +66,15 @@ cursor.insertText("Hello World");
//! [1]
QImage img = ...
QImage img;
textDocument->addResource(QTextDocument::ImageResource, QUrl("myimage"), img);
cursor.insertImage("myimage");
//! [1]
} // wrapper0
void wrapper1() {
//! [2]
QTextCursor cursor(textDocument);
cursor.beginEditBlock();
@ -71,8 +84,10 @@ cursor.endEditBlock();
textDocument->undo();
//! [2]
} // wrapper1
void wrapper2() {
//! [3]
QTextCursor cursor(textDocument);
cursor.beginEditBlock();
@ -80,7 +95,7 @@ cursor.insertText("Hello");
cursor.insertText("World");
cursor.endEditBlock();
...
// ...
cursor.joinPreviousEditBlock();
cursor.insertText("Hey");
@ -88,3 +103,6 @@ cursor.endEditBlock();
textDocument->undo();
//! [3]
} // wrapper2
} // src_gui_text_qtextcursor

View File

@ -47,7 +47,13 @@
** $QT_END_LICENSE$
**
****************************************************************************/
namespace src_gui_text_qtextdocument {
/* wrap non-code snippet
//! [0]
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>...
//! [0]
*/ // wrap non-code snippet
} // src_gui_text_qtextdocument

View File

@ -47,9 +47,15 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QTextDocumentWriter>
namespace src_gui_text_qtextdocumentwriter {
void wrapper() {
//! [0]
QTextDocumentWriter writer;
writer.setFormat("odf"); // same as writer.setFormat("ODF");
//! [0]
} // wrapper
} // src_gui_text_qtextdocumentwriter

View File

@ -47,6 +47,24 @@
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QTextLayout>
#include <QTextLine>
namespace src_gui_text_qtextlayout {
struct Wrapper : public QPaintDevice
{
void wrapper1();
};
QTextLayout textLayout;
void wrapper0() {
qreal lineWidth = 0;
QFont aFont;
QFontMetrics fontMetrics(aFont);
//! [0]
int leading = fontMetrics.leading();
@ -66,8 +84,16 @@ while (1) {
textLayout.endLayout();
//! [0]
} // wrapper0
void Wrapper::wrapper1() {
//! [1]
QPainter painter(this);
textLayout.draw(&painter, QPoint(0, 0));
//! [1]
} // Wrapper::wrapper1
} // src_gui_text_qtextlayout