From 996a27af2dd3f9915d547f7c0c3cf76cc22b536b Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Mon, 12 Jul 2010 14:08:30 +0000
Subject: [PATCH] Added GradientManager, loads preset gradients from info xml
 file

String constructor for gfx.Gradients (loads given gradient from GradientManager)

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2554 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/gfx/pymod/wrap_gfx.cc       |  2 +
 modules/gfx/src/CMakeLists.txt      |  2 +
 modules/gfx/src/gradient.cc         | 14 ++++-
 modules/gfx/src/gradient.hh         |  3 +
 modules/gfx/src/gradient_manager.cc | 91 +++++++++++++++++++++++++++++
 modules/gfx/src/gradient_manager.hh | 56 ++++++++++++++++++
 6 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 modules/gfx/src/gradient_manager.cc
 create mode 100644 modules/gfx/src/gradient_manager.hh

diff --git a/modules/gfx/pymod/wrap_gfx.cc b/modules/gfx/pymod/wrap_gfx.cc
index dd2049702..741675581 100644
--- a/modules/gfx/pymod/wrap_gfx.cc
+++ b/modules/gfx/pymod/wrap_gfx.cc
@@ -154,12 +154,14 @@ BOOST_PYTHON_MODULE(_gfx)
     ;
   
   class_<Gradient>("Gradient", init<>())
+    .def(init<const String&>())
     .def("SetColorAt", &Gradient::SetColorAt)
     .def("GetColorAt", &Gradient::GetColorAt)
     .def("GetStops", &Gradient::GetStops)
     .def("GradientToInfo", &Gradient::GradientToInfo)
     .def("GradientFromInfo", &Gradient::GradientFromInfo).staticmethod("GradientFromInfo")
   ;
+  implicitly_convertible<String, Gradient>();
 
   class_<StopList>("StopList", init<>())
     .def(vector_indexing_suite<StopList>())
diff --git a/modules/gfx/src/CMakeLists.txt b/modules/gfx/src/CMakeLists.txt
index c07798109..0d10efc8c 100644
--- a/modules/gfx/src/CMakeLists.txt
+++ b/modules/gfx/src/CMakeLists.txt
@@ -30,6 +30,7 @@ glext_include.hh
 render_mode.hh
 material.hh
 gradient.hh
+gradient_manager.hh
 module_config.hh
 primitives.hh
 povray_fw.hh
@@ -104,6 +105,7 @@ scene.cc
 selection.cc
 surface.cc
 gradient.cc
+gradient_manager.cc
 vertex_array.cc
 vertex_array_helper.cc
 material.cc
diff --git a/modules/gfx/src/gradient.cc b/modules/gfx/src/gradient.cc
index 7b9566a1b..e0b50a92f 100644
--- a/modules/gfx/src/gradient.cc
+++ b/modules/gfx/src/gradient.cc
@@ -23,6 +23,8 @@
 #include <ost/info/info.hh>
 #include <ost/info/info_fw.hh>
 
