Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Editor.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2015 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OMNI_UI_MIXIN_EDITOR_H
21 #define OMNI_UI_MIXIN_EDITOR_H
22 
23 #include <QLayout>
24 
25 namespace omni {
26  namespace ui {
27  namespace mixin {
28  /// Mixin for a widget that holds an editor widget
29  template<typename WIDGET, typename EDITOR>
30  class Editor {
31  public:
32  /// Widget type
33  typedef WIDGET widget_type;
34 
35  /// Editor widget type, e.g. QDoubleSpinBox
36  typedef EDITOR editor_type;
37 
38  Editor(widget_type *_widget) : widget_(_widget) {}
39 
40  /// Create editor with type
41  template<typename T = editor_type>
42  inline T* createEditor()
43  {
44  if (editor_) return nullptr;
45 
46  editor_ = new T(widget_);
47  T *_e = editorAs<T>();
48  _e->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
49  _e->setStyleSheet("background : transparent");
50 
51  if (widget_->layout()) widget_->layout()->addWidget(_e);
52  editorSetup();
53  hideEditor();
54  return _e;
55  }
56 
57  /// Return pointer editor (const version)
58  editor_type const* editor() const {
59  return editor_;
60  }
61 
62  /// Return pointer editor
64  return editor_;
65  }
66 
67  /// Return editor as type T
68  template<typename T>
69  T* editorAs()
70  {
71  return static_cast<T *>(editor_);
72  }
73 
74  /// Return editor as type T (const version)
75  template<typename T>
76  T const* editorAs() const
77  {
78  return static_cast<T const *>(editor_);
79  }
80 
81  /// Set editor visibility and set focus
82  virtual void setEditorVisible(bool _visible) {
83  if (!editor_) return;
84 
85  editor_->setVisible(_visible);
86 
87  if (_visible) {
88  editor_->setFocus();
89  }
90  }
91 
92  /// Show editor and set focus
93  void showEditor() {
94  setEditorVisible(true);
95  }
96 
97  /// Hide editor
98  void hideEditor() {
99  setEditorVisible(false);
100  }
101 
102  private:
103  virtual void editorSetup() = 0;
104  editor_type *editor_ = nullptr;
105  widget_type *widget_ = nullptr;
106  };
107  }
108  }
109 }
110 
111 #endif /* OMNI_UI_MIXIN_EDITOR_H */
editor_type const * editor() const
Return pointer editor (const version)
Definition: Editor.h:58
virtual void editorSetup()=0
T const * editorAs() const
Return editor as type T (const version)
Definition: Editor.h:76
virtual void setEditorVisible(bool _visible)
Set editor visibility and set focus.
Definition: Editor.h:82
T * createEditor()
Create editor with type.
Definition: Editor.h:42
widget_type * widget_
Definition: Editor.h:105
void showEditor()
Show editor and set focus.
Definition: Editor.h:93
void hideEditor()
Hide editor.
Definition: Editor.h:98
Editor(widget_type *_widget)
Definition: Editor.h:38
editor_type * editor()
Return pointer editor.
Definition: Editor.h:63
WIDGET widget_type
Widget type.
Definition: Editor.h:33
Circular dial widget with range value and editor.
Definition: Dial.h:33
EDITOR editor_type
Editor widget type, e.g. QDoubleSpinBox.
Definition: Editor.h:36
editor_type * editor_
Definition: Editor.h:104
Mixin for a widget that holds an editor widget.
Definition: Editor.h:30
T * editorAs()
Return editor as type T.
Definition: Editor.h:69