File indexing completed on 2025-02-23 04:34:23

0001 /**
0002  * \file enumdelegate.cpp
0003  * Abstract base class for delegates which display enums in a combobox.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 14 Mar 2014
0008  *
0009  * Copyright (C) 2014-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 "enumdelegate.h"
0028 #include <QComboBox>
0029 #include <QFontMetrics>
0030 
0031 /**
0032  * Constructor.
0033  * @param parent parent object
0034  */
0035 EnumDelegate::EnumDelegate(QObject* parent) : QItemDelegate(parent)
0036 {
0037 }
0038 
0039 /**
0040  * Create an editor to edit the cells contents.
0041  * @param parent parent widget
0042  * @return combo box editor widget.
0043  */
0044 QWidget* EnumDelegate::createEditor(
0045   QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
0046 {
0047   auto cb = new QComboBox(parent);
0048   cb->addItems(getEnumStrings());
0049   return cb;
0050 }
0051 
0052 /**
0053  * Set data to be edited by the editor.
0054  * @param editor editor widget
0055  * @param index  index of item
0056  */
0057 void EnumDelegate::setEditorData(
0058   QWidget* editor, const QModelIndex& index) const
0059 {
0060   auto cb = qobject_cast<QComboBox*>(editor);
0061   if (int enumNr = index.data(Qt::EditRole).toInt(); cb && enumNr >= 0) {
0062     cb->setCurrentIndex(getIndexForEnum(enumNr));
0063   } else {
0064     QItemDelegate::setEditorData(editor, index);
0065   }
0066 }
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 EnumDelegate::setModelData(
0075   QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
0076 {
0077   if (auto cb = qobject_cast<QComboBox*>(editor)) {
0078     if (int enumNr = getEnumForIndex(cb->currentIndex()); enumNr >= 0) {
0079       model->setData(index, enumNr, Qt::EditRole);
0080       return;
0081     }
0082   }
0083   QItemDelegate::setModelData(editor, model, index);
0084 }
0085 
0086 /**
0087  * Get size needed by delegate.
0088  * @param option style
0089  * @param index  index of item
0090  * @return size needed by delegate.
0091  */
0092 QSize EnumDelegate::sizeHint(const QStyleOptionViewItem& option,
0093                                   const QModelIndex& index) const
0094 {
0095   QSize size(QItemDelegate::sizeHint(option, index));
0096   bool ok;
0097   int enumNr = index.data(Qt::EditRole).toInt(&ok);
0098   if (ok) {
0099     QFont fnt(qvariant_cast<QFont>(index.data(Qt::FontRole))
0100               .resolve(option.font));
0101     QFontMetrics fm(fnt);
0102 #if QT_VERSION >= 0x050b00
0103     int origWidth = fm.horizontalAdvance(QString::number(enumNr));
0104     int delegateWidth = fm.horizontalAdvance(getStringForEnum(enumNr));
0105 #else
0106     int origWidth = fm.width(QString::number(enumNr));
0107     int delegateWidth = fm.width(getStringForEnum(enumNr));
0108 #endif
0109     size.setWidth(size.width() + delegateWidth - origWidth);
0110   }
0111   return size;
0112 }
0113 
0114 /**
0115  * Render item view text.
0116  * @param painter painter
0117  * @param option style
0118  * @param rect the text has to be rendered within this rectangle
0119  * @param text to be rendered
0120  */
0121 void EnumDelegate::drawDisplay(
0122   QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect,
0123   const QString& text) const
0124 {
0125   bool ok;
0126   int enumNr = text.toInt(&ok);
0127   QItemDelegate::drawDisplay(painter, option, rect,
0128                              ok ? getStringForEnum(enumNr) : text);
0129 }