From f628f4a2b1560e174a5a62ab54fa84365db3fecd Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 8 Jan 2018 20:10:32 +0100 Subject: BookmarksModel::insertItem --- lib/bookmarks/bookmarksmodel.cpp | 69 +++++++++++++++++++++++++++++++++++++++ lib/bookmarks/bookmarksmodel.h | 6 ++++ lib/bookmarks/bookmarkswidget.cpp | 13 +++++++- 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/lib/bookmarks/bookmarksmodel.cpp b/lib/bookmarks/bookmarksmodel.cpp index 7e0c35b..69f6334 100644 --- a/lib/bookmarks/bookmarksmodel.cpp +++ b/lib/bookmarks/bookmarksmodel.cpp @@ -114,6 +114,26 @@ int BookmarksModel::rowCount(const QModelIndex &parent) const return parentItem->childCount(); } +QModelIndex BookmarksModel::insertItem(BookmarkItem::BookmarkItemType type, const QModelIndex &parent) +{ + BookmarkItem *parentItem; + if(!parent.isValid()) { + parentItem = m_rootItem; + } else { + parentItem = static_cast(parent.internalPointer()); + if(parentItem->type() == BookmarkItem::Bookmark) + parentItem = parentItem->parentItem(); + } + + beginInsertRows(index(parentItem), parentItem->row(), parentItem->row()); + auto *node = new BookmarkItem(type, parentItem); + node->title = tr("title"); + node->href = tr("href"); + endInsertRows(); + + return index(node); +} + int BookmarksModel::columnCount(const QModelIndex &parent) const { if(parent.isValid()) { @@ -123,6 +143,26 @@ int BookmarksModel::columnCount(const QModelIndex &parent) const } } +Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const +{ + if(!index.isValid()) + return Qt::NoItemFlags; + + Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; + auto *node = static_cast(index.internalPointer()); + switch(node->type()) { + case BookmarkItem::Root: + break; + case BookmarkItem::Folder: + flags |= Qt::ItemIsEditable; + break; + case BookmarkItem::Bookmark: + flags |= Qt::ItemIsEnabled | Qt::ItemNeverHasChildren; + break; + } + return flags; +} + QVariant BookmarksModel::data(const QModelIndex &index, int role) const { // get data of invalid index? @@ -167,6 +207,35 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const } } +bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if(!index.isValid()) + return false; + + auto *node = static_cast(index.internalPointer()); + switch(node->type()) { + case BookmarkItem::Root: + return false; + + case BookmarkItem::Folder: + if(index.column() == 0) { + node->title = value.toString(); + return true; + } + return false; + + case BookmarkItem::Bookmark: + if(index.column() == 0) { + node->title = value.toString(); + return true; + } else if(index.column() == 1) { + node->href = value.toString(); + return true; + } + return false; + } +} + bool BookmarksModel::hasChildren(const QModelIndex &parent) const { if(!parent.isValid()) { diff --git a/lib/bookmarks/bookmarksmodel.h b/lib/bookmarks/bookmarksmodel.h index baf2ba9..986a195 100644 --- a/lib/bookmarks/bookmarksmodel.h +++ b/lib/bookmarks/bookmarksmodel.h @@ -33,9 +33,15 @@ public: QModelIndex index(BookmarkItem *node, int column = 0) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QModelIndex insertItem(BookmarkItem::BookmarkItemType type, const QModelIndex &parent); + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + + Qt::ItemFlags flags(const QModelIndex &index) const override; QVariant data(const QModelIndex &index, int role) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool hasChildren(const QModelIndex &parent) const override; diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index 17e1732..e510218 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -28,6 +28,7 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) m_model = new BookmarksModel(style(), this); ui->treeView->setModel(m_model); + //ui->treeView->setEditTriggers(QAbstractItemView::EditKeyPressed); xbel = new Xbel(path); BookmarkItem *rootNode = xbel->read(); @@ -38,6 +39,15 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) { emit openUrl(m_model->data(index, BookmarksModel::OpenUrlRole).toUrl()); }); + + connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() { + QModelIndex idx = ui->treeView->currentIndex(); + m_model->insertItem(BookmarkItem::Bookmark, idx); + }); + connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() { + QModelIndex idx = ui->treeView->currentIndex(); + m_model->insertItem(BookmarkItem::Folder, idx); + }); } BookmarksWidget::~BookmarksWidget() @@ -75,6 +85,7 @@ void BookmarksWidget::expandNodes(BookmarkItem *node) } } -void BookmarksWidget::closeOthers() { +void BookmarksWidget::closeOthers() +{ emit closeOthersSignal(); } -- cgit v1.2.1