diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-15 13:19:46 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-15 13:19:46 +0200 |
commit | 9643e146c35a3a3e9abf0bf332c7c1efd00be9c2 (patch) | |
tree | e69c2d5ce92786a566c5e2c997f4ce516c35d238 /src/bookmarks | |
parent | Move/rename files for readability (diff) | |
download | smolbote-9643e146c35a3a3e9abf0bf332c7c1efd00be9c2.tar.xz |
Move BookmarksWidget out of libbookmarks
Diffstat (limited to 'src/bookmarks')
-rw-r--r-- | src/bookmarks/bookmarksform.ui | 129 | ||||
-rw-r--r-- | src/bookmarks/bookmarkswidget.cpp | 138 | ||||
-rw-r--r-- | src/bookmarks/bookmarkswidget.h | 50 | ||||
-rw-r--r-- | src/bookmarks/editbookmarkdialog.cpp | 70 | ||||
-rw-r--r-- | src/bookmarks/editbookmarkdialog.h | 44 | ||||
-rw-r--r-- | src/bookmarks/editbookmarkdialog.ui | 108 |
6 files changed, 539 insertions, 0 deletions
diff --git a/src/bookmarks/bookmarksform.ui b/src/bookmarks/bookmarksform.ui new file mode 100644 index 0000000..a003e53 --- /dev/null +++ b/src/bookmarks/bookmarksform.ui @@ -0,0 +1,129 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>BookmarksDialog</class> + <widget class="QWidget" name="BookmarksDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>420</width> + <height>600</height> + </rect> + </property> + <property name="windowTitle"> + <string>Bookmarks</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QToolButton" name="addFolder_toolButton"> + <property name="text"> + <string>Add Folder</string> + </property> + </widget> + </item> + <item> + <widget class="QToolButton" name="addBookmark_toolButton"> + <property name="text"> + <string>Add Bookmark</string> + </property> + </widget> + </item> + <item> + <widget class="QToolButton" name="deleteItem_toolButton"> + <property name="text"> + <string>Delete Item</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QTreeView" name="treeView"> + <property name="editTriggers"> + <set>QAbstractItemView::NoEditTriggers</set> + </property> + <property name="dragEnabled"> + <bool>true</bool> + </property> + <property name="dragDropMode"> + <enum>QAbstractItemView::DragDrop</enum> + </property> + <property name="defaultDropAction"> + <enum>Qt::MoveAction</enum> + </property> + <attribute name="headerDefaultSectionSize"> + <number>200</number> + </attribute> + <attribute name="headerMinimumSectionSize"> + <number>50</number> + </attribute> + </widget> + </item> + <item> + <widget class="QGroupBox" name="bookmark_groupBox"> + <property name="title"> + <string>Edit Bookmark</string> + </property> + <layout class="QFormLayout" name="formLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="bookmarkTitle_label"> + <property name="text"> + <string>Title</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="bookmarkHref_label"> + <property name="text"> + <string>Address</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="bookmarkTitle"/> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="bookmarkHref"/> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="folder_groupBox"> + <property name="title"> + <string>Edit Folder</string> + </property> + <layout class="QFormLayout" name="formLayout_2"> + <item row="0" column="0"> + <widget class="QLabel" name="folderTitle_label"> + <property name="text"> + <string>Name</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="folderTitle"/> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/src/bookmarks/bookmarkswidget.cpp b/src/bookmarks/bookmarkswidget.cpp new file mode 100644 index 0000000..26fdddf --- /dev/null +++ b/src/bookmarks/bookmarkswidget.cpp @@ -0,0 +1,138 @@ +/* + * 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 "bookmarkswidget.h" +#include "editbookmarkdialog.h" +#include "ui_bookmarksform.h" +#include "bookmarkformat.h" +#include <QTreeView> +#include <QUrl> + +inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIndex &rootIndex) +{ + for(int i = 0; i < model->rowCount(rootIndex); ++i) { + QModelIndex idx = model->index(i, 0, rootIndex); + if(model->isItemExpanded(idx)) + view->expand(idx); + + // check if index has children + if(model->rowCount(idx) > 0) + expandChildren(view, model, idx); + } +} + +BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) + : QWidget(parent) + , ui(new Ui::BookmarksDialog) +{ + // make sure this dialog does not get deleted on close + setAttribute(Qt::WA_DeleteOnClose, false); + + ui->setupUi(this); + ui->bookmark_groupBox->setVisible(false); + ui->folder_groupBox->setVisible(false); + + ui->addFolder_toolButton->setIcon(style()->standardPixmap(QStyle::SP_DirIcon)); + ui->addBookmark_toolButton->setIcon(style()->standardPixmap(QStyle::SP_FileIcon)); + ui->deleteItem_toolButton->setIcon(style()->standardPixmap(QStyle::SP_TrashIcon)); + ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete); + + model = new BookmarkModel(this); + m_bookmarksPath = path; + QFile bookmarksFile(m_bookmarksPath); + if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + BookmarkFormat<XbelFormat>(&bookmarksFile) >> model; + } + model->resetModified(); + + ui->treeView->setModel(model); + 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 + editBookmark(index); + }); + + connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) { + model->setItemExpanded(index, true); + }); + connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) { + model->setItemExpanded(index, false); + }); + + ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->treeView, &QTreeView::customContextMenuRequested, this, [this](const QPoint &pos) { + const QModelIndex idx = ui->treeView->indexAt(pos); + if(idx.isValid()) { + const QUrl url = model->data(idx, 1, Qt::DisplayRole).toUrl(); + const QPoint _pos = ui->treeView->viewport()->mapToGlobal(pos); + + emit showContextMenu(url, _pos); + } + }); + + // 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); + }); + + // 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); + }); + + // deleteItem + connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() { + const QModelIndex idx = ui->treeView->currentIndex(); + model->removeRow(idx.row(), idx.parent()); + }); +} + +BookmarksWidget::~BookmarksWidget() +{ + delete ui; +} + +void BookmarksWidget::editBookmark(const QModelIndex &index) +{ + auto *dlg = new EditBookmarkDialog(model, index, this); + dlg->exec(); +} + +void BookmarksWidget::save() +{ + if(!model->isModified()) + return; + + QFile bookmarksFile(m_bookmarksPath); + if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + BookmarkFormat<XbelFormat> f(&bookmarksFile); + model >> f; + bookmarksFile.flush(); + } +} + +void BookmarksWidget::addBookmark(const QString &title, const QString &url) +{ + model->appendBookmark(title, url, QModelIndex()); +} + +void BookmarksWidget::search(const QString &term, const std::function<void(QStringList &)> &callback) const +{ + QStringList ret = model->search(term); + callback(ret); +} diff --git a/src/bookmarks/bookmarkswidget.h b/src/bookmarks/bookmarkswidget.h new file mode 100644 index 0000000..149d2a6 --- /dev/null +++ b/src/bookmarks/bookmarkswidget.h @@ -0,0 +1,50 @@ +/* + * 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 + */ + +#ifndef BOOKMARKSDIALOG_H +#define BOOKMARKSDIALOG_H + +#include <QFile> +#include <QShortcut> +#include <QWidget> +#include <functional> + +namespace Ui +{ +class BookmarksDialog; +} + +class BookmarksView; +class BookmarkModel; +class BookmarksWidget : public QWidget +{ + Q_OBJECT + +public: + explicit BookmarksWidget(const QString &path, QWidget *parent = nullptr); + ~BookmarksWidget() override; + +protected: + void editBookmark(const QModelIndex &index); + +signals: + void showContextMenu(const QUrl &url, const QPoint &pos); + void openUrl(const QUrl &url); + +public slots: + void save(); + void addBookmark(const QString &title, const QString &url); + void search(const QString &term, const std::function<void(QStringList &)> &callback) const; + +private: + Ui::BookmarksDialog *ui; + QString m_bookmarksPath; + BookmarkModel *model; +}; + +#endif // BOOKMARKSDIALOG_H diff --git a/src/bookmarks/editbookmarkdialog.cpp b/src/bookmarks/editbookmarkdialog.cpp new file mode 100644 index 0000000..7df90b8 --- /dev/null +++ b/src/bookmarks/editbookmarkdialog.cpp @@ -0,0 +1,70 @@ +/* + * 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 "editbookmarkdialog.h" +#include "bookmarkitem.h" +#include "bookmarkmodel.h" +#include "ui_editbookmarkdialog.h" + +EditBookmarkDialog::EditBookmarkDialog(BookmarkModel *model, const QModelIndex &index, QWidget *parent) + : QDialog(parent) + , ui(new Ui::EditBookmarkDialog) +{ + 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]() { + titleChanged = true; + }); + + ui->address->setText(model->data(index, BookmarkItem::Href, Qt::DisplayRole).toString()); + 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]() { + tagsChanged = true; + }); + + ui->description->setPlainText(model->data(index, BookmarkItem::Description, Qt::DisplayRole).toString()); + 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); + } +} diff --git a/src/bookmarks/editbookmarkdialog.h b/src/bookmarks/editbookmarkdialog.h new file mode 100644 index 0000000..fb53239 --- /dev/null +++ b/src/bookmarks/editbookmarkdialog.h @@ -0,0 +1,44 @@ +/* + * 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 + */ + +#ifndef SMOLBOTE_EDITBOOKMARKDIALOG_H +#define SMOLBOTE_EDITBOOKMARKDIALOG_H + +#include <QDialog> +#include <QModelIndex> + +namespace Ui +{ +class EditBookmarkDialog; +} + +class BookmarkModel; +class EditBookmarkDialog : public QDialog +{ + Q_OBJECT + +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/src/bookmarks/editbookmarkdialog.ui b/src/bookmarks/editbookmarkdialog.ui new file mode 100644 index 0000000..b988394 --- /dev/null +++ b/src/bookmarks/editbookmarkdialog.ui @@ -0,0 +1,108 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>EditBookmarkDialog</class> + <widget class="QDialog" name="EditBookmarkDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>320</height> + </rect> + </property> + <property name="windowTitle"> + <string>Edit Bookmark</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QFormLayout" name="formLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="title_label"> + <property name="text"> + <string>Title</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="title"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="address_label"> + <property name="text"> + <string>Address</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="address"/> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="tags_label"> + <property name="text"> + <string>Tags</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLineEdit" name="tags"/> + </item> + <item row="3" column="0"> + <widget class="QLabel" name="description_label"> + <property name="text"> + <string>Description</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QPlainTextEdit" name="description"/> + </item> + </layout> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Close|QDialogButtonBox::Save</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>EditBookmarkDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>248</x> + <y>254</y> + </hint> + <hint type="destinationlabel"> + <x>157</x> + <y>274</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>EditBookmarkDialog</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>316</x> + <y>260</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui> |