aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bookmarks/bookmarkitem.cpp')
-rw-r--r--lib/bookmarks/bookmarkitem.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/bookmarkitem.cpp
new file mode 100644
index 0000000..21d034f
--- /dev/null
+++ b/lib/bookmarks/bookmarkitem.cpp
@@ -0,0 +1,139 @@
+/*
+ * 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/gitea/aqua/smolbote
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "bookmarkitem.h"
+#include <QApplication>
+#include <QStyle>
+
+BookmarkItem::BookmarkItem(const QVector<QVariant> &data, Type type, BookmarkItem *parent)
+{
+ m_parentItem = parent;
+
+ m_type = type;
+ if(m_type == Folder) {
+ m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
+ m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
+ } else if(m_type == Bookmark)
+ m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_FileIcon));
+
+ m_data.resize(FieldCount);
+ for(int i = 0; i < FieldCount; ++i) {
+ m_data[i] = data.value(i, QVariant());
+ }
+}
+
+BookmarkItem::~BookmarkItem()
+{
+ qDeleteAll(m_children);
+}
+
+BookmarkItem *BookmarkItem::parent() const
+{
+ return m_parentItem;
+}
+
+bool BookmarkItem::appendChild(BookmarkItem *childItem)
+{
+ // Only folders can have children, so only append them on folders
+ // This way, we don't need to add checks to the other methods
+ if(m_type == Folder || m_type == Root) {
+ m_children.append(childItem);
+ return true;
+ }
+
+ return false;
+}
+
+bool BookmarkItem::insertChild(int position, BookmarkItem *childItem)
+{
+ // 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)
+
+ if(m_type == Folder || m_type == Root) {
+ m_children.insert(qMax(position, 0), childItem);
+ return true;
+ }
+
+ return false;
+}
+
+bool BookmarkItem::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;
+}
+
+BookmarkItem *BookmarkItem::takeChild(int index, BookmarkItem *newParent)
+{
+ m_children[index]->m_parentItem = newParent;
+ return m_children.takeAt(index);
+}
+
+BookmarkItem *BookmarkItem::child(int index) const
+{
+ return m_children.value(index);
+}
+
+int BookmarkItem::childCount() const
+{
+ return m_children.count();
+}
+
+QVariant BookmarkItem::data(Fields column) const
+{
+ return m_data.value(column);
+}
+
+bool BookmarkItem::setData(Fields column, const QVariant &data)
+{
+ if(column >= FieldCount)
+ return false;
+
+ m_data[column] = data;
+ return true;
+}
+
+QIcon BookmarkItem::icon() const
+{
+ return m_icon;
+}
+
+bool BookmarkItem::isExpanded() const
+{
+ return m_isExpanded;
+}
+
+void BookmarkItem::setExpanded(bool expanded)
+{
+ if(m_type == BookmarkItem::Folder)
+ m_isExpanded = expanded;
+}
+
+QString BookmarkItem::tooltip() const
+{
+ return m_data.value(Tags).toStringList().join(", ");
+}
+
+BookmarkItem::Type BookmarkItem::type() const
+{
+ return m_type;
+}
+
+int BookmarkItem::row() const
+{
+ if(m_parentItem)
+ return m_parentItem->m_children.indexOf(const_cast<BookmarkItem *>(this));
+
+ return 0;
+}