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

0001 /**
0002  * \file genremodel.cpp
0003  * Model with genres.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 22 Jun 2014
0008  *
0009  * Copyright (C) 2014-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 "genremodel.h"
0028 #include "genres.h"
0029 #include "tagconfig.h"
0030 
0031 namespace {
0032 
0033 QStringList createGenreItems()
0034 {
0035   QStringList items;
0036   for (const char** sl = Genres::s_strList; *sl != nullptr; ++sl) {
0037     items.append(QString::fromLatin1(*sl)); // clazy:exclude=reserve-candidates
0038   }
0039   return items;
0040 }
0041 
0042 }
0043 
0044 /**
0045  * Constructor.
0046  * @param id3v1 true to create genres for ID3v1
0047  * @param parent parent widget
0048  */
0049 GenreModel::GenreModel(bool id3v1, QObject* parent)
0050   : QStringListModel(parent), m_id3v1(id3v1)
0051 {
0052   setObjectName(QLatin1String("GenreModel"));
0053   init();
0054 }
0055 
0056 /**
0057  * Initialize module with genres.
0058  * This method is called by the constructor. It shall be called after
0059  * construction if genre settings are changed.
0060  */
0061 void GenreModel::init()
0062 {
0063   QStringList items;
0064   if (TagConfig::instance().onlyCustomGenres()) {
0065     items.append(QLatin1String(""));
0066   } else {
0067     items = createGenreItems();
0068   }
0069   QStringList customGenres = TagConfig::instance().customGenres();
0070   if (m_id3v1) {
0071     for (auto it = customGenres.constBegin(); it != customGenres.constEnd(); ++it) {
0072       if (Genres::getNumber(*it) != 255) {
0073         items.append(*it);
0074       }
0075     }
0076     if (items.count() <= 1) {
0077       // No custom genres for ID3v1 => Show standard genres
0078       items = createGenreItems();
0079     }
0080   } else {
0081     for (auto it = customGenres.constBegin(); it != customGenres.constEnd(); ++it) {
0082       items.append(*it);
0083     }
0084   }
0085   setStringList(items);
0086 }
0087 
0088 /**
0089  * Get the row for a genre.
0090  * If the genre is not found, it is added at the returned row.
0091  * @param genreStr genre string
0092  * @return row number.
0093  */
0094 int GenreModel::getRowForGenre(const QString& genreStr)
0095 {
0096   int genreIndex, customIndex;
0097   if (TagConfig::instance().onlyCustomGenres()) {
0098     genreIndex = 0;
0099     customIndex = 0;
0100   } else {
0101     genreIndex = genreStr.isNull()
0102         ? 0 : Genres::getIndex(Genres::getNumber(genreStr));
0103     customIndex = Genres::count + 1;
0104   }
0105   if (genreIndex <= 0) {
0106     QModelIndexList indexes = match(index(0, 0), Qt::DisplayRole, genreStr, 1,
0107                                     Qt::MatchExactly | Qt::MatchCaseSensitive);
0108     genreIndex = indexes.isEmpty() ? -1 : indexes.first().row();
0109     if (genreIndex < 0) {
0110       genreIndex = customIndex;
0111       setData(index(genreIndex, 0), genreStr, Qt::EditRole);
0112     }
0113   }
0114   return genreIndex;
0115 }