]> git.tdb.fi Git - libs/gltk.git/commitdiff
Use a different approach for custom item widgets
authorMikko Rasa <tdb@tdb.fi>
Sun, 30 Jun 2013 21:03:38 +0000 (00:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 30 Jun 2013 21:17:31 +0000 (00:17 +0300)
Subclassed widgets can't be loaded from datafiles without providing custom
statements.  Dropdown doesn't allow changing the type of the List either.

source/dropdown.h
source/list.cpp
source/list.h

index eaa29c193c2260288a185bb2131c693ffeecdfdd..06aefa5af96d32cc0b6b3cc0d590bb092298164b 100644 (file)
@@ -46,6 +46,9 @@ public:
        ListData &get_data() { return list.get_data(); }
        const ListData &get_data() const { return list.get_data(); }
 
+       template<typename T>
+       void set_item_type() { list.set_item_type<T>(); }
+
        void set_selected_index(int);
        int get_selected_index() const { return list.get_selected_index(); }
 
index 7227f0c59f6cd971ec330f6de266b84d8ecadc82..eaab9e0e5ed8857a0bdaea7e779374577efbf7da 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
 #include "graphic.h"
@@ -12,6 +13,11 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
+incompatible_data::incompatible_data(const type_info &ti):
+       logic_error("expected "+Debug::demangle(ti.name()))
+{ }
+
+
 List::List():
        data(new BasicListData<string>),
        own_data(true)
@@ -28,6 +34,7 @@ List::List(ListData &d):
 
 void List::init()
 {
+       item_factory = 0;
        sel_index = -1;
        first = 0;
        max_scroll = 0;
@@ -42,6 +49,7 @@ void List::init()
 
 List::~List()
 {
+       delete item_factory;
        delete observer;
        if(own_data)
                delete data;
@@ -74,6 +82,9 @@ void List::autosize_special(const Part &part, Geometry &ageom)
 
 void List::set_data(ListData &d)
 {
+       if(item_factory)
+               item_factory->set_data(d);
+
        delete observer;
        if(own_data)
                delete data;
@@ -105,7 +116,10 @@ void List::items_changed()
 
 List::Item *List::create_item(unsigned index)
 {
-       return new BasicItem(data->get_string(index));
+       if(item_factory)
+               return item_factory->create_item(index);
+       else
+               return new BasicItem(data->get_string(index));
 }
 
 void List::set_view_size(unsigned s)
index 14d9eac291e3bb2b7daee7c9862a268108689182..351b0a461e27aed15c0d271835317301926b70aa 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef MSP_GLTK_LIST_H_
 #define MSP_GLTK_LIST_H_
 
+#include <stdexcept>
+#include <typeinfo>
 #include <sigc++/signal.h>
 #include "container.h"
 #include "label.h"
 namespace Msp {
 namespace GLtk {
 
+/**
+Thrown if a list's item type is incompatible with its data.
+*/
+class incompatible_data: public std::logic_error
+{
+public:
+       incompatible_data(const std::type_info &);
+       virtual ~incompatible_data() throw() { }
+};
+
+
 /**
 Shows a list of items, allowing the user to select one.  A slider is included
 to allow scrolling through a long list.
@@ -41,7 +54,7 @@ private:
                void refresh_item(unsigned);
        };
 
-protected:
+public:
        class Item: public Container
        {
        public:
@@ -55,7 +68,7 @@ protected:
                virtual void render_special(const Part &, GL::Renderer &) const;
        };
 
-public:
+private:
        class BasicItem: public Item
        {
        private:
@@ -68,6 +81,43 @@ public:
                virtual void on_style_change();
        };
 
+       class ItemFactory
+       {
+       protected:
+               ItemFactory() { }
+       public:
+               virtual ~ItemFactory() { }
+
+               virtual void set_data(const ListData &) = 0;
+               virtual Item *create_item(unsigned) const = 0;
+       };
+
+       template<typename I>
+       class TypedItemFactory: public ItemFactory
+       {
+       private:
+               typedef typename I::ValueType ValueType;
+
+               const ListDataStore<ValueType> *data;
+
+       public:
+               TypedItemFactory(const ListData &d)
+               { set_data(d); }
+
+               virtual void set_data(const ListData &d)
+               {
+                       if(const ListDataStore<ValueType> *ds = dynamic_cast<const ListDataStore<ValueType> *>(&d))
+                               data = ds;
+                       else
+                               throw incompatible_data(typeid(ValueType));
+               }
+
+               virtual Item *create_item(unsigned i) const
+               {
+                       return new I(data->get(i));
+               }
+       };
+
 public:
        sigc::signal<void, unsigned> signal_item_selected;
 
@@ -75,6 +125,7 @@ private:
        ListData *data;
        bool own_data;
        DataObserver *observer;
+       ItemFactory *item_factory;
        int sel_index;
        unsigned first;
        unsigned max_scroll;
@@ -102,8 +153,17 @@ public:
        const ListData &get_data() const { return *data; }
 private:
        void items_changed();
-protected:
-       virtual Item *create_item(unsigned);
+
+public:
+       template<typename I>
+       void set_item_type()
+       {
+               ItemFactory *f = new TypedItemFactory<I>(*data);
+               delete item_factory;
+               item_factory = f;
+       }
+private:
+       Item *create_item(unsigned);
 
 public:
        void set_view_size(unsigned);