1 /* BItems.hpp
2  * Copyright (C) 2019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18  #ifndef BWIDGETS_BITEMS_HPP_
19  #define BWIDGETS_BITEMS_HPP_
20 
21 #include <list>
22 #include <initializer_list>
23 #include "Widget.hpp"
24 #include <cmath>
25 
26 #ifndef UNSELECTED
27 #define UNSELECTED -HUGE_VAL
28 #define BITEMS_DEFAULT_TEXT_PADDING 4.0
29 #endif
30 
31 namespace BItems
32 {
33 
34 class Item
35 {
36 public:
37         Item ();
38         Item (const double value, BWidgets::Widget* widget);
39         Item (const double value, const std::string& text);
40         Item (const Item& that);
41 
42         ~Item ();
43 
44         Item& operator= (const Item& that);
45 
46         void setValue (const double value);
47         double getValue () const;
48         void setWidget (BWidgets::Widget* widget);
49         void setWidget (const std::string& text);
50         void cloneWidgetFrom (BWidgets::Widget* widget);
51         BWidgets::Widget* getWidget () const;
52 
53 protected:
54         void deleteInternal ();
55 
56         double value;
57         BWidgets::Widget* widget;
58         BWidgets::Widget* internal;
59 };
60 
61 class ItemList : private std::list<Item>
62 {
63 public:
64         ItemList ();
65         ItemList (const Item& item);
66         ItemList (const std::list<Item>& items);
67         ItemList (BWidgets::Widget* widget);
68         ItemList (const std::initializer_list<BWidgets::Widget*>& widgets);
69         ItemList (const std::string& text);
70         ItemList (const std::initializer_list<std::string>& texts);
71 
72         using std::list<Item>::operator=;
73         using std::list<Item>::begin;
74         using std::list<Item>::end;
75         using std::list<Item>::size;
76         using std::list<Item>::empty;
77         using std::list<Item>::front;
78         using std::list<Item>::back;
79         using std::list<Item>::pop_back;
80         using std::list<Item>::pop_front;
81         using std::list<Item>::erase;
82         using std::list<Item>::iterator;
83 
84         void push_back (const Item& item);
85         void push_back (BWidgets::Widget* widget);
86         void push_back (const std::string& text);
87         // TODO void push_front
88         // TODO iterator insert
89 
90         Item* getItem (const double value);
91         // TODO renumber
92 
93 protected:
94         double getNextValue () const;
95 };
96 
97 }
98 
99 #endif /* BWIDGETS_BITEMS_HPP_ */
100