1 /* Shortcut.h */ 2 3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras) 4 * 5 * This file is part of sayonara player 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef SHORTCUT_H 22 #define SHORTCUT_H 23 24 #include "ShortcutIdentifier.h" 25 #include "Utils/Pimpl.h" 26 27 #include <QShortcut> 28 29 #define ShortcutHandlerPrivate private 30 31 class QKeySequence; 32 class QWidget; 33 /** 34 * @brief A single shortcut managed by ShortcutHandler. 35 * This class holds information about the default shortcuts, 36 * the user defined shortcuts, a name attached to each shortcut 37 * an identifier which is written into the database and a list 38 * of the corresponding shortcuts in the Qt format 39 * @ingroup Shortcuts 40 */ 41 class Shortcut 42 { 43 private: 44 PIMPL(Shortcut) 45 46 Shortcut(); 47 48 /** 49 * @brief Converts the sequences from get_sequences() to a list of qt specific 50 * shortcuts and writes them into the _qt_shortcuts field 51 * @param parent the widget the shortcut is mapped to 52 * @return a list of shortcuts in the Qt format 53 */ 54 QList<QShortcut*> initQtShortcut(QWidget* parent, Qt::ShortcutContext context); 55 56 57 friend class ShortcutHandler; 58 ShortcutHandlerPrivate: 59 void setQtShortcuts(const QList<QShortcut*>& qshortcuts); 60 void removeQtShortcut(QShortcut* qshortcut); 61 QList<QShortcut*> qtShortcuts() const; 62 63 64 public: 65 /** 66 * @brief Shortcut 67 * @param identifier an unique identifier used to write the shortcut into the database 68 * @param name the name displayed in the Shortcut configuration dialog 69 * @param defaultShortcut one default shortcut 70 */ 71 Shortcut(ShortcutIdentifier identifier, const QString& defaultShortcut); 72 73 /** 74 * @brief Shortcut 75 * @param identifier an unique identifier used to write the shortcut into the database 76 * @param name the name displayed in the Shortcut configuration dialog 77 * @param defaultShortcuts a list of default shortcuts 78 */ 79 Shortcut(ShortcutIdentifier identifier, const QStringList& defaultShortcuts); 80 81 /** 82 * @brief Copy constructor 83 * @param other 84 */ 85 Shortcut(const Shortcut& other); 86 87 Shortcut& operator=(const Shortcut& other); 88 89 ~Shortcut(); 90 91 /** 92 * @brief get a raw and invalid shortcut. This function is used instead of the default constructor 93 * @return an uninitialized shortcut 94 */ 95 static Shortcut getInvalid(); 96 97 /** 98 * @brief 99 * @param shortcuts map new user-readable key sequences to this shortcut 100 */ 101 void changeShortcut(const QStringList& shortcuts); 102 103 /** 104 * @brief get the human-readable name of the shortcut 105 * @return 106 */ 107 QString name() const; 108 109 /** 110 * @brief get a human-readable list of mapped default shortcuts 111 * @return 112 */ 113 QStringList defaultShortcut() const; 114 115 /** 116 * @brief get a list key squences mapped to this shortcut 117 * @return 118 */ 119 QList<QKeySequence> sequences() const; 120 QKeySequence sequence() const; 121 122 /** 123 * @brief get a human-readable list of mapped shortcuts 124 * @return 125 */ 126 const QStringList& shortcuts() const; 127 128 /** 129 * @brief get the unique identifier 130 * @return 131 */ 132 ShortcutIdentifier identifier() const; 133 QString databaseKey() const; 134 135 /** 136 * @brief Check if the shortcut is valid or if it was retrieved via getInvalid() 137 * @return 138 */ 139 bool isValid() const; 140 141 template<typename T> 142 /** 143 * @brief create a qt shortcut for a widget 144 * @param parent the widget the shortcut is attached to 145 * @param func a lambda function which will be triggered when shortcut is pressed 146 */ 147 void connect(QWidget* parent, T func, Qt::ShortcutContext context=Qt::WindowShortcut) 148 { 149 QList<QShortcut*> shortcuts = initQtShortcut(parent, context); 150 for(QShortcut* sc : shortcuts) 151 { 152 parent->connect(sc, &QShortcut::activated, func); 153 } 154 } 155 156 157 /** 158 * @brief create a qt shortcut for a widget 159 * @param parent the widget the shortcut is attached to 160 * @param the receiver object of the shortcut 161 * @param the slot which is triggered when pressing that shortcut 162 */ 163 void connect(QWidget* parent, QObject* receiver, const char* slot, Qt::ShortcutContext context=Qt::WindowShortcut); 164 }; 165 166 #endif // SHORTCUT_H 167