diff options
-rw-r--r-- | lib/bookmarks/bookmarkitem.cpp | 10 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkitem.h | 1 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksmodel.cpp | 17 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksmodel.h | 1 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkswidget.cpp | 42 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkswidget.h | 3 |
6 files changed, 59 insertions, 15 deletions
diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/bookmarkitem.cpp index 5dd3c22..25b1004 100644 --- a/lib/bookmarks/bookmarkitem.cpp +++ b/lib/bookmarks/bookmarkitem.cpp @@ -30,6 +30,12 @@ void BookmarkItem::appendChild(BookmarkItem *child) m_childItems.append(child); } +bool BookmarkItem::removeChild(BookmarkItem *child) +{ + Q_CHECK_PTR(child); + return m_childItems.removeOne(child); +} + BookmarkItem *BookmarkItem::child(int row) { return m_childItems.at(row); @@ -42,12 +48,12 @@ int BookmarkItem::childIndex(BookmarkItem *item) const int BookmarkItem::childCount() const { - return m_childItems.count(); + return m_childItems.size(); } int BookmarkItem::row() const { - if(m_parentItem) { + if(m_parentItem != nullptr) { return m_parentItem->m_childItems.indexOf(const_cast<BookmarkItem *>(this)); } diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/bookmarkitem.h index 5059fd6..fa1a91c 100644 --- a/lib/bookmarks/bookmarkitem.h +++ b/lib/bookmarks/bookmarkitem.h @@ -25,6 +25,7 @@ public: ~BookmarkItem(); void appendChild(BookmarkItem *child); + bool removeChild(BookmarkItem *child); BookmarkItem *child(int row); int childIndex(BookmarkItem *item) const; int childCount() const; diff --git a/lib/bookmarks/bookmarksmodel.cpp b/lib/bookmarks/bookmarksmodel.cpp index ba537ba..5b91216 100644 --- a/lib/bookmarks/bookmarksmodel.cpp +++ b/lib/bookmarks/bookmarksmodel.cpp @@ -7,8 +7,8 @@ */ #include "bookmarksmodel.h" -#include <QMimeData> #include <QBuffer> +#include <QMimeData> BookmarksModel::BookmarksModel(QStyle *style, QObject *parent) : QAbstractItemModel(parent) @@ -148,6 +148,21 @@ QModelIndex BookmarksModel::insertItem(BookmarkItem::BookmarkItemType type, cons return index(node); } +bool BookmarksModel::removeItem(const QModelIndex &node) +{ + if(!node.isValid()) + return false; + + auto *item = static_cast<BookmarkItem *>(node.internalPointer()); + auto *parentItem = item->parentItem(); + + beginRemoveRows(node, node.row(), node.row()); + parentItem->removeChild(item); + delete item; + endRemoveRows(); + return true; +} + int BookmarksModel::columnCount(const QModelIndex &parent) const { if(parent.isValid()) { diff --git a/lib/bookmarks/bookmarksmodel.h b/lib/bookmarks/bookmarksmodel.h index b6cadbf..7ae20fa 100644 --- a/lib/bookmarks/bookmarksmodel.h +++ b/lib/bookmarks/bookmarksmodel.h @@ -42,6 +42,7 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; QModelIndex insertItem(BookmarkItem::BookmarkItemType type, const QModelIndex &parent); + bool removeItem(const QModelIndex &node); int columnCount(const QModelIndex &parent = QModelIndex()) const override; diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index 5e0e391..8711b35 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -35,10 +35,18 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) ui->treeView->setDragDropMode(QAbstractItemView::InternalMove); ui->treeView->setDropIndicatorShown(true); - m_bookmarksFile.setFileName(path); - m_bookmarksFile.open(QIODevice::ReadWrite | QIODevice::Text); - xbel = new Xbel(&m_bookmarksFile); - qDebug("Reading bookmarks [%s] %s", qUtf8Printable(path), m_model->read(xbel) ? "ok" : "failed"); + m_bookmarksPath = path; + + // read bookmarks + { + QFile bookmarksFile(m_bookmarksPath); + if (bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + Xbel xbel(&bookmarksFile); + qDebug("Reading bookmarks [%s] %s", qUtf8Printable(path), m_model->read(&xbel) ? "ok" : "failed"); + bookmarksFile.close(); + } + } + m_model->expandItems(ui->treeView); // open bookmark action @@ -58,6 +66,14 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) m_model->insertItem(BookmarkItem::Folder, idx); }); + connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() { + QModelIndex idx = ui->treeView->currentIndex(); + ui->treeView->clearSelection(); + ui->folder_groupBox->setVisible(false); + ui->bookmark_groupBox->setVisible(false); + m_model->removeItem(idx); + }); + // set folder title action connect(ui->folderTitle, &QLineEdit::returnPressed, this, [this]() { QModelIndex idx = ui->treeView->currentIndex(); @@ -82,7 +98,7 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) // set item action connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, [this](const QModelIndex ¤t, const QModelIndex &previous) { // save the previous item - if(previous.isValid()) { + if(previous.isValid() && (ui->folder_groupBox->isVisible() || ui->bookmark_groupBox->isVisible())) { if(m_model->type(previous) == BookmarkItem::Folder) { m_model->setData(previous, ui->folderTitle->text(), BookmarksModel::TitleRole); ui->folder_groupBox->setVisible(false); @@ -108,17 +124,23 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) BookmarksWidget::~BookmarksWidget() { - m_bookmarksFile.close(); - delete xbel; delete ui; } void BookmarksWidget::save() { - if(m_model->isModified()) - qDebug("Writing bookmarks %s", m_model->write(xbel) ? "ok" : "failed"); - else + if(!m_model->isModified()) { qDebug("Writing bookmarks skipped"); + return; + } + + QFile bookmarksFile(m_bookmarksPath); + if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + Xbel xbel(&bookmarksFile); + qDebug("Writing bookmarks %s", m_model->write(&xbel) ? "ok" : "failed"); + bookmarksFile.flush(); + bookmarksFile.close(); + } } QAbstractItemModel *BookmarksWidget::model() const diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h index abb4ac4..f84b21a 100644 --- a/lib/bookmarks/bookmarkswidget.h +++ b/lib/bookmarks/bookmarkswidget.h @@ -40,8 +40,7 @@ signals: private: Ui::BookmarksDialog *ui; BookmarksModel *m_model; - Xbel *xbel; - QFile m_bookmarksFile; + QString m_bookmarksPath; }; #endif // BOOKMARKSDIALOG_H |