Created
November 23, 2017 14:28
-
-
Save anonymous/65ff1aa8745a73244f37f6abf7b48ec1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ENTRYMODEL_HPP | |
#define ENTRYMODEL_HPP | |
#include <QStringListModel> | |
#include <QVariant> | |
#include <QString> | |
#include <QStringList> | |
#include <QObject> | |
class DatabaseManager; | |
class EntryModel : public QStringListModel { | |
public: | |
EntryModel(QObject* parent); | |
~EntryModel(); | |
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | |
int rowCount(const QModelIndex &parent = QModelIndex()) const override; | |
QModelIndex insertEntry(const QString& name, const QString& category); | |
bool hasEntry(const QString& name) const; | |
private: | |
DatabaseManager& mDatabase; | |
QStringList* mEntries; | |
}; | |
#endif // ENTRYMODEL_HPP | |
#include "EntryModel.hpp" | |
#include "DatabaseManager.hpp" | |
EntryModel::EntryModel(QObject* parent) | |
: QStringListModel(parent), | |
mDatabase(DatabaseManager::instance()), | |
mEntries(mDatabase.mEntryDAO.getEntries()) | |
{ | |
} | |
EntryModel::~EntryModel() { | |
delete mEntries; | |
mEntries = nullptr; | |
} | |
QVariant EntryModel::data(const QModelIndex &index, int role) const { | |
if (!index.isValid()) { | |
return QVariant(); | |
} | |
return mEntries->at(index.row()); | |
} | |
int EntryModel::rowCount(const QModelIndex &parent) const { | |
return mEntries->size(); | |
} | |
QModelIndex EntryModel::insertEntry(const QString &name, const QString &category) { | |
// TODO: get right index, sort alphabetically | |
int rowIndex = 0; | |
beginInsertRows(QModelIndex(), 0, 0); | |
mDatabase.mEntryDAO.addEntry(name, category); | |
// TODO: have addEntry return true/false | |
for (int i = 1; i < mEntries->size(); i++) { | |
if (mEntries->at(i) > name) { | |
mEntries->insert(i - 1, name); | |
break; | |
} | |
} | |
endInsertRows(); | |
return index(rowIndex, 0); | |
} | |
bool EntryModel::hasEntry(const QString &name) const { | |
if (mEntries->contains(name, Qt::CaseInsensitive)) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment