MCPcopy Create free account
hub / github.com/KDE/ghostwriter / loadFile

Method loadFile

src/documentmanager.cpp:718–793  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

716}
717
718bool 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());

Callers 5

MathJax.jsFile · 0.80
sre_browser.jsFile · 0.80
asciimath.jsFile · 0.80
openFileAtMethod · 0.80
reloadMethod · 0.80

Calls 12

openMethod · 0.80
closeMethod · 0.80
setPlainTextMethod · 0.80
navigateDocumentMethod · 0.80
cursorPositionMethod · 0.80
isWriteableMethod · 0.80
setTimestampMethod · 0.80
clearMethod · 0.65
filePathMethod · 0.45
setEncodingMethod · 0.45
errorMethod · 0.45
setReadOnlyMethod · 0.45

Tested by

no test coverage detected