1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-16 00:14:34 +02:00

Trying to fix signal issue

This commit is contained in:
Laurent Cozic
2017-01-31 21:20:13 +00:00
parent 8572dbab8f
commit 1ca242cdc3
10 changed files with 86 additions and 34 deletions

View File

@ -8,6 +8,7 @@ Item {
property int mouseAreaDefaultWidth
property Menu contextMenu
signal startedEditing;
signal stoppedEditing;
signal editingAccepted(int index, string text);
@ -24,6 +25,7 @@ Item {
textField.text = display
root.ListView.view.focus = true;
textField.selectAll()
root.startedEditing();
} else {
mouseArea.anchors.rightMargin = 0;
label.visible = true

View File

@ -8,24 +8,37 @@ Item {
property alias currentItem: listView.currentItem
property string currentItemId
signal startedEditing;
signal stoppedEditing;
signal editingAccepted(int index, string text);
signal deleteButtonClicked(int index);
// While an item is being edited, this property hold the item ID.
// It is then used, once the model is updated, to restore the selection.
property string editedItemId;
function startEditing(index) {
root.editedItemId = listView.model.indexToId(index);
currentIndex = model.rowCount() - 1;
currentItem.startEditing();
print("Start editing", root.editedItemId);
}
function stopEditing() {
currentItem.stopEditing();
print("Stop editing", root.editedItemId);
//print(root.editedItemId, listView.model.idToIndex(root.editedItemId));
//currentIndex = listView.model.idToIndex(root.editedItemId);
}
function selectItemById(id) {
print("selectItemBy()", id);
currentItemId = id
var newIndex = listView.model.idToIndex(currentItemId);
print("newIndex", newIndex);
currentIndex = newIndex
if (newIndex < 0) currentItemId = "";
print("currentItemId", currentItemId);
}
Rectangle {
@ -39,16 +52,19 @@ Item {
Connections {
target: model
onDataChanged: {
if (currentItemId !== "") {
print("Connection.onDataChanged");
selectItemById(currentItemId);
print("Connection.onDataChanged", root.editedItemId);
if (root.editedItemId !== "") {
selectItemById(root.editedItemId);
root.editedItemId = "";
}
}
}
onCurrentItemChanged: {
currentItemId = model.indexToId(currentIndex);
}
// onCurrentItemChanged: {
// print("onCurrentItemChanged avant", currentItemId);
// currentItemId = model.indexToId(currentIndex);
// print("onCurrentItemChanged apres", currentItemId);
// }
id: listView
highlightMoveVelocity: -1
@ -70,10 +86,17 @@ Item {
onTriggered: deleteButtonClicked(currentIndex);
}
}
onStartedEditing: {
print("onStartedEditing()");
root.editedItemId = listView.model.indexToId(index);
root.startedEditing();
}
onStoppedEditing: {
print("onStoppedEditing()");
root.stoppedEditing();
}
onEditingAccepted: function(index, text) {
print("onEditingAccepted()");
root.editingAccepted(index, text);
}
}

View File

@ -27,8 +27,8 @@ Item {
}
function handleItemListStoppedEditing(list) {
if (folderList.model.virtualItemShown()) {
folderList.model.hideVirtualItem();
if (list.model.virtualItemShown()) {
list.model.hideVirtualItem();
}
}

View File

@ -57,6 +57,14 @@ Item {
page.onShown();
}
function selectFolderbyId(id) {
mainPage.folderList.selectItemById(id);
}
function selectNoteById(id) {
mainPage.noteList.selectItemById(id);
}
function emitLoginStarted() {
root.loginStarted();
}

View File

@ -7,7 +7,7 @@ Database::Database() {}
void Database::initialize(const QString &path) {
version_ = -1;
transactionCount_ = 0;
logQueries_ = false;
logQueries_ = !false;
//QFile::remove(path);
@ -130,7 +130,9 @@ bool Database::commit() {
bool Database::execQuery(QSqlQuery &query) {
if (logQueries_) {
qDebug().noquote() << "SQL:" << query.lastQuery();
QString sql = query.lastQuery();
if (sql.startsWith("insert", Qt::CaseInsensitive)) {
qDebug().noquote() << "SQL:" << sql;
QMapIterator<QString, QVariant> i(query.boundValues());
while (i.hasNext()) {
@ -138,6 +140,7 @@ bool Database::execQuery(QSqlQuery &query) {
qDebug().noquote() << "SQL:" << i.key() << "=" << i.value().toString();
}
}
}
return query.exec();
}

View File

@ -50,7 +50,9 @@ std::vector<std::unique_ptr<Note>> Folder::notes(const QString &orderBy, int lim
}
int Folder::noteIndexById(const QString &orderBy, const QString& id) const {
QSqlQuery q = jop::db().prepare(QString("SELECT id FROM %1 WHERE parent_id = :parent_id ORDER BY %2")
qDebug() << "Folder::noteIndexById" << orderBy << id;
QSqlQuery q = jop::db().prepare(QString("SELECT id, %2 FROM %1 WHERE parent_id = :parent_id ORDER BY %2")
.arg(BaseModel::tableName(jop::NotesTable))
.arg(orderBy));
q.bindValue(":parent_id", idString());
@ -60,6 +62,8 @@ int Folder::noteIndexById(const QString &orderBy, const QString& id) const {
int index = 0;
while (q.next()) {
QString qId = q.value(0).toString();
QString qTitle = q.value(1).toString();
qDebug() << "CURRENT" << qId << qTitle;
if (qId == id) return index;
index++;
}

View File

@ -5,15 +5,6 @@
using namespace jop;
FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
// Qt::QueuedConnection needs to be used here because in the dispatcher_XXX slots
// the object that is being worked on might get deleted via cacheClear(). For example:
// 1. setData() requests a model from the cache
// 2. it updates it and call model->save()
// 3. save() emits "folderUpdated"
// 4. in dispatcher_folderUpdated() (which, without QueuedConnection is called immediately) the cache is cleared, including the model
// 5. the model is now an invalid pointer and the rest of the code in save() crashes
// This is solved using QueuedConnection, as it means the cache will be cleared only once model is no longer in use.
connect(&dispatcher(), SIGNAL(folderCreated(QString)), this, SLOT(dispatcher_folderCreated(QString)));
connect(&dispatcher(), SIGNAL(folderUpdated(QString)), this, SLOT(dispatcher_folderUpdated(QString)));
connect(&dispatcher(), SIGNAL(folderDeleted(QString)), this, SLOT(dispatcher_folderDeleted(QString)));

View File

@ -28,6 +28,7 @@ const Note *NoteModel::atIndex(int index) const {
Folder folder = this->folder();
qDebug() << "NoteModel: cache recreated";
std::vector<std::unique_ptr<Note>> notes = folder.notes(orderBy_, to - from + 1, from);
int noteIndex = from;
for (int i = 0; i < notes.size(); i++) {
@ -96,11 +97,12 @@ bool NoteModel::cacheIsset(int index) const {
}
void NoteModel::cacheClear() const {
qDebug() << "NoteModel::cacheClear()";
cache_.clear();
}
void NoteModel::dispatcher_noteCreated(const QString &noteId) {
qDebug() << "NoteModel Folder created" << noteId;
qDebug() << "NoteModel note created" << noteId;
cacheClear();

View File

@ -10,22 +10,40 @@ void Window::showPage(const QString &pageName) {
QMetaObject::invokeMethod((QObject*)rootObject(), "showPage", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, pageNameV));
}
void Window::emitSignal(const QString &name, const QVariantList &args) {
QVariant Window::callQml(const QString &name, const QVariantList &args) {
QVariant returnedValue;
QString nameCopy(name);
nameCopy = nameCopy.left(1).toUpper() + nameCopy.right(nameCopy.length() - 1);
nameCopy = "emit" + nameCopy;
qDebug() << "Going to call QML:" << nameCopy;
qDebug() << "Going to call QML:" << name;
QObject* o = (QObject*)rootObject();
if (args.size() == 0) {
QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue));
QMetaObject::invokeMethod(o, name.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue));
} else if (args.size() == 1) {
QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]));
QMetaObject::invokeMethod(o, name.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]));
} else if (args.size() == 2) {
QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]));
QMetaObject::invokeMethod(o, name.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]));
} else if (args.size() == 3) {
QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]), Q_ARG(QVariant, args[2]));
QMetaObject::invokeMethod(o, name.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]), Q_ARG(QVariant, args[2]));
} else {
qFatal("Window::emitSignal: add support for more args!");
}
return returnedValue;
}
void Window::emitSignal(const QString &name, const QVariantList &args) {
QString nameCopy(name);
nameCopy = nameCopy.left(1).toUpper() + nameCopy.right(nameCopy.length() - 1);
nameCopy = "emit" + nameCopy;
callQml(nameCopy, args);
// qDebug() << "Going to call QML:" << nameCopy;
// QObject* o = (QObject*)rootObject();
// if (args.size() == 0) {
// QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue));
// } else if (args.size() == 1) {
// QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]));
// } else if (args.size() == 2) {
// QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]));
// } else if (args.size() == 3) {
// QMetaObject::invokeMethod(o, nameCopy.toStdString().c_str(), Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, args[0]), Q_ARG(QVariant, args[1]), Q_ARG(QVariant, args[2]));
// } else {
// qFatal("Window::emitSignal: add support for more args!");
// }
}

View File

@ -13,6 +13,7 @@ public:
Window();
void showPage(const QString& pageName);
QVariant callQml(const QString& name, const QVariantList& args = QVariantList());
void emitSignal(const QString& name, const QVariantList& args = QVariantList());
};