| 716 | } |
| 717 | |
| 718 | bool DocumentManagerPrivate::loadFile(const Bookmark &location) |
| 719 | { |
| 720 | Q_Q(DocumentManager); |
| 721 | |
| 722 | QFile inputFile(location.filePath()); |
| 723 | |
| 724 | if (!inputFile.open(QIODevice::ReadOnly)) { |
| 725 | MessageBoxHelper::critical(editor, DocumentManager::tr("Could not read %1").arg(location.filePath()), inputFile.errorString()); |
| 726 | |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | // NOTE: Must set editor's text cursor to the beginning |
| 731 | // of the document before clearing the document/editor |
| 732 | // of text to prevent a crash in Qt 5.10 on opening or |
| 733 | // reloading a file if a file has already been previously |
| 734 | // opened in the editor. |
| 735 | // |
| 736 | QTextCursor cursor(document); |
| 737 | cursor.setPosition(0); |
| 738 | editor->setTextCursor(cursor); |
| 739 | |
| 740 | document->clearUndoRedoStacks(); |
| 741 | document->setUndoRedoEnabled(false); |
| 742 | document->clear(); |
| 743 | |
| 744 | QApplication::setOverrideCursor(Qt::WaitCursor); |
| 745 | emit q->operationStarted(DocumentManager::tr("opening %1").arg(location.filePath())); |
| 746 | QTextStream inStream(&inputFile); |
| 747 | |
| 748 | // Markdown files need to be in UTF-8 format, so assume that is |
| 749 | // what the user is opening by default. Enable autodetection |
| 750 | // of of UTF-16 or UTF-32 BOM in case the file isn't UTF-8 encoded. |
| 751 | // |
| 752 | inStream.setEncoding(QStringConverter::Utf8); |
| 753 | inStream.setAutoDetectUnicode(true); |
| 754 | |
| 755 | QString text = inStream.readAll(); |
| 756 | |
| 757 | if (QFile::NoError != inputFile.error()) { |
| 758 | MessageBoxHelper::critical(editor, DocumentManager::tr("Could not read %1").arg(location.filePath()), inputFile.errorString()); |
| 759 | |
| 760 | inputFile.close(); |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | inputFile.close(); |
| 765 | |
| 766 | setFilePath(location.filePath()); |
| 767 | editor->setPlainText(text); |
| 768 | editor->navigateDocument(0); |
| 769 | emit q->operationUpdate(); |
| 770 | |
| 771 | document->setUndoRedoEnabled(true); |
| 772 | |
| 773 | editor->navigateDocument(location.cursorPosition()); |
| 774 | editor->setReadOnly(!location.isWriteable()); |
| 775 | document->setReadOnly(!location.isWriteable()); |
no test coverage detected