/* ============================================================ * rekonq * ============================================================ * SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2022 aqua * ============================================================ * 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("

%1

").arg(m_data.title); if (const auto href = m_data.href.toString(); !href.isEmpty()) msg += QString("

%2

").arg(m_data.href.toString()); return msg; } int BookmarksTreeItem::row() const { if (m_parent) return static_cast(m_parent->m_children.indexOf(const_cast(this))); return 0; }