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

0001 /**
0002  * \file configtablemodel.h
0003  * Model for table with context menu to add and remove rows.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 13 Mar 2011
0008  *
0009  * Copyright (C) 2005-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 <QStringList>
0031 #include <QPair>
0032 #include <QMap>
0033 #include "kid3api.h"
0034 
0035 enum class ModelSectionResizeMode : int;
0036 
0037 /**
0038  * Context menu commands configuration table model.
0039  */
0040 class KID3_CORE_EXPORT ConfigTableModel : public QAbstractTableModel {
0041 public:
0042   /**
0043    * Constructor.
0044    * @param parent parent widget
0045    */
0046   explicit ConfigTableModel(QObject* parent = nullptr);
0047 
0048   /**
0049    * Destructor.
0050    */
0051   ~ConfigTableModel() override = default;
0052 
0053   /**
0054    * Get item flags for index.
0055    * @param index model index
0056    * @return item flags
0057    */
0058   Qt::ItemFlags flags(const QModelIndex& index) const override;
0059 
0060   /**
0061    * Get data for a given role.
0062    * @param index model index
0063    * @param role item data role
0064    * @return data for role
0065    */
0066   QVariant data(const QModelIndex& index,
0067                 int role = Qt::DisplayRole) const override;
0068 
0069   /**
0070    * Set data for a given role.
0071    * @param index model index
0072    * @param value data value
0073    * @param role item data role
0074    * @return true if successful
0075    */
0076   bool setData(const QModelIndex& index, const QVariant& value,
0077                int role = Qt::EditRole) override;
0078 
0079   /**
0080    * Get data for header section.
0081    * @param section column or row
0082    * @param orientation horizontal or vertical
0083    * @param role item data role
0084    * @return header data for role
0085    */
0086   QVariant headerData(int section, Qt::Orientation orientation,
0087                       int role = Qt::DisplayRole) const override;
0088 
0089   /**
0090    * Set data for header section.
0091    * Not supported.
0092    * @return false
0093    */
0094   bool setHeaderData(int, Qt::Orientation, const QVariant&,
0095                      int = Qt::EditRole) override { return false; }
0096 
0097   /**
0098    * Get number of rows.
0099    * @param parent parent model index, invalid for table models
0100    * @return number of rows,
0101    * if parent is valid number of children (0 for table models)
0102    */
0103   int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0104 
0105   /**
0106    * Get number of columns.
0107    * @param parent parent model index, invalid for table models
0108    * @return number of columns,
0109    * if parent is valid number of children (0 for table models)
0110    */
0111   int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0112 
0113   /**
0114    * Insert rows.
0115    * @param row rows are inserted before this row, if 0 at the begin,
0116    * if rowCount() at the end
0117    * @param count number of rows to insert
0118    * @param parent parent model index, invalid for table models
0119    * @return true if successful
0120    */
0121   bool insertRows(int row, int count,
0122                   const QModelIndex& parent = QModelIndex()) override;
0123 
0124   /**
0125    * Remove rows.
0126    * @param row rows are removed starting with this row
0127    * @param count number of rows to remove
0128    * @param parent parent model index, invalid for table models
0129    * @return true if successful
0130    */
0131   bool removeRows(int row, int count,
0132                   const QModelIndex& parent = QModelIndex()) override;
0133 
0134   /**
0135    * Get the resize modes to be used for the columns.
0136    * @return list of resize modes for the columns
0137    */
0138   QList<ModelSectionResizeMode> getHorizontalResizeModes() const;
0139 
0140   /**
0141    * Set the column labels.
0142    * @param labels column labels
0143    */
0144   void setLabels(const QStringList& labels);
0145 
0146  /**
0147    * Set the model from a map.
0148    * @param map list with keys and values
0149    */
0150   void setMap(const QList<QPair<QString, QString>>& map);
0151 
0152   /**
0153    * Get map from the model.
0154    * @return list with keys and values
0155    */
0156   QList<QPair<QString, QString>> getMap() const;
0157 
0158 private:
0159   QStringList m_labels;
0160   QList<QPair<QString, QString> > m_keyValues;
0161 };