File indexing completed on 2024-05-12 05:40:26

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 "mindmap/model/nodestylemodel.h"
0021 
0022 #include "mindmap/data/nodestyle.h"
0023 
0024 namespace mindmap
0025 {
0026 NodeStyleModel::NodeStyleModel(QObject* parent) : QAbstractListModel(parent)
0027 {
0028     initStyles();
0029 }
0030 
0031 NodeStyleModel::~NodeStyleModel() = default;
0032 
0033 int NodeStyleModel::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     return static_cast<int>(m_styles.size());
0040 }
0041 
0042 QVariant NodeStyleModel::data(const QModelIndex& index, int role) const
0043 {
0044     if(!index.isValid())
0045         return QVariant();
0046 
0047     if(index.row() >= static_cast<int>(m_styles.size()))
0048         return {};
0049 
0050     auto const& style= m_styles[static_cast<std::size_t>(index.row())];
0051 
0052     QVariant var;
0053     switch(role)
0054     {
0055     case ColorOne:
0056         var= QVariant::fromValue(style->colorOne());
0057         break;
0058     case ColorTwo:
0059         var= QVariant::fromValue(style->colorTwo());
0060         break;
0061     case TextColor:
0062         var= QVariant::fromValue(style->textColor());
0063         break;
0064     default:
0065         break;
0066     }
0067 
0068     return var;
0069 }
0070 
0071 QHash<int, QByteArray> NodeStyleModel::roleNames() const
0072 {
0073     // clang-format off
0074     static QHash<int, QByteArray> roles
0075         =  {{NodeStyleModel::ColorOne, "colorOne"},
0076             {NodeStyleModel::ColorTwo, "colorTwo"},
0077             {NodeStyleModel::TextColor, "textColor"}};
0078     //clang-format on
0079     return roles;
0080 }
0081 
0082 NodeStyle* NodeStyleModel::getStyle(int index) const
0083 {
0084     if(m_styles.empty())
0085     {
0086         auto tmp = new NodeStyle();
0087         tmp->setColorOne(Qt::white);
0088         tmp->setColorTwo(Qt::lightGray);
0089         tmp->setTextColor(Qt::black);
0090         return tmp;
0091     }
0092     index = std::max(0,index);
0093 
0094     auto idx = static_cast<std::size_t>(index);
0095 
0096     if(idx >= m_styles.size())
0097        idx = 0;
0098 
0099     return m_styles[idx].get();
0100 }
0101 
0102 void NodeStyleModel::initStyles()
0103 {
0104     std::vector<QString> colors({"#c0c0c0","#A4C400",
0105     "#60A917","#008A00","#00ABA9","#1BA1E2","#3E65FF",
0106     "#6A00FF","#AA00FF","#F472D0","#D80073","#A20025",
0107     "#E51400","#FA6800","#F0A30A","#E3C800",
0108     "#825A2C","#6D8764","#647687","#76608A"});
0109 
0110     beginResetModel();
0111     m_styles.clear();
0112 
0113     std::transform(colors.begin(), colors.end(), std::back_inserter(m_styles),[](const QString& colorstr) -> std::unique_ptr<NodeStyle> {
0114         QColor one(Qt::white);
0115         QColor text(Qt::black);
0116         QColor color;
0117         color.setNamedColor(colorstr);
0118         auto style = new NodeStyle();
0119         style->setColorOne(one);
0120         style->setColorTwo(color);
0121         style->setTextColor(text);
0122         return std::unique_ptr<NodeStyle>(style);
0123     });
0124 
0125     std::transform(colors.begin(), colors.end(), std::back_inserter(m_styles),[](const QString& colorstr){
0126         QColor one(Qt::black);
0127         QColor text(Qt::white);
0128         QColor color;
0129         color.setNamedColor(colorstr);
0130         auto style = new NodeStyle();
0131         style->setColorOne(color);
0132         style->setColorTwo(one);
0133         style->setTextColor(text);
0134         return std::unique_ptr<NodeStyle>(style);
0135     });
0136 
0137     endResetModel();
0138 
0139 
0140 }
0141 }