1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[Bookmarks] Added BookmarksModel

This commit is contained in:
nowrep 2014-02-07 22:48:30 +01:00
parent c9a556658a
commit 9271b70802
8 changed files with 498 additions and 19 deletions

View File

@ -17,7 +17,7 @@
* ============================================================ */
#include "bookmarkitem.h"
BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem *parent)
BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem* parent)
: m_type(type)
, m_parent(parent)
, m_expanded(false)
@ -42,12 +42,12 @@ void BookmarkItem::setType(BookmarkItem::Type type)
m_type = type;
}
BookmarkItem *BookmarkItem::parent() const
BookmarkItem* BookmarkItem::parent() const
{
return m_parent;
}
QList<BookmarkItem *> BookmarkItem::children() const
QList<BookmarkItem*> BookmarkItem::children() const
{
return m_children;
}
@ -102,7 +102,7 @@ void BookmarkItem::setExpanded(bool expanded)
m_expanded = expanded;
}
void BookmarkItem::addChild(BookmarkItem *child, int index)
void BookmarkItem::addChild(BookmarkItem* child, int index)
{
if (child->m_parent) {
child->m_parent->removeChild(child);
@ -110,7 +110,7 @@ void BookmarkItem::addChild(BookmarkItem *child, int index)
child->m_parent = this;
if (index == -1) {
if (index < 0) {
m_children.append(child);
}
else {
@ -118,7 +118,7 @@ void BookmarkItem::addChild(BookmarkItem *child, int index)
}
}
void BookmarkItem::removeChild(BookmarkItem *child)
void BookmarkItem::removeChild(BookmarkItem* child)
{
child->m_parent = 0;
m_children.removeOne(child);

View File

@ -41,7 +41,7 @@ public:
Type type() const;
void setType(Type type);
BookmarkItem *parent() const;
BookmarkItem* parent() const;
QList<BookmarkItem*> children() const;
QUrl url() const;
@ -59,8 +59,8 @@ public:
bool isExpanded() const;
void setExpanded(bool expanded);
void addChild(BookmarkItem *child, int index = -1);
void removeChild(BookmarkItem *child);
void addChild(BookmarkItem* child, int index = -1);
void removeChild(BookmarkItem* child);
static Type typeFromString(const QString &string);
static QString typeToString(Type type);

View File

@ -17,6 +17,7 @@
* ============================================================ */
#include "bookmarks.h"
#include "bookmarkitem.h"
#include "bookmarksmodel.h"
#include "tabbedwebview.h"
#include "iconprovider.h"
#include "mainapplication.h"
@ -46,11 +47,11 @@ void Bookmarks::loadBookmarks()
m_root = new BookmarkItem(BookmarkItem::Root);
m_folderToolbar = new BookmarkItem(BookmarkItem::Folder, m_root);
m_folderToolbar->setTitle(tr("Bookmarks ToolBar"));
m_folderToolbar->setTitle(tr("Bookmarks Toolbar"));
m_folderToolbar->setDescription(tr("Bookmarks located in Bookmarks Toolbar"));
m_folderMenu = new BookmarkItem(BookmarkItem::Folder, m_root);
m_folderMenu->setTitle(tr("Bookmarks In Menu"));
m_folderMenu->setTitle(tr("Bookmarks Menu"));
m_folderMenu->setDescription(tr("Bookmarks located in Bookmarks Menu"));
m_folderUnsorted = new BookmarkItem(BookmarkItem::Folder, m_root);
@ -78,6 +79,8 @@ void Bookmarks::loadBookmarks()
readBookmarks(bookmarksMap.value("bookmark_bar").toMap().value("children").toList(), m_folderToolbar);
readBookmarks(bookmarksMap.value("bookmark_menu").toMap().value("children").toList(), m_folderMenu);
readBookmarks(bookmarksMap.value("other").toMap().value("children").toList(), m_folderUnsorted);
m_model = new BookmarksModel(this, this);
}
void Bookmarks::saveBookmarks()
@ -118,7 +121,7 @@ void Bookmarks::saveBookmarks()
bFile.close();
}
void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem *parent)
void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent)
{
if (!parent) {
return;
@ -145,7 +148,7 @@ void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem *parent)
}
}
QVariantList Bookmarks::writeBookmarks(BookmarkItem *parent)
QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
{
QVariantList list;
@ -172,7 +175,7 @@ QVariantList Bookmarks::writeBookmarks(BookmarkItem *parent)
return list;
}
void Bookmarks::writeChildren(BookmarkItem *parent)
void Bookmarks::writeChildren(BookmarkItem* parent)
{
if (!parent) {
return;
@ -745,6 +748,47 @@ QString Bookmarks::fromTranslatedFolder(const QString &name)
return folder;
}
BookmarkItem* Bookmarks::rootItem() const
{
return m_root;
}
bool Bookmarks::removeBookmark(BookmarkItem* item)
{
if (!canBeModified(item)) {
return false;
}
m_model->removeBookmark(item);
emit bookmarkRemoved(item);
return true;
}
bool Bookmarks::canBeModified(BookmarkItem* item) const
{
return item != m_root && item != m_folderToolbar && item != m_folderMenu && item != m_folderUnsorted;
}
void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->type() == BookmarkItem::Folder);
Q_ASSERT(item);
insertBookmark(parent, 0, item);
}
void Bookmarks::insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(parent->type() == BookmarkItem::Folder);
Q_ASSERT(item);
m_model->addBookmark(parent, row, item);
emit bookmarkAdded(item);
}
void Bookmarks::changeBookmarkParent(int id, const QString &newParent, const QString &oldParent, bool* ok)
{
QSqlQuery query;

View File

@ -33,6 +33,7 @@ class QIcon;
class WebView;
class BookmarkItem;
class BookmarksModel;
class QT_QUPZILLA_EXPORT Bookmarks : public QObject
{
@ -98,6 +99,13 @@ public:
static QString toTranslatedFolder(const QString &name);
static QString fromTranslatedFolder(const QString &name);
BookmarkItem* rootItem() const;
bool canBeModified(BookmarkItem* item) const;
void addBookmark(BookmarkItem* parent, BookmarkItem* item);
void insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item);
bool removeBookmark(BookmarkItem* item);
signals:
void bookmarkAdded(const Bookmarks::Bookmark &bookmark);
void bookmarkDeleted(const Bookmarks::Bookmark &bookmark);
@ -114,6 +122,13 @@ signals:
void folderParentChanged(const QString &name, bool isSubfolder);
// Item was added to bookmarks
void bookmarkAdded(BookmarkItem* item);
// Item was removed from bookmarks
void bookmarkRemoved(BookmarkItem* item);
// Item data has changed
void bookmarkChanged(BookmarkItem* item);
public slots:
void bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant,
const QString &folder = QLatin1String("unsorted"), bool* ok = 0);
@ -136,6 +151,8 @@ private:
BookmarkItem* m_folderToolbar;
BookmarkItem* m_folderMenu;
BookmarkItem* m_folderUnsorted;
BookmarksModel* m_model;
};
typedef Bookmarks::Bookmark Bookmark;

