aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkmodel.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-09-24 14:55:35 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-09-24 14:55:35 +0200
commit579c713959c150a276f42b3f69ccfb89d9e6eebb (patch)
tree7c4f2082e2ff5dcdee7fcfe7887e7bbc82ec8bf6 /lib/bookmarks/bookmarkmodel.cpp
parentAdd bookmark auto-save (5min) (diff)
downloadsmolbote-579c713959c150a276f42b3f69ccfb89d9e6eebb.tar.xz
Bookmarks: add BookmarkItem and BookmarkModel
- read-only xbel - only enabled in debug build
Diffstat (limited to 'lib/bookmarks/bookmarkmodel.cpp')
-rw-r--r--lib/bookmarks/bookmarkmodel.cpp193
1 files changed, 193 insertions, 0 deletions
diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/bookmarkmodel.cpp
new file mode 100644
index 0000000..3b57fea
--- /dev/null
+++ b/lib/bookmarks/bookmarkmodel.cpp
@@ -0,0 +1,193 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "bookmarkmodel.h"
+#include "bookmarkitem.h"
+
+BookmarkModel::BookmarkModel(QObject *parent)
+ : QAbstractItemModel(parent)
+{
+ rootItem = new BookmarkItem({ "Title", "Address" }, BookmarkItem::Folder, nullptr);
+}
+
+BookmarkModel::~BookmarkModel()
+{
+ delete rootItem;
+}
+
+QVariant BookmarkModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
+ return rootItem->data(section);
+
+ return QVariant();
+}
+
+QVariant BookmarkModel::data(const QModelIndex &index, int role) const
+{
+ if(!index.isValid())
+ return QVariant();
+
+ if(role == Qt::DecorationRole && index.column() == 0)
+ return static_cast<BookmarkItem *>(index.internalPointer())->icon();
+ else if(role == Qt::ToolTipRole)
+ return static_cast<BookmarkItem *>(index.internalPointer())->tooltip();
+ else if(role != Qt::DisplayRole)
+ return QVariant();
+
+ return static_cast<BookmarkItem *>(index.internalPointer())->data(index.column());
+}
+
+QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) const
+{
+ if(!index.isValid())
+ return QVariant();
+
+ if(role == Qt::DisplayRole)
+ return static_cast<BookmarkItem *>(index.internalPointer())->data(column);
+
+ return QVariant();
+}
+
+bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ if(!index.isValid() || role != Qt::DisplayRole)
+ return false;
+
+ bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
+ if(success)
+ emit dataChanged(index, index, { role });
+ return success;
+}
+
+bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, BookmarkItem::Fields column, int role)
+{
+ if(!index.isValid() || role != Qt::DisplayRole)
+ return false;
+
+ bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(column, value);
+ if(success)
+ emit dataChanged(index, index, { role });
+ return success;
+}
+
+Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const
+{
+ if(getItem(index)->type() == BookmarkItem::Folder)
+ return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
+ else
+ return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled;
+}
+
+bool BookmarkModel::isItemExpanded(const QModelIndex &index) const
+{
+ if(!index.isValid())
+ return false;
+
+ return static_cast<BookmarkItem *>(index.internalPointer())->isExpanded();
+}
+
+int BookmarkModel::rowCount(const QModelIndex &index) const
+{
+ if(index.column() > 0)
+ return 0;
+
+ return getItem(index)->childCount();
+}
+
+bool BookmarkModel::insertRows(int position, int rows, const QModelIndex &parent)
+{
+ auto *parentItem = getItem(parent);
+
+ // if position is negative, or past the number of children in the item
+ if(position < 0 || position > parentItem->childCount())
+ return false;
+
+ beginInsertRows(parent, position, position + rows - 1);
+ for(int i = 0; i < rows; ++i) {
+ parentItem->insertChild(position + i, new BookmarkItem({}, BookmarkItem::Bookmark, parentItem));
+ }
+ endInsertRows();
+ return true;
+}
+
+bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent)
+{
+ auto *parentItem = getItem(parent);
+
+ beginRemoveRows(parent, position, position + rows - 1);
+ bool success = parentItem->removeChildAt(position, rows);
+ endRemoveRows();
+ return success;
+}
+
+bool BookmarkModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ auto *sourceParentItem = getItem(sourceParent);
+ auto *destinationParentItem = getItem(destinationParent);
+
+ bool success = true;
+ beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
+ for(int i = 0; i < count; ++i) {
+ auto *child = sourceParentItem->takeChild(sourceRow, destinationParentItem);
+ if(!destinationParentItem->insertChild(destinationChild, child)) {
+ success = false;
+ break;
+ }
+ }
+ endMoveRows();
+ return success;
+}
+
+int BookmarkModel::columnCount(const QModelIndex &index) const
+{
+ return 2;
+ // if(!index.isValid())
+ // return rootItem->columnCount();
+ // else
+ // return static_cast<BookmarkItem *>(index.internalPointer())->columnCount();
+}
+
+Qt::DropActions BookmarkModel::supportedDropActions() const
+{
+ return Qt::MoveAction;
+}
+
+QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) const
+{
+ if(!this->hasIndex(row, column, parent))
+ return QModelIndex();
+
+ BookmarkItem *parentItem = getItem(parent);
+ BookmarkItem *childItem = parentItem->child(row);
+ if(childItem)
+ return createIndex(row, column, childItem);
+ else
+ return QModelIndex();
+}
+
+QModelIndex BookmarkModel::parent(const QModelIndex &index) const
+{
+ if(!index.isValid())
+ return QModelIndex();
+
+ auto *childItem = static_cast<BookmarkItem *>(index.internalPointer());
+ auto *parentItem = childItem->parent();
+
+ if(parentItem == rootItem)
+ return QModelIndex();
+
+ return createIndex(parentItem->row(), 0, parentItem);
+}
+BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const
+{
+ if(!index.isValid())
+ return rootItem;
+ else
+ return static_cast<BookmarkItem *>(index.internalPointer());
+}