File indexing completed on 2025-01-19 03:50:38
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2009-02-15 0007 * Description : contextmenu helper class - Groups methods. 0008 * 0009 * SPDX-FileCopyrightText: 2009-2011 by Andi Clemens <andi dot clemens at gmail dot com> 0010 * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0011 * 0012 * SPDX-License-Identifier: GPL-2.0-or-later 0013 * 0014 * ============================================================ */ 0015 0016 #include "contextmenuhelper_p.h" 0017 0018 namespace Digikam 0019 { 0020 0021 void ContextMenuHelper::addGroupMenu(const imageIds& ids, const QList<QAction*>& extraMenuItems) 0022 { 0023 QList<QAction*> actions = groupMenuActions(ids); 0024 0025 if (actions.isEmpty() && extraMenuItems.isEmpty()) 0026 { 0027 return; 0028 } 0029 0030 if (!extraMenuItems.isEmpty()) 0031 { 0032 if (!actions.isEmpty()) 0033 { 0034 QAction* const separator = new QAction(this); 0035 separator->setSeparator(true); 0036 actions << separator; 0037 } 0038 0039 actions << extraMenuItems; 0040 } 0041 0042 QMenu* const menu = new QMenu(i18nc("@action: group items", "Group")); 0043 0044 Q_FOREACH (QAction* const action, actions) 0045 { 0046 menu->addAction(action); 0047 } 0048 0049 d->parent->addMenu(menu); 0050 } 0051 0052 void ContextMenuHelper::addGroupActions(const imageIds& ids) 0053 { 0054 Q_FOREACH (QAction* const action, groupMenuActions(ids)) 0055 { 0056 d->parent->addAction(action); 0057 } 0058 } 0059 0060 QList<QAction*> ContextMenuHelper::groupMenuActions(const imageIds& ids) 0061 { 0062 setSelectedIds(ids); 0063 0064 QList<QAction*> actions; 0065 0066 if (ids.isEmpty()) 0067 { 0068 if (d->imageFilterModel) 0069 { 0070 if (!d->imageFilterModel->isAllGroupsOpen()) 0071 { 0072 QAction* const openAction = new QAction(i18nc("@action:inmenu", "Open All Groups"), this); 0073 connect(openAction, SIGNAL(triggered()), 0074 this, SLOT(slotOpenGroups())); 0075 actions << openAction; 0076 } 0077 else 0078 { 0079 QAction* const closeAction = new QAction(i18nc("@action:inmenu", "Close All Groups"), this); 0080 connect(closeAction, SIGNAL(triggered()), 0081 this, SLOT(slotCloseGroups())); 0082 actions << closeAction; 0083 } 0084 } 0085 0086 return actions; 0087 } 0088 0089 ItemInfo info(ids.first()); 0090 0091 if (ids.size() == 1) 0092 { 0093 if (info.hasGroupedImages()) 0094 { 0095 if (d->imageFilterModel) 0096 { 0097 if (!d->imageFilterModel->isGroupOpen(info.id())) 0098 { 0099 QAction* const action = new QAction(i18nc("@action:inmenu", "Show Grouped Images"), this); 0100 connect(action, SIGNAL(triggered()), 0101 this, SLOT(slotOpenGroups())); 0102 actions << action; 0103 } 0104 else 0105 { 0106 QAction* const action = new QAction(i18nc("@action:inmenu", "Hide Grouped Images"), this); 0107 connect(action, SIGNAL(triggered()), 0108 this, SLOT(slotCloseGroups())); 0109 actions << action; 0110 } 0111 } 0112 0113 QAction* const separator = new QAction(this); 0114 separator->setSeparator(true); 0115 actions << separator; 0116 0117 QAction* const clearAction = new QAction(i18nc("@action:inmenu", "Ungroup"), this); 0118 connect(clearAction, SIGNAL(triggered()), 0119 this, SIGNAL(signalUngroup())); 0120 actions << clearAction; 0121 } 0122 else if (info.isGrouped()) 0123 { 0124 QAction* const action = new QAction(i18nc("@action:inmenu", "Remove From Group"), this); 0125 connect(action, SIGNAL(triggered()), 0126 this, SIGNAL(signalRemoveFromGroup())); 0127 actions << action; 0128 0129 // TODO: set as group leader / pick image 0130 } 0131 } 0132 else 0133 { 0134 QAction* const closeAction = new QAction(i18nc("@action:inmenu", "Group Selected Here"), this); 0135 connect(closeAction, SIGNAL(triggered()), 0136 this, SIGNAL(signalCreateGroup())); 0137 actions << closeAction; 0138 0139 QAction* const closeActionDate = new QAction(i18nc("@action:inmenu", "Group Selected By Time"), this); 0140 connect(closeActionDate, SIGNAL(triggered()), 0141 this, SIGNAL(signalCreateGroupByTime())); 0142 actions << closeActionDate; 0143 0144 QAction* const closeActionType = new QAction(i18nc("@action:inmenu", "Group Selected By Filename"), this); 0145 connect(closeActionType, SIGNAL(triggered()), 0146 this, SIGNAL(signalCreateGroupByFilename())); 0147 actions << closeActionType; 0148 0149 QAction* const closeActionTimelapse = new QAction(i18nc("@action:inmenu", "Group Selected By Timelapse / Burst"), this); 0150 connect(closeActionTimelapse, SIGNAL(triggered()), 0151 this, SIGNAL(signalCreateGroupByTimelapse())); 0152 actions << closeActionTimelapse; 0153 0154 QAction* const separator = new QAction(this); 0155 separator->setSeparator(true); 0156 actions << separator; 0157 0158 if (d->imageFilterModel) 0159 { 0160 QAction* const openAction = new QAction(i18nc("@action:inmenu", "Show Grouped Images"), this); 0161 connect(openAction, SIGNAL(triggered()), 0162 this, SLOT(slotOpenGroups())); 0163 actions << openAction; 0164 0165 QAction* const hideAction = new QAction(i18nc("@action:inmenu", "Hide Grouped Images"), this); 0166 connect(hideAction, SIGNAL(triggered()), 0167 this, SLOT(slotCloseGroups())); 0168 actions << hideAction; 0169 0170 QAction* const separator2 = new QAction(this); 0171 separator2->setSeparator(true); 0172 actions << separator2; 0173 } 0174 0175 QAction* const removeAction = new QAction(i18nc("@action:inmenu", "Remove Selected From Groups"), this); 0176 connect(removeAction, SIGNAL(triggered()), 0177 this, SIGNAL(signalRemoveFromGroup())); 0178 actions << removeAction; 0179 0180 QAction* const clearAction = new QAction(i18nc("@action:inmenu", "Ungroup Selected"), this); 0181 connect(clearAction, SIGNAL(triggered()), 0182 this, SIGNAL(signalUngroup())); 0183 actions << clearAction; 0184 } 0185 0186 return actions; 0187 } 0188 0189 void ContextMenuHelper::setGroupsOpen(bool open) 0190 { 0191 if (!d->imageFilterModel || d->selectedIds.isEmpty()) 0192 { 0193 return; 0194 } 0195 0196 GroupItemFilterSettings settings = d->imageFilterModel->groupItemFilterSettings(); 0197 0198 Q_FOREACH (const qlonglong& id, d->selectedIds) 0199 { 0200 ItemInfo info(id); 0201 0202 if (info.hasGroupedImages()) 0203 { 0204 settings.setOpen(id, open); 0205 } 0206 } 0207 0208 d->imageFilterModel->setGroupItemFilterSettings(settings); 0209 } 0210 0211 void ContextMenuHelper::slotOpenGroups() 0212 { 0213 setGroupsOpen(true); 0214 } 0215 0216 void ContextMenuHelper::slotCloseGroups() 0217 { 0218 setGroupsOpen(false); 0219 } 0220 0221 void ContextMenuHelper::slotOpenAllGroups() 0222 { 0223 if (!d->imageFilterModel) 0224 { 0225 return; 0226 } 0227 0228 d->imageFilterModel->setAllGroupsOpen(true); 0229 } 0230 0231 void ContextMenuHelper::slotCloseAllGroups() 0232 { 0233 if (!d->imageFilterModel) 0234 { 0235 return; 0236 } 0237 0238 d->imageFilterModel->setAllGroupsOpen(false); 0239 } 0240 0241 } // namespace Digikam