summaryrefslogtreecommitdiffstats
path: root/src/itemgroup.cpp
blob: 1b640d3fa5990080269077ff7170888518e08f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "icndocument.h"
#include "item.h"
#include "itemgroup.h"
#include "mechanicsdocument.h"

#include <qtimer.h>


ItemGroup::ItemGroup( ItemDocument *view, const char *name )
	: QObject( view, name )
{
	m_activeItem = 0l;
	b_itemsAreSameType = true;
	p_view = view;
	p_icnDocument = dynamic_cast<ICNDocument*>(p_view);
	p_mechanicsDocument = dynamic_cast<MechanicsDocument*>(p_view);
	QTimer::singleShot( 0, this, SLOT(getViewPtrs()) );
}


ItemGroup::~ItemGroup()
{
}


void ItemGroup::getViewPtrs()
{
	p_icnDocument = dynamic_cast<ICNDocument*>(p_view);
	p_mechanicsDocument = dynamic_cast<MechanicsDocument*>(p_view);
}


ItemList ItemGroup::items( bool excludeParentedItems ) const
{
	if (excludeParentedItems)
		return m_itemList;
	
	ItemList items = m_itemList;
	ItemList parents = m_itemList;
	
	uint oldSize = items.size();
	do
	{
		oldSize = items.size();
		ItemList children;
		
		ItemList::iterator end = parents.end();
		for ( ItemList::iterator it = parents.begin(); it != end; ++it )
			children += (*it)->children();
		
		end = children.end();
		for ( ItemList::iterator it = children.begin(); it != end; ++it )
		{
			if ( children.contains(*it) > 1 )
				*it = 0l;
		}
		children.remove((Item*)0l);
		
		items += children;
		parents = children;
	}
	while ( oldSize != items.size() );
	
	return items;
}


bool ItemGroup::itemsHaveSameDataValue( const QString &id ) const
{
	if ( m_itemList.size() < 1 ) {
		return true;
	}
	
	if (!itemsAreSameType()) {
		return false;
	}
	
	ItemList::const_iterator it = m_itemList.begin();
	const ItemList::const_iterator end = m_itemList.end();
	QVariant firstData = (*it)->property(id)->value();
	for ( ++it; it != end; ++it )
	{
		if ( (*it) && (*it)->property(id) && (*it)->property(id)->value() != firstData ) {
			return false;
		}
	}
	return true;
}


bool ItemGroup::itemsHaveSameData() const
{
	if ( m_itemList.size() < 1 ) {
		return true;
	}
	
	if (!itemsAreSameType()) {
		return false;
	}
	
	VariantDataMap *variantMap = m_itemList.first()->variantMap();
	const VariantDataMap::const_iterator vitEnd = variantMap->end();
	for ( VariantDataMap::const_iterator vit = variantMap->begin(); vit != vitEnd; ++vit )
	{
		if ( !itemsHaveSameDataValue(vit.key()) ) {
			return false;
		}
	}
	return true;
}


bool ItemGroup::itemsHaveDefaultData() const
{
	if (!itemsHaveSameData()) {
		return false;
	}
	
	if ( m_itemList.size() < 1 ) {
		return true;
	}
	
	VariantDataMap *variantMap = (*m_itemList.begin())->variantMap();
	const VariantDataMap::const_iterator vitEnd = variantMap->end();
	for ( VariantDataMap::const_iterator vit = variantMap->begin(); vit != vitEnd; ++vit )
	{
		if ( !vit.data()->isHidden() && vit.data()->value() != vit.data()->defaultValue() )
			return false;
	}
	return true;
}


void ItemGroup::registerItem( Item *item )
{
	if ( !item || m_itemList.contains(item) ) {
		return;
	}
	
	m_itemList += item;
	updateAreSameStatus();
}


void ItemGroup::unregisterItem( Item *item )
{
	if ( m_itemList.remove(item) > 0 ) {
		updateAreSameStatus();
	}
}


