diff options
Diffstat (limited to 'src/bookmarks/bookmarkstreeitem.cpp')
-rw-r--r-- | src/bookmarks/bookmarkstreeitem.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/bookmarks/bookmarkstreeitem.cpp b/src/bookmarks/bookmarkstreeitem.cpp new file mode 100644 index 00000000..fba1cc19 --- /dev/null +++ b/src/bookmarks/bookmarkstreeitem.cpp @@ -0,0 +1,109 @@ +/* ============================================================ + * rekonq + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ + * Description: rekonq bookmarks model + * ============================================================ */ + +#include "bookmarkstreeitem.hpp" + +BookmarksTreeItem::BookmarksTreeItem(Types type, Attributes_t &&args, BookmarksTreeItem *parent) + : m_parent(parent), m_type(type), m_data(args) +{ + switch (m_type) { + case Folder: + m_icon = QIcon::fromTheme("folder"); + break; + case Bookmark: + m_icon = QIcon::fromTheme("bookmark"); + break; + case Alias: + case Root: + case Separator: + break; + } +} + +BookmarksTreeItem::~BookmarksTreeItem() { qDeleteAll(m_children); } + +bool BookmarksTreeItem::appendChild(BookmarksTreeItem *childItem) +{ + // only root and folders can have children + if (m_type == Folder || m_type == Root) { + m_children.append(childItem); + return true; + } + return false; +} + +bool BookmarksTreeItem::insertChild(int position, BookmarksTreeItem *childItem) +{ + if (m_type == Folder || m_type == Root) { + // position is invalid (-1) when dropping an item onto the folder, which leads to crash + // make sure that position passed is >= 0 (insert item at first position) + childItem->m_parent = this; + m_children.insert(qMax(position, 0), childItem); + return true; + } + + return false; +} + +bool BookmarksTreeItem::removeChildAt(int index, int count) +{ + if (index < 0 || index + count > m_children.size()) return false; + + // delete the item at index count times + for (int i = 0; i < count; ++i) { delete m_children.takeAt(index); } + return true; +} + +bool BookmarksTreeItem::setData(Attributes column, const QVariant &data) +{ + switch (column) { + case Title: + m_data.title = data.toString(); + return true; + case Href: + m_data.href = data.toUrl(); + return true; + case Added: + m_data.added = data.toDateTime(); + return true; + case Visited: + m_data.visited = data.toDateTime(); + return true; + case Modified: + m_data.modified = data.toDateTime(); + return true; + case Description: + m_data.description = data.toString(); + return true; + case Id: + m_data.id = data.toString(); + return true; + } + + return false; +} + +void BookmarksTreeItem::setExpanded(bool expanded) +{ + if (m_type == BookmarksTreeItem::Folder) m_isExpanded = expanded; +} + +QString BookmarksTreeItem::tooltip() const +{ + auto msg = QString("<p><b>%1</b></p>").arg(m_data.title); + if (const auto href = m_data.href.toString(); !href.isEmpty()) + msg += QString("<p>%2</p>").arg(m_data.href.toString()); + return msg; +} + +int BookmarksTreeItem::row() const +{ + if (m_parent) return static_cast<int>(m_parent->m_children.indexOf(const_cast<BookmarksTreeItem *>(this))); + return 0; +} |