File indexing completed on 2025-02-23 04:34:22
0001 /** 0002 * \file sectionactions.cpp 0003 * Actions for section shortcuts. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 22 Mar 2020 0008 * 0009 * Copyright (C) 2020-2024 Urs Fleisch 0010 * 0011 * This file is part of Kid3. 0012 * 0013 * Kid3 is free software; you can redistribute it and/or modify 0014 * it under the terms of the GNU General Public License as published by 0015 * the Free Software Foundation; either version 2 of the License, or 0016 * (at your option) any later version. 0017 * 0018 * Kid3 is distributed in the hope that it will be useful, 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 * GNU General Public License for more details. 0022 * 0023 * You should have received a copy of the GNU General Public License 0024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0025 */ 0026 0027 #include "sectionactions.h" 0028 #include <QAction> 0029 #include <QAbstractItemView> 0030 0031 /** 0032 * Constructor. 0033 * @param groups action groups to add 0034 * @param widget widget to which actions are added 0035 */ 0036 SectionActions::SectionActions(ActionGroups groups, QWidget* widget) 0037 : QObject(widget), 0038 m_widget(widget), 0039 m_previousSectionAction(nullptr), m_nextSectionAction(nullptr), 0040 m_copyAction(nullptr), m_pasteAction(nullptr), 0041 m_removeAction(nullptr), m_transferAction(nullptr), 0042 m_editAction(nullptr), m_addAction(nullptr), m_deleteAction(nullptr) 0043 { 0044 auto shortcutContext = qobject_cast<QAbstractItemView*>(m_widget) 0045 ? Qt::WidgetShortcut : Qt::WidgetWithChildrenShortcut; 0046 if (groups & Navigation) { 0047 m_previousSectionAction = new QAction(m_widget); 0048 m_previousSectionAction->setObjectName(QLatin1String("previous_section")); 0049 m_previousSectionAction->setShortcut(QKeySequence::Back); 0050 m_previousSectionAction->setShortcutContext(shortcutContext); 0051 m_widget->addAction(m_previousSectionAction); 0052 0053 m_nextSectionAction = new QAction(m_widget); 0054 m_nextSectionAction->setObjectName(QLatin1String("next_section")); 0055 m_nextSectionAction->setShortcut(QKeySequence::Forward); 0056 m_nextSectionAction->setShortcutContext(shortcutContext); 0057 m_widget->addAction(m_nextSectionAction); 0058 } 0059 if (groups & Transfer) { 0060 m_transferAction = new QAction(m_widget); 0061 m_transferAction->setObjectName(QLatin1String("transfer_section")); 0062 m_transferAction->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_V); 0063 m_transferAction->setShortcutContext(shortcutContext); 0064 m_widget->addAction(m_transferAction); 0065 } 0066 if (groups & EditSection) { 0067 m_copyAction = new QAction(m_widget); 0068 m_copyAction->setObjectName(QLatin1String("copy_section")); 0069 m_copyAction->setShortcut(QKeySequence::Copy); 0070 m_copyAction->setShortcutContext(shortcutContext); 0071 m_widget->addAction(m_copyAction); 0072 0073 m_pasteAction = new QAction(m_widget); 0074 m_pasteAction->setObjectName(QLatin1String("paste_section")); 0075 m_pasteAction->setShortcut(QKeySequence::Paste); 0076 m_pasteAction->setShortcutContext(shortcutContext); 0077 m_widget->addAction(m_pasteAction); 0078 0079 m_removeAction = new QAction(m_widget); 0080 m_removeAction->setObjectName(QLatin1String("remove_section")); 0081 m_removeAction->setShortcut(Qt::SHIFT | Qt::Key_Delete); 0082 m_removeAction->setShortcutContext(shortcutContext); 0083 m_widget->addAction(m_removeAction); 0084 } 0085 if (groups & EditElement) { 0086 m_editAction = new QAction(m_widget); 0087 m_editAction->setObjectName(QLatin1String("edit_section_element")); 0088 m_editAction->setShortcut(Qt::Key_F2); 0089 m_editAction->setShortcutContext(shortcutContext); 0090 m_widget->addAction(m_editAction); 0091 0092 m_addAction = new QAction(m_widget); 0093 m_addAction->setObjectName(QLatin1String("add_section_element")); 0094 m_addAction->setShortcut(Qt::Key_Insert); 0095 m_addAction->setShortcutContext(shortcutContext); 0096 m_widget->addAction(m_addAction); 0097 0098 m_deleteAction = new QAction(m_widget); 0099 m_deleteAction->setObjectName(QLatin1String("delete_section_element")); 0100 m_deleteAction->setShortcut(QKeySequence::Delete); 0101 m_deleteAction->setShortcutContext(shortcutContext); 0102 m_widget->addAction(m_deleteAction); 0103 } 0104 } 0105 0106 /** 0107 * Set keyboard shortcuts for section actions. 0108 * @param map map of action names to key sequences 0109 */ 0110 void SectionActions::setShortcuts(const QMap<QString, QKeySequence>& map) 0111 { 0112 const QList actions = { 0113 m_previousSectionAction, 0114 m_nextSectionAction, 0115 m_copyAction, 0116 m_pasteAction, 0117 m_removeAction, 0118 m_transferAction, 0119 m_editAction, 0120 m_addAction, 0121 m_deleteAction 0122 }; 0123 for (QAction* action : actions) { 0124 if (action) { 0125 if (QString name = action->objectName(); !name.isEmpty()) { 0126 if (const auto it = map.constFind(name); 0127 it != map.constEnd()) { 0128 action->setShortcut(*it); 0129 } 0130 } 0131 } 0132 } 0133 } 0134 0135 /** 0136 * Get section action default shortcut information. 0137 * @return list with name, display name, shortcut for all section actions. 0138 */ 0139 QList<std::tuple<QString, QString, QKeySequence>> 0140 SectionActions::defaultShortcuts() 0141 { 0142 return { 0143 // All these "std::tuple<QString, QString, QKeySequence>" are quite awkward, 0144 // but seem to be necessary to build with GCC 4.8. 0145 std::tuple<QString, QString, QKeySequence>{ 0146 QLatin1String("previous_section"), tr("Previous"), QKeySequence::Back}, 0147 std::tuple<QString, QString, QKeySequence>{ 0148 QLatin1String("next_section"), tr("Next"), QKeySequence::Forward}, 0149 std::tuple<QString, QString, QKeySequence>{ 0150 QLatin1String("transfer_section"), tr("Transfer"), 0151 Qt::CTRL | Qt::SHIFT | Qt::Key_V}, 0152 std::tuple<QString, QString, QKeySequence>{ 0153 QLatin1String("copy_section"), tr("Copy"), QKeySequence::Copy}, 0154 std::tuple<QString, QString, QKeySequence>{ 0155 QLatin1String("paste_section"), tr("Paste"), QKeySequence::Paste}, 0156 std::tuple<QString, QString, QKeySequence>{ 0157 QLatin1String("remove_section"), tr("Remove"), 0158 Qt::SHIFT | Qt::Key_Delete}, 0159 std::tuple<QString, QString, QKeySequence>{ 0160 QLatin1String("edit_section_element"), tr("Edit"), Qt::Key_F2}, 0161 std::tuple<QString, QString, QKeySequence>{ 0162 QLatin1String("add_section_element"), tr("Add"), Qt::Key_Insert}, 0163 std::tuple<QString, QString, QKeySequence>{ 0164 QLatin1String("delete_section_element"), tr("Delete"), 0165 QKeySequence::Delete}, 0166 std::tuple<QString, QString, QKeySequence>{ 0167 QLatin1String("open_parent"), tr("Open Parent Folder"), 0168 Qt::CTRL | Qt::Key_Up}, 0169 std::tuple<QString, QString, QKeySequence>{ 0170 QLatin1String("open_current"), tr("Open Current Folder"), 0171 Qt::CTRL | Qt::Key_Down} 0172 }; 0173 }