Worked on the c++ model

This commit is contained in:
Scharel Clemens 2019-05-13 23:32:37 +02:00
parent 52147548cc
commit 66b56d0ed2
5 changed files with 93 additions and 121 deletions

View file

@ -6,8 +6,16 @@ import "../js/showdown/dist/showdown.js" as ShowDown
Dialog {
id: noteDialog
property var note
property int noteID
property int id
property int modified
property string title
property string category
property string content
property bool favorite
property string etag
property bool error
property string errorMessage
property string date
property var showdown: ShowDown.showdown
property var converter: new showdown.Converter(
@ -30,27 +38,26 @@ Dialog {
}
onStatusChanged: {
if (status === DialogStatus.Opened) {
api.getNoteFromApi(noteID)
api.getNoteFromApi(id)
}
}
Component.onCompleted: {
noteID = note.id
parseContent()
}
function reloadContent() {
api.getNoteFromApi(note.id)
/*note = api.getNote(note.id)
dialogHeader.title = note.title
favoriteButton.selected = note.favorite
categoryField.text = note.category
modifiedDetail.modified = note.modified
api.getNoteFromApi(id)
/*note = api.getNote(id)
dialogHeader.title = title
favoriteButton.selected = favorite
categoryField.text = category
modifiedDetail.modified = modified
parseContent()*/
}
function parseContent() {
//note = api.getNoteFromApi(note.id, false)
var convertedText = converter.makeHtml(note.content)
//note = api.getNoteFromApi(id, false)
var convertedText = converter.makeHtml(content)
var occurence = -1
convertedText = convertedText.replace(/^<li>(<p>)?\[ \] (.*)(<.*)$/gmi,
function(match, p1, p2, p3, offset) {
@ -93,7 +100,7 @@ Dialog {
MenuItem {
text: qsTr("Delete")
onClicked: remorse.execute("Deleting", function() { api.deleteNote(note.id) } )
onClicked: remorse.execute("Deleting", function() { api.deleteNote(id) } )
}
MenuItem {
text: enabled ? qsTr("Reload") : qsTr("Updating...")
@ -111,7 +118,7 @@ Dialog {
DialogHeader {
id: dialogHeader
title: note.title
title: title
acceptText: qsTr("Edit")
cancelText: qsTr("Notes")
}
@ -143,7 +150,7 @@ Dialog {
onLinkActivated: {
//console.log(link)
var occurence = -1
var newContent = note.content
var newContent = content
if (/^tasklist:checkbox_(\d+)$/m.test(link)) {
newContent = newContent.replace(/- \[ \] (.*)$/gm,
function(match, p1, offset, string) {
@ -152,9 +159,9 @@ Dialog {
return (appSettings.useCapitalX ? '- [X] ' : '- [x] ') + p1 }
else { return match }
} )
note.content = newContent
content = newContent
parseContent()
api.updateNote(note.id, { 'content': note.content } )
api.updateNote(id, { 'content': content } )
}
else if (/^tasklist:uncheckbox_(\d+)$/m.test(link)) {
newContent = newContent.replace(/- \[[xX]\] (.*)$/gm,
@ -164,9 +171,9 @@ Dialog {
return '- [ ] ' + p1 }
else { return match }
} )
note.content = newContent
content = newContent
parseContent()
api.updateNote(note.id, { 'content': note.content } )
api.updateNote(id, { 'content': content } )
}
else {
Qt.openUrlExternally(link)
@ -221,18 +228,18 @@ Dialog {
width: parent.width - x
IconButton {
id: favoriteButton
property bool selected: note.favorite
property bool selected: favorite
width: Theme.iconSizeMedium
icon.source: (selected ? "image://theme/icon-m-favorite-selected?" : "image://theme/icon-m-favorite?") +
(favoriteButton.highlighted ? Theme.secondaryHighlightColor : Theme.secondaryColor)
onClicked: {
api.updateNote(note.id, {'favorite': !note.favorite})
api.updateNote(id, {'favorite': !favorite})
}
}
TextField {
id: categoryField
width: parent.width - favoriteButton.width
text: note.category
text: category
placeholderText: qsTr("No category")
label: qsTr("Category")
EnterKey.iconSource: "image://theme/icon-m-enter-accept"
@ -240,8 +247,8 @@ Dialog {
categoryField.focus = false
}
onFocusChanged: {
if (focus === false && text !== note.category) {
api.updateNote(note.id, {'content': note.content, 'category': text}) // This does not seem to work without adding the content
if (focus === false && text !== category) {
api.updateNote(id, {'content': content, 'category': text}) // This does not seem to work without adding the content
}
}
}
@ -250,7 +257,7 @@ Dialog {
DetailItem {
id: modifiedDetail
label: qsTr("Modified")
property int modified: note.modified
property int modified: modified
value: new Date(modified * 1000).toLocaleString(Qt.locale(), Locale.ShortFormat)
}
}

View file

@ -103,7 +103,17 @@ Page {
}
onClicked: pageStack.push(Qt.resolvedUrl("../pages/NotePage.qml"),
{ note: notesList.model.get(index) })
{ id: id,
modified: modified,
title: title,
category: category,
content: content,
favorite: favorite,
etag: etag,
error: error,
errorMessage: errorMessage,
date: date
})
onPressAndHold: menu.open(note)
Separator {

View file

@ -57,99 +57,32 @@ void NotesModel::clearSearch() {
search();
}
bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
bool NotesModel::applyJSON(const QString &json) {
qDebug() << "Applying new JSON input";// << json;
uint notesModified = 0;
QJsonParseError error;
QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8(), &error);
if (!jdoc.isNull() && error.error == QJsonParseError::NoError) {
if (jdoc.isArray()) {
qDebug() << "- It's an array...";
QJsonArray jarr = jdoc.array();
QList<int> notesToRemove;
for (int i = 0; i < m_notes.size(); i++)
notesToRemove << i;
while (!jarr.empty()) {
//qDebug() << jarr.count() << "JSON Objects to handle...";
QJsonValue jval = jarr.first();
if (jval.isObject()) {
//qDebug() << "It's an object, all fine...";
QJsonObject jobj = jval.toObject();
if (!jobj.isEmpty()) {
//qDebug() << "Adding it to the model...";
Note note = Note::fromjson(jobj); // TODO connect signals
if (!note.error()) {
int oldPosition = indexOf(note.id());
Note oldNote = get(oldPosition);
if (oldPosition >= 0 && note.etag() != oldNote.etag() && replaceIfArray) {
removeNote(note.id());
insertNote(note);
}
else if (oldPosition) { //TODO
}
//qDebug() << "New note" << note.title << "adding it to the notes to add...";
int position = insertPosition(note);
insertNote(note);
//qDebug() << "Adding note"<< note.title << "on position" << position;
//beginInsertRows(QModelIndex(), position, position);
//m_notes.insert(position, note);
//endInsertRows();
}
else {
qDebug() << "Note contains an error:" << note.errorMessage();
}
}
else {
qDebug() << "Unknown JSON object. This message should never occure!";
}
insertJnote(jval.toObject());
}
jarr.pop_front();
}
// TODO the current implementation does not respect the changement of the index
/*for (int i = 0; i < notesToRemove.size(); i++) {
qDebug() << "Removing note" << m_notes[notesToRemove[i]].note.title;
beginRemoveRows(QModelIndex(), notesToRemove[i], notesToRemove[i]);
m_notes.removeAt(notesToRemove[i]);
endRemoveRows();
}*/
/*for (int i = 0; i < notesToAdd.size(); i++) {
beginInsertRows(QModelIndex(), notesToAdd[i].param, notesToAdd[i].param);
ModelNote<Note, bool> note;
note.note = notesToAdd[i].note;
qDebug() << "Adding note"<< note.note.title << "on position" << notesToAdd[i].param;
m_notes.insert(notesToAdd[i].param, note);
endInsertRows();
}*/
}
else if (jdoc.isObject()) {
qDebug() << "It's a single object...";
QJsonObject jobj = jdoc.object();
if (!jobj.isEmpty() && !jobj.value(roleNames()[ErrorRole]).toBool(true)) {
Note note = Note::fromjson(jobj); // TODO connect signals
int position = indexOf(note.id());
if (position >= 0 && replaceIfArray) {
m_notes[position].note = note;
}
else {
position = insertPosition(note);
ModelNote<Note, bool> noteToInsert;
noteToInsert.note = note; noteToInsert.param = true;
beginInsertRows(index(position), position, position);
m_notes.insert(position, noteToInsert);
endInsertRows();
}
notesModified++;
}
insertJnote(jdoc.object());
}
else {
qDebug() << "Unknown JSON document. This message should never occure!";
return false;
}
if (notesModified > 0) {
sort(); // TODO react to signal connect()
search(m_searchText);
}
return true;
}
else
{
@ -158,7 +91,7 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
return error.error == QJsonParseError::NoError;
}
int NotesModel::insertNote(Note &note) {
int NotesModel::insertNote(const Note &note) {
int position = insertPosition(note);
ModelNote<Note, bool> modelNote;
modelNote.note = note;
@ -392,6 +325,27 @@ void NotesModel::sort() {
}
}
void NotesModel::insertJnote(const QJsonObject &jobj) {
if (!jobj.isEmpty()) {
//qDebug() << "Adding it to the model...";
Note note = Note::fromjson(jobj); // TODO connect signals
if (!note.error()) {
int oldPosition = indexOf(note.id());
Note oldNote = get(oldPosition);
if (note.etag() != oldNote.etag()) {
removeNote(note.id());
}
insertNote(note);
}
else {
qDebug() << "Note contains an error:" << note.errorMessage();
}
}
else {
qDebug() << "Unknown JSON object. This message should never occure!";
}
}
int NotesModel::insertPosition(const Note &n) const {
int lower = 0;
int upper = m_notes.size();

View file

@ -32,8 +32,8 @@ public:
Q_INVOKABLE void search(QString searchText = QString());
Q_INVOKABLE void clearSearch();
Q_INVOKABLE bool applyJSON(QString json, bool replaceIfArray = true);
Q_INVOKABLE int insertNote(Note &note);
Q_INVOKABLE bool applyJSON(const QString &json);
Q_INVOKABLE int insertNote(const Note &note);
Q_INVOKABLE bool removeAt(int position);
Q_INVOKABLE bool removeNote(int id);
Q_INVOKABLE void clear();
@ -94,6 +94,7 @@ private:
void sort();
//void update();
void insertJnote(const QJsonObject &jobj);
int insertPosition(const Note &n) const;
bool noteLessThan(const Note &n1, const Note &n2) const;
/*static bool noteLessThanByDate(const Note &n1, const Note &n2);

View file

@ -232,52 +232,52 @@
<context>
<name>NotePage</name>
<message>
<location filename="../qml/pages/NotePage.qml" line="95"/>
<location filename="../qml/pages/NotePage.qml" line="102"/>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="99"/>
<location filename="../qml/pages/NotePage.qml" line="106"/>
<source>Reload</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="99"/>
<location filename="../qml/pages/NotePage.qml" line="106"/>
<source>Updating...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="105"/>
<location filename="../qml/pages/NotePage.qml" line="112"/>
<source>Last update</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="108"/>
<location filename="../qml/pages/NotePage.qml" line="115"/>
<source>never</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="115"/>
<location filename="../qml/pages/NotePage.qml" line="122"/>
<source>Edit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="116"/>
<location filename="../qml/pages/NotePage.qml" line="123"/>
<source>Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="236"/>
<location filename="../qml/pages/NotePage.qml" line="243"/>
<source>No category</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="237"/>
<location filename="../qml/pages/NotePage.qml" line="244"/>
<source>Category</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="252"/>
<location filename="../qml/pages/NotePage.qml" line="259"/>
<source>Modified</source>
<translation type="unfinished"></translation>
</message>
@ -328,57 +328,57 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="187"/>
<location filename="../qml/pages/NotesPage.qml" line="197"/>
<source>Modified</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="190"/>
<location filename="../qml/pages/NotesPage.qml" line="200"/>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="192"/>
<location filename="../qml/pages/NotesPage.qml" line="202"/>
<source>Deleting note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="217"/>
<location filename="../qml/pages/NotesPage.qml" line="227"/>
<source>No account yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="218"/>
<location filename="../qml/pages/NotesPage.qml" line="228"/>
<source>Got to the settings to add an account</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="224"/>
<location filename="../qml/pages/NotesPage.qml" line="234"/>
<source>No notes yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="225"/>
<location filename="../qml/pages/NotesPage.qml" line="235"/>
<source>Pull down to add a note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="231"/>
<location filename="../qml/pages/NotesPage.qml" line="241"/>
<source>No result</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="232"/>
<location filename="../qml/pages/NotesPage.qml" line="242"/>
<source>Try another query</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="238"/>
<location filename="../qml/pages/NotesPage.qml" line="248"/>
<source>An error occurred</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="249"/>
<location filename="../qml/pages/NotesPage.qml" line="259"/>
<source>Open the settings to configure your Nextcloud accounts</source>
<translation type="unfinished"></translation>
</message>