1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-01 21:24:45 +02:00

Removed dependency to foldercollection

This commit is contained in:
Laurent Cozic 2017-01-01 11:33:14 +01:00
parent 4570fcac18
commit b5dc92e3a7
10 changed files with 136 additions and 32 deletions

View File

@ -21,6 +21,13 @@ Item {
currentItem.stopEditing(); currentItem.stopEditing();
} }
function selectItemById(id) {
currentItemId = id
var newIndex = listView.model.idToIndex(currentItemId);
currentIndex = newIndex
if (newIndex < 0) currentItemId = "";
}
Rectangle { Rectangle {
color: "#eeeeff" color: "#eeeeff"
border.color: "#ff0000" border.color: "#ff0000"
@ -33,9 +40,7 @@ Item {
target: model target: model
onDataChanged: { onDataChanged: {
if (currentItemId !== "") { if (currentItemId !== "") {
var newIndex = model.idToIndex(currentItemId); selectItemById(currentItemId);
currentIndex = newIndex
if (newIndex < 0) currentItemId = "";
} }
} }
} }

View File

@ -16,7 +16,6 @@ SOURCES += \
webapi.cpp \ webapi.cpp \
synchronizer.cpp \ synchronizer.cpp \
settings.cpp \ settings.cpp \
models/foldercollection.cpp \
uuid.cpp \ uuid.cpp \
dispatcher.cpp dispatcher.cpp
@ -46,7 +45,6 @@ HEADERS += \
webapi.h \ webapi.h \
synchronizer.h \ synchronizer.h \
settings.h \ settings.h \
models/foldercollection.h \
simpletypes.h \ simpletypes.h \
uuid.h \ uuid.h \
dispatcher.h dispatcher.h

View File

@ -6,7 +6,6 @@
#include "database.h" #include "database.h"
#include "models/foldermodel.h" #include "models/foldermodel.h"
#include "models/notecollection.h" #include "models/notecollection.h"
#include "models/foldercollection.h"
#include "models/notemodel.h" #include "models/notemodel.h"
#include "models/qmlnote.h" #include "models/qmlnote.h"
#include "webapi.h" #include "webapi.h"

View File

@ -95,6 +95,20 @@ QSqlQuery Database::buildSqlQuery(Database::QueryType type, const QString &table
return query; return query;
} }
bool Database::errorCheck(const QSqlQuery& query) {
if (query.lastError().isValid()) {
qCritical().noquote() << "SQL query error: " << query.lastError().text().trimmed() << ". Query was: " << query.lastQuery();
QMapIterator<QString, QVariant> i(query.boundValues());
while (i.hasNext()) {
i.next();
qCritical() << i.key() << "=" << i.value().toString();
}
return false;
}
return true;
}
int Database::version() const { int Database::version() const {
if (version_ >= 0) return version_; if (version_ >= 0) return version_;

View File

@ -17,6 +17,7 @@ public:
QSqlQuery query(const QString& sql) const; QSqlQuery query(const QString& sql) const;
QSqlDatabase& database(); QSqlDatabase& database();
QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QStringList& fields, const VariantVector& values, const QString& whereCondition = ""); QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QStringList& fields, const VariantVector& values, const QString& whereCondition = "");
bool errorCheck(const QSqlQuery& query);
private: private:

View File

