1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2017-2018 Gleb Mishchenko, The appleseedhq Organization
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28 
29 // XXX: must be included before Qt headers because of `slot' redefine
30 #include "python/pythoninterpreter.h"
31 
32 // appleseed.studio headers.
33 #include "mainwindow/mainwindow.h"
34 #include "mainwindow/minimizebutton.h"
35 
36 // appleseed.foundation headers.
37 #include "foundation/core/exceptions/exception.h"
38 #include "foundation/platform/python.h"
39 #include "foundation/utility/casts.h"
40 
41 // appleseed.shared headers.
42 #include "application/application.h"
43 
44 // Qt headers.
45 #include <QDockWidget>
46 
47 // Standard headers.
48 #include <cstdint>
49 
50 namespace bpy = boost::python;
51 using namespace appleseed::studio;
52 using namespace foundation;
53 using namespace renderer;
54 
main_window()55 MainWindow* main_window()
56 {
57     return PythonInterpreter::instance().get_main_window();
58 }
59 
project_manager()60 ProjectManager* project_manager()
61 {
62     return main_window()->get_project_manager();
63 }
64 
new_project()65 void new_project()
66 {
67     main_window()->new_project();
68 }
69 
open_project(const char * project_path)70 bool open_project(const char* project_path)
71 {
72     return main_window()->open_project(project_path);
73 }
74 
save_project()75 bool save_project()
76 {
77     const Project* project = project_manager()->get_project();
78     return
79         project != nullptr && project->has_path()
80             ? main_window()->save_project(project->get_path())
81             : false;
82 }
83 
save_project_as(const char * project_path)84 bool save_project_as(const char* project_path)
85 {
86     return main_window()->save_project(project_path);
87 }
88 
pack_project_as(const char * project_path)89 bool pack_project_as(const char* project_path)
90 {
91     return main_window()->pack_project(project_path);
92 }
93 
close_project()94 void close_project()
95 {
96     main_window()->close_project();
97 }
98 
current_project()99 Project* current_project()
100 {
101     return project_manager()->get_project();
102 }
103 
is_project_dirty()104 bool is_project_dirty()
105 {
106     return project_manager()->is_project_dirty();
107 }
108 
set_project_dirty()109 void set_project_dirty()
110 {
111     project_manager()->set_project_dirty_flag();
112 }
113 
main_window_as_pylong()114 bpy::long_ main_window_as_pylong()
115 {
116     const auto ptr = binary_cast<std::uintptr_t>(main_window());
117     return bpy::long_(ptr);
118 }
119 
create_dock_widget(const char * dock_name)120 bpy::long_ create_dock_widget(const char* dock_name)
121 {
122     const QDockWidget* dock_widget = main_window()->create_dock_widget(dock_name);
123     const auto ptr = binary_cast<std::uintptr_t>(dock_widget);
124     return bpy::long_(ptr);
125 }
126 
BOOST_PYTHON_MODULE(_appleseedstudio)127 BOOST_PYTHON_MODULE(_appleseedstudio)
128 {
129     bpy::def("new_project", new_project);
130     bpy::def("open_project", open_project, bpy::args("project_path"));
131     bpy::def("save_project", save_project);
132     bpy::def("save_project_as", save_project_as, bpy::args("project_path"));
133     bpy::def("pack_project_as", pack_project_as, bpy::args("project_path"));
134     bpy::def("close_project", close_project);
135 
136     bpy::def("current_project", current_project, bpy::return_value_policy<bpy::reference_existing_object>());
137     bpy::def("is_project_dirty", is_project_dirty);
138     bpy::def("set_project_dirty", set_project_dirty);
139 
140     bpy::def("main_window", main_window_as_pylong);
141     bpy::def("create_dock_widget", create_dock_widget, bpy::args("dock_name"));
142 
143     boost::python::def("get_root_path", appleseed::shared::Application::get_root_path);
144 }
145