OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
widget_registry.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_WIDGET_REGISTRY_HH
20 #define OST_GUI_WIDGET_REGISTRY_HH
21 
22 #include <QWidget>
23 #include <QMap>
24 
25 #include <ost/gui/module_config.hh>
26 
27 /*
28  Author: Marco Biasini
29  */
30 namespace ost { namespace gui {
31 
32 class Widget;
33 
34 class DLLEXPORT_OST_GUI WidgetFactory : public QObject {
35 protected:
36  WidgetFactory(const QString& id, const QString& name):
37  id_(id), name_(name)
38  {}
39 public:
40  const QString& GetUniqueID() const { return id_; }
41  const QString& GetFullName() const { return name_; }
42 
43  virtual Widget* Create(QWidget* parent)=0;
44 private:
45  QString id_;
46  QString name_;
47 };
48 
49 #define OST_REGISTER_WIDGET_WITH_DEFAULT_FACTORY(ns, class_name, full_name) \
50  class class_name##Factory : public WidgetFactory { \
51  public: \
52  class_name##Factory(): WidgetFactory(#ns"::"#class_name, full_name) {} \
53  Widget* Create(QWidget* parent) \
54  { \
55  return new class_name(parent); \
56  } \
57  }; \
58  bool class_name##_registered=WidgetRegistry::Instance()-> \
59  RegisterWidgetFactory(new class_name##Factory)
60 
61 #define OST_REGISTER_WIDGET(class_name, factory) \
62  bool class_name##_registered=WidgetRegistry::Instance()-> \
63  RegisterWidgetFactory(new factory)
64 
65 
67 class DLLEXPORT_OST_GUI WidgetRegistry : public QObject {
68 public:
69  static WidgetRegistry* Instance();
73  bool RegisterWidgetFactory(WidgetFactory* factory);
74 
78  bool UnregisterWidgetFactory(const QString& id);
79 
81  Widget* Create(const QString& id, QWidget* parent);
82  QString GetFullName(const QString& id);
83 private:
84  WidgetRegistry() {}
85  QMap<QString, WidgetFactory*> id_to_factory_;
86 };
87 
88 
89 }}
90 
91 #endif
const QString & GetFullName() const
central registry for widgets
#define DLLEXPORT_OST_GUI
WidgetFactory(const QString &id, const QString &name)
Base class for widgets.
Definition: widget.hh:40
const QString & GetUniqueID() const