summaryrefslogtreecommitdiffstats
path: root/src/mechanics/mechanicsdocument.cpp
blob: 98542ef1eb1a3e88fe4a9c58d5cfe8dc52400244 (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
/***************************************************************************
 *   Copyright (C) 2004-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 "canvasmanipulator.h"
#include "documentiface.h"
#include "drawpart.h"
#include "itemlibrary.h"
#include "mechanicsdocument.h"
#include "mechanicsitem.h"
#include "mechanicsgroup.h"
#include "mechanicssimulation.h"
#include "mechanicsview.h"

#include <klocale.h>

MechanicsDocument::MechanicsDocument( const TQString &caption, KTechlab *ktechlab, const char *name )
	: ItemDocument( caption, ktechlab, name )
{
	m_type = Document::dt_mechanics;
	m_pDocumentIface = new MechanicsDocumentIface(this);
	m_fileExtensionInfo = i18n("*.mechanics|Mechanics (*.mechanics)\n*|All Files");
	m_canvas->retune(128);
	
	m_selectList = new MechanicsGroup(this);
	
	m_cmManager->addManipulatorInfo( CMSelect::manipulatorInfo() );
	m_cmManager->addManipulatorInfo( CMDraw::manipulatorInfo() );
	m_cmManager->addManipulatorInfo( CMRightClick::manipulatorInfo() );
	m_cmManager->addManipulatorInfo( CMRepeatedItemAdd::manipulatorInfo() );
	m_cmManager->addManipulatorInfo( CMItemResize::manipulatorInfo() );
	m_cmManager->addManipulatorInfo( CMMechItemMove::manipulatorInfo() );
	m_mechanicsSimulation = new MechanicsSimulation(this);
	requestStateSave();
}


MechanicsDocument::~MechanicsDocument()
{
	m_bDeleted = true;
	
	// Remove all items from the canvas
	selectAll();
	deleteSelection();
	delete m_mechanicsSimulation;
}

View *MechanicsDocument::createView( ViewContainer *viewContainer, uint viewAreaId, const char *name )
{
	ItemView *itemView = new MechanicsView( this, viewContainer, viewAreaId, name );
	handleNewView(itemView);
	return itemView;
}


ItemGroup *MechanicsDocument::selectList() const
{
	return m_selectList;
}




bool MechanicsDocument::isValidItem( const TQString &itemId )
{
	return itemId.startsWith("mech/") || itemId.startsWith("dp/");
}


bool MechanicsDocument::isValidItem( Item *item )
{
	return item && ((dynamic_cast<MechanicsItem*>(item)) || (dynamic_cast<DrawPart*>(item)));
}


Item* MechanicsDocument::addItem( const TQString &id, const TQPoint &p, bool newItem )
{
	if ( !isValidItem(id) )
		return 0l;
	
	Item *item = itemLibrary()->createItem( id, this, newItem );
	if (!item)
		return 0L;
	
	TQRect rect = item->boundingRect();
	
	int dx = (int)(p.x())-rect.width()/2;
	int dy = (int)(p.y())-rect.height()/2;
	
	if ( dx < 16 || dx > canvas()->width() ) dx = 16;
	if ( dy < 16 || dy > canvas()->height() ) dy = 16;
	
	item->move( dx, dy );
	item->show();
	
	registerItem(item);
// 	setModified(true);
	requestStateSave();
	return item;
}


void MechanicsDocument::deleteSelection()
{
	// End whatever editing mode we are in, as we don't want to start editing
	// something that is about to no longer exist...
	m_cmManager->cancelCurrentManipulation();
	
	if ( m_selectList->isEmpty() )
		return;
	
	// We nee to tell the selete items to remove themselves, and then
	// pass the items that have add themselves to the delete list to the
	// CommandAddItems command
	
	m_selectList->deleteAllItems();
	flushDeleteList();
	setModified(true);
	
	// We need to emit this so that property widgets etc...
	// can clear themselves.
	emit itemUnselected(0L);
	requestStateSave();
}


bool MechanicsDocument::registerItem( TQCanvasItem *qcanvasItem )
{
	return ItemDocument::registerItem(qcanvasItem);
}


void MechanicsDocument::appendDeleteList( TQCanvasItem *qcanvasItem )
{
	MechanicsItem *mechItem = dynamic_cast<MechanicsItem*>(qcanvasItem);
	if ( !mechItem || m_itemDeleteList.contains(mechItem) ) {
		return;
	}
	
	m_itemDeleteList.append(mechItem);
	m_itemList.remove(mechItem);
	
	disconnect( mechItem, TQT_SIGNAL(selected(Item*,bool)), this, TQT_SIGNAL(itemSelected(Item*)) );
	disconnect( mechItem, TQT_SIGNAL(unselected(Item*,bool)), this, TQT_SIGNAL(itemUnselected(Item*)) );
	
	mechItem->removeItem();
}


void MechanicsDocument::flushDeleteList()
{
	// Remove duplicate items in the delete list
	ItemList::iterator end = m_itemDeleteList.end();
	for ( ItemList::iterator it = m_itemDeleteList.begin(); it != end; ++it )
	{
		if ( *it && m_itemDeleteList.contains(*it) > 1 )
			*it = 0l;
	}
	m_itemDeleteList.remove(TQGuardedPtr<Item>(0l));
	
	end = m_itemDeleteList.end();
	for ( ItemList::iterator it = m_itemDeleteList.begin(); it != end; ++it )
	{
		m_itemList.remove(*it);
		(*it)->setCanvas(0l);
		delete *it;
	}
}


MechanicsItem* MechanicsDocument::mechanicsItemWithID( const TQString &id )
{
	return dynamic_cast<MechanicsItem*>(itemWithID(id));
}


void MechanicsDocument::selectAll()
{
	const ItemList::iterator end = m_itemList.end();
	for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
	{
		select(*it);
	}
}


void MechanicsDocument::copy()
{
}

#include "mechanicsdocument.moc"