tool.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* tool.h - abstract canvas tool
  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_CANVAS_TOOL_H_DE911363_8896_5B67_A000_6B06587D552A
  18. #define STUDIO_CANVAS_TOOL_H_DE911363_8896_5B67_A000_6B06587D552A
  19. #include <QObject>
  20. #include <core/abstract_factory.h>
  21. #include <core/util/class_init.h>
  22. #include "attachable.h"
  23. namespace rainynite::studio {
  24. class CanvasTool : public QObject, public CanvasAttachable {
  25. public:
  26. virtual string icon() const = 0;
  27. virtual string name() const = 0;
  28. bool eventFilter(QObject* target, QEvent* event) override;
  29. };
  30. template <class CanvasT>
  31. struct CanvasToolFactory : public AbstractFactory<CanvasTool> {
  32. virtual size_t priority() const = 0;
  33. };
  34. // TODO: make more generic
  35. template <class CanvasT, class ToolT, size_t P>
  36. struct CanvasToolFactoryInstance :
  37. public CanvasToolFactory<CanvasT>,
  38. private class_init::ListRegistered<
  39. CanvasToolFactoryInstance<CanvasT, ToolT, P>,
  40. CanvasToolFactory<CanvasT>
  41. >
  42. {
  43. unique_ptr<CanvasTool> operator()() const override {
  44. return make_unique<ToolT>();
  45. }
  46. size_t priority() const override {
  47. return P;
  48. }
  49. };
  50. #define REGISTER_CANVAS_TOOL(Tool, CanvasT, P) \
  51. template struct CanvasToolFactoryInstance<CanvasT, Tool, P>
  52. template <class CanvasT>
  53. vector<CanvasToolFactory<CanvasT>*> const& get_canvas_tools() {
  54. return class_init::class_list_registry<CanvasToolFactory<CanvasT>>();
  55. }
  56. } // namespace rainynite::studio
  57. #endif