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

0001 /**
0002  * \file enumdelegate.h
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 #pragma once
0028 
0029 #include <QItemDelegate>
0030 
0031 /**
0032  * Abstract base class for delegates which display enums in a combobox.
0033  */
0034 class EnumDelegate : public QItemDelegate {
0035 public:
0036   /**
0037    * Constructor.
0038    * @param parent parent object
0039    */
0040   explicit EnumDelegate(QObject* parent = nullptr);
0041 
0042   /**
0043    * Destructor.
0044    */
0045   ~EnumDelegate() override = default;
0046 
0047   /**
0048    * Create an editor to edit the cells contents.
0049    * @param parent parent widget
0050    * @param option style
0051    * @param index  index of item
0052    * @return combo box editor widget.
0053    */
0054   QWidget* createEditor(
0055     QWidget* parent, const QStyleOptionViewItem& option,
0056     const QModelIndex& index) const override;
0057 
0058   /**
0059    * Set data to be edited by the editor.
0060    * @param editor editor widget
0061    * @param index  index of item
0062    */
0063   void setEditorData(QWidget* editor, const QModelIndex& index) const override;
0064 
0065   /**
0066    * Set model data supplied by editor.
0067    * @param editor editor widget
0068    * @param model  model
0069    * @param index  index of item
0070    */
0071   void setModelData(
0072     QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
0073 
0074   /**
0075    * Get size needed by delegate.
0076    * @param option style
0077    * @param index  index of item
0078    * @return size needed by delegate.
0079    */
0080   QSize sizeHint(const QStyleOptionViewItem& option,
0081                  const QModelIndex& index) const override;
0082 
0083 protected:
0084   /**
0085    * Render item view text.
0086    * @param painter painter
0087    * @param option style
0088    * @param rect the text has to be rendered within this rectangle
0089    * @param text to be rendered
0090    */
0091   void drawDisplay(
0092     QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect,
0093     const QString& text) const override;
0094 
0095   /**
0096    * Get list of string representations for an enum.
0097    * @return string for enum with index 0, string for enum with index 1, ...
0098    */
0099   virtual QStringList getEnumStrings() const = 0;
0100 
0101   /**
0102    * Get string representation for an enum value.
0103    * @param enumNr the enum value as an integer
0104    * @return string representation
0105    */
0106   virtual QString getStringForEnum(int enumNr) const = 0;
0107 
0108   /**
0109    * Get index of an enum value.
0110    * @param enumNr the enum value as an integer
0111    * @return index of this enum value in the list returned by getEnumStrings()
0112    */
0113   virtual int getIndexForEnum(int enumNr) const = 0;
0114 
0115   /**
0116    * Get enum value for an index.
0117    * @param index index of enum in list returned by getEnumStrings()
0118    * @return corresponding enum value as an integer
0119    */
0120   virtual int getEnumForIndex(int index) const = 0;
0121 };