aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-12-27 16:27:25 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-12-27 16:27:25 +0100
commitab6f5290fc1582cf89257e6e5d12ec33ed428143 (patch)
tree68b5856bf7f3fdbcf5ff0ad6ec83945a8bbb60dc /lib
parentRename TabWidget::deleteTab to TabWidget::removeTab (diff)
downloadsmolbote-ab6f5290fc1582cf89257e6e5d12ec33ed428143.tar.xz
Bookmarks: track modified state in the model rather than the widget
Dragging and dropping bookmarks is done by the QTreeView rather than through the BookmarksWidget, so the widget could not track modification state correctly when items were reordered. BUG: #9 Bookmark reordering does not persist
Diffstat (limited to 'lib')
-rw-r--r--lib/bookmarks/bookmarkswidget.cpp21
-rw-r--r--lib/bookmarks/bookmarkswidget.h2
-rw-r--r--lib/bookmarks/model/bookmarkmodel.cpp21
-rw-r--r--lib/bookmarks/model/bookmarkmodel.h11
4 files changed, 34 insertions, 21 deletions
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp
index 9bb29f8..045b424 100644
--- a/lib/bookmarks/bookmarkswidget.cpp
+++ b/lib/bookmarks/bookmarkswidget.cpp
@@ -7,9 +7,9 @@
*/
#include "bookmarkswidget.h"
+#include "forms/editbookmarkdialog.h"
#include "model/bookmarkitem.h"
#include "model/bookmarkmodel.h"
-#include "forms/editbookmarkdialog.h"
#include "ui_bookmarksform.h"
#include "xbel.h"
#include <QTreeView>
@@ -51,9 +51,9 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
Xbel::read(&bookmarksFile, model->root());
bookmarksFile.close();
}
+ model->resetModified();
ui->treeView->setModel(model);
-
expandChildren(ui->treeView, model, QModelIndex());
// item activated
@@ -66,11 +66,9 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) {
model->setItemExpanded(index, true);
- m_isChanged = true;
});
connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) {
model->setItemExpanded(index, false);
- m_isChanged = true;
});
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
@@ -90,7 +88,6 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
const QModelIndex childIdx = model->appendBookmark(tr("Title"), QString(), idx);
ui->treeView->setCurrentIndex(childIdx);
editBookmark(childIdx);
- m_isChanged = true;
});
// addFolder
@@ -99,14 +96,12 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
const QModelIndex childIdx = model->appendFolder(tr("Title"), idx);
ui->treeView->setCurrentIndex(childIdx);
editBookmark(childIdx);
- m_isChanged = true;
});
// deleteItem
connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
const QModelIndex idx = ui->treeView->currentIndex();
- if(model->removeRow(idx.row(), idx.parent()))
- m_isChanged = true;
+ model->removeRow(idx.row(), idx.parent());
});
}
@@ -115,33 +110,29 @@ BookmarksWidget::~BookmarksWidget()
delete ui;
}
-
void BookmarksWidget::editBookmark(const QModelIndex &index)
{
auto *dlg = new EditBookmarkDialog(model, index, this);
- if(dlg->exec() == QDialog::Accepted)
- m_isChanged = true;
+ dlg->exec();
}
void BookmarksWidget::save()
{
- if(!m_isChanged) {
+ if(!model->isModified())
return;
- }
QFile bookmarksFile(m_bookmarksPath);
if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
Xbel::write(&bookmarksFile, model->root());
bookmarksFile.flush();
bookmarksFile.close();
- m_isChanged = false;
+ model->resetModified();
}
}
void BookmarksWidget::addBookmark(const QString &title, const QString &url)
{
model->appendBookmark(title, url, QModelIndex());
- m_isChanged = true;
}
void BookmarksWidget::search(const QString &term, std::function<void(QStringList &)> callback) const
diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h
index e293f65..f30db7d 100644
--- a/lib/bookmarks/bookmarkswidget.h
+++ b/lib/bookmarks/bookmarkswidget.h
@@ -43,10 +43,8 @@ public slots:
private:
Ui::BookmarksDialog *ui;
- bool m_isChanged = false;
QString m_bookmarksPath;
BookmarkModel *model;
-
};
#endif // BOOKMARKSDIALOG_H
diff --git a/lib/bookmarks/model/bookmarkmodel.cpp b/lib/bookmarks/model/bookmarkmodel.cpp
index 977248c..9214c24 100644
--- a/lib/bookmarks/model/bookmarkmodel.cpp
+++ b/lib/bookmarks/model/bookmarkmodel.cpp
@@ -72,8 +72,10 @@ bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int
success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
}
- if(success)
+ if(success) {
emit dataChanged(index, index, { role });
+ m_isModified = true;
+ }
return success;
}
@@ -83,8 +85,10 @@ bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, Boo
return false;
bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(column, value);
- if(success)
+ if(success) {
emit dataChanged(index, index, { role });
+ m_isModified = true;
+ }
return success;
}
@@ -104,11 +108,13 @@ bool BookmarkModel::isItemExpanded(const QModelIndex &index) const
return static_cast<BookmarkItem *>(index.internalPointer())->isExpanded();
}
-void BookmarkModel::setItemExpanded(const QModelIndex& index, bool expanded)
+void BookmarkModel::setItemExpanded(const QModelIndex &index, bool expanded)
{
BookmarkItem *item = getItem(index);
- if(item->type() == BookmarkItem::Folder)
+ if(item->type() == BookmarkItem::Folder) {
item->setExpanded(expanded);
+ m_isModified = true;
+ }
}
int BookmarkModel::rowCount(const QModelIndex &index) const
@@ -129,6 +135,7 @@ QModelIndex BookmarkModel::appendBookmark(const QString &title, const QString &u
parentItem->appendChild(childItem);
endInsertRows();
+ m_isModified = true;
return createIndex(row, 0, childItem);
}
@@ -142,6 +149,7 @@ QModelIndex BookmarkModel::appendFolder(const QString &title, const QModelIndex
parentItem->appendChild(childItem);
endInsertRows();
+ m_isModified = true;
return createIndex(row, 0, childItem);
}
@@ -152,6 +160,9 @@ bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent
beginRemoveRows(parent, position, position + rows - 1);
bool success = parentItem->removeChildAt(position, rows);
endRemoveRows();
+
+ if(success)
+ m_isModified = true;
return success;
}
@@ -339,5 +350,7 @@ bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction actio
delete fakeRoot;
}
+
+ m_isModified = true;
return true;
}
diff --git a/lib/bookmarks/model/bookmarkmodel.h b/lib/bookmarks/model/bookmarkmodel.h
index 3e0f35e..8efd2fb 100644
--- a/lib/bookmarks/model/bookmarkmodel.h
+++ b/lib/bookmarks/model/bookmarkmodel.h
@@ -52,11 +52,22 @@ public:
QStringList search(const QString &term) const;
+ void resetModified()
+ {
+ m_isModified = false;
+ }
+ bool isModified() const
+ {
+ return m_isModified;
+ }
+
private:
const QLatin1Literal mimeType = QLatin1Literal("application/xbel");
BookmarkItem *getItem(const QModelIndex &index) const;
BookmarkItem *rootItem;
+
+ bool m_isModified = false;
};
#endif // SMOLBOTE_BOOKMARKMODEL_H