| 1034 | } |
| 1035 | |
| 1036 | void TeXDocumentWindow::loadFile(const QFileInfo & fileInfo, bool asTemplate, bool inBackground, bool reload, QTextCodec * forceCodec) |
| 1037 | { |
| 1038 | QString fileContents = readFile(fileInfo, &codec, &lineEndings, forceCodec); |
| 1039 | showLineEndingSetting(); |
| 1040 | showEncodingSetting(); |
| 1041 | |
| 1042 | if (fileContents.isNull()) |
| 1043 | return; |
| 1044 | bool identicalContent{fileContents == textEdit->text()}; |
| 1045 | |
| 1046 | // Only re-set the content if it has actually changed. Setting the content |
| 1047 | // has many side effects, e.g., destroying the undo/redo stack. |
| 1048 | if (!reload || !identicalContent) { |
| 1049 | QApplication::setOverrideCursor(Qt::WaitCursor); |
| 1050 | |
| 1051 | textEdit->setPlainText(fileContents); |
| 1052 | |
| 1053 | // Ensure the window is shown early (before setPlainText()). |
| 1054 | // - this ensures it is shown before the PDF (if opening a new doc) |
| 1055 | // - this avoids problems during layouting (which can be broken if the |
| 1056 | // geometry, highlighting, ... is changed before the window is shown) |
| 1057 | if (!reload) |
| 1058 | show(); |
| 1059 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
| 1060 | |
| 1061 | { |
| 1062 | // Try to work around QTBUG-20354 |
| 1063 | // It seems that adding additionalFormats (as is done automatically on |
| 1064 | // setPlainText() by the syntax highlighter) can disturb the layouting |
| 1065 | // process, leaving some blocks with size zero. This causes the |
| 1066 | // corresponding lines to "disappear" and can even crash the application |
| 1067 | // in connection with the "highlight current line" feature. |
| 1068 | QTextDocument * doc = textEdit->document(); |
| 1069 | Q_ASSERT(doc); |
| 1070 | QAbstractTextDocumentLayout * docLayout = doc->documentLayout(); |
| 1071 | Q_ASSERT(docLayout); |
| 1072 | |
| 1073 | int tries{0}; |
| 1074 | for (tries = 0; tries < 10; ++tries) { |
| 1075 | bool isLayoutOK = true; |
| 1076 | for (QTextBlock b = doc->firstBlock(); b.isValid(); b = b.next()) { |
| 1077 | if (docLayout-> blockBoundingRect(b).isEmpty()) { |
| 1078 | isLayoutOK = false; |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | if (isLayoutOK) break; |
| 1083 | // Re-setting the document content naturally triggers a relayout |
| 1084 | // (also a rehighlight). Note that layouting only works sensibly |
| 1085 | // once show() was called, or else there is no valid widget geometry |
| 1086 | // to act as bounding box. |
| 1087 | #if QT_VERSION < QT_VERSION_CHECK(5, 9, 0) |
| 1088 | doc->setPlainText(doc->toPlainText()); |
| 1089 | #else |
| 1090 | doc->setPlainText(doc->toRawText()); |
| 1091 | #endif |
| 1092 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
| 1093 | } |
nothing calls this directly
no test coverage detected