From 8d48c62aaf14e023b49c4701c26552732e7c609b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Ander=20Pe=C3=B1alba?= Date: Fri, 20 Aug 2010 12:03:14 +0200 Subject: Bookmarks panel code reordered --- src/bookmarks/bookmarkspanel.cpp | 150 +++++++++++++++++------------------ src/bookmarks/bookmarkstreemodel.cpp | 112 +++++++++++++------------- src/bookmarks/bookmarkstreemodel.h | 1 + 3 files changed, 132 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/src/bookmarks/bookmarkspanel.cpp b/src/bookmarks/bookmarkspanel.cpp index 557fabc2..22701baa 100644 --- a/src/bookmarks/bookmarkspanel.cpp +++ b/src/bookmarks/bookmarkspanel.cpp @@ -76,6 +76,81 @@ void BookmarksPanel::showing(bool b) } +void BookmarksPanel::startLoadFoldedState() +{ + m_loadingState = true; + loadFoldedState(QModelIndex()); + m_loadingState = false; +} + + +void BookmarksPanel::contextMenu(const QPoint &pos) +{ + if (m_loadingState) + return; + + BookmarksContextMenu menu(bookmarkForIndex( m_treeView->indexAt(pos) ), + Application::bookmarkProvider()->bookmarkManager(), + Application::bookmarkProvider()->bookmarkOwner(), + this + ); + + menu.exec(m_treeView->mapToGlobal(pos)); +} + + +void BookmarksPanel::deleteBookmark() +{ + QModelIndex index = m_treeView->currentIndex(); + if (m_loadingState || !index.isValid()) + return; + + Application::bookmarkProvider()->bookmarkOwner()->bookmarkSelected(bookmarkForIndex(index)); + Application::bookmarkProvider()->bookmarkOwner()->deleteBookmark(); +} + + +void BookmarksPanel::onCollapse(const QModelIndex &index) +{ + if (m_loadingState) + return; + + bookmarkForIndex(index).internalElement().setAttribute("folded", "yes"); + emit expansionChanged(); +} + + +void BookmarksPanel::onExpand(const QModelIndex &index) +{ + if (m_loadingState) + return; + + bookmarkForIndex(index).internalElement().setAttribute("folded", "no"); + emit expansionChanged(); +} + + +void BookmarksPanel::loadFoldedState(const QModelIndex &root) +{ + int count = m_treeView->model()->rowCount(root); + QModelIndex index; + + for (int i = 0; i < count; ++i) + { + index = m_treeView->model()->index(i, 0, root); + if (index.isValid()) + { + KBookmark bm = bookmarkForIndex(index); + if (bm.isGroup()) + { + m_treeView->setExpanded(index, bm.toGroup().isOpen()); + loadFoldedState(index); + } + } + } +} + + void BookmarksPanel::setup() { kDebug() << "Loading bookmarks panel setup..."; @@ -143,78 +218,3 @@ KBookmark BookmarksPanel::bookmarkForIndex(const QModelIndex &index) BtmItem *node = static_cast(originalIndex.internalPointer()); return node->getBkm(); } - - -void BookmarksPanel::onCollapse(const QModelIndex &index) -{ - if (m_loadingState) - return; - - bookmarkForIndex(index).internalElement().setAttribute("folded", "yes"); - emit expansionChanged(); -} - - -void BookmarksPanel::onExpand(const QModelIndex &index) -{ - if (m_loadingState) - return; - - bookmarkForIndex(index).internalElement().setAttribute("folded", "no"); - emit expansionChanged(); -} - - -void BookmarksPanel::startLoadFoldedState() -{ - m_loadingState = true; - loadFoldedState(QModelIndex()); - m_loadingState = false; -} - - -void BookmarksPanel::loadFoldedState(const QModelIndex &root) -{ - int count = m_treeView->model()->rowCount(root); - QModelIndex index; - - for (int i = 0; i < count; ++i) - { - index = m_treeView->model()->index(i, 0, root); - if (index.isValid()) - { - KBookmark bm = bookmarkForIndex(index); - if (bm.isGroup()) - { - m_treeView->setExpanded(index, bm.toGroup().isOpen()); - loadFoldedState(index); - } - } - } -} - - -void BookmarksPanel::contextMenu(const QPoint &pos) -{ - if (m_loadingState) - return; - - BookmarksContextMenu menu(bookmarkForIndex( m_treeView->indexAt(pos) ), - Application::bookmarkProvider()->bookmarkManager(), - Application::bookmarkProvider()->bookmarkOwner(), - this - ); - - menu.exec(m_treeView->mapToGlobal(pos)); -} - - -void BookmarksPanel::deleteBookmark() -{ - QModelIndex index = m_treeView->currentIndex(); - if (m_loadingState || !index.isValid()) - return; - - Application::bookmarkProvider()->bookmarkOwner()->bookmarkSelected(bookmarkForIndex(index)); - Application::bookmarkProvider()->bookmarkOwner()->deleteBookmark(); -} diff --git a/src/bookmarks/bookmarkstreemodel.cpp b/src/bookmarks/bookmarkstreemodel.cpp index 5bf1f769..8e95a615 100644 --- a/src/bookmarks/bookmarkstreemodel.cpp +++ b/src/bookmarks/bookmarkstreemodel.cpp @@ -250,6 +250,62 @@ QVariant BookmarksTreeModel::data(const QModelIndex &index, int role) const } +QStringList BookmarksTreeModel::mimeTypes() const +{ + return KBookmark::List::mimeDataTypes(); +} + + +bool BookmarksTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) +{ + if (action != Qt::MoveAction || !data->hasFormat("application/rekonq-bookmark")) + return false; + + QByteArray addresses = data->data("application/rekonq-bookmark"); + KBookmark bookmark = Application::bookmarkProvider()->bookmarkManager()->findByAddress(QString::fromLatin1(addresses.data())); + + KBookmarkGroup root; + if (parent.isValid()) + root = bookmarkForIndex(parent).toGroup(); + else + root = Application::bookmarkProvider()->rootGroup(); + + QModelIndex destIndex = index(row, column, parent); + + if (destIndex.isValid() && row != -1) + { + root.moveBookmark(bookmark, root.previous(bookmarkForIndex(destIndex))); + } + else + { + root.deleteBookmark(bookmark); + root.addBookmark(bookmark); + } + + Application::bookmarkProvider()->bookmarkManager()->emitChanged(); + + return true; +} + + +Qt::DropActions BookmarksTreeModel::supportedDropActions() const +{ + return Qt::MoveAction; +} + + +QMimeData* BookmarksTreeModel::mimeData(const QModelIndexList &indexes) const +{ + QMimeData *mimeData = new QMimeData; + + QByteArray address = bookmarkForIndex(indexes.first()).address().toLatin1(); + mimeData->setData("application/rekonq-bookmark", address); + bookmarkForIndex(indexes.first()).populateMimeData(mimeData); + + return mimeData; +} + + void BookmarksTreeModel::bookmarksChanged(const QString &groupAddress) { if (groupAddress.isEmpty()) @@ -325,59 +381,3 @@ KBookmark BookmarksTreeModel::bookmarkForIndex(const QModelIndex &index) const { return static_cast(index.internalPointer())->getBkm(); } - - -Qt::DropActions BookmarksTreeModel::supportedDropActions() const -{ - return Qt::MoveAction; -} - - -QStringList BookmarksTreeModel::mimeTypes() const -{ - return KBookmark::List::mimeDataTypes(); -} - - -QMimeData* BookmarksTreeModel::mimeData(const QModelIndexList &indexes) const -{ - QMimeData *mimeData = new QMimeData; - - QByteArray address = bookmarkForIndex(indexes.first()).address().toLatin1(); - mimeData->setData("application/rekonq-bookmark", address); - bookmarkForIndex(indexes.first()).populateMimeData(mimeData); - - return mimeData; -} - - -bool BookmarksTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) -{ - if (action != Qt::MoveAction || !data->hasFormat("application/rekonq-bookmark")) - return false; - - QByteArray addresses = data->data("application/rekonq-bookmark"); - KBookmark bookmark = Application::bookmarkProvider()->bookmarkManager()->findByAddress(QString::fromLatin1(addresses.data())); - - KBookmarkGroup root; - if (parent.isValid()) - root = bookmarkForIndex(parent).toGroup(); - else - root = Application::bookmarkProvider()->rootGroup(); - - QModelIndex destIndex = index(row, column, parent); - - if (destIndex.isValid() && row != -1) - { - root.moveBookmark(bookmark, root.previous(bookmarkForIndex(destIndex))); - } - else - { - root.deleteBookmark(bookmark); - root.addBookmark(bookmark); - } - - Application::bookmarkProvider()->bookmarkManager()->emitChanged(); - - return true; -} diff --git a/src/bookmarks/bookmarkstreemodel.h b/src/bookmarks/bookmarkstreemodel.h index 0f115272..6b85365b 100644 --- a/src/bookmarks/bookmarkstreemodel.h +++ b/src/bookmarks/bookmarkstreemodel.h @@ -43,6 +43,7 @@ class BtmItem public: BtmItem(const KBookmark &bm); ~BtmItem(); + QVariant data(int role = Qt::DisplayRole) const; int row() const; int childCount() const; -- cgit v1.2.1