dock_registry.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* dock_registry.h - Dock registry
  2. * Copyright (C) 2017-2018 caryoscelus
  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 <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_GENERIC_DOCK_REGISTRY_H_D4B0FD04_ED8E_5231_9949_64326B0FE611
  18. #define STUDIO_GENERIC_DOCK_REGISTRY_H_D4B0FD04_ED8E_5231_9949_64326B0FE611
  19. #include <core/std/memory.h>
  20. #include <Qt>
  21. #include <core/util/class_init.h>
  22. class QDockWidget;
  23. namespace rainynite::studio {
  24. class EditorContext;
  25. class DockFactory {
  26. public:
  27. virtual unique_ptr<QDockWidget> operator()(shared_ptr<EditorContext> context) const = 0;
  28. virtual Qt::DockWidgetArea preferred_area() const = 0;
  29. };
  30. inline unique_ptr<QDockWidget> spawn_dock(std::string const& name, shared_ptr<EditorContext> context) {
  31. return class_init::name_info<DockFactory>(name)(context);
  32. }
  33. inline Qt::DockWidgetArea dock_preferred_area(std::string const& name) {
  34. return class_init::name_info<DockFactory>(name).preferred_area();
  35. }
  36. inline map<std::string, DockFactory*> const& get_all_docks() {
  37. return class_init::string_registry<DockFactory>();
  38. }
  39. } // namespace rainynite::studio
  40. #define ABSTRACT_REGISTER_DOCK(Name, Dock, init, position) \
  41. class Dock##Factory : \
  42. public DockFactory, \
  43. private class_init::StringRegistered<Dock##Factory, DockFactory> \
  44. { \
  45. public: \
  46. static string name() { \
  47. return Name; \
  48. } \
  49. unique_ptr<QDockWidget> operator()(shared_ptr<EditorContext> context) const override { \
  50. return init(std::move(context)); \
  51. } \
  52. Qt::DockWidgetArea preferred_area() const override { \
  53. return position; \
  54. } \
  55. }
  56. #define REGISTER_CONTEXT_DOCK(Name, Dock, position) \
  57. ABSTRACT_REGISTER_DOCK(Name, Dock, make_unique<Dock>, position)
  58. #define REGISTER_DOCK(Name, Dock, position) \
  59. ABSTRACT_REGISTER_DOCK(Name, Dock, [](auto) { return make_unique<Dock>(); }, position)
  60. #endif