File indexing completed on 2024-05-19 04:56:08

0001 /**
0002  * \file standardtablemodel.h
0003  * Table model with containing values for multiple roles.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 12 Jul 2019
0008  *
0009  * Copyright (C) 2019-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 <QAbstractTableModel>
0030 #include "kid3api.h"
0031 
0032 /**
0033  * Table model with containing values for multiple roles.
0034  */
0035 class KID3_CORE_EXPORT StandardTableModel : public QAbstractTableModel {
0036   Q_OBJECT
0037 public:
0038   /**
0039    * Constructor.
0040    * @param parent parent widget
0041    */
0042   explicit StandardTableModel(QObject* parent = nullptr);
0043 
0044   /**
0045    * Get item flags for index.
0046    * @param index model index
0047    * @return item flags
0048    */
0049   Qt::ItemFlags flags(const QModelIndex& index) const override;
0050 
0051   /**
0052    * Get data for header section.
0053    * @param section column or row
0054    * @param orientation horizontal or vertical
0055    * @param role item data role
0056    * @return header data for role
0057    */
0058   QVariant headerData(int section, Qt::Orientation orientation,
0059                       int role = Qt::DisplayRole) const override;
0060 
0061   /**
0062    * Set data for header section.
0063    * @param section column or row
0064    * @param orientation horizontal or vertical
0065    * @param value header data
0066    * @param role item data role
0067    * @return true if ok.
0068    */
0069   bool setHeaderData(int section, Qt::Orientation orientation,
0070                      const QVariant& value,
0071                      int role = Qt::EditRole) override;
0072 
0073   /**
0074    * Get supported drop actions.
0075    * @return supported drop actions.
0076    */
0077   Qt::DropActions supportedDropActions() const override;
0078 
0079   /**
0080    * Get number of rows.
0081    * @param parent parent model index
0082    * @return number of rows, if parent is valid number of children
0083    */
0084   int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0085 
0086   /**
0087    * Get number of columns.
0088    * @param parent parent model index
0089    * @return number of columns for children of given \a parent
0090    */
0091   int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0092 
0093   /**
0094    * Get data for a given role.
0095    * @param index model index
0096    * @param role item data role
0097    * @return data for role
0098    */
0099   QVariant data(const QModelIndex& index,
0100                 int role = Qt::DisplayRole) const override;
0101 
0102   /**
0103    * Set data for a given role.
0104    * @param index model index
0105    * @param value data value
0106    * @param role item data role
0107    * @return true if successful
0108    */
0109   bool setData(const QModelIndex& index, const QVariant& value,
0110                int role = Qt::EditRole) override;
0111 
0112   /**
0113    * Insert rows.
0114    * @param row rows are inserted before this row, if 0 at the begin,
0115    * if rowCount() at the end
0116    * @param count number of rows to insert
0117    * @param parent parent model index, invalid for table models
0118    * @return true if successful
0119    */
0120   bool insertRows(int row, int count,
0121                   const QModelIndex& parent = QModelIndex()) override;
0122 
0123   /**
0124    * Remove rows.
0125    * @param row rows are removed starting with this row
0126    * @param count number of rows to remove
0127    * @param parent parent model index, invalid for table models
0128    * @return true if successful
0129    */
0130   bool removeRows(int row, int count,
0131                   const QModelIndex& parent = QModelIndex()) override;
0132 
0133   /**
0134    * Set number of columns.
0135    * @param columns number of columns, default is 1
0136    */
0137   void setColumnCount(int columns);
0138 
0139   /**
0140    * Set horizontal header labels.
0141    * @param labels header data
0142    */
0143   void setHorizontalHeaderLabels(const QStringList& labels);
0144 
0145   /**
0146    * Clear all rows.
0147    * The number of columns and the header data are not affected.
0148    */
0149   void clear();
0150 
0151 private:
0152   QVector<QString> m_horizontalHeaderLabels;
0153   QVector<QVector<QMap<int, QVariant>>> m_cont;
0154   int m_numColumns;
0155 };