main_window.cpp 10 KB

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