diff options
author | kh1 <qt-info@nokia.com> | 2010-02-03 12:54:51 (GMT) |
---|---|---|
committer | kh1 <qt-info@nokia.com> | 2010-02-05 11:05:41 (GMT) |
commit | 919a8b360950718c68b4da68aeb22a402f99ae3b (patch) | |
tree | 38a67cd6f822b6af09c279779799597bf10d3e49 /tools | |
parent | 1e52af6135f293b353d15d2436e431bac426c5fb (diff) | |
download | Qt-919a8b360950718c68b4da68aeb22a402f99ae3b.zip Qt-919a8b360950718c68b4da68aeb22a402f99ae3b.tar.gz Qt-919a8b360950718c68b4da68aeb22a402f99ae3b.tar.bz2 |
Rewrote bookmark handling, the previous code was just plain awful.
Task-number: QT-2603
Reviewed-by: ck
Diffstat (limited to 'tools')
19 files changed, 2174 insertions, 1199 deletions
diff --git a/tools/assistant/tools/assistant/assistant.pro b/tools/assistant/tools/assistant/assistant.pro index 980f078..e62d30c 100644 --- a/tools/assistant/tools/assistant/assistant.pro +++ b/tools/assistant/tools/assistant/assistant.pro @@ -16,64 +16,79 @@ DEPENDPATH += ../shared # ## Work around a qmake issue when statically linking to # ## not-yet-installed plugins LIBS += -L$$QT_BUILD_TREE/plugins/sqldrivers -HEADERS += helpviewer.h \ - mainwindow.h \ - indexwindow.h \ - topicchooser.h \ +HEADERS += aboutdialog.h \ + bookmarkdialog.h \ + bookmarkfiltermodel.h \ + bookmarkitem.h \ + bookmarkmanager.h \ + bookmarkmodel.h \ + centralwidget.h \ + cmdlineparser.h \ contentwindow.h \ - searchwidget.h \ - preferencesdialog.h \ filternamedialog.h \ - centralwidget.h \ + helpenginewrapper.h \ + helpviewer.h \ + indexwindow.h \ installdialog.h \ - bookmarkmanager.h \ - remotecontrol.h \ - cmdlineparser.h \ - aboutdialog.h \ + mainwindow.h \ + preferencesdialog.h \ qtdocinstaller.h \ + remotecontrol.h \ + searchwidget.h \ + topicchooser.h \ + tracer.h \ xbelsupport.h \ - ../shared/collectionconfiguration.h \ - helpenginewrapper.h \ - tracer.h + ../shared/collectionconfiguration.h win32:HEADERS += remotecontrol_win.h -SOURCES += helpviewer.cpp \ - main.cpp \ - mainwindow.cpp \ - indexwindow.cpp \ - topicchooser.cpp \ + +SOURCES += aboutdialog.cpp \ + bookmarkdialog.cpp \ + bookmarkfiltermodel.cpp \ + bookmarkitem.cpp \ + bookmarkmanager.cpp \ + bookmarkmodel.cpp \ + centralwidget.cpp \ + cmdlineparser.cpp \ contentwindow.cpp \ - searchwidget.cpp \ - preferencesdialog.cpp \ filternamedialog.cpp \ - centralwidget.cpp \ + helpenginewrapper.cpp \ + helpviewer.cpp \ + indexwindow.cpp \ installdialog.cpp \ - bookmarkmanager.cpp \ - remotecontrol.cpp \ - cmdlineparser.cpp \ - aboutdialog.cpp \ + main.cpp \ + mainwindow.cpp \ + preferencesdialog.cpp \ qtdocinstaller.cpp \ + remotecontrol.cpp \ + searchwidget.cpp \ + topicchooser.cpp \ xbelsupport.cpp \ ../shared/collectionconfiguration.cpp \ - helpenginewrapper.cpp -FORMS += topicchooser.ui \ - preferencesdialog.ui \ + +FORMS += bookmarkdialog.ui \ + bookmarkwidget.ui \ filternamedialog.ui \ installdialog.ui \ - bookmarkdialog.ui + preferencesdialog.ui \ + topicchooser.ui + RESOURCES += assistant.qrc \ assistant_images.qrc -win32 { + +win32 { !wince*:LIBS += -lshell32 RC_FILE = assistant.rc } -mac { + +mac { ICON = assistant.icns TARGET = Assistant QMAKE_INFO_PLIST = Info_mac.plist } -contains(CONFIG, static): { + +contains(CONFIG, static): { SQLPLUGINS = $$unique(sql-plugins) - contains(SQLPLUGINS, sqlite): { + contains(SQLPLUGINS, sqlite): { QTPLUGIN += qsqlite DEFINES += USE_STATIC_SQLITE_PLUGIN } diff --git a/tools/assistant/tools/assistant/bookmarkdialog.cpp b/tools/assistant/tools/assistant/bookmarkdialog.cpp new file mode 100644 index 0000000..f081c15 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkdialog.cpp @@ -0,0 +1,222 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "tracer.h" + +#include "bookmarkdialog.h" +#include "bookmarkfiltermodel.h" +#include "bookmarkitem.h" +#include "bookmarkmodel.h" + +#include <QtGui/QKeyEvent> +#include <QtGui/QMenu> + +QT_BEGIN_NAMESPACE + +BookmarkDialog::BookmarkDialog(BookmarkModel *sourceModel, const QString &title, + const QString &url, QWidget *parent) + : QDialog(parent) + , m_url(url) + , m_title(title) + , bookmarkModel(sourceModel) +{ + TRACE_OBJ + ui.setupUi(this); + + ui.bookmarkEdit->setText(m_title); + ui.newFolderButton->setVisible(false); + ui.buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); + + connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(accepted())); + connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(rejected())); + connect(ui.newFolderButton, SIGNAL(clicked()), this, SLOT(addFolder())); + connect(ui.toolButton, SIGNAL(clicked()), this, SLOT(toolButtonClicked())); + connect(ui.bookmarkEdit, SIGNAL(textChanged(QString)), this, + SLOT(textChanged(QString))); + + bookmarkProxyModel = new BookmarkFilterModel(this); + bookmarkProxyModel->setSourceModel(bookmarkModel); + ui.bookmarkFolders->setModel(bookmarkProxyModel); + connect(ui.bookmarkFolders, SIGNAL(currentIndexChanged(int)), this, + SLOT(currentIndexChanged(int))); + + bookmarkTreeModel = new BookmarkTreeModel(this); + bookmarkTreeModel->setSourceModel(bookmarkModel); + ui.treeView->setModel(bookmarkTreeModel); + + ui.treeView->expandAll(); + ui.treeView->setVisible(false); + ui.treeView->installEventFilter(this); + ui.treeView->viewport()->installEventFilter(this); + ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(ui.treeView, SIGNAL(customContextMenuRequested(QPoint)), this, + SLOT(customContextMenuRequested(QPoint))); + connect(ui.treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex, + QModelIndex)), this, SLOT(currentIndexChanged(QModelIndex))); + + ui.bookmarkFolders->setCurrentIndex(0); + ui.treeView->setCurrentIndex(ui.treeView->indexAt(QPoint(2, 2))); +} + +BookmarkDialog::~BookmarkDialog() +{ + TRACE_OBJ +} + +bool BookmarkDialog::eventFilter(QObject *object, QEvent *event) +{ + TRACE_OBJ + if (object != ui.treeView && object != ui.treeView->viewport()) + return QWidget::eventFilter(object, event); + + if (event->type() == QEvent::KeyPress) { + QKeyEvent *ke = static_cast<QKeyEvent*>(event); + switch (ke->key()) { + case Qt::Key_F2: { + bookmarkModel->setItemsEditable(true); + ui.treeView->edit(ui.treeView->currentIndex()); + bookmarkModel->setItemsEditable(false); + } break; + default: break; + } + } + + return QObject::eventFilter(object, event); +} + +void BookmarkDialog::currentIndexChanged(int row) +{ + TRACE_OBJ + QModelIndex next = bookmarkProxyModel->index(row, 0, QModelIndex()); + if (next.isValid()) { + next = bookmarkProxyModel->mapToSource(next); + ui.treeView->setCurrentIndex(bookmarkTreeModel->mapFromSource(next)); + } +} + +void BookmarkDialog::currentIndexChanged(const QModelIndex &index) +{ + TRACE_OBJ + const QModelIndex current = bookmarkTreeModel->mapToSource(index); + if (current.isValid()) { + const int row = bookmarkProxyModel->mapFromSource(current).row(); + ui.bookmarkFolders->setCurrentIndex(row); + } +} + +void BookmarkDialog::accepted() +{ + TRACE_OBJ + QModelIndex index = ui.treeView->currentIndex(); + if (index.isValid()) { + index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index)); + if (BookmarkItem *item = bookmarkModel->itemFromIndex(index)) + item->setData(DataVector() << m_title << m_url << false); + } else + rejected(); + + accept(); +} + +void BookmarkDialog::rejected() +{ + TRACE_OBJ + foreach (const QPersistentModelIndex &index, cache) + bookmarkModel->removeItem(index); + reject(); +} + +void BookmarkDialog::addFolder() +{ + TRACE_OBJ + QModelIndex index = ui.treeView->currentIndex(); + if (index.isValid()) { + index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index), + true); + cache.append(index); + + index = bookmarkTreeModel->mapFromSource(index); + if (index.isValid()) { + bookmarkModel->setItemsEditable(true); + ui.treeView->edit(index); + ui.treeView->expand(index); + ui.treeView->setCurrentIndex(index); + bookmarkModel->setItemsEditable(false); + } + } +} + +void BookmarkDialog::toolButtonClicked() +{ + TRACE_OBJ + const bool visible = !ui.treeView->isVisible(); + ui.treeView->setVisible(visible); + ui.newFolderButton->setVisible(visible); + + if (visible) { + resize(QSize(width(), 400)); + ui.toolButton->setText(QLatin1String("-")); + } else { + resize(width(), minimumHeight()); + ui.toolButton->setText(QLatin1String("+")); + } +} + +void BookmarkDialog::textChanged(const QString& text) +{ + m_title = text; +} + +void BookmarkDialog::customContextMenuRequested(const QPoint &point) +{ + TRACE_OBJ + QMenu menu(QLatin1String(""), this); + QAction *renameItem = menu.addAction(tr("Rename Folder")); + + QAction *picked = menu.exec(ui.treeView->mapToGlobal(point)); + if (picked == renameItem) { + bookmarkModel->setItemsEditable(true); + ui.treeView->edit(ui.treeView->currentIndex()); + bookmarkModel->setItemsEditable(false); + } +} + +QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/bookmarkdialog.h b/tools/assistant/tools/assistant/bookmarkdialog.h new file mode 100644 index 0000000..ba38c7a --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkdialog.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef BOOKMARKDIALOG_H +#define BOOKMARKDIALOG_H + +#include "ui_bookmarkdialog.h" + +QT_BEGIN_NAMESPACE + +class BookmarkModel; +class BookmarkFilterModel; +class BookmarkTreeModel; + +class BookmarkDialog : public QDialog +{ + Q_OBJECT +public: + BookmarkDialog(BookmarkModel *bookmarkModel, const QString &title, + const QString &url, QWidget *parent = 0); + ~BookmarkDialog(); + +private: + bool eventFilter(QObject *object, QEvent *event); + +private slots: + void currentIndexChanged(int index); + void currentIndexChanged(const QModelIndex &index); + + void accepted(); + void rejected(); + + void addFolder(); + void toolButtonClicked(); + void textChanged(const QString& text); + void customContextMenuRequested(const QPoint &point); + +private: + QString m_url; + QString m_title; + Ui::BookmarkDialog ui; + QList<QPersistentModelIndex> cache; + + BookmarkModel *bookmarkModel; + BookmarkTreeModel *bookmarkTreeModel; + BookmarkFilterModel *bookmarkProxyModel; +}; + +QT_END_NAMESPACE + +#endif // BOOKMARKDIALOG_H diff --git a/tools/assistant/tools/assistant/bookmarkdialog.ui b/tools/assistant/tools/assistant/bookmarkdialog.ui index 7a878f9..5131531 100644 --- a/tools/assistant/tools/assistant/bookmarkdialog.ui +++ b/tools/assistant/tools/assistant/bookmarkdialog.ui @@ -1,38 +1,39 @@ -<ui version="4.0" > +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> <class>BookmarkDialog</class> - <widget class="QDialog" name="BookmarkDialog" > - <property name="geometry" > + <widget class="QDialog" name="BookmarkDialog"> + <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>450</width> - <height>135</height> + <height>133</height> </rect> </property> - <property name="sizePolicy" > - <sizepolicy vsizetype="Preferred" hsizetype="Preferred" > + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> - <property name="windowTitle" > + <property name="windowTitle"> <string>Add Bookmark</string> </property> - <layout class="QVBoxLayout" name="verticalLayout_3" > + <layout class="QVBoxLayout" name="verticalLayout_3"> <item> - <layout class="QHBoxLayout" name="horizontalLayout" > + <layout class="QHBoxLayout" name="horizontalLayout"> <item> - <layout class="QVBoxLayout" name="verticalLayout_2" > + <layout class="QVBoxLayout" name="verticalLayout_2"> <item> - <widget class="QLabel" name="label" > - <property name="text" > + <widget class="QLabel" name="label"> + <property name="text"> <string>Bookmark:</string> </property> </widget> </item> <item> - <widget class="QLabel" name="label_2" > - <property name="text" > + <widget class="QLabel" name="label_2"> + <property name="text"> <string>Add in Folder:</string> </property> </widget> @@ -40,35 +41,35 @@ </layout> </item> <item> - <layout class="QVBoxLayout" name="verticalLayout" > + <layout class="QVBoxLayout" name="verticalLayout"> <item> - <widget class="QLineEdit" name="bookmarkEdit" /> + <widget class="QLineEdit" name="bookmarkEdit"/> </item> <item> - <widget class="QComboBox" name="bookmarkFolders" /> + <widget class="QComboBox" name="bookmarkFolders"/> </item> </layout> </item> </layout> </item> <item> - <layout class="QHBoxLayout" name="horizontalLayout_3" > + <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> - <widget class="QToolButton" name="toolButton" > - <property name="minimumSize" > + <widget class="QToolButton" name="toolButton"> + <property name="minimumSize"> <size> <width>25</width> <height>20</height> </size> </property> - <property name="text" > + <property name="text"> <string>+</string> </property> </widget> </item> <item> - <widget class="Line" name="line" > - <property name="orientation" > + <widget class="Line" name="line"> + <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </widget> @@ -76,30 +77,39 @@ </layout> </item> <item> - <widget class="QTreeView" name="treeView" > - <property name="sizePolicy" > - <sizepolicy vsizetype="Ignored" hsizetype="Expanding" > + <widget class="QTreeView" name="treeView"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Ignored"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> + <property name="uniformRowHeights"> + <bool>true</bool> + </property> + <property name="allColumnsShowFocus"> + <bool>true</bool> + </property> + <property name="headerHidden"> + <bool>true</bool> + </property> </widget> </item> <item> - <layout class="QHBoxLayout" name="horizontalLayout_4" > + <layout class="QHBoxLayout" name="horizontalLayout_4"> <item> - <widget class="QPushButton" name="newFolderButton" > - <property name="text" > + <widget class="QPushButton" name="newFolderButton"> + <property name="text"> <string>New Folder</string> </property> </widget> </item> <item> - <widget class="QDialogButtonBox" name="buttonBox" > - <property name="orientation" > + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> <enum>Qt::Horizontal</enum> </property> - <property name="standardButtons" > + <property name="standardButtons"> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> @@ -116,11 +126,11 @@ <receiver>BookmarkDialog</receiver> <slot>accept()</slot> <hints> - <hint type="sourcelabel" > + <hint type="sourcelabel"> <x>248</x> <y>254</y> </hint> - <hint type="destinationlabel" > + <hint type="destinationlabel"> <x>157</x> <y>274</y> </hint> @@ -132,11 +142,11 @@ <receiver>BookmarkDialog</receiver> <slot>reject()</slot> <hints> - <hint type="sourcelabel" > + <hint type="sourcelabel"> <x>316</x> <y>260</y> </hint> - <hint type="destinationlabel" > + <hint type="destinationlabel"> <x>286</x> <y>274</y> </hint> diff --git a/tools/assistant/tools/assistant/bookmarkfiltermodel.cpp b/tools/assistant/tools/assistant/bookmarkfiltermodel.cpp new file mode 100644 index 0000000..5874493 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkfiltermodel.cpp @@ -0,0 +1,322 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "bookmarkfiltermodel.h" + +#include "bookmarkitem.h" +#include "bookmarkmodel.h" + +BookmarkFilterModel::BookmarkFilterModel(QObject *parent) + : QAbstractProxyModel(parent) + , hideBookmarks(true) + , sourceModel(0) +{ +} + +void +BookmarkFilterModel::setSourceModel(QAbstractItemModel *_sourceModel) +{ + beginResetModel(); + + QAbstractProxyModel::setSourceModel(sourceModel); + sourceModel = qobject_cast<BookmarkModel*> (_sourceModel); + + connect(sourceModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, + SLOT(changed(QModelIndex, QModelIndex))); + + connect(sourceModel, SIGNAL(rowsInserted(QModelIndex, int, int)), + this, SLOT(rowsInserted(QModelIndex, int, int))); + + connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), + this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int))); + connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, + SLOT(rowsRemoved(QModelIndex, int, int))); + + connect(sourceModel, SIGNAL(layoutAboutToBeChanged()), this, + SLOT(layoutAboutToBeChanged())); + connect(sourceModel, SIGNAL(layoutChanged()), this, + SLOT(layoutChanged())); + + connect(sourceModel, SIGNAL(modelAboutToBeReset()), this, + SLOT(modelAboutToBeReset())); + connect(sourceModel, SIGNAL(modelReset()), this, SLOT(modelReset())); + + if (sourceModel) + setupCache(sourceModel->index(0, 0, QModelIndex())); + + endResetModel(); +} + +int +BookmarkFilterModel::rowCount(const QModelIndex &index) const +{ + Q_UNUSED(index) + return cache.count(); +} + +int +BookmarkFilterModel::columnCount(const QModelIndex &index) const +{ + Q_UNUSED(index) + if (sourceModel) + return sourceModel->columnCount(); + return 0; +} + +QModelIndex +BookmarkFilterModel::mapToSource(const QModelIndex &proxyIndex) const +{ + const int row = proxyIndex.row(); + if (proxyIndex.isValid() && row >= 0 && row < cache.count()) + return cache[row]; + return QModelIndex(); +} + +QModelIndex +BookmarkFilterModel::mapFromSource(const QModelIndex &sourceIndex) const +{ + return index(cache.indexOf(sourceIndex), 0, QModelIndex()); +} + +QModelIndex +BookmarkFilterModel::parent(const QModelIndex &child) const +{ + Q_UNUSED(child) + return QModelIndex(); +} + +QModelIndex +BookmarkFilterModel::index(int row, int column, const QModelIndex &index) const +{ + Q_UNUSED(index) + if (row < 0 || column < 0 || cache.count() <= row + || !sourceModel || sourceModel->columnCount() <= column) { + return QModelIndex(); + } + return createIndex(row, 0); +} + +Qt::DropActions +BookmarkFilterModel::supportedDropActions () const +{ + if (sourceModel) + return sourceModel->supportedDropActions(); + return Qt::IgnoreAction; +} + +Qt::ItemFlags +BookmarkFilterModel::flags(const QModelIndex &index) const +{ + if (sourceModel) + return sourceModel->flags(index); + return Qt::NoItemFlags; +} + +QVariant +BookmarkFilterModel::data(const QModelIndex &index, int role) const +{ + if (sourceModel) + return sourceModel->data(mapToSource(index), role); + return QVariant(); +} + +bool +BookmarkFilterModel::setData(const QModelIndex &index, const QVariant &value, + int role) +{ + if (sourceModel) + return sourceModel->setData(mapToSource(index), value, role); + return false; +} + +void +BookmarkFilterModel::filterBookmarks() +{ + if (sourceModel) { + beginResetModel(); + hideBookmarks = true; + setupCache(sourceModel->index(0, 0, QModelIndex())); + endResetModel(); + } +} + +void +BookmarkFilterModel::filterBookmarkFolders() +{ + if (sourceModel) { + beginResetModel(); + hideBookmarks = false; + setupCache(sourceModel->index(0, 0, QModelIndex())); + endResetModel(); + } +} + +void +BookmarkFilterModel::changed(const QModelIndex &topLeft, + const QModelIndex &bottomRight) +{ + emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight)); +} + +void +BookmarkFilterModel::rowsInserted(const QModelIndex &parent, int start, int end) +{ + if (!sourceModel) + return; + + QModelIndex cachePrevious = parent; + if (BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) { + BookmarkItem *newItem = parentItem->child(start); + + // iterate over tree hirarchie to find the previous folder + for (int i = 0; i < parentItem->childCount(); ++i) { + if (BookmarkItem *child = parentItem->child(i)) { + const QModelIndex &tmp = sourceModel->indexFromItem(child); + if (tmp.data(UserRoleFolder).toBool() && child != newItem) + cachePrevious = tmp; + } + } + + const QModelIndex &newIndex = sourceModel->indexFromItem(newItem); + const bool isFolder = newIndex.data(UserRoleFolder).toBool(); + if ((isFolder && hideBookmarks) || (!isFolder && !hideBookmarks)) { + beginInsertRows(mapFromSource(parent), start, end); + cache.insert(cache.indexOf(cachePrevious) + 1, newIndex); + endInsertRows(); + } + } +} + +void +BookmarkFilterModel::rowsAboutToBeRemoved(const QModelIndex &parent, int start, + int end) +{ + if (!sourceModel) + return; + + if (BookmarkItem *parentItem = sourceModel->itemFromIndex(parent)) { + if (BookmarkItem *child = parentItem->child(start)) { + indexToRemove = sourceModel->indexFromItem(child); + if (cache.contains(indexToRemove)) + beginRemoveRows(mapFromSource(parent), start, end); + } + } +} + +void +BookmarkFilterModel::rowsRemoved(const QModelIndex &/*parent*/, int, int) +{ + if (cache.contains(indexToRemove)) { + cache.removeAll(indexToRemove); + endRemoveRows(); + } +} + +void +BookmarkFilterModel::layoutAboutToBeChanged() +{ + // TODO: ??? +} + +void +BookmarkFilterModel::layoutChanged() +{ + // TODO: ??? +} + +void +BookmarkFilterModel::modelAboutToBeReset() +{ + beginResetModel(); +} + +void +BookmarkFilterModel::modelReset() +{ + if (sourceModel) + setupCache(sourceModel->index(0, 0, QModelIndex())); + endResetModel(); +} + +void +BookmarkFilterModel::setupCache(const QModelIndex &parent) +{ + cache.clear(); + collectItems(parent); +} + +void +BookmarkFilterModel::collectItems(const QModelIndex &parent) +{ + if (parent.isValid()) { + bool isFolder = sourceModel->data(parent, UserRoleFolder).toBool(); + if ((isFolder && hideBookmarks) || (!isFolder && !hideBookmarks)) + cache.append(parent); + + if (sourceModel->hasChildren(parent)) { + for (int i = 0; i < sourceModel->rowCount(parent); ++i) + collectItems(sourceModel->index(i, 0, parent)); + } + } +} + +// -- BookmarkTreeModel + +BookmarkTreeModel::BookmarkTreeModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ +} + +int +BookmarkTreeModel::columnCount(const QModelIndex &parent) const +{ + return qMin(1, QSortFilterProxyModel::columnCount(parent)); +} + +bool +BookmarkTreeModel::filterAcceptsRow(int row, const QModelIndex &parent) const +{ + Q_UNUSED(row) + BookmarkModel *model = qobject_cast<BookmarkModel*> (sourceModel()); + if (model->rowCount(parent) > 0 + && model->data(model->index(row, 0, parent), UserRoleFolder).toBool()) + return true; + return false; +}
\ No newline at end of file diff --git a/tools/assistant/tools/assistant/bookmarkfiltermodel.h b/tools/assistant/tools/assistant/bookmarkfiltermodel.h new file mode 100644 index 0000000..4ea7ab9 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkfiltermodel.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef BOOKMARKFILTERMODEL_H +#define BOOKMARKFILTERMODEL_H + +#include <QtCore/QPersistentModelIndex> + +#include <QtGui/QAbstractProxyModel> +#include <QtGui/QSortFilterProxyModel> + +QT_BEGIN_NAMESPACE + +class BookmarkItem; +class BookmarkModel; + +typedef QList<QPersistentModelIndex> PersistentModelIndexCache; + +class BookmarkFilterModel : public QAbstractProxyModel +{ + Q_OBJECT +public: + explicit BookmarkFilterModel(QObject *parent = 0); + + void setSourceModel(QAbstractItemModel *sourceModel); + + int rowCount(const QModelIndex &index) const; + int columnCount(const QModelIndex &index) const; + + QModelIndex mapToSource(const QModelIndex &proxyIndex) const; + QModelIndex mapFromSource(const QModelIndex &sourceIndex) const; + + QModelIndex parent(const QModelIndex &child) const; + QModelIndex index(int row, int column, const QModelIndex &parent) const; + + Qt::DropActions supportedDropActions () const; + Qt::ItemFlags flags(const QModelIndex &index) const; + + QVariant data(const QModelIndex &index, int role) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + + void filterBookmarks(); + void filterBookmarkFolders(); + +private slots: + void changed(const QModelIndex &topLeft, const QModelIndex &bottomRight); + void rowsInserted(const QModelIndex &parent, int start, int end); + void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end); + void rowsRemoved(const QModelIndex &parent, int start, int end); + void layoutAboutToBeChanged(); + void layoutChanged(); + void modelAboutToBeReset(); + void modelReset(); + +private: + void setupCache(const QModelIndex &parent); + void collectItems(const QModelIndex &parent); + +private: + bool hideBookmarks; + BookmarkModel *sourceModel; + PersistentModelIndexCache cache; + QPersistentModelIndex indexToRemove; +}; + +// -- BookmarkTreeModel + +class BookmarkTreeModel : public QSortFilterProxyModel +{ + Q_OBJECT +public: + BookmarkTreeModel(QObject *parent = 0); + int columnCount(const QModelIndex &parent = QModelIndex()) const; + +protected: + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; +}; + +QT_END_NAMESPACE + +#endif // BOOKMARKFILTERMODEL_H diff --git a/tools/assistant/tools/assistant/bookmarkitem.cpp b/tools/assistant/tools/assistant/bookmarkitem.cpp new file mode 100644 index 0000000..082ffce --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkitem.cpp @@ -0,0 +1,138 @@ +#include "bookmarkitem.h" + +#include <QtCore/QDebug> + +QT_BEGIN_NAMESPACE + +BookmarkItem::BookmarkItem(const DataVector &data, BookmarkItem *parent) + : m_data(data) + , m_parent(parent) +{ +} + +BookmarkItem::~BookmarkItem() +{ + qDeleteAll(m_children); +} + +BookmarkItem* +BookmarkItem::parent() const +{ + return m_parent; +} + +void +BookmarkItem::setParent(BookmarkItem *parent) +{ + m_parent = parent; +} + +void +BookmarkItem::addChild(BookmarkItem *child) +{ + child->setParent(this); + m_children.append(child); +} + +BookmarkItem* +BookmarkItem::child(int number) const +{ + if (number >= 0 && number < m_children.count()) + return m_children[number]; + return 0; +} + +int BookmarkItem::childCount() const +{ + return m_children.count(); +} + +int BookmarkItem::childNumber() const +{ + if (m_parent) + return m_parent->m_children.indexOf(const_cast<BookmarkItem*>(this)); + return 0; +} + +QVariant +BookmarkItem::data(int column) const +{ + if (column == 0) + return m_data[0]; + + if (column == 1 || column == UserRoleUrl) + return m_data[1]; + + if (column == UserRoleFolder) + return m_data[1].toString() == QLatin1String("Folder"); + + if (column == UserRoleExpanded) + return m_data[2]; + + return QVariant(); +} + +void +BookmarkItem::setData(const DataVector &data) +{ + m_data = data; +} + +bool +BookmarkItem::setData(int column, const QVariant &newValue) +{ + int index = -1; + if (column == 0 || column == 1) + index = column; + + if (column == UserRoleUrl || column == UserRoleFolder) + index = 1; + + if (column == UserRoleExpanded) + index = 2; + + if (index < 0) + return false; + + m_data[index] = newValue; + return true; +} + +bool +BookmarkItem::insertChildren(bool isFolder, int position, int count) +{ + if (position < 0 || position > m_children.size()) + return false; + + for (int row = 0; row < count; ++row) { + m_children.insert(position, new BookmarkItem(DataVector() + << QObject::tr(isFolder ? "New Folder" : "Untitled") + << (isFolder ? "Folder" : "about:blank") << false, this)); + } + + return true; +} + +bool +BookmarkItem::removeChildren(int position, int count) +{ + if (position < 0 || position > m_children.size()) + return false; + + for (int row = 0; row < count; ++row) + delete m_children.takeAt(position); + + return true; +} + +void +BookmarkItem::dumpTree(int indent) const +{ + const QString tree(indent, ' '); + qDebug() << tree + (data(UserRoleFolder).toBool() ? "Folder" : "Bookmark") + << "Label:" << data(0).toString() << "parent:" << m_parent << "this:" + << this; + + foreach (BookmarkItem *item, m_children) + item->dumpTree(indent + 4); +} diff --git a/tools/assistant/tools/assistant/bookmarkitem.h b/tools/assistant/tools/assistant/bookmarkitem.h new file mode 100644 index 0000000..be77e24 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkitem.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef BOOKMARKITEM_H +#define BOOKMARKITEM_H + +#include <QtCore/QVariant> +#include <QtCore/QVector> + +QT_BEGIN_NAMESPACE + +enum { + UserRoleUrl = Qt::UserRole + 50, + UserRoleFolder = Qt::UserRole + 100, + UserRoleExpanded = Qt::UserRole + 150 +}; + +typedef QVector<QVariant> DataVector; + +class BookmarkItem +{ +public: + BookmarkItem(const DataVector &data, BookmarkItem *parent = 0); + ~BookmarkItem(); + + BookmarkItem *parent() const; + void setParent(BookmarkItem *parent); + + void addChild(BookmarkItem *child); + BookmarkItem *child(int number) const; + + int childCount() const; + int childNumber() const; + + QVariant data(int column) const; + void setData(const DataVector &data); + bool setData(int column, const QVariant &value); + + bool insertChildren(bool isFolder, int position, int count); + bool removeChildren(int position, int count); + + void dumpTree(int indent) const; + +private: + DataVector m_data; + + BookmarkItem *m_parent; + QList<BookmarkItem*> m_children; +}; + +QT_END_NAMESPACE + +#endif // BOOKMARKITEM_H diff --git a/tools/assistant/tools/assistant/bookmarkmanager.cpp b/tools/assistant/tools/assistant/bookmarkmanager.cpp index b7bd449..8fba811 100644 --- a/tools/assistant/tools/assistant/bookmarkmanager.cpp +++ b/tools/assistant/tools/assistant/bookmarkmanager.cpp @@ -41,575 +41,207 @@ #include "tracer.h" #include "bookmarkmanager.h" +#include "bookmarkdialog.h" +#include "bookmarkfiltermodel.h" +#include "bookmarkitem.h" +#include "bookmarkmodel.h" #include "centralwidget.h" #include "helpenginewrapper.h" +#include <QtGui/QFileDialog> #include <QtGui/QMenu> -#include <QtGui/QIcon> -#include <QtGui/QStyle> -#include <QtGui/QLabel> -#include <QtGui/QLayout> -#include <QtCore/QEvent> -#include <QtGui/QComboBox> #include <QtGui/QKeyEvent> -#include <QtGui/QLineEdit> #include <QtGui/QMessageBox> -#include <QtGui/QHeaderView> -#include <QtGui/QToolButton> -#include <QtGui/QPushButton> -#include <QtGui/QApplication> -#include <QtGui/QDialogButtonBox> #include <QtGui/QSortFilterProxyModel> -QT_BEGIN_NAMESPACE - -BookmarkDialog::BookmarkDialog(BookmarkManager *manager, const QString &title, - const QString &url, QWidget *parent) - : QDialog(parent) - , m_url(url) - , m_title(title) - , bookmarkManager(manager) -{ - TRACE_OBJ - ui.setupUi(this); - - installEventFilter(this); - ui.treeView->installEventFilter(this); - ui.treeView->viewport()->installEventFilter(this); - - ui.bookmarkEdit->setText(title); - ui.newFolderButton->setVisible(false); - ui.buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); - ui.bookmarkFolders->addItems(bookmarkManager->bookmarkFolders()); - - proxyModel = new QSortFilterProxyModel(this); - proxyModel->setFilterKeyColumn(0); - proxyModel->setDynamicSortFilter(true); - proxyModel->setFilterRole(Qt::UserRole + 10); - proxyModel->setSourceModel(bookmarkManager->treeBookmarkModel()); - proxyModel->setFilterRegExp(QRegExp(QLatin1String("Folder"), - Qt::CaseSensitive, QRegExp::FixedString)); - ui.treeView->setModel(proxyModel); - - ui.treeView->expandAll(); - ui.treeView->setVisible(false); - ui.treeView->header()->setVisible(false); - ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu); - - connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject())); - connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(addAccepted())); - connect(ui.newFolderButton, SIGNAL(clicked()), this, SLOT(addNewFolder())); - connect(ui.toolButton, SIGNAL(clicked()), this, SLOT(toolButtonClicked())); - connect(ui.bookmarkEdit, SIGNAL(textChanged(QString)), this, - SLOT(textChanged(QString))); - - connect(bookmarkManager->treeBookmarkModel(), - SIGNAL(itemChanged(QStandardItem*)), - this, SLOT(itemChanged(QStandardItem*))); - - connect(ui.bookmarkFolders, SIGNAL(currentIndexChanged(QString)), this, - SLOT(selectBookmarkFolder(QString))); +#include <QFile> +#include "xbelsupport.h" - connect(ui.treeView, SIGNAL(customContextMenuRequested(QPoint)), this, - SLOT(customContextMenuRequested(QPoint))); +QT_BEGIN_NAMESPACE - connect(ui.treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex, - QModelIndex)), this, SLOT(currentChanged(QModelIndex))); -} +// -- BookmarkManager::BookmarkWidget -BookmarkDialog::~BookmarkDialog() +void BookmarkManager::BookmarkWidget::focusInEvent(QFocusEvent *event) { TRACE_OBJ -} + if (event->reason() != Qt::MouseFocusReason) { + ui.lineEdit->selectAll(); + ui.lineEdit->setFocus(); -void BookmarkDialog::addAccepted() -{ - TRACE_OBJ - QItemSelectionModel *model = ui.treeView->selectionModel(); - const QModelIndexList &list = model->selection().indexes(); - - QModelIndex index; - if (!list.isEmpty()) - index = proxyModel->mapToSource(list.at(0)); - - bookmarkManager->addNewBookmark(index, ui.bookmarkEdit->text(), m_url); - accept(); -} - -void BookmarkDialog::addNewFolder() -{ - TRACE_OBJ - QItemSelectionModel *model = ui.treeView->selectionModel(); - const QModelIndexList &list = model->selection().indexes(); - - QModelIndex index; - if (!list.isEmpty()) - index = list.at(0); - - QModelIndex newFolder = - bookmarkManager->addNewFolder(proxyModel->mapToSource(index)); - if (newFolder.isValid()) { - ui.treeView->expand(index); - const QModelIndex &index = proxyModel->mapFromSource(newFolder); - model->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); - - ui.bookmarkFolders->clear(); - ui.bookmarkFolders->addItems(bookmarkManager->bookmarkFolders()); - - const QString &name = index.data().toString(); - ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->findText(name)); - renameFolder(index, newFolder); + // force the focus in event on bookmark manager + emit focusInEvent(); } } -void BookmarkDialog::toolButtonClicked() -{ - TRACE_OBJ - bool visible = !ui.treeView->isVisible(); - ui.treeView->setVisible(visible); - ui.newFolderButton->setVisible(visible); - - if (visible) { - resize(QSize(width(), 400)); - ui.toolButton->setText(QLatin1String("-")); - } else { - resize(width(), minimumHeight()); - ui.toolButton->setText(QLatin1String("+")); - } -} +// -- BookmarkManager::BookmarkTreeView -void BookmarkDialog::itemChanged(QStandardItem *item) +BookmarkManager::BookmarkTreeView::BookmarkTreeView(QWidget *parent) + : QTreeView(parent) { TRACE_OBJ - if (renameItem != item) { - renameItem = item; - oldText = item->text(); - return; - } + setAcceptDrops(true); + setDragEnabled(true); + setAutoExpandDelay(1000); + setUniformRowHeights(true); + setDropIndicatorShown(true); + setExpandsOnDoubleClick(true); - if (item->text() != oldText) { - ui.bookmarkFolders->clear(); - ui.bookmarkFolders->addItems(bookmarkManager->bookmarkFolders()); + connect(this, SIGNAL(expanded(QModelIndex)), this, + SLOT(setExpandedData(QModelIndex))); + connect(this, SIGNAL(collapsed(QModelIndex)), this, + SLOT(setExpandedData(QModelIndex))); - QString name = tr("Bookmarks"); - const QModelIndex &index = ui.treeView->currentIndex(); - if (index.isValid()) - name = index.data().toString(); - ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->findText(name)); - } } -void BookmarkDialog::textChanged(const QString &string) +void BookmarkManager::BookmarkTreeView::subclassKeyPressEvent(QKeyEvent *event) { TRACE_OBJ - ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!string.isEmpty()); + QTreeView::keyPressEvent(event); } -void BookmarkDialog::selectBookmarkFolder(const QString &folderName) +void BookmarkManager::BookmarkTreeView::setExpandedData(const QModelIndex &index) { TRACE_OBJ - if (folderName.isEmpty()) - return; - - if (folderName == tr("Bookmarks")) { - ui.treeView->clearSelection(); - return; - } - - QStandardItemModel *model = bookmarkManager->treeBookmarkModel(); - QList<QStandardItem*> list = model->findItems(folderName, - Qt::MatchCaseSensitive | Qt::MatchRecursive, 0); - if (!list.isEmpty()) { - const QModelIndex &index = model->indexFromItem(list.at(0)); - QItemSelectionModel *model = ui.treeView->selectionModel(); - if (model) { - model->setCurrentIndex(proxyModel->mapFromSource(index), - QItemSelectionModel::ClearAndSelect); - } - } + if (BookmarkModel *treeModel = qobject_cast<BookmarkModel*> (model())) + treeModel->setData(index, isExpanded(index), UserRoleExpanded); } -void BookmarkDialog::customContextMenuRequested(const QPoint &point) -{ - TRACE_OBJ - QModelIndex index = ui.treeView->indexAt(point); - if (!index.isValid()) - return; - - QMenu menu(QLatin1String(""), this); +// -- BookmarkManager - QAction *removeItem = menu.addAction(tr("Delete Folder")); - QAction *renameItem = menu.addAction(tr("Rename Folder")); +QMutex BookmarkManager::mutex; +BookmarkManager* BookmarkManager::bookmarkManager = 0; - QAction *picked = menu.exec(ui.treeView->mapToGlobal(point)); - if (!picked) - return; - - const QModelIndex &proxyIndex = proxyModel->mapToSource(index); - if (picked == removeItem) { - bookmarkManager->removeBookmarkItem(ui.treeView, proxyIndex); - ui.bookmarkFolders->clear(); - ui.bookmarkFolders->addItems(bookmarkManager->bookmarkFolders()); - - QString name = tr("Bookmarks"); - index = ui.treeView->currentIndex(); - if (index.isValid()) - name = index.data().toString(); - ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->findText(name)); - } else if (picked == renameItem) { - renameFolder(index, proxyIndex); - } -} +// -- public -void BookmarkDialog::renameFolder(const QModelIndex &index, - const QModelIndex &proxyIndex) +BookmarkManager* BookmarkManager::instance() { TRACE_OBJ - const BookmarkModel * const model = bookmarkManager->treeBookmarkModel(); - if (QStandardItem *item = model->itemFromIndex(proxyIndex)) { - item->setEditable(true); - ui.treeView->edit(index); - item->setEditable(false); + if (!bookmarkManager) { + QMutexLocker _(&mutex); + if (!bookmarkManager) + bookmarkManager = new BookmarkManager(); } + return bookmarkManager; } -void BookmarkDialog::currentChanged(const QModelIndex ¤t) +void BookmarkManager::destroy() { TRACE_OBJ - QString text = tr("Bookmarks"); - if (current.isValid()) - text = current.data().toString(); - ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->findText(text)); + delete bookmarkManager; + bookmarkManager = 0; } -bool BookmarkDialog::eventFilter(QObject *object, QEvent *e) +QWidget* BookmarkManager::bookmarkDockWidget() const { TRACE_OBJ - if (object != ui.treeView && object != ui.treeView->viewport()) - return QWidget::eventFilter(object, e); - - if (e->type() == QEvent::KeyPress) { - QKeyEvent *ke = static_cast<QKeyEvent*>(e); - switch (ke->key()) { - case Qt::Key_F2: { - const QModelIndex &index = ui.treeView->currentIndex(); - const QModelIndex &source = proxyModel->mapToSource(index); - QStandardItem *item = - bookmarkManager->treeBookmarkModel()->itemFromIndex(source); - if (item) { - item->setEditable(true); - ui.treeView->edit(index); - item->setEditable(false); - } - } break; - - case Qt::Key_Delete: { - const QModelIndex &index = ui.treeView->currentIndex(); - bookmarkManager->removeBookmarkItem(ui.treeView, - proxyModel->mapToSource(index)); - ui.bookmarkFolders->clear(); - ui.bookmarkFolders->addItems(bookmarkManager->bookmarkFolders()); - - QString name = tr("Bookmarks"); - if (index.isValid()) - name = index.data().toString(); - ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->findText(name)); - } break; - - default: - break; - } - } - - return QObject::eventFilter(object, e); + if (bookmarkWidget) + return bookmarkWidget; + return 0; } - -// BookmarkWidget - - -BookmarkWidget::BookmarkWidget(BookmarkManager *manager, QWidget *parent, - bool showButtons) - : QWidget(parent) - , addButton(0) - , removeButton(0) - , bookmarkManager(manager) +void BookmarkManager::takeBookmarksMenu(QMenu* menu) { TRACE_OBJ - setup(showButtons); - - installEventFilter(this); - treeView->installEventFilter(this); - treeView->viewport()->installEventFilter(this); + bookmarkMenu = menu; + refeshBookmarkMenu(); } -BookmarkWidget::~BookmarkWidget() -{ - TRACE_OBJ -} +// -- public slots -void BookmarkWidget::removeClicked() +void BookmarkManager::addBookmark(const QString &title, const QString &url) { TRACE_OBJ - const QModelIndex &index = treeView->currentIndex(); - if (searchField->text().isEmpty()) { - bookmarkManager->removeBookmarkItem(treeView, - filterBookmarkModel->mapToSource(index)); - } + showBookmarkDialog(title.isEmpty() ? tr("Untiled") : title, + url.isEmpty() ? QLatin1String("about:blank") : url); } -void BookmarkWidget::filterChanged() -{ - TRACE_OBJ - bool searchBookmarks = searchField->text().isEmpty(); - if (!searchBookmarks) { - regExp.setPattern(searchField->text()); - filterBookmarkModel->setSourceModel(bookmarkManager->listBookmarkModel()); - } else { - regExp.setPattern(QLatin1String("")); - filterBookmarkModel->setSourceModel(bookmarkManager->treeBookmarkModel()); - } - - if (addButton) - addButton->setEnabled(searchBookmarks); - - if (removeButton) - removeButton->setEnabled(searchBookmarks); - - filterBookmarkModel->setFilterRegExp(regExp); +// -- private - const QModelIndex &index = treeView->indexAt(QPoint(1, 1)); - if (index.isValid()) - treeView->setCurrentIndex(index); - - if (searchBookmarks) - expandItems(); -} +BookmarkManager::BookmarkManager() + : typeAndSearch(false) + , bookmarkMenu(0) + , bookmarkModel(new BookmarkModel) + , bookmarkWidget(new BookmarkWidget) + , bookmarkTreeView(new BookmarkTreeView) +{ + TRACE_OBJ + bookmarkWidget->installEventFilter(this); + connect(bookmarkWidget->ui.add, SIGNAL(clicked()), this, + SLOT(addBookmark())); + connect(bookmarkWidget->ui.remove, SIGNAL(clicked()), this, + SLOT(removeBookmark())); + connect(bookmarkWidget->ui.lineEdit, SIGNAL(textChanged(QString)), this, + SLOT(textChanged(QString))); + connect(bookmarkWidget, SIGNAL(focusInEvent()), this, SLOT(focusInEvent())); -void BookmarkWidget::expand(const QModelIndex &index) -{ - TRACE_OBJ - const QModelIndex &source = filterBookmarkModel->mapToSource(index); - QStandardItem *item = - bookmarkManager->treeBookmarkModel()->itemFromIndex(source); - if (item) - item->setData(treeView->isExpanded(index), Qt::UserRole + 11); -} + bookmarkTreeView->setModel(bookmarkModel); + bookmarkTreeView->installEventFilter(this); + bookmarkTreeView->viewport()->installEventFilter(this); + bookmarkTreeView->setContextMenuPolicy(Qt::CustomContextMenu); + bookmarkWidget->ui.stackedWidget->addWidget(bookmarkTreeView); -void BookmarkWidget::activated(const QModelIndex &index) -{ - TRACE_OBJ - if (!index.isValid()) - return; + connect(bookmarkTreeView, SIGNAL(activated(QModelIndex)), this, + SLOT(setSourceFromIndex(QModelIndex))); + connect(bookmarkTreeView, SIGNAL(customContextMenuRequested(QPoint)), this, + SLOT(customContextMenuRequested(QPoint))); - QString data = index.data(Qt::UserRole + 10).toString(); - if (data != QLatin1String("Folder")) - emit requestShowLink(data); + connect(&HelpEngineWrapper::instance(), SIGNAL(setupFinished()), this, + SLOT(setupFinished())); + connect(bookmarkModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, + SLOT(refeshBookmarkMenu())); } -void BookmarkWidget::customContextMenuRequested(const QPoint &point) +BookmarkManager::~BookmarkManager() { TRACE_OBJ - QModelIndex index = treeView->indexAt(point); - if (!index.isValid()) - return; - - QAction *showItem = 0; - QAction *removeItem = 0; - QAction *renameItem = 0; - QAction *showItemNewTab = 0; - - QMenu menu(QLatin1String(""), this); - QString data = index.data(Qt::UserRole + 10).toString(); - if (data == QLatin1String("Folder")) { - removeItem = menu.addAction(tr("Delete Folder")); - renameItem = menu.addAction(tr("Rename Folder")); - } else { - showItem = menu.addAction(tr("Show Bookmark")); - showItemNewTab = menu.addAction(tr("Show Bookmark in New Tab")); - if (searchField->text().isEmpty()) { - menu.addSeparator(); - removeItem = menu.addAction(tr("Delete Bookmark")); - renameItem = menu.addAction(tr("Rename Bookmark")); - } - } - - QAction *pickedAction = menu.exec(treeView->mapToGlobal(point)); - if (!pickedAction) - return; - - if (pickedAction == showItem) { - emit requestShowLink(data); - } - else if (pickedAction == showItemNewTab) { - CentralWidget::instance()->setSourceInNewTab(data); - } - else if (pickedAction == removeItem) { - bookmarkManager->removeBookmarkItem(treeView, - filterBookmarkModel->mapToSource(index)); - } - else if (pickedAction == renameItem) { - const QModelIndex &source = filterBookmarkModel->mapToSource(index); - QStandardItem *item = - bookmarkManager->treeBookmarkModel()->itemFromIndex(source); - if (item) { - item->setEditable(true); - treeView->edit(index); - item->setEditable(false); - } - } + HelpEngineWrapper::instance().setBookmarks(bookmarkModel->bookmarks()); } -void BookmarkWidget::setup(bool showButtons) +void BookmarkManager::removeItem(const QModelIndex &index) { TRACE_OBJ - regExp.setPatternSyntax(QRegExp::FixedString); - regExp.setCaseSensitivity(Qt::CaseInsensitive); - - QLayout *vlayout = new QVBoxLayout(this); - vlayout->setMargin(4); - - QLabel *label = new QLabel(tr("Filter:"), this); - vlayout->addWidget(label); - - searchField = new QLineEdit(this); - vlayout->addWidget(searchField); - connect(searchField, SIGNAL(textChanged(QString)), this, - SLOT(filterChanged())); - - treeView = new TreeView(this); - vlayout->addWidget(treeView); - -#ifdef Q_OS_MAC -# define SYSTEM "mac" -#else -# define SYSTEM "win" -#endif - - if (showButtons) { - QLayout *hlayout = new QHBoxLayout(); - vlayout->addItem(hlayout); - - hlayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding)); - - addButton = new QToolButton(this); - addButton->setText(tr("Add")); - addButton->setIcon(QIcon(QLatin1String(":/trolltech/assistant/images/" - SYSTEM "/addtab.png"))); - addButton->setAutoRaise(true); - addButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - hlayout->addWidget(addButton); - connect(addButton, SIGNAL(clicked()), this, SIGNAL(addBookmark())); - - removeButton = new QToolButton(this); - removeButton->setText(tr("Remove")); - removeButton->setIcon(QIcon(QLatin1String(":/trolltech/assistant/images/" - SYSTEM "/closetab.png"))); - removeButton->setAutoRaise(true); - removeButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - hlayout->addWidget(removeButton); - connect(removeButton, SIGNAL(clicked()), this, SLOT(removeClicked())); + QModelIndex current = index; + if (typeAndSearch) { // need to map because of proxy + current = typeAndSearchModel->mapToSource(current); + current = bookmarkFilterModel->mapToSource(current); + } else if (!bookmarkModel->parent(index).isValid()) { + return; // check if we should delete the "Bookmarks Menu", bail } - filterBookmarkModel = new QSortFilterProxyModel(this); - treeView->setModel(filterBookmarkModel); - - treeView->setDragEnabled(true); - treeView->setAcceptDrops(true); - treeView->setAutoExpandDelay(1000); - treeView->setDropIndicatorShown(true); - treeView->header()->setVisible(false); - treeView->setContextMenuPolicy(Qt::CustomContextMenu); - - connect(treeView, SIGNAL(expanded(QModelIndex)), this, - SLOT(expand(QModelIndex))); - connect(treeView, SIGNAL(collapsed(QModelIndex)), this, - SLOT(expand(QModelIndex))); - connect(treeView, SIGNAL(activated(QModelIndex)), this, - SLOT(activated(QModelIndex))); - connect(treeView, SIGNAL(customContextMenuRequested(QPoint)), - this, SLOT(customContextMenuRequested(QPoint))); - - filterBookmarkModel->setFilterKeyColumn(0); - filterBookmarkModel->setDynamicSortFilter(true); - filterBookmarkModel->setSourceModel(bookmarkManager->treeBookmarkModel()); - - expandItems(); -} - -void BookmarkWidget::expandItems() -{ - TRACE_OBJ - QStandardItemModel *model = bookmarkManager->treeBookmarkModel(); - QList<QStandardItem*>list = model->findItems(QLatin1String("*"), - Qt::MatchWildcard | Qt::MatchRecursive, 0); - foreach (const QStandardItem *item, list) { - const QModelIndex &index = model->indexFromItem(item); - treeView->setExpanded(filterBookmarkModel->mapFromSource(index), - item->data(Qt::UserRole + 11).toBool()); + if (bookmarkModel->hasChildren(current)) { + int value = QMessageBox::question(bookmarkTreeView, tr("Remove"), + tr("You are going to delete a Folder, this will also<br>" + "remove it's content. Are you sure to continue?"), + QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel); + if (value == QMessageBox::Cancel) + return; } + bookmarkModel->removeItem(current); } -void BookmarkWidget::focusInEvent(QFocusEvent *e) +bool BookmarkManager::eventFilter(QObject *object, QEvent *event) { - TRACE_OBJ - if (e->reason() != Qt::MouseFocusReason) { - searchField->selectAll(); - searchField->setFocus(); - - QModelIndex index = treeView->indexAt(QPoint(1, 1)); - if (index.isValid()) - treeView->setCurrentIndex(index); - - } -} + if (object != bookmarkTreeView && object != bookmarkTreeView->viewport() + && object != bookmarkWidget) + return QObject::eventFilter(object, event); -bool BookmarkWidget::eventFilter(QObject *object, QEvent *e) -{ TRACE_OBJ - if (object != this && object != treeView - && object != treeView->viewport()) { - return QWidget::eventFilter(object, e); - } - - if (e->type() == QEvent::KeyPress) { - QKeyEvent *ke = static_cast<QKeyEvent*>(e); - const bool tree = object == treeView || object == treeView->viewport(); + const bool isWidget = object == bookmarkWidget; + if (event->type() == QEvent::KeyPress) { + QKeyEvent *ke = static_cast<QKeyEvent*>(event); switch (ke->key()) { case Qt::Key_F2: { - const QModelIndex &index = treeView->currentIndex(); - const QModelIndex &src = filterBookmarkModel->mapToSource(index); - if (tree && searchField->text().isEmpty()) { - if (QStandardItem *item = bookmarkManager->treeBookmarkModel() - ->itemFromIndex(src)) { - item->setEditable(true); - treeView->edit(index); - item->setEditable(false); - } - } - } break; - - case Qt::Key_Enter: { - case Qt::Key_Return: - if (tree) { - const QString &data = treeView->selectionModel()->currentIndex() - .data(Qt::UserRole + 10).toString(); - if (!data.isEmpty() && data != QLatin1String("Folder")) - emit requestShowLink(data); - } + renameBookmark(bookmarkTreeView->currentIndex()); } break; case Qt::Key_Delete: { - const QModelIndex &index = treeView->currentIndex(); - const QModelIndex &src = filterBookmarkModel->mapToSource(index); - if (tree && searchField->text().isEmpty()) - bookmarkManager->removeBookmarkItem(treeView, src); + removeItem(bookmarkTreeView->currentIndex()); } break; - case Qt::Key_Up: { + case Qt::Key_Up: { // needs event filter on widget case Qt::Key_Down: - if (!tree) - treeView->subclassKeyPressEvent(ke); + if (isWidget) + bookmarkTreeView->subclassKeyPressEvent(ke); } break; case Qt::Key_Escape: { @@ -620,348 +252,255 @@ bool BookmarkWidget::eventFilter(QObject *object, QEvent *e) } } - if (e->type() == QEvent::MouseButtonRelease) { - QMouseEvent *me = static_cast<QMouseEvent*>(e); - bool controlPressed = me->modifiers() & Qt::ControlModifier; - if(((me->button() == Qt::LeftButton) && controlPressed) - || (me->button() == Qt::MidButton)) { - const QModelIndex &index = treeView->currentIndex(); - const QString &data = index.data(Qt::UserRole + 10).toString(); - if (!data.isEmpty() && data != QLatin1String("Folder")) - CentralWidget::instance()->setSourceInNewTab(data); + if (event->type() == QEvent::MouseButtonRelease && !isWidget) { + QMouseEvent *me = static_cast<QMouseEvent*>(event); + switch (me->button()) { + case Qt::LeftButton: { + if (me->modifiers() & Qt::ControlModifier) + setSourceFromIndex(bookmarkTreeView->currentIndex(), true); + } break; + + case Qt::MidButton: { + setSourceFromIndex(bookmarkTreeView->currentIndex(), true); + } break; + + default: break; } } - return QWidget::eventFilter(object, e); + return QObject::eventFilter(object, event); } - -// BookmarkModel - - -BookmarkModel::BookmarkModel(int rows, int columns, QObject *parent) - : QStandardItemModel(rows, columns, parent) +void BookmarkManager::buildBookmarksMenu(const QModelIndex &index, QMenu* menu) { TRACE_OBJ -} + if (!index.isValid()) + return; -BookmarkModel::~BookmarkModel() -{ - TRACE_OBJ + const QString &text = index.data().toString(); + const QIcon &icon = qVariantValue<QIcon>(index.data(Qt::DecorationRole)); + if (index.data(UserRoleFolder).toBool()) { + if (QMenu* subMenu = menu->addMenu(icon, text)) { + for (int i = 0; i < bookmarkModel->rowCount(index); ++i) + buildBookmarksMenu(bookmarkModel->index(i, 0, index), subMenu); + } + } else { + QAction *action = menu->addAction(icon, text); + action->setData(index.data(UserRoleUrl).toString()); + } } -Qt::DropActions BookmarkModel::supportedDropActions() const +void BookmarkManager::showBookmarkDialog(const QString &name, const QString &url) { TRACE_OBJ - return Qt::MoveAction; + BookmarkDialog dialog(bookmarkModel, name, url, bookmarkTreeView); + dialog.exec(); } -Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const +// -- private slots + +void BookmarkManager::setupFinished() { TRACE_OBJ - Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index); - if ((!index.isValid()) // can only happen for the invisible root item - || index.data(Qt::UserRole + 10).toString() == QLatin1String("Folder")) - return (Qt::ItemIsDropEnabled | defaultFlags) &~ Qt::ItemIsDragEnabled; - - return (Qt::ItemIsDragEnabled | defaultFlags) &~ Qt::ItemIsDropEnabled; -} + bookmarkModel->setBookmarks(HelpEngineWrapper::instance().bookmarks()); + bookmarkModel->expandFoldersIfNeeeded(bookmarkTreeView); + refeshBookmarkMenu(); -// BookmarkManager + bookmarkTreeView->hideColumn(1); + bookmarkTreeView->header()->setVisible(false); + bookmarkTreeView->header()->setStretchLastSection(true); + bookmarkFilterModel = new BookmarkFilterModel(this); + bookmarkFilterModel->setSourceModel(bookmarkModel); + bookmarkFilterModel->filterBookmarkFolders(); -BookmarkManager::BookmarkManager() - : treeModel(new BookmarkModel(0, 1, this)) - , listModel(new BookmarkModel(0, 1, this)) - , renameItem(0) -{ - TRACE_OBJ - folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon); - bookmarkIcon = QIcon(QLatin1String(":/trolltech/assistant/images/bookmark.png")); - - connect(treeModel, SIGNAL(itemChanged(QStandardItem*)), this, - SLOT(itemChanged(QStandardItem*))); - connect(treeModel, SIGNAL(itemChanged(QStandardItem*)), this, - SIGNAL(bookmarksChanged())); - connect(treeModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), - this, SIGNAL(bookmarksChanged())); + typeAndSearchModel = new QSortFilterProxyModel(this); + typeAndSearchModel->setDynamicSortFilter(true); + typeAndSearchModel->setSourceModel(bookmarkFilterModel); } -BookmarkManager::~BookmarkManager() +void BookmarkManager::addBookmark() { TRACE_OBJ - treeModel->clear(); - listModel->clear(); + if (CentralWidget *widget = CentralWidget::instance()) { + showBookmarkDialog(widget->currentTitle(), + widget->currentSource().toString()); + } } -BookmarkModel* BookmarkManager::treeBookmarkModel() +void BookmarkManager::removeBookmark() { TRACE_OBJ - return treeModel; + removeItem(bookmarkTreeView->currentIndex()); } -BookmarkModel* BookmarkManager::listBookmarkModel() -{ - TRACE_OBJ - return listModel; -} +//void BookmarkManager::manageBookmarks() +//{ +// TRACE_OBJ +//} -void BookmarkManager::saveBookmarks() +void BookmarkManager::refeshBookmarkMenu() { TRACE_OBJ - QByteArray bookmarks; - QDataStream stream(&bookmarks, QIODevice::WriteOnly); + if (!bookmarkMenu) + return; - readBookmarksRecursive(treeModel->invisibleRootItem(), stream, 0); - HelpEngineWrapper::instance().setBookmarks(bookmarks); -} + bookmarkMenu->clear(); -QStringList BookmarkManager::bookmarkFolders() const -{ - TRACE_OBJ - QStringList folders(tr("Bookmarks")); + //bookmarkMenu->addAction(tr("Manage Bookmarks..."), this, + // SLOT(manageBookmarks())); + bookmarkMenu->addAction(tr("Import..."), this, SLOT(importBookmarks())); + bookmarkMenu->addAction(tr("Export..."), this, SLOT(exportBookmarks())); + bookmarkMenu->addAction(tr("Add Bookmark..."), this, SLOT(addBookmark()), + QKeySequence(tr("Ctrl+D"))); + bookmarkMenu->addSeparator(); - QList<QStandardItem*>list = treeModel->findItems(QLatin1String("*"), - Qt::MatchWildcard | Qt::MatchRecursive, 0); + const QModelIndex &root = bookmarkModel->index(0, 0, QModelIndex()); + for (int i = 0; i < bookmarkModel->rowCount(root); ++i) + buildBookmarksMenu(bookmarkModel->index(i, 0, root), bookmarkMenu); - QString data; - foreach (const QStandardItem *item, list) { - data = item->data(Qt::UserRole + 10).toString(); - if (data == QLatin1String("Folder")) - folders << item->data(Qt::DisplayRole).toString(); - } - return folders; + connect(bookmarkMenu, SIGNAL(triggered(QAction*)), this, + SLOT(setSourceFromAction(QAction*))); } -QModelIndex BookmarkManager::addNewFolder(const QModelIndex &index) +void BookmarkManager::renameBookmark(const QModelIndex &index) { - TRACE_OBJ - QStandardItem *item = new QStandardItem(uniqueFolderName()); - item->setEditable(false); - item->setData(false, Qt::UserRole + 11); - item->setData(QLatin1String("Folder"), Qt::UserRole + 10); - item->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon)); - - if (index.isValid()) { - treeModel->itemFromIndex(index)->appendRow(item); - } else { - treeModel->appendRow(item); - } - return treeModel->indexFromItem(item); + // check if we should rename the "Bookmarks Menu", bail + if (!typeAndSearch && !bookmarkModel->parent(index).isValid()) + return; + + bookmarkModel->setItemsEditable(true); + bookmarkTreeView->edit(index); + bookmarkModel->setItemsEditable(false); } -void BookmarkManager::removeBookmarkItem(QTreeView *treeView, - const QModelIndex &index) +void BookmarkManager::importBookmarks() { TRACE_OBJ - QStandardItem *item = treeModel->itemFromIndex(index); - if (item) { - QString data = index.data(Qt::UserRole + 10).toString(); - if (data == QLatin1String("Folder") && item->rowCount() > 0) { - int value = QMessageBox::question(treeView, tr("Remove"), - tr("You are going to delete a Folder, this will also<br>" - "remove it's content. Are you sure to continue?"), - QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel); - - if (value == QMessageBox::Cancel) - return; - } + const QString &fileName = QFileDialog::getOpenFileName(0, tr("Open File"), + QDir::currentPath(), tr("Files (*.xbel)")); - if (data != QLatin1String("Folder")) { - QList<QStandardItem*>itemList = listModel->findItems(item->text()); - foreach (const QStandardItem *i, itemList) { - if (i->data(Qt::UserRole + 10) == data) { - listModel->removeRow(i->row()); - break; - } - } - } else { - removeBookmarkFolderItems(item); - } - treeModel->removeRow(item->row(), index.parent()); + if (fileName.isEmpty()) + return; + + QFile file(fileName); + if (file.open(QIODevice::ReadOnly)) { + XbelReader reader(bookmarkModel); + reader.readFromFile(&file); } } -void BookmarkManager::showBookmarkDialog(QWidget *parent, const QString &name, - const QString &url) +void BookmarkManager::exportBookmarks() { TRACE_OBJ - BookmarkDialog dialog(this, name, url, parent); - dialog.exec(); -} + QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"), + QLatin1String("untitled.xbel"), tr("Files (*.xbel)")); -void BookmarkManager::addNewBookmark(const QModelIndex &index, - const QString &name, const QString &url) -{ - TRACE_OBJ - QStandardItem *item = new QStandardItem(name); - item->setEditable(false); - item->setIcon(bookmarkIcon); - item->setData(false, Qt::UserRole + 11); - item->setData(url, Qt::UserRole + 10); + const QLatin1String suffix(".xbel"); + if (!fileName.endsWith(suffix)) + fileName.append(suffix); - if (index.isValid()) - treeModel->itemFromIndex(index)->appendRow(item); - else - treeModel->appendRow(item); - listModel->appendRow(item->clone()); - emit bookmarksChanged(); + QFile file(fileName); + if (file.open(QIODevice::WriteOnly)) { + XbelWriter writer(bookmarkModel); + writer.writeToFile(&file); + } else { + QMessageBox::information(bookmarkTreeView, tr("Qt Assistant"), + tr("Unable to save bookmarks."), tr("OK")); + } } -void BookmarkManager::fillBookmarkMenu(QMenu *menu) +void BookmarkManager::setSourceFromAction(QAction *action) { TRACE_OBJ - if (!menu || !treeModel) - return; + const QVariant &data = action->data(); - map.clear(); - fillBookmarkMenu(menu, treeModel->invisibleRootItem()); + if (data.canConvert<QUrl>()) + emit setSource(data.toUrl()); } -void BookmarkManager::fillBookmarkMenu(QMenu *menu, QStandardItem *root) +void BookmarkManager::setSourceFromIndex(const QModelIndex &index, bool newTab) { TRACE_OBJ - for (int i = 0; i < root->rowCount(); ++i) { - QStandardItem *item = root->child(i); - if (item && item->data(Qt::UserRole + 10) - .toString() == QLatin1String("Folder")) { - QMenu* newMenu = menu->addMenu(folderIcon, item->text()); - if (item->rowCount() > 0) - fillBookmarkMenu(newMenu, item); - } else { - map.insert(menu->addAction(item->text()), item->index()); - } - } -} + QAbstractItemModel *base = bookmarkModel; + if (typeAndSearch) + base = typeAndSearchModel; -QUrl BookmarkManager::urlForAction(QAction* action) const -{ - TRACE_OBJ - if (map.contains(action)) { - const QModelIndex &index = map.value(action); - if (QStandardItem* item = treeModel->itemFromIndex(index)) - return QUrl(item->data(Qt::UserRole + 10).toString()); + if (base->data(index, UserRoleFolder).toBool()) + return; + + const QVariant &data = base->data(index, UserRoleUrl); + if (data.canConvert<QUrl>()) { + if (newTab) + emit setSourceInNewTab(data.toUrl()); + else + emit setSource(data.toUrl()); } - return QUrl(); } -void BookmarkManager::itemChanged(QStandardItem *item) +void BookmarkManager::customContextMenuRequested(const QPoint &point) { TRACE_OBJ - if (renameItem != item) { - renameItem = item; - oldText = item->text(); + QModelIndex index = bookmarkTreeView->indexAt(point); + if (!index.isValid()) return; - } - if (item->text() != oldText) { - if (item->data(Qt::UserRole + 10).toString() != QLatin1String("Folder")) { - QList<QStandardItem*>itemList = listModel->findItems(oldText); - if (itemList.count() > 0) - itemList.at(0)->setText(item->text()); - } - } -} + // check if we should open the menu on "Bookmarks Menu", bail + if (!typeAndSearch && !bookmarkModel->parent(index).isValid()) + return; -void BookmarkManager::setupBookmarkModels() -{ - TRACE_OBJ - treeModel->clear(); - listModel->clear(); - - qint32 depth; - bool expanded; - QString name, type; - QList<int> lastDepths; - QList<QStandardItem*> parents; - - QByteArray ba = HelpEngineWrapper::instance().bookmarks(); - QDataStream stream(ba); - while (!stream.atEnd()) { - stream >> depth >> name >> type >> expanded; - - QStandardItem *item = new QStandardItem(name); - item->setEditable(false); - item->setData(type, Qt::UserRole + 10); - item->setData(expanded, Qt::UserRole + 11); - if (depth == 0) { - parents.clear(); lastDepths.clear(); - treeModel->appendRow(item); - parents << item; lastDepths << depth; - } else { - if (depth <= lastDepths.last()) { - while (depth <= lastDepths.last() && parents.count() > 0) { - parents.pop_back(); lastDepths.pop_back(); - } - } - parents.last()->appendRow(item); - if (type == QLatin1String("Folder")) { - parents << item; lastDepths << depth; - } - } + QAction *remove = 0; + QAction *rename = 0; + QAction *showItem = 0; + QAction *showItemInNewTab = 0; - if (type != QLatin1String("Folder")) { - item->setIcon(bookmarkIcon); - listModel->appendRow(item->clone()); - } else { - item->setIcon(folderIcon); - } + QMenu menu(QLatin1String("")); + if (!typeAndSearch && bookmarkModel->data(index, UserRoleFolder).toBool()) { + remove = menu.addAction(tr("Delete Folder")); + rename = menu.addAction(tr("Rename Folder")); + } else { + showItem = menu.addAction(tr("Show Bookmark")); + showItemInNewTab = menu.addAction(tr("Show Bookmark in New Tab")); + menu.addSeparator(); + remove = menu.addAction(tr("Delete Bookmark")); + rename = menu.addAction(tr("Rename Bookmark")); } -} -QString BookmarkManager::uniqueFolderName() const -{ - TRACE_OBJ - QString folderName = tr("New Folder"); - QList<QStandardItem*> list = treeModel->findItems(folderName, - Qt::MatchContains | Qt::MatchRecursive, 0); - if (!list.isEmpty()) { - QStringList names; - foreach (const QStandardItem *item, list) - names << item->text(); - - for (int i = 1; i <= names.count(); ++i) { - folderName = (tr("New Folder") + QLatin1String(" %1")).arg(i); - if (!names.contains(folderName)) - break; - } - } - return folderName; + QAction *pickedAction = menu.exec(bookmarkTreeView->mapToGlobal(point)); + if (pickedAction == rename) + renameBookmark(index); + else if (pickedAction == remove) + removeItem(index); + else if (pickedAction == showItem || pickedAction == showItemInNewTab) + setSourceFromIndex(index, pickedAction == showItemInNewTab); } -void BookmarkManager::removeBookmarkFolderItems(QStandardItem *item) +void BookmarkManager::focusInEvent() { TRACE_OBJ - for (int j = 0; j < item->rowCount(); ++j) { - QStandardItem *child = item->child(j); - if (child->rowCount() > 0) - removeBookmarkFolderItems(child); - - QString data = child->data(Qt::UserRole + 10).toString(); - QList<QStandardItem*>itemList = listModel->findItems(child->text()); - foreach (const QStandardItem *i, itemList) { - if (i->data(Qt::UserRole + 10) == data) { - listModel->removeRow(i->row()); - break; - } - } - } + const QModelIndex &index = bookmarkTreeView->indexAt(QPoint(2, 2)); + if (index.isValid()) + bookmarkTreeView->setCurrentIndex(index); } -void BookmarkManager::readBookmarksRecursive(const QStandardItem *item, - QDataStream &stream, const qint32 depth) const +void BookmarkManager::textChanged(const QString &text) { TRACE_OBJ - for (int j = 0; j < item->rowCount(); ++j) { - const QStandardItem *child = item->child(j); - stream << depth; - stream << child->data(Qt::DisplayRole).toString(); - stream << child->data(Qt::UserRole + 10).toString(); - stream << child->data(Qt::UserRole + 11).toBool(); - - if (child->rowCount() > 0) - readBookmarksRecursive(child, stream, (depth +1)); + if (!bookmarkWidget->ui.lineEdit->text().isEmpty()) { + if (!typeAndSearch) { + typeAndSearch = true; + bookmarkTreeView->setItemsExpandable(false); + bookmarkTreeView->setRootIsDecorated(false); + bookmarkTreeView->setModel(typeAndSearchModel); + } + typeAndSearchModel->setFilterRegExp(QRegExp(text)); + } else { + typeAndSearch = false; + bookmarkTreeView->setModel(bookmarkModel); + bookmarkTreeView->setItemsExpandable(true); + bookmarkTreeView->setRootIsDecorated(true); + bookmarkModel->expandFoldersIfNeeeded(bookmarkTreeView); } } diff --git a/tools/assistant/tools/assistant/bookmarkmanager.h b/tools/assistant/tools/assistant/bookmarkmanager.h index fbacc2c..88342d5 100644 --- a/tools/assistant/tools/assistant/bookmarkmanager.h +++ b/tools/assistant/tools/assistant/bookmarkmanager.h @@ -38,179 +38,118 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - #ifndef BOOKMARKMANAGER_H #define BOOKMARKMANAGER_H -#include "ui_bookmarkdialog.h" - -#include <QtCore/QUrl> -#include <QtCore/QObject> -#include <QtCore/QString> -#include <QtCore/QByteArray> -#include <QtCore/QDataStream> - -#include <QtGui/QIcon> -#include <QtGui/QDialog> -#include <QtGui/QWidget> +#include <QtCore/QMutex> #include <QtGui/QTreeView> -#include <QtGui/QStandardItemModel> + +#include "ui_bookmarkwidget.h" QT_BEGIN_NAMESPACE -class QEvent; -class QLineEdit; -class QTreeView; -class QToolButton; -class QStandardItem; -class QHelpEngineCore; -class QAbstractItemModel; +class BookmarkModel; +class BookmarkFilterModel; +class QKeyEvent; class QSortFilterProxyModel; -class BookmarkManager; - -class BookmarkDialog : public QDialog +class BookmarkManager : public QObject { Q_OBJECT + class BookmarkWidget; + class BookmarkTreeView; + class BookmarkListView; + Q_DISABLE_COPY(BookmarkManager); public: - BookmarkDialog(BookmarkManager *manager, const QString &title, - const QString &url, QWidget *parent = 0); - ~BookmarkDialog(); + static BookmarkManager* instance(); + static void destroy(); -private slots: - void addAccepted(); - void addNewFolder(); - void toolButtonClicked(); - void itemChanged(QStandardItem *item); - void textChanged(const QString& string); - void selectBookmarkFolder(const QString &folderName); - void customContextMenuRequested(const QPoint &point); - void currentChanged(const QModelIndex& current); + QWidget* bookmarkDockWidget() const; + void takeBookmarksMenu(QMenu* menu); -private: - bool eventFilter(QObject *object, QEvent *e); - void renameFolder(const QModelIndex &index, const QModelIndex &proxyIndex); +public slots: + void addBookmark(const QString &title, const QString &url); + +signals: + void escapePressed(); + void setSource(const QUrl &url); + void setSourceInNewTab(const QUrl &url); private: - QString m_url; - QString m_title; + BookmarkManager(); + ~BookmarkManager(); - QString oldText; - QStandardItem *renameItem; + void removeItem(const QModelIndex &index); + bool eventFilter(QObject *object, QEvent *event); + void buildBookmarksMenu(const QModelIndex &index, QMenu *menu); + void showBookmarkDialog(const QString &name, const QString &url); - Ui::BookmarkDialog ui; - BookmarkManager *bookmarkManager; - QSortFilterProxyModel *proxyModel; -}; +private slots: + void setupFinished(); -class TreeView : public QTreeView { - Q_OBJECT -public: - TreeView(QWidget* parent = 0) : QTreeView(parent) {} - void subclassKeyPressEvent(QKeyEvent* event) - { - QTreeView::keyPressEvent(event); - } -}; + void addBookmark(); + void removeBookmark(); +// void manageBookmarks(); + void refeshBookmarkMenu(); + void renameBookmark(const QModelIndex &index); -class BookmarkWidget : public QWidget -{ - Q_OBJECT + void importBookmarks(); + void exportBookmarks(); -public: - BookmarkWidget(BookmarkManager *manager, QWidget *parent = 0, - bool showButtons = true); - ~BookmarkWidget(); + void setSourceFromAction(QAction *action); + void setSourceFromIndex(const QModelIndex &index, bool newTab = false); -signals: - void addBookmark(); - void requestShowLink(const QUrl &url); - void escapePressed(); - -private slots: - void removeClicked(); - void filterChanged(); - void expand(const QModelIndex& index); - void activated(const QModelIndex &index); + void focusInEvent(); + void textChanged(const QString &text); void customContextMenuRequested(const QPoint &point); private: - void setup(bool showButtons); - void expandItems(); - void focusInEvent(QFocusEvent *e); - bool eventFilter(QObject *object, QEvent *event); + bool typeAndSearch; -private: - QRegExp regExp; - TreeView *treeView; - QLineEdit *searchField; - QToolButton *addButton; - QToolButton *removeButton; - BookmarkManager *bookmarkManager; - QSortFilterProxyModel* filterBookmarkModel; -}; + static QMutex mutex; + static BookmarkManager *bookmarkManager; -class BookmarkModel : public QStandardItemModel -{ - Q_OBJECT + QMenu *bookmarkMenu; -public: - BookmarkModel(int rows, int columns, QObject *parent = 0); - ~BookmarkModel(); + BookmarkModel *bookmarkModel; + BookmarkFilterModel *bookmarkFilterModel; + QSortFilterProxyModel *typeAndSearchModel; - Qt::DropActions supportedDropActions() const; - Qt::ItemFlags flags(const QModelIndex &index) const; + BookmarkWidget *bookmarkWidget; + BookmarkTreeView *bookmarkTreeView; }; -class BookmarkManager : public QObject +class BookmarkManager::BookmarkWidget : public QWidget { Q_OBJECT - public: - BookmarkManager(); - ~BookmarkManager(); + BookmarkWidget(QWidget *parent = 0) + : QWidget(parent) { ui.setupUi(this); } + virtual ~BookmarkWidget() {} - BookmarkModel* treeBookmarkModel(); - BookmarkModel* listBookmarkModel(); - - void saveBookmarks(); - QStringList bookmarkFolders() const; - QModelIndex addNewFolder(const QModelIndex& index); - void removeBookmarkItem(QTreeView *treeView, const QModelIndex& index); - void showBookmarkDialog(QWidget* parent, const QString &name, - const QString &url); - void addNewBookmark(const QModelIndex& index, const QString &name, - const QString &url); - void setupBookmarkModels(); - - void fillBookmarkMenu(QMenu *menu); - QUrl urlForAction(QAction* action) const; + Ui::BookmarkWidget ui; signals: - void bookmarksChanged(); - -private slots: - void itemChanged(QStandardItem *item); + void focusInEvent(); private: - QString uniqueFolderName() const; - void removeBookmarkFolderItems(QStandardItem *item); - void readBookmarksRecursive(const QStandardItem *item, QDataStream &stream, - const qint32 depth) const; - void fillBookmarkMenu(QMenu *menu, QStandardItem *root); + void focusInEvent(QFocusEvent *event); +}; -private: - QString oldText; - QIcon folderIcon; - QIcon bookmarkIcon; - - BookmarkModel *treeModel; - BookmarkModel *listModel; - QStandardItem *renameItem; - QMap<QAction*, QModelIndex> map; +class BookmarkManager::BookmarkTreeView : public QTreeView +{ + Q_OBJECT +public: + BookmarkTreeView(QWidget *parent = 0); + ~BookmarkTreeView() {} + + void subclassKeyPressEvent(QKeyEvent *event); + +private slots: + void setExpandedData(const QModelIndex &index); }; QT_END_NAMESPACE -#endif +#endif // BOOKMARKMANAGER_H diff --git a/tools/assistant/tools/assistant/bookmarkmodel.cpp b/tools/assistant/tools/assistant/bookmarkmodel.cpp new file mode 100644 index 0000000..f265ca4 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkmodel.cpp @@ -0,0 +1,426 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "bookmarkmodel.h" +#include "bookmarkitem.h" + +#include <QtCore/QMimeData> +#include <QtCore/QStack> + +#include <QtGui/QApplication> +#include <QtGui/QStyle> +#include <QtGui/QTreeView> + +const QLatin1String MIMETYPE("application/bookmarks.assistant"); + +BookmarkModel::BookmarkModel() + : QAbstractItemModel() + , m_folder(false) + , m_editable(false) + , rootItem(0) +{ +} + +BookmarkModel::~BookmarkModel() +{ + delete rootItem; +} + +QByteArray +BookmarkModel::bookmarks() const +{ + QByteArray ba; + QDataStream stream(&ba, QIODevice::WriteOnly); + + const QModelIndex &root = index(0,0, QModelIndex()); + for (int i = 0; i < rowCount(root); ++i) + collectItems(index(i, 0, root), 0, &stream); + + return ba; +} + +void +BookmarkModel::setBookmarks(const QByteArray &bookmarks) +{ + folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon); + bookmarkIcon = QIcon(QLatin1String(":/trolltech/assistant/images/bookmark.png")); + + rootItem = new BookmarkItem(DataVector() << tr("Name") << tr("Address") + << true); + BookmarkItem* item = new BookmarkItem(DataVector() << tr("Bookmarks Menu") + << QLatin1String("Folder") << true); + rootItem->addChild(item); + + QStack<BookmarkItem*> parents; + parents.push(item); + + qint32 depth; + bool expanded; + QString name, url; + QDataStream stream(bookmarks); + while (!stream.atEnd()) { + stream >> depth >> name >> url >> expanded; + + while ((parents.count() - 1) != depth) + parents.pop(); + + item = new BookmarkItem(DataVector() << name << url << expanded); + if (url == QLatin1String("Folder")) { + parents.top()->addChild(item); + parents.push(item); + } else { + parents.top()->addChild(item); + } + } + + cache.clear(); + const QModelIndex &root = index(0,0, QModelIndex()); + + setupCache(root); + cache.insert(static_cast<BookmarkItem*> (root.internalPointer()), root); +} + +void +BookmarkModel::setItemsEditable(bool editable) +{ + m_editable = editable; +} + +void +BookmarkModel::expandFoldersIfNeeeded(QTreeView *treeView) +{ + foreach (const QModelIndex &index, cache) + treeView->setExpanded(index, index.data(UserRoleExpanded).toBool()); +} + +QModelIndex +BookmarkModel::addItem(const QModelIndex &parent, bool isFolder) +{ + m_folder = isFolder; + QModelIndex next; + if (insertRow(rowCount(parent), parent)) + next = index(rowCount(parent) - 1, 0, parent); + m_folder = false; + + return next; +} + +bool +BookmarkModel::removeItem(const QModelIndex &index) +{ + if (!index.isValid()) + return false; + + QModelIndexList indexes; + if (index.rowCount() > 0) + indexes = collectItems(index); + indexes.append(index); + + foreach (const QModelIndex &itemToRemove, indexes) { + if (!removeRow(itemToRemove.row(), itemToRemove.parent())) + return false; + cache.remove(itemFromIndex(itemToRemove)); + } + return true; +} + +int +BookmarkModel::rowCount(const QModelIndex &index) const +{ + if (BookmarkItem *item = itemFromIndex(index)) + return item->childCount(); + return 0; +} + +int +BookmarkModel::columnCount(const QModelIndex &/*index*/) const +{ + return 2; +} + +QModelIndex +BookmarkModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) + return QModelIndex(); + + if (BookmarkItem *childItem = itemFromIndex(index)) { + if (BookmarkItem *parent = childItem->parent()) { + if (parent != rootItem) + return createIndex(parent->childNumber(), 0, parent); + } + } + return QModelIndex(); +} + +QModelIndex +BookmarkModel::index(int row, int column, const QModelIndex &index) const +{ + if (index.isValid() && (index.column() != 0 && index.column() != 1)) + return QModelIndex(); + + if (BookmarkItem *parent = itemFromIndex(index)) { + if (BookmarkItem *childItem = parent->child(row)) + return createIndex(row, column, childItem); + } + return QModelIndex(); +} + +Qt::DropActions +BookmarkModel::supportedDropActions () const +{ + return /* Qt::CopyAction | */Qt::MoveAction; +} + +Qt::ItemFlags +BookmarkModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::NoItemFlags; + + Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; + + if (m_editable) + defaultFlags |= Qt::ItemIsEditable; + + if (BookmarkItem *item = itemFromIndex(index)) { + if (index.data(UserRoleFolder).toBool()) { + if (index.column() > 0) + defaultFlags &= ~Qt::ItemIsEditable; + return defaultFlags | Qt::ItemIsDropEnabled; + } + } + return defaultFlags | Qt::ItemIsDragEnabled; +} + +QVariant +BookmarkModel::data(const QModelIndex &index, int role) const +{ + if (index.isValid()) { + if (BookmarkItem *item = itemFromIndex(index)) { + switch (role) { + case Qt::EditRole: { + case Qt::DisplayRole: + if (index.data(UserRoleFolder).toBool() && index.column() == 1) + return QLatin1String(""); + return item->data(index.column()); + } break; + case Qt::DecorationRole: { + if (index.column() == 0) + return index.data(UserRoleFolder).toBool() + ? folderIcon : bookmarkIcon; + } break; + default:; + return item->data(role); + } + } + } + return QVariant(); +} + +bool +BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + bool result = false; + if (role != Qt::EditRole && role != UserRoleExpanded) + return result; + + if (BookmarkItem *item = itemFromIndex(index)) { + if (role == Qt::EditRole) { + const bool isFolder = index.data(UserRoleFolder).toBool(); + if (!isFolder || (isFolder && index.column() == 0)) + result = item->setData(index.column(), value); + } else if (role == UserRoleExpanded) { + result = item->setData(UserRoleExpanded, value); + } + } + + if (result) + emit dataChanged(index, index); + return result; +} + +QVariant +BookmarkModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (rootItem && orientation == Qt::Horizontal && role == Qt::DisplayRole) + return rootItem->data(section); + return QVariant(); +} + +QModelIndex +BookmarkModel::indexFromItem(BookmarkItem *item) const +{ + return cache.value(item, QModelIndex()); +} + +BookmarkItem* +BookmarkModel::itemFromIndex(const QModelIndex &index) const +{ + if (index.isValid()) + return static_cast<BookmarkItem*>(index.internalPointer()); + return rootItem; +} + +bool +BookmarkModel::insertRows(int position, int rows, const QModelIndex &parent) +{ + if (!parent.data(UserRoleFolder).toBool()) + return false; + + bool success = false; + if (BookmarkItem *parentItem = itemFromIndex(parent)) { + beginInsertRows(parent, position, position + rows - 1); + success = parentItem->insertChildren(m_folder, position, rows); + if (success) { + const QModelIndex ¤t = index(position, 0, parent); + cache.insert(itemFromIndex(current), current); + } + endInsertRows(); + } + return success; +} + +bool +BookmarkModel::removeRows(int position, int rows, const QModelIndex &index) +{ + bool success = false; + if (BookmarkItem *parent = itemFromIndex(index)) { + beginRemoveRows(index, position, position + rows - 1); + success = parent->removeChildren(position, rows); + endRemoveRows(); + } + return success; +} + +QStringList +BookmarkModel::mimeTypes() const +{ + return QStringList() << MIMETYPE; +} + +QMimeData* +BookmarkModel::mimeData(const QModelIndexList &indexes) const +{ + if (indexes.isEmpty()) + return 0; + + QByteArray data; + QDataStream stream(&data, QIODevice::WriteOnly); + + foreach (const QModelIndex &index, indexes) { + if (index.column() == 0) + collectItems(index, 0, &stream); + } + + QMimeData *mimeData = new QMimeData(); + mimeData->setData(MIMETYPE, data); + return mimeData; +} + +bool +BookmarkModel::dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) +{ + if (action == Qt::IgnoreAction) + return true; + + if (!data->hasFormat(MIMETYPE) || column > 0) + return false; + + QByteArray ba = data->data(MIMETYPE); + QDataStream stream(&ba, QIODevice::ReadOnly); + while (stream.atEnd()) + return false; + + qint32 depth; + bool expanded; + QString name, url; + while (!stream.atEnd()) { + stream >> depth >> name >> url >> expanded; + if (insertRow(qMax(0, row), parent)) { + const QModelIndex ¤t = index(qMax(0, row), 0, parent); + if (current.isValid()) { + BookmarkItem* item = itemFromIndex(current); + item->setData(DataVector() << name << url << expanded); + } + } + } + return true; +} + +void +BookmarkModel::setupCache(const QModelIndex &parent) +{ + const QModelIndexList &list = collectItems(parent); + foreach (const QModelIndex &index, list) + cache.insert(itemFromIndex(index), index); +} + +QModelIndexList +BookmarkModel::collectItems(const QModelIndex &parent) const +{ + QModelIndexList list; + for (int i = rowCount(parent) - 1; i >= 0 ; --i) { + const QModelIndex &next = index(i, 0, parent); + if (data(next, UserRoleFolder).toBool()) + list += collectItems(next); + list.append(next); + } + return list; +} + +void +BookmarkModel::collectItems(const QModelIndex &parent, qint32 depth, + QDataStream *stream) const +{ + if (parent.isValid()) { + *stream << depth; + *stream << parent.data().toString(); + *stream << parent.data(UserRoleUrl).toString(); + *stream << parent.data(UserRoleExpanded).toBool(); + + for (int i = 0; i < rowCount(parent); ++i) { + if (parent.data(UserRoleFolder).toBool()) + collectItems(index(i, 0 , parent), depth + 1, stream); + } + } +} diff --git a/tools/assistant/tools/assistant/bookmarkmodel.h b/tools/assistant/tools/assistant/bookmarkmodel.h new file mode 100644 index 0000000..6b2a0b8 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkmodel.h @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef BOOKMARKMODEL_H +#define BOOKMARKMODEL_H + +#include <QtCore/QAbstractItemModel> + +#include <QtGui/QIcon> + +QT_BEGIN_NAMESPACE + +class BookmarkItem; +class QMimeData; +class QTreeView; + +typedef QMap<BookmarkItem*, QPersistentModelIndex> ItemModelIndexCache; + +class BookmarkModel : public QAbstractItemModel +{ + Q_OBJECT +public: + BookmarkModel(); + ~BookmarkModel(); + + QByteArray bookmarks() const; + void setBookmarks(const QByteArray &bookmarks); + + void setItemsEditable(bool editable); + void expandFoldersIfNeeeded(QTreeView *treeView); + + QModelIndex addItem(const QModelIndex &parent, bool isFolder = false); + bool removeItem(const QModelIndex &index); + + int rowCount(const QModelIndex &index = QModelIndex()) const; + int columnCount(const QModelIndex &index = QModelIndex()) const; + + QModelIndex parent(const QModelIndex &index) const; + QModelIndex index(int row, int column, const QModelIndex &index) const; + + Qt::DropActions supportedDropActions () const; + Qt::ItemFlags flags(const QModelIndex &index) const; + + QVariant data(const QModelIndex &index, int role) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + + QModelIndex indexFromItem(BookmarkItem *item) const; + BookmarkItem *itemFromIndex(const QModelIndex &index) const; + + bool insertRows(int position, int rows, const QModelIndex &parent); + bool removeRows(int position, int rows, const QModelIndex &parent); + + QStringList mimeTypes() const; + QMimeData* mimeData(const QModelIndexList &indexes) const; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, + int column, const QModelIndex &parent); + +private: + void setupCache(const QModelIndex &parent); + QModelIndexList collectItems(const QModelIndex &parent) const; + void collectItems(const QModelIndex &parent, qint32 depth, + QDataStream *stream) const; + +private: + int columns; + bool m_folder; + bool m_editable; + QIcon folderIcon; + QIcon bookmarkIcon; + QTreeView *treeView; + BookmarkItem *rootItem; + ItemModelIndexCache cache; +}; + +QT_END_NAMESPACE + +#endif // BOOKMARKMODEL_H diff --git a/tools/assistant/tools/assistant/bookmarkwidget.ui b/tools/assistant/tools/assistant/bookmarkwidget.ui new file mode 100644 index 0000000..3015740 --- /dev/null +++ b/tools/assistant/tools/assistant/bookmarkwidget.ui @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>BookmarkWidget</class> + <widget class="QWidget" name="BookmarkWidget"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>235</width> + <height>606</height> + </rect> + </property> + <property name="windowTitle"> + <string>Bookmarks</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Filter:</string> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <widget class="QLineEdit" name="lineEdit"/> + </item> + <item> + <widget class="QStackedWidget" name="stackedWidget"/> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="add"> + <property name="text"> + <string>Add</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="remove"> + <property name="text"> + <string>Remove</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/tools/assistant/tools/assistant/centralwidget.cpp b/tools/assistant/tools/assistant/centralwidget.cpp index 076498e..77b9678 100644 --- a/tools/assistant/tools/assistant/centralwidget.cpp +++ b/tools/assistant/tools/assistant/centralwidget.cpp @@ -842,7 +842,7 @@ void CentralWidget::showTabBarContextMenu(const QPoint &point) } if (pickedAction == newBookmark) - emit addNewBookmark(viewer->documentTitle(), viewer->source().toString()); + emit addBookmark(viewer->documentTitle(), viewer->source().toString()); } bool CentralWidget::eventFilter(QObject *object, QEvent *e) diff --git a/tools/assistant/tools/assistant/centralwidget.h b/tools/assistant/tools/assistant/centralwidget.h index c0bee81..a7c0d6f 100644 --- a/tools/assistant/tools/assistant/centralwidget.h +++ b/tools/assistant/tools/assistant/centralwidget.h @@ -170,7 +170,7 @@ signals: void highlighted(const QString &link); void forwardAvailable(bool available); void backwardAvailable(bool available); - void addNewBookmark(const QString &title, const QString &url); + void addBookmark(const QString &title, const QString &url); protected: void keyPressEvent(QKeyEvent *); diff --git a/tools/assistant/tools/assistant/mainwindow.cpp b/tools/assistant/tools/assistant/mainwindow.cpp index cfd0fee..2ff6b5e 100644 --- a/tools/assistant/tools/assistant/mainwindow.cpp +++ b/tools/assistant/tools/assistant/mainwindow.cpp @@ -41,20 +41,20 @@ #include "tracer.h" #include "mainwindow.h" + +#include "bookmarkmanager.h" #include "centralwidget.h" #include "helpviewer.h" #include "indexwindow.h" #include "topicchooser.h" #include "contentwindow.h" #include "preferencesdialog.h" -#include "bookmarkmanager.h" #include "helpenginewrapper.h" #include "remotecontrol.h" #include "cmdlineparser.h" #include "aboutdialog.h" #include "searchwidget.h" #include "qtdocinstaller.h" -#include "xbelsupport.h" // #define TRACING_REQUESTED @@ -95,6 +95,7 @@ QT_BEGIN_NAMESPACE MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) : QMainWindow(parent) + , m_bookmarkWidget(0) , m_filterCombo(0) , m_toolBarMenu(0) , m_cmdLine(cmdLine) @@ -131,10 +132,22 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) contentDock->setWidget(m_contentWindow); addDockWidget(Qt::LeftDockWidgetArea, contentDock); - QDockWidget *bookmarkDock = new QDockWidget(tr("Bookmarks"), this); - bookmarkDock->setObjectName(QLatin1String("BookmarkWindow")); - bookmarkDock->setWidget(setupBookmarkWidget()); - addDockWidget(Qt::LeftDockWidgetArea, bookmarkDock); + QDockWidget *bookmarkDock = 0; + if (BookmarkManager *manager = BookmarkManager::instance()) { + bookmarkDock = new QDockWidget(tr("Bookmarks"), this); + bookmarkDock->setObjectName(QLatin1String("BookmarkWindow")); + bookmarkDock->setWidget(m_bookmarkWidget = manager->bookmarkDockWidget()); + addDockWidget(Qt::LeftDockWidgetArea, bookmarkDock); + + connect(manager, SIGNAL(escapePressed()), this, + SLOT(activateCurrentCentralWidgetTab())); + connect(manager, SIGNAL(setSource(QUrl)), m_centralWidget, + SLOT(setSource(QUrl))); + connect(manager, SIGNAL(setSourceInNewTab(QUrl)), m_centralWidget, + SLOT(setSourceInNewTab(QUrl))); + connect(m_centralWidget, SIGNAL(addBookmark(QString, QString)), manager, + SLOT(addBookmark(QString, QString))); + } QHelpSearchEngine *searchEngine = helpEngineWrapper.searchEngine(); connect(searchEngine, SIGNAL(indexingStarted()), this, SLOT(indexingStarted())); @@ -153,14 +166,6 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) setupFilterToolbar(); setupAddressToolbar(); - m_bookmarkManager->setupBookmarkModels(); - m_bookmarkMenu->addSeparator(); - m_bookmarkManager->fillBookmarkMenu(m_bookmarkMenu); - connect(m_bookmarkMenu, SIGNAL(triggered(QAction*)), this, - SLOT(showBookmark(QAction*))); - connect(m_bookmarkManager, SIGNAL(bookmarksChanged()), this, - SLOT(updateBookmarkMenu())); - const QString windowTitle = helpEngineWrapper.windowTitle(); setWindowTitle(windowTitle.isEmpty() ? defWindowTitle : windowTitle); QByteArray iconArray = helpEngineWrapper.applicationIcon(); @@ -186,7 +191,8 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) restoreGeometry(ba); } else { tabifyDockWidget(contentDock, indexDock); - tabifyDockWidget(indexDock, bookmarkDock); + if (bookmarkDock) + tabifyDockWidget(indexDock, bookmarkDock); contentDock->raise(); resize(QSize(800, 600)); } @@ -219,9 +225,9 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) hideIndex(); if (m_cmdLine->bookmarks() == CmdLineParser::Show) - showBookmarks(); + showBookmarksDockWidget(); else if (m_cmdLine->bookmarks() == CmdLineParser::Hide) - hideBookmarks(); + hideBookmarksDockWidget(); if (m_cmdLine->search() == CmdLineParser::Show) showSearch(); @@ -233,7 +239,7 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) else if (m_cmdLine->index() == CmdLineParser::Activate) showIndex(); else if (m_cmdLine->bookmarks() == CmdLineParser::Activate) - showBookmarks(); + showBookmarksDockWidget(); if (!m_cmdLine->currentFilter().isEmpty()) { const QString &curFilter = m_cmdLine->currentFilter(); @@ -270,7 +276,7 @@ bool MainWindow::usesDefaultCollection() const void MainWindow::closeEvent(QCloseEvent *e) { TRACE_OBJ - m_bookmarkManager->saveBookmarks(); + BookmarkManager::destroy(); HelpEngineWrapper::instance().setMainWindow(saveState()); HelpEngineWrapper::instance().setMainWindowGeometry(saveGeometry()); QMainWindow::closeEvent(e); @@ -380,35 +386,6 @@ void MainWindow::checkInitState() } } -void MainWindow::updateBookmarkMenu() -{ - TRACE_OBJ - if (m_bookmarkManager) { - m_bookmarkMenu->removeAction(m_importBookmarkAction); - m_bookmarkMenu->removeAction(m_exportBookmarkAction); - m_bookmarkMenu->removeAction(m_bookmarkMenuAction); - - m_bookmarkMenu->clear(); - - m_bookmarkMenu->addAction(m_importBookmarkAction); - m_bookmarkMenu->addAction(m_exportBookmarkAction); - m_bookmarkMenu->addAction(m_bookmarkMenuAction); - m_bookmarkMenu->addSeparator(); - - m_bookmarkManager->fillBookmarkMenu(m_bookmarkMenu); - } -} - -void MainWindow::showBookmark(QAction *action) -{ - TRACE_OBJ - if (m_bookmarkManager) { - const QUrl &url = m_bookmarkManager->urlForAction(action); - if (url.isValid()) - m_centralWidget->setSource(url); - } -} - void MainWindow::insertLastPages() { TRACE_OBJ @@ -513,7 +490,7 @@ void MainWindow::setupActions() QKeySequence(tr("ALT+C"))); m_viewMenu->addAction(tr("Index"), this, SLOT(showIndex()), QKeySequence(tr("ALT+I"))); - m_viewMenu->addAction(tr("Bookmarks"), this, SLOT(showBookmarks()), + m_viewMenu->addAction(tr("Bookmarks"), this, SLOT(showBookmarksDockWidget()), QKeySequence(tr("ALT+O"))); m_viewMenu->addAction(tr("Search"), this, SLOT(showSearchWidget()), QKeySequence(tr("ALT+S"))); @@ -549,14 +526,8 @@ void MainWindow::setupActions() tmp->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("Ctrl+Alt+Left")) << QKeySequence(Qt::CTRL + Qt::Key_PageUp)); - m_bookmarkMenu = menuBar()->addMenu(tr("&Bookmarks")); - m_importBookmarkAction = m_bookmarkMenu->addAction(tr("Import..."), - this, SLOT(importBookmarks())); - m_exportBookmarkAction = m_bookmarkMenu->addAction(tr("Export..."), - this, SLOT(exportBookmarks())); - m_bookmarkMenuAction = m_bookmarkMenu->addAction(tr("Add Bookmark..."), - this, SLOT(addBookmark())); - m_bookmarkMenuAction->setShortcut(tr("CTRL+D")); + if (BookmarkManager *manager = BookmarkManager::instance()) + manager->takeBookmarksMenu(menuBar()->addMenu(tr("&Bookmarks"))); menu = menuBar()->addMenu(tr("&Help")); m_aboutAction = menu->addAction(tr("About..."), this, SLOT(showAboutDialog())); @@ -617,14 +588,6 @@ void MainWindow::setupActions() SLOT(updateNavigationItems())); connect(m_centralWidget, SIGNAL(highlighted(QString)), statusBar(), SLOT(showMessage(QString))); - connect(m_centralWidget, SIGNAL(addNewBookmark(QString,QString)), this, - SLOT(addNewBookmark(QString,QString))); - - // bookmarks - connect(m_bookmarkWidget, SIGNAL(requestShowLink(QUrl)), m_centralWidget, - SLOT(setSource(QUrl))); - connect(m_bookmarkWidget, SIGNAL(escapePressed()), this, - SLOT(activateCurrentCentralWidgetTab())); // index window connect(m_indexWindow, SIGNAL(linkActivated(QUrl)), m_centralWidget, @@ -757,13 +720,6 @@ void MainWindow::showNewAddress(const QUrl &url) m_addressLineEdit->setText(url.toString()); } -void MainWindow::addBookmark() -{ - TRACE_OBJ - addNewBookmark(m_centralWidget->currentTitle(), - m_centralWidget->currentSource().toString()); -} - void MainWindow::gotoAddress() { TRACE_OBJ @@ -831,15 +787,6 @@ void MainWindow::copyAvailable(bool yes) m_copyAction->setEnabled(yes); } -void MainWindow::addNewBookmark(const QString &title, const QString &url) -{ - TRACE_OBJ - if (url.isEmpty() || url == QLatin1String("about:blank")) - return; - - m_bookmarkManager->showBookmarkDialog(this, title, url); -} - void MainWindow::showAboutDialog() { TRACE_OBJ @@ -940,22 +887,23 @@ void MainWindow::setBookmarksVisible(bool visible) { TRACE_OBJ if (visible) - showBookmarks(); + showBookmarksDockWidget(); else - hideBookmarks(); + hideBookmarksDockWidget(); } - -void MainWindow::showBookmarks() +void MainWindow::showBookmarksDockWidget() { TRACE_OBJ - activateDockWidget(m_bookmarkWidget); + if (m_bookmarkWidget) + activateDockWidget(m_bookmarkWidget); } -void MainWindow::hideBookmarks() +void MainWindow::hideBookmarksDockWidget() { TRACE_OBJ - m_bookmarkWidget->parentWidget()->hide(); + if (m_bookmarkWidget) + m_bookmarkWidget->parentWidget()->hide(); } void MainWindow::setSearchVisible(bool visible) @@ -1087,15 +1035,6 @@ void MainWindow::indexingFinished() m_progressWidget = 0; } -QWidget* MainWindow::setupBookmarkWidget() -{ - TRACE_OBJ - m_bookmarkManager = new BookmarkManager; - m_bookmarkWidget = new BookmarkWidget(m_bookmarkManager, this); - connect(m_bookmarkWidget, SIGNAL(addBookmark()), this, SLOT(addBookmark())); - return m_bookmarkWidget; -} - QString MainWindow::collectionFileDirectory(bool createDir, const QString &cacheDir) { TRACE_OBJ @@ -1131,43 +1070,6 @@ QString MainWindow::defaultHelpCollectionFileName() arg(QLatin1String(QT_VERSION_STR)); } -void MainWindow::importBookmarks() -{ - TRACE_OBJ - const QString &fileName = QFileDialog::getOpenFileName(0, tr("Open File"), - QDir::currentPath(), tr("Files (*.xbel)")); - - if (fileName.isEmpty()) - return; - - QFile file(fileName); - if (file.open(QIODevice::ReadOnly)) { - XbelReader reader(m_bookmarkManager->treeBookmarkModel(), - m_bookmarkManager->listBookmarkModel()); - reader.readFromFile(&file); - } -} - -void MainWindow::exportBookmarks() -{ - TRACE_OBJ - QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"), - "untitled.xbel", tr("Files (*.xbel)")); - - QLatin1String suffix(".xbel"); - if (!fileName.endsWith(suffix)) - fileName.append(suffix); - - QFile file(fileName); - if (file.open(QIODevice::WriteOnly)) { - XbelWriter writer(m_bookmarkManager->treeBookmarkModel()); - writer.writeToFile(&file); - } else { - QMessageBox::information(this, tr("Qt Assistant"), - tr("Unable to save bookmarks."), tr("OK")); - } -} - void MainWindow::currentFilterChanged(const QString &filter) { TRACE_OBJ diff --git a/tools/assistant/tools/assistant/mainwindow.h b/tools/assistant/tools/assistant/mainwindow.h index 3c70666..40ca624 100644 --- a/tools/assistant/tools/assistant/mainwindow.h +++ b/tools/assistant/tools/assistant/mainwindow.h @@ -58,8 +58,6 @@ class QHelpEngineCore; class QHelpEngine; class CentralWidget; class ContentWindow; -class BookmarkManager; -class BookmarkWidget; class CmdLineParser; class QtDocInstaller; @@ -97,10 +95,8 @@ public slots: private slots: void showContents(); void showIndex(); - void showBookmarks(); void showSearch(); void insertLastPages(); - void addBookmark(); void gotoAddress(); void showPreferences(); void showNewAddress(); @@ -109,7 +105,6 @@ private slots: void updateNavigationItems(); void updateTabCloseAction(); void showNewAddress(const QUrl &url); - void addNewBookmark(const QString &title, const QString &url); void showTopicChooser(const QMap<QString, QUrl> &links, const QString &keyword); void updateApplicationFont(); void filterDocumentation(const QString &customFilter); @@ -119,18 +114,12 @@ private slots: void indexingFinished(); void qtDocumentationInstalled(bool newDocsInstalled); void registerDocumentation(const QString &component, - const QString &absFileName); + const QString &absFileName); void resetQtDocInfo(const QString &component); void checkInitState(); void documentationRemoved(const QString &namespaceName); void documentationUpdated(const QString &namespaceName); - void updateBookmarkMenu(); - void showBookmark(QAction *action); - - void importBookmarks(); - void exportBookmarks(); - private: bool initHelpDB(); void setupActions(); @@ -140,17 +129,21 @@ private: void setupFilterToolbar(); void setupAddressToolbar(); QMenu *toolBarMenu(); - QWidget *setupBookmarkWidget(); void hideContents(); void hideIndex(); - void hideBookmarks(); void hideSearch(); +private slots: + void showBookmarksDockWidget(); + void hideBookmarksDockWidget(); + +private: + QWidget *m_bookmarkWidget; + +private: CentralWidget *m_centralWidget; IndexWindow *m_indexWindow; ContentWindow *m_contentWindow; - BookmarkWidget *m_bookmarkWidget; - BookmarkManager *m_bookmarkManager; QLineEdit *m_addressLineEdit; QComboBox *m_filterCombo; @@ -172,10 +165,6 @@ private: QMenu *m_viewMenu; QMenu *m_toolBarMenu; - QMenu *m_bookmarkMenu; - QAction *m_bookmarkMenuAction; - QAction *m_importBookmarkAction; - QAction *m_exportBookmarkAction; CmdLineParser *m_cmdLine; diff --git a/tools/assistant/tools/assistant/xbelsupport.cpp b/tools/assistant/tools/assistant/xbelsupport.cpp index f2c0bcb..7d5a08b 100644 --- a/tools/assistant/tools/assistant/xbelsupport.cpp +++ b/tools/assistant/tools/assistant/xbelsupport.cpp @@ -41,9 +41,12 @@ #include "tracer.h" #include "xbelsupport.h" -#include "bookmarkmanager.h" -#include <QtCore/QCoreApplication> +#include "bookmarkitem.h" +#include "bookmarkmodel.h" + +#include <QtCore/QDate> +#include <QtCore/QModelIndex> QT_BEGIN_NAMESPACE @@ -55,7 +58,7 @@ struct Bookmark { XbelWriter::XbelWriter(BookmarkModel *model) : QXmlStreamWriter() - , treeModel(model) + , bookmarkModel(model) { TRACE_OBJ setAutoFormatting(true); @@ -71,53 +74,46 @@ void XbelWriter::writeToFile(QIODevice *device) writeStartElement(QLatin1String("xbel")); writeAttribute(QLatin1String("version"), QLatin1String("1.0")); - QStandardItem *root = treeModel->invisibleRootItem(); - for (int i = 0; i < root->rowCount(); ++i) - writeData(root->child(i)); - + const QModelIndex &root = bookmarkModel->index(0,0, QModelIndex()); + for (int i = 0; i < bookmarkModel->rowCount(root); ++i) + writeData(bookmarkModel->index(i, 0, root)); writeEndDocument(); } -void XbelWriter::writeData(QStandardItem *child) +void XbelWriter::writeData(const QModelIndex &index) { TRACE_OBJ - Bookmark entry; - entry.title = child->data(Qt::DisplayRole).toString(); - entry.url = child->data(Qt::UserRole + 10).toString(); - - if (entry.url == QLatin1String("Folder")) { - writeStartElement(QLatin1String("folder")); - - entry.folded = !child->data(Qt::UserRole + 11).toBool(); - writeAttribute(QLatin1String("folded"), - entry.folded ? QLatin1String("yes") : QLatin1String("no")); - - writeTextElement(QLatin1String("title"), entry.title); - - for (int i = 0; i < child->rowCount(); ++i) - writeData(child->child(i)); - - writeEndElement(); - } else { - writeStartElement(QLatin1String("bookmark")); - writeAttribute(QLatin1String("href"), entry.url); - writeTextElement(QLatin1String("title"), entry.title); - writeEndElement(); + if (index.isValid()) { + Bookmark entry; + entry.title = index.data().toString(); + entry.url = index.data(UserRoleUrl).toString(); + + if (index.data(UserRoleFolder).toBool()) { + writeStartElement(QLatin1String("folder")); + entry.folded = !index.data(UserRoleExpanded).toBool(); + writeAttribute(QLatin1String("folded"), entry.folded + ? QLatin1String("yes") : QLatin1String("no")); + writeTextElement(QLatin1String("title"), entry.title); + + for (int i = 0; i < bookmarkModel->rowCount(index); ++i) + writeData(bookmarkModel->index(i, 0 , index)); + writeEndElement(); + } else { + writeStartElement(QLatin1String("bookmark")); + writeAttribute(QLatin1String("href"), entry.url); + writeTextElement(QLatin1String("title"), entry.title); + writeEndElement(); + } } } +// -- XbelReader -// XbelReader - - -XbelReader::XbelReader(BookmarkModel *tree, BookmarkModel *list) +XbelReader::XbelReader(BookmarkModel *model) : QXmlStreamReader() - , treeModel(tree) - , listModel(list) + , bookmarkModel(model) { TRACE_OBJ - folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon); - bookmarkIcon = QIcon(QLatin1String(":/trolltech/assistant/images/bookmark.png")); } bool XbelReader::readFromFile(QIODevice *device) @@ -132,7 +128,11 @@ bool XbelReader::readFromFile(QIODevice *device) if (name() == QLatin1String("xbel") && attributes().value(QLatin1String("version")) == QLatin1String("1.0")) { + const QModelIndex &root = bookmarkModel->index(0,0, QModelIndex()); + parents.append(bookmarkModel->addItem(root, true)); readXBEL(); + bookmarkModel->setData(parents.first(), + QDate::currentDate().toString(Qt::ISODate), Qt::EditRole); } else { raiseError(QLatin1String("The file is not an XBEL version 1.0 file.")); } @@ -153,39 +153,22 @@ void XbelReader::readXBEL() if (isStartElement()) { if (name() == QLatin1String("folder")) - readFolder(0); + readFolder(); else if (name() == QLatin1String("bookmark")) - readBookmark(0); + readBookmark(); else readUnknownElement(); } } } -void XbelReader::readUnknownElement() +void XbelReader::readFolder() { TRACE_OBJ - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) - readUnknownElement(); - } -} - -void XbelReader::readFolder(QStandardItem *item) -{ - TRACE_OBJ - QStandardItem *folder = createChildItem(item); - folder->setIcon(folderIcon); - folder->setData(QLatin1String("Folder"), Qt::UserRole + 10); - - bool expanded = - (attributes().value(QLatin1String("folded")) != QLatin1String("no")); - folder->setData(expanded, Qt::UserRole + 11); + parents.append(bookmarkModel->addItem(parents.last(), true)); + bookmarkModel->setData(parents.last(), + attributes().value(QLatin1String("folded")) == QLatin1String("no"), + UserRoleExpanded); while (!atEnd()) { readNext(); @@ -194,26 +177,29 @@ void XbelReader::readFolder(QStandardItem *item) break; if (isStartElement()) { - if (name() == QLatin1String("title")) - folder->setText(readElementText()); - else if (name() == QLatin1String("folder")) - readFolder(folder); + if (name() == QLatin1String("title")) { + bookmarkModel->setData(parents.last(), readElementText(), + Qt::EditRole); + } else if (name() == QLatin1String("folder")) + readFolder(); else if (name() == QLatin1String("bookmark")) - readBookmark(folder); + readBookmark(); else readUnknownElement(); } } + + parents.removeLast(); } -void XbelReader::readBookmark(QStandardItem *item) +void XbelReader::readBookmark() { TRACE_OBJ - QStandardItem *bookmark = createChildItem(item); - bookmark->setIcon(bookmarkIcon); - bookmark->setText(QCoreApplication::tr("Unknown title")); - bookmark->setData(attributes().value(QLatin1String("href")).toString(), - Qt::UserRole + 10); + const QModelIndex &index = bookmarkModel->addItem(parents.last(), false); + if (BookmarkItem* item = bookmarkModel->itemFromIndex(index)) { + item->setData(UserRoleUrl, attributes().value(QLatin1String("href")) + .toString()); + } while (!atEnd()) { readNext(); @@ -223,27 +209,25 @@ void XbelReader::readBookmark(QStandardItem *item) if (isStartElement()) { if (name() == QLatin1String("title")) - bookmark->setText(readElementText()); + bookmarkModel->setData(index, readElementText(), Qt::EditRole); else readUnknownElement(); } } - - listModel->appendRow(bookmark->clone()); } -QStandardItem *XbelReader::createChildItem(QStandardItem *item) +void XbelReader::readUnknownElement() { TRACE_OBJ - QStandardItem *childItem = new QStandardItem(); - childItem->setEditable(false); + while (!atEnd()) { + readNext(); - if (item) - item->appendRow(childItem); - else - treeModel->appendRow(childItem); + if (isEndElement()) + break; - return childItem; + if (isStartElement()) + readUnknownElement(); + } } QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/xbelsupport.h b/tools/assistant/tools/assistant/xbelsupport.h index 16f53f5..037415f 100644 --- a/tools/assistant/tools/assistant/xbelsupport.h +++ b/tools/assistant/tools/assistant/xbelsupport.h @@ -42,11 +42,11 @@ #ifndef XBELSUPPORT_H #define XBELSUPPORT_H -#include <QtGui/QIcon> #include <QtXml/QXmlStreamReader> +#include <QtCore/QPersistentModelIndex> QT_FORWARD_DECLARE_CLASS(QIODevice) -QT_FORWARD_DECLARE_CLASS(QStandardItem) +QT_FORWARD_DECLARE_CLASS(QModelIndex) QT_BEGIN_NAMESPACE @@ -59,31 +59,27 @@ public: void writeToFile(QIODevice *device); private: - void writeData(QStandardItem *item); + void writeData(const QModelIndex &index); private: - BookmarkModel *treeModel; + BookmarkModel *bookmarkModel; }; class XbelReader : public QXmlStreamReader { public: - XbelReader(BookmarkModel *tree, BookmarkModel *list); + XbelReader(BookmarkModel *model); bool readFromFile(QIODevice *device); private: void readXBEL(); + void readFolder(); + void readBookmark(); void readUnknownElement(); - void readFolder(QStandardItem *item); - void readBookmark(QStandardItem *item); - QStandardItem* createChildItem(QStandardItem *item); private: - QIcon folderIcon; - QIcon bookmarkIcon; - - BookmarkModel *treeModel; - BookmarkModel *listModel; + BookmarkModel *bookmarkModel; + QList<QPersistentModelIndex> parents; }; QT_END_NAMESPACE |