]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add support for displaying named icons in an Image widget
authorMikko Rasa <tdb@tdb.fi>
Sun, 11 Sep 2016 13:27:46 +0000 (16:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 11 Sep 2016 13:27:46 +0000 (16:27 +0300)
source/image.cpp
source/image.h

index 383f906911993d2a83d8d434ec791f8c0e90d303..0102a47e487ea8bf4a9efdf29cf1696172820b8f 100644 (file)
@@ -1,6 +1,8 @@
 #include <msp/gl/meshbuilder.h>
 #include "image.h"
 #include "part.h"
+#include "resources.h"
+#include "root.h"
 #include "style.h"
 
 using namespace std;
@@ -35,16 +37,43 @@ void Image::autosize_special(const Part &part, Geometry &ageom) const
 void Image::set_image(const GL::Texture2D *i)
 {
        image = i;
+       icon_name = string();
        signal_autosize_changed.emit();
        rebuild();
 }
 
+void Image::set_icon(const string &n)
+{
+       icon_name = n;
+       update_icon();
+}
+
 void Image::set_keep_aspect(bool ka)
 {
        keep_aspect = ka;
        rebuild();
 }
 
+void Image::update_icon()
+{
+       if(style)
+       {
+               Root *root = find_ancestor<Root>();
+               if(root)
+               {
+                       if(icon_name.empty())
+                               image = 0;
+                       else
+                               image = &root->get_resources().get<GL::Texture2D>(icon_name);
+                       signal_autosize_changed.emit();
+                       rebuild();
+                       return;
+               }
+       }
+
+       image = 0;
+}
+
 void Image::rebuild_special(const Part &part)
 {
        if(part.get_name()=="image")
@@ -88,5 +117,17 @@ void Image::rebuild_special(const Part &part)
        }
 }
 
+void Image::on_style_change()
+{
+       if(!icon_name.empty())
+               update_icon();
+}
+
+void Image::on_reparent()
+{
+       if(!icon_name.empty())
+               update_icon();
+}
+
 } // namespace GLtk
 } // namespace Msp
index 3194216d8bff06b1b69cd7b04db12272d79d6b28..18556108b9ae3c67edf0766234111d4e5e946d42 100644 (file)
@@ -14,6 +14,7 @@ class Image: public Widget
 {
 private:
        const GL::Texture2D *image;
+       std::string icon_name;
        bool keep_aspect;
 
 public:
@@ -26,10 +27,14 @@ private:
 
 public:
        void set_image(const GL::Texture2D *);
+       void set_icon(const std::string &);
        void set_keep_aspect(bool);
 
 private:
+       void update_icon();
        virtual void rebuild_special(const Part &);
+       virtual void on_style_change();
+       virtual void on_reparent();
 };
 
 } // namespace GLtk