From 0b8d83230e6b461d0e32f467e360b31d6d75e296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Ander=20Pe=C3=B1alba?= Date: Thu, 19 Aug 2010 23:47:48 +0200 Subject: Bookmarks panel code clean-up --- src/bookmarks/bookmarkspanel.cpp | 68 ++++++++++---------- src/bookmarks/bookmarkspanel.h | 19 ++---- src/bookmarks/bookmarkstreemodel.cpp | 117 ++++++++++++----------------------- src/bookmarks/bookmarkstreemodel.h | 21 +++++-- 4 files changed, 91 insertions(+), 134 deletions(-) (limited to 'src') diff --git a/src/bookmarks/bookmarkspanel.cpp b/src/bookmarks/bookmarkspanel.cpp index 231df638..557fabc2 100644 --- a/src/bookmarks/bookmarkspanel.cpp +++ b/src/bookmarks/bookmarkspanel.cpp @@ -27,54 +27,51 @@ // Self Includes #include "bookmarkspanel.h" -#include "bookmarkspanel.moc" // Local Includes +#include "application.h" #include "bookmarksmanager.h" #include "bookmarkstreemodel.h" #include "bookmarksproxy.h" #include "bookmarkscontextmenu.h" #include "bookmarkowner.h" +#include "paneltreeview.h" // Auto Includes #include "rekonq.h" // Qt includes -#include -#include -#include +#include +#include +#include // KDE includes #include -#include -#include -#include - BookmarksPanel::BookmarksPanel(const QString &title, QWidget *parent, Qt::WindowFlags flags) : QDockWidget(title, parent, flags) , m_treeView(new PanelTreeView(this)) , m_loadingState(false) - , _loaded(false) + , m_loaded(false) { setObjectName("bookmarksPanel"); setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); connect(this, SIGNAL(visibilityChanged(bool)), this, SLOT(showing(bool))); - setShown(ReKonfig::showBookmarksPanel()); + setVisible(ReKonfig::showBookmarksPanel()); } BookmarksPanel::~BookmarksPanel() { - ReKonfig::setShowBookmarksPanel(!isHidden()); + ReKonfig::setShowBookmarksPanel(false); } void BookmarksPanel::showing(bool b) { - if(b && !_loaded) + if(b && !m_loaded) setup(); } @@ -97,8 +94,6 @@ void BookmarksPanel::setup() // setup tree view m_treeView->setUniformRowHeights(true); - m_treeView->setTextElideMode(Qt::ElideMiddle); - m_treeView->setAlternatingRowColors(true); m_treeView->header()->hide(); m_treeView->setDragEnabled(true); m_treeView->setAutoExpandDelay(750); @@ -120,16 +115,20 @@ void BookmarksPanel::setup() proxy->setSourceModel(model); m_treeView->setModel(proxy); + connect(search, SIGNAL(textChanged(const QString &)), proxy, SLOT(setFilterFixedString(const QString &))); + + connect(model, SIGNAL(bookmarksUpdated()), this, SLOT(startLoadFoldedState())); + connect(m_treeView, SIGNAL(contextMenuItemRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &))); connect(m_treeView, SIGNAL(contextMenuGroupRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &))); connect(m_treeView, SIGNAL(contextMenuEmptyRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &))); connect(m_treeView, SIGNAL(delKeyPressed()), this, SLOT(deleteBookmark())); connect(m_treeView, SIGNAL(collapsed(const QModelIndex &)), this, SLOT(onCollapse(const QModelIndex &))); connect(m_treeView, SIGNAL(expanded(const QModelIndex &)), this, SLOT(onExpand(const QModelIndex &))); - connect(search, SIGNAL(textChanged(const QString &)), proxy, SLOT(setFilterFixedString(const QString &))); + startLoadFoldedState(); - _loaded = true; + m_loaded = true; } @@ -138,10 +137,10 @@ KBookmark BookmarksPanel::bookmarkForIndex(const QModelIndex &index) if (!index.isValid()) return KBookmark(); - const QAbstractProxyModel* proxyModel = dynamic_cast< const QAbstractProxyModel* >(index.model()); + const BookmarksProxy *proxyModel = static_cast(index.model()); QModelIndex originalIndex = proxyModel->mapToSource(index); - BtmItem *node = static_cast< BtmItem* >(originalIndex.internalPointer()); + BtmItem *node = static_cast(originalIndex.internalPointer()); return node->getBkm(); } @@ -151,8 +150,7 @@ void BookmarksPanel::onCollapse(const QModelIndex &index) if (m_loadingState) return; - KBookmark bookmark = bookmarkForIndex(index); - bookmark.internalElement().setAttribute("folded", "yes"); + bookmarkForIndex(index).internalElement().setAttribute("folded", "yes"); emit expansionChanged(); } @@ -162,8 +160,7 @@ void BookmarksPanel::onExpand(const QModelIndex &index) if (m_loadingState) return; - KBookmark bookmark = bookmarkForIndex(index); - bookmark.internalElement().setAttribute("folded", "no"); + bookmarkForIndex(index).internalElement().setAttribute("folded", "no"); emit expansionChanged(); } @@ -178,17 +175,20 @@ void BookmarksPanel::startLoadFoldedState() void BookmarksPanel::loadFoldedState(const QModelIndex &root) { - int count = m_treeView->model()->rowCount(root); QModelIndex index; - for (int i = 0; i < count; i++) + for (int i = 0; i < count; ++i) { index = m_treeView->model()->index(i, 0, root); - if (index.isValid() && bookmarkForIndex(index).isGroup()) + if (index.isValid()) { - m_treeView->setExpanded(index, bookmarkForIndex(index).toGroup().isOpen()); - loadFoldedState(index); + KBookmark bm = bookmarkForIndex(index); + if (bm.isGroup()) + { + m_treeView->setExpanded(index, bm.toGroup().isOpen()); + loadFoldedState(index); + } } } } @@ -196,17 +196,14 @@ void BookmarksPanel::loadFoldedState(const QModelIndex &root) void BookmarksPanel::contextMenu(const QPoint &pos) { - QModelIndex index = m_treeView->indexAt(pos); if (m_loadingState) return; - KBookmark selected = bookmarkForIndex(index); - - BookmarksContextMenu menu( selected, + BookmarksContextMenu menu(bookmarkForIndex( m_treeView->indexAt(pos) ), Application::bookmarkProvider()->bookmarkManager(), Application::bookmarkProvider()->bookmarkOwner(), this - ); + ); menu.exec(m_treeView->mapToGlobal(pos)); } @@ -215,10 +212,9 @@ void BookmarksPanel::contextMenu(const QPoint &pos) void BookmarksPanel::deleteBookmark() { QModelIndex index = m_treeView->currentIndex(); - if (!index.isValid()) + if (m_loadingState || !index.isValid()) return; - KBookmark bm = bookmarkForIndex(index); - Application::instance()->bookmarkProvider()->bookmarkOwner()->bookmarkSelected(bm); - Application::instance()->bookmarkProvider()->bookmarkOwner()->deleteBookmark(); + Application::bookmarkProvider()->bookmarkOwner()->bookmarkSelected(bookmarkForIndex(index)); + Application::bookmarkProvider()->bookmarkOwner()->deleteBookmark(); } diff --git a/src/bookmarks/bookmarkspanel.h b/src/bookmarks/bookmarkspanel.h index d94f2b99..4afa953b 100644 --- a/src/bookmarks/bookmarkspanel.h +++ b/src/bookmarks/bookmarkspanel.h @@ -33,28 +33,21 @@ // Rekonq Includes #include "rekonq_defines.h" -// Local Includes -#include "application.h" -#include "paneltreeview.h" - // Qt Includes #include -// KDE Includes -#include - // Forward Declarations -class KUrl; +class PanelTreeView; +class KBookmark; class QModelIndex; - class REKONQ_TESTS_EXPORT BookmarksPanel : public QDockWidget { Q_OBJECT public: explicit BookmarksPanel(const QString &title, QWidget *parent = 0, Qt::WindowFlags flags = 0); - ~BookmarksPanel(); + virtual ~BookmarksPanel(); signals: void openUrl(const KUrl &, const Rekonq::OpenType &); @@ -64,7 +57,7 @@ signals: public slots: void showing(bool); void startLoadFoldedState(); - + private slots: void contextMenu(const QPoint &pos); @@ -78,9 +71,7 @@ private: KBookmark bookmarkForIndex(const QModelIndex &index); PanelTreeView *m_treeView; - bool m_loadingState; - - bool _loaded; + bool m_loadingState, m_loaded; }; #endif // BOOKMARKSPANEL_H diff --git a/src/bookmarks/bookmarkstreemodel.cpp b/src/bookmarks/bookmarkstreemodel.cpp index 299efaf0..5bf1f769 100644 --- a/src/bookmarks/bookmarkstreemodel.cpp +++ b/src/bookmarks/bookmarkstreemodel.cpp @@ -27,18 +27,13 @@ // Self Includes #include "bookmarkstreemodel.h" -#include "bookmarkstreemodel.moc" // Local Includes #include "application.h" #include "bookmarksmanager.h" // Qt Includes -#include - -// KDE includes -#include -#include +#include BtmItem::BtmItem(const KBookmark &bm) @@ -69,9 +64,9 @@ QVariant BtmItem::data(int role) const { QString tooltip = ""; - if (!m_kbm.fullText().isEmpty()) + if (!m_kbm.text().isEmpty()) { - tooltip += m_kbm.fullText(); + tooltip += m_kbm.text(); } if (m_kbm.isGroup()) { @@ -140,6 +135,7 @@ KBookmark BtmItem::getBkm() const return m_kbm; } + // ------------------------------------------------------------------------------------- @@ -149,7 +145,6 @@ BookmarksTreeModel::BookmarksTreeModel(QObject *parent) { resetModel(); connect(Application::bookmarkProvider()->bookmarkManager(), SIGNAL(changed(const QString &, const QString &)), this, SLOT(bookmarksChanged(const QString &))); - connect(this, SIGNAL(bookmarksUpdated()), parent, SLOT(startLoadFoldedState())); } @@ -168,33 +163,19 @@ int BookmarksTreeModel::rowCount(const QModelIndex &parent) const } else { - parentItem = static_cast< BtmItem* >(parent.internalPointer()); + parentItem = static_cast(parent.internalPointer()); } return parentItem->childCount(); } -int BookmarksTreeModel::columnCount(const QModelIndex &parent) const +int BookmarksTreeModel::columnCount(const QModelIndex& /*parent*/) const { - Q_UNUSED(parent) - // name return 1; } -QVariant BookmarksTreeModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal - && role == Qt::DisplayRole - && section == 0 - ) - return i18n("Bookmark"); - - return QVariant(); -} - - Qt::ItemFlags BookmarksTreeModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractItemModel::flags(index); @@ -214,26 +195,18 @@ Qt::ItemFlags BookmarksTreeModel::flags(const QModelIndex &index) const QModelIndex BookmarksTreeModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) - { return QModelIndex(); - } BtmItem *parentItem; if (!parent.isValid()) - { parentItem = m_root; - } else - { - parentItem = static_cast< BtmItem* >(parent.internalPointer()); - } + parentItem = static_cast(parent.internalPointer()); BtmItem *childItem = parentItem->child(row); if (childItem) - { return createIndex(row, column, childItem); - } return QModelIndex(); } @@ -242,17 +215,13 @@ QModelIndex BookmarksTreeModel::index(int row, int column, const QModelIndex &pa QModelIndex BookmarksTreeModel::parent(const QModelIndex &index) const { if (!index.isValid()) - { return QModelIndex(); - } - BtmItem *childItem = static_cast< BtmItem* >(index.internalPointer()); + BtmItem *childItem = static_cast(index.internalPointer()); BtmItem *parentItem = childItem->parent(); if (parentItem == m_root) - { return QModelIndex(); - } return createIndex(parentItem->row(), 0, parentItem); } @@ -261,11 +230,9 @@ QModelIndex BookmarksTreeModel::parent(const QModelIndex &index) const QVariant BookmarksTreeModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) - { return QVariant(); - } - BtmItem *node = static_cast< BtmItem* >(index.internalPointer()); + BtmItem *node = static_cast(index.internalPointer()); if (node && node == m_root) { if (role == Qt::DisplayRole) @@ -288,7 +255,6 @@ void BookmarksTreeModel::bookmarksChanged(const QString &groupAddress) if (groupAddress.isEmpty()) { resetModel(); - emit bookmarksUpdated(); } else { @@ -296,11 +262,12 @@ void BookmarksTreeModel::bookmarksChanged(const QString &groupAddress) BtmItem *node = m_root; QModelIndex nodeIndex; - QStringList indexChain( groupAddress.split( '/', QString::SkipEmptyParts) ); - foreach( const QString &sIndex, indexChain ) + QStringList indexChain( groupAddress.split('/', QString::SkipEmptyParts) ); + bool ok; + int i; + foreach (const QString &sIndex, indexChain) { - bool ok; - int i = sIndex.toInt( &ok ); + i = sIndex.toInt( &ok ); if( !ok ) break; @@ -312,8 +279,9 @@ void BookmarksTreeModel::bookmarksChanged(const QString &groupAddress) } populate(node, Application::bookmarkProvider()->bookmarkManager()->findByAddress(groupAddress).toGroup()); endResetModel(); - emit bookmarksUpdated(); } + + emit bookmarksUpdated(); } @@ -328,10 +296,6 @@ void BookmarksTreeModel::setRoot(KBookmarkGroup bmg) beginResetModel(); delete m_root; m_root = new BtmItem(KBookmark()); - - if (bmg.isNull()) - return; - populate(m_root, bmg); endResetModel(); } @@ -375,7 +339,7 @@ QStringList BookmarksTreeModel::mimeTypes() const } -QMimeData* BookmarksTreeModel::mimeData(const QModelIndexList & indexes) const +QMimeData* BookmarksTreeModel::mimeData(const QModelIndexList &indexes) const { QMimeData *mimeData = new QMimeData; @@ -387,38 +351,33 @@ QMimeData* BookmarksTreeModel::mimeData(const QModelIndexList & indexes) const } -bool BookmarksTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex & parent) +bool BookmarksTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { - if (action == Qt::MoveAction) - { - if (data->hasFormat("application/rekonq-bookmark")) - { - QByteArray addresses = data->data("application/rekonq-bookmark"); - KBookmark bookmark = Application::bookmarkProvider()->bookmarkManager()->findByAddress(QString::fromLatin1(addresses.data())); + if (action != Qt::MoveAction || !data->hasFormat("application/rekonq-bookmark")) + return false; - QModelIndex destIndex = index(row, column, parent); + QByteArray addresses = data->data("application/rekonq-bookmark"); + KBookmark bookmark = Application::bookmarkProvider()->bookmarkManager()->findByAddress(QString::fromLatin1(addresses.data())); - KBookmark dropDestBookmark; - if (destIndex.isValid()) - dropDestBookmark = bookmarkForIndex(destIndex); + KBookmarkGroup root; + if (parent.isValid()) + root = bookmarkForIndex(parent).toGroup(); + else + root = Application::bookmarkProvider()->rootGroup(); - KBookmarkGroup root = Application::bookmarkProvider()->rootGroup(); - if (parent.isValid()) - root = bookmarkForIndex(parent).toGroup(); + QModelIndex destIndex = index(row, column, parent); - if (destIndex.isValid() && row != -1) - { - root.moveBookmark(bookmark, root.previous(dropDestBookmark)); + if (destIndex.isValid() && row != -1) + { + root.moveBookmark(bookmark, root.previous(bookmarkForIndex(destIndex))); + } + else + { + root.deleteBookmark(bookmark); + root.addBookmark(bookmark); + } - } - else - { - root.deleteBookmark(bookmark); - root.addBookmark(bookmark); - } + Application::bookmarkProvider()->bookmarkManager()->emitChanged(); - Application::bookmarkProvider()->bookmarkManager()->emitChanged(); - } - } return true; } diff --git a/src/bookmarks/bookmarkstreemodel.h b/src/bookmarks/bookmarkstreemodel.h index c509840b..0f115272 100644 --- a/src/bookmarks/bookmarkstreemodel.h +++ b/src/bookmarks/bookmarkstreemodel.h @@ -36,7 +36,7 @@ #include // Qt Includes -#include +#include class BtmItem { @@ -66,22 +66,33 @@ class REKONQ_TESTS_EXPORT BookmarksTreeModel : public QAbstractItemModel public: explicit BookmarksTreeModel(QObject *parent = 0); - ~BookmarksTreeModel(); + virtual ~BookmarksTreeModel(); + /** + * @return number of rows under the given parent. + */ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + /** + * @return number of columns (always 1). + */ virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; virtual Qt::ItemFlags flags(const QModelIndex &index) const; + /** + * @return index in the model specified by the given row, column and parent. + */ virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + /** + * @return parent of the given index. + */ virtual QModelIndex parent(const QModelIndex &index) const; virtual QVariant data(const QModelIndex &index, int role) const; virtual QStringList mimeTypes() const; - virtual bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent); + virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); virtual Qt::DropActions supportedDropActions() const; - virtual QMimeData *mimeData(const QModelIndexList & indexes) const; + virtual QMimeData *mimeData(const QModelIndexList &indexes) const; private slots: void bookmarksChanged(const QString &groupAddress); -- cgit v1.2.1