Skip to content

Commit

Permalink
Improve data flow (#27)
Browse files Browse the repository at this point in the history
* Add an editor auto save timer and make the document class a bit cleaner
* Fix QTextDocument init and don't save empty docs
* Add memory cleanup and rename some private variables
  • Loading branch information
vkbo authored Jan 30, 2022
1 parent b8eabe4 commit 29b2e57
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 90 deletions.
6 changes: 5 additions & 1 deletion sample/content/e709ba3f-3141-4b4b-95df-4a8d3e91a8ba.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"m:created": "2022-01-30T18:54:49",
"m:updated": "2022-01-30T18:58:53",
"m:updated": "2022-01-30T21:17:06",
"x:content": [
{
"u:fmt": "p:ac",
"u:txt": "t|My Novel"
},
{
"u:fmt": "p:ac",
"u:txt": "t|"
}
]
}
4 changes: 2 additions & 2 deletions sample/project/project.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"c:meta": {
"m:created": "2021-12-14T22:24:25",
"m:updated": "2022-01-30T18:58:56"
"m:updated": "2022-01-30T21:17:06"
},
"c:project": {
"s:last-doc-main": "7e5a1a98-d1a3-44a1-ab4e-2b5d21d92201",
"s:last-doc-main": "e709ba3f-3141-4b4b-95df-4a8d3e91a8ba",
"u:project-name": "Sample Project"
},
"c:settings": {
Expand Down
48 changes: 42 additions & 6 deletions src/editor/doceditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "collett.h"
#include "settings.h"
#include "doceditor.h"
#include "textedit.h"
#include "document.h"
Expand All @@ -28,6 +29,7 @@
#include <QFont>
#include <QTime>
#include <QUuid>
#include <QTimer>
#include <QObject>
#include <QWidget>
#include <QJsonObject>
Expand All @@ -44,6 +46,8 @@ GuiDocEditor::GuiDocEditor(QWidget *parent)
m_document = nullptr;

m_textArea = new GuiTextEdit(this);
m_textArea->setReadOnly(true);

m_editToolBar = new GuiEditToolBar(this);

QVBoxLayout *outerBox = new QVBoxLayout;
Expand All @@ -54,6 +58,12 @@ GuiDocEditor::GuiDocEditor(QWidget *parent)

this->setLayout(outerBox);

CollettSettings *settings = CollettSettings::instance();

// Timers
m_autoSave = new QTimer(this);
m_autoSave->setInterval(settings->editorAutoSave() * 1000);

// Connections

connect(m_editToolBar, SIGNAL(documentAction(DocAction)),
Expand All @@ -62,6 +72,8 @@ GuiDocEditor::GuiDocEditor(QWidget *parent)
this, SLOT(editorCharFormatChanged(const QTextCharFormat&)));
connect(m_textArea, SIGNAL(currentBlockChanged(const QTextBlock&)),
this, SLOT(editorBlockChanged(const QTextBlock&)));
connect(m_autoSave, SIGNAL(timeout()),
this, SLOT(flushEditorData()));
}

