Working on file integration to the model

This commit is contained in:
Scharel Clemens 2020-05-10 20:36:56 +02:00
parent c319dd1050
commit 964011847c
2 changed files with 70 additions and 30 deletions

View file

@ -150,7 +150,7 @@ void NotesModel::setAccount(const QString& account) {
//qDebug() << account << m_dir.path(); //qDebug() << account << m_dir.path();
} }
} }
/*
const QList<int> NotesModel::noteIds() { const QList<int> NotesModel::noteIds() {
QList<int> ids; QList<int> ids;
if (m_fileDir.exists() && !account().isEmpty()) { if (m_fileDir.exists() && !account().isEmpty()) {
@ -178,8 +178,8 @@ bool NotesModel::noteExists(const int id) {
int NotesModel::noteModified(const int id) { int NotesModel::noteModified(const int id) {
return Note::modified(QJsonObject::fromVariantMap(getNoteById(id))); return Note::modified(QJsonObject::fromVariantMap(getNoteById(id)));
} }
*/
const QVariantMap NotesModel::getNoteById(const int id) const { const QVariantMap NotesModel::note(const int id) const {
QVariantMap json; QVariantMap json;
QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix)); QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix));
QFile file(fileinfo.filePath()); QFile file(fileinfo.filePath());
@ -198,6 +198,28 @@ const QVariantMap NotesModel::getNoteById(const int id) const {
return json; return json;
} }
bool NotesModel::setNote(const QVariantMap &note, int id) const {
bool ok;
if (id < 0) {
id = note.value(m_roleNames[IdRole]).toInt(&ok);
}
else {
ok = true;
}
if (id >= 0 && ok) {
ok = false;
QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix));
QFile file(fileinfo.filePath());
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
QByteArray data = QJsonDocument(QJsonObject::fromVariantMap(note)).toJson();
if (file.write(data) == data.size()) {
ok = true;
}
}
}
return ok;
}
bool NotesModel::getAllNotes(const QStringList &exclude) { bool NotesModel::getAllNotes(const QStringList &exclude) {
bool success = true; bool success = true;
if (mp_notesApi) if (mp_notesApi)
@ -352,44 +374,60 @@ Qt::ItemFlags NotesModel::flags(const QModelIndex &index) const {
} }
int NotesModel::rowCount(const QModelIndex &parent) const { int NotesModel::rowCount(const QModelIndex &parent) const {
return m_notes.size(); if (parent.column() == 0 && m_fileDir.exists() && !account().isEmpty()) {
return static_cast<int>(m_fileDir.count());
}
else {
return 0;
}
} }
QVariant NotesModel::data(const QModelIndex &index, int role) const { QVariant NotesModel::data(const QModelIndex &index, int role) {
//qDebug(); if (role == ModifiedStringRole)
QVariant data; return itemData(index).value(role);
if (index.isValid() && index.row() <= m_notes.size()) {
QMap<int, QJsonObject>::const_iterator i = m_notes.cbegin();
i += index.row();
data = i.value()[(m_roleNames[role])];
}
return data;
} }
bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) { bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
//qDebug(); return setItemData(index, QMap<int, QVariant>{ { role, value } } );
if (index.isValid() && index.row() <= m_notes.size()) {
QMap<int, QJsonObject>::iterator i = m_notes.begin();
i += index.row();
i.value()[m_roleNames[role]] = QJsonValue::fromVariant(value);
emit dataChanged(index, index, QVector<int>( role ));
return true;
}
return false;
} }
QMap<int, QVariant> NotesModel::itemData(const QModelIndex &index) const { QMap<int, QVariant> NotesModel::itemData(const QModelIndex &index) {
//qDebug();
QMap<int, QVariant> map; QMap<int, QVariant> map;
if (index.isValid() && index.row() <= m_notes.size()) { if (index.isValid() && index.row() < m_files.size()) {
for (int role = IdRole; role < NoneRole; ++role) { QMap<int, QFile>::iterator i = m_files.begin();
map.insert(role, data(index, role)); i += index.row();
if (i.value().isReadable()) {
QJsonObject json = QJsonDocument::fromJson(i.value().readAll()).object();
for (int role = IdRole; role <= ErrorMessageRole; ++role) {
map.insert(role, json.value(m_roleNames[role]));
}
}
else {
qDebug() << "File not readable: " << i.value().fileName();
} }
} }
return map; return map;
} }
bool NotesModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) { bool NotesModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) {
if (index.isValid() && index.row() < m_files.size()) {
QMap<int, QFile>::iterator i = m_files.begin();
i += index.row();
if (i.value().isReadable() && i.value().isWritable()) {
QJsonObject json = QJsonDocument::fromJson(i.value().readAll()).object();
QMapIterator<int, QVariant> i(roles);
while (i.hasNext()) {
i.next();
json.insert(m_roleNames[i.key()], QJsonValue::fromVariant(i.value()));
}
}
}
else {
qDebug() << "File not writable: " << i.value().fileName();
}
//qDebug(); //qDebug();
bool retval = true; bool retval = true;
QMapIterator<int, QVariant> role(roles); QMapIterator<int, QVariant> role(roles);

View file

@ -3,6 +3,7 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QFile>
#include <QJsonArray> #include <QJsonArray>
#include <QDateTime> #include <QDateTime>
#include "note.h" #include "note.h"
@ -67,12 +68,12 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role) const; virtual QVariant data(const QModelIndex &index, int role);
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role); virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
QMap<int, QVariant> itemData(const QModelIndex &index) const; QMap<int, QVariant> itemData(const QModelIndex &index);
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles); virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
void setNotesApi(NotesApi* notesApi); void setNotesApi(NotesApi* notesApi);
@ -81,7 +82,8 @@ public:
QString account() const; QString account() const;
void setAccount(const QString& account); void setAccount(const QString& account);
Q_INVOKABLE const QVariantMap getNoteById(const int id) const; Q_INVOKABLE const QVariantMap note(const int id) const;
Q_INVOKABLE bool setNote(const QVariantMap& note, int id = -1) const;
public slots: public slots:
Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList()); Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());