From 2e9b9376fd8857e4667d8c6e18cb0f66a3fc3726 Mon Sep 17 00:00:00 2001 From: nehlsen Date: Mon, 16 Nov 2009 15:03:42 +0100 Subject: added bookmarks panel and model(read only atm) --- src/bookmarkspanel/bookmarkspanel.cpp | 122 ++++++++++++++ src/bookmarkspanel/bookmarkspanel.h | 58 +++++++ src/bookmarkspanel/bookmarkstreemodel.cpp | 255 ++++++++++++++++++++++++++++++ src/bookmarkspanel/bookmarkstreemodel.h | 69 ++++++++ 4 files changed, 504 insertions(+) create mode 100644 src/bookmarkspanel/bookmarkspanel.cpp create mode 100644 src/bookmarkspanel/bookmarkspanel.h create mode 100644 src/bookmarkspanel/bookmarkstreemodel.cpp create mode 100644 src/bookmarkspanel/bookmarkstreemodel.h (limited to 'src/bookmarkspanel') diff --git a/src/bookmarkspanel/bookmarkspanel.cpp b/src/bookmarkspanel/bookmarkspanel.cpp new file mode 100644 index 00000000..1f3dff9b --- /dev/null +++ b/src/bookmarkspanel/bookmarkspanel.cpp @@ -0,0 +1,122 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Nils Weigel +* +* +* 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 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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 . +* +* ============================================================ */ + + +// rekonq includes +#include "bookmarkspanel.h" +#include "bookmarkstreemodel.h" + +// Auto Includes +#include "rekonq.h" + +// Qt includes +#include +#include +#include +#include +#include + +// KDE includes +#include +#include + +BookmarksPanel::BookmarksPanel(const QString &title, QWidget *parent, Qt::WindowFlags flags): + QDockWidget(title, parent, flags) +{ + setup(); + + setShown(ReKonfig::showBookmarksPanel()); +} + + +BookmarksPanel::~BookmarksPanel() +{ + ReKonfig::setShowBookmarksPanel(!isHidden()); + + delete ui; +} + +void BookmarksPanel::bookmarkActivated( const QModelIndex &index ) +{ + if( index.isValid() ) + emit openUrl( qVariantValue< KUrl >( index.data( Qt::UserRole ) ) ); +} + +void BookmarksPanel::setup() +{ + setObjectName("bookmarksPanel"); + setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); + + ui = new QWidget(this); + + // setup search bar + QHBoxLayout *searchLayout = new QHBoxLayout; + searchLayout->setContentsMargins(5, 0, 0, 0); + QLabel *searchLabel = new QLabel(i18n("Search:")); + searchLayout->addWidget(searchLabel); + KLineEdit *search = new KLineEdit; + search->setClearButtonShown(true); + searchLayout->addWidget(search); + + // setup tree view + QTreeView *treeView = new QTreeView(ui); + treeView->setUniformRowHeights(true); + treeView->setSelectionBehavior(QAbstractItemView::SelectRows); + treeView->setTextElideMode(Qt::ElideMiddle); + treeView->setAlternatingRowColors(true); + treeView->header()->hide(); + treeView->setRootIsDecorated( false ); + + // put everything together + QVBoxLayout *vBoxLayout = new QVBoxLayout; + vBoxLayout->setContentsMargins(0, 0, 0, 0); + vBoxLayout->addLayout(searchLayout); + vBoxLayout->addWidget(treeView); + + // add it to the UI + ui->setLayout(vBoxLayout); + setWidget(ui); + + BookmarksTreeModel *model = new BookmarksTreeModel( this ); + treeView->setModel( model ); + + connect( treeView, SIGNAL( activated(QModelIndex) ), this, SLOT( bookmarkActivated(QModelIndex) ) ); + +// QSortFilterProxyModel *proxy = new QSortFilterProxyModel(ui); + //- +// HistoryManager *historyManager = Application::historyManager(); +// QAbstractItemModel *model = historyManager->historyTreeModel(); +// +// m_treeProxyModel->setSourceModel(model); +// m_treeView->setModel(m_treeProxyModel); +// m_treeView->setExpanded(m_treeProxyModel->index(0, 0), true); +// m_treeView->header()->hideSection(1); +// QFontMetrics fm(font()); +// int header = fm.width(QLatin1Char('m')) * 40; +// m_treeView->header()->resizeSection(0, header); +// +// connect(search, SIGNAL(textChanged(QString)), m_treeProxy, SLOT(setFilterFixedString(QString))); +// connect(m_treeView, SIGNAL(activated(const QModelIndex&)), this, SLOT(open())); +} diff --git a/src/bookmarkspanel/bookmarkspanel.h b/src/bookmarkspanel/bookmarkspanel.h new file mode 100644 index 00000000..8c3e6121 --- /dev/null +++ b/src/bookmarkspanel/bookmarkspanel.h @@ -0,0 +1,58 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Nils Weigel +* +* +* 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 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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 . +* +* ============================================================ */ + + +#ifndef BOOKMARKSPANEL_H +#define BOOKMARKSPANEL_H + +// Qt Includes +#include + +// Forward Declarations +class KUrl; +class QModelIndex; + +class BookmarksPanel : public QDockWidget +{ + Q_OBJECT + Q_DISABLE_COPY(BookmarksPanel) + +public: + explicit BookmarksPanel(const QString &title, QWidget *parent = 0, Qt::WindowFlags flags = 0); + ~BookmarksPanel(); + +signals: + void openUrl(const KUrl &); + +private slots: + void bookmarkActivated( const QModelIndex &index ); + +private: + void setup(); + + QWidget *ui; +}; + +#endif // BOOKMARKSPANEL_H diff --git a/src/bookmarkspanel/bookmarkstreemodel.cpp b/src/bookmarkspanel/bookmarkstreemodel.cpp new file mode 100644 index 00000000..99441a63 --- /dev/null +++ b/src/bookmarkspanel/bookmarkstreemodel.cpp @@ -0,0 +1,255 @@ +#include "bookmarkstreemodel.h" + +// rekonq includes +#include "../application.h" +#include "../bookmarks.h" + +// KDE includes +#include +#include + +class BookmarksTreeModel::BtmItem +{ +public: + BtmItem(const KBookmark &bm): + m_parent(0), m_kbm(bm) + { + } + ~BtmItem() + { + qDeleteAll(m_children); + } + + QVariant data( int role = Qt::DisplayRole ) const + { + if( m_kbm.isNull() ) + return QVariant();// should only happen for root item + + if( role == Qt::DisplayRole ) + return m_kbm.text(); + if( role == Qt::DecorationRole ) + return KIcon( m_kbm.icon() ); + if( role == Qt::UserRole ) + return m_kbm.url(); + + return QVariant(); + } + + int row() const + { + if(m_parent) + return m_parent->m_children.indexOf( const_cast< BtmItem* >( this ) ); + return 0; + } + int childCount() const + { + return m_children.count(); + } + BtmItem* child( int n ) + { + Q_ASSERT(n>=0); + Q_ASSERT(nm_parent = this; + m_children << child; + } + void clear() + { + qDeleteAll(m_children); + m_children.clear(); + } + +private: + BtmItem *m_parent; + QList< BtmItem* > m_children; + + KBookmark m_kbm; +}; + +BookmarksTreeModel::BookmarksTreeModel(QObject *parent): + QAbstractItemModel(parent), m_root(0) +{ + resetModel(); + connect( Application::bookmarkProvider()->bookmarkManager(), SIGNAL( changed(QString,QString) ), this, SLOT( bookmarksChanged(QString) ) ); + connect( Application::bookmarkProvider()->bookmarkManager(), SIGNAL( bookmarksChanged(QString) ), this, SLOT( bookmarksChanged(QString) ) ); +} + +BookmarksTreeModel::~BookmarksTreeModel() +{ + delete m_root; +} + +int BookmarksTreeModel::rowCount(const QModelIndex &parent) const +{ + BtmItem *parentItem = 0; + if( !parent.isValid() ) { + parentItem = m_root; + } + else { + parentItem = static_cast< BtmItem* >( parent.internalPointer() ); + } + + return parentItem->childCount(); +} + +int BookmarksTreeModel::columnCount(const QModelIndex &/*parent*/) const +{ + // 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 +{ + return Qt::ItemIsEnabled|Qt::ItemIsSelectable; +} + +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() ); + } + + BtmItem *childItem = parentItem->child( row ); + if( childItem ) { + return createIndex( row, column, childItem ); + } + + return QModelIndex(); +} + +QModelIndex BookmarksTreeModel::parent(const QModelIndex &index) const +{ + if( !index.isValid() ) { + return QModelIndex(); + } + + BtmItem *childItem = static_cast< BtmItem* >( index.internalPointer() ); + BtmItem *parentItem = childItem->parent(); + + if( parentItem == m_root ) { + return QModelIndex(); + } + + return createIndex( parentItem->row(), 0, parentItem ); +} + +QVariant BookmarksTreeModel::data(const QModelIndex &index, int role) const +{ + if( !index.isValid() ) { + return QVariant(); + } + + BtmItem *node = static_cast< BtmItem* >( index.internalPointer() ); + if( node && node == m_root ) { + if( role == Qt::DisplayRole ) + return i18n( "Bookmarks" ); + else if( role == Qt::DecorationRole ) + return KIcon( "bookmarks" ); + } + else if( node ) { + return node->data( role ); + } + + return QVariant(); +} + +// bool BookmarksTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) +// { +// } + +void BookmarksTreeModel::bookmarksChanged( const QString &groupAddress ) +{ +// qDebug( "bookmarksChanged '%s'", qPrintable( groupAddress ) ); + + if( groupAddress.isEmpty() ) { + resetModel(); + return; + } + + BtmItem *node = m_root; + QModelIndex nodeIndex; + + QStringList indexChain( groupAddress.split( '/', QString::SkipEmptyParts) ); + foreach( QString sIndex, indexChain ) { + bool ok; + int i = sIndex.toInt( &ok ); + if( !ok ) + break; + + if( i < 0 || i >= node->childCount() ) + break; + + node = node->child( i ); + nodeIndex = index( i, 0, nodeIndex ); + } +// qDebug( " changed: '%s'(0-%d)", ( node == m_root ? "ROOT" : qPrintable( node->data().toString() ) ), node->childCount() ); + emit dataChanged( index( 0, 0, nodeIndex ), index( node->childCount(), 0, nodeIndex ) ); +} + +void BookmarksTreeModel::resetModel() +{ + setRoot(Application::bookmarkProvider()->rootGroup()); +} + +void BookmarksTreeModel::setRoot(KBookmarkGroup bmg) +{ + delete m_root; + m_root = new BtmItem(KBookmark()); + + if( bmg.isNull() ) { + return; + } + + populate( m_root, bmg ); + + reset(); +} + +void BookmarksTreeModel::populate( BtmItem *node, KBookmarkGroup bmg) +{ + node->clear(); + + if( bmg.isNull() ) { + return; + } + + KBookmark bm = bmg.first(); + while( !bm.isNull() ) { + BtmItem *newChild = new BtmItem( bm ); + if( bm.isGroup() ) + populate( newChild, bm.toGroup() ); + + node->appendChild( newChild ); + bm = bmg.next( bm ); + } +} diff --git a/src/bookmarkspanel/bookmarkstreemodel.h b/src/bookmarkspanel/bookmarkstreemodel.h new file mode 100644 index 00000000..9753999c --- /dev/null +++ b/src/bookmarkspanel/bookmarkstreemodel.h @@ -0,0 +1,69 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Nils Weigel +* +* +* 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 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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 . +* +* ============================================================ */ + + +#ifndef BOOKMARKSTREEMODEL_H +#define BOOKMARKSTREEMODEL_H + +// Qt Includes +#include + +// KDE includes +#include + +class BookmarksTreeModel : public QAbstractItemModel +{ + Q_OBJECT + Q_DISABLE_COPY(BookmarksTreeModel) + +public: + explicit BookmarksTreeModel(QObject *parent = 0); + ~BookmarksTreeModel(); + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + 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; + + virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + virtual QModelIndex parent(const QModelIndex &index) const; + virtual QVariant data(const QModelIndex &index, int role) const; +// virtual bool setData(const QModelIndex &index, const QVariant &value, int role); + +private slots: + void bookmarksChanged( const QString &groupAddress ); + +private: + class BtmItem; + BtmItem *m_root; + + void resetModel(); + + void setRoot(KBookmarkGroup bmg); + void populate( BtmItem *node, KBookmarkGroup bmg); +}; + +#endif // BOOKMARKSTREEMODEL_H -- cgit v1.2.1