xref: /haiku/src/apps/cortex/DiagramView/DiagramItemGroup.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // DiagramItemGroup.h (Cortex/DiagramView)
2 //
3 // * PURPOSE
4 //   Basic class for managing and accessing DiagramItem objects.
5 //   Objects of this class can manage one or more of the DiagramItem
6 //   type M_BOX, M_WIRE and M_ENDPOINT. Many methods let you specify
7 //   which type of item you want to deal with.
8 //
9 // * HISTORY
10 //   c.lenz		28sep99		Begun
11 //
12 
13 #ifndef __DiagramItemGroup_H__
14 #define __DiagramItemGroup_H__
15 
16 #include "DiagramItem.h"
17 
18 #include <List.h>
19 #include <Point.h>
20 
21 #include "cortex_defs.h"
22 __BEGIN_CORTEX_NAMESPACE
23 
24 class DiagramItemGroup
25 {
26 
27 public:					// *** ctor/dtor
28 
29 						DiagramItemGroup(
30 							uint32 acceptedTypes,
31 							bool multiSelection = true);
32 
33 	virtual				~DiagramItemGroup();
34 
35 public:					// *** hook functions
36 
37 	// is called whenever the current selection has changed
38 	virtual void		selectionChanged()
39 						{ /*does nothing */ }
40 
41 public:				// *** item accessors
42 
43 	// returns the number of items in the group (optionally only those
44 	// of the given type)
45 	uint32			countItems(
46 							uint32 whichType = DiagramItem::M_ANY) const;
47 
48 	// returns a pointer to the item in the lists which is
49 	// at the given index; if none is found, this function
50 	// returns 0
51 	DiagramItem		   *itemAt(
52 							uint32 index,
53 							uint32 whichType = DiagramItem::M_ANY) const;
54 
55 	// returns a pointer to the item that is currently under
56 	// the given point, and 0 if there is none
57 	DiagramItem		   *itemUnder(
58 							BPoint point);
59 
60 public:					// *** item operations
61 
62 	// adds an item to the group; returns true on success
63 	virtual bool		addItem(
64 							DiagramItem *item);
65 
66 	// removes an item from the group; returns true on success
67 	bool				removeItem(
68 							DiagramItem *item);
69 
70 	// performs a quicksort on a list of items with the provided
71 	// compare function (one is already defined in the DiagramItem
72 	// implementation); can't handle more than one item type at a
73 	// time!
74 	void				sortItems(
75 							uint32 itemType,
76 							int (*compareFunc)(const void *, const void *));
77 
78 	// fires a draw() command at all items of a specific type that
79 	// intersect with the updateRect
80 	void				drawItems(
81 							BRect updateRect,
82 							uint32 whichType = DiagramItem::M_ANY,
83 							BRegion *updateRegion = 0);
84 
85 	// returns in outRegion the region of items that lay "over" the given
86 	// DiagramItem in which; returns false if no items are above or the item
87 	// doesn't exist
88 	bool				getClippingAbove(
89 							DiagramItem *which,
90 							BRegion *outRegion);
91 
92 public:					// *** selection accessors
93 
94 	// returns the type of DiagramItems in the current selection
95 	// (currently only one type at a time is supported!)
96 	uint32			selectedType() const;
97 
98 	// returns the number of items in the current selection
99 	uint32			countSelectedItems() const;
100 
101 	// returns a pointer to the item in the list which is
102 	// at the given index; if none is found, this function
103 	// returns 0
104 	DiagramItem		   *selectedItemAt(
105 							uint32 index) const;
106 
107 	// returns the ability of the group to handle multiple selections
108 	// as set in the constructor
109 	bool				multipleSelection() const
110 						{ return m_multiSelection; }
111 
112 public:					// *** selection related operations
113 
114 	// selects the given item and optionally deselects all others
115 	// (thereby replacing the former selection)
116 	bool				selectItem(
117 							DiagramItem *which,
118 							bool deselectOthers = true);
119 
120 	// simply deselects one item
121 	bool				deselectItem(
122 							DiagramItem *which);
123 
124 	// performs a quicksort on the list of selected items with the
125 	// provided compare function (one is already defined in the DiagramItem
126 	// implementation)
127 	void				sortSelectedItems(
128 							int (*compareFunc)(const void *, const void *));
129 
130 	// select all items of the given type
131 	bool				selectAll(
132 							uint32 itemType = DiagramItem::M_ANY);
133 
134 	// deselect all items of the given type
135 	bool				deselectAll(
136 							uint32 itemType = DiagramItem::M_ANY);
137 
138 	// moves all selected items by a given amount, taking
139 	// item alignment into account; in updateRegion the areas
140 	// that still require updating by the caller are returned
141 	void				dragSelectionBy(
142 							float x,
143 							float y,
144 							BRegion *updateRegion);
145 
146 	// removes all selected items from the group
147 	void				removeSelection();
148 
149 public:					// *** alignment related
150 
151 	// set/get the 'item alignment' grid size; this is used when
152 	// items are being dragged and inserted
153 	void				setItemAlignment(
154 							float horizontal,
155 							float vertical)
156 						{ m_itemAlignment.Set(horizontal, vertical); }
157 	void				getItemAlignment(
158 							float *horizontal,
159 							float *vertical);
160 
161 	// align a given point to the current grid
162 	void				align(
163 							float *x,
164 							float *y) const;
165 	BPoint				align(
166 							BPoint point) const;
167 
168 protected:				// *** accessors
169 
170 	// returns a pointer to the last item returned by the itemUnder()
171 	// function; this can be used by deriving classes to implement a
172 	// transit system
173 	DiagramItem		   *lastItemUnder()
174 						{ return m_lastItemUnder; }
175 
176 	void				resetItemUnder()
177 						{ m_lastItemUnder = 0; }
178 
179 private:				// *** data members
180 
181 	// pointers to the item-lists (one list for each item type)
182 	BList			   *m_boxes;
183 	BList			   *m_wires;
184 	BList			   *m_endPoints;
185 
186 	// pointer to the list containing the current selection
187 	BList			   *m_selection;
188 
189 	// the DiagramItem type(s) of items this group will accept
190 	uint32				m_types;
191 
192 	// specifies the "grid"-size for the frames of items
193 	BPoint				m_itemAlignment;
194 
195 	// can multiple items be selected at once ?
196 	bool				m_multiSelection;
197 
198 	// cached pointer to the item that was found in itemUnder()
199 	DiagramItem		   *m_lastItemUnder;
200 };
201 
202 __END_CORTEX_NAMESPACE
203 #endif /* __DiagramItemGroup_H__ */
204