Skip to content

Commit

Permalink
saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Sep 29, 2024
1 parent 2f21b73 commit 6f0a72b
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 119 deletions.
22 changes: 11 additions & 11 deletions CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 14.0.1, 2024-09-17T00:00:06. -->
<!-- Written by QtCreator 14.0.1, 2024-09-23T15:38:14. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -102,14 +102,14 @@
<value type="int" key="CMake.Configure.BaseEnvironment">2</value>
<value type="bool" key="CMake.Configure.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMake.Configure.UserEnvironmentChanges"/>
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake
-DCMAKE_GENERATOR:STRING=Ninja
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}</value>
-DCMAKE_GENERATOR:STRING=Ninja</value>
<value type="QString" key="CMake.Source.Directory">/data/Code/Qt/Notepad--</value>
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/data/Code/Qt/Notepad--/build/Desktop-Debug</value>
Expand Down Expand Up @@ -160,14 +160,14 @@
<value type="int" key="CMake.Configure.BaseEnvironment">2</value>
<value type="bool" key="CMake.Configure.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMake.Configure.UserEnvironmentChanges"/>
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake
-DCMAKE_GENERATOR:STRING=Ninja
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}</value>
-DCMAKE_GENERATOR:STRING=Ninja</value>
<value type="QString" key="CMake.Source.Directory">/data/Code/Qt/Notepad--</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/data/Code/Qt/Notepad--/build/Desktop-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
Expand Down
141 changes: 93 additions & 48 deletions src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Document::Document(const QString &filePath, QWidget *parent)
connect(m_fileLoaderWorker, &FileLoaderWorker::errorOccurred, this, &Document::onLoadingError);
connect(m_fileLoaderWorker, &FileLoaderWorker::contentLoaded, this, &Document::insertContentIntoEditor);
connect(m_fileLoaderWorker, &FileLoaderWorker::contentLoaded, this, &Document::onContentLoaded);
connect(m_fileLoaderWorker, &FileLoaderWorker::loadingProgress, this, &Document::onLoadingProgress, Qt::UniqueConnection);
connect(m_fileLoaderWorker, &FileLoaderWorker::fileSizeDetermined, this, &Document::onFileSizeDetermined);

connect(m_fileLoaderWorker, &FileLoaderWorker::savingStarted, this, &Document::onSavingStarted);
connect(m_fileLoaderWorker, &FileLoaderWorker::savingProgress, this, &Document::onSavingProgress);
connect(m_fileLoaderWorker, &FileLoaderWorker::savingFinished, this, &Document::onSavingFinished);
connect(this, &Document::savingProgress, m_progressBar, &QProgressBar::setValue);

connect(m_fileLoaderWorker, &FileLoaderWorker::loadingProgress, this, [this](int progress) {
QMetaObject::invokeMethod(m_progressBar, "setValue", Qt::QueuedConnection, Q_ARG(int, progress));
});

connect(m_fileLoaderWorker, &FileLoaderWorker::loadingFinished, this, &Document::onLoadingFinished);
connect(m_fileLoaderWorker, &FileLoaderWorker::savingFinished, this, &Document::onSavingFinished);
Expand All @@ -65,6 +76,7 @@ Document::Document(const QString &filePath, QWidget *parent)
// Connect signals for progress updates
connect(this, &Document::hideProgressBar, this, [this]() {
m_progressBar->setVisible(false); // Hide progress bar when loading/saving is done
m_statusLabel->setText("");
});

qDebug() << "Document and thread initialized for file:" << m_filePath;
Expand Down Expand Up @@ -96,36 +108,6 @@ Document::~Document() {
qDebug() << "Document destructor called, all resources cleaned up.";
}

void Document::onContentLoaded(const QString &chunk) {
// Clear the editor when starting to load the file (only if m_totalBytesRead is 0, indicating it's the first chunk)
if (m_totalBytesRead == 0) {
qDebug() << "Starting new file load, clearing editor content.";
editor->clear();
m_bufferedContent.clear(); // Clear the buffered content for new file
}

// Append the chunk to the buffered content
m_bufferedContent += chunk;

// Move the cursor to the end before inserting the new chunk
editor->moveCursor(QTextCursor::End);
editor->insertPlainText(chunk);
qDebug() << "Inserted chunk of size:" << chunk.size() << " into the editor. Total bytes read so far: " << m_totalBytesRead;

// Update the total bytes read
m_totalBytesRead += chunk.size();

// Emit progress updates every 1MB to avoid frequent UI updates
if (m_totalBytesRead % (1024 * 1024) == 0) {
emit loadingProgress(m_totalBytesRead);
}

// Emit loading finished if the whole file is loaded
if (m_totalBytesRead == m_fileSize) {
emit loadingFinished();
}
}

