Just pushing to transport the current code to another computer

This commit is contained in:
Scharel Clemens 2019-05-13 18:39:45 +02:00
parent a841a96e76
commit 52147548cc
3 changed files with 110 additions and 90 deletions

View file

@ -74,29 +74,29 @@ public:
SearchAll = 0x7 SearchAll = 0x7
}; };
Q_DECLARE_FLAGS(SearchAttributes, SearchAttribute) Q_DECLARE_FLAGS(SearchAttributes, SearchAttribute)
static Note *fromjson(const QJsonObject& jobj) { static Note fromjson(const QJsonObject& jobj) {
Note *note = new Note; Note note = new Note;
note->setId(jobj.value("id").toInt()); note.setId(jobj.value("id").toInt());
note->setModified(jobj.value("modified").toInt()); note.setModified(jobj.value("modified").toInt());
note->setTitle(jobj.value("title").toString()); note.setTitle(jobj.value("title").toString());
note->setCategory(jobj.value("category").toString()); note.setCategory(jobj.value("category").toString());
note->setContent(jobj.value("content").toString()); note.setContent(jobj.value("content").toString());
note->setFavorite(jobj.value("favorite").toBool()); note.setFavorite(jobj.value("favorite").toBool());
note->setEtag(jobj.value("etag").toString()); note.setEtag(jobj.value("etag").toString());
note->setError(jobj.value("error").toBool(true)); note.setError(jobj.value("error").toBool(true));
note->setErrorMessage(jobj.value("errorMessage").toString()); note.setErrorMessage(jobj.value("errorMessage").toString());
return note; return note;
} }
static bool searchInNote(const QString &query, const Note *note, SearchAttributes criteria = QFlag(SearchAll), Qt::CaseSensitivity cs = Qt::CaseInsensitive) { static bool searchInNote(const QString &query, const Note &note, SearchAttributes criteria = QFlag(SearchAll), Qt::CaseSensitivity cs = Qt::CaseInsensitive) {
bool queryFound = false; bool queryFound = false;
if (criteria.testFlag(SearchInTitle)) { if (criteria.testFlag(SearchInTitle)) {
queryFound |= note->title().contains(query, cs); queryFound |= note.title().contains(query, cs);
} }
if (criteria.testFlag(SearchInContent)) { if (criteria.testFlag(SearchInContent)) {
queryFound |= note->content().contains(query, cs); queryFound |= note.content().contains(query, cs);
} }
if (criteria.testFlag(SearchInCategory)) { if (criteria.testFlag(SearchInCategory)) {
queryFound |= note->category().contains(query, cs); queryFound |= note.category().contains(query, cs);
} }
return queryFound; return queryFound;
} }

View file

