File indexing completed on 2025-04-27 14:27:22
0001 /*************************************************************************** 0002 * Copyright (C) 2019 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "model/thememodel.h" 0021 0022 #include "data/rolisteamtheme.h" 0023 0024 ThemeModel::ThemeModel(QObject* parent) : QAbstractListModel(parent) {} 0025 0026 QVariant ThemeModel::headerData(int, Qt::Orientation, int) const 0027 { 0028 return QVariant(); 0029 } 0030 0031 ThemeModel::~ThemeModel()= default; 0032 0033 int ThemeModel::rowCount(const QModelIndex& parent) const 0034 { 0035 // For list models only the root node (an invalid parent) should return the list's size. For all 0036 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model. 0037 if(parent.isValid()) 0038 return 0; 0039 0040 return static_cast<int>(m_themeList.size()); 0041 } 0042 0043 int ThemeModel::indexOf(RolisteamTheme* theme) const 0044 { 0045 auto it= std::find_if(std::begin(m_themeList), std::end(m_themeList), 0046 [theme](const std::unique_ptr<RolisteamTheme>& itTheme) { return itTheme.get() == theme; }); 0047 0048 if(it == std::end(m_themeList)) 0049 return -1; 0050 return std::distance(std::begin(m_themeList), it); 0051 } 0052 0053 QVariant ThemeModel::data(const QModelIndex& index, int role) const 0054 { 0055 if(!index.isValid()) 0056 return QVariant(); 0057 0058 if(role == Qt::DisplayRole) 0059 role= Name; 0060 0061 const auto& theme= m_themeList.at(static_cast<std::size_t>(index.row())); 0062 0063 if(role == Qt::DecorationRole && !theme->isRemovable()) 0064 return QIcon::fromTheme("lock.png"); 0065 0066 QVariant var; 0067 switch(role) 0068 { 0069 case Name: 0070 var= QVariant::fromValue(theme->getName()); 0071 break; 0072 } 0073 return var; 0074 } 0075 0076 void ThemeModel::removeTheme(int pos) 0077 { 0078 0079 auto idx= static_cast<std::size_t>(pos); 0080 if(m_themeList.size() <= idx) 0081 return; 0082 0083 beginRemoveRows(QModelIndex(), pos, pos); 0084 m_themeList.erase(m_themeList.begin() + pos); 0085 endRemoveRows(); 0086 } 0087 0088 void ThemeModel::addTheme(RolisteamTheme* theme) 0089 { 0090 auto size= static_cast<int>(m_themeList.size()); 0091 beginInsertRows(QModelIndex(), size, size); 0092 std::unique_ptr<RolisteamTheme> ptr(theme); 0093 m_themeList.push_back(std::move(ptr)); 0094 endInsertRows(); 0095 } 0096 void ThemeModel::clear() 0097 { 0098 beginResetModel(); 0099 m_themeList.clear(); 0100 endResetModel(); 0101 } 0102 const std::vector<std::unique_ptr<RolisteamTheme>>& ThemeModel::themes() const 0103 { 0104 return m_themeList; 0105 } 0106 QString ThemeModel::name(int themePos) const 0107 { 0108 auto idx= static_cast<std::size_t>(themePos); 0109 0110 if(idx >= m_themeList.size()) 0111 return {}; 0112 0113 const auto& it= m_themeList.at(idx); 0114 return it->getName(); 0115 } 0116 RolisteamTheme* ThemeModel::theme(int pos) const 0117 { 0118 auto idx= static_cast<std::size_t>(pos); 0119 0120 if(idx >= m_themeList.size()) 0121 return nullptr; 0122 0123 const auto& it= m_themeList.at(idx); 0124 return it.get(); 0125 }