+#include <ost/gfx/gradient_manager.hh>
+
 namespace ost { namespace gfx {
 
 
@@ -30,6 +32,16 @@ Gradient::Gradient()
 {
 }
 
+Gradient::Gradient(const String& name)
+{
+  Gradient gradient = GradientManager::Instance().GetGradient(name);
+  StopList stops = gradient.GetStops();
+  for(unsigned int i = 0; i < stops.size(); i++){
+    Stop stop = stops[i];
+    this->SetColorAt(stop.t, stop.color);
+  }
+}
+
 void Gradient::SetColorAt(float t, const Color& c)
 {
   std::vector<Stop>::iterator i;
@@ -44,7 +56,7 @@ void Gradient::SetColorAt(float t, const Color& c)
 Color Gradient::GetColorAt(float t) const
 {
   if (stops_.empty())
-    return Color(0.0, 0.0, 0.0, 1.0);
+    throw Error("Can not calculate color with 0 Stops set!");
   
   uint c=0;
   while (t>=stops_[c].t && c<stops_.size()) {
diff --git a/modules/gfx/src/gradient.hh b/modules/gfx/src/gradient.hh
index a27136528..5b28b849b 100644
--- a/modules/gfx/src/gradient.hh
+++ b/modules/gfx/src/gradient.hh
@@ -71,6 +71,9 @@ class DLLEXPORT_OST_GFX Gradient {
 
 public:
   Gradient();
+
+  Gradient(const String& name);
+
   /// \brief get color
   Color GetColorAt(float t) const;
   /// \brief set color
diff --git a/modules/gfx/src/gradient_manager.cc b/modules/gfx/src/gradient_manager.cc
new file mode 100644
index 000000000..9b6fb63f7
--- /dev/null
+++ b/modules/gfx/src/gradient_manager.cc
@@ -0,0 +1,91 @@
+// Copyright (C) 2008-2010 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+/*
+  Author: Stefan Scheuber
+*/
+#include "boost/filesystem/operations.hpp"
+
+#include <ost/platform.hh>
+
+#include <ost/info/info.hh>
+
+#include "gradient_manager.hh"
+
+namespace ost { namespace gfx {
+
+String STORED_GRADIENT_PATH = "/scene/gradients.xml";
+
+GradientManager& GradientManager::Instance()
+{
+  static GradientManager inst;
+  return inst;
+}
+
+GradientManager::GradientManager()
+{
+  String path = GetSharedDataPath()+STORED_GRADIENT_PATH;
+  if(boost::filesystem::exists(path)){
+    info::InfoHandle info_handler = info::LoadInfo(path);
+    if(info_handler.HasGroup("Gradients")){
+      info::InfoGroup gradients = info_handler.GetGroup("Gradients");
+      info::InfoGroupList list = gradients.GetGroups("Gradient");
+      for(unsigned int i = 0; i< list.size();i++){
+        info::InfoGroup info_gradient = list[i];
+        String name = info_gradient.GetAttribute("Name");
+        Gradient gradient = Gradient::GradientFromInfo(info_gradient);
+        this->SetGradient(name,gradient);
+      }
+    }
+  }
+}
+
+GradientManager::~GradientManager()
+{
+
+}
+
+const std::vector<String> GradientManager::GetGradientNames() const
+{
+  std::vector<String> keys;
+  for(std::map<String , Gradient>::const_iterator it = gradients_.begin(); it != gradients_.end(); ++it) {
+    keys.push_back(it->first);
+  }
+  return keys;
+}
+
+const Gradient& GradientManager::GetGradient(String name) const
+{
+  std::map<String,Gradient>::const_iterator it=gradients_.find(name);
+  if(gradients_.end() != it){
+    return gradients_[name];
+  }
+  return empty_gradient_;
+}
+
+void GradientManager::SetGradient(String name, Gradient gradient)
+{
+  gradients_[name] = gradient;
+}
+
+void GradientManager::RemoveGradient(String name)
+{
+  std::map<String,Gradient>::const_iterator it=gradients_.find(name);
+  if(gradients_.end() != it){
+    gradients_.erase(name);
+  }
+}
+
+}} //ns
diff --git a/modules/gfx/src/gradient_manager.hh b/modules/gfx/src/gradient_manager.hh
new file mode 100644
index 000000000..7551570ba
--- /dev/null
+++ b/modules/gfx/src/gradient_manager.hh
@@ -0,0 +1,56 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2010 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+#ifndef OST_GFX_GRADIENT_MANAGER_HH
+#define OST_GFX_GRADIENT_MANAGER_HH
+
+#include <vector>
+#include <map>
+
+#include <ost/gfx/module_config.hh>
+#include <ost/gfx/gradient.hh>
+
+/*
+  Author: Stefan Scheuber
+*/
+
+namespace ost { namespace gfx {
+
+class
+//DLLEXPORT_OST_GFX
+GradientManager {
+
+public:
+  static GradientManager& Instance();
+  const std::vector<String> GetGradientNames() const;
+  const Gradient& GetGradient(String name) const;
+
+  void SetGradient(String name, Gradient gradient);
+  void RemoveGradient(String name);
+
+  virtual ~GradientManager();
+
+private:
+  GradientManager();
+  mutable std::map<String, Gradient> gradients_;
+  Gradient empty_gradient_;
+};
+
+}} //ns
+
+#endif /* OST_GFX_GRADIENT_MANAGER_HH */
-- 
GitLab