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

0001 /**
0002  * \file shortcutsdelegate.cpp
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 #include "shortcutsdelegate.h"
0028 
0029 #include <QToolButton>
0030 #include <QLineEdit>
0031 #include <QHBoxLayout>
0032 #include <QKeyEvent>
0033 #include <QKeySequenceEdit>
0034 
0035 /**
0036  * Constructor.
0037  * @param parent parent object
0038  */
0039 ShortcutsDelegate::ShortcutsDelegate(QObject* parent)
0040   : QItemDelegate(parent), m_resetFlag(false)
0041 {
0042 }
0043 
0044 /**
0045  * Create an editor to edit the cells contents.
0046  * @param parent parent widget
0047  * @param option style
0048  * @param index  index of item
0049  * @return editor widget
0050  */
0051 QWidget* ShortcutsDelegate::createEditor(
0052   QWidget* parent, const QStyleOptionViewItem& option,
0053   const QModelIndex& index) const
0054 {
0055   QWidget* editor = QItemDelegate::createEditor(parent, option, index);
0056   if (auto le = qobject_cast<QLineEdit*>(editor)) {
0057     auto shortcutsEditor = new ShortcutsDelegateEditor(le, parent);
0058     editor = shortcutsEditor;
0059     connect(shortcutsEditor, &ShortcutsDelegateEditor::clearClicked,
0060             this, &ShortcutsDelegate::clearAndCloseEditor);
0061     connect(shortcutsEditor, &ShortcutsDelegateEditor::clearClicked,
0062             this, &ShortcutsDelegate::clearAndCloseEditor);
0063     connect(shortcutsEditor, &ShortcutsDelegateEditor::resetClicked,
0064             this, &ShortcutsDelegate::resetToDefault);
0065     connect(shortcutsEditor, &ShortcutsDelegateEditor::valueEntered,
0066             this, &ShortcutsDelegate::commitAndCloseEditor);
0067   }
0068   return editor;
0069 }
0070 
0071 /**
0072  * Reset editor to default value.
0073  */
0074 void ShortcutsDelegate::resetToDefault()
0075 {
0076   if (auto editor =
0077       qobject_cast<ShortcutsDelegateEditor*>(sender())) {
0078     m_resetFlag = true;
0079     emit commitData(editor);
0080     emit closeEditor(editor);
0081   }
0082 }
0083 
0084 /**
0085  * Commit editor value and close editor.
0086  */
0087 void ShortcutsDelegate::commitAndCloseEditor()
0088 {
0089   if (auto editor =
0090       qobject_cast<ShortcutsDelegateEditor*>(sender())) {
0091     emit commitData(editor);
0092     emit closeEditor(editor);
0093   }
0094 }
0095 
0096 /**
0097  * Clear editor value and close editor.
0098  */
0099 void ShortcutsDelegate::clearAndCloseEditor()
0100 {
0101   if (auto editor =
0102       qobject_cast<ShortcutsDelegateEditor*>(sender())) {
0103     editor->getEditor()->clear();
0104     emit commitData(editor);
0105     emit closeEditor(editor);
0106   }
0107 }
0108 
0109 /**
0110  * Set data to be edited by the editor.
0111  * @param editor editor widget
0112  * @param index  index of item
0113  */
0114 void ShortcutsDelegate::setEditorData(
0115   QWidget* editor, const QModelIndex& index) const
0116 {
0117   if (auto compoundWidget =
0118       qobject_cast<ShortcutsDelegateEditor*>(editor)) {
0119     QItemDelegate::setEditorData(compoundWidget->getEditor(), index);
0120   }
0121 }
0122 
0123 /**
0124  * Set model data supplied by editor.
0125  * @param editor editor widget
0126  * @param model  model
0127  * @param index  index of item
0128  */
0129 void ShortcutsDelegate::setModelData(
0130   QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
0131 {
0132   if (auto compoundWidget =
0133       qobject_cast<ShortcutsDelegateEditor*>(editor)) {
0134     if (m_resetFlag) {
0135       m_resetFlag = false;
0136       model->setData(index, QVariant(), Qt::EditRole);
0137     } else {
0138       QItemDelegate::setModelData(compoundWidget->getEditor(), model, index);
0139     }
0140   }
0141 }
0142 
0143 /**
0144  * Updates the geometry of the @a editor for the item with the given
0145  * @a index, according to the rectangle specified in the @a option.
0146  * @param editor editor widget
0147  * @param option style
0148  */
0149 void ShortcutsDelegate::updateEditorGeometry(
0150   QWidget* editor, const QStyleOptionViewItem& option,
0151   const QModelIndex&) const
0152 {
0153   // If this method is not overridden, the ShortcutsDelegateEditor
0154   // is displayed as a thin horizontal line, see also
0155   // http://stackoverflow.com/questions/5457154/qstyleditemdelegate-how-does-updateeditorgeometry-works
0156   QRect rect(option.rect);
0157   QSize sizeHint(editor->sizeHint());
0158 
0159   if (rect.width() < sizeHint.width()) {
0160     rect.setWidth(sizeHint.width());
0161   }
0162   if (rect.height() < sizeHint.height()) {
0163     int yAdj = (rect.height() - sizeHint.height()) / 2;
0164     rect.setHeight(sizeHint.height());
0165     rect.translate(0, yAdj);
0166   }
0167 
0168   editor->setGeometry(rect);
0169 }
0170 
0171 
0172 /**
0173  * Constructor.
0174  *
0175  * @param parent parent widget
0176  */
0177 ShortcutsDelegateEditor::ShortcutsDelegateEditor(
0178   QLineEdit* lineEdit, QWidget* parent)
0179   : QFrame(parent) {
0180   auto hlayout = new QHBoxLayout(this);
0181   hlayout->setContentsMargins(0, 0, 0, 0);
0182   delete lineEdit;
0183   m_editor = new QKeySequenceEdit(parent);
0184   connect(m_editor, &QKeySequenceEdit::editingFinished,
0185           this, &ShortcutsDelegateEditor::valueEntered);
0186   setFocusProxy(m_editor);
0187   hlayout->addWidget(m_editor, 0, Qt::AlignLeft);
0188   auto clearButton = new QToolButton(this);
0189   clearButton->setText(tr("Clear"));
0190   connect(clearButton, &QAbstractButton::clicked,
0191           this, &ShortcutsDelegateEditor::clearClicked);
0192   hlayout->addWidget(clearButton);
0193   auto resetButton = new QToolButton(this);
0194   resetButton->setText(tr("Reset"));
0195   connect(resetButton, &QAbstractButton::clicked,
0196           this, &ShortcutsDelegateEditor::resetClicked);
0197   hlayout->addWidget(resetButton);
0198 }