multiline_editor.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* multiline_editor.cpp - edit string with multi-line text editor
  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. #include <QTextEdit>
  18. #include <util/strings.h>
  19. #include <generic/custom_widgets.h>
  20. #include <generic/node_editor_widget.h>
  21. namespace rainynite::studio {
  22. class MultilineEditor : public QTextEdit {
  23. public:
  24. MultilineEditor(QWidget* parent=nullptr) :
  25. QTextEdit(parent)
  26. {}
  27. string value() const {
  28. return util::str(toPlainText());
  29. }
  30. void update_value(string const& value) {
  31. setPlainText(util::str(value));
  32. }
  33. virtual void write_action() = 0;
  34. protected:
  35. void focusOutEvent(QFocusEvent* event) override {
  36. QTextEdit::focusOutEvent(event);
  37. write_action();
  38. }
  39. };
  40. using MultilineTextEditWidget = NodeEditorWidget<MultilineEditor, string>;
  41. REGISTER_CUSTOM_WIDGET(MultilineTextEdit, string, MultilineTextEditWidget);
  42. } // namespace rainynite::studio