View File

@ -0,0 +1,337 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarksmodel.h"
#include "bookmarkitem.h"
#include "bookmarks.h"
#include "iconprovider.h"
#include <QApplication>
#include <QMimeData>
#include <QStyle>
//#define BOOKMARKSMODEL_DEBUG
#ifdef BOOKMARKSMODEL_DEBUG
#include <QTreeView>
#include "modeltest.h"
#endif
BookmarksModel::BookmarksModel(Bookmarks* bookmarks, QObject* parent)
: QAbstractItemModel(parent)
, m_bookmarks(bookmarks)
{
connect(m_bookmarks, SIGNAL(bookmarkChanged(BookmarkItem*)), this, SLOT(bookmarkChanged(BookmarkItem*)));
#ifdef BOOKMARKSMODEL_DEBUG
new ModelTest(this, this);
QTreeView* view = new QTreeView;
view->setModel(this);
view->setSelectionMode(QAbstractItemView::ExtendedSelection);
view->setDragEnabled(true);
view->setAcceptDrops(true);
view->setDropIndicatorShown(true);
view->expandAll();
view->show();
#endif
}
void BookmarksModel::addBookmark(BookmarkItem* parent, int row, BookmarkItem* item)
{
Q_ASSERT(parent);
Q_ASSERT(item);
Q_ASSERT(row >= 0);
Q_ASSERT(row <= parent->children().count());
beginInsertRows(index(parent), row, row);
parent->addChild(item, row);
endInsertRows();
}
void BookmarksModel::removeBookmark(BookmarkItem* item)
{
Q_ASSERT(item);
Q_ASSERT(item->parent());
int idx = item->parent()->children().indexOf(item);
beginRemoveRows(index(item->parent()), idx, idx);
item->parent()->removeChild(item);
endRemoveRows();
}
Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const
{
BookmarkItem* itm = item(index);
if (!index.isValid() || !itm) {
return Qt::NoItemFlags;
}
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (itm->type() == BookmarkItem::Folder) {
flags |= Qt::ItemIsDropEnabled;
}
if (m_bookmarks->canBeModified(itm)) {
flags |= Qt::ItemIsDragEnabled;
}
return flags;
}
QVariant BookmarksModel::data(const QModelIndex &index, int role) const
{
BookmarkItem* itm = item(index);
if (!itm) {
return QVariant();
}
switch (role) {
case TypeRole:
return itm->type();
case UrlRole:
return itm->url();
case TitleRole:
return itm->title();
case DescriptionRole:
return itm->description();
case KeywordRole:
return itm->keyword();
case ExpandedRole:
return itm->isExpanded();
case Qt::DisplayRole:
case Qt::ToolTipRole:
switch (index.column()) {
case 0:
return itm->title();
case 1:
return itm->url();
default:
return QVariant();
}
case Qt::DecorationRole:
if (index.column() != 0) {
return QVariant();
}
switch (itm->type()) {
case BookmarkItem::Folder:
return QApplication::style()->standardIcon(QStyle::SP_DirIcon);
case BookmarkItem::Url:
return _iconForUrl(itm->url());
default:
return QVariant();
}
default:
return QVariant();
}
}
QVariant BookmarksModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0:
return tr("Title");
case 1:
return tr("Address");
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
int BookmarksModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0) {
return 0;
}
BookmarkItem* itm = item(parent);
return itm->children().count();
}
int BookmarksModel::columnCount(const QModelIndex &parent) const
{
if (parent.column() > 0) {
return 0;
}
return 2;
}
bool BookmarksModel::hasChildren(const QModelIndex &parent) const
{
BookmarkItem* itm = item(parent);
return !itm->children().isEmpty();
}
#if 0
bool BookmarksModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (!hasIndex(row, 0, parent) || !hasIndex(row + count, 0, parent)) {
return false;
}
BookmarkItem* itm = item(parent);
int offset = 0;
for (int i = 0; i < count; ++i) {
int idx = i + row + offset;
BookmarkItem* child = itm->children().at(idx);
if (!m_bookmarks->removeBookmark(child)) {
offset++;
continue;
}
}
return true;
}
#endif
Qt::DropActions BookmarksModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
#define MIMETYPE QLatin1String("application/qupzilla.bookmarks")
QStringList BookmarksModel::mimeTypes() const
{
QStringList types;
types.append(MIMETYPE);
return types;
}
QMimeData* BookmarksModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData* mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
// If item's parent (=folder) is also selected, we will just move the whole folder
if (index.isValid() && index.column() == 0 && !indexes.contains(index.parent())) {
stream << index.row() << (quint32) index.internalId();
}
}
mimeData->setData(MIMETYPE, encodedData);
return mimeData;
}
bool BookmarksModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
Q_UNUSED(column)
if (action == Qt::IgnoreAction) {
return true;
}
if (!data->hasFormat(MIMETYPE) || !parent.isValid()) {
return false;
}
BookmarkItem* parentItm = item(parent);
Q_ASSERT(parentItm->type() == BookmarkItem::Folder);
QByteArray encodedData = data->data(MIMETYPE);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QList<BookmarkItem*> items;
while (!stream.atEnd()) {
int row;
quint32 id;
stream >> row >> id;
QModelIndex index = createIndex(row, 0, id);
BookmarkItem* itm = item(index);
Q_ASSERT(index.isValid());
Q_ASSERT(itm != m_bookmarks->rootItem());
// Cannot move bookmark to itself
if (itm == parentItm) {
return false;
}
items.append(itm);
}
row = qMax(row, 0);
foreach (BookmarkItem* itm, items) {
// If we are moving an item through the folder and item is above the row to insert,
// we must decrease row by one (by the dropped folder)
if (itm->parent() == parentItm && itm->parent()->children().indexOf(itm) < row) {
row--;
}
m_bookmarks->removeBookmark(itm);
m_bookmarks->insertBookmark(parentItm, row++, itm);
}
return true;
}
QModelIndex BookmarksModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
return QModelIndex();
}
BookmarkItem* itm = item(child);
return index(itm->parent());
}
QModelIndex BookmarksModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
BookmarkItem* parentItem = item(parent);
return createIndex(row, column, parentItem->children().at(row));
}
QModelIndex BookmarksModel::index(BookmarkItem* item) const
{
BookmarkItem* parent = item->parent();
if (!parent) {
return QModelIndex();
}
return createIndex(parent->children().indexOf(item), 0, item);
}
void BookmarksModel::bookmarkChanged(BookmarkItem* item)
{
QModelIndex idx = index(item);
emit dataChanged(idx, idx);
}
BookmarkItem* BookmarksModel::item(const QModelIndex &index) const
{
BookmarkItem* itm = static_cast<BookmarkItem*>(index.internalPointer());
return itm ? itm : m_bookmarks->rootItem();
}

