File indexing completed on 2024-03-24 15:44:19

0001 /***************************************************************************
0002  *   Copyright (C) 2012-2016 by Daniel Nicoletti <dantti12@gmail.com>      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; see the file COPYING. If not, write to       *
0016  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0017  *   Boston, MA 02110-1301, USA.                                           *
0018  ***************************************************************************/
0019 
0020 #include "ProfileNamedColorsModel.h"
0021 #include <QColor>
0022 ProfileNamedColorsModel::ProfileNamedColorsModel(QObject *parent)
0023     : QAbstractListModel(parent)
0024 {
0025 }
0026 
0027 void ProfileNamedColorsModel::setNamedColors(const QMap<QString, QColor> &namedColors)
0028 {
0029     beginResetModel();
0030     m_data = namedColors;
0031     m_keys = m_data.keys();
0032     endResetModel();
0033 }
0034 
0035 int ProfileNamedColorsModel::rowCount(const QModelIndex &index) const
0036 {
0037     Q_UNUSED(index);
0038     return m_keys.count();
0039 }
0040 QVariant ProfileNamedColorsModel::data(const QModelIndex &index, int role) const
0041 {
0042     if (index.row() < 0 || index.row() >= m_keys.count()) {
0043         return QVariant();
0044     }
0045     switch (role) {
0046     case NameRole:
0047         return m_keys[index.row()];
0048     case ColorRole:
0049         return m_data[m_keys[index.row()]];
0050     case IsDarkColorRole:
0051         auto color = m_data[m_keys[index.row()]];
0052         // 0.2126*r + 0.7151*g + 0.0721*b
0053         int y = 0.2126 * color.red() + 0.7151 * color.green() + 0.0721 * color.blue();
0054         return y < 140;
0055     }
0056     return QVariant();
0057 }
0058 QHash<int, QByteArray> ProfileNamedColorsModel::roleNames() const
0059 {
0060     return {{NameRole, "name"}, {ColorRole, "colorValue"}, {IsDarkColorRole, "isDarkColor"}};
0061 }
0062 
0063 #include "moc_ProfileNamedColorsModel.cpp"