Skip to content

Commit

Permalink
feat: support multi-thraded read fiels.
Browse files Browse the repository at this point in the history
Change-Id: I50e15a805a1ba2e8f211d591f0ef5b51be21880e
  • Loading branch information
rekols committed Aug 28, 2018
1 parent 456230b commit d4de743
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 31 deletions.
6 changes: 4 additions & 2 deletions editor/deepin-editor.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
54 changes: 30 additions & 24 deletions editor/src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "editor.h"
#include "utils.h"
#include "fileloadthread.h"

#include <QApplication>
#include <QDebug>
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions editor/src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Editor : public QWidget

private:
void detectNewline();
void handleFileLoadFinished(const QString &encode, QTextDocument *doc);

private:
QHBoxLayout *m_layout;
Expand Down
60 changes: 60 additions & 0 deletions editor/src/fileloadthread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: rekols <rekols@foxmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include "fileloadthread.h"
#include "utils.h"

#include <QCoreApplication>
#include <QPlainTextEdit>
#include <QTextDocument>
#include <QTextStream>
#include <QFile>

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);
}
}
43 changes: 43 additions & 0 deletions editor/src/fileloadthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: rekols <rekols@foxmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef FILELOADTHREAD_H
#define FILELOADTHREAD_H

#include <QThread>

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
4 changes: 2 additions & 2 deletions editor/src/texteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions editor/src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion editor/src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ public slots:
int m_remberPositionScrollOffset;

QString m_blankFileDir;
QString m_readonlyFileDir;
int m_fontSize;

QString m_titlebarStyleSheet;
Expand Down

0 comments on commit d4de743

Please sign in to comment.