Warning, file /utilities/keditbookmarks/src/bookmarklistview.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2000 David Faure <faure@kde.org>
0003    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0004    Copyright (C) 2005 Daniel Teske <teske@squorn.de>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License, or (at your option) version 3.
0010 
0011    This program is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU General Public License for more details.
0015 
0016    You should have received a copy of the GNU General Public License
0017    along with this program.  If not, see <http://www.gnu.org/licenses/>
0018 */
0019 
0020 #include "bookmarklistview.h"
0021 #include "globalbookmarkmanager.h"
0022 #include "kbookmarkmodel/commands.h"
0023 #include "kbookmarkmodel/model.h"
0024 #include "settings.h"
0025 #include "toplevel.h" // for KEBApp
0026 
0027 #include <QContextMenuEvent>
0028 #include <QHeaderView>
0029 #include <QItemSelection>
0030 #include <QMenu>
0031 
0032 #include "keditbookmarks_debug.h"
0033 
0034 BookmarkFolderView::BookmarkFolderView(BookmarkListView *view, QWidget *parent)
0035     : KBookmarkView(parent)
0036     , mview(view)
0037 {
0038     mmodel = new BookmarkFolderViewFilterModel(parent);
0039     mmodel->setSourceModel(view->model());
0040     setModel(mmodel);
0041     header()->setVisible(false);
0042     setRootIsDecorated(false);
0043     setDropIndicatorShown(true);
0044     setCurrentIndex(mmodel->index(0, 0, QModelIndex()));
0045 
0046     connect(mmodel, &BookmarkFolderViewFilterModel::modelReset, this, &BookmarkFolderView::slotReset);
0047 }
0048 
0049 BookmarkFolderView::~BookmarkFolderView()
0050 {
0051 }
0052 
0053 void BookmarkFolderView::selectionChanged(const QItemSelection &deselected, const QItemSelection &selected)
0054 {
0055     const QModelIndexList &list = selectionModel()->selectedIndexes();
0056     if (list.count())
0057         mview->setRootIndex(mmodel->mapToSource(list.at(0)));
0058     else
0059         mview->setRootIndex(QModelIndex());
0060     KBookmarkView::selectionChanged(deselected, selected);
0061 }
0062 
0063 void BookmarkFolderView::slotReset()
0064 {
0065     setCurrentIndex(mmodel->index(0, 0, QModelIndex()));
0066     loadFoldedState();
0067 }
0068 
0069 KBookmark BookmarkFolderView::bookmarkForIndex(const QModelIndex &idx) const
0070 {
0071     qCDebug(KEDITBOOKMARKS_LOG) << "BookmarkFolderView::bookmarkForIndex" << idx;
0072     const QModelIndex &index = mmodel->mapToSource(idx);
0073     return static_cast<KBookmarkModel *>(mmodel->sourceModel())->bookmarkForIndex(index);
0074 }
0075 
0076 /********/
0077 
0078 BookmarkListView::BookmarkListView(QWidget *parent)
0079     : KBookmarkView(parent)
0080 {
0081     setDragEnabled(true);
0082 }
0083 
0084 void BookmarkListView::setModel(QAbstractItemModel *model)
0085 {
0086     KBookmarkView::setModel(model);
0087 }
0088 
0089 KBookmarkModel *BookmarkListView::bookmarkModel() const
0090 {
0091     return dynamic_cast<KBookmarkModel *>(QTreeView::model());
0092 }
0093 
0094 KBookmark BookmarkListView::bookmarkForIndex(const QModelIndex &idx) const
0095 {
0096     return bookmarkModel()->bookmarkForIndex(idx);
0097 }
0098 
0099 BookmarkListView::~BookmarkListView()
0100 {
0101     saveColumnSetting();
0102 }
0103 
0104 void BookmarkListView::contextMenuEvent(QContextMenuEvent *e)
0105 {
0106     QModelIndex index = indexAt(e->pos());
0107     KBookmark bk;
0108     if (index.isValid())
0109         bk = bookmarkForIndex(index);
0110 
0111     QMenu *popup;
0112     if (!index.isValid() || (bk.address() == GlobalBookmarkManager::self()->root().address()) || (bk.isGroup())) // FIXME add empty folder padder
0113     {
0114         popup = KEBApp::self()->popupMenuFactory(QStringLiteral("popup_folder"));
0115     } else {
0116         popup = KEBApp::self()->popupMenuFactory(QStringLiteral("popup_bookmark"));
0117     }
0118     if (popup)
0119         popup->popup(e->globalPos());
0120 }
0121 
0122 void BookmarkListView::loadColumnSetting()
0123 {
0124     header()->resizeSection(KEBApp::NameColumn, KEBSettings::name());
0125     header()->resizeSection(KEBApp::UrlColumn, KEBSettings::uRL());
0126     header()->resizeSection(KEBApp::CommentColumn, KEBSettings::comment());
0127     header()->resizeSection(KEBApp::StatusColumn, KEBSettings::status());
0128 }
0129 
0130 void BookmarkListView::saveColumnSetting()
0131 {
0132     KEBSettings::setName(header()->sectionSize(KEBApp::NameColumn));
0133     KEBSettings::setURL(header()->sectionSize(KEBApp::UrlColumn));
0134     KEBSettings::setComment(header()->sectionSize(KEBApp::CommentColumn));
0135     KEBSettings::setStatus(header()->sectionSize(KEBApp::StatusColumn));
0136     KEBSettings::self()->save();
0137 }
0138 
0139 /************/
0140 
0141 BookmarkFolderViewFilterModel::BookmarkFolderViewFilterModel(QObject *parent)
0142     : QSortFilterProxyModel(parent)
0143 {
0144 }
0145 
0146 QStringList BookmarkFolderViewFilterModel::mimeTypes() const
0147 {
0148     return sourceModel()->mimeTypes();
0149 }
0150 
0151 bool BookmarkFolderViewFilterModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
0152 {
0153     QModelIndex dropDestProxyIndex;
0154     bool isInsertBetweenOp = false;
0155     if (row == -1) {
0156         dropDestProxyIndex = parent;
0157     } else {
0158         dropDestProxyIndex = index(row, column, parent);
0159         isInsertBetweenOp = true;
0160     }
0161     QModelIndex dropDestIndex = mapToSource(dropDestProxyIndex);
0162     if (!isInsertBetweenOp) {
0163         // Just dropping it onto dropDestIndex - ignore row and column.
0164         return sourceModel()->dropMimeData(data, action, -1, -1, dropDestIndex);
0165     } else {
0166         // Dropping before dropDestIndex.  We want to keep row and column
0167         // relative to the parent.
0168         // I'm reasonably certain the parent must be valid in this case.  If you get a crash here - nag me!
0169         Q_ASSERT(parent.isValid());
0170         QModelIndex dropDestParentIndex = mapToSource(parent);
0171         return sourceModel()->dropMimeData(data, action, dropDestIndex.row(), dropDestIndex.column(), dropDestParentIndex);
0172     }
0173 }
0174 
0175 BookmarkFolderViewFilterModel::~BookmarkFolderViewFilterModel()
0176 {
0177 }
0178 
0179 bool BookmarkFolderViewFilterModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
0180 {
0181     Q_UNUSED(source_parent);
0182 
0183     // Show name, hide everything else
0184     return (source_column == 0);
0185 }
0186 
0187 bool BookmarkFolderViewFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0188 {
0189     QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0190     const KBookmark bk = index.data(KBookmarkModel::KBookmarkRole).value<KBookmark>();
0191     return bk.isGroup();
0192 }