summaryrefslogtreecommitdiffstats
path: root/src/gui/itemselector.cpp
blob: c02fffd946a6d040cf41f71cf18977f70f805e45 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/***************************************************************************
 *   Copyright (C) 2003,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 <vector> // Temporay fix for pthread.h problem
#include "circuitdocument.h"
#include "docmanager.h"
#include "flowcodedocument.h"
#include "itemdocument.h"
#include "itemlibrary.h"
#include "itemselector.h"
#include "libraryitem.h"
#include "mechanicsdocument.h"

#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>

#include <tqdragobject.h>
#include <tqlayout.h>
#include <tqpopupmenu.h>
#include <tqwhatsthis.h>

#include <assert.h>

ILVItem::ILVItem( TQListView* tqparent, const TQString &id )
	: KListViewItem( tqparent, 0 )
{
	m_id = id;
	b_isRemovable = false;
	m_pProjectItem = 0l;
}

ILVItem::ILVItem( TQListViewItem* tqparent, const TQString &id )
	: KListViewItem( tqparent, 0 )
{
	m_id = id;
	b_isRemovable = false;
	m_pProjectItem = 0l;
}


ItemSelector::ItemSelector( TQWidget *tqparent, const char *name )
	: KListView( tqparent, name )
{
    addColumn( i18n( "Component" ) );
	setFullWidth(true);
	setSorting( -1, FALSE );
    setRootIsDecorated(true);
    setDragEnabled(true);
	
// 	connect( this, TQT_SIGNAL(executed(TQListViewItem*) ), this, TQT_SLOT(slotItemExecuted(TQListViewItem*)) );
	connect( this, TQT_SIGNAL(clicked(TQListViewItem*)), this, TQT_SLOT(slotItemClicked(TQListViewItem*)) );
	connect( this, TQT_SIGNAL(doubleClicked(TQListViewItem*)), this, TQT_SLOT(slotItemDoubleClicked(TQListViewItem*)) );
	connect( this, TQT_SIGNAL(contextMenuRequested(TQListViewItem*, const TQPoint&, int )), this, TQT_SLOT(slotContextMenuRequested(TQListViewItem*, const TQPoint&, int )) );
}

ItemSelector::~ItemSelector()
{
	writeOpenStates();
}


void ItemSelector::clear()
{
	m_categories.clear();
	KListView::clear();
}


void ItemSelector::addItem( const TQString & caption, const TQString & id, const TQString & _category, const TQPixmap & icon, bool removable )
{
	ILVItem *tqparentItem = 0L;
	
	TQString category = _category;
	if ( !category.startsWith("/") ) {
		category.prepend('/');
	}
	
	do
	{
		category.remove(0,1);
		TQString cat;
		category.tqreplace( "\\/", "|" );
		int pos = category.tqfind('/');
		if ( pos == -1 ) cat = category;
		else cat = category.left( pos );
		
		cat.tqreplace( "|", "/" );
	
		if ( m_categories.tqfindIndex(cat) == -1 )
		{
			m_categories.append(cat);
			
			if (tqparentItem) {
				tqparentItem = new ILVItem( tqparentItem, "" );
			}
			else {
				tqparentItem = new ILVItem( this, "" );
			}
			tqparentItem->setOpen( readOpenState(cat) );
			
			tqparentItem->setExpandable(true);
			tqparentItem->setText( 0, cat );
		}
		else
		{
			tqparentItem = (ILVItem*)tqfindItem( cat, 0 );
		}
		
		category.remove( 0, pos );
	} while ( category.tqcontains('/') );
	
	if ( !tqparentItem )
	{
		kdError() << "Unexpected error in finding tqparent item for category list"<<endl;
		return;
	}
	
	ILVItem *item = new ILVItem( tqparentItem, id );
	item->setPixmap( 0, icon );
	item->setText( 0, caption );
	item->setRemovable(removable);
}


void ItemSelector::writeOpenStates()
{
	KConfig *config = kapp->config();
	config->setGroup( name() );
	
	const TQStringList::iterator end = m_categories.end();
	for ( TQStringList::iterator it = m_categories.begin(); it != end; ++it )
	{
		TQListViewItem *item = tqfindItem( *it, 0 );
		if (item) {
			config->writeEntry( *it+"IsOpen", item->isOpen() );
		}
	}
}


bool ItemSelector::readOpenState( const TQString &id )
{
	KConfig *config = kapp->config();
	config->setGroup( name() );
	
	return config->readBoolEntry( id+"IsOpen", true );
}


void ItemSelector::slotContextMenuRequested( TQListViewItem* item, const TQPoint& pos, int /*col*/ )
{
	if ( !item || !(static_cast<ILVItem*>(item))->isRemovable() ) {
		return;
	}
	
	TQPopupMenu *menu = new TQPopupMenu(this);
	menu->insertItem( i18n("Remove %1").tqarg(item->text(0)), this, TQT_SLOT(slotRemoveSelectedItem()), TQt::Key_Delete );
	menu->popup(pos);
}


