import.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* import.h - "import" actions base
  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_ACTIONS_IMPORT_H_258614B5_435C_5FD5_B0FF_AC3B8137DB0A
  18. #define STUDIO_ACTIONS_IMPORT_H_258614B5_435C_5FD5_B0FF_AC3B8137DB0A
  19. #include <QFileDialog>
  20. #include <core/node/abstract_list.h>
  21. #include <core/node/abstract_node.h>
  22. #include <core/util/nullptr.h>
  23. #include <generic/action.h>
  24. #include <generic/process_node.h>
  25. #include <util/strings.h>
  26. namespace rainynite::studio::actions {
  27. struct ImportError : public std::runtime_error {
  28. ImportError(string const& msg) :
  29. std::runtime_error(msg)
  30. {}
  31. };
  32. /**
  33. * Import file paths
  34. */
  35. template <typename Processor>
  36. class ImportFiles :
  37. public UiAction,
  38. public ContextListener,
  39. FindTargetNode<Processor>
  40. {
  41. public:
  42. void process() override {
  43. if (auto ctx = get_context()) {
  44. if (auto target = this->find_appropriate_target(*no_null(ctx->tree()), ctx)) {
  45. // TODO: filter images
  46. auto strings = QFileDialog::getOpenFileNames(nullptr, "Import layers");
  47. target->start_list();
  48. for (auto const& s : strings)
  49. target->feed(util::str(s));
  50. target->end_list();
  51. } else {
  52. throw ImportError("Cannot find suitable target node.");
  53. }
  54. } else {
  55. throw ImportError("No context! This is a bug.");
  56. }
  57. }
  58. };
  59. } // namespace rainynite::studio::actions
  60. #endif