All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ColorSchemeCatalogManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/color/ColorSchemeCatalogManager.cpp
22 
23  \brief Implementation of the singleton to manage color scheme catalogs.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/PlatformUtils.h"
29 #include "../common/STLUtils.h"
30 #include "../common/Translator.h"
31 #include "ColorScheme.h"
32 #include "ColorSchemeCatalog.h"
33 #include "ColorSchemeGroup.h"
35 
36 // STL
37 #include <cassert>
38 #include <fstream>
39 #include <vector>
40 
41 // Boost
42 #include <boost/foreach.hpp>
43 #include <boost/property_tree/ptree.hpp>
44 #include <boost/property_tree/json_parser.hpp>
45 
47 {
48  te::color::ColorSchemeCatalog* csc = findByName("Default");
49 
50  if(csc)
51  throw te::common::Exception(TE_TR("The default color scheme catalog is already initialized!"));
52 
53  try
54  {
55  std::ifstream f;
56 
57  std::string jsonf = te::common::FindInTerraLibPath("share/terralib/json/color_ramps.json");
58 
59  if(jsonf.empty())
60  throw te::common::Exception(TE_TR("Could not find color_ramps.json file!"));
61 
62  f.open(jsonf.c_str());
63 
64  if (!f.is_open())
65  return;
66 
67  //create default color scheme catalog
68  csc = new te::color::ColorSchemeCatalog("Default");
69 
70  insert(csc);
71 
72  boost::property_tree::ptree pt;
73  boost::property_tree::json_parser::read_json(f,pt);
74  BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("ramps"))
75  {
76  std::string name = v.second.get<std::string>("name");
77 
78  //create terraview color scheme group
80 
81  csc->push_back(csg);
82 
83  BOOST_FOREACH(boost::property_tree::ptree::value_type &t, v.second.get_child("schemes"))
84  {
85  std::string name = t.second.get<std::string>("name");
86 
88 
89  std::vector<te::color::RGBAColor>* rgbaVec = new std::vector<te::color::RGBAColor>();
90 
91  BOOST_FOREACH(boost::property_tree::ptree::value_type &c, t.second.get_child("values"))
92  {
93  unsigned int red = c.second.get<unsigned int>("red");
94  unsigned int green = c.second.get<unsigned int>("green");
95  unsigned int blue = c.second.get<unsigned int>("blue");
96 
97  rgbaVec->push_back(te::color::RGBAColor(red, green, blue, TE_OPAQUE));
98  }
99 
100  cs->push_back(rgbaVec);
101 
102  csg->push_back(cs);
103  }
104  }
105 
106  f.close();
107  }
108  catch(boost::property_tree::json_parser::json_parser_error &je)
109  {
110  std::string errmsg = "Error parsing: " + je.filename() + ": " + je.message();
111  te::common::Exception ex(TE_TR(errmsg));
112  throw(ex);
113  }
114  catch (std::exception const& e)
115  {
116  std::cerr << e.what() << std::endl;
117  }
118  return;
119 }
120 
122 {
123  assert(c);
124 
125  if(findByName(c->getName()))
126  throw te::common::Exception(TE_TR("There is already a color scheme catalog with the given name!"));
127 
128  m_catalogs.push_back(c);
129  m_catalogIdxByName.insert(std::map<std::string, ColorSchemeCatalog*>::value_type(c->getName(), c));
130 }
131 
133 {
134  assert(c);
135 
136 // first, find candidates for deletion... if one of then is not found, raise an exception
137  std::map<std::string, ColorSchemeCatalog*>::iterator itProjectIdxByName = m_catalogIdxByName.find(c->getName());
138 
139  if(itProjectIdxByName != m_catalogIdxByName.end())
140  throw te::common::Exception(TE_TR("Couldn't find the catalog with the given name!"));
141 
142  size_t i = 0;
143 
144  for(; i < m_catalogs.size(); ++i)
145  {
146  if(m_catalogs[i]->getName() == c->getName())
147  break;
148  }
149 
150  if(i == m_catalogs.size())
151  throw te::common::Exception(TE_TR("Couldn't find the catalog with the given name!"));
152 
153 // if we are here, so all entries are ok... just remove them
154  m_catalogs.erase(m_catalogs.begin() + i);
155  m_catalogIdxByName.erase(itProjectIdxByName);
156 }
157 
159 {
160  disconnect(c);
161 
162 // and delete the projet from main memory
163  delete (c);
164 }
165 
167 {
168  std::map<std::string, ColorSchemeCatalog*>::const_iterator it = m_catalogIdxByName.find(name);
169 
170  if(it != m_catalogIdxByName.end())
171  return it->second;
172 
173  return 0;
174 }
175 
176 std::pair<std::vector<te::color::ColorSchemeCatalog*>::const_iterator,
177  std::vector<te::color::ColorSchemeCatalog*>::const_iterator> te::color::ColorSchemeCatalogManager::getIterator() const
178 {
179  return std::pair<std::vector<te::color::ColorSchemeCatalog*>::const_iterator,
180  std::vector<te::color::ColorSchemeCatalog*>::const_iterator>(m_catalogs.begin(), m_catalogs.end());
181 }
182 
183 const std::vector<te::color::ColorSchemeCatalog*>& te::color::ColorSchemeCatalogManager::getCatalogs() const
184 {
185  return m_catalogs;
186 }
187 
189 {
190  return m_catalogs.empty();
191 }
192 
194 {
195  te::common::FreeContents(m_catalogs);
196 
197  m_catalogs.clear();
198 
199  m_catalogIdxByName.clear();
200 }
201 
203 {
204  clear();
205 }
206 
208 {
209 }
210 
ColorSchemeCatalog * findByName(const std::string &name) const
It returns the catalog identified by a given name or NULL if none is found.
TECOMMONEXPORT std::string FindInTerraLibPath(const std::string &p)
Returns the path relative to a directory or file in the context of TerraLib.
This class represents a group of color schemes.
void erase(ColorSchemeCatalog *c)
It removes the catalog from the manager and clears it resources.
It models the concept of color scheme.
Definition: ColorScheme.h:58
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void clear()
It unloads all catalogs managed by ColorSchemeCatalogManager.
bool isEmpty() const
It returns true if the manager contains at least one catalog. If no catalog exists, it returns false.
The ColorSchemeCatalogManager is a singleton that can be used to manage all loaded color scheme catal...
void push_back(ColorSchemeGroup *group)
It adds a new group to the catalog and sets its relationship.
void disconnect(ColorSchemeCatalog *c)
It removes the internal reference to the catalog.
void init()
Inializes the manager from a JSON file containing instances of color schemes.
const std::string & getName() const
It returns the catalog name.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void insert(ColorSchemeCatalog *c)
It inserts a new catalog that will be managed by ColorSchemeCatalogManager.
const std::vector< ColorSchemeCatalog * > & getCatalogs() const
It returns the list of catalogs available in the system.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
The concept of color scheme.
ColorSchemeCatalogManager()
It initializes the Singleton.
This class represents a group of color schemes.
A catalog for color schemes.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A catalog for color schemes groups.
std::pair< std::vector< ColorSchemeCatalog * >::const_iterator, std::vector< ColorSchemeCatalog * >::const_iterator > getIterator() const
It returns a pair of iterators over the catalogs of this manager.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55