aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-10-05 21:07:45 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-10-05 21:07:45 +0200
commita1d164895fac7fd705c8cb37c4c94700be32a0a2 (patch)
tree38320fda6dbe01f209b7fae508b089929be8c665
parentFix line endings in appveyor.yml (diff)
downloadsmolbote-a1d164895fac7fd705c8cb37c4c94700be32a0a2.tar.xz
bookmarks: fix new/delete buttons
-rw-r--r--lib/bookmarks/CMakeLists.txt23
-rw-r--r--lib/bookmarks/bookmarkswidget.cpp43
-rw-r--r--lib/bookmarks/bookmarkswidget.h3
-rw-r--r--lib/bookmarks/forms/editbookmarkdialog.cpp6
-rw-r--r--lib/bookmarks/model/bookmarkitem.cpp (renamed from lib/bookmarks/bookmarkitem.cpp)0
-rw-r--r--lib/bookmarks/model/bookmarkitem.h (renamed from lib/bookmarks/bookmarkitem.h)0
-rw-r--r--lib/bookmarks/model/bookmarkmodel.cpp (renamed from lib/bookmarks/bookmarkmodel.cpp)35
-rw-r--r--lib/bookmarks/model/bookmarkmodel.h (renamed from lib/bookmarks/bookmarkmodel.h)4
-rw-r--r--lib/bookmarks/xbel.cpp2
9 files changed, 90 insertions, 26 deletions
diff --git a/lib/bookmarks/CMakeLists.txt b/lib/bookmarks/CMakeLists.txt
index d8bdb2b..ebbb035 100644
--- a/lib/bookmarks/CMakeLists.txt
+++ b/lib/bookmarks/CMakeLists.txt
@@ -7,18 +7,19 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
add_library(bookmarks
- bookmarksform.ui
- bookmarkswidget.cpp
- bookmarkswidget.h
- # xbel
- xbel.cpp
- xbel.h
+ bookmarksform.ui
+ bookmarkswidget.cpp
+ bookmarkswidget.h
+ # xbel
+ xbel.cpp
+ xbel.h
+
+ # bookmark item/model/view
+ model/bookmarkitem.cpp
+ model/bookmarkitem.h
+ model/bookmarkmodel.cpp
+ model/bookmarkmodel.h
- # bookmark item/model/view
- bookmarkitem.cpp
- bookmarkitem.h
- bookmarkmodel.cpp
- bookmarkmodel.h
forms/editbookmarkdialog.cpp
forms/editbookmarkdialog.h
forms/editbookmarkdialog.ui
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp
index 4fa42eb..1a4552a 100644
--- a/lib/bookmarks/bookmarkswidget.cpp
+++ b/lib/bookmarks/bookmarkswidget.cpp
@@ -7,8 +7,8 @@
*/
#include "bookmarkswidget.h"
-#include "bookmarkitem.h"
-#include "bookmarkmodel.h"
+#include "model/bookmarkitem.h"
+#include "model/bookmarkmodel.h"
#include "forms/editbookmarkdialog.h"
#include "ui_bookmarksform.h"
#include "xbel.h"
@@ -56,16 +56,39 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
expandChildren(ui->treeView, model, QModelIndex());
+ // item activated
connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
if(index.column() == 1)
emit openUrl(index.data(Qt::DisplayRole).toUrl());
else {
- auto *dlg = new EditBookmarkDialog(model, index, this);
- if(dlg->exec() == QDialog::Accepted) {
- m_isChanged = true;
- }
+ editBookmark(index);
}
});
+
+ // addBookmark
+ connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
+ const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
+ const QModelIndex childIdx = model->appendBookmark(tr("Title"), QString(), idx);
+ ui->treeView->setCurrentIndex(childIdx);
+ editBookmark(childIdx);
+ m_isChanged = true;
+ });
+
+ // addFolder
+ connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
+ const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
+ const QModelIndex childIdx = model->appendFolder(tr("Title"), idx);
+ ui->treeView->setCurrentIndex(childIdx);
+ editBookmark(childIdx);
+ m_isChanged = true;
+ });
+
+ // deleteItem
+ connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
+ const QModelIndex idx = ui->treeView->currentIndex();
+ if(model->removeRow(idx.row(), idx.parent()))
+ m_isChanged = true;
+ });
}
BookmarksWidget::~BookmarksWidget()
@@ -73,6 +96,14 @@ BookmarksWidget::~BookmarksWidget()
delete ui;
}
+
+void BookmarksWidget::editBookmark(const QModelIndex &index)
+{
+ auto *dlg = new EditBookmarkDialog(model, index, this);
+ if(dlg->exec() == QDialog::Accepted)
+ m_isChanged = true;
+}
+
void BookmarksWidget::save()
{
if(!m_isChanged) {
diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h
index aa15033..73628df 100644
--- a/lib/bookmarks/bookmarkswidget.h
+++ b/lib/bookmarks/bookmarkswidget.h
@@ -29,6 +29,9 @@ public:
explicit BookmarksWidget(const QString &path, QWidget *parent = nullptr);
~BookmarksWidget() override;
+protected:
+ void editBookmark(const QModelIndex &index);
+
signals:
void openUrl(const QUrl &url);
diff --git a/lib/bookmarks/forms/editbookmarkdialog.cpp b/lib/bookmarks/forms/editbookmarkdialog.cpp
index a01f4ac..9c6efa0 100644
--- a/lib/bookmarks/forms/editbookmarkdialog.cpp
+++ b/lib/bookmarks/forms/editbookmarkdialog.cpp
@@ -7,8 +7,8 @@
*/
#include "editbookmarkdialog.h"
-#include "../bookmarkitem.h"
-#include "../bookmarkmodel.h"
+#include "model/bookmarkitem.h"
+#include "model/bookmarkmodel.h"
#include "ui_editbookmarkdialog.h"
EditBookmarkDialog::EditBookmarkDialog(BookmarkModel *model, const QModelIndex &index, QWidget *parent)
@@ -67,4 +67,4 @@ void EditBookmarkDialog::saveChanges()
if(descriptionChanged) {
m_model->setData(m_index, ui->description->toPlainText(), BookmarkItem::Description, Qt::DisplayRole);
}
-} \ No newline at end of file
+}
diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/model/bookmarkitem.cpp
index 4715b6f..4715b6f 100644
--- a/lib/bookmarks/bookmarkitem.cpp
+++ b/lib/bookmarks/model/bookmarkitem.cpp
diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/model/bookmarkitem.h
index 8c9463f..8c9463f 100644
--- a/lib/bookmarks/bookmarkitem.h
+++ b/lib/bookmarks/model/bookmarkitem.h
diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/model/bookmarkmodel.cpp
index 936176b..aa8b0ac 100644
--- a/lib/bookmarks/bookmarkmodel.cpp
+++ b/lib/bookmarks/model/bookmarkmodel.cpp
@@ -112,20 +112,34 @@ int BookmarkModel::rowCount(const QModelIndex &index) const
return getItem(index)->childCount();
}
-bool BookmarkModel::appendBookmark(const QString &title, const QString &url, const QModelIndex &parent)
+QModelIndex BookmarkModel::appendBookmark(const QString &title, const QString &url, const QModelIndex &parent)
{
auto *parentItem = getItem(parent);
int row = parentItem->childCount();
beginInsertRows(parent, row, row);
- parentItem->appendChild(new BookmarkItem({ title, url }, BookmarkItem::Bookmark, parentItem));
+ auto *childItem = new BookmarkItem({ title, url }, BookmarkItem::Bookmark, parentItem);
+ parentItem->appendChild(childItem);
endInsertRows();
- return true;
+
+ return createIndex(row, 0, childItem);
+}
+
+QModelIndex BookmarkModel::appendFolder(const QString &title, const QModelIndex &parent)
+{
+ auto *parentItem = getItem(parent);
+ const int row = parentItem->childCount();
+
+ beginInsertRows(parent, row, row);
+ auto *childItem = new BookmarkItem({ title }, BookmarkItem::Folder, parentItem);
+ parentItem->appendChild(childItem);
+ endInsertRows();
+
+ return createIndex(row, 0, childItem);
}
bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent)
{
- qDebug("removeRows: pos=%i rows=%i", position, rows);
auto *parentItem = getItem(parent);
beginRemoveRows(parent, position, position + rows - 1);
@@ -167,6 +181,19 @@ QModelIndex BookmarkModel::parent(const QModelIndex &index) const
return createIndex(parentItem->row(), 0, parentItem);
}
+QModelIndex BookmarkModel::parentFolder(const QModelIndex &index) const
+{
+ // invalid index is the root index -> return it back
+ if(!index.isValid())
+ return QModelIndex();
+
+ if(getItem(index)->type() == BookmarkItem::Bookmark) {
+ return index.parent();
+ }
+
+ return index;
+}
+
inline bool has(const QStringList &terms, const QStringList &where)
{
for(const QString &term : terms) {
diff --git a/lib/bookmarks/bookmarkmodel.h b/lib/bookmarks/model/bookmarkmodel.h
index 28267aa..4da2f9c 100644
--- a/lib/bookmarks/bookmarkmodel.h
+++ b/lib/bookmarks/model/bookmarkmodel.h
@@ -30,7 +30,8 @@ public:
bool isItemExpanded(const QModelIndex &index) const;
int rowCount(const QModelIndex &index) const override;
- bool appendBookmark(const QString &title, const QString &url, const QModelIndex &parent);
+ QModelIndex appendBookmark(const QString &title, const QString &url, const QModelIndex &parent);
+ QModelIndex appendFolder(const QString &title, const QModelIndex &parent);
bool removeRows(int position, int rows, const QModelIndex &parent) override;
int columnCount(const QModelIndex &index) const override;
@@ -41,6 +42,7 @@ public:
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override;
+ QModelIndex parentFolder(const QModelIndex &index) const;
BookmarkItem *root()
{
diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp
index 174995d..1cb5756 100644
--- a/lib/bookmarks/xbel.cpp
+++ b/lib/bookmarks/xbel.cpp
@@ -7,7 +7,7 @@
*/
#include "xbel.h"
-#include "bookmarkitem.h"
+#include "model/bookmarkitem.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>