void ItemSelector::slotRemoveSelectedItem()
{
	ILVItem *item = dynamic_cast<ILVItem*>(selectedItem());
	if (!item)
		return;
	
	emit itemRemoved( item->key( 0, 0 ) );
	ILVItem *tqparent = dynamic_cast<ILVItem*>(item->TQListViewItem::tqparent());
	delete item;
	// Get rid of the category as well if it has no tqchildren
	if ( tqparent && !tqparent->firstChild() )
	{
		m_categories.remove(tqparent->text(0));
		delete tqparent;
	}
}


void ItemSelector::setListCaption( const TQString &caption )
{
	setColumnText( 0, caption );
}



void ItemSelector::slotItemClicked( TQListViewItem *item )
{
	if (!item)
		return;
	
	if ( ItemDocument * itemDocument = dynamic_cast<ItemDocument*>(DocManager::self()->getFocusedDocument()) )
		itemDocument->slotUnsetRepeatedItemId();
	
	emit itemClicked( item->key( 0, 0 ) );
}


void ItemSelector::slotItemDoubleClicked( TQListViewItem *item )
{
	if (!item)
		return;
	
	TQString id = item->key( 0, 0 );
	
	if ( Document * doc = DocManager::self()->getFocusedDocument() )
	{
		if ( doc->type() == Document::dt_flowcode && id.startsWith("flow/") )
			(static_cast<FlowCodeDocument*>(doc))->slotSetRepeatedItemId(id);
		
		else if ( doc->type() == Document::dt_circuit && (id.startsWith("ec/") || id.startsWith("sc/")) )
			(static_cast<CircuitDocument*>(doc))->slotSetRepeatedItemId(id);
		
		else if ( doc->type() == Document::dt_mechanics && id.startsWith("mech/") )
			(static_cast<MechanicsDocument*>(doc))->slotSetRepeatedItemId(id);
	}
	
	emit itemDoubleClicked(id);
}


TQDragObject* ItemSelector::dragObject()
{
	const TQString id = currentItem()->key(0,0);
	
	TQStoredDrag * d = 0l;
	
	if ( id.startsWith("flow/") )
		d = new TQStoredDrag( "ktechlab/flowpart", this );
	
	else if ( id.startsWith("ec/") )
		d = new TQStoredDrag( "ktechlab/component", this );
	
	else if ( id.startsWith("sc/") )
		d = new TQStoredDrag( "ktechlab/subcircuit", this );
	
	else if ( id.startsWith("mech/") )
		d = new TQStoredDrag( "ktechlab/mechanical", this );
	
	if (d)
	{
		TQByteArray data;
		TQDataStream stream( data, IO_WriteOnly );
		stream << id;
		d->setEncodedData(data);
	}
	
	// A pixmap cursor is often hard to make out
// 	TQPixmap *pixmap = const_cast<TQPixmap*>(currentItem()->pixmap(0));
// 	if (pixmap) d->setPixmap(*pixmap);

    return d;
}



