diff --git a/editor/deepin-editor.pro b/editor/deepin-editor.pro index 5de34080..76aa1a5a 100644 --- a/editor/deepin-editor.pro +++ b/editor/deepin-editor.pro @@ -27,7 +27,8 @@ HEADERS += src/window.h \ src/themeview.h \ src/themeitem.h \ src/uncommentselection.h \ - src/utils.h + src/utils.h \ + src/fileloadthread.h SOURCES += src/window.cpp \ src/startmanager.cpp \ @@ -46,7 +47,8 @@ SOURCES += src/window.cpp \ src/themeview.cpp \ src/themeitem.cpp \ src/uncommentselection.cpp \ - src/main.cpp + src/main.cpp \ + src/fileloadthread.cpp QT += KSyntaxHighlighting QT += core diff --git a/editor/src/editor.cpp b/editor/src/editor.cpp index 821fde3a..faeb74d1 100644 --- a/editor/src/editor.cpp +++ b/editor/src/editor.cpp @@ -23,6 +23,7 @@ #include "editor.h" #include "utils.h" +#include "fileloadthread.h" #include #include @@ -48,34 +49,22 @@ Editor::Editor(QWidget *parent) void Editor::loadFile(const QString &filepath) { - QFile file(filepath); - - if (file.open(QFile::ReadOnly)) { - // set mouse status to wait. - QApplication::setOverrideCursor(Qt::WaitCursor); - - // reads all remaining data from the file. - QByteArray fileContent = file.readAll(); - - // read the encode. - m_fileEncode = Utils::getFileEncode(fileContent, filepath); - - QTextStream stream(&fileContent); - stream.setCodec(m_fileEncode); - textEditor->setPlainText(stream.readAll()); - - // restore mouse style. - QApplication::restoreOverrideCursor(); + // set mouse status to wait. + QApplication::setOverrideCursor(Qt::WaitCursor); - updatePath(filepath); - detectNewline(); + // update file path. + updatePath(filepath); - qDebug() << QString("Detect file %1 with encoding: %2, line endings: %3").arg(filepath).arg(QString(m_fileEncode)).arg(m_newline); + // begin to load the file. + FileLoadThread *thread = new FileLoadThread(filepath); + connect(thread, &FileLoadThread::loadFinished, this, &Editor::handleFileLoadFinished); + connect(thread, &FileLoadThread::finished, thread, &FileLoadThread::deleteLater); - textEditor->loadHighlighter(); - } + // editing is not allowed during loading. + textEditor->setReadOnly(true); - file.close(); + // start the thread. + thread->start(); } bool Editor::saveFile(const QString &encode, const QString &newline) @@ -176,3 +165,20 @@ void Editor::detectNewline() file.close(); } + +void Editor::handleFileLoadFinished(const QString &encode, QTextDocument *doc) +{ + QPlainTextDocumentLayout *layout = new QPlainTextDocumentLayout(doc); + doc->setDocumentLayout(layout); + textEditor->setDocument(doc); + + // restore mouse style. + QApplication::restoreOverrideCursor(); + + + // update status. + textEditor->setReadOnly(false); + textEditor->setModified(false); + + // textEditor->loadHighlighter(); +} diff --git a/editor/src/editor.h b/editor/src/editor.h index 9cad456b..44c647d4 100644 --- a/editor/src/editor.h +++ b/editor/src/editor.h @@ -47,6 +47,7 @@ class Editor : public QWidget private: void detectNewline(); + void handleFileLoadFinished(const QString &encode, QTextDocument *doc); private: QHBoxLayout *m_layout; diff --git a/editor/src/fileloadthread.cpp b/editor/src/fileloadthread.cpp new file mode 100644 index 00000000..cacb7425 --- /dev/null +++ b/editor/src/fileloadthread.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: rekols + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fileloadthread.h" +#include "utils.h" + +#include +#include +#include +#include +#include + +FileLoadThread::FileLoadThread(const QString &filepath, QObject *parent) + : QThread(parent), + m_filePath(filepath) +{ + +} + +FileLoadThread::~FileLoadThread() +{ +} + +void FileLoadThread::run() +{ + QFile file(m_filePath); + + if (file.open(QIODevice::ReadOnly)) { + // reads all remaining data from the file. + QByteArray content = file.readAll(); + + // read the encode. + QByteArray encode = Utils::getFileEncode(content, m_filePath); + + QTextStream stream(&content); + stream.setCodec(encode); + + QTextDocument *doc = new QTextDocument; + doc->moveToThread(QCoreApplication::instance()->thread()); + doc->setPlainText(content); + + emit loadFinished(encode, doc); + } +} diff --git a/editor/src/fileloadthread.h b/editor/src/fileloadthread.h new file mode 100644 index 00000000..5b6cabbc --- /dev/null +++ b/editor/src/fileloadthread.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: rekols + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef FILELOADTHREAD_H +#define FILELOADTHREAD_H + +#include + +class QTextDocument; +class FileLoadThread : public QThread +{ + Q_OBJECT + +public: + FileLoadThread(const QString &filepath, QObject *QObject = nullptr); + ~FileLoadThread(); + + void run(); + +signals: + void loadFinished(const QString &encode, QTextDocument *doc); + +private: + QString m_filePath; +}; + +#endif diff --git a/editor/src/texteditor.cpp b/editor/src/texteditor.cpp index 5d520a29..24aadd86 100644 --- a/editor/src/texteditor.cpp +++ b/editor/src/texteditor.cpp @@ -1990,8 +1990,8 @@ void TextEditor::setTheme(const KSyntaxHighlighting::Theme &theme, QString theme m_selectionColor = QColor(jsonMap["editor-colors"].toMap()["selection"].toString()); m_regionMarkerColor = QColor(theme.textColor(KSyntaxHighlighting::Theme::RegionMarker)); - m_highlighter->setTheme(theme); - m_highlighter->rehighlight(); + // m_highlighter->setTheme(theme); + // m_highlighter->rehighlight(); lineNumberArea->update(); highlightCurrentLine(); diff --git a/editor/src/window.cpp b/editor/src/window.cpp index 735fc485..303651a3 100644 --- a/editor/src/window.cpp +++ b/editor/src/window.cpp @@ -64,8 +64,6 @@ Window::Window(DMainWindow *parent) m_themeName(m_settings->settings->option("base.theme.default")->value().toString()) { m_blankFileDir = QDir(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first()).filePath("blank-files"); - m_readonlyFileDir = QDir(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first()).filePath("readonly-files"); - autoSaveDBus = new DBusDaemon::dbus("com.deepin.editor.daemon", "/", QDBusConnection::systemBus(), this); // Init. installEventFilter(this); diff --git a/editor/src/window.h b/editor/src/window.h index 44061c0a..be458ec4 100644 --- a/editor/src/window.h +++ b/editor/src/window.h @@ -185,7 +185,6 @@ public slots: int m_remberPositionScrollOffset; QString m_blankFileDir; - QString m_readonlyFileDir; int m_fontSize; QString m_titlebarStyleSheet;