OpenStructure
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-2020 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:
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
const char * GetSlotMethod() const
Definition: tool_option.hh:132
ToolOptionButton(const String &key, const String &verbose_name, QObject *receiver, const char *slot_method)
QObject * GetReceiver() const
Definition: tool_option.hh:133
ConstIterator Begin() const
Definition: tool_option.hh:117
void Add(const String &text, int tag)
ToolOptionEnum(const String &key, const String &verbose_name)
std::vector< Tuple >::const_iterator ConstIterator
Definition: tool_option.hh:106
void SetIndex(int index)
Definition: tool_option.hh:112
ConstIterator End() const
Definition: tool_option.hh:118
const String & GetKey() const
Type GetType() const
const String & GetVerboseName() const
ToolOption(const String &key, const String &verbose_name, Type type)
bool SetValue(T value)
Definition: tool_option.hh:74
ToolOptionNum(const String &key, const String &verbose_name, T default_v, T min_value=std::numeric_limits< T >::min(), T max_value=std::numeric_limits< T >::max())
Definition: tool_option.hh:61
#define DLLEXPORT_OST_GUI
std::string String
Definition: base.hh:54
ToolOptionNum< int, ToolOption::INT > ToolOptionInt
Definition: tool_option.hh:89
std::vector< ToolOption * > ToolOptionList
Definition: tool_option.hh:140
ToolOptionNum< float, ToolOption::FLOAT > ToolOptionFloat
Definition: tool_option.hh:90
Definition: base.dox:1