main_window.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* main_window.cpp - main window
  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. #include <fstream>
  18. #include <fmt/format.h>
  19. #include <QFileDialog>
  20. #include <QInputDialog>
  21. #include <QErrorMessage>
  22. #include <QMessageBox>
  23. #include <QDockWidget>
  24. #include <QActionGroup>
  25. #include <QCloseEvent>
  26. #include <QDebug>
  27. #include <core/document.h>
  28. #include <core/action_stack.h>
  29. #include <core/filters/yaml_reader.h>
  30. #include <core/filters/yaml_writer.h>
  31. #include <version.h>
  32. #include <util/strings.h>
  33. #include <generic/dock_registry.h>
  34. #include <generic/action.h>
  35. #include "renderer.h"
  36. #include "audio.h"
  37. #include "about.h"
  38. #include "main_window.h"
  39. #include "ui_main_window.h"
  40. using namespace fmt::literals;
  41. namespace rainynite::studio {
  42. MainWindow::MainWindow(QWidget* parent) :
  43. QMainWindow(parent),
  44. ContextListener(),
  45. audio_player(make_unique<AudioPlayer>()),
  46. ui(make_unique<Ui::MainWindow>()),
  47. error_box(make_unique<QErrorMessage>()),
  48. tool_actions(make_unique<QActionGroup>(this))
  49. {
  50. setWindowState(Qt::WindowMaximized);
  51. ui->setupUi(this);
  52. renderer = make_shared<Renderer>(ui->canvas);
  53. window_title_template = util::str(windowTitle());
  54. update_title();
  55. connect(ui->action_new, SIGNAL(triggered()), this, SLOT(new_document()));
  56. connect(ui->action_open, SIGNAL(triggered()), this, SLOT(open()));
  57. connect(ui->action_reload, SIGNAL(triggered()), this, SLOT(reload()));
  58. connect(ui->action_save, SIGNAL(triggered()), this, SLOT(save()));
  59. connect(ui->action_save_as, SIGNAL(triggered()), this, SLOT(save_as()));
  60. connect(ui->action_about, SIGNAL(triggered()), this, SLOT(about()));
  61. connect(ui->action_quit, SIGNAL(triggered()), this, SLOT(quit()));
  62. connect(ui->action_undo, &QAction::triggered, [this]() {
  63. undo();
  64. });
  65. connect(ui->action_redo, &QAction::triggered, [this]() {
  66. redo();
  67. });
  68. connect(ui->action_render, SIGNAL(triggered()), renderer.get(), SLOT(render()));
  69. connect(ui->action_render_frame, SIGNAL(triggered()), renderer.get(), SLOT(render_frame()));
  70. connect(ui->action_stop_render, SIGNAL(triggered()), renderer.get(), SLOT(stop_render()));
  71. connect(ui->action_redraw, SIGNAL(triggered()), renderer.get(), SLOT(redraw()));
  72. connect(ui->action_extra_style, SIGNAL(toggled(bool)), renderer.get(), SLOT(toggle_extra_style(bool)));
  73. connect(ui->action_auto_redraw, SIGNAL(toggled(bool)), renderer.get(), SLOT(toggle_auto_redraw(bool)));
  74. connect(
  75. ui->action_mirror,
  76. &QAction::toggled,
  77. [this](bool value) {
  78. ui->canvas->mirror_horizontally(value);
  79. }
  80. );
  81. setup_tools();
  82. add_all_actions_to_menu(*this, *ui->menu_actions, *error_box);
  83. setup_dock_menu();
  84. add_all_docks();
  85. new_document();
  86. renderer->render_frame();
  87. }
  88. MainWindow::~MainWindow() = default;
  89. void MainWindow::new_document() {
  90. document = make_shared<core::Document>();
  91. set_core_context(document->get_default_context());
  92. set_fname("");
  93. }
  94. void MainWindow::open() {
  95. auto fname_qt = QFileDialog::getOpenFileName(this, "Open", "", "RainyNite file (*.rnite)(*.rnite);;All files(*)");
  96. set_fname(util::str(fname_qt));
  97. reload();
  98. }
  99. void MainWindow::reload() {
  100. if (fname.empty())
  101. return;
  102. // TODO: proper filter modularization
  103. unique_ptr<core::DocumentReader> yaml_reader = make_unique<core::filters::YamlReader>();
  104. vector<string> errors;
  105. shared_ptr<core::Document> new_document;
  106. for (auto&& reader : { std::move(yaml_reader) }) {
  107. try {
  108. std::ifstream in(fname);
  109. new_document = reader->read_document(in);
  110. if (new_document == nullptr)
  111. throw std::runtime_error("Unknown parse failure");
  112. set_core_context(new_document->get_default_context());
  113. in.close();
  114. break;
  115. } catch (std::exception const& ex) {
  116. errors.push_back("Uncaught exception in filter:\n{}"_format(ex.what()));
  117. } catch (...) {
  118. errors.push_back("Unknown error while trying to open document via filter\n");
  119. }
  120. }
  121. if (new_document == nullptr) {
  122. auto msg = util::str(std::accumulate(errors.begin(), errors.end(), string("\n\n")));
  123. qDebug() << msg;
  124. error_box->showMessage(msg);
  125. }
  126. document = new_document;
  127. renderer->render_frame();
  128. }
  129. bool MainWindow::save_as() {
  130. QString filter;
  131. auto fname_qt = QFileDialog::getSaveFileName(
  132. this,
  133. "Save",
  134. util::str(fname),
  135. "RainyNite file (*.rnite)(*.rnite);;"
  136. "All files(*)",
  137. &filter
  138. );
  139. if (!fname_qt.isEmpty()) {
  140. if (!filter.contains("*.rnite")) {
  141. error_box->showMessage("Unknown save format");
  142. return false;
  143. }
  144. set_fname(util::str(fname_qt));
  145. return save("yaml");
  146. }
  147. return false;
  148. }
  149. bool MainWindow::save(QString format) {
  150. if (fname.empty()) {
  151. return save_as();
  152. }
  153. if (format.isEmpty())
  154. format = util::str(saved_format);
  155. if (format.isEmpty())
  156. format = "yaml";
  157. // TODO: proper filter modularization
  158. unique_ptr<core::DocumentWriter> writer;
  159. if (format == "yaml") {
  160. writer.reset(new core::filters::YamlWriter());
  161. } else {
  162. error_box->showMessage("Unknown save format");
  163. return false;
  164. }
  165. try {
  166. std::ofstream out(fname);
  167. writer->write_document(out, document);
  168. saved_format = util::str(format);
  169. is_saved = true;
  170. update_title();
  171. return true;
  172. } catch (std::exception const& ex) {
  173. auto msg = util::str("Uncaught exception while saving document:\n{}"_format(ex.what()));
  174. qDebug() << msg;
  175. error_box->showMessage(msg);
  176. } catch (...) {
  177. qDebug() << "Unknown error while saving document";
  178. }
  179. return false;
  180. }
  181. void MainWindow::set_fname(string const& fname_) {
  182. fname = fname_;
  183. get_context()->set_file_name(fname);
  184. renderer->set_fname(fname);
  185. update_title();
  186. }
  187. void MainWindow::update_title() {
  188. setWindowTitle(util::str(fmt::format(
  189. window_title_template,
  190. "status"_a=is_saved ? "" : "*",
  191. "file"_a=fname,
  192. "version"_a=RAINYNITE_STUDIO_VERSION,
  193. "codename"_a=RAINYNITE_STUDIO_CODENAME
  194. )));
  195. }
  196. void MainWindow::about() {
  197. AboutDialog dialog;
  198. dialog.exec();
  199. }
  200. void MainWindow::closeEvent(QCloseEvent* event) {
  201. event->ignore();
  202. quit();
  203. }
  204. void MainWindow::quit() {
  205. if (!is_saved && !confirm_exit())
  206. return;
  207. renderer.reset();
  208. QApplication::quit();
  209. }
  210. bool MainWindow::confirm_exit() {
  211. QMessageBox dialog;
  212. dialog.setText("Document was (perhaps) modified.");
  213. dialog.setInformativeText("Save changes?");
  214. dialog.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  215. dialog.setDefaultButton(QMessageBox::Cancel);
  216. while (true) {
  217. switch (dialog.exec()) {
  218. case QMessageBox::Save:
  219. if (!save())
  220. break;
  221. [[fallthrough]];
  222. case QMessageBox::Discard:
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. }
  229. void MainWindow::add_all_docks() {
  230. for (auto const& e : get_all_docks()) {
  231. add_dock(e.first);
  232. }
  233. }
  234. void MainWindow::add_dock(string const& name) {
  235. auto dock = spawn_dock(name, get_context());
  236. auto position = dock_preferred_area(name);
  237. addDockWidget(position, dock.release());
  238. }
  239. void MainWindow::setup_dock_menu() {
  240. for (auto const& e : get_all_docks()) {
  241. auto name = e.first;
  242. ui->menu_dock->addAction(util::str(name), [this, name]() {
  243. add_dock(name);
  244. });
  245. }
  246. }
  247. void MainWindow::setup_tools() {
  248. connect(
  249. ui->canvas,
  250. &AbstractCanvas::tool_changed,
  251. [this](string const& s) {
  252. auto found = tool_actions_named.find(s);
  253. if (found != tool_actions_named.end()) {
  254. found->second->setChecked(true);
  255. }
  256. }
  257. );
  258. ui->canvas->load_registered_tools();
  259. for (auto const& e : ui->canvas->list_tools()) {
  260. auto const& name = e.first;
  261. auto tool = e.second;
  262. auto action = ui->tools_bar->addAction(
  263. QIcon::fromTheme(util::str(tool->icon())),
  264. util::str(name),
  265. [this, name] () {
  266. ui->canvas->use_tool(name);
  267. }
  268. );
  269. action->setCheckable(true);
  270. tool_actions->addAction(action);
  271. tool_actions_named.emplace(name, action);
  272. }
  273. ui->canvas->use_tool("Default");
  274. }
  275. void MainWindow::set_context(shared_ptr<EditorContext> context_) {
  276. context_->set_file_name(fname);
  277. ContextListener::set_context(context_);
  278. renderer->set_context(context_);
  279. audio_player->set_context(context_);
  280. for (auto dock : findChildren<QWidget*>()) {
  281. if (auto ctx_dock = dynamic_cast<ContextListener*>(dock))
  282. ctx_dock->set_context(context_);
  283. }
  284. auto set_dirty = [this]() {
  285. is_saved = false;
  286. update_title();
  287. };
  288. connect_boost(
  289. get_context()->action_stack()->action_closed,
  290. set_dirty
  291. );
  292. connect_boost(
  293. get_context()->action_stack()->undone_or_redone,
  294. set_dirty
  295. );
  296. }
  297. } // namespace rainynite::studio