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