From 5d0adf426a33440542eb88eca83c804dcec58475 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 11 Jan 2018 17:30:38 +0100 Subject: Writing BookmarksModel to xbel --- lib/bookmarks/bookmarksmodel.cpp | 45 +++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'lib/bookmarks/bookmarksmodel.cpp') diff --git a/lib/bookmarks/bookmarksmodel.cpp b/lib/bookmarks/bookmarksmodel.cpp index e67668f..5cf343f 100644 --- a/lib/bookmarks/bookmarksmodel.cpp +++ b/lib/bookmarks/bookmarksmodel.cpp @@ -7,7 +7,6 @@ */ #include "bookmarksmodel.h" -#include BookmarksModel::BookmarksModel(QStyle *style, QObject *parent) : QAbstractItemModel(parent) @@ -27,12 +26,25 @@ BookmarksModel::~BookmarksModel() delete m_rootItem; } -void BookmarksModel::setRoot(BookmarkItem *root) +bool BookmarksModel::isModified() const { - Q_CHECK_PTR(root); + return modified; +} - delete m_rootItem; - m_rootItem = root; +bool BookmarksModel::read(Xbel *xbel) +{ + Q_CHECK_PTR(xbel); + // if there are items in the bookmark list, we're adding to them, so the model becomes modified + if(m_rootItem->childCount() != 0) { + modified = true; + } + return xbel->read(m_rootItem); +} + +bool BookmarksModel::write(Xbel *xbel) +{ + Q_CHECK_PTR(xbel); + return xbel->write(m_rootItem); } QVariant BookmarksModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -220,6 +232,7 @@ bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, in case BookmarkItem::Folder: if(index.column() == 0) { node->title = value.toString(); + modified = true; emit dataChanged(index, index); return true; } @@ -228,10 +241,12 @@ bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, in case BookmarkItem::Bookmark: if(index.column() == 0) { node->title = value.toString(); + modified = true; emit dataChanged(index, index); return true; } else if(index.column() == 1) { node->href = value.toString(); + modified = true; emit dataChanged(index, index); return true; } @@ -275,3 +290,23 @@ QModelIndexList BookmarksModel::match(const QModelIndex &start, int role, const return list; } + +void BookmarksModel::expandItems(QTreeView *view, BookmarkItem *root) +{ + if(root == nullptr) + root = m_rootItem; + + // iterate through children + for(int i = 0; i < root->childCount(); ++i) { + BookmarkItem *childNode = root->child(i); + + // and if it's a folder + if(childNode->type() == BookmarkItem::Folder) { + QModelIndex idx = index(childNode); + view->setExpanded(idx, !childNode->folded); + + // only folders have children, so go through them + expandItems(view, childNode); + } + } +} -- cgit v1.2.1