//BEGIN class ComponentSelector
ComponentSelector * ComponentSelector::m_pSelf = 0l;


ComponentSelector * ComponentSelector::self( KateMDI::ToolView * tqparent )
{
	if (!m_pSelf)
	{
		assert(tqparent);
		m_pSelf = new ComponentSelector(tqparent);
	}
	return m_pSelf;
}


ComponentSelector::ComponentSelector( KateMDI::ToolView * tqparent )
	: ItemSelector( (TQWidget*)tqparent, "Component Selector" )
{
	TQWhatsThis::add( this, i18n(
			"Add components to the circuit diagram by dragging them into the circuit.<br><br>"
			
			"To add more than one component of the same type, doubleclick on a component, and click repeatedly in the circuit to place the component. Right click to stop placement.<br><br>"
			
			"Some components (such as subcircuits) can be removed by right clicking on the item and selecting \"Remove\"."
							   ) );
	
	setListCaption( i18n("Component") );
	
	LibraryItemList *items = itemLibrary()->items();
	const LibraryItemList::iterator end = items->end();
	for ( LibraryItemList::iterator it = items->begin(); it != end; ++it )
	{
		if ( (*it)->type() == LibraryItem::lit_component )
			addItem( (*it)->name(), (*it)->activeID(), (*it)->category(), (*it)->icon16() );
	}
}
//END class ComponentSelector



//BEGIN class FlowPartSelector
FlowPartSelector * FlowPartSelector::m_pSelf = 0l;


FlowPartSelector * FlowPartSelector::self( KateMDI::ToolView * tqparent )
{
	if (!m_pSelf)
	{
		assert(tqparent);
		m_pSelf = new FlowPartSelector(tqparent);
	}
	return m_pSelf;
}


FlowPartSelector::FlowPartSelector( KateMDI::ToolView * tqparent )
	: ItemSelector( (TQWidget*)tqparent, "Part Selector" )
{
	TQWhatsThis::add( this, i18n("Add FlowPart to the FlowCode document by dragging them there.<br><br>To add more than one FlowPart of the same type, doubleclick on a FlowPart, and click repeatedly in the FlowChart to place the component. Right click to stop placement.") );
	
	setListCaption( i18n("Flow Part") );
	
	LibraryItemList *items = itemLibrary()->items();
	const LibraryItemList::iterator end = items->end();
	for ( LibraryItemList::iterator it = items->begin(); it != end; ++it )
	{
		if ( (*it)->type() == LibraryItem::lit_flowpart )
			addItem( (*it)->name(), (*it)->activeID(), (*it)->category(), (*it)->icon16() );
	}
}
//END class FlowPartSelector


//BEGIN class MechanicsSelector
MechanicsSelector * MechanicsSelector::m_pSelf = 0l;


MechanicsSelector * MechanicsSelector::self( KateMDI::ToolView * tqparent )
{
	if (!m_pSelf)
	{
		assert(tqparent);
		m_pSelf = new MechanicsSelector( (TQWidget*)tqparent );
	}
	return m_pSelf;
}


MechanicsSelector::MechanicsSelector( TQWidget *tqparent )
	: ItemSelector( (TQWidget*)tqparent, "Mechanics Selector" )
{
	TQWhatsThis::add( this, i18n("Add mechanical parts to the mechanics work area by dragging them there.") );
	
	LibraryItemList *items = itemLibrary()->items();
	const LibraryItemList::iterator end = items->end();
	for ( LibraryItemList::iterator it = items->begin(); it != end; ++it )
	{
		if ( (*it)->type() == LibraryItem::lit_mechanical )
		{
			addItem( (*it)->name(), (*it)->activeID(), (*it)->category(), (*it)->icon16() );
		}
	}
}
//END class MechanicsSelector


#include "itemselector.moc"