aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/bookmarks/bookmarkitem.cpp2
-rw-r--r--lib/bookmarks/bookmarkitem.h8
-rw-r--r--lib/bookmarks/bookmarkmodel.cpp165
-rw-r--r--lib/bookmarks/bookmarkmodel.h21
-rw-r--r--lib/bookmarks/bookmarksform.ui19
-rw-r--r--lib/bookmarks/bookmarksview.cpp45
-rw-r--r--lib/bookmarks/bookmarksview.h18
-rw-r--r--lib/bookmarks/bookmarkswidget.cpp129
-rw-r--r--lib/bookmarks/bookmarkswidget.h3
-rw-r--r--lib/bookmarks/forms/editbookmarkdialog.cpp41
-rw-r--r--lib/bookmarks/forms/editbookmarkdialog.h12
-rw-r--r--lib/bookmarks/forms/editbookmarkdialog.ui4
-rw-r--r--lib/bookmarks/xbel.cpp95
-rw-r--r--lib/bookmarks/xbel.h8
14 files changed, 259 insertions, 311 deletions
diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/bookmarkitem.cpp
index 197c0ae..ff78eb5 100644
--- a/lib/bookmarks/bookmarkitem.cpp
+++ b/lib/bookmarks/bookmarkitem.cpp
@@ -77,7 +77,7 @@ BookmarkItem *BookmarkItem::takeChild(int index, BookmarkItem *newParent)
return m_children.takeAt(index);
}
-BookmarkItem *BookmarkItem::child(int index)
+BookmarkItem *BookmarkItem::child(int index) const
{
return m_children.value(index);
}
diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/bookmarkitem.h
index 37a9b5d..fd5e67a 100644
--- a/lib/bookmarks/bookmarkitem.h
+++ b/lib/bookmarks/bookmarkitem.h
@@ -17,8 +17,8 @@ class BookmarkItem
{
public:
enum Type {
- Folder,
- Bookmark,
+ Folder = 0,
+ Bookmark = 1,
};
enum Fields {
@@ -39,7 +39,7 @@ public:
bool removeChildAt(int index, int count = 1);
BookmarkItem *takeChild(int index, BookmarkItem *newParent);
- BookmarkItem *child(int index);
+ BookmarkItem *child(int index) const;
int childCount() const;
QVariant data(int column) const;
@@ -65,4 +65,4 @@ private:
QVector<QVariant> m_data;
};
-#endif //SMOLBOTE_BOOKMARKITEM_H
+#endif // SMOLBOTE_BOOKMARKITEM_H
diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/bookmarkmodel.cpp
index 3b57fea..b55dcdd 100644
--- a/lib/bookmarks/bookmarkmodel.cpp
+++ b/lib/bookmarks/bookmarkmodel.cpp
@@ -8,6 +8,9 @@
#include "bookmarkmodel.h"
#include "bookmarkitem.h"
+#include "xbel.h"
+#include <QBuffer>
+#include <QMimeData>
BookmarkModel::BookmarkModel(QObject *parent)
: QAbstractItemModel(parent)
@@ -35,12 +38,15 @@ QVariant BookmarkModel::data(const QModelIndex &index, int role) const
if(role == Qt::DecorationRole && index.column() == 0)
return static_cast<BookmarkItem *>(index.internalPointer())->icon();
+
else if(role == Qt::ToolTipRole)
return static_cast<BookmarkItem *>(index.internalPointer())->tooltip();
- else if(role != Qt::DisplayRole)
- return QVariant();
- return static_cast<BookmarkItem *>(index.internalPointer())->data(index.column());
+ else if(role == Qt::DisplayRole)
+ return static_cast<BookmarkItem *>(index.internalPointer())->data(index.column());
+
+ else
+ return QVariant();
}
QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) const
@@ -56,10 +62,15 @@ QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) con
bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
- if(!index.isValid() || role != Qt::DisplayRole)
+ if(!index.isValid())
return false;
- bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
+ bool success = false;
+
+ if(role == Qt::DisplayRole) {
+ success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
+ }
+
if(success)
emit dataChanged(index, index, { role });
return success;
@@ -81,7 +92,7 @@ Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const
if(getItem(index)->type() == BookmarkItem::Folder)
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
else
- return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled;
+ return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemNeverHasChildren;
}
bool BookmarkModel::isItemExpanded(const QModelIndex &index) const
@@ -100,24 +111,20 @@ int BookmarkModel::rowCount(const QModelIndex &index) const
return getItem(index)->childCount();
}
-bool BookmarkModel::insertRows(int position, int rows, const QModelIndex &parent)
+bool BookmarkModel::appendBookmark(const QString &title, const QString &url, const QModelIndex &parent)
{
auto *parentItem = getItem(parent);
- // if position is negative, or past the number of children in the item
- if(position < 0 || position > parentItem->childCount())
- return false;
-
- beginInsertRows(parent, position, position + rows - 1);
- for(int i = 0; i < rows; ++i) {
- parentItem->insertChild(position + i, new BookmarkItem({}, BookmarkItem::Bookmark, parentItem));
- }
+ int row = parentItem->childCount();
+ beginInsertRows(parent, row, row);
+ parentItem->appendChild(new BookmarkItem({ title, url }, BookmarkItem::Bookmark, parentItem));
endInsertRows();
return true;
}
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);
@@ -126,36 +133,10 @@ bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent
return success;
}
-bool BookmarkModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
-{
- auto *sourceParentItem = getItem(sourceParent);
- auto *destinationParentItem = getItem(destinationParent);
-
- bool success = true;
- beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
- for(int i = 0; i < count; ++i) {
- auto *child = sourceParentItem->takeChild(sourceRow, destinationParentItem);
- if(!destinationParentItem->insertChild(destinationChild, child)) {
- success = false;
- break;
- }
- }
- endMoveRows();
- return success;
-}
-
int BookmarkModel::columnCount(const QModelIndex &index) const
{
+ Q_UNUSED(index);
return 2;
- // if(!index.isValid())
- // return rootItem->columnCount();
- // else
- // return static_cast<BookmarkItem *>(index.internalPointer())->columnCount();
-}
-
-Qt::DropActions BookmarkModel::supportedDropActions() const
-{
- return Qt::MoveAction;
}
QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) const
@@ -184,6 +165,26 @@ QModelIndex BookmarkModel::parent(const QModelIndex &index) const
return createIndex(parentItem->row(), 0, parentItem);
}
+
+inline QStringList searchThrough(const QString &term, BookmarkItem *item)
+{
+ QStringList results;
+ for(int i = 0; i < item->childCount(); ++i) {
+ auto *child = item->child(i);
+ if(child->type() == BookmarkItem::Bookmark && child->data(BookmarkItem::Href).toString().contains(term))
+ results.append(child->data(BookmarkItem::Href).toString());
+ else if(child->type() == BookmarkItem::Folder)
+ results.append(searchThrough(term, child));
+ }
+ return results;
+}
+
+QStringList BookmarkModel::search(const QString &term) const
+{
+ // TODO tag searching
+ return searchThrough(term, rootItem);
+}
+
BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const
{
if(!index.isValid())
@@ -191,3 +192,83 @@ BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const
else
return static_cast<BookmarkItem *>(index.internalPointer());
}
+
+/*
+ * Drag'n'Drop implementation
+ * How drag and drop actually works: the view encodes the data of the original
+ * item (through ::mimeData), and then uses ::dropMimeData to create the new
+ * item. If successful, the old item is removed (through ::removeRows).
+ * This means that the encoding and decoding needs to be provided. In this case,
+ * this is done through xbel.
+ */
+
+Qt::DropActions BookmarkModel::supportedDropActions() const
+{
+ return Qt::MoveAction;
+}
+
+QStringList BookmarkModel::mimeTypes() const
+{
+ return { mimeType };
+}
+
+QMimeData *BookmarkModel::mimeData(const QModelIndexList &indexes) const
+{
+ auto *mimeData = new QMimeData();
+ QByteArray data;
+ XbelWriter writer;
+
+ QDataStream stream(&data, QIODevice::WriteOnly);
+ for(const QModelIndex &index : indexes) {
+ if(index.isValid() && index.column() == 0) {
+ QByteArray encodedData;
+ QBuffer buffer(&encodedData);
+ buffer.open(QIODevice::WriteOnly);
+
+ writer.write(&buffer, getItem(index));
+
+ stream << encodedData;
+ }
+ }
+ mimeData->setData(mimeType, data);
+ return mimeData;
+}
+bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent)
+{
+ if(action == Qt::IgnoreAction)
+ return true;
+
+ if(action != Qt::MoveAction)
+ return false;
+
+ if(!mimeData->hasFormat(mimeType) || column > 0)
+ return false;
+
+ QByteArray data = mimeData->data(mimeType);
+ QDataStream stream(&data, QIODevice::ReadOnly);
+ if(stream.atEnd())
+ return false;
+
+ while(!stream.atEnd()) {
+ QByteArray encodedData;
+ stream >> encodedData;
+
+ QBuffer buffer(&encodedData);
+ buffer.open(QIODevice::ReadOnly);
+
+ XbelReader reader(&buffer);
+ auto *fakeRoot = new BookmarkItem({}, BookmarkItem::Folder, nullptr);
+ auto *parentItem = getItem(parent);
+ reader.read(fakeRoot);
+
+ beginInsertRows(parent, row, row + fakeRoot->childCount() - 1);
+ for(int i = 0; i < fakeRoot->childCount(); ++i) {
+ auto *child = fakeRoot->takeChild(0, parentItem);
+ parentItem->insertChild(row, child);
+ }
+ endInsertRows();
+
+ delete fakeRoot;
+ }
+ return true;
+}
diff --git a/lib/bookmarks/bookmarkmodel.h b/lib/bookmarks/bookmarkmodel.h
index 90fdb2a..ea51caf 100644
--- a/lib/bookmarks/bookmarkmodel.h
+++ b/lib/bookmarks/bookmarkmodel.h
@@ -9,8 +9,11 @@
#ifndef SMOLBOTE_BOOKMARKMODEL_H
#define SMOLBOTE_BOOKMARKMODEL_H
-#include <QAbstractItemModel>
#include "bookmarkitem.h"
+#include <QAbstractItemModel>
+
+/* TODO BookmarkModel: moving items internally instead of copying during drag-drop
+ */
class BookmarkModel : public QAbstractItemModel
{
@@ -30,22 +33,30 @@ public:
bool isItemExpanded(const QModelIndex &index) const;
int rowCount(const QModelIndex &index) const override;
- bool insertRows(int position, int rows, const QModelIndex &parent) override;
+ bool appendBookmark(const QString &title, const QString &url, const QModelIndex &parent);
bool removeRows(int position, int rows, const QModelIndex &parent) override;
- bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
int columnCount(const QModelIndex &index) const override;
+
Qt::DropActions supportedDropActions() const override;
+ QStringList mimeTypes() const override;
+ QMimeData *mimeData(const QModelIndexList &indexes) const override;
+ bool dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override;
- BookmarkItem *root() {
+ BookmarkItem *root()
+ {
return rootItem;
}
+ QStringList search(const QString &term) const;
+
private:
+ const QLatin1Literal mimeType = QLatin1Literal("application/xbel");
+
BookmarkItem *getItem(const QModelIndex &index) const;
BookmarkItem *rootItem;
};
-#endif //SMOLBOTE_BOOKMARKMODEL_H
+#endif // SMOLBOTE_BOOKMARKMODEL_H
diff --git a/lib/bookmarks/bookmarksform.ui b/lib/bookmarks/bookmarksform.ui
index 491bdc3..45057ac 100644
--- a/lib/bookmarks/bookmarksform.ui
+++ b/lib/bookmarks/bookmarksform.ui
@@ -53,7 +53,7 @@
</layout>
</item>
<item>
- <widget class="BookmarksView" name="treeWidget">
+ <widget class="BookmarksView" name="treeView">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
@@ -66,25 +66,12 @@
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
- <property name="columnCount">
- <number>2</number>
- </property>
<attribute name="headerDefaultSectionSize">
<number>200</number>
</attribute>
<attribute name="headerMinimumSectionSize">
- <number>100</number>
+ <number>50</number>
</attribute>
- <column>
- <property name="text">
- <string notr="true">Title</string>
- </property>
- </column>
- <column>
- <property name="text">
- <string>Address</string>
- </property>
- </column>
</widget>
</item>
<item>
@@ -140,7 +127,7 @@
<customwidgets>
<customwidget>
<class>BookmarksView</class>
- <extends>QTreeWidget</extends>
+ <extends>QTreeView</extends>
<header>bookmarksview.h</header>
</customwidget>
</customwidgets>
diff --git a/lib/bookmarks/bookmarksview.cpp b/lib/bookmarks/bookmarksview.cpp
index a92a3a7..632a14c 100644
--- a/lib/bookmarks/bookmarksview.cpp
+++ b/lib/bookmarks/bookmarksview.cpp
@@ -7,46 +7,17 @@
*/
#include "bookmarksview.h"
-
-inline QTreeWidgetItem *createTreeWidgetItem(QTreeWidget *widget, QTreeWidgetItem *parentItem)
-{
- if(parentItem)
- return new QTreeWidgetItem(parentItem);
- else
- return new QTreeWidgetItem(widget);
-}
+#include <QDropEvent>
BookmarksView::BookmarksView(QWidget *parent)
- : QTreeWidget(parent)
+ : QTreeView(parent)
{
- QStyle *qStyle = style();
- folderIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
- folderIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
- bookmarkIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_FileIcon));
+ qDebug("BookmarksView constructor!");
+ //setDragDropOverwriteMode(true);
}
-
-QTreeWidgetItem *BookmarksView::createBookmark(QTreeWidgetItem *parentItem)
+void BookmarksView::dropEvent(QDropEvent *event)
{
- QTreeWidgetItem *item = createTreeWidgetItem(this, parentItem);
- item->setFlags(item->flags().setFlag(Qt::ItemIsDropEnabled, false) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled);
- item->setData(0, Qt::UserRole, Type::Bookmark);
- item->setIcon(0, bookmarkIcon);
- return item;
-}
-
-QTreeWidgetItem *BookmarksView::createFolder(QTreeWidgetItem *parentItem)
-{
- QTreeWidgetItem *item = createTreeWidgetItem(this, parentItem);
- item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
- item->setData(0, Qt::UserRole, Type::Folder);
- item->setIcon(0, folderIcon);
- return item;
-}
-
-BookmarksView::Type BookmarksView::itemType(QTreeWidgetItem *item) const
-{
- if(item == nullptr)
- return Invalid;
-
- return item->data(0, Qt::UserRole).value<Type>();
+ qDebug("drop event action was: %i", event->dropAction());
+ //event->setDropAction(Qt::MoveAction);
+ QTreeView::dropEvent(event);
}
diff --git a/lib/bookmarks/bookmarksview.h b/lib/bookmarks/bookmarksview.h
index f99fd2a..ab0ab86 100644
--- a/lib/bookmarks/bookmarksview.h
+++ b/lib/bookmarks/bookmarksview.h
@@ -9,30 +9,20 @@
#ifndef SMOLBOTE_BOOKMARKSVIEW_H
#define SMOLBOTE_BOOKMARKSVIEW_H
-#include <QTreeWidget>
+#include <QTreeView>
-class BookmarksView : public QTreeWidget
+class BookmarksView : public QTreeView
{
Q_OBJECT
public:
- enum Type {
- Invalid,
- Folder,
- Bookmark
- };
- Q_ENUM(Type)
explicit BookmarksView(QWidget *parent = nullptr);
- QTreeWidgetItem *createBookmark(QTreeWidgetItem *parentItem);
- QTreeWidgetItem *createFolder(QTreeWidgetItem *parentItem);
-
- Type itemType(QTreeWidgetItem *item) const;
+protected:
+ void dropEvent(QDropEvent *event) override;
private:
- QIcon folderIcon;
- QIcon bookmarkIcon;
};
#endif // SMOLBOTE_BOOKMARKSVIEW_H
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp
index 8e6677c..a7ea6d1 100644
--- a/lib/bookmarks/bookmarkswidget.cpp
+++ b/lib/bookmarks/bookmarkswidget.cpp
@@ -50,26 +50,20 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
// Testing model/view
#ifdef QT_DEBUG
{
- auto *treeView = new QTreeView(this);
- treeView->setDragEnabled(true);
- treeView->setAcceptDrops(true);
- treeView->setDropIndicatorShown(true);
- treeView->setDragDropMode(QAbstractItemView::InternalMove);
- ui->verticalLayout->addWidget(treeView);
- auto *model = new BookmarkModel(this);
+ model = new BookmarkModel(this);
QFile bookmarksFile(path);
if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
XbelReader xbel(&bookmarksFile);
- xbel.read(model);
+ xbel.read(model->root());
bookmarksFile.close();
}
- treeView->setModel(model);
+ ui->treeView->setModel(model);
- expandChildren(treeView, model, QModelIndex());
+ expandChildren(ui->treeView, model, QModelIndex());
- connect(treeView, &QTreeView::activated, this, [this, model](const QModelIndex &index) {
+ connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
if(index.column() == 1)
emit openUrl(index.data(Qt::DisplayRole).toUrl());
else {
@@ -82,84 +76,6 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
//
m_bookmarksPath = path;
-
- // read bookmarks
- {
- QFile bookmarksFile(m_bookmarksPath);
- if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
- XbelReader xbel(&bookmarksFile);
- xbel.read(ui->treeWidget);
- //qDebug("Reading bookmarks [%s] %s", qUtf8Printable(path), m_model->read(&xbel) ? "ok" : "failed");
- bookmarksFile.close();
- }
- }
-
- // open bookmark action
- connect(ui->treeWidget, &QTreeWidget::itemActivated, this, [this](QTreeWidgetItem *item, int column) {
- Q_UNUSED(column);
- if(ui->treeWidget->itemType(item) == BookmarksView::Bookmark)
- emit openUrl(QUrl::fromUserInput(item->text(1)));
- });
-
- // add bookmark action
- connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
- auto *parentItem = ui->treeWidget->currentItem();
- if(ui->treeWidget->itemType(parentItem) == BookmarksView::Bookmark)
- parentItem = parentItem->parent();
-
- auto *bookmark = ui->treeWidget->createBookmark(parentItem);
- bookmark->setText(0, tr("title"));
- bookmark->setText(1, tr("href"));
-
- this->m_isChanged = true;
- });
-
- // add folder action
- connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
- auto *parentItem = ui->treeWidget->currentItem();
- if(ui->treeWidget->itemType(parentItem) == BookmarksView::Bookmark)
- parentItem = parentItem->parent();
-
- auto *folder = ui->treeWidget->createFolder(parentItem);
- folder->setText(0, tr("title"));
-
- this->m_isChanged = true;
- });
-
- // delete item action
- connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
- delete ui->treeWidget->currentItem();
- this->m_isChanged = true;
- });
-
- // edit item action
- connect(ui->treeWidget, &QTreeWidget::currentItemChanged, this, [this](QTreeWidgetItem *current, QTreeWidgetItem *previous) {
- // save the previous item
- if(previous) {
- if(ui->treeWidget->itemType(previous) == BookmarksView::Folder) {
- previous->setText(0, ui->folderTitle->text());
- ui->folder_groupBox->setVisible(false);
- } else if(ui->treeWidget->itemType(previous) == BookmarksView::Bookmark) {
- previous->setText(0, ui->bookmarkTitle->text());
- previous->setText(1, ui->bookmarkHref->text());
- ui->bookmark_groupBox->setVisible(false);
- }
- }
-
- // edit current item
- if(current) {
- if(ui->treeWidget->itemType(current) == BookmarksView::Folder) {
- ui->folderTitle->setText(current->text(0));
- ui->folder_groupBox->setVisible(true);
- } else if(ui->treeWidget->itemType(current) == BookmarksView::Bookmark) {
- ui->bookmarkTitle->setText(current->text(0));
- ui->bookmarkHref->setText(current->text(1));
- ui->bookmark_groupBox->setVisible(true);
- }
- }
-
- this->m_isChanged = true;
- });
}
BookmarksWidget::~BookmarksWidget()
@@ -173,34 +89,31 @@ void BookmarksWidget::save()
return;
}
- QFile bookmarksFile(m_bookmarksPath);
- if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
- XbelWriter xbel(&bookmarksFile);
- xbel.write(ui->treeWidget);
- bookmarksFile.flush();
- bookmarksFile.close();
-
- m_isChanged = false;
- }
+// QFile bookmarksFile(m_bookmarksPath);
+// if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
+// XbelWriter xbel(&bookmarksFile);
+// xbel.write(ui->treeWidget);
+// bookmarksFile.flush();
+// bookmarksFile.close();
+//
+// m_isChanged = false;
+// }
}
void BookmarksWidget::addBookmark(const QString &title, const QString &url)
{
- auto *bookmark = ui->treeWidget->createBookmark(nullptr);
- bookmark->setText(0, title);
- bookmark->setText(1, url);
-
+ model->appendBookmark(title, url, QModelIndex());
m_isChanged = true;
}
void BookmarksWidget::search(const QString &term, std::function<void(QStringList &)> callback) const
{
- QStringList ret;
- const QList<QTreeWidgetItem *> res = ui->treeWidget->findItems(term, Qt::MatchContains | Qt::MatchRecursive, 1);
- for(const QTreeWidgetItem *i : res) {
- if(!i->text(1).isEmpty())
- ret.append(i->text(1));
- }
+ QStringList ret = model->search(term);
+// const QList<QTreeWidgetItem *> res = ui->treeWidget->findItems(term, Qt::MatchContains | Qt::MatchRecursive, 1);
+// for(const QTreeWidgetItem *i : res) {
+// if(!i->text(1).isEmpty())
+// ret.append(i->text(1));
+// }
callback(ret);
}
diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h
index 496bde9..4a377f4 100644
--- a/lib/bookmarks/bookmarkswidget.h
+++ b/lib/bookmarks/bookmarkswidget.h
@@ -20,6 +20,7 @@ class BookmarksDialog;
}
class BookmarksView;
+class BookmarkModel;
class BookmarksWidget : public QWidget
{
Q_OBJECT
@@ -40,6 +41,8 @@ private:
Ui::BookmarksDialog *ui;
bool m_isChanged = false;
QString m_bookmarksPath;
+ BookmarkModel *model;
+
};
#endif // BOOKMARKSDIALOG_H
diff --git a/lib/bookmarks/forms/editbookmarkdialog.cpp b/lib/bookmarks/forms/editbookmarkdialog.cpp
index 47aa0b7..7bc1331 100644
--- a/lib/bookmarks/forms/editbookmarkdialog.cpp
+++ b/lib/bookmarks/forms/editbookmarkdialog.cpp
@@ -18,28 +18,53 @@ EditBookmarkDialog::EditBookmarkDialog(BookmarkModel *model, const QModelIndex &
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose, true);
+ Q_CHECK_PTR(model);
+ m_model = model;
+ m_index = index;
+
ui->title->setText(model->data(index, BookmarkItem::Title, Qt::DisplayRole).toString());
- connect(ui->title, &QLineEdit::editingFinished, this, [this, model, index]() {
- model->setData(index, ui->title->text(), BookmarkItem::Title, Qt::DisplayRole);
+ connect(ui->title, &QLineEdit::editingFinished, this, [this]() {
+ titleChanged = true;
});
ui->address->setText(model->data(index, BookmarkItem::Href, Qt::DisplayRole).toString());
- connect(ui->address, &QLineEdit::editingFinished, this, [this, model, index]() {
- model->setData(index, ui->address->text(), BookmarkItem::Href, Qt::DisplayRole);
+ connect(ui->address, &QLineEdit::editingFinished, this, [this]() {
+ hrefChanged = true;
});
ui->tags->setText(model->data(index, BookmarkItem::Tags, Qt::DisplayRole).toStringList().join(", "));
- connect(ui->tags, &QLineEdit::editingFinished, this, [this, model, index]() {
- model->setData(index, ui->tags->text().split(", "), BookmarkItem::Tags, Qt::DisplayRole);
+ connect(ui->tags, &QLineEdit::editingFinished, this, [this]() {
+ tagsChanged = true;
});
ui->description->setPlainText(model->data(index, BookmarkItem::Description, Qt::DisplayRole).toString());
- connect(ui->description, &QPlainTextEdit::textChanged, this, [this, model, index]() {
- model->setData(index, ui->description->toPlainText(), BookmarkItem::Description, Qt::DisplayRole);
+ connect(ui->description, &QPlainTextEdit::textChanged, this, [this]() {
+ descriptionChanged = true;
});
+
+ connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &EditBookmarkDialog::saveChanges);
}
EditBookmarkDialog::~EditBookmarkDialog()
{
delete ui;
}
+
+void EditBookmarkDialog::saveChanges()
+{
+ if(titleChanged) {
+ m_model->setData(m_index, ui->title->text(), BookmarkItem::Title, Qt::DisplayRole);
+ }
+
+ if(hrefChanged) {
+ m_model->setData(m_index, ui->address->text(), BookmarkItem::Href, Qt::DisplayRole);
+ }
+
+ if(tagsChanged) {
+ m_model->setData(m_index, ui->tags->text().split(", "), BookmarkItem::Tags, Qt::DisplayRole);
+ }
+
+ 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/forms/editbookmarkdialog.h b/lib/bookmarks/forms/editbookmarkdialog.h
index 968b6af..863eaf8 100644
--- a/lib/bookmarks/forms/editbookmarkdialog.h
+++ b/lib/bookmarks/forms/editbookmarkdialog.h
@@ -10,6 +10,7 @@
#define SMOLBOTE_EDITBOOKMARKDIALOG_H
#include <QDialog>
+#include <QModelIndex>
namespace Ui
{
@@ -25,8 +26,19 @@ public:
explicit EditBookmarkDialog(BookmarkModel *model, const QModelIndex &index, QWidget *parent = nullptr);
~EditBookmarkDialog() override;
+public slots:
+ void saveChanges();
+
private:
Ui::EditBookmarkDialog *ui;
+
+ BookmarkModel *m_model;
+ QModelIndex m_index;
+
+ bool titleChanged = false;
+ bool hrefChanged = false;
+ bool tagsChanged = false;
+ bool descriptionChanged = false;
};
#endif // SMOLBOTE_EDITBOOKMARKDIALOG_H
diff --git a/lib/bookmarks/forms/editbookmarkdialog.ui b/lib/bookmarks/forms/editbookmarkdialog.ui
index 1861fcc..b988394 100644
--- a/lib/bookmarks/forms/editbookmarkdialog.ui
+++ b/lib/bookmarks/forms/editbookmarkdialog.ui
@@ -6,12 +6,12 @@
<rect>
<x>0</x>
<y>0</y>
- <width>262</width>
+ <width>400</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
- <string>Dialog</string>
+ <string>Edit Bookmark</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp
index a1bf2f6..c23f0be 100644
--- a/lib/bookmarks/xbel.cpp
+++ b/lib/bookmarks/xbel.cpp
@@ -18,28 +18,6 @@ XbelReader::XbelReader(QIODevice *file)
m_file = file;
}
-void readChildElements(QXmlStreamReader &reader, BookmarksView *widget, QTreeWidgetItem *parentItem = nullptr)
-{
- while(reader.readNextStartElement()) {
- if(reader.name() == "title") {
- Q_CHECK_PTR(parentItem);
- parentItem->setText(0, reader.readElementText());
-
- } else if(reader.name() == "folder") {
- auto *item = widget->createFolder(parentItem);
- widget->setItemExpanded(item, !(reader.attributes().value("folded") == QLatin1String("yes")));
- readChildElements(reader, widget, item);
-
- } else if(reader.name() == "bookmark") {
- auto *item = widget->createBookmark(parentItem);
- item->setText(1, reader.attributes().value("href").toString());
- readChildElements(reader, widget, item);
-
- } else {
- reader.skipCurrentElement();
- }
- }
-}
void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent)
{
@@ -65,20 +43,7 @@ void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent)
}
}
-void XbelReader::read(BookmarksView *treeWidget)
-{
- QXmlStreamReader qXmlStreamReader(m_file);
-
- if(qXmlStreamReader.readNextStartElement()) {
- if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) {
- return;
- }
-
- readChildElements(qXmlStreamReader, treeWidget);
- }
-}
-
-void XbelReader::read(BookmarkModel *model)
+void XbelReader::read(BookmarkItem *item)
{
QXmlStreamReader qXmlStreamReader(m_file);
@@ -87,46 +52,40 @@ void XbelReader::read(BookmarkModel *model)
return;
}
- readChildElements(qXmlStreamReader, model->root());
+ readChildElements(qXmlStreamReader, item);
}
}
-XbelWriter::XbelWriter(QIODevice *file)
+XbelWriter::XbelWriter()
{
- Q_CHECK_PTR(file);
- m_file = file;
}
-void writeChildElements(QXmlStreamWriter &writer, BookmarksView *treeWidget, QTreeWidgetItem *parentItem)
+inline void writeChildElement(QXmlStreamWriter &writer, const BookmarkItem *item)
{
- switch(treeWidget->itemType(parentItem)) {
- case BookmarksView::Folder:
- writer.writeStartElement("folder");
- writer.writeAttribute("folded", !treeWidget->isItemExpanded(parentItem) ? "yes" : "no");
- writer.writeTextElement("title", parentItem->text(0));
-
- for(int i = 0; i < parentItem->childCount(); ++i) {
- writeChildElements(writer, treeWidget, parentItem->child(i));
- }
-
- writer.writeEndElement();
- break;
-
- case BookmarksView::Bookmark:
- writer.writeStartElement("bookmark");
- writer.writeAttribute("href", parentItem->text(1));
- writer.writeTextElement("title", parentItem->text(0));
- writer.writeEndElement();
- break;
-
- case BookmarksView::Invalid:
- break;
+ switch (item->type()) {
+ case BookmarkItem::Folder:
+ writer.writeStartElement("folder");
+ writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no");
+ writer.writeTextElement("title", item->data(BookmarkItem::Title).toString());
+
+ for(int i = 0; i < item->childCount(); ++i) {
+ writeChildElement(writer, item->child(i));
+ }
+
+ writer.writeEndElement();
+ break;
+
+ case BookmarkItem::Bookmark:
+ writer.writeStartElement("bookmark");
+ writer.writeAttribute("href", item->data(BookmarkItem::Href).toString());
+ writer.writeTextElement("title", item->data(BookmarkItem::Title).toString());
+ writer.writeEndElement();
+ break;
}
}
-
-void XbelWriter::write(BookmarksView *treeWidget)
+void XbelWriter::write(QIODevice *device, const BookmarkItem *item)
{
- QXmlStreamWriter xmlWriter(m_file);
+ QXmlStreamWriter xmlWriter(device);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
@@ -135,9 +94,7 @@ void XbelWriter::write(BookmarksView *treeWidget)
xmlWriter.writeStartElement("xbel");
xmlWriter.writeAttribute("version", "1.0");
- for(int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
- writeChildElements(xmlWriter, treeWidget, treeWidget->topLevelItem(i));
- }
+ writeChildElement(xmlWriter, item);
xmlWriter.writeEndDocument();
}
diff --git a/lib/bookmarks/xbel.h b/lib/bookmarks/xbel.h
index e285b70..e38ae98 100644
--- a/lib/bookmarks/xbel.h
+++ b/lib/bookmarks/xbel.h
@@ -19,8 +19,7 @@ class XbelReader
{
public:
explicit XbelReader(QIODevice *file);
- void read(BookmarksView *treeWidget);
- void read(BookmarkModel *model);
+ void read(BookmarkItem *model);
private:
QIODevice *m_file;
@@ -29,11 +28,10 @@ private:
class XbelWriter
{
public:
- explicit XbelWriter(QIODevice *file);
- void write(BookmarksView *treeWidget);
+ explicit XbelWriter();
+ void write(QIODevice *device, const BookmarkItem *item);
private:
- QIODevice *m_file;
};
#endif // XBELREADER_H