File indexing completed on 2024-05-19 04:55:53

0001 /**
0002  * \file batchimportsourcesmodel.cpp
0003  * Context menu commands configuration table model.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 2 Jan 2013
0008  *
0009  * Copyright (C) 2013-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 "batchimportsourcesmodel.h"
0028 
0029 /** Column indices. */
0030 enum ColumnIndex {
0031   CI_Name,
0032   CI_Accuracy,
0033   CI_StandardTags,
0034   CI_AdditionalTags,
0035   CI_CoverArt,
0036   CI_NumColumns
0037 };
0038 
0039 /**
0040  * Constructor.
0041  * @param parent parent widget
0042  */
0043 BatchImportSourcesModel::BatchImportSourcesModel(QObject* parent)
0044   : QAbstractTableModel(parent)
0045 {
0046   setObjectName(QLatin1String("BatchImportSourcesModel"));
0047 }
0048 
0049 /**
0050  * Get item flags for index.
0051  * @param index model index
0052  * @return item flags
0053  */
0054 Qt::ItemFlags BatchImportSourcesModel::flags(const QModelIndex& index) const
0055 {
0056   Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
0057   if (index.isValid()) {
0058     theFlags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0059     if (index.column() == CI_StandardTags ||
0060         index.column() == CI_AdditionalTags ||
0061         index.column() == CI_CoverArt) {
0062       theFlags |= Qt::ItemIsUserCheckable;
0063     }
0064   }
0065   return theFlags;
0066 }
0067 
0068 /**
0069  * Get data for a given role.
0070  * @param index model index
0071  * @param role item data role
0072  * @return data for role
0073  */
0074 QVariant BatchImportSourcesModel::data(const QModelIndex& index, int role) const
0075 {
0076   if (!index.isValid() ||
0077       index.row() < 0 || index.row() >= m_sources.size() ||
0078       index.column() < 0 || index.column() >= CI_NumColumns)
0079     return QVariant();
0080   const BatchImportProfile::Source& item = m_sources.at(index.row());
0081   if (role == Qt::DisplayRole || role == Qt::EditRole) {
0082     switch (index.column()) {
0083     case CI_Name:
0084       return item.getName();
0085     case CI_Accuracy:
0086       return item.getRequiredAccuracy();
0087     default: ;
0088     }
0089   }
0090   if (role == Qt::CheckStateRole) {
0091     switch (index.column()) {
0092     case CI_StandardTags:
0093       return item.standardTagsEnabled() ? Qt::Checked : Qt::Unchecked;
0094     case CI_AdditionalTags:
0095       return item.additionalTagsEnabled() ? Qt::Checked : Qt::Unchecked;
0096     case CI_CoverArt:
0097       return item.coverArtEnabled() ? Qt::Checked : Qt::Unchecked;
0098     default: ;
0099     }
0100   }
0101   return QVariant();
0102 }
0103 
0104 /**
0105  * Set data for a given role.
0106  * @param index model index
0107  * @param value data value
0108  * @param role item data role
0109  * @return true if successful
0110  */
0111 bool BatchImportSourcesModel::setData(const QModelIndex& index,
0112                                       const QVariant& value, int role)
0113 {
0114   if (!index.isValid() ||
0115       index.row() < 0 || index.row() >= m_sources.size() ||
0116       index.column() < 0 || index.column() >= CI_NumColumns)
0117     return false;
0118   BatchImportProfile::Source& item = m_sources[index.row()]; // clazy:exclude=detaching-member
0119   if (role == Qt::EditRole) {
0120     switch (index.column()) {
0121     case CI_Name:
0122       item.setName(value.toString());
0123       break;
0124     case CI_Accuracy:
0125       item.setRequiredAccuracy(value.toInt());
0126       break;
0127     default:
0128       return false;
0129     }
0130   } else if (role == Qt::CheckStateRole) {
0131     switch (index.column()) {
0132     case CI_StandardTags:
0133       item.enableStandardTags(value.toInt() == Qt::Checked);
0134       break;
0135     case CI_AdditionalTags:
0136       item.enableAdditionalTags(value.toInt() == Qt::Checked);
0137       break;
0138     case CI_CoverArt:
0139       item.enableCoverArt(value.toInt() == Qt::Checked);
0140       break;
0141     default:
0142       return false;
0143     }
0144   } else {
0145     return false;
0146   }
0147   emit dataChanged(index, index);
0148   return true;
0149 }
0150 
0151 /**
0152  * Get data for header section.
0153  * @param section column or row
0154  * @param orientation horizontal or vertical
0155  * @param role item data role
0156  * @return header data for role
0157  */
0158 QVariant BatchImportSourcesModel::headerData(
0159     int section, Qt::Orientation orientation, int role) const
0160 {
0161   if (role != Qt::DisplayRole)
0162     return QVariant();
0163   if (orientation == Qt::Horizontal) {
0164     switch (section) {
0165     case CI_Name:
0166       return tr("Server");
0167     case CI_Accuracy:
0168       return tr("Accuracy");
0169     case CI_StandardTags:
0170       return tr("Standard Tags");
0171     case CI_AdditionalTags:
0172       return tr("Additional Tags");
0173     case CI_CoverArt:
0174       return tr("Cover Art");
0175     default:
0176       return section + 1;
0177     }
0178   }
0179   return section + 1;
0180 }
0181 
0182 /**
0183  * Get number of rows.
0184  * @param parent parent model index, invalid for table models
0185  * @return number of rows,
0186  * if parent is valid number of children (0 for table models)
0187  */
0188 int BatchImportSourcesModel::rowCount(const QModelIndex& parent) const
0189 {
0190   return parent.isValid() ? 0 : m_sources.size();
0191 }
0192 
0193 /**
0194  * Get number of columns.
0195  * @param parent parent model index, invalid for table models
0196  * @return number of columns,
0197  * if parent is valid number of children (0 for table models)
0198  */
0199 int BatchImportSourcesModel::columnCount(const QModelIndex& parent) const
0200 {
0201   return parent.isValid() ? 0 : CI_NumColumns;
0202 }
0203 
0204 /**
0205  * Insert rows.
0206  * @param row rows are inserted before this row, if 0 at the begin,
0207  * if rowCount() at the end
0208  * @param count number of rows to insert
0209  * @return true if successful
0210  */
0211 bool BatchImportSourcesModel::insertRows(int row, int count,
0212                                          const QModelIndex&)
0213 {
0214   if (count > 0) {
0215     beginInsertRows(QModelIndex(), row, row + count - 1);
0216     for (int i = 0; i < count; ++i)
0217       m_sources.insert(row, BatchImportProfile::Source());
0218     endInsertRows();
0219   }
0220   return true;
0221 }
0222 
0223 /**
0224  * Remove rows.
0225  * @param row rows are removed starting with this row
0226  * @param count number of rows to remove
0227  * @return true if successful
0228  */
0229 bool BatchImportSourcesModel::removeRows(int row, int count,
0230                         const QModelIndex&)
0231 {
0232   if (count > 0) {
0233     beginRemoveRows(QModelIndex(), row, row + count - 1);
0234     for (int i = 0; i < count; ++i)
0235       m_sources.removeAt(row);
0236     endRemoveRows();
0237   }
0238   return true;
0239 }
0240 
0241 /**
0242  * Set batch import source of a given @a row.
0243  * @param row number of row to set
0244  * @param source batch import source
0245  */
0246 void BatchImportSourcesModel::setBatchImportSource(
0247     int row, const BatchImportProfile::Source& source)
0248 {
0249   if (row >= 0 && row < m_sources.size()) {
0250     m_sources[row] = source;
0251     emit dataChanged(index(row, 0), index(row, CI_NumColumns - 1));
0252   }
0253 }
0254 
0255 /**
0256  * Get batch import source of a given @a row.
0257  * @param row number of row to get
0258  * @param source the batch import source is returned here
0259  */
0260 void BatchImportSourcesModel::getBatchImportSource(
0261     int row, BatchImportProfile::Source& source) const
0262 {
0263   if (row >= 0 && row < m_sources.size()) {
0264     source = m_sources.at(row);
0265   }
0266 }
0267 
0268 /**
0269  * Set the model from the import sources.
0270  * @param sources batch import sources
0271  */
0272 void BatchImportSourcesModel::setBatchImportSources(
0273     const QList<BatchImportProfile::Source>& sources)
0274 {
0275   beginResetModel();
0276   m_sources = sources;
0277   endResetModel();
0278 }
0279 
0280 /**
0281  * Get the import sources from the model.
0282  * @return batch import sources.
0283  */
0284 QList<BatchImportProfile::Source> BatchImportSourcesModel::getBatchImportSources() const
0285 {
0286   return m_sources;
0287 }