mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-23 18:53:36 +02:00
refactoring to use vector of smart pointers
This commit is contained in:
parent
db8ad8c666
commit
31ded8c6a9
@ -16,7 +16,7 @@ Item {
|
||||
list.model.addData(text)
|
||||
list.selectItemById(list.model.lastInsertId());
|
||||
} else {
|
||||
list.model.setData(index, text)
|
||||
list.model.setTitle(index, text)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,51 +10,50 @@ int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(par
|
||||
return baseModelCount() + (virtualItemShown() ? 1 : 0);
|
||||
}
|
||||
|
||||
//QVariant AbstractListModel::data(const QModelIndex & index, int role) const {
|
||||
// BaseModel model = baseModel();
|
||||
QVariant AbstractListModel::data(const QModelIndex & index, int role) const {
|
||||
BaseModel* model = NULL;
|
||||
|
||||
// if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||
// model.setValue("title", BaseModel::Value(QString("Untitled")));
|
||||
// } else {
|
||||
// model = atIndex(index.row());
|
||||
// }
|
||||
if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||
if (role == Qt::DisplayRole) return "Untitled";
|
||||
return "";
|
||||
} else {
|
||||
model = atIndex(index.row());
|
||||
}
|
||||
|
||||
// if (role == Qt::DisplayRole) {
|
||||
// return model.value("title").toQVariant();
|
||||
// }
|
||||
if (role == Qt::DisplayRole) {
|
||||
return model->value("title").toQVariant();
|
||||
}
|
||||
|
||||
// if (role == IdRole) {
|
||||
// return model.id().toQVariant();
|
||||
// }
|
||||
if (role == IdRole) {
|
||||
return model->id().toQVariant();
|
||||
}
|
||||
|
||||
// return QVariant();
|
||||
//}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
//BaseModel AbstractListModel::atIndex(int index) const {
|
||||
// if (cache_.size()) {
|
||||
// if (index < 0 || index >= cache_.size()) {
|
||||
// qWarning() << "Invalid folder index:" << index;
|
||||
// return Folder();
|
||||
// }
|
||||
BaseModel* AbstractListModel::atIndex(int index) const {
|
||||
qFatal("AbstractListModel::atIndex() not implemented");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return cache_[index];
|
||||
// }
|
||||
BaseModel* AbstractListModel::atIndex(const QModelIndex &index) const {
|
||||
return atIndex(index.row());
|
||||
}
|
||||
|
||||
// cache_.clear();
|
||||
bool AbstractListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
||||
BaseModel* model = atIndex(index.row());
|
||||
if (!model) return false;
|
||||
|
||||
// cache_ = Folder::all(orderBy_);
|
||||
if (role == Qt::EditRole) {
|
||||
model->setValue("title", value);
|
||||
if (!model->save()) return false;
|
||||
cacheClear();
|
||||
return true;
|
||||
}
|
||||
|
||||
// if (!cache_.size()) {
|
||||
// qWarning() << "Invalid folder index:" << index;
|
||||
// return Folder();
|
||||
// } else {
|
||||
// return atIndex(index);
|
||||
// }
|
||||
//}
|
||||
|
||||
//BaseModel AbstractListModel::atIndex(const QModelIndex &index) const {
|
||||
// return atIndex(index.row());
|
||||
//}
|
||||
qWarning() << "Unsupported role" << role;
|
||||
return false;
|
||||
}
|
||||
|
||||
int AbstractListModel::baseModelCount() const {
|
||||
qFatal("AbstractListModel::baseModelCount() not implemented");
|
||||
|
@ -20,9 +20,10 @@ public:
|
||||
|
||||
AbstractListModel();
|
||||
int rowCount(const QModelIndex & parent = QModelIndex()) const;
|
||||
//QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||
// BaseModel atIndex(int index) const;
|
||||
// BaseModel atIndex(const QModelIndex &index) const;
|
||||
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||
virtual BaseModel* atIndex(int index) const;
|
||||
BaseModel* atIndex(const QModelIndex &index) const;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -11,51 +11,14 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
|
||||
connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted()));
|
||||
}
|
||||
|
||||
QVariant FolderModel::data(const QModelIndex & index, int role) const {
|
||||
Folder* folder = NULL;
|
||||
|
||||
if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||
if (role == Qt::DisplayRole) return "Untitled";
|
||||
return "";
|
||||
//folder->setValue("title", BaseModel::Value(QString("Untitled")));
|
||||
} else {
|
||||
folder = atIndex(index.row());
|
||||
}
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
return folder->value("title").toQVariant();
|
||||
}
|
||||
|
||||
if (role == IdRole) {
|
||||
return folder->id().toQVariant();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
||||
Folder* folder = atIndex(index.row());
|
||||
if (!folder) return false;
|
||||
|
||||
if (role == Qt::EditRole) {
|
||||
folder->setValue("title", value);
|
||||
if (!folder->save()) return false;
|
||||
cacheClear();
|
||||
return true;
|
||||
}
|
||||
|
||||
qWarning() << "Unsupported role" << role;
|
||||
return false;
|
||||
}
|
||||
|
||||
Folder* FolderModel::atIndex(int index) const {
|
||||
BaseModel* FolderModel::atIndex(int index) const {
|
||||
if (cacheSize()) {
|
||||
if (index < 0 || index >= cacheSize()) {
|
||||
qWarning() << "Invalid folder index:" << index;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (Folder*)cacheGet(index);
|
||||
return cacheGet(index);
|
||||
}
|
||||
|
||||
cacheClear();
|
||||
@ -70,8 +33,39 @@ Folder* FolderModel::atIndex(int index) const {
|
||||
}
|
||||
}
|
||||
|
||||
Folder* FolderModel::atIndex(const QModelIndex &index) const {
|
||||
return atIndex(index.row());
|
||||
QString FolderModel::indexToId(int index) const {
|
||||
return data(this->index(index), IdRole).toString();
|
||||
}
|
||||
|
||||
int FolderModel::idToIndex(const QString &id) const {
|
||||
int count = this->rowCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
Folder* folder = (Folder*)atIndex(i);
|
||||
if (folder->idString() == id) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString FolderModel::lastInsertId() const {
|
||||
return lastInsertId_;
|
||||
}
|
||||
|
||||
bool FolderModel::setTitle(int index, const QVariant &value, int role) {
|
||||
return setData(this->index(index), value, role);
|
||||
}
|
||||
|
||||
void FolderModel::addData(const QString &title) {
|
||||
Folder folder;
|
||||
folder.setValue("title", title);
|
||||
if (!folder.save()) return;
|
||||
|
||||
lastInsertId_ = folder.id().toString();
|
||||
}
|
||||
|
||||
void FolderModel::deleteData(const int index) {
|
||||
Folder* folder = (Folder*)atIndex(index);
|
||||
if (!folder) return;
|
||||
folder->dispose();
|
||||
}
|
||||
|
||||
int FolderModel::baseModelCount() const {
|
||||
@ -99,41 +93,6 @@ int FolderModel::cacheSize() const {
|
||||
return cache_.size();
|
||||
}
|
||||
|
||||
QString FolderModel::indexToId(int index) const {
|
||||
return data(this->index(index), IdRole).toString();
|
||||
}
|
||||
|
||||
int FolderModel::idToIndex(const QString &id) const {
|
||||
int count = this->rowCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
Folder* folder = atIndex(i);
|
||||
if (folder->idString() == id) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString FolderModel::lastInsertId() const {
|
||||
return lastInsertId_;
|
||||
}
|
||||
|
||||
bool FolderModel::setData(int index, const QVariant &value, int role) {
|
||||
return setData(this->index(index), value, role);
|
||||
}
|
||||
|
||||
void FolderModel::addData(const QString &title) {
|
||||
Folder folder;
|
||||
folder.setValue("title", title);
|
||||
if (!folder.save()) return;
|
||||
|
||||
lastInsertId_ = folder.id().toString();
|
||||
}
|
||||
|
||||
void FolderModel::deleteData(const int index) {
|
||||
Folder* folder = atIndex(index);
|
||||
if (!folder) return;
|
||||
folder->dispose();
|
||||
}
|
||||
|
||||
// TODO: instead of clearing the whole cache every time, the individual items
|
||||
// could be created/updated/deleted
|
||||
|
||||
|
@ -18,14 +18,10 @@ public:
|
||||
FolderModel();
|
||||
|
||||
void addFolder(Folder* folder);
|
||||
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
||||
Folder* atIndex(int index) const;
|
||||
Folder* atIndex(const QModelIndex &index) const;
|
||||
BaseModel* atIndex(int index) const;
|
||||
|
||||
protected:
|
||||
|
||||
//BaseModel newBaseModel() const;
|
||||
int baseModelCount() const;
|
||||
BaseModel* cacheGet(int index) const;
|
||||
void cacheSet(int index, BaseModel *baseModel) const;
|
||||
@ -45,7 +41,7 @@ public slots:
|
||||
|
||||
void addData(const QString& title);
|
||||
void deleteData(const int index);
|
||||
bool setData(int index, const QVariant &value, int role = Qt::EditRole);
|
||||
bool setTitle(int index, const QVariant &value, int role = Qt::EditRole);
|
||||
QString indexToId(int index) const;
|
||||
int idToIndex(const QString& id) const;
|
||||
QString lastInsertId() const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user