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) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29 
30 #pragma once
31 
32 // appleseed.studio headers.
33 #include "mainwindow/project/attributeeditor.h"
34 #include "mainwindow/project/entitybrowser.h"
35 #include "mainwindow/project/entityeditor.h"
36 #include "mainwindow/project/entityeditorcontext.h"
37 #include "mainwindow/project/entityeditorformfactorybase.h"
38 #include "mainwindow/project/entityitem.h"
39 #include "mainwindow/project/multimodelentityeditorformfactory.h"
40 #include "mainwindow/project/projectbuilder.h"
41 #include "mainwindow/project/tools.h"
42 
43 // appleseed.renderer headers.
44 #include "renderer/api/entity.h"
45 #include "renderer/api/utility.h"
46 
47 // appleseed.foundation headers.
48 #include "foundation/platform/compiler.h"
49 #include "foundation/utility/containers/dictionary.h"
50 
51 // Standard headers.
52 #include <memory>
53 #include <string>
54 
55 namespace appleseed {
56 namespace studio {
57 
58 //
59 // Entity item class for multi-model entities such as BSDFs, EDFs, etc.
60 //
61 
62 template <typename Entity, typename ParentEntity, typename CollectionItem>
63 class MultiModelEntityItem
64   : public EntityItem<Entity, ParentEntity, CollectionItem>
65 {
66   public:
67     MultiModelEntityItem(
68         EntityEditorContext&    editor_context,
69         Entity*                 entity,
70         ParentEntity&           parent,
71         CollectionItem*         collection_item);
72 
73     foundation::Dictionary get_values() const override;
74 
75   private:
76     typedef EntityItem<Entity, ParentEntity, CollectionItem> Base;
77     typedef typename renderer::EntityTraits<Entity> EntityTraitsType;
78 
79     typedef MultiModelEntityEditorFormFactory<
80         typename EntityTraitsType::FactoryRegistrarType
81     > MultiModelEntityEditorFormFactoryType;
82 
83     void slot_edit(AttributeEditor* attribute_editor) override;
84 };
85 
86 
87 //
88 // MultiModelEntityItem class implementation.
89 //
90 
91 template <typename Entity, typename ParentEntity, typename CollectionItem>
MultiModelEntityItem(EntityEditorContext & editor_context,Entity * entity,ParentEntity & parent,CollectionItem * collection_item)92 MultiModelEntityItem<Entity, ParentEntity, CollectionItem>::MultiModelEntityItem(
93     EntityEditorContext&        editor_context,
94     Entity*                     entity,
95     ParentEntity&               parent,
96     CollectionItem*             collection_item)
97   : Base(editor_context, entity, parent, collection_item)
98 {
99 }
100 
101 template <typename Entity, typename ParentEntity, typename CollectionItem>
get_values()102 foundation::Dictionary MultiModelEntityItem<Entity, ParentEntity, CollectionItem>::get_values() const
103 {
104     foundation::Dictionary values =
105         renderer::EntityTraits<Entity>::get_entity_values(Base::m_entity);
106 
107     values.insert(
108         EntityEditorFormFactoryBase::ModelParameter,
109         Base::m_entity->get_model());
110 
111     return values;
112 }
113 
114 template <typename Entity, typename ParentEntity, typename CollectionItem>
slot_edit(AttributeEditor * attribute_editor)115 void MultiModelEntityItem<Entity, ParentEntity, CollectionItem>::slot_edit(AttributeEditor* attribute_editor)
116 {
117     if (!Base::allows_edition())
118         return;
119 
120     std::unique_ptr<EntityEditor::IFormFactory> form_factory(
121         new MultiModelEntityEditorFormFactoryType(
122             Base::m_editor_context.m_project.template get_factory_registrar<Entity>(),
123             Base::m_entity->get_name()));
124 
125     std::unique_ptr<EntityEditor::IEntityBrowser> entity_browser(
126         new EntityBrowser<ParentEntity>(Base::m_parent));
127 
128     if (attribute_editor)
129     {
130         attribute_editor->edit(
131             std::move(form_factory),
132             std::move(entity_browser),
133             std::unique_ptr<CustomEntityUI>(),
134             get_values(),
135             this,
136             SLOT(slot_edit_accepted(foundation::Dictionary)));
137     }
138     else
139     {
140         const std::string window_title =
141             std::string("Edit ") +
142             EntityTraitsType::get_human_readable_entity_type_name();
143 
144         open_entity_editor(
145             QTreeWidgetItem::treeWidget(),
146             window_title,
147             Base::m_editor_context.m_project,
148             Base::m_editor_context.m_settings,
149             std::move(form_factory),
150             std::move(entity_browser),
151             get_values(),
152             this,
153             SLOT(slot_edit_accepted(foundation::Dictionary)),
154             SLOT(slot_edit_accepted(foundation::Dictionary)),
155             SLOT(slot_edit_accepted(foundation::Dictionary)));
156     }
157 }
158 
159 }   // namespace studio
160 }   // namespace appleseed
161