void Document::onLoadingStarted() {
qDebug() << "Loading started for document:" << m_filePath;
m_statusLabel->setText("Loading File...");
Expand All @@ -134,29 +116,48 @@ void Document::onLoadingStarted() {
m_statusLabel->setVisible(true);
}

void Document::onSavingStarted() {
m_isSaving = true;
m_statusLabel->setText("Saving File...");
m_progressBar->setVisible(true);
m_progressBar->setValue(0);
}

void Document::onLoadingProgress(int progress) {
qDebug() << "Progress updated:" << progress;
if (progress - m_lastSmoothedProgress >= m_smoothProgressUpdateInterval || progress == 100) {
m_lastSmoothedProgress = progress;
m_progressBar->setValue(progress);

qDebug() << "Smooth progress updated to:" << progress;
}

if (progress == 100) {
emit hideProgressBar();
}
}

void Document::onSavingProgress(int progress) {
qDebug() << "Saving progress updated:" << progress;
m_progressBar->setValue(progress);

if (progress == 100) {
emit hideProgressBar();
}
}

void Document::onLoadingFinished() {
m_isLoading = false;
emit loadingProgress(100); // Ensure the progress is set to 100
emit loadingProgress(100);
emit hideProgressBar(); // Hide the progress bar when loading finishes
qDebug() << "Loading finished, m_isLoading = " << m_isLoading;
}

void Document::onSavingFinished() {
m_isSaving = false;
emit savingFinished();
emit hideProgressBar();
qDebug() << "Saving finished, m_isSaving = " << m_isSaving;
m_statusLabel->setText(""); // Clear the label after saving
m_progressBar->setVisible(false); // Hide the progress bar when saving is finished
}


void Document::onLoadingError(const QString &error) {
qDebug() << "Error loading document:" << m_filePath << " Error:" << error;
m_statusLabel->setText("Error: " + error);
Expand Down Expand Up @@ -195,29 +196,21 @@ void Document::saveFile() {
return;
}

// Get file path and current content from the editor
QString filePath = m_filePath;
QString currentText = editor->toPlainText();
qDebug() << "Current text to be saved length: " << currentText.length();
qDebug() << "First 100 characters: " << currentText.left(100);

if (filePath.isEmpty()) {
qDebug() << "File path is empty. Save operation aborted.";
return;
}

// Log the file content and length for verification
qDebug() << "Saving file:" << filePath;
qDebug() << "File content length:" << currentText.length();
qDebug() << "File content (first 100 chars):" << currentText.left(100);

// Connect the savingFinished signal if not already connected
connect(m_fileLoaderWorker, &FileLoaderWorker::savingFinished, this, &Document::onSavingFinished, Qt::UniqueConnection);
// Reset progress bar and show it
m_progressBar->setValue(0);
m_progressBar->setVisible(true);
m_statusLabel->setText("Saving File...");

// Start the save operation in the worker thread
m_isSaving = true;
m_statusLabel->setText("Saving File...");
m_progressBar->setVisible(true);
m_fileLoaderWorker->saveFile(filePath, currentText);
qDebug() << "Save operation started for file:" << filePath;
}
Expand Down Expand Up @@ -366,3 +359,55 @@ void Document::saveDocument() {
// Call the worker to save the file content
m_fileLoaderWorker->saveFile(filePath, fileContent);
}

void Document::onFileSizeDetermined(qint64 fileSize) {
m_fileSize = fileSize;
qDebug() << "File size set in Document: " << m_fileSize;
}

void Document::onContentLoaded(const QString &chunk) {
if (m_fileSize <= 0) {
qDebug() << "onContentLoaded: Invalid file size!";
return;
}

m_totalBytesRead += chunk.size();

// Buffer more content before updating the editor
m_bufferedContent += chunk;

// Calculate a dynamic buffer size (e.g., 1% of file size or 1MB minimum)
qint64 dynamicBufferSize = std::max(m_fileSize / 100, static_cast<qint64>(1 * 1024 * 1024));

if (m_bufferedContent.size() >= dynamicBufferSize) {
editor->moveCursor(QTextCursor::End);
qDebug() << "Buffered content size before inserting: " << m_bufferedContent.size();

// Insert content with proper line breaks
editor->insertPlainText(m_bufferedContent);
m_bufferedContent.clear(); // Clear buffer after insertion

QCoreApplication::processEvents(); // Keep UI responsive
}

// Update progress bar
emit loadingProgress(static_cast<int>((m_totalBytesRead * 100) / m_fileSize));

// Handle loading finished
if (m_totalBytesRead >= m_fileSize) {
emit loadingFinished();

// Insert remaining buffered content
if (!m_bufferedContent.isEmpty()) {
editor->moveCursor(QTextCursor::End);
editor->insertPlainText(m_bufferedContent);
m_bufferedContent.clear();
}

// Move cursor to the first line after loading completes
editor->moveCursor(QTextCursor::Start);

// Clear status label after loading completes
m_statusLabel->setText("");
}
}
19 changes: 13 additions & 6 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,23 @@ class Document : public QWidget {
void loadingProgress(int progress);
void loadingFinished();
void hideProgressBar();

void savingStarted();
void savingProgress(int progress);
void savingFinished();
void savingProgress(int progress);

public slots:
void onLoadingStarted();
void onLoadingProgress(int progress);
void onLoadingFinished();
void onSavingFinished();
void onLoadingError(const QString &error);
void insertContentIntoEditor(const QString &content);
void onContentLoaded(const QString &content);
void onSavingStarted();

private slots:
void onContentLoaded(const QString &chunk);
void onFileSizeDetermined(qint64 fileSize);
void onSavingProgress(int progress);
void onSavingFinished();

private:
void loadContent();
Expand All @@ -65,8 +69,8 @@ public slots:
void loadEntireFile();
bool compareText(const QString &text1, const QString &text2);
void mirrorChangesToMemory(qint64 segmentStart, const QString &text);
QByteArray calculateMD5Stream(QFile *file);
QByteArray calculateModifiedMD5();
QString calculateMD5Stream(QFile *file);
QString calculateModifiedMD5();

bool m_isLoading;
QString m_bufferedContent;
Expand All @@ -87,5 +91,8 @@ public slots:
QMap<qint64, QString> m_changedSegments;
QString m_currentText;
QString m_language;
int m_lastProgress;
int m_lastSmoothedProgress = 0;
int m_smoothProgressUpdateInterval = 1;
};

Loading

0 comments on commit 6f0a72b

Please sign in to comment.