1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

refactoring to use vector of smart pointers

This commit is contained in:
Laurent Cozic 2017-01-13 10:24:34 +01:00
parent 5aec22491c
commit db8ad8c666
4 changed files with 49 additions and 66 deletions

View File

@ -56,22 +56,17 @@ int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(par
// return atIndex(index.row());
//}
BaseModel AbstractListModel::newBaseModel() const {
qFatal("AbstractListModel::baseModel() not implemented");
return BaseModel();
}
int AbstractListModel::baseModelCount() const {
qFatal("AbstractListModel::baseModelCount() not implemented");
return 0;
}
BaseModel AbstractListModel::cacheGet(int index) const {
BaseModel* AbstractListModel::cacheGet(int index) const {
qFatal("AbstractListModel::cacheGet() not implemented");
return BaseModel();
return NULL;
}
void AbstractListModel::cacheSet(int index, const BaseModel &baseModel) {
void AbstractListModel::cacheSet(int index, BaseModel* baseModel) const {
qFatal("AbstractListModel::cacheSet() not implemented");
}
@ -80,10 +75,15 @@ bool AbstractListModel::cacheIsset(int index) const {
return false;
}
void AbstractListModel::cacheClear() {
void AbstractListModel::cacheClear() const {
qFatal("AbstractListModel::cacheClear() not implemented");
}
int AbstractListModel::cacheSize() const {
qFatal("AbstractListModel::cacheSize() not implemented");
return -1;
}
void AbstractListModel::showVirtualItem() {
virtualItemShown_ = true;
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);

View File

@ -26,13 +26,16 @@ public:
protected:
virtual BaseModel newBaseModel() const;
virtual int baseModelCount() const;
virtual BaseModel cacheGet(int index) const;
virtual void cacheSet(int index, const BaseModel& baseModel);
// All these methods are const because we want to be able to clear the
// cache or set values from any method including const ones.
// http://stackoverflow.com/a/4248661/561309
virtual BaseModel* cacheGet(int index) const;
virtual void cacheSet(int index, BaseModel* baseModel) const;
virtual bool cacheIsset(int index) const;
virtual void cacheClear();
virtual void cacheClear() const;
virtual int cacheSize() const;
private:

View File

@ -12,26 +12,6 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
}
QVariant FolderModel::data(const QModelIndex & index, int role) const {
//QVector<std::unique_ptr<Folder>> folders;
// std::unique_ptr<Folder> f(new Folder());
// f->setValue("title", QString("ancd"));
// BaseModel* b = static_cast<BaseModel*>(f.get());
// qDebug() << b->value("title").toString();
// Folder* f2 = static_cast<Folder*>(b);
// qDebug() << "*" << f2->value("title").toString();
// std::unique_ptr<BaseModel> baseModel(f.release());
// qDebug() << "££££££££££££££££££££££££££££££££" << baseModel->value("title").toString();
// std::unique_ptr<Folder> f2(baseModel.release());
// qDebug() << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << f2->value("title").toString();
//f->setValue("title", "abcd");
Folder* folder = NULL;
if (virtualItemShown() && index.row() == rowCount() - 1) {
@ -60,7 +40,7 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r
if (role == Qt::EditRole) {
folder->setValue("title", value);
if (!folder->save()) return false;
cache_.clear();
cacheClear();
return true;
}
@ -69,20 +49,20 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r
}
Folder* FolderModel::atIndex(int index) const {
if (cache_.size()) {
if (index < 0 || index >= cache_.size()) {
if (cacheSize()) {
if (index < 0 || index >= cacheSize()) {
qWarning() << "Invalid folder index:" << index;
return NULL;
}
return cache_[index].get();
return (Folder*)cacheGet(index);
}
cache_.clear();
cacheClear();
cache_ = Folder::all(orderBy_);
if (!cache_.size()) {
if (!cacheSize()) {
qWarning() << "Invalid folder index:" << index;
return NULL;
} else {
@ -94,30 +74,30 @@ Folder* FolderModel::atIndex(const QModelIndex &index) const {
return atIndex(index.row());
}
BaseModel FolderModel::newBaseModel() const {
return Folder();
}
int FolderModel::baseModelCount() const {
return Folder::count();
}
//BaseModel FolderModel::cacheGet(int index) const {
// return cache_[index];
//}
BaseModel *FolderModel::cacheGet(int index) const {
return cache_[index].get();
}
//void FolderModel::cacheSet(int index, const BaseModel &baseModel) {
//// Folder f(baseModel);
//// cache_[index] = f;
//}
void FolderModel::cacheSet(int index, BaseModel* baseModel) const {
Folder* folder = static_cast<Folder*>(baseModel);
cache_[index] = std::unique_ptr<Folder>(folder);
}
//bool FolderModel::cacheIsset(int index) const {
// return index > 0 && cache_.size() > index;
//}
bool FolderModel::cacheIsset(int index) const {
return index > 0 && cache_.size() > index;
}
//void FolderModel::cacheClear() {
// cache_.clear();
//}
void FolderModel::cacheClear() const {
cache_.clear();
}
int FolderModel::cacheSize() const {
return cache_.size();
}
QString FolderModel::indexToId(int index) const {
return data(this->index(index), IdRole).toString();
@ -160,7 +140,7 @@ void FolderModel::deleteData(const int index) {
void FolderModel::dispatcher_folderCreated(const QString &folderId) {
qDebug() << "FolderModel Folder created" << folderId;
cache_.clear();
cacheClear();
int from = 0;
int to = rowCount() - 1;
@ -179,7 +159,7 @@ void FolderModel::dispatcher_folderCreated(const QString &folderId) {
void FolderModel::dispatcher_folderUpdated(const QString &folderId) {
qDebug() << "FolderModel Folder udpated" << folderId;
cache_.clear();
cacheClear();
QVector<int> roles;
roles << Qt::DisplayRole;
@ -192,7 +172,7 @@ void FolderModel::dispatcher_folderDeleted(const QString &folderId) {
int index = idToIndex(folderId);
if (index < 0) return;
cache_.clear();
cacheClear();
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
@ -200,7 +180,7 @@ void FolderModel::dispatcher_folderDeleted(const QString &folderId) {
void FolderModel::dispatcher_allFoldersDeleted() {
qDebug() << "FolderModel All folders deleted";
cache_.clear();
cacheClear();
beginResetModel();
endResetModel();
}

View File

@ -25,18 +25,18 @@ public:
protected:
BaseModel newBaseModel() const;
//BaseModel newBaseModel() const;
int baseModelCount() const;
// virtual BaseModel cacheGet(int index) const;
// virtual void cacheSet(int index, const BaseModel& baseModel);
// virtual bool cacheIsset(int index) const;
// virtual void cacheClear();
BaseModel* cacheGet(int index) const;
void cacheSet(int index, BaseModel *baseModel) const;
bool cacheIsset(int index) const;
void cacheClear() const;
int cacheSize() const;
private:
QList<Folder> folders_;
QString orderBy_;
//mutable QVector<Folder> cache_;
mutable std::vector<std::unique_ptr<Folder>> cache_;
QString lastInsertId_;