View File

@ -0,0 +1,78 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef BOOKMARKSMODEL_H
#define BOOKMARKSMODEL_H
#include <QAbstractItemModel>
#include "qz_namespace.h"
class Bookmarks;
class BookmarkItem;
class QT_QUPZILLA_EXPORT BookmarksModel : public QAbstractItemModel
{
Q_OBJECT
public:
enum Roles {
TypeRole = Qt::UserRole + 1,
UrlRole = Qt::UserRole + 2,
TitleRole = Qt::UserRole + 3,
DescriptionRole = Qt::UserRole + 4,
KeywordRole = Qt::UserRole + 5,
ExpandedRole = Qt::UserRole + 6,
MaxRole = ExpandedRole
};
explicit BookmarksModel(Bookmarks* bookmarks, QObject* parent = 0);
void addBookmark(BookmarkItem* parent, int row, BookmarkItem* item);
void removeBookmark(BookmarkItem* item);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
bool hasChildren(const QModelIndex &parent) const;
Qt::DropActions supportedDropActions() const;
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList &indexes) const;
bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
QModelIndex parent(const QModelIndex &child) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(BookmarkItem* item) const;
#if 0
bool removeRows(int row, int count, const QModelIndex &parent);
#endif
private slots:
void bookmarkChanged(BookmarkItem* item);
private:
BookmarkItem* item(const QModelIndex &index) const;
Bookmarks* m_bookmarks;
};
#endif // BOOKMARKSMODEL_H