@ -16,7 +16,7 @@ NotesModel::~NotesModel() {
} }
void NotesModel::setSortBy(QString sortBy) { void NotesModel::setSortBy(QString sortBy) {
qDebug() << "Setting sorting by:" << sortBy; qDebug() << "Sorting by:" << sortBy;
if (sortBy != m_sortBy && sortingNames().values().contains(sortBy.toLocal8Bit())) { if (sortBy != m_sortBy && sortingNames().values().contains(sortBy.toLocal8Bit())) {
m_sortBy = sortBy; m_sortBy = sortBy;
sort(); sort();
@ -25,7 +25,7 @@ void NotesModel::setSortBy(QString sortBy) {
} }
void NotesModel::setFavoritesOnTop(bool favoritesOnTop) { void NotesModel::setFavoritesOnTop(bool favoritesOnTop) {
qDebug() << "Setting favorites on top:" << favoritesOnTop; qDebug() << "Favorites on top:" << favoritesOnTop;
if (favoritesOnTop != m_favoritesOnTop) { if (favoritesOnTop != m_favoritesOnTop) {
m_favoritesOnTop = favoritesOnTop; m_favoritesOnTop = favoritesOnTop;
sort(); sort();
@ -54,7 +54,7 @@ void NotesModel::search(QString searchText) {
} }
void NotesModel::clearSearch() { void NotesModel::clearSearch() {
search(""); search();
} }
bool NotesModel::applyJSON(QString json, bool replaceIfArray) { bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
@ -64,7 +64,7 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8(), &error); QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8(), &error);
if (!jdoc.isNull() && error.error == QJsonParseError::NoError) { if (!jdoc.isNull() && error.error == QJsonParseError::NoError) {
if (jdoc.isArray()) { if (jdoc.isArray()) {
qDebug() << "It's an array..."; qDebug() << "- It's an array...";
QJsonArray jarr = jdoc.array(); QJsonArray jarr = jdoc.array();
QList<int> notesToRemove; QList<int> notesToRemove;
for (int i = 0; i < m_notes.size(); i++) for (int i = 0; i < m_notes.size(); i++)
@ -75,31 +75,33 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
if (jval.isObject()) { if (jval.isObject()) {
//qDebug() << "It's an object, all fine..."; //qDebug() << "It's an object, all fine...";
QJsonObject jobj = jval.toObject(); QJsonObject jobj = jval.toObject();
if (!jobj.isEmpty() && !jobj.value(roleNames()[ErrorRole]).toBool(true)) { if (!jobj.isEmpty()) {
//qDebug() << "Adding it to the model..."; //qDebug() << "Adding it to the model...";
Note* note = Note::fromjson(jobj); // TODO connect signals Note note = Note::fromjson(jobj); // TODO connect signals
int position = indexOf(note->id()); if (!note.error()) {
if (position >= 0 && replaceIfArray) { int oldPosition = indexOf(note.id());
//qDebug() << "Replacing note" << note.title << "on position" << position; Note oldNote = get(oldPosition);
m_notes[position].note = note; if (oldPosition >= 0 && note.etag() != oldNote.etag() && replaceIfArray) {
emit dataChanged(index(position), index(position)); removeNote(note.id());
notesToRemove.removeAt(position); insertNote(note);
delete 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 { else {
//qDebug() << "New note" << note.title << "adding it to the notes to add..."; qDebug() << "Note contains an error:" << note.errorMessage();
position = insertPosition(*note);
ModelNote<Note*, bool> noteToInsert;
noteToInsert.note = note; noteToInsert.param = true;
//qDebug() << "Adding note"<< note.title << "on position" << position;
beginInsertRows(QModelIndex(), position, position);
m_notes.insert(position, noteToInsert);
endInsertRows();
} }
notesModified++;
} }
else { else {
qDebug() << "Something is wrong, skipping it..."; qDebug() << "Unknown JSON object. This message should never occure!";
} }
} }
jarr.pop_front(); jarr.pop_front();
@ -124,15 +126,14 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
qDebug() << "It's a single object..."; qDebug() << "It's a single object...";
QJsonObject jobj = jdoc.object(); QJsonObject jobj = jdoc.object();
if (!jobj.isEmpty() && !jobj.value(roleNames()[ErrorRole]).toBool(true)) { if (!jobj.isEmpty() && !jobj.value(roleNames()[ErrorRole]).toBool(true)) {
Note* note = Note::fromjson(jobj); // TODO connect signals Note note = Note::fromjson(jobj); // TODO connect signals
int position = indexOf(note->id()); int position = indexOf(note.id());
if (position >= 0 && replaceIfArray) { if (position >= 0 && replaceIfArray) {
m_notes[position].note = note; m_notes[position].note = note;
delete note;
} }
else { else {
position = insertPosition(*note); position = insertPosition(note);
ModelNote<Note*, bool> noteToInsert; ModelNote<Note, bool> noteToInsert;
noteToInsert.note = note; noteToInsert.param = true; noteToInsert.note = note; noteToInsert.param = true;
beginInsertRows(index(position), position, position); beginInsertRows(index(position), position, position);
m_notes.insert(position, noteToInsert); m_notes.insert(position, noteToInsert);
@ -141,7 +142,9 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
notesModified++; notesModified++;
} }
} }
qDebug() << "Unknown JSON document. This message should never occure!"; else {
qDebug() << "Unknown JSON document. This message should never occure!";
}
if (notesModified > 0) { if (notesModified > 0) {
sort(); // TODO react to signal connect() sort(); // TODO react to signal connect()
search(m_searchText); search(m_searchText);
@ -155,15 +158,33 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
return error.error == QJsonParseError::NoError; return error.error == QJsonParseError::NoError;
} }
int NotesModel::insertNote(Note &note) {
int position = insertPosition(note);
ModelNote<Note, bool> modelNote;
modelNote.note = note;
modelNote.param = true;
beginInsertRows(QModelIndex(), position, position);
m_notes.insert(position, modelNote);
endInsertRows();
return position;
}
bool NotesModel::removeAt(int position){
if (position >= 0 && position < m_notes.size()) {
beginRemoveRows(QModelIndex(), position, position);
m_notes.removeAt(position);
endRemoveRows();
return true;
}
return false;
}
bool NotesModel::removeNote(int id) { bool NotesModel::removeNote(int id) {
bool noteRemoved = false; bool noteRemoved = false;
int index = indexOf(id); int position = indexOf(id);
while (index >= 0) { while (position >= 0) {
beginRemoveRows(QModelIndex(), index, index); noteRemoved |= removeAt(position);;
m_notes.removeAt(index); position = indexOf(id);
endRemoveRows();
noteRemoved = true;
index = indexOf(id);
} }
return noteRemoved; return noteRemoved;
} }
@ -171,23 +192,20 @@ bool NotesModel::removeNote(int id) {
void NotesModel::clear() { void NotesModel::clear() {
m_searchText.clear(); m_searchText.clear();
beginRemoveRows(QModelIndex(), 0, rowCount()); beginRemoveRows(QModelIndex(), 0, rowCount());
for (int i = 0; i < m_notes.size(); i++) {
delete m_notes[i].note; // TODO disconnect signals
}
m_notes.clear(); m_notes.clear();
endRemoveRows(); endRemoveRows();
} }
int NotesModel::indexOf(int id) const { int NotesModel::indexOf(int id) const {
for (int i = 0; i < m_notes.size(); i++) { for (int i = 0; i < m_notes.size(); i++) {
if (m_notes[i].note->id() == id) if (m_notes[i].note.id() == id)
return i; return i;
} }
return -1; return -1;
} }
Note* NotesModel::get(int index) const { Note NotesModel::get(int index) const {
Note* note = NULL; Note note;
if (index >= 0 && index < m_notes.size()) { if (index >= 0 && index < m_notes.size()) {
note = m_notes[index].note; note = m_notes[index].note;
} }
@ -262,16 +280,16 @@ int NotesModel::rowCount(const QModelIndex &parent) const {
QVariant NotesModel::data(const QModelIndex &index, int role) const { QVariant NotesModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant(); if (!index.isValid()) return QVariant();
else if (role == VisibleRole) return m_notes[index.row()].param; else if (role == VisibleRole) return m_notes[index.row()].param;
else if (role == IdRole) return m_notes[index.row()].note->id(); else if (role == IdRole) return m_notes[index.row()].note.id();
else if (role == ModifiedRole) return m_notes[index.row()].note->modified(); else if (role == ModifiedRole) return m_notes[index.row()].note.modified();
else if (role == TitleRole) return m_notes[index.row()].note->title(); else if (role == TitleRole) return m_notes[index.row()].note.title();
else if (role == CategoryRole) return m_notes[index.row()].note->category(); else if (role == CategoryRole) return m_notes[index.row()].note.category();
else if (role == ContentRole) return m_notes[index.row()].note->content(); else if (role == ContentRole) return m_notes[index.row()].note.content();
else if (role == FavoriteRole) return m_notes[index.row()].note->favorite(); else if (role == FavoriteRole) return m_notes[index.row()].note.favorite();
else if (role == EtagRole) return m_notes[index.row()].note->etag(); else if (role == EtagRole) return m_notes[index.row()].note.etag();
else if (role == ErrorRole) return m_notes[index.row()].note->error(); else if (role == ErrorRole) return m_notes[index.row()].note.error();
else if (role == ErrorMessageRole) return m_notes[index.row()].note->errorMessage(); else if (role == ErrorMessageRole) return m_notes[index.row()].note.errorMessage();
else if (role == DateStringRole) return m_notes[index.row()].note->dateString(); else if (role == DateStringRole) return m_notes[index.row()].note.dateString();
return QVariant(); return QVariant();
} }
@ -288,27 +306,27 @@ QMap<int, QVariant> NotesModel::itemData(const QModelIndex &index) const {
bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) { bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid()) return false; if (!index.isValid()) return false;
else if (role == ModifiedRole && m_notes[index.row()].note->modified() != value.toUInt()) { else if (role == ModifiedRole && m_notes[index.row()].note.modified() != value.toUInt()) {
m_notes[index.row()].note->setModified(value.toInt()); m_notes[index.row()].note.setModified(value.toInt());
emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected
emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, DateStringRole} ); // TODO remove when signals from Note are connected emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, DateStringRole} ); // TODO remove when signals from Note are connected
sort(); sort();
return true; return true;
} }
else if (role == CategoryRole && m_notes[index.row()].note->category() != value.toString()) { else if (role == CategoryRole && m_notes[index.row()].note.category() != value.toString()) {
m_notes[index.row()].note->setCategory(value.toString()); m_notes[index.row()].note.setCategory(value.toString());
emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected
sort(); sort();
return true; return true;
} }
else if (role == ContentRole && m_notes[index.row()].note->content() != value.toString()) { else if (role == ContentRole && m_notes[index.row()].note.content() != value.toString()) {
m_notes[index.row()].note->setContent(value.toString()); m_notes[index.row()].note.setContent(value.toString());
emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected
sort(); sort();
return true; return true;
} }
else if (role == FavoriteRole && m_notes[index.row()].note->favorite() != value.toBool()) { else if (role == FavoriteRole && m_notes[index.row()].note.favorite() != value.toBool()) {
m_notes[index.row()].note->setFavorite(value.toBool()); m_notes[index.row()].note.setFavorite(value.toBool());
emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected emit dataChanged(this->index(index.row()), this->index(index.row()), QVector<int> { 1, role } ); // TODO remove when signals from Note are connected
sort(); sort();
return true; return true;
@ -330,16 +348,16 @@ bool NotesModel::setItemData(const QModelIndex &index, const QMap<int, QVariant>
void NotesModel::sort() { void NotesModel::sort() {
qDebug() << "Sorting notes in the model"; qDebug() << "Sorting notes in the model";
QList<ModelNote<Note*, bool> > notes; QList<ModelNote<Note, bool> > notes;
QMap<QString, ModelNote<Note*, bool> > map; QMap<QString, ModelNote<Note, bool> > map;
QMap<QString, ModelNote<Note*, bool> > favorites; QMap<QString, ModelNote<Note, bool> > favorites;
if (m_sortBy == sortingNames()[sortByDate]) { if (m_sortBy == sortingNames()[sortByDate]) {
emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint); emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint);
for (int i = 0; i < m_notes.size(); i++) { for (int i = 0; i < m_notes.size(); i++) {
if (m_favoritesOnTop && m_notes[i].note->favorite()) if (m_favoritesOnTop && m_notes[i].note.favorite())
favorites.insert(QString::number(std::numeric_limits<uint>::max() - m_notes[i].note->modified()), m_notes[i]); favorites.insert(QString::number(std::numeric_limits<uint>::max() - m_notes[i].note.modified()), m_notes[i]);
else else
map.insert(QString::number(std::numeric_limits<uint>::max() - m_notes[i].note->modified()), m_notes[i]); map.insert(QString::number(std::numeric_limits<uint>::max() - m_notes[i].note.modified()), m_notes[i]);
} }
notes = favorites.values(); notes = favorites.values();
notes.append(map.values()); notes.append(map.values());
@ -349,10 +367,10 @@ void NotesModel::sort() {
else if (m_sortBy == sortingNames()[sortByCategory]) { else if (m_sortBy == sortingNames()[sortByCategory]) {
emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint); emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint);
for (int i = 0; i < m_notes.size(); i++) { for (int i = 0; i < m_notes.size(); i++) {
if (m_favoritesOnTop && m_notes[i].note->favorite()) if (m_favoritesOnTop && m_notes[i].note.favorite())
favorites.insert(m_notes[i].note->category(), m_notes[i]); favorites.insert(m_notes[i].note.category(), m_notes[i]);
else else
map.insert(m_notes[i].note->category(), m_notes[i]); map.insert(m_notes[i].note.category(), m_notes[i]);
} }
notes = favorites.values(); notes = favorites.values();
notes.append(map.values()); notes.append(map.values());
@ -362,10 +380,10 @@ void NotesModel::sort() {
else if (m_sortBy == sortingNames()[sortByTitle]) { else if (m_sortBy == sortingNames()[sortByTitle]) {
emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint); emit layoutAboutToBeChanged(QList<QPersistentModelIndex> (), VerticalSortHint);
for (int i = 0; i < m_notes.size(); i++) { for (int i = 0; i < m_notes.size(); i++) {
if (m_favoritesOnTop && m_notes[i].note->favorite()) if (m_favoritesOnTop && m_notes[i].note.favorite())
favorites.insert(m_notes[i].note->title(), m_notes[i]); favorites.insert(m_notes[i].note.title(), m_notes[i]);
else else
map.insert(m_notes[i].note->title(), m_notes[i]); map.insert(m_notes[i].note.title(), m_notes[i]);
} }
notes = favorites.values(); notes = favorites.values();
notes.append(map.values()); notes.append(map.values());

View file

@ -29,15 +29,17 @@ public:
QString searchText() const { return m_searchText; } QString searchText() const { return m_searchText; }
void setSearchText(QString searchText); void setSearchText(QString searchText);
Q_INVOKABLE void search(QString searchText); Q_INVOKABLE void search(QString searchText = QString());
Q_INVOKABLE void clearSearch(); Q_INVOKABLE void clearSearch();
Q_INVOKABLE bool applyJSON(QString json, bool replaceIfArray = true); Q_INVOKABLE bool applyJSON(QString json, bool replaceIfArray = true);
Q_INVOKABLE int insertNote(Note &note);
Q_INVOKABLE bool removeAt(int position);
Q_INVOKABLE bool removeNote(int id); Q_INVOKABLE bool removeNote(int id);
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();
Q_INVOKABLE int indexOf(int id) const; Q_INVOKABLE int indexOf(int id) const;
Q_INVOKABLE Note *get(int index) const; Q_INVOKABLE Note get(int index) const;
enum NoteRoles { enum NoteRoles {
VisibleRole = Qt::UserRole, VisibleRole = Qt::UserRole,
@ -85,7 +87,7 @@ signals:
void searchTextChanged(QString searchText); void searchTextChanged(QString searchText);
private: private:
QList<ModelNote<Note*, bool> > m_notes; QList<ModelNote<Note, bool> > m_notes;
QString m_sortBy; QString m_sortBy;
bool m_favoritesOnTop; bool m_favoritesOnTop;
QString m_searchText; QString m_searchText;