File indexing completed on 2025-01-05 04:00:03

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-01-14
0007  * Description : Basic search tree view with editing functionality
0008  *
0009  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2009-2010 by Johannes Wienke <languitar at semipol dot de>
0011  * SPDX-FileCopyrightText: 2009      by Andi Clemens <andi dot clemens at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "editablesearchtreeview.h"
0018 
0019 // Qt includes
0020 
0021 #include <QAction>
0022 #include <QIcon>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "digikam_debug.h"
0031 #include "contextmenuhelper.h"
0032 
0033 namespace Digikam
0034 {
0035 
0036 class Q_DECL_HIDDEN EditableSearchTreeView::Private
0037 {
0038 public:
0039 
0040     explicit Private()
0041       : searchModificationHelper(nullptr),
0042         renameSearchAction      (nullptr),
0043         deleteSearchAction      (nullptr)
0044     {
0045     }
0046 
0047     SearchModificationHelper* searchModificationHelper;
0048 
0049     QAction*                  renameSearchAction;
0050     QAction*                  deleteSearchAction;
0051 };
0052 
0053 EditableSearchTreeView::EditableSearchTreeView(QWidget* const parent,
0054                                                SearchModel* const searchModel,
0055                                                SearchModificationHelper* const searchModificationHelper)
0056     : SearchTreeView(parent),
0057       d             (new Private)
0058 {
0059     setAlbumModel(searchModel);
0060     d->searchModificationHelper = searchModificationHelper;
0061 
0062     d->renameSearchAction       = new QAction(QIcon::fromTheme(QLatin1String("document-edit")), i18n("Rename..."), this);
0063     d->deleteSearchAction       = new QAction(QIcon::fromTheme(QLatin1String("edit-delete")),   i18n("Delete"),    this);
0064 
0065     setSortingEnabled(true);
0066     setSelectAlbumOnClick(true);
0067     setEnableContextMenu(true);
0068 }
0069 
0070 EditableSearchTreeView::~EditableSearchTreeView()
0071 {
0072     delete d;
0073 }
0074 
0075 QString EditableSearchTreeView::contextMenuTitle() const
0076 {
0077     return i18n("Searches");
0078 }
0079 
0080 void EditableSearchTreeView::addCustomContextMenuActions(ContextMenuHelper& cmh, Album* album)
0081 {
0082     SAlbum* const searchAlbum = dynamic_cast<SAlbum*>(album);
0083 
0084     // disable actions if there is no album or the album is a temporary search
0085 
0086     bool activate = false;
0087 
0088     if (searchAlbum)
0089     {
0090         activate = !searchAlbum->isTemporarySearch();
0091         d->deleteSearchAction->setEnabled(true);
0092     }
0093 
0094     d->renameSearchAction->setEnabled(activate);
0095 
0096     cmh.addAction(d->renameSearchAction);
0097     cmh.addAction(d->deleteSearchAction);
0098 }
0099 
0100 void EditableSearchTreeView::handleCustomContextMenuAction(QAction* action, const AlbumPointer<Album>& album)
0101 {
0102     Album* const a            = album;
0103     SAlbum* const searchAlbum = dynamic_cast<SAlbum*>(a);
0104 
0105     if (!searchAlbum || !action)
0106     {
0107         return;
0108     }
0109 
0110     if      (action == d->renameSearchAction)
0111     {
0112         d->searchModificationHelper->slotSearchRename(searchAlbum);
0113     }
0114     else if (action == d->deleteSearchAction)
0115     {
0116         d->searchModificationHelper->slotSearchDelete(searchAlbum);
0117     }
0118 }
0119 
0120 } // namespace Digikam
0121 
0122 #include "moc_editablesearchtreeview.cpp"