Warning, file /multimedia/kid3/src/app/qt/shortcutsmodel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file shortcutsmodel.h
0003  * Keyboard shortcuts configuration tree model.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Dec 2011
0008  *
0009  * Copyright (C) 2011-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 #pragma once
0028 
0029 #include <QAbstractItemModel>
0030 #include <QList>
0031 
0032 class QAction;
0033 class ISettings;
0034 
0035 /**
0036  * Keyboard shortcuts configuration tree model.
0037  *
0038  * The model is hierarchical with two levels: The keyboard shortcuts have
0039  * columns with the text of the action and the key sequences and have context
0040  * parent items, which describe the menu or section in the GUI where the
0041  * action can be found. The model can be used in a QTreeView, to edit the
0042  * @ref ShortcutColumn, a ShortcutsDelegate can be used.
0043  */
0044 class ShortcutsModel : public QAbstractItemModel {
0045   Q_OBJECT
0046 public:
0047   /**
0048    * Columns in model.
0049    */
0050   enum Columns {
0051     ActionColumn,   /**< Action text */
0052     ShortcutColumn, /**< Shortcut key sequence */
0053     NumColumns      /**< Number of columns */
0054   };
0055 
0056   /**
0057    * Constructor.
0058    * @param parent parent widget
0059    */
0060   explicit ShortcutsModel(QObject* parent = nullptr);
0061 
0062   /**
0063    * Destructor.
0064    */
0065   ~ShortcutsModel() override = default;
0066 
0067   /**
0068    * Get item flags for index.
0069    * @param index model index
0070    * @return item flags
0071    */
0072   Qt::ItemFlags flags(const QModelIndex& index) const override;
0073 
0074   /**
0075    * Get data for a given role.
0076    * @param index model index
0077    * @param role item data role
0078    * @return data for role
0079    */
0080   QVariant data(const QModelIndex& index,
0081                 int role = Qt::DisplayRole) const override;
0082 
0083   /**
0084    * Set data for a given role.
0085    * @param index model index
0086    * @param value data value
0087    * @param role item data role
0088    * @return true if successful
0089    */
0090   bool setData(const QModelIndex& index, const QVariant& value,
0091                int role = Qt::EditRole) override;
0092 
0093   /**
0094    * Get data for header section.
0095    * @param section column or row
0096    * @param orientation horizontal or vertical
0097    * @param role item data role
0098    * @return header data for role
0099    */
0100   QVariant headerData(int section, Qt::Orientation orientation,
0101                       int role = Qt::DisplayRole) const override;
0102 
0103   /**
0104    * Get number of rows.
0105    * @param parent parent model index
0106    * @return number of rows, if parent is valid number of children
0107    */
0108   int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0109 
0110   /**
0111    * Get number of columns.
0112    * @param parent parent model index
0113    * @return number of columns for children of given \a parent
0114    */
0115   int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0116 
0117   /**
0118    * Get model index of item.
0119    * @param row row of item
0120    * @param column column of item
0121    * @param parent index of parent item
0122    * @return model index of item
0123    */
0124   QModelIndex index(int row, int column,
0125                     const QModelIndex& parent = QModelIndex()) const override;
0126 
0127   /**
0128    * Get parent of item.
0129    * @param index model index of item
0130    * @return model index of parent item
0131    */
0132   QModelIndex parent(const QModelIndex& index) const override;
0133 
0134   /**
0135    * Register an action.
0136    *
0137    * @param action action to be added to model
0138    * @param context context of action
0139    */
0140   void registerAction(QAction* action, const QString& context);
0141 
0142   /**
0143    * Unregister an action.
0144    *
0145    * @param action action to be removed from model
0146    * @param context context of action
0147    */
0148   void unregisterAction(const QAction* action, const QString& context);
0149 
0150   /**
0151    * Get mapping of shortcut names to key sequences.
0152    * @return shortcut map.
0153    */
0154   QMap<QString, QKeySequence> shortcutsMap() const;
0155 
0156   /**
0157    * Assign the shortcuts which have been changed to their actions.
0158    *
0159    * @return true if there was at least one shortcut changed
0160    */
0161   bool assignChangedShortcuts();
0162 
0163   /**
0164    * Save the shortcuts to a given configuration.
0165    *
0166    * @param config configuration settings
0167    */
0168   void writeToConfig(ISettings* config) const;
0169 
0170   /**
0171    * Read the shortcuts from a given configuration.
0172    *
0173    * @param config configuration settings
0174    */
0175   void readFromConfig(ISettings* config);
0176 
0177   /**
0178    * Clear all shortcuts to their default values.
0179    */
0180   void clearShortcuts();
0181 
0182 public slots:
0183   /**
0184    * Forget about all changed shortcuts.
0185    */
0186   void discardChangedShortcuts();
0187 
0188 signals:
0189   /**
0190    * Emitted if a keyboard shortcut is already used.
0191    * @param key string representation of key sequence
0192    * @param context context of action
0193    * @param action action using @a key
0194    */
0195   void shortcutAlreadyUsed(const QString& key, const QString& context,
0196                            const QAction* action);
0197 
0198   /**
0199    * Emitted if a keyboard shortcut is set.
0200    * Can be used to clear a previously displayed "already used" warning.
0201    * @param key string representation of key sequence
0202    * @param context context of action
0203    * @param action action using @a key
0204    */
0205   void shortcutSet(const QString& key, const QString& context,
0206                    const QAction* action);
0207 
0208 private:
0209   class ShortcutItem {
0210   public:
0211     explicit ShortcutItem(QAction* act);
0212 
0213     QAction* action() { return m_action; }
0214     const QAction* action() const { return m_action; }
0215 
0216     QString customShortcut() const { return m_customShortcut; }
0217     void setCustomShortcut(const QString& shortcut);
0218 
0219     void revertCustomShortcut();
0220 
0221     void clearCustomShortcut();
0222 
0223     void assignCustomShortcut();
0224 
0225     bool isCustomShortcutChanged() const {
0226       return m_customShortcut != m_oldCustomShortcut ||
0227           m_customShortcut.isNull() != m_oldCustomShortcut.isNull();
0228     }
0229 
0230     QString activeShortcut() const {
0231       return m_customShortcut.isNull() ? m_defaultShortcut : m_customShortcut;
0232     }
0233 
0234     bool isCustomShortcutActive() const {
0235       return !m_customShortcut.isNull();
0236     }
0237 
0238     QString actionText() const;
0239 
0240   private:
0241     QAction* m_action;
0242     QString m_defaultShortcut;
0243     QString m_customShortcut;
0244     QString m_oldCustomShortcut;
0245   };
0246 
0247   class ShortcutGroup : public QList<ShortcutItem> {
0248   public:
0249     explicit ShortcutGroup(const QString& ctx);
0250 
0251     QString context() const { return m_context; }
0252 
0253   private:
0254     QString m_context;
0255   };
0256 
0257   const ShortcutGroup* shortcutGroupForIndex(const QModelIndex& index) const;
0258 
0259   QList<ShortcutGroup> m_shortcutGroups;
0260 };