xref: /haiku/src/apps/icon-o-matic/shape/commands/RemovePathsCommand.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2006, 2023, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Zardshard
8  */
9 
10 #include "RemovePathsCommand.h"
11 
12 #include <new>
13 
14 #include <Catalog.h>
15 #include <List.h>
16 #include <Locale.h>
17 #include <StringFormat.h>
18 
19 #include "Container.h"
20 #include "PathSourceShape.h"
21 #include "Shape.h"
22 #include "VectorPath.h"
23 
24 
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-RemovePathsCmd"
27 
28 
29 using std::nothrow;
30 
31 
32 RemovePathsCommand::RemovePathsCommand(
33 		Container<VectorPath>* container, const int32* indices, int32 count)
34 	: RemoveCommand<VectorPath>(container, indices, count),
35 	  fShapes(indices && count > 0 ? new (nothrow) BList[count] : NULL)
36 {
37 	if (!fShapes)
38 		return;
39 
40 	// get the shapes associated with each path
41 	for (int32 i = 0; i < fCount; i++) {
42 		if (fItems[i]) {
43 			int32 listenerCount = fItems[i]->CountListeners();
44 			for (int32 j = 0; j < listenerCount; j++) {
45 				PathSourceShape* shape
46 					= dynamic_cast<PathSourceShape*>(fItems[i]->ListenerAtFast(j));
47 				if (shape)
48 					fShapes[i].AddItem((void*)shape);
49 			}
50 		}
51 	}
52 }
53 
54 
55 RemovePathsCommand::~RemovePathsCommand()
56 {
57 	delete[] fShapes;
58 }
59 
60 
61 status_t
62 RemovePathsCommand::InitCheck()
63 {
64 	return fShapes ? B_OK : B_NO_INIT;
65 }
66 
67 
68 status_t
69 RemovePathsCommand::Perform()
70 {
71 	// remove paths from the container
72 	status_t ret = RemoveCommand<VectorPath>::Perform();
73 	if (ret != B_OK)
74 		return ret;
75 	fItemsRemoved = false; // We're not done yet!
76 
77 	// remove paths from shapes that reference them
78 	for (int32 i = 0; i < fCount; i++) {
79 		if (!fItems[i])
80 			continue;
81 		int32 shapeCount = fShapes[i].CountItems();
82 		for (int32 j = 0; j < shapeCount; j++) {
83 			PathSourceShape* shape = (PathSourceShape*)fShapes[i].ItemAtFast(j);
84 			shape->Paths()->RemoveItem(fItems[i]);
85 		}
86 	}
87 	fItemsRemoved = true;
88 
89 	return ret;
90 }
91 
92 
93 status_t
94 RemovePathsCommand::Undo()
95 {
96 	// add paths to the container
97 	status_t ret = RemoveCommand<VectorPath>::Undo();
98 	if (ret != B_OK)
99 		return ret;
100 	fItemsRemoved = true; // We're not done yet!
101 
102 	// add paths to shapes which previously referenced them
103 	for (int32 i = 0; i < fCount; i++) {
104 		if (!fItems[i])
105 			continue;
106 		int32 shapeCount = fShapes[i].CountItems();
107 		for (int32 j = 0; j < shapeCount; j++) {
108 			PathSourceShape* shape = (PathSourceShape*)fShapes[i].ItemAtFast(j);
109 			shape->Paths()->AddItem(fItems[i]);
110 		}
111 	}
112 
113 	fItemsRemoved = false;
114 
115 	return ret;
116 }
117 
118 
119 void
120 RemovePathsCommand::GetName(BString& name)
121 {
122 	static BStringFormat format(B_TRANSLATE("Remove {0, plural, "
123 		"one{path} other{paths}}"));
124 	format.Format(name, fCount);
125 }
126