View File

@ -12,7 +12,8 @@ DEFINES *= QUPZILLA_SHAREDLIBRARY
include(3rdparty/qtsingleapplication.pri)
include(../defines.pri)
include(../../translations/translations.pri)
#include(../../tests/modeltest/modeltest.pri)
CONFIG(debug, debug|release): include(../../tests/modeltest/modeltest.pri)
!mac:contains(DEFINES, USE_QTWEBKIT_2_2) {
include(plugins/qtwebkit/qtwebkit-plugins.pri)
@ -253,7 +254,8 @@ SOURCES += \
webview/javascript/externaljsobject.cpp \
bookmarks/bookmarks.cpp \
bookmarks/bookmarkitem.cpp \
tools/json.cpp
tools/json.cpp \
bookmarks/bookmarksmodel.cpp
HEADERS += \
@ -443,7 +445,8 @@ HEADERS += \
webview/javascript/externaljsobject.h \
bookmarks/bookmarks.h \
bookmarks/bookmarkitem.h \
tools/json.h
tools/json.h \
bookmarks/bookmarksmodel.h
FORMS += \
preferences/autofillmanager.ui \

View File

@ -24,7 +24,7 @@
#include <QJsonDocument>
#endif
QVariant Json::parse(const QByteArray &data, bool *ok)
QVariant Json::parse(const QByteArray &data, bool* ok)
{
#if QT_VERSION < 0x050000
QJson::Parser parser;
@ -41,7 +41,7 @@ QVariant Json::parse(const QByteArray &data, bool *ok)
#endif
}
QByteArray Json::serialize(const QVariant &variant, bool *ok)
QByteArray Json::serialize(const QVariant &variant, bool* ok)
{
#if QT_VERSION < 0x050000
QJson::Serializer serializer;