File indexing completed on 2024-05-12 04:55:02

0001 /**
0002  * \file shortcutsdelegate.h
0003  * Keyboard shortcuts item delegate.
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 <QItemDelegate>
0030 
0031 class QKeySequenceEdit;
0032 
0033 /**
0034  * Item delegate to edit and reset keyboard shortcuts.
0035  */
0036 class ShortcutsDelegate : public QItemDelegate {
0037   Q_OBJECT
0038 public:
0039   /**
0040    * Constructor.
0041    * @param parent parent object
0042    */
0043   explicit ShortcutsDelegate(QObject* parent = nullptr);
0044 
0045   /**
0046    * Destructor.
0047    */
0048   ~ShortcutsDelegate() override = default;
0049 
0050   /**
0051    * Create an editor to edit the cells contents.
0052    * @param parent parent widget
0053    * @param option style
0054    * @param index  index of item
0055    * @return editor widget
0056    */
0057   QWidget* createEditor(
0058     QWidget* parent, const QStyleOptionViewItem& option,
0059     const QModelIndex& index) const override;
0060 
0061   /**
0062    * Set data to be edited by the editor.
0063    * @param editor editor widget
0064    * @param index  index of item
0065    */
0066   void setEditorData(QWidget* editor, const QModelIndex& index) const override;
0067 
0068   /**
0069    * Set model data supplied by editor.
0070    * @param editor editor widget
0071    * @param model  model
0072    * @param index  index of item
0073    */
0074   void setModelData(
0075     QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
0076 
0077   /**
0078    * Updates the geometry of the @a editor for the item with the given
0079    * @a index, according to the rectangle specified in the @a option.
0080    * @param editor editor widget
0081    * @param option style
0082    * @param index  index of item
0083    */
0084   void updateEditorGeometry(
0085     QWidget* editor, const QStyleOptionViewItem& option,
0086       const QModelIndex& index) const override;
0087 
0088 private slots:
0089   void clearAndCloseEditor();
0090   void resetToDefault();
0091   void commitAndCloseEditor();
0092 
0093 private:
0094   mutable bool m_resetFlag;
0095 };
0096 
0097 /**
0098  * Editor widget for delegate with buttons to clear and reset the value.
0099  *
0100  * The editor consists of a line edit to edit the value and buttons to clear and
0101  * reset the value to the default.
0102  */
0103 class ShortcutsDelegateEditor : public QFrame {
0104   Q_OBJECT
0105 public:
0106   /**
0107    * Constructor.
0108    *
0109    * @param lineEdit widget used to edit value
0110    * @param parent parent widget
0111    */
0112   explicit ShortcutsDelegateEditor(QLineEdit* lineEdit,
0113                                    QWidget* parent = nullptr);
0114 
0115   /**
0116    * Destructor.
0117    */
0118   ~ShortcutsDelegateEditor() override = default;
0119 
0120   /*!
0121    * Get edit widget.
0122    * @return editor widget
0123    */
0124   QKeySequenceEdit* getEditor() { return m_editor; }
0125 
0126 signals:
0127   /**
0128    * Emitted when a shortcut has been entered.
0129    */
0130   void valueEntered();
0131 
0132   /**
0133    * Emitted when the clear button is clicked.
0134    */
0135   void clearClicked();
0136 
0137   /**
0138    * Emitted when the reset button is clicked.
0139    */
0140   void resetClicked();
0141 
0142 private:
0143   QKeySequenceEdit* m_editor;
0144 };