/**
Expand All @@ -80,6 +92,9 @@ bool GuiDocEditor::openDocument(const QUuid &uuid) {
m_document = m_data->project()->document(uuid);
m_textArea->setJsonContent(m_document->content());

m_autoSave->start();
m_textArea->setReadOnly(false);

return true;
}

Expand All @@ -89,19 +104,21 @@ bool GuiDocEditor::saveDocument() {
qWarning() << "No project loaded";
return false;
}
if (m_docUuid.isNull()) {
if (!hasDocument()) {
qWarning() << "No document to save";
return false;
}
QTime startTime = QTime::currentTime();
m_document->save(m_textArea->toJsonContent());
QTime endTime = QTime::currentTime();
qDebug() << "Save file took (ms):" << startTime.msecsTo(endTime);

m_document->setLocked(true);
m_document->setContent(m_textArea->toJsonContent());
m_document->setLocked(false);

return true;
}

void GuiDocEditor::closeDocument() {
m_autoSave->stop();
m_textArea->setReadOnly(true);
m_textArea->clear();
m_docUuid = QUuid();
m_document = nullptr;
Expand All @@ -117,7 +134,7 @@ QUuid GuiDocEditor::currentDocument() const {
}

bool GuiDocEditor::hasDocument() const {
return m_document != nullptr;
return m_document != nullptr && !m_docUuid.isNull();
}

/**
Expand All @@ -141,4 +158,23 @@ void GuiDocEditor::editorBlockChanged(const QTextBlock &block) {
m_editToolBar->m_textIndent->setChecked(blockFormat.textIndent() > 0.0);
}

void GuiDocEditor::flushEditorData() {

qDebug() << "Ding!";

if (!m_data->hasProject() || !hasDocument()) {
return;
}

if (m_textArea->isModified()) {
qDebug() << "Autosaving editor content";
m_document->setLocked(true);
m_document->setContent(m_textArea->toJsonContent());
m_document->setLocked(false);
if (m_document->write()) {
m_textArea->setModified(false);
}
}
}

} // namespace Collett
3 changes: 3 additions & 0 deletions src/editor/doceditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "edittoolbar.h"

#include <QUuid>
#include <QTimer>
#include <QObject>
#include <QWidget>
#include <QTextBlock>
Expand Down Expand Up @@ -61,6 +62,7 @@ class GuiDocEditor : public QWidget
private:
GuiTextEdit *m_textArea;
GuiEditToolBar *m_editToolBar;
QTimer *m_autoSave;

CollettData *m_data;
Document *m_document;
Expand All @@ -69,6 +71,7 @@ class GuiDocEditor : public QWidget
private slots:
void editorCharFormatChanged(const QTextCharFormat &fmt);
void editorBlockChanged(const QTextBlock &block);
void flushEditorData();

};
} // namespace Collett
Expand Down
55 changes: 46 additions & 9 deletions src/editor/textedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ GuiTextEdit::GuiTextEdit(QWidget *parent)
{
// Settings
setAcceptRichText(true);

// Text Options
QTextOption opts;
opts.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
document()->setDefaultTextOption(opts);
document()->setDocumentMargin(40);
initDocument(this->document());

CollettSettings *settings = CollettSettings::instance();
m_format = settings->textFormat();
Expand All @@ -61,14 +56,37 @@ GuiTextEdit::GuiTextEdit(QWidget *parent)
}

/**
* Methods
* =======
* Class Setters
* =============
*/

void GuiTextEdit::setModified(bool state) {
this->document()->setModified(state);
}

/**
* Class Getters
* =============
*/

bool GuiTextEdit::isModified() const {
return this->document()->isModified();
}

/**
* Class Methods
* =============
*/

QJsonArray GuiTextEdit::toJsonContent() {

QJsonArray json;

if (this->document()->blockCount() == 1 && this->document()->firstBlock().text().trimmed().isEmpty()) {
// No text content
return json;
}

QTextBlock block = this->document()->firstBlock();
while(block.isValid()) {
QJsonObject jsonBlock;
Expand Down Expand Up @@ -148,12 +166,13 @@ QJsonArray GuiTextEdit::toJsonContent() {

void GuiTextEdit::setJsonContent(const QJsonArray &json) {

QTextDocument *doc = this->document();
QTextDocument *doc = new QTextDocument(this);
QTextCursor cursor = QTextCursor(doc);
bool isFirst = true;

doc->setUndoRedoEnabled(false);
doc->clear();
initDocument(doc);

for (const QJsonValue &jsonBlockValue : json) {

Expand Down Expand Up @@ -264,6 +283,24 @@ void GuiTextEdit::setJsonContent(const QJsonArray &json) {
}

doc->setUndoRedoEnabled(true);
doc->setModified(false);

this->setDocument(doc);
}

/**
* Internal Functions
* ==================
*/

void GuiTextEdit::initDocument(QTextDocument *doc) {

// Text Options
QTextOption opts;
opts.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
doc->setDefaultTextOption(opts);
doc->setDocumentMargin(40);

}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/editor/textedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class GuiTextEdit : public QTextEdit
GuiTextEdit(QWidget *parent=nullptr);
~GuiTextEdit() {};

// Setters

void setModified(bool state);

// Getters

bool isModified() const;

// Methods

QJsonArray toJsonContent();
void setJsonContent(const QJsonArray &json);

Expand All @@ -52,6 +62,8 @@ class GuiTextEdit : public QTextEdit

int m_currentBlockNo = -1;

void initDocument(QTextDocument *doc);

signals:
void currentBlockChanged(const QTextBlock &block);

Expand Down
Loading

0 comments on commit 29b2e57

Please sign in to comment.