void ItemGroup::updateAreSameStatus()
{
	b_itemsAreSameType = true;
	
	if ( m_itemList.size() < 2 ) {
		return;
	}
	
	QString activeId = (*m_itemList.begin())->id();
	int discardIndex = activeId.findRev("__");
	if ( discardIndex != -1 ) activeId.truncate(discardIndex);
	
	const ItemList::iterator end = m_itemList.end();
	for ( ItemList::iterator it = ++m_itemList.begin(); it != end && b_itemsAreSameType; ++it )
	{
		if (*it)
		{
			QString id = (*it)->id();
			discardIndex = id.findRev("__");
			if ( discardIndex != -1 ) id.truncate(discardIndex);
			if ( id != activeId )
			{
				b_itemsAreSameType = false;
			}
		}
	}
}


void ItemGroup::slotAlignHorizontally()
{
	if ( m_itemList.size() < 2 )
		return;
	
	double avg_y = 0.;
	
	const ItemList::iterator end = m_itemList.end();
	for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
		avg_y += (*it)->y();
	
	int new_y = int(avg_y/(8*m_itemList.size()))*8+4;
	
	for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
		(*it)->move( (*it)->x(), new_y );
	
	p_icnDocument->requestStateSave();
}


void ItemGroup::slotAlignVertically()
{
	if ( m_itemList.size() < 2 )
		return;
	
	double avg_x = 0.;
	
	const ItemList::iterator end = m_itemList.end();
	for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
		avg_x += (*it)->x();
	
	int new_x = int(avg_x/(8*m_itemList.size()))*8+4;
	
	for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
		(*it)->move( new_x, (*it)->y() );
	
	p_icnDocument->requestStateSave();
}


void ItemGroup::slotDistributeHorizontally()
{
	if ( m_itemList.size() < 2 )
		return;
	
	// We sort the items by their horizontal position so that we can calculate
	// an average spacing
	typedef std::multimap< double, Item * > DIMap;
	
	DIMap ranked;
	const ItemList::iterator ilend = m_itemList.end();
	for ( ItemList::iterator it = m_itemList.begin(); it != ilend; ++it )
		ranked.insert( std::make_pair( (*it)->x(), *it ) );
	
	double avg_spacing = 0;
	
	Item * previous = 0l;
	const DIMap::iterator rankedEnd = ranked.end();
	for ( DIMap::iterator it = ranked.begin(); it != rankedEnd; ++it )
	{
		Item * item = it->second;
		if (previous)
		{
			double spacing = item->x() + item->offsetX() - (previous->x() + previous->width() + previous->offsetX());
			avg_spacing += spacing;
		}
		previous = item;
	}
	
	avg_spacing /= (m_itemList.size()-1);
	
	DIMap::iterator it = ranked.begin();
	// Position that we are up to
	double at = it->second->x() + it->second->width() + it->second->offsetX();
	for ( ++it; it != rankedEnd; ++it )
	{
		Item * item = it->second;
		double new_x = at - item->offsetX() + avg_spacing;
		item->move( int(new_x/8)*8+4, item->y() );
		at = new_x + item->width() + item->offsetX();
	}
	
	p_icnDocument->requestStateSave();
}


void ItemGroup::slotDistributeVertically()
{
	if ( m_itemList.size() < 2 )
		return;
	
	// We sort the items by their horizontal position so that we can calculate
	// an average spacing
	typedef std::multimap< double, Item * > DIMap;
	
	DIMap ranked;
	const ItemList::iterator ilend = m_itemList.end();
	for ( ItemList::iterator it = m_itemList.begin(); it != ilend; ++it )
		ranked.insert( std::make_pair( (*it)->y(), *it ) );
	
	double avg_spacing = 0;
	
	Item * previous = 0l;
	const DIMap::iterator rankedEnd = ranked.end();
	for ( DIMap::iterator it = ranked.begin(); it != rankedEnd; ++it )
	{
		Item * item = it->second;
		if (previous)
		{
			double spacing = item->y() + item->offsetY() - (previous->y() + previous->height() + previous->offsetY());
			avg_spacing += spacing;
		}
		previous = item;
	}
	
	avg_spacing /= (m_itemList.size()-1);
	
	DIMap::iterator it = ranked.begin();
	// Position that we are up to
	double at = it->second->y() + it->second->height() + it->second->offsetY();
	for ( ++it; it != rankedEnd; ++it )
	{
		Item * item = it->second;
		double new_y = at - item->offsetY() + avg_spacing;
		item->move( item->x(), int(new_y/8)*8+4 );
		at = new_y + item->height() + item->offsetY();
	}
	
	p_icnDocument->requestStateSave();
}

#include "itemgroup.moc"