File indexing completed on 2024-05-05 05:40:27
0001 /*************************************************************************** 0002 * Copyright (C) 2021 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/actiononlistmodel.h" 0021 0022 #include <QDebug> 0023 0024 ActionOnListModel::ActionOnListModel(const QStringList& data, const QList<ActionInfo>& actions, const QString& root, 0025 QObject* parent) 0026 : QAbstractListModel(parent), m_actions(actions), m_root(root) 0027 0028 { 0029 std::transform(std::begin(data), std::end(data), std::back_inserter(m_data), 0030 [](const QString& path) 0031 { 0032 DataInfo info; 0033 info.data= path; 0034 return info; 0035 }); 0036 } 0037 0038 int ActionOnListModel::rowCount(const QModelIndex& parent) const 0039 { 0040 if(parent.isValid()) 0041 return 0; 0042 0043 return m_data.size(); 0044 } 0045 0046 QVariant ActionOnListModel::data(const QModelIndex& index, int role) const 0047 { 0048 if(!index.isValid()) 0049 return QVariant(); 0050 0051 auto curr= m_data.at(index.row()); 0052 QVariant res; 0053 if(role == Qt::DisplayRole || role == Name) 0054 { 0055 res= curr.data.replace(m_root, ""); 0056 } 0057 else if(role == Action) 0058 res= curr.indexAction; 0059 else if(role == PossibleAction) 0060 { 0061 QVariantList list; 0062 std::transform(std::begin(m_actions), std::end(m_actions), std::back_inserter(list), 0063 [](const ActionInfo& info) { return info.name; }); 0064 res= list; 0065 } 0066 else if(role == PossibleIcon) 0067 { 0068 QVariantList list; 0069 std::transform(std::begin(m_actions), std::end(m_actions), std::back_inserter(list), 0070 [](const ActionInfo& info) { return info.icon; }); 0071 res= list; 0072 } 0073 return res; 0074 } 0075 0076 QHash<int, QByteArray> ActionOnListModel::roleNames() const 0077 { 0078 return {{Name, "name"}, {Action, "action"}, {PossibleAction, "actions"}, {PossibleIcon, "icons"}}; 0079 } 0080 0081 bool ActionOnListModel::canValidate() const 0082 { 0083 bool res= false; 0084 0085 if(m_data.isEmpty()) 0086 res= true; 0087 else 0088 { 0089 res= std::all_of(std::begin(m_data), std::end(m_data), 0090 [](const DataInfo& info) { return info.indexAction != -1; }); 0091 } 0092 0093 return res; 0094 } 0095 0096 void ActionOnListModel::setAction(int idx, int action) 0097 { 0098 if(idx < 0 || idx >= m_data.size() || action < 0 || action >= m_actions.size()) 0099 return; 0100 0101 auto& info= m_data[idx]; 0102 info.indexAction= action; 0103 auto idxItem= index(idx, 0); 0104 emit dataChanged(idxItem, idxItem); 0105 } 0106 0107 const QList<DataInfo>& ActionOnListModel::dataset() const 0108 { 0109 return m_data; 0110 }