@ -35,7 +35,9 @@ Item {
onEditingAccepted: function(index, text) { onEditingAccepted: function(index, text) {
if (folderList.model.virtualItemShown()) { if (folderList.model.virtualItemShown()) {
folderList.model.hideVirtualItem();
folderList.model.addData(text) folderList.model.addData(text)
folderList.selectItemById(folderList.model.lastInsertId());
} else { } else {
folderList.model.setData(index, text) folderList.model.setData(index, text)
} }

View File

@ -16,7 +16,7 @@ FolderCollection::FolderCollection(Database& db, const QString& parentId, const
Folder FolderCollection::at(int index) const { Folder FolderCollection::at(int index) const {
if (cache_.size()) { if (cache_.size()) {
if (index < 0 || index >= count()) { if (index < 0 || index >= cache_.size()) {
qWarning() << "Invalid folder index:" << index; qWarning() << "Invalid folder index:" << index;
return Folder(); return Folder();
} }

View File

@ -1,15 +1,18 @@
#include "foldermodel.h" #include "foldermodel.h"
#include "uuid.h"
using namespace jop; using namespace jop;
FolderModel::FolderModel(Database &database) : QAbstractListModel(), folderCollection_(database, 0, "title"), db_(database), orderBy_("title") { FolderModel::FolderModel(Database &database) : QAbstractListModel(), db_(database), orderBy_("title") {
virtualItemShown_ = false; virtualItemShown_ = false;
connect(&folderCollection_, SIGNAL(changed(int,int,const QStringList&)), this, SLOT(folderCollection_changed(int,int,const QStringList&)));
} }
int FolderModel::rowCount(const QModelIndex & parent) const { int FolderModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent); Q_UNUSED(parent);
return folderCollection_.count() + (virtualItemShown_ ? 1 : 0); QSqlQuery q = db_.query("SELECT count(*) as row_count FROM folders");
q.exec();
q.next();
return q.value(0).toInt() + (virtualItemShown_ ? 1 : 0);
} }
// NOTE: to lazy load - send back "Loading..." if item not currently loaded // NOTE: to lazy load - send back "Loading..." if item not currently loaded
@ -21,7 +24,7 @@ QVariant FolderModel::data(const QModelIndex & index, int role) const {
if (virtualItemShown_ && index.row() == rowCount() - 1) { if (virtualItemShown_ && index.row() == rowCount() - 1) {
folder.setTitle("Untitled"); folder.setTitle("Untitled");
} else { } else {
folder = folderCollection_.at(index.row()); folder = atIndex(index.row());
} }
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
@ -36,16 +39,25 @@ QVariant FolderModel::data(const QModelIndex & index, int role) const {
} }
bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role) { bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role) {
Folder folder = folderCollection_.at(index.row()); Folder folder = atIndex(index.row());
if (role == Qt::EditRole) { if (role == Qt::EditRole) {
emit dataChanging(); emit dataChanging();
QStringList fields; QStringList fields;
fields << "title";
VariantVector values; VariantVector values;
values << value; fields << "title" << "synced";
folderCollection_.update(folder.id(), fields, values); values << value << QVariant(0);
QSqlQuery q = db_.buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + folder.id() + "\"");
q.exec();
if (!db_.errorCheck(q)) return false;
cache_.clear();
QVector<int> roles;
roles << Qt::DisplayRole;
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
return true; return true;
} }
@ -53,6 +65,39 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r
return false; return false;
} }
Folder FolderModel::atIndex(int index) const {
if (cache_.size()) {
if (index < 0 || index >= cache_.size()) {
qWarning() << "Invalid folder index:" << index;
return Folder();
}
return cache_[index];
}
cache_.clear();
QSqlQuery q = db_.query("SELECT " + Folder::dbFields().join(",") + " FROM folders ORDER BY " + orderBy_);
q.exec();
while (q.next()) {
Folder folder;
folder.fromSqlQuery(q);
cache_.push_back(folder);
}
if (!cache_.size()) {
qWarning() << "Invalid folder index:" << index;
return Folder();
} else {
return atIndex(index);
}
}
Folder FolderModel::atIndex(const QModelIndex &index) const {
return atIndex(index.row());
}
void FolderModel::showVirtualItem() { void FolderModel::showVirtualItem() {
virtualItemShown_ = true; virtualItemShown_ = true;
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1); beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
@ -70,7 +115,16 @@ QString FolderModel::idAtIndex(int index) const {
} }
int FolderModel::idToIndex(const QString &id) const { int FolderModel::idToIndex(const QString &id) const {
return folderCollection_.idToIndex(id); int count = this->rowCount();
for (int i = 0; i < count; i++) {
Folder folder = atIndex(i);
if (folder.id() == id) return i;
}
return -1;
}
QString FolderModel::lastInsertId() const {
return lastInsertId_;
} }
bool FolderModel::virtualItemShown() const { bool FolderModel::virtualItemShown() const {
@ -90,25 +144,49 @@ QHash<int, QByteArray> FolderModel::roleNames() const {
} }
void FolderModel::addData(const QString &title) { void FolderModel::addData(const QString &title) {
emit dataChanging();
QStringList fields; QStringList fields;
fields << "title";
VariantVector values; VariantVector values;
values << QVariant(title); QString folderId = uuid::createUuid();
folderCollection_.add(fields, values); fields << "id" << "title" << "synced";
values << folderId << QVariant(title) << QVariant(0);
QSqlQuery q = db_.buildSqlQuery(Database::Insert, "folders", fields, values);
q.exec();
if (!db_.errorCheck(q)) return;
cache_.clear();
lastInsertId_ = folderId;
QVector<int> roles;
roles << Qt::DisplayRole;
int from = 0;
int to = rowCount() - 1;
// Necessary to make sure a new item is added to the view, even
// though it might not be positioned there due to sorting
beginInsertRows(QModelIndex(), to, to);
endInsertRows();
emit dataChanged(this->index(from), this->index(to), roles);
} }
void FolderModel::deleteData(const int index) { void FolderModel::deleteData(const int index) {
QString id = folderCollection_.indexToId(index); QString folderId = idAtIndex(index);
folderCollection_.remove(id);
} QSqlQuery q(db_.database());
q.prepare("DELETE FROM folders WHERE id = :id");
q.bindValue(":id", folderId);
q.exec();
if (!db_.errorCheck(q)) return;
cache_.clear();
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
void FolderModel::folderCollection_changed(int from, int to, const QStringList& fields) {
beginRemoveRows(QModelIndex(), from, to);
QVector<int> roles; QVector<int> roles;
roles << Qt::DisplayRole; roles << Qt::DisplayRole;
qDebug() << "update" << from << to; emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
emit dataChanged(this->index(from), this->index(to), roles);
endRemoveRows();
} }

View File

@ -3,8 +3,8 @@
#include <stable.h> #include <stable.h>
#include "models/folder.h"
#include "database.h" #include "database.h"
#include "models/foldercollection.h"
namespace jop { namespace jop {
@ -26,6 +26,8 @@ public:
int rowCount(const QModelIndex & parent = QModelIndex()) const; int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
Folder atIndex(int index) const;
Folder atIndex(const QModelIndex &index) const;
protected: protected:
@ -34,22 +36,23 @@ protected:
private: private:
QList<Folder> folders_; QList<Folder> folders_;
FolderCollection folderCollection_;
bool virtualItemShown_; bool virtualItemShown_;
QString orderBy_; QString orderBy_;
Database& db_; Database& db_;
mutable QVector<Folder> cache_;
QString lastInsertId_;
public slots: public slots:
void addData(const QString& title); void addData(const QString& title);
void deleteData(const int index); void deleteData(const int index);
bool setData(int index, const QVariant &value, int role = Qt::EditRole); bool setData(int index, const QVariant &value, int role = Qt::EditRole);
void folderCollection_changed(int from, int to, const QStringList &fields);
void showVirtualItem(); void showVirtualItem();
bool virtualItemShown() const; bool virtualItemShown() const;
void hideVirtualItem(); void hideVirtualItem();
QString idAtIndex(int index) const; QString idAtIndex(int index) const;
int idToIndex(const QString& id) const; int idToIndex(const QString& id) const;
QString lastInsertId() const;
signals: signals:

View File

@ -26,6 +26,10 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonParseError> #include <QJsonParseError>
#include <QTimer>
#endif // __cplusplus #endif // __cplusplus
#endif // STABLE_H #endif // STABLE_H