Qt: honor control characters in ScriptingTextBuffer::print
This commit is contained in:
parent
1eb130a28b
commit
938ee55d42
@ -49,26 +49,70 @@ void ScriptingTextBuffer::setBufferName(const QString& name) {
|
|||||||
emit bufferNameChanged(name);
|
emit bufferNameChanged(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptingTextBuffer::lineBreak() {
|
||||||
|
bool nextBlockExists = m_shim.cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 1);
|
||||||
|
if (!nextBlockExists) {
|
||||||
|
m_shim.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor, 1);
|
||||||
|
m_shim.cursor.insertBlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingTextBuffer::carriageReturn() {
|
||||||
|
m_shim.cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingTextBuffer::tab() {
|
||||||
|
QTextCursor& cursor = m_shim.cursor;
|
||||||
|
int column = cursor.positionInBlock();
|
||||||
|
int move = tabStop - (column % tabStop) + 1;
|
||||||
|
if (column + move >= m_dims.width()) {
|
||||||
|
lineBreak();
|
||||||
|
} else if (column + move <= cursor.block().length()) {
|
||||||
|
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, move - 1);
|
||||||
|
} else {
|
||||||
|
move = column + move - cursor.block().length();
|
||||||
|
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor, 1);
|
||||||
|
cursor.insertText(QString(move, ' '));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingTextBuffer::insertString(const QString& text) {
|
||||||
|
QTextCursor& cursor = m_shim.cursor;
|
||||||
|
if (cursor.positionInBlock() >= m_dims.width()) {
|
||||||
|
lineBreak();
|
||||||
|
}
|
||||||
|
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, text.length());
|
||||||
|
cursor.insertText(text);
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptingTextBuffer::print(const QString& text) {
|
void ScriptingTextBuffer::print(const QString& text) {
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
for (QString split : text.split('\n')) {
|
|
||||||
while (m_shim.cursor.positionInBlock() + split.length() > m_dims.width()) {
|
QTextCursor& cursor = m_shim.cursor;
|
||||||
int cut = m_dims.width() - m_shim.cursor.positionInBlock();
|
QString toInsert;
|
||||||
if (!m_shim.cursor.atBlockEnd()) {
|
|
||||||
m_shim.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
for (const QChar& ch : text) {
|
||||||
|
int column = cursor.positionInBlock();
|
||||||
|
if (ch == '\t' || ch == '\n' || ch == '\r' || column + toInsert.length() >= m_dims.width()) {
|
||||||
|
insertString(toInsert);
|
||||||
|
toInsert.clear();
|
||||||
}
|
}
|
||||||
m_shim.cursor.insertText(split.left(cut));
|
switch (ch.unicode()) {
|
||||||
if (m_shim.cursor.atEnd()) {
|
case '\t':
|
||||||
m_shim.cursor.insertBlock();
|
tab();
|
||||||
} else {
|
break;
|
||||||
m_shim.cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 1);
|
case '\n':
|
||||||
|
lineBreak();
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
carriageReturn();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
toInsert += ch;
|
||||||
}
|
}
|
||||||
split = split.mid(cut);
|
|
||||||
}
|
}
|
||||||
if (!m_shim.cursor.atBlockEnd()) {
|
if (!toInsert.isEmpty()) {
|
||||||
m_shim.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, split.length());
|
insertString(toInsert);
|
||||||
}
|
|
||||||
m_shim.cursor.insertText(split);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ signals:
|
|||||||
void bufferNameChanged(const QString&);
|
void bufferNameChanged(const QString&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
enum { tabStop = 4 };
|
||||||
|
|
||||||
struct ScriptingBufferShim : public mScriptTextBuffer {
|
struct ScriptingBufferShim : public mScriptTextBuffer {
|
||||||
ScriptingTextBuffer* p;
|
ScriptingTextBuffer* p;
|
||||||
QTextCursor cursor;
|
QTextCursor cursor;
|
||||||
@ -44,6 +46,11 @@ private:
|
|||||||
QMutex m_mutex;
|
QMutex m_mutex;
|
||||||
QString m_name;
|
QString m_name;
|
||||||
|
|
||||||
|
void lineBreak();
|
||||||
|
void carriageReturn();
|
||||||
|
void tab();
|
||||||
|
void insertString(const QString& text);
|
||||||
|
|
||||||
static void init(struct mScriptTextBuffer*, const char* name);
|
static void init(struct mScriptTextBuffer*, const char* name);
|
||||||
static void deinit(struct mScriptTextBuffer*);
|
static void deinit(struct mScriptTextBuffer*);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user