OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tool_option.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_GUI_TOOL_OPTION_HH
20 #define OST_GUI_TOOL_OPTION_HH
21 
22 /*
23  Author: Marco Biasini
24  */
25 
26 #include <limits>
27 #include <vector>
28 #include <iostream>
29 
30 
31 #include <ost/gui/module_config.hh>
32 #include <QObject>
33 
34 
35 namespace ost { namespace gui {
36 
37 class DLLEXPORT_OST_GUI ToolOption : public QObject {
38  Q_OBJECT
39 public:
40  typedef enum {
41  INT, FLOAT, ENUM, BUTTON
42  } Type;
43 protected:
44  ToolOption(const String& key, const String& verbose_name, Type type);
45 public:
46  const String& GetKey() const;
47  const String& GetVerboseName() const;
48  Type GetType() const;
49 private:
50  String key_;
51  String verbose_name_;
52  Type type_;
53 };
54 
55 
56 
57 
58 template <typename T, ToolOption::Type C>
59 class DLLEXPORT ToolOptionNum : public ToolOption {
60 public:
61  ToolOptionNum(const String& key, const String& verbose_name, T default_v,
62  T min_value=std::numeric_limits<T>::min(),
63  T max_value=std::numeric_limits<T>::max()):
64  ToolOption(key, verbose_name, C), value_(default_v),
65  default_(default_v), min_value_(min_value), max_value_(max_value)
66  {
67 
68  }
69 
70  T GetDefault() const { return default_; }
71  T GetUpperLimit() const { return max_value_; }
72  T GetLowerLimit() const { return min_value_; }
73  T GetValue() const { return value_; }
74  bool SetValue(T value)
75  {
76  if (value>=min_value_ && value<=max_value_) {
77  value_=value;
78  return true;
79  }
80  return false;
81  }
82 private:
83  T value_;
84  T default_;
85  T min_value_;
86  T max_value_;
87 };
88 
91 
92 
93 #if !defined(_MSC_VER)
94  extern template class ToolOptionNum<int,ToolOption::INT>;
95  extern template class ToolOptionNum<float,ToolOption::FLOAT>;
96 #endif
97 
98 
99 
101 public:
102  struct Tuple {
104  int tag;
105  };
106  typedef std::vector<Tuple>::const_iterator ConstIterator;
107 public:
108  ToolOptionEnum(const String& key, const String& verbose_name);
109 
110  void Add(const String& text, int tag);
111 
112  void SetIndex(int index) { index_=index; }
113 
114  int GetIndex() const { return index_; }
115  int GetValue() const { return tuples_[index_].tag; }
116 
117  ConstIterator Begin() const { return tuples_.begin(); }
118  ConstIterator End() const { return tuples_.end(); }
119  size_t Size() { return tuples_.size(); }
120 
121 private:
122  std::vector<Tuple> tuples_;
123  int index_;
124 };
125 
127 public:
128  ToolOptionButton(const String& key,
129  const String& verbose_name,
130  QObject* receiver,
131  const char *slot_method);
132  const char* GetSlotMethod() const { return slot_method_; }
133  QObject* GetReceiver() const { return receiver_; }
134 private:
135  const char *slot_method_;
136  QObject* receiver_;
137 };
138 
139 
140 typedef std::vector<ToolOption*> ToolOptionList;
141 
142 }}
143 
144 #endif