From: Mikko Rasa Date: Sun, 30 Jun 2013 21:03:38 +0000 (+0300) Subject: Use a different approach for custom item widgets X-Git-Url: http://git.tdb.fi/?p=libs%2Fgltk.git;a=commitdiff_plain;h=74934c6f7a6fd0f9e9bf11eeb9f33cf9279bf471 Use a different approach for custom item widgets Subclassed widgets can't be loaded from datafiles without providing custom statements. Dropdown doesn't allow changing the type of the List either. --- diff --git a/source/dropdown.h b/source/dropdown.h index eaa29c1..06aefa5 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -46,6 +46,9 @@ public: ListData &get_data() { return list.get_data(); } const ListData &get_data() const { return list.get_data(); } + template + void set_item_type() { list.set_item_type(); } + void set_selected_index(int); int get_selected_index() const { return list.get_selected_index(); } diff --git a/source/list.cpp b/source/list.cpp index 7227f0c..eaab9e0 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,3 +1,4 @@ +#include #include #include #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), 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) diff --git a/source/list.h b/source/list.h index 14d9eac..351b0a4 100644 --- a/source/list.h +++ b/source/list.h @@ -1,6 +1,8 @@ #ifndef MSP_GLTK_LIST_H_ #define MSP_GLTK_LIST_H_ +#include +#include #include #include "container.h" #include "label.h" @@ -10,6 +12,17 @@ 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 + class TypedItemFactory: public ItemFactory + { + private: + typedef typename I::ValueType ValueType; + + const ListDataStore *data; + + public: + TypedItemFactory(const ListData &d) + { set_data(d); } + + virtual void set_data(const ListData &d) + { + if(const ListDataStore *ds = dynamic_cast *>(&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 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 + void set_item_type() + { + ItemFactory *f = new TypedItemFactory(*data); + delete item_factory; + item_factory = f; + } +private: + Item *create_item(unsigned); public: void set_view_size(unsigned);