File indexing completed on 2025-01-05 03:57:54

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-08-27
0007  * Description : Showfoto folder view bookmarks list
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotofolderviewbookmarklist.h"
0016 
0017 // Qt includes
0018 
0019 #include <QHeaderView>
0020 #include <QMimeData>
0021 #include <QFileInfo>
0022 #include <QUrl>
0023 #include <QList>
0024 #include <QDir>
0025 #include <QDrag>
0026 #include <QMenu>
0027 
0028 // KDE includes
0029 
0030 #include <klocalizedstring.h>
0031 
0032 // Local include
0033 
0034 #include "digikam_debug.h"
0035 #include "showfotofolderviewbookmarks.h"
0036 #include "showfotofolderviewbookmarkitem.h"
0037 #include "dfileoperations.h"
0038 
0039 using namespace Digikam;
0040 
0041 namespace ShowFoto
0042 {
0043 
0044 class Q_DECL_HIDDEN ShowfotoFolderViewBookmarkList::Private
0045 {
0046 public:
0047 
0048     explicit Private()
0049         : parent (nullptr)
0050     {
0051     }
0052 
0053 public:
0054 
0055     ShowfotoFolderViewBookmarks* parent;
0056 };
0057 
0058 ShowfotoFolderViewBookmarkList::ShowfotoFolderViewBookmarkList(ShowfotoFolderViewBookmarks* const parent)
0059     : QTreeWidget(parent),
0060       d          (new Private)
0061 {
0062     d->parent = parent;
0063     setWhatsThis(i18nc("@info", "You can add or remove bookmarked places here."));
0064     setAlternatingRowColors(true);
0065     setColumnCount(1);
0066     setHeaderHidden(true);
0067     setSortingEnabled(true);
0068     setUniformRowHeights(true);
0069     setAllColumnsShowFocus(true);
0070     sortByColumn(0, Qt::AscendingOrder);
0071     setSelectionMode(QAbstractItemView::SingleSelection);
0072     header()->setSectionResizeMode(QHeaderView::Stretch);
0073     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0074     setAcceptDrops(true);
0075     setDragEnabled(true);
0076     viewport()->setAcceptDrops(true);
0077     setDropIndicatorShown(true);
0078     viewport()->setMouseTracking(true);
0079     setContextMenuPolicy(Qt::CustomContextMenu);
0080 
0081     connect(this, SIGNAL(customContextMenuRequested(QPoint)),
0082             this, SLOT(slotContextMenu(QPoint)));
0083 }
0084 
0085 ShowfotoFolderViewBookmarkList::~ShowfotoFolderViewBookmarkList()
0086 {
0087     delete d;
0088 }
0089 
0090 void ShowfotoFolderViewBookmarkList::slotOpenInFileManager()
0091 {
0092     QList<QUrl> urls;
0093     ShowfotoFolderViewBookmarkItem* const fvitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(currentItem());
0094 
0095     if (fvitem)
0096     {
0097         urls << QUrl::fromLocalFile(fvitem->path());
0098     }
0099 
0100     DFileOperations::openInFileManager(urls);
0101 }
0102 
0103 void ShowfotoFolderViewBookmarkList::slotLoadContents()
0104 {
0105     ShowfotoFolderViewBookmarkItem* const fvitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(currentItem());
0106 
0107     if (fvitem)
0108     {
0109         Q_EMIT signalLoadContents(fvitem->path());
0110     }
0111 }
0112 
0113 void ShowfotoFolderViewBookmarkList::slotContextMenu(const QPoint& pos)
0114 {
0115     ShowfotoFolderViewBookmarkItem* const fvitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(itemAt(pos));
0116 
0117     if (fvitem && (fvitem->parent() == d->parent->topBookmarksItem()))
0118     {
0119         QMenu* const ctxmenu        = new QMenu(this);
0120         ctxmenu->setTitle(i18nc("@title", "Bookmarks"));
0121         ctxmenu->addAction(d->parent->toolBarAction(QLatin1String("AddBookmark")));
0122         ctxmenu->addAction(d->parent->toolBarAction(QLatin1String("DelBookmark")));
0123         ctxmenu->addAction(d->parent->toolBarAction(QLatin1String("EditBookmark")));
0124         ctxmenu->addSeparator();
0125         ctxmenu->addActions(d->parent->pluginActions());
0126         ctxmenu->addSeparator();
0127 
0128         QAction* const loadContents = new QAction(QIcon::fromTheme(QLatin1String("media-playlist-normal")),
0129                                                   i18nc("@action: context menu", "Load Contents"), ctxmenu);
0130         ctxmenu->addAction(loadContents);
0131 
0132         connect(loadContents, SIGNAL(triggered()),
0133                 this, SLOT(slotLoadContents()));
0134 
0135         QAction* const openFileMngr = new QAction(QIcon::fromTheme(QLatin1String("folder-open")),
0136                                                   i18nc("@action: context menu", "Open in File Manager"), ctxmenu);
0137         ctxmenu->addAction(openFileMngr);
0138 
0139         connect(openFileMngr, SIGNAL(triggered()),
0140                 this, SLOT(slotOpenInFileManager()));
0141 
0142         ctxmenu->exec(mapToGlobal(pos));
0143 
0144         delete ctxmenu;
0145     }
0146 }
0147 
0148 void ShowfotoFolderViewBookmarkList::dragEnterEvent(QDragEnterEvent* e)
0149 {
0150     QTreeWidget::dragEnterEvent(e);
0151     e->accept();
0152 }
0153 
0154 void ShowfotoFolderViewBookmarkList::dragMoveEvent(QDragMoveEvent* e)
0155 {
0156     if (e->mimeData()->hasUrls())
0157     {
0158         QList<QUrl> urls = e->mimeData()->urls();
0159 
0160         QFileInfo inf(urls.first().toLocalFile());
0161 
0162         if (inf.isDir())
0163         {
0164 
0165 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0166 
0167             QTreeWidgetItem* const item                  = itemAt(e->position().toPoint());
0168 
0169 #else
0170 
0171             QTreeWidgetItem* const item                  = itemAt(e->pos());
0172 
0173 #endif
0174 
0175             ShowfotoFolderViewBookmarkItem* const fvitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(item);
0176 
0177             if (
0178                 (item == d->parent->topBookmarksItem()) ||
0179                 (fvitem && (fvitem->parent() == d->parent->topBookmarksItem()))
0180                )
0181             {
0182                 QTreeWidget::dragMoveEvent(e);
0183                 e->accept();
0184                 return;
0185             }
0186         }
0187     }
0188 
0189     e->ignore();
0190 }
0191 
0192 void ShowfotoFolderViewBookmarkList::dropEvent(QDropEvent* e)
0193 {
0194     if (e->mimeData()->hasUrls())
0195     {
0196         QList<QUrl> urls = e->mimeData()->urls();
0197 
0198         QFileInfo inf(urls.first().toLocalFile());
0199 
0200         if (inf.isDir())
0201         {
0202 
0203 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0204 
0205             QTreeWidgetItem* const item                  = itemAt(e->position().toPoint());
0206 
0207 #else
0208 
0209             QTreeWidgetItem* const item                  = itemAt(e->pos());
0210 
0211 #endif
0212 
0213             ShowfotoFolderViewBookmarkItem* const fvitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(item);
0214 
0215             if (
0216                 (item == d->parent->topBookmarksItem()) ||
0217                 (fvitem && (fvitem->parent() == d->parent->topBookmarksItem()))
0218                )
0219             {
0220                 QTreeWidget::dropEvent(e);
0221 
0222                 QString path = inf.filePath();
0223 
0224                 if (!path.endsWith(QDir::separator()))
0225                 {
0226                     path.append(QDir::separator());
0227                 }
0228 
0229                 Q_EMIT signalAddBookmark(path);
0230 
0231                 e->acceptProposedAction();
0232                 return;
0233             }
0234         }
0235     }
0236 
0237     e->ignore();
0238 }
0239 
0240 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0241 
0242 QMimeData* ShowfotoFolderViewBookmarkList::mimeData(const QList<QTreeWidgetItem*>& items) const
0243 
0244 #else
0245 
0246 // cppcheck-suppress passedByValue
0247 QMimeData* ShowfotoFolderViewBookmarkList::mimeData(const QList<QTreeWidgetItem*> items) const     // clazy:exclude=function-args-by-ref
0248 
0249 #endif
0250 
0251 {
0252     QList<QUrl> urls;
0253 
0254     Q_FOREACH (QTreeWidgetItem* const itm, items)
0255     {
0256         ShowfotoFolderViewBookmarkItem* const vitem = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(itm);
0257 
0258         if (vitem)
0259         {
0260             urls.append(QUrl::fromLocalFile(vitem->path()));
0261         }
0262     }
0263 
0264     QMimeData* const data = new QMimeData;
0265     data->setUrls(urls);
0266 
0267     return data;
0268 }
0269 
0270 void ShowfotoFolderViewBookmarkList::startDrag(Qt::DropActions /*supportedActions*/)
0271 {
0272     QList<QTreeWidgetItem*> items = selectedItems();
0273 
0274     if (items.isEmpty())
0275     {
0276         return;
0277     }
0278 
0279     QDrag* const drag = new QDrag(this);
0280     drag->setMimeData(mimeData(items));
0281     drag->exec();
0282 }
0283 
0284 ShowfotoFolderViewBookmarkItem* ShowfotoFolderViewBookmarkList::bookmarkExists(const QString& path) const
0285 {
0286     bool found                           = false;
0287     ShowfotoFolderViewBookmarkItem* item = nullptr;
0288 
0289     for (int i = 0 ; i < d->parent->topBookmarksItem()->childCount() ; ++i)
0290     {
0291         item = dynamic_cast<ShowfotoFolderViewBookmarkItem*>(d->parent->topBookmarksItem()->child(i));
0292 
0293         if (item && (path == item->path()))
0294         {
0295             found = true;
0296             break;
0297         }
0298     }
0299 
0300     if (found)
0301     {
0302         return item;
0303     }
0304 
0305     return nullptr;
0306 }
0307 
0308 QString ShowfotoFolderViewBookmarkList::bookmarkBaseName(const QString& path) const
0309 {
0310     if (path.endsWith(QDir::separator()))
0311     {
0312         return (path.section(QDir::separator(), -2).remove(QDir::separator()));
0313     }
0314     else
0315     {
0316         return (QFileInfo(path).baseName());
0317     }
0318 }
0319 
0320 } // namespace ShowFoto
0321 
0322 #include "moc_showfotofolderviewbookmarklist.cpp"