bezier_editor.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* bezier_editor.cpp - bezier editor helper widget
  2. * Copyright (C) 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. #include <core/action_stack.h>
  18. #include <generic/node_editor.h>
  19. #include <generic/custom_widgets.h>
  20. #include "ui_bezier_editor.h"
  21. namespace rainynite::studio {
  22. class BezierEditorWidget : public QWidget, public NodeEditor {
  23. public:
  24. BezierEditorWidget() :
  25. ui(make_unique<Ui::BezierEditorWidget>())
  26. {
  27. ui->setupUi(this);
  28. connect(ui->is_closed, &QCheckBox::toggled, this, &BezierEditorWidget::set_closed);
  29. }
  30. virtual ~BezierEditorWidget() {
  31. }
  32. void node_update() override {
  33. if (auto node = get_node_as<Geom::BezierKnots>()) {
  34. ui->is_closed->setCheckable(node->can_set_any_at());
  35. ui->is_closed->setChecked(node->value(get_core_context()).closed);
  36. } else {
  37. // error
  38. ui->is_closed->setCheckable(false);
  39. ui->is_closed->setChecked(false);
  40. }
  41. }
  42. protected:
  43. void set_closed(bool closed) {
  44. if (auto action_stack = get_context()->action_stack()) {
  45. if (auto node = get_node_as<Geom::BezierKnots>()) {
  46. if (node->can_set_any_at()) {
  47. if (auto maybe_path = get_value<Geom::BezierKnots>()) {
  48. auto path = *std::move(maybe_path);
  49. if (path.closed == closed)
  50. return;
  51. path.closed = closed;
  52. write_value(path);
  53. close_action();
  54. }
  55. }
  56. }
  57. }
  58. }
  59. private:
  60. unique_ptr<Ui::BezierEditorWidget> ui;
  61. };
  62. REGISTER_CUSTOM_WIDGET(BezierEdit, Geom::BezierKnots, BezierEditorWidget);
  63. } // namespace rainynite::studio