File indexing completed on 2024-04-14 05:43:09

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/model.h"
0023 #include "settings.h"
0024 #include "toplevel.h" // for KEBApp
0025 
0026 #include <QContextMenuEvent>
0027 #include <QHeaderView>
0028 #include <QItemSelection>
0029 #include <QMenu>
0030 
0031 #include "keditbookmarks_debug.h"
0032 
0033 BookmarkFolderView::BookmarkFolderView(BookmarkListView *view, QWidget *parent)
0034     : KBookmarkView(parent)
0035     , mview(view)
0036 {
0037     mmodel = new BookmarkFolderViewFilterModel(parent);
0038     mmodel->setSourceModel(view->model());
0039     setModel(mmodel);
0040     header()->setVisible(false);
0041     setRootIsDecorated(false);
0042     setDropIndicatorShown(true);
0043     setCurrentIndex(mmodel->index(0, 0, QModelIndex()));
0044 
0045     connect(mmodel, &BookmarkFolderViewFilterModel::modelReset, this, &BookmarkFolderView::slotReset);
0046 }
0047 
0048 BookmarkFolderView::~BookmarkFolderView()
0049 {
0050 }
0051 
0052 void BookmarkFolderView::selectionChanged(const QItemSelection &deselected, const QItemSelection &selected)
0053 {
0054     const QModelIndexList &list = selectionModel()->selectedIndexes();
0055     if (list.count())
0056         mview->setRootIndex(mmodel->mapToSource(list.at(0)));
0057     else
0058         mview->setRootIndex(QModelIndex());
0059     KBookmarkView::selectionChanged(deselected, selected);
0060 }
0061 
0062 void BookmarkFolderView::slotReset()
0063 {
0064     setCurrentIndex(mmodel->index(0, 0, QModelIndex()));
0065     loadFoldedState();
0066 }
0067 
0068 KBookmark BookmarkFolderView::bookmarkForIndex(const QModelIndex &idx) const
0069 {
0070     qCDebug(KEDITBOOKMARKS_LOG) << "BookmarkFolderView::bookmarkForIndex" << idx;
0071     const QModelIndex &index = mmodel->mapToSource(idx);
0072     return static_cast<KBookmarkModel *>(mmodel->sourceModel())->bookmarkForIndex(index);
0073 }
0074 
0075 /********/
0076 
0077 BookmarkListView::BookmarkListView(QWidget *parent)
0078     : KBookmarkView(parent)
0079 {
0080     setDragEnabled(true);
0081 }
0082 
0083 void BookmarkListView::setModel(QAbstractItemModel *model)
0084 {
0085     KBookmarkView::setModel(model);
0086 }
0087 
0088 KBookmarkModel *BookmarkListView::bookmarkModel() const
0089 {
0090     return dynamic_cast<KBookmarkModel *>(QTreeView::model());
0091 }
0092 
0093 KBookmark BookmarkListView::bookmarkForIndex(const QModelIndex &idx) const
0094 {
0095     return bookmarkModel()->bookmarkForIndex(idx);
0096 }
0097 
0098 BookmarkListView::~BookmarkListView()
0099 {
0100     saveColumnSetting();
0101 }
0102 
0103 void BookmarkListView::contextMenuEvent(QContextMenuEvent *e)
0104 {
0105     QModelIndex index = indexAt(e->pos());
0106     KBookmark bk;
0107     if (index.isValid())
0108         bk = bookmarkForIndex(index);
0109 
0110     QMenu *popup;
0111     if (!index.isValid() || (bk.address() == GlobalBookmarkManager::self()->root().address()) || (bk.isGroup())) // FIXME add empty folder padder
0112     {
0113         popup = KEBApp::self()->popupMenuFactory(QStringLiteral("popup_folder"));
0114     } else {
0115         popup = KEBApp::self()->popupMenuFactory(QStringLiteral("popup_bookmark"));
0116     }
0117     if (popup)
0118         popup->popup(e->globalPos());
0119 }
0120 
0121 void BookmarkListView::loadColumnSetting()
0122 {
0123     header()->resizeSection(KEBApp::NameColumn, KEBSettings::name());
0124     header()->resizeSection(KEBApp::UrlColumn, KEBSettings::uRL());
0125     header()->resizeSection(KEBApp::CommentColumn, KEBSettings::comment());
0126     header()->resizeSection(KEBApp::StatusColumn, KEBSettings::status());
0127 }
0128 
0129 void BookmarkListView::saveColumnSetting()
0130 {
0131     KEBSettings::setName(header()->sectionSize(KEBApp::NameColumn));
0132     KEBSettings::setURL(header()->sectionSize(KEBApp::UrlColumn));
0133     KEBSettings::setComment(header()->sectionSize(KEBApp::CommentColumn));
0134     KEBSettings::setStatus(header()->sectionSize(KEBApp::StatusColumn));
0135     KEBSettings::self()->save();
0136 }
0137 
0138 /************/
0139 
0140 BookmarkFolderViewFilterModel::BookmarkFolderViewFilterModel(QObject *parent)
0141     : QSortFilterProxyModel(parent)
0142 {
0143 }
0144 
0145 QStringList BookmarkFolderViewFilterModel::mimeTypes() const
0146 {
0147     return sourceModel()->mimeTypes();
0148 }
0149 
0150 bool BookmarkFolderViewFilterModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
0151 {
0152     QModelIndex dropDestProxyIndex;
0153     bool isInsertBetweenOp = false;
0154     if (row == -1) {
0155         dropDestProxyIndex = parent;
0156     } else {
0157         dropDestProxyIndex = index(row, column, parent);
0158         isInsertBetweenOp = true;
0159     }
0160     QModelIndex dropDestIndex = mapToSource(dropDestProxyIndex);
0161     if (!isInsertBetweenOp) {
0162         // Just dropping it onto dropDestIndex - ignore row and column.
0163         return sourceModel()->dropMimeData(data, action, -1, -1, dropDestIndex);
0164     } else {
0165         // Dropping before dropDestIndex.  We want to keep row and column
0166         // relative to the parent.
0167         // I'm reasonably certain the parent must be valid in this case.  If you get a crash here - nag me!
0168         Q_ASSERT(parent.isValid());
0169         QModelIndex dropDestParentIndex = mapToSource(parent);
0170         return sourceModel()->dropMimeData(data, action, dropDestIndex.row(), dropDestIndex.column(), dropDestParentIndex);
0171     }
0172 }
0173 
0174 BookmarkFolderViewFilterModel::~BookmarkFolderViewFilterModel()
0175 {
0176 }
0177 
0178 bool BookmarkFolderViewFilterModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
0179 {
0180     Q_UNUSED(source_parent);
0181 
0182     // Show name, hide everything else
0183     return (source_column == 0);
0184 }
0185 
0186 bool BookmarkFolderViewFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0187 {
0188     QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0189     const KBookmark bk = index.data(KBookmarkModel::KBookmarkRole).value<KBookmark>();
0190     return bk.isGroup();
0191 }
0192 
0193 #include "moc_bookmarklistview.cpp"