diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-25 16:49:56 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-25 16:49:56 +0200 |
commit | e0badcc7ef354b4bfdeedfde8f5ec3e82c27e741 (patch) | |
tree | cd997551fcd5c67c770d55f8182f678666fef240 /lib | |
parent | Bookmarks: integrate model/view (diff) | |
download | smolbote-e0badcc7ef354b4bfdeedfde8f5ec3e82c27e741.tar.xz |
Bookmarks: add tags and description fields to xbel
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bookmarks/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkitem.cpp | 6 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkitem.h | 7 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkmodel.cpp | 14 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksform.ui | 9 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksview.cpp | 23 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksview.h | 28 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkswidget.cpp | 23 | ||||
-rw-r--r-- | lib/bookmarks/xbel.cpp | 86 | ||||
-rw-r--r-- | lib/bookmarks/xbel.h | 33 |
10 files changed, 81 insertions, 150 deletions
diff --git a/lib/bookmarks/CMakeLists.txt b/lib/bookmarks/CMakeLists.txt index d2256c6..d8bdb2b 100644 --- a/lib/bookmarks/CMakeLists.txt +++ b/lib/bookmarks/CMakeLists.txt @@ -10,8 +10,6 @@ add_library(bookmarks bookmarksform.ui bookmarkswidget.cpp bookmarkswidget.h - bookmarksview.cpp - bookmarksview.h # xbel xbel.cpp xbel.h diff --git a/lib/bookmarks/bookmarkitem.cpp b/lib/bookmarks/bookmarkitem.cpp index ff78eb5..67b13b0 100644 --- a/lib/bookmarks/bookmarkitem.cpp +++ b/lib/bookmarks/bookmarkitem.cpp @@ -41,7 +41,7 @@ bool BookmarkItem::appendChild(BookmarkItem *childItem) { // Only folders can have children, so only append them on folders // This way, we don't need to add checks to the other methods - if(m_type == Folder) { + if(m_type == Folder || m_type == Root) { m_children.append(childItem); return true; } @@ -51,7 +51,7 @@ bool BookmarkItem::appendChild(BookmarkItem *childItem) bool BookmarkItem::insertChild(int position, BookmarkItem *childItem) { - if(m_type == Folder) { + if(m_type == Folder || m_type == Root) { m_children.insert(position, childItem); return true; } @@ -87,7 +87,7 @@ int BookmarkItem::childCount() const return m_children.count(); } -QVariant BookmarkItem::data(int column) const +QVariant BookmarkItem::data(Fields column) const { return m_data.value(column); } diff --git a/lib/bookmarks/bookmarkitem.h b/lib/bookmarks/bookmarkitem.h index fd5e67a..6f523dd 100644 --- a/lib/bookmarks/bookmarkitem.h +++ b/lib/bookmarks/bookmarkitem.h @@ -17,8 +17,9 @@ class BookmarkItem { public: enum Type { - Folder = 0, - Bookmark = 1, + Root, + Folder, + Bookmark, }; enum Fields { @@ -42,7 +43,7 @@ public: BookmarkItem *child(int index) const; int childCount() const; - QVariant data(int column) const; + QVariant data(Fields column) const; bool setData(Fields column, const QVariant &data); QIcon icon() const; diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/bookmarkmodel.cpp index b55dcdd..78e42b8 100644 --- a/lib/bookmarks/bookmarkmodel.cpp +++ b/lib/bookmarks/bookmarkmodel.cpp @@ -15,7 +15,7 @@ BookmarkModel::BookmarkModel(QObject *parent) : QAbstractItemModel(parent) { - rootItem = new BookmarkItem({ "Title", "Address" }, BookmarkItem::Folder, nullptr); + rootItem = new BookmarkItem({ tr("Title"), tr("Address") }, BookmarkItem::Root, nullptr); } BookmarkModel::~BookmarkModel() @@ -26,7 +26,7 @@ BookmarkModel::~BookmarkModel() QVariant BookmarkModel::headerData(int section, Qt::Orientation orientation, int role) const { if(orientation == Qt::Horizontal && role == Qt::DisplayRole) - return rootItem->data(section); + return rootItem->data(static_cast<BookmarkItem::Fields>(section)); return QVariant(); } @@ -43,7 +43,7 @@ QVariant BookmarkModel::data(const QModelIndex &index, int role) const return static_cast<BookmarkItem *>(index.internalPointer())->tooltip(); else if(role == Qt::DisplayRole) - return static_cast<BookmarkItem *>(index.internalPointer())->data(index.column()); + return static_cast<BookmarkItem *>(index.internalPointer())->data(static_cast<BookmarkItem::Fields>(index.column())); else return QVariant(); @@ -55,7 +55,7 @@ QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) con return QVariant(); if(role == Qt::DisplayRole) - return static_cast<BookmarkItem *>(index.internalPointer())->data(column); + return static_cast<BookmarkItem *>(index.internalPointer())->data(static_cast<BookmarkItem::Fields>(column)); return QVariant(); } @@ -216,7 +216,6 @@ 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) { @@ -225,7 +224,7 @@ QMimeData *BookmarkModel::mimeData(const QModelIndexList &indexes) const QBuffer buffer(&encodedData); buffer.open(QIODevice::WriteOnly); - writer.write(&buffer, getItem(index)); + Xbel::write(&buffer, getItem(index)); stream << encodedData; } @@ -256,10 +255,9 @@ bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction actio QBuffer buffer(&encodedData); buffer.open(QIODevice::ReadOnly); - XbelReader reader(&buffer); auto *fakeRoot = new BookmarkItem({}, BookmarkItem::Folder, nullptr); auto *parentItem = getItem(parent); - reader.read(fakeRoot); + Xbel::read(&buffer, fakeRoot); beginInsertRows(parent, row, row + fakeRoot->childCount() - 1); for(int i = 0; i < fakeRoot->childCount(); ++i) { diff --git a/lib/bookmarks/bookmarksform.ui b/lib/bookmarks/bookmarksform.ui index 45057ac..a003e53 100644 --- a/lib/bookmarks/bookmarksform.ui +++ b/lib/bookmarks/bookmarksform.ui @@ -53,7 +53,7 @@ </layout> </item> <item> - <widget class="BookmarksView" name="treeView"> + <widget class="QTreeView" name="treeView"> <property name="editTriggers"> <set>QAbstractItemView::NoEditTriggers</set> </property> @@ -124,13 +124,6 @@ </item> </layout> </widget> - <customwidgets> - <customwidget> - <class>BookmarksView</class> - <extends>QTreeView</extends> - <header>bookmarksview.h</header> - </customwidget> - </customwidgets> <resources/> <connections/> </ui> diff --git a/lib/bookmarks/bookmarksview.cpp b/lib/bookmarks/bookmarksview.cpp deleted file mode 100644 index 632a14c..0000000 --- a/lib/bookmarks/bookmarksview.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "bookmarksview.h" -#include <QDropEvent> - -BookmarksView::BookmarksView(QWidget *parent) - : QTreeView(parent) -{ - qDebug("BookmarksView constructor!"); - //setDragDropOverwriteMode(true); -} -void BookmarksView::dropEvent(QDropEvent *event) -{ - 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 deleted file mode 100644 index ab0ab86..0000000 --- a/lib/bookmarks/bookmarksview.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_BOOKMARKSVIEW_H -#define SMOLBOTE_BOOKMARKSVIEW_H - -#include <QTreeView> - -class BookmarksView : public QTreeView -{ - Q_OBJECT - -public: - - explicit BookmarksView(QWidget *parent = nullptr); - -protected: - void dropEvent(QDropEvent *event) override; - -private: -}; - -#endif // SMOLBOTE_BOOKMARKSVIEW_H diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index a7ea6d1..d9af4ee 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -54,8 +54,7 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) QFile bookmarksFile(path); if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - XbelReader xbel(&bookmarksFile); - xbel.read(model->root()); + Xbel::read(&bookmarksFile, model->root()); bookmarksFile.close(); } @@ -68,7 +67,9 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) emit openUrl(index.data(Qt::DisplayRole).toUrl()); else { auto *dlg = new EditBookmarkDialog(model, index, this); - dlg->exec(); + if(dlg->exec() == QDialog::Accepted) { + m_isChanged = true; + } } }); } @@ -89,15 +90,13 @@ 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)) { + Xbel::write(&bookmarksFile, model->root()); + bookmarksFile.flush(); + bookmarksFile.close(); + m_isChanged = false; + } } void BookmarksWidget::addBookmark(const QString &title, const QString &url) diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp index c23f0be..7fedce7 100644 --- a/lib/bookmarks/xbel.cpp +++ b/lib/bookmarks/xbel.cpp @@ -7,24 +7,22 @@ */ #include "xbel.h" -#include <QFile> -#include <QAbstractItemModel> #include "bookmarkitem.h" -#include "bookmarkmodel.h" +#include <QXmlStreamReader> +#include <QXmlStreamWriter> -XbelReader::XbelReader(QIODevice *file) -{ - Q_CHECK_PTR(file); - m_file = file; -} - - -void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) +inline void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) { while(reader.readNextStartElement()) { if(reader.name() == "title") { parent->setData(BookmarkItem::Title, reader.readElementText()); + } else if(reader.name() == "tags") { + parent->setData(BookmarkItem::Tags, reader.readElementText().split(";")); + + } else if(reader.name() == "description") { + parent->setData(BookmarkItem::Description, reader.readElementText()); + } else if(reader.name() == "folder") { auto *item = new BookmarkItem({}, BookmarkItem::Folder, parent); item->setExpanded(!(reader.attributes().value("folded") == QLatin1Literal("yes"))); @@ -43,9 +41,9 @@ void readChildElements(QXmlStreamReader &reader, BookmarkItem *parent) } } -void XbelReader::read(BookmarkItem *item) +void Xbel::read(QIODevice *device, BookmarkItem *item) { - QXmlStreamReader qXmlStreamReader(m_file); + QXmlStreamReader qXmlStreamReader(device); if(qXmlStreamReader.readNextStartElement()) { if(!(qXmlStreamReader.name() == "xbel" && qXmlStreamReader.attributes().value("version") == "1.0")) { @@ -56,34 +54,46 @@ void XbelReader::read(BookmarkItem *item) } } -XbelWriter::XbelWriter() +inline void writeChildElements(QXmlStreamWriter &writer, const BookmarkItem *item) { -} + switch(item->type()) { + case BookmarkItem::Root: + for(int i = 0; i < item->childCount(); ++i) { + writeChildElements(writer, item->child(i)); + } + break; + + case BookmarkItem::Folder: + writer.writeStartElement("folder"); + writer.writeAttribute("folded", !item->isExpanded() ? "yes" : "no"); + writer.writeTextElement("title", item->data(BookmarkItem::Title).toString()); + if(!item->data(BookmarkItem::Tags).isNull()) + writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); + if(!item->data(BookmarkItem::Description).isNull()) + writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); + + for(int i = 0; i < item->childCount(); ++i) { + writeChildElements(writer, item->child(i)); + } -inline void writeChildElement(QXmlStreamWriter &writer, const BookmarkItem *item) -{ - 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; + 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()); + if(!item->data(BookmarkItem::Tags).isNull()) + writer.writeTextElement("tags", item->data(BookmarkItem::Tags).toStringList().join(";")); + if(!item->data(BookmarkItem::Description).isNull()) + writer.writeTextElement("description", item->data(BookmarkItem::Description).toString()); + + writer.writeEndElement(); + break; } } -void XbelWriter::write(QIODevice *device, const BookmarkItem *item) + +void Xbel::write(QIODevice *device, const BookmarkItem *item) { QXmlStreamWriter xmlWriter(device); xmlWriter.setAutoFormatting(true); @@ -94,7 +104,7 @@ void XbelWriter::write(QIODevice *device, const BookmarkItem *item) xmlWriter.writeStartElement("xbel"); xmlWriter.writeAttribute("version", "1.0"); - writeChildElement(xmlWriter, item); + writeChildElements(xmlWriter, item); xmlWriter.writeEndDocument(); } diff --git a/lib/bookmarks/xbel.h b/lib/bookmarks/xbel.h index e38ae98..9be7dd4 100644 --- a/lib/bookmarks/xbel.h +++ b/lib/bookmarks/xbel.h @@ -6,32 +6,15 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef XBELREADER_H -#define XBELREADER_H +#ifndef XBEL_H +#define XBEL_H -#include "bookmarksview.h" -#include <QString> -#include <QXmlStreamReader> -#include "bookmarkmodel.h" - -class BookmarkModel; -class XbelReader -{ -public: - explicit XbelReader(QIODevice *file); - void read(BookmarkItem *model); - -private: - QIODevice *m_file; -}; - -class XbelWriter +class QIODevice; +class BookmarkItem; +namespace Xbel { -public: - explicit XbelWriter(); - void write(QIODevice *device, const BookmarkItem *item); - -private: +void read(QIODevice *device, BookmarkItem *model); +void write(QIODevice *device, const BookmarkItem *item); }; -#endif // XBELREADER_H +#endif // XBEL_H |