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

0001 /**
0002  * \file configtablemodel.cpp
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 #include "configtablemodel.h"
0028 #include "modelsectionresizemode.h"
0029 
0030 /**
0031  * Constructor.
0032  * @param parent parent widget
0033  */
0034 ConfigTableModel::ConfigTableModel(QObject* parent)
0035   : QAbstractTableModel(parent)
0036 {
0037   setObjectName(QLatin1String("ConfigTableModel"));
0038 }
0039 
0040 /**
0041  * Get item flags for index.
0042  * @param index model index
0043  * @return item flags
0044  */
0045 Qt::ItemFlags ConfigTableModel::flags(const QModelIndex& index) const
0046 {
0047   Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
0048   if (index.isValid())
0049     theFlags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
0050   return theFlags;
0051 }
0052 
0053 /**
0054  * Get data for a given role.
0055  * @param index model index
0056  * @param role item data role
0057  * @return data for role
0058  */
0059 QVariant ConfigTableModel::data(const QModelIndex& index, int role) const
0060 {
0061   if (!index.isValid() ||
0062       index.row() < 0 || index.row() >= m_keyValues.size() ||
0063       index.column() < 0 || index.column() >= 2)
0064     return QVariant();
0065   const QPair<QString, QString>& keyValue = m_keyValues.at(index.row());
0066   if (role == Qt::DisplayRole || role == Qt::EditRole) {
0067     if (index.column() == 0)
0068       return keyValue.first;
0069     return keyValue.second;
0070   }
0071   return QVariant();
0072 }
0073 
0074 /**
0075  * Set data for a given role.
0076  * @param index model index
0077  * @param value data value
0078  * @param role item data role
0079  * @return true if successful
0080  */
0081 bool ConfigTableModel::setData(const QModelIndex& index,
0082                                  const QVariant& value, int role)
0083 {
0084   if (!index.isValid() || role != Qt::EditRole ||
0085       index.row() < 0 || index.row() >= m_keyValues.size() ||
0086       index.column() < 0 || index.column() >= 2)
0087     return false;
0088   QPair<QString, QString>& keyValue = m_keyValues[index.row()]; // clazy:exclude=detaching-member
0089   if (index.column() == 0) {
0090     keyValue.first = value.toString();
0091   } else {
0092     keyValue.second = value.toString();
0093   }
0094   emit dataChanged(index, index);
0095   return true;
0096 }
0097 
0098 /**
0099  * Get data for header section.
0100  * @param section column or row
0101  * @param orientation horizontal or vertical
0102  * @param role item data role
0103  * @return header data for role
0104  */
0105 QVariant ConfigTableModel::headerData(
0106     int section, Qt::Orientation orientation, int role) const
0107 {
0108   if (role != Qt::DisplayRole)
0109     return QVariant();
0110   if (orientation == Qt::Horizontal && section < m_labels.size()) {
0111     return m_labels[section];
0112   }
0113   return section + 1;
0114 }
0115 
0116 /**
0117  * Get number of rows.
0118  * @param parent parent model index, invalid for table models
0119  * @return number of rows,
0120  * if parent is valid number of children (0 for table models)
0121  */
0122 int ConfigTableModel::rowCount(const QModelIndex& parent) const
0123 {
0124   return parent.isValid() ? 0 : m_keyValues.size();
0125 }
0126 
0127 /**
0128  * Get number of columns.
0129  * @param parent parent model index, invalid for table models
0130  * @return number of columns,
0131  * if parent is valid number of children (0 for table models)
0132  */
0133 int ConfigTableModel::columnCount(const QModelIndex& parent) const
0134 {
0135   return parent.isValid() ? 0 : 2;
0136 }
0137 
0138 /**
0139  * Insert rows.
0140  * @param row rows are inserted before this row, if 0 at the begin,
0141  * if rowCount() at the end
0142  * @param count number of rows to insert
0143  * @return true if successful
0144  */
0145 bool ConfigTableModel::insertRows(int row, int count,
0146                         const QModelIndex&)
0147 {
0148   if (count > 0) {
0149     beginInsertRows(QModelIndex(), row, row + count - 1);
0150     for (int i = 0; i < count; ++i)
0151       m_keyValues.insert(row, QPair<QString, QString>());
0152     endInsertRows();
0153   }
0154   return true;
0155 }
0156 
0157 /**
0158  * Remove rows.
0159  * @param row rows are removed starting with this row
0160  * @param count number of rows to remove
0161  * @return true if successful
0162  */
0163 bool ConfigTableModel::removeRows(int row, int count,
0164                         const QModelIndex&)
0165 {
0166   if (count > 0) {
0167     beginRemoveRows(QModelIndex(), row, row + count - 1);
0168     for (int i = 0; i < count; ++i)
0169       m_keyValues.removeAt(row);
0170     endRemoveRows();
0171   }
0172   return true;
0173 }
0174 
0175 /**
0176  * Get the resize modes to be used for the columns.
0177  * @return list of resize modes for the columns
0178  */
0179 QList<ModelSectionResizeMode>
0180     ConfigTableModel::getHorizontalResizeModes() const
0181 {
0182   return {ModelSectionResizeMode::Stretch, ModelSectionResizeMode::Stretch};
0183 }
0184 
0185 /**
0186  * Set the column labels.
0187  * @param labels column labels
0188  */
0189 void ConfigTableModel::setLabels(const QStringList& labels)
0190 {
0191   beginResetModel();
0192   m_labels = labels;
0193   endResetModel();
0194 }
0195 
0196 /**
0197  * Set the model from a map.
0198  *
0199  * @param map list with keys and values
0200  */
0201 void ConfigTableModel::setMap(const QList<QPair<QString, QString>>& map)
0202 {
0203   beginResetModel();
0204   m_keyValues = map;
0205   // make sure that at least one line is in the table
0206   if (m_keyValues.isEmpty())
0207     m_keyValues.append({QString(), QString()});
0208   endResetModel();
0209 }
0210 
0211 /**
0212  * Get map from the model.
0213  * @return list with keys and values
0214  */
0215 QList<QPair<QString, QString>> ConfigTableModel::getMap() const
0216 {
0217   return m_keyValues;
0218 }