1 /*
2  * Software License Agreement  (BSD License)
3  *
4  *  Point Cloud Library  (PCL) - www.pointclouds.org
5  *  Copyright  (c) 2012, Jeremie Papon.
6  *
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *
13  *   * Redistributions of source code must retain the above copyright
14  *     notice, this list of conditions and the following disclaimer.
15  *   * Redistributions in binary form must reproduce the above
16  *     copyright notice, this list of conditions and the following
17  *     disclaimer in the documentation and/or other materials provided
18  *     with the distribution.
19  *   * Neither the name of Willow Garage, Inc. nor the names of its
20  *     contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  (INCLUDING,
29  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  *  LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  *  POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/apps/cloud_composer/items/cloud_item.h>
41 
42 #include <QUndoCommand>
43 
44 namespace pcl
45 {
46   namespace cloud_composer
47   {
48     class AbstractTool;
49     class ProjectModel;
50     struct OutputPair
51     {
52       QList <const CloudComposerItem*> input_items_;
53       QList <CloudComposerItem*> output_items_;
54     };
55 
56 
57 
58     class CloudCommand : public QUndoCommand
59     {
60       public:
61         CloudCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
62 
63 
64         ~CloudCommand ();
65 
66         virtual bool
67         runCommand (AbstractTool* tool) = 0;
68 
69         void
70         undo ()  override = 0;
71 
72         void
73         redo () override = 0;
74 
75         //QList <CloudComposerItem*>
76        // executeToolOnTemplateCloud (AbstractTool* tool, ConstItemList &input_data);
77 
78         void
79         setProjectModel (ProjectModel* model);
80 
81         inline void
setInputData(ConstItemList input_data)82         setInputData (ConstItemList input_data)
83         {
84           original_data_ = std::move(input_data);
85         }
86       protected:
87         /** \brief Removes the original item(s) from the model and replaces with the replacement(s)
88          *  Replacements are only inserted once, original items must have same parent
89          *  This stores the removed items in removed_items_
90          */
91         bool
92         replaceOriginalWithNew (const QList <const CloudComposerItem*>& originals, const QList <CloudComposerItem*>& new_items);
93 
94         /** \brief This removes new_items from the model and restores originals */
95         bool
96         restoreOriginalRemoveNew (const QList <const CloudComposerItem*>& originals, const QList <CloudComposerItem*>& new_items);
97 
98         ConstItemList original_data_;
99 
100         QMap <QStandardItem*, QStandardItem*> removed_to_parent_map_;
101         QList <OutputPair> output_data_;
102         ProjectModel* project_model_;
103 
104         /** \brief This determines if we delete original items or not on destruction
105          * If the command is being deleted because stack is at limit, then we want
106          * to only delete the originals, since the command is staying for good (new items shouldn't be deleted)
107          * On the other hand, if we destruct after an undo, then we want to delete the new items (but not the originals)
108          */
109         bool last_was_undo_;
110 
111         /** \brief This is used to check if a templated version of a tool can be used
112          *  For this to return true, all items must be clouds, and must have the same template type
113          */
114         bool
115         canUseTemplates (ConstItemList &input_data);
116 
117         bool can_use_templates_;
118         int template_type_;
119     };
120 
121     class ModifyItemCommand : public CloudCommand
122     {
123       public:
124         ModifyItemCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
125 
126         bool
127         runCommand (AbstractTool* tool) override;
128 
129         void
130         undo () override;
131 
132         void
133         redo () override;
134       private:
135 
136 
137 
138     };
139 
140     class NewItemCloudCommand : public CloudCommand
141     {
142       public:
143         NewItemCloudCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
144 
145         bool
146         runCommand (AbstractTool* tool) override;
147 
148         void
149         undo () override;
150 
151         void
152         redo () override;
153 
154     };
155 
156 
157     class SplitCloudCommand : public CloudCommand
158     {
159       public:
160         SplitCloudCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
161 
162         bool
163         runCommand (AbstractTool* tool) override;
164 
165         void
166         undo () override;
167 
168         void
169         redo () override;
170       private:
171 
172     };
173 
174     class DeleteItemCommand : public CloudCommand
175     {
176       public:
177         DeleteItemCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
178 
179         bool
180         runCommand (AbstractTool* tool) override;
181 
182         void
183         undo () override;
184 
185         void
186         redo () override;
187       private:
188     };
189 
190     class MergeCloudCommand : public CloudCommand
191     {
192       public:
193         /** \brief Construct for a merge command
194          *  \param[in] input_data Input list of CloudItem s from the project model which will be merged
195          *  \param[in] temporary_clouds Input list of CloudItems which
196          */
197         MergeCloudCommand (ConstItemList input_data, QUndoCommand* parent = nullptr);
198 
199         bool
200         runCommand (AbstractTool* tool) override;
201 
202         void
203         undo () override;
204 
205         void
206         redo () override;
207 
208         inline void
setSelectedIndicesMap(const QMap<CloudItem *,pcl::PointIndices::Ptr> & selected_item_index_map)209         setSelectedIndicesMap( const QMap <CloudItem*, pcl::PointIndices::Ptr >& selected_item_index_map)
210         {
211           selected_item_index_map_ = selected_item_index_map;
212         }
213 
214       private:
215         QMap <CloudItem*, pcl::PointIndices::Ptr > selected_item_index_map_;
216     };
217   }
218 }
219 
220 Q_DECLARE_METATYPE (ConstItemList);
221