Working on file integration to the model

This commit is contained in:
Scharel Clemens 2020-05-08 19:33:41 +02:00
parent aa8daf89f5
commit c319dd1050
3 changed files with 112 additions and 33 deletions

View file

@ -64,7 +64,7 @@ ApplicationWindow
if (sortBy == "none")
notesProxyModel.invalidate()
else
notesProxyModel.sortRole = notesProxyModel.roleFromName(sortBy)
notesProxyModel.sortRole = notesModel.roleFromName(sortBy)
}
onFavoritesOnTopChanged: {
notesProxyModel.favoritesOnTop = favoritesOnTop

View file

@ -6,7 +6,9 @@
#include <QDebug>
NotesProxyModel::NotesProxyModel(QObject *parent) : QSortFilterProxyModel(parent) {
m_favoritesOnTop = true;
m_favoritesOnTop = false;
m_sortByRole = -1;
m_searchFilterString = "";
//connect(this, SIGNAL(favoritesOnTopChanged(bool)), this, SLOT(resort()));
}
@ -19,12 +21,27 @@ void NotesProxyModel::setFavoritesOnTop(bool favoritesOnTop) {
if (favoritesOnTop != m_favoritesOnTop) {
m_favoritesOnTop = favoritesOnTop;
emit favoritesOnTopChanged(m_favoritesOnTop);
sort(0);
}
sort();
}
int NotesProxyModel::roleFromName(const QString &name) const {
return roleNames().key(name.toLocal8Bit());
void NotesProxyModel::setSortBy(const QString sortBy) {
qDebug() << "Sort by: " << sortBy;
int role = roleNames().key(sortBy.toLocal8Bit(), -1);
if (role >= 0 && role != m_sortByRole) {
m_sortByRole = role;
emit sortByChanged(sortBy);
setSortRole(role);
}
}
void NotesProxyModel::setSearchFilter(const QString searchFilter) {
qDebug() << "Search by:" << searchFilter;
if (searchFilter != m_searchFilterString) {
m_searchFilterString = searchFilter;
emit searchFilterChanged(m_searchFilterString);
setFilterFixedString(m_searchFilterString);
}
}
bool NotesProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
@ -37,10 +54,10 @@ bool NotesProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
void NotesProxyModel::sort() {
/*void NotesProxyModel::sort() {
//invalidate();
QSortFilterProxyModel::sort(0);
}
}*/
const QHash<int, QByteArray> NotesModel::m_roleNames = QHash<int, QByteArray> ( {
{NotesModel::IdRole, "id"},
@ -58,6 +75,10 @@ const QHash<int, QByteArray> NotesModel::m_roleNames = QHash<int, QByteArray> (
NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) {
mp_notesApi = nullptr;
mp_notesStore = nullptr;
//m_fileDir.setCurrent(directory);
m_fileDir.setPath("");
m_fileDir.setFilter(QDir::Files);
m_fileDir.setNameFilters( { "*." + m_fileSuffix } );
}
NotesModel::~NotesModel() {
@ -103,35 +124,78 @@ void NotesModel::setNotesStore(NotesStore *notesStore) {
}
QString NotesModel::account() const {
QString account;
if (mp_notesStore)
account = mp_notesStore->account();
else if (mp_notesApi)
account = mp_notesApi->account();
return account;
if (m_fileDir != QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation))) {
return m_fileDir.path();
}
return QString();
}
void NotesModel::setAccount(const QString &account) {
if (mp_notesApi)
mp_notesApi->setAccount(account);
if (mp_notesStore)
mp_notesStore->setAccount(account);
void NotesModel::setAccount(const QString& account) {
qDebug() << "Setting account: " << account;
if (account != m_fileDir.path()) {
if (m_fileDir != QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation))) {
m_fileDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
}
if (!account.isEmpty()) {
m_fileDir.setPath(account);
if (m_fileDir.mkpath(".")) {
emit accountChanged(m_fileDir.path());
}
else {
qDebug() << "Failed to create or already present: " << m_fileDir.path();
m_fileDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
//emit noteError(DirCannotWriteError);
}
}
//qDebug() << account << m_dir.path();
}
}
const QList<int> NotesModel::noteIds() {
return mp_notesStore->noteIds();
QList<int> ids;
if (m_fileDir.exists() && !account().isEmpty()) {
QFileInfoList files = m_fileDir.entryInfoList();
for (int i = 0; i < files.size(); ++i) {
bool ok;
int id = files[i].baseName().toInt(&ok);
if (ok) {
ids << id;
}
}
}
else {
//qDebug() << errorMessage(DirNotFoundError);
//emit noteError(DirCannotReadError);
}
return ids;
}
bool NotesModel::noteExists(const int id) {
return mp_notesStore->noteExists(id);
QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix));
return fileinfo.exists();
}
int NotesModel::noteModified(const int id) {
return mp_notesStore->noteModified(id);
return Note::modified(QJsonObject::fromVariantMap(getNoteById(id)));
}
const QVariantMap NotesModel::getNoteById(const int id) const {
return mp_notesStore->readNoteFile(id).toVariantMap();
QVariantMap json;
QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix));
QFile file(fileinfo.filePath());
if (file.exists()) {
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
json = QJsonDocument::fromJson(file.readAll()).object().toVariantMap();
file.close();
}
else {
//emit noteError(FileCannotReadError);
}
}
else {
//emit noteError(FileNotFoundError);
}
return json;
}
bool NotesModel::getAllNotes(const QStringList &exclude) {
@ -274,9 +338,13 @@ QHash<int, QByteArray> NotesModel::roleNames() const {
return m_roleNames;
}
int NotesModel::roleFromName(const QString &name) const {
return roleNames().key(name.toLocal8Bit());
}
Qt::ItemFlags NotesModel::flags(const QModelIndex &index) const {
if (index.isValid()) {
return Qt::ItemIsEnabled | Qt::ItemIsEditable; // | Qt::ItemIsSelectable
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
else {
return Qt::NoItemFlags;

View file

@ -12,34 +12,41 @@
class NotesProxyModel : public QSortFilterProxyModel {
Q_OBJECT
Q_PROPERTY(bool favoritesOnTop READ favoritesOnTop WRITE setFavoritesOnTop NOTIFY favoritesOnTopChanged)
Q_PROPERTY(QString sortBy READ sortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(QString searchFilter READ searchFilter WRITE setSearchFilter NOTIFY searchFilterChanged)
public:
explicit NotesProxyModel(QObject *parent = 0);
explicit NotesProxyModel(QObject *parent = nullptr);
virtual ~NotesProxyModel();
bool favoritesOnTop() const { return m_favoritesOnTop; }
void setFavoritesOnTop(bool favoritesOnTop);
QString sortBy() const { return roleNames().value(m_sortByRole); }
void setSortBy(const QString sortBy);
QString searchFilter() const { return m_searchFilterString; }
void setSearchFilter(const QString searchFilter);
Q_INVOKABLE void sort();
Q_INVOKABLE int roleFromName(const QString &name) const;
//Q_INVOKABLE void sort();
protected:
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
private slots:
signals:
void favoritesOnTopChanged(bool favoritesOnTop);
void sortByChanged(QString sortBy);
void searchFilterChanged(QString searchFilter);
private:
bool m_favoritesOnTop;
int m_sortByRole;
QString m_searchFilterString;
};
class NotesModel : public QAbstractListModel {
Q_OBJECT
public:
explicit NotesModel(QObject *parent = 0);
explicit NotesModel(QObject *parent = nullptr);
virtual ~NotesModel();
enum NoteRoles {
@ -56,11 +63,15 @@ public:
NoneRole = Qt::UserRole + 10
};
QHash<int, QByteArray> roleNames() const;
Q_INVOKABLE int roleFromName(const QString &roleName) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role) 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 insertRows(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;
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
@ -70,10 +81,6 @@ public:
QString account() const;
void setAccount(const QString& account);
const QList<int> noteIds();
bool noteExists(const int id);
int noteModified(const int id);
Q_INVOKABLE const QVariantMap getNoteById(const int id) const;
public slots:
@ -104,6 +111,10 @@ private:
QMap<int, QJsonObject> m_notes;
const static QHash<int, QByteArray> m_roleNames;
QMap<int, QFile> m_files;
QDir m_fileDir;
const static QString m_fileSuffix;
NotesApi* mp_notesApi;
NotesStore* mp_notesStore;
};