MarkRendererManager.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/maptools/MarkRendererManager.cpp
22 
23  \brief This is a singleton for managing all mark renderers instances available in the system.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../common/StringUtils.h"
29 #include "../core/translator/Translator.h"
30 #include "../se/Mark.h"
31 #include "AbstractMarkRenderer.h"
32 #include "Exception.h"
33 #include "MarkRendererManager.h"
34 
35 // Boost
36 #include <boost/format.hpp>
37 
38 // STL
39 #include <cassert>
40 #include <vector>
41 
43 
45 {
46  clear();
47 }
48 
50 {
51  LockWrite l;
52 
53  if(size == 0)
54  throw Exception(TE_TR("Requested size is invalid!"));
55 
56  if(mark == nullptr)
57  throw Exception(TE_TR("The given mark is invalid!"));
58 
59  const std::string* name = mark->getWellKnownName();
60  if(name == nullptr)
61  throw Exception(TE_TR("The given mark not have a valid well known name!"));
62 
63  if(name->empty())
64  throw Exception(TE_TR("The mark well known name is empty!"));
65 
66  // Default renderer key
67  std::string key;
68 
69  // Try find the renderer key
70  std::vector<std::string> tokens;
71  te::common::Tokenize(*name, tokens, "://");
72 
73  if(tokens.size() > 1)
74  key = tokens[0];
75 
76  std::map<std::string, AbstractMarkRenderer*>::iterator it = m_renderers.find(key);
77 
78  if(it == m_renderers.end())
79  throw Exception((boost::format(TE_TR("There is not a mark renderer registered for the given mark %1%.")) % *name).str());
80 
81  return it->second->render(mark, size);
82 }
83 
84 void te::map::MarkRendererManager::getAllSupportedMarks(std::vector<std::string>& marks) const
85 {
86  std::map<std::string, AbstractMarkRenderer*>::const_iterator it;
87  for(it = m_renderers.begin(); it != m_renderers.end(); ++it)
88  it->second->getSupportedMarks(marks);
89 }
90 
91 void te::map::MarkRendererManager::add(const std::string& key, AbstractMarkRenderer* renderer)
92 {
93  std::map<std::string, AbstractMarkRenderer*>::iterator it = m_renderers.find(key);
94 
95  if(it != m_renderers.end())
96  throw Exception((boost::format(TE_TR("There is already a mark renderer registered with the given key %1%.")) % key).str());
97 
98  m_renderers[key] = renderer;
99 }
100 
102 {
104  m_renderers.clear();
105 }
void getAllSupportedMarks(std::vector< std::string > &marks) const
It informs the set of all supported marks available in the system.
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
An exception class for the MapTools module.
~MarkRendererManager()
Singleton destructor.
Base exception class for plugin module.
This is a singleton for managing all mark renderers instances available in the system.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:221
void add(const std::string &key, AbstractMarkRenderer *renderer)
It adds a new mark renderer to the manager.
const std::string * getWellKnownName() const
Definition: Mark.cpp:60
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void clear()
It clears the marks renderers of the manager.
std::map< std::string, AbstractMarkRenderer * > m_renderers
The mark renderers in the manager.
An abstract class for conversion of Symbology Enconding Mark elements to an image pattern...
An abstract class for conversion of Symbology Enconding Mark elements to an image pattern...
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
te::color::RGBAColor ** render(const te::se::Mark *mark, std::size_t size)
It generates the image pattern from the given Symbology Enconding Mark element.
MarkRendererManager()
It initializes the singleton instance of the mark renderer manager.