OGS
ColorTableModel.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "ColorTableModel.h"
5
7 const std::map<std::string, DataHolderLib::Color*>& colorLookupTable,
8 QObject* parent /*= 0*/)
9{
10 Q_UNUSED(parent)
11
12 this->buildTable(colorLookupTable);
13}
14
16
18 const QModelIndex& parent /*= QModelIndex()*/) const
19{
20 Q_UNUSED(parent)
21
22 return 2;
23}
24
25QVariant ColorTableModel::headerData(int section, Qt::Orientation orientation,
26 int role /*= Qt::DisplayRole*/) const
27{
28 if (role != Qt::DisplayRole)
29 {
30 return QVariant();
31 }
32
33 if (orientation == Qt::Horizontal)
34 {
35 switch (section)
36 {
37 case 0:
38 return "Name";
39 case 1:
40 return "Colour";
41 default:
42 return QVariant();
43 }
44 }
45 else
46 {
47 return QString("Row %1").arg(section);
48 }
49}
50
51QVariant ColorTableModel::data(const QModelIndex& index, int role) const
52{
53 if (!index.isValid())
54 {
55 return QVariant();
56 }
57
58 if (index.row() >= _listOfPairs.size() || index.row() < 0)
59 {
60 return QVariant();
61 }
62
63 if (role == Qt::DisplayRole)
64 {
65 QPair<QString, QColor> pair = _listOfPairs.at(index.row());
66
67 switch (index.column())
68 {
69 case 0:
70 return pair.first;
71 case 1:
72 return pair.second;
73 default:
74 return QVariant();
75 }
76 }
77 return QVariant();
78}
79
81 const std::map<std::string, DataHolderLib::Color*>& colorLookupTable)
82{
83 int count = 0;
84 beginInsertRows(QModelIndex(), 0, colorLookupTable.size() - 1);
85
86 for (const auto& row : colorLookupTable)
87 {
88 QColor color((*row.second)[0], (*row.second)[1], (*row.second)[2]);
89 QString name(QString::fromStdString(row.first));
90
91 /* Saudi Arabia strat names *
92 if (it->first.compare("1")==0) name="Buweib";
93 if (it->first.compare("2")==0) name="Wasia";
94 if (it->first.compare("3")==0) name="Aruma";
95 if (it->first.compare("4")==0) name="Umm Er Radhuma";
96 if (it->first.compare("5")==0) name="Rus";
97 if (it->first.compare("6")==0) name="Dammam";
98 if (it->first.compare("7")==0) name="Neogene";
99 */
100
101 QPair<QString, QColor> pair(name, color);
102 _listOfPairs.insert(count++, pair);
103 }
104
105 endInsertRows();
106 return true;
107}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QVariant data(const QModelIndex &index, int role) const override
ColorTableModel(const std::map< std::string, DataHolderLib::Color * > &colorLookupTable, QObject *parent=nullptr)
bool buildTable(const std::map< std::string, DataHolderLib::Color * > &colorLookupTable)
~ColorTableModel() override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QList< QPair< QString, QColor > > _listOfPairs