node_string_editor.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* node_string_editor.h - simple text-based node editor
  2. * Copyright (C) 2017 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_WIDGETS_NODE_STRING_EDITOR_H_F0A3331B_DE7D_5F83_883F_370E122FDE44
  18. #define STUDIO_WIDGETS_NODE_STRING_EDITOR_H_F0A3331B_DE7D_5F83_883F_370E122FDE44
  19. #include <fmt/format.h>
  20. #include <QLineEdit>
  21. #include <QDebug>
  22. #include <util/strings.h>
  23. #include <generic/node_editor_widget.h>
  24. namespace rainynite::studio {
  25. class QLineEditWrapper : public QLineEdit {
  26. public:
  27. QLineEditWrapper(QWidget* parent=nullptr) :
  28. QLineEdit(parent)
  29. {}
  30. string value() const {
  31. return util::str(text());
  32. }
  33. void update_value(any const& value) {
  34. using namespace fmt::literals;
  35. string s;
  36. try {
  37. s = core::serialize::value_to_string(value);
  38. } catch (class_init::RuntimeTypeError const& ex) {
  39. qDebug() << "Type Exception: " << ex.what();
  40. s = "<??>";
  41. } catch (std::exception const& ex) {
  42. qDebug() << "Uncaught Exception: " << ex.what();
  43. s = "<Uncaught Exception: {}>"_format(ex.what());
  44. }
  45. setText(util::str(s));
  46. }
  47. };
  48. using NodeStringEditor = UntypedNodeEditorWidget<QLineEditWrapper>;
  49. } // namespace rainynite::studio
  50. #endif