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

0001 /**
0002  * \file frameitemdelegate.h
0003  * Delegate for table widget items.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 01 May 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 GenreModel;
0032 
0033 /**
0034  * Delegate for table widget items.
0035  */
0036 class FrameItemDelegate : public QItemDelegate {
0037   Q_OBJECT
0038 public:
0039   /**
0040    * Constructor.
0041    * @param genreModel genre model
0042    * @param parent parent QTableView
0043    */
0044   explicit FrameItemDelegate(GenreModel* genreModel, QObject* parent = nullptr);
0045 
0046   /**
0047    * Destructor.
0048    */
0049   ~FrameItemDelegate() override = default;
0050 
0051   /**
0052    * Render delegate.
0053    * @param painter painter to be used
0054    * @param option style
0055    * @param index index of item
0056    */
0057   void paint(QPainter* painter, const QStyleOptionViewItem& option,
0058              const QModelIndex& index) const override;
0059 
0060   /**
0061    * Get size needed by delegate.
0062    * @param option style
0063    * @param index  index of item
0064    * @return size needed by delegate.
0065    */
0066   QSize sizeHint(const QStyleOptionViewItem& option,
0067                  const QModelIndex& index) const override;
0068 
0069   /**
0070    * Create an editor to edit the cells contents.
0071    * @param parent parent widget
0072    * @param option style
0073    * @param index  index of item
0074    * @return combo box editor widget.
0075    */
0076   QWidget* createEditor(
0077     QWidget* parent, const QStyleOptionViewItem& option,
0078     const QModelIndex& index) const override;
0079 
0080   /**
0081    * Set data to be edited by the editor.
0082    * @param editor editor widget
0083    * @param index  index of item
0084    */
0085   void setEditorData(QWidget* editor, const QModelIndex& index) const override;
0086 
0087   /**
0088    * Set model data supplied by editor.
0089    * @param editor editor widget
0090    * @param model  model
0091    * @param index  index of item
0092    */
0093   void setModelData(
0094     QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
0095 
0096 private slots:
0097   /**
0098    * Format text if enabled.
0099    * @param txt text to format and set in line edit
0100    */
0101   void formatTextIfEnabled(const QString& txt);
0102 
0103   /**
0104    * Commit data and close the editor.
0105    */
0106   void commitAndCloseEditor();
0107 
0108 private:
0109   GenreModel* m_genreModel;
0110   QValidator* m_trackNumberValidator;
0111   QValidator* m_dateTimeValidator;
0112 };
0113 
0114 /**
0115  * Editor for star rating.
0116  */
0117 class StarEditor : public QWidget {
0118   Q_OBJECT
0119 public:
0120   /**
0121    * Constructor.
0122    * @param parent parent widget
0123    */
0124   explicit StarEditor(QWidget* parent = nullptr);
0125 
0126   /**
0127    * Get size needed by editor.
0128    * @return size needed by editor.
0129    */
0130   QSize sizeHint() const override;
0131 
0132   /**
0133    * Set star rating.
0134    * @param starCount number of stars
0135    */
0136   void setStarCount(int starCount);
0137 
0138   /**
0139    * Get star rating.
0140    * @return number of stars.
0141    */
0142   int starCount() const { return m_starCount; }
0143 
0144   /**
0145    * Check if the star rating has been modified in the editor since it has been
0146    * set using setStarCount().
0147    * @return true if rating modified in editor.
0148    */
0149   bool isStarCountEdited() const { return m_starCountEdited; }
0150 
0151 signals:
0152   /**
0153    * Emitted when editing is finished.
0154    */
0155   void editingFinished();
0156 
0157 protected:
0158   /**
0159    * Called when widget is painted.
0160    * @param event paint event
0161    */
0162   void paintEvent(QPaintEvent* event) override;
0163 
0164   /**
0165    * Called when the mouse is moved inside the widget.
0166    * @param event mouse event
0167    */
0168   void mouseMoveEvent(QMouseEvent* event) override;
0169 
0170   /**
0171    * Called when the mouse is released inside the widget.
0172    * @param event mouse event
0173    */
0174   void mouseReleaseEvent(QMouseEvent* event) override;
0175 
0176   /**
0177    * Called when a key is pressed while the widget has focus.
0178    * @param event key event
0179    */
0180   void keyPressEvent(QKeyEvent* event) override;
0181 
0182 private:
0183   int starAtPosition(int x);
0184   void modifyStarCount(int starCount);
0185 
0186   int m_starCount;
0187   int m_paintedStarCount;
0188   bool m_starCountEdited;
0189 };