home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / watched_path_property.h < prev    next >
C/C++ Source or Header  |  2009-02-16  |  4KB  |  131 lines

  1. #ifndef K3DSDK_WATCHED_PATH_PROPERTY_H
  2. #define K3DSDK_WATCHED_PATH_PROPERTY_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file watched_path_property.h
  24.         \author Bart Janssens (bart.janssens@lid.kviv.be)
  25.         \created Feb 6, 2009
  26. */
  27.  
  28. #include "data.h"
  29. #include "hints.h"
  30. #include "ihint.h"
  31. #include "iuser_interface.h"
  32. #include "iwatched_path_property.h"
  33. #include "path.h"
  34. #include "types.h"
  35.  
  36. namespace k3d
  37. {
  38.  
  39. template<typename value_t, class name_policy_t>
  40. class watched_path_property :
  41.     public iwatched_path_property,
  42.     public path_property<value_t, name_policy_t>
  43. {
  44.     typedef path_property<value_t, name_policy_t> base;
  45. public:
  46.     void watch(const bool_t Watch)
  47.     {
  48.         m_watched = Watch;
  49.         base::changed_signal().emit(0);
  50.     }
  51.     
  52.     const bool_t is_watched() const
  53.     {
  54.         return m_watched;
  55.     }
  56.  
  57. protected:
  58.     template<typename init_t>
  59.     watched_path_property(const init_t& Init) : base(Init), m_watched(true), m_watch_id(0)
  60.     {
  61.         base::changed_signal().connect(hint::converter<
  62.             hint::convert<hint::any, hint::unchanged> >(sigc::mem_fun(*this, &watched_path_property::on_path_changed)));
  63.     }
  64.     
  65.     ~watched_path_property()
  66.     {
  67.         user_interface().unwatch_path(m_watch_id);
  68.     }
  69.     
  70. private:
  71.     void on_path_changed(ihint* Hint)
  72.     {
  73.         if(dynamic_cast<hint::file_changed*>(Hint))
  74.         {
  75.             return;
  76.         }
  77.         
  78.         user_interface().unwatch_path(m_watch_id);
  79.         m_watch_id = 0;
  80.         
  81.         const filesystem::path path = base::pipeline_value();
  82.         if(path.empty() || !m_watched)
  83.         {
  84.             return;
  85.         }
  86.         
  87.         m_watch_id = k3d::user_interface().watch_path(path, sigc::mem_fun(*this, &watched_path_property::on_file_changed));
  88.     }
  89.     
  90.     void on_file_changed()
  91.     {
  92.         base::changed_signal().emit(hint::file_changed::instance());
  93.     }
  94.     
  95.     bool_t m_watched;
  96.     uint_t m_watch_id;    
  97. };
  98.     
  99. /// Serialization policy for filesystem path data that handles external filesystem resources
  100. template<typename value_t, class property_policy_t>
  101. class watched_path_serialization :
  102.     public path_serialization<value_t, property_policy_t>
  103. {
  104.     // This policy only works for data stored by-value
  105.     BOOST_STATIC_ASSERT((!boost::is_pointer<value_t>::value));
  106.  
  107.     typedef path_serialization<value_t, property_policy_t> base;
  108.     
  109. public:
  110.     void save(xml::element& Element, const ipersistent::save_context& Context)
  111.     {
  112.         xml::element& xml_storage = save_external_resource(Element, Context, property_policy_t::name(), property_policy_t::property_path_reference(), property_policy_t::internal_value());
  113.         xml_storage.append(xml::attribute("watched", property_policy_t::is_watched()));
  114.     }
  115.  
  116.     void load(xml::element& Element, const ipersistent::load_context& Context)
  117.     {
  118.         base::load(Element, Context);
  119.         property_policy_t::watch(xml::attribute_value<bool_t>(Element, "watched", false));
  120.     }
  121.  
  122. protected:
  123.     template<typename init_t>
  124.     watched_path_serialization(const init_t& Init) : base(Init) {}
  125. };
  126.  
  127. } // namespace k3d
  128.  
  129. #endif // !K3DSDK_WATCHED_PATH_PROPERTY_H
  130.  
  131.