summaryrefslogtreecommitdiffstats
path: root/src/iteminterface.cpp
blob: c1b3521611a852b095f4af792cdb0a002ec9c2ae (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/***************************************************************************
 *   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 "circuitview.h"
#include "colorcombo.h"
#include "contexthelp.h"
#include "cnitem.h"
#include "cnitemgroup.h"
#include "doublespinbox.h"
#include "itemdocument.h"
#include "itemeditor.h"
#include "iteminterface.h"
#include "itemview.h"
#include "ktechlab.h"

#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <knuminput.h>
#include <kurlrequester.h>
#include <ktoolbar.h>
#include <tqapplication.h>
#include <tqlabel.h>
#include <tqcheckbox.h>

#include <assert.h>

ItemInterface * ItemInterface::m_pSelf = 0l;

ItemInterface * ItemInterface::self( KTechlab * ktechlab )
{
	if ( !m_pSelf )
	{
		assert(ktechlab);
		m_pSelf = new ItemInterface(ktechlab);
	}
	return m_pSelf;
}


ItemInterface::ItemInterface( KTechlab * ktechlab )
	: TQObject(ktechlab),
	p_ktechlab(ktechlab)
{
	m_pActiveItemEditorToolBar = 0;
	p_cvb = 0l;
	p_itemGroup = 0l;
	p_lastItem = 0l;
	m_currentActionTicket = -1;
	m_toolBarWidgetID = -1;
}


ItemInterface::~ItemInterface()
{
}


void ItemInterface::slotGetActionTicket()
{
	m_currentActionTicket = p_cvb ? p_cvb->getActionTicket() : -1;
}


void ItemInterface::slotItemDocumentChanged( ItemDocument * doc )
{
	slotClearAll();
	if ( ItemDocument * itemDocument = dynamic_cast<ItemDocument*>((Document*)p_cvb) )
	{
		disconnect( itemDocument, TQT_SIGNAL(itemSelected(Item*)), this, TQT_SLOT(slotUpdateItemInterface()) );
		disconnect( itemDocument, TQT_SIGNAL(itemUnselected(Item*)), this, TQT_SLOT(slotUpdateItemInterface()) );
	}
	
	p_itemGroup = 0l;
	p_cvb = doc;
	
	slotGetActionTicket();
	
	if (!p_cvb)
		return;
	
	connect( p_cvb, TQT_SIGNAL(itemSelected(Item*)), this, TQT_SLOT(slotUpdateItemInterface()) );
	connect( p_cvb, TQT_SIGNAL(itemUnselected(Item*)), this, TQT_SLOT(slotUpdateItemInterface()) );
	
	p_itemGroup = p_cvb->selectList();
	
	slotUpdateItemInterface();
}


void ItemInterface::clearItemEditorToolBar()
{
	if ( m_pActiveItemEditorToolBar && m_toolBarWidgetID != -1 )
		m_pActiveItemEditorToolBar->removeItem(m_toolBarWidgetID);
	m_toolBarWidgetID = -1;
	itemEditTBCleared();
}


void ItemInterface::slotClearAll()
{
	ContextHelp::self()->slotClear();
	ItemEditor::self()->slotClear();
	clearItemEditorToolBar();
	p_lastItem = 0l;
}


void ItemInterface::slotMultipleSelected()
{
	ContextHelp::self()->slotMultipleSelected();
	ItemEditor::self()->slotMultipleSelected();
	clearItemEditorToolBar();
	p_lastItem = 0l;
}


void ItemInterface::slotUpdateItemInterface()
{
	if (!p_itemGroup)
		return;
	
	slotGetActionTicket();
	updateItemActions();
	
	if (!p_itemGroup->itemsAreSameType() )
	{
		slotMultipleSelected();
		return;
	}
	if ( p_lastItem && p_itemGroup->activeItem() )
	{
		ItemEditor::self()->updateMergeDefaults(p_itemGroup);
		return;
	}
	
	p_lastItem = p_itemGroup->activeItem();
	if (!p_lastItem)
	{
		slotClearAll();
		return;
	}
	
	ContextHelp::self()->slotUpdate(p_lastItem);
	ItemEditor::self()->slotUpdate(p_itemGroup);
	if ( CNItem * cnItem = dynamic_cast<CNItem*>((Item*)p_lastItem) )
		ItemEditor::self()->slotUpdate(cnItem);
	
	// Update item editor toolbar
	if ( ItemView * itemView = dynamic_cast<ItemView*>(p_cvb->activeView()) )
	{
		if ( m_pActiveItemEditorToolBar = dynamic_cast<KToolBar*>(p_ktechlab->factory()->container("itemEditorTB",itemView)) )
		{
			m_pActiveItemEditorToolBar->setFullSize( true );
			TQWidget * widget = configWidget();
			m_toolBarWidgetID = 1;
			m_pActiveItemEditorToolBar->insertWidget( m_toolBarWidgetID, 0, widget );
		}
	}
}


void ItemInterface::updateItemActions()
{
	ItemView * itemView = ((ItemDocument*)p_cvb) ? dynamic_cast<ItemView*>(p_cvb->activeView()) : 0l;
	if ( !itemView )
		return;
	
	bool itemsSelected = p_itemGroup && p_itemGroup->itemCount();
	
	itemView->action("edit_raise")->setEnabled(itemsSelected);
	itemView->action("edit_lower")->setEnabled(itemsSelected);
	p_ktechlab->action("edit_cut")->setEnabled(itemsSelected);
	p_ktechlab->action("edit_copy")->setEnabled(itemsSelected);
	
	CNItemGroup * cnItemGroup = dynamic_cast<CNItemGroup*>((ItemGroup*)p_itemGroup);
	CircuitView * circuitView = dynamic_cast<CircuitView*>(itemView);
	
	if ( cnItemGroup && circuitView  )
	{
		circuitView->action("edit_flip")->setEnabled(cnItemGroup->canFlip());
		bool canRotate = cnItemGroup->canRotate();
		circuitView->action("edit_rotate_ccw")->setEnabled(canRotate);
		circuitView->action("edit_rotate_cw")->setEnabled(canRotate);
	}
}


void ItemInterface::setFlowPartOrientation( unsigned orientation )
{
	CNItemGroup *cnItemGroup = dynamic_cast<CNItemGroup*>((ItemGroup*)p_itemGroup);
	if (!cnItemGroup)
		return;
	
	cnItemGroup->setFlowPartOrientation( orientation );
}


void ItemInterface::setComponentOrientation( int angleDegrees, bool flipped )
{
	CNItemGroup *cnItemGroup = dynamic_cast<CNItemGroup*>((ItemGroup*)p_itemGroup);
	if (!cnItemGroup)
		return;
	
	cnItemGroup->setComponentOrientation( angleDegrees, flipped );
}


void ItemInterface::itemEditTBCleared()
{
	m_stringLineEditMap.clear();
	m_stringComboBoxMap.clear();
	m_stringURLReqMap.clear();
	m_intSpinBoxMap.clear();
	m_doubleSpinBoxMap.clear();
	m_colorComboMap.clear();
	m_boolCheckMap.clear();
}


// The bool specifies whether advanced data should be shown
TQWidget * ItemInterface::configWidget()
{
	if ( !p_itemGroup || !p_itemGroup->activeItem() || !m_pActiveItemEditorToolBar )
		return 0l;
	
	VariantDataMap *variantMap = p_itemGroup->activeItem()->variantMap();
	
	TQWidget * tqparent = m_pActiveItemEditorToolBar;
	
	// Create new widget with the toolbar or dialog as the tqparent
	TQWidget * configWidget = new TQWidget( tqparent, "tbConfigWidget" );
	configWidget->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding, 1, 1 ) );
	
	TQHBoxLayout * configLayout = new TQHBoxLayout( configWidget );
// 	configLayout->setAutoAdd( true );
	configLayout->setSpacing( 6 );
	
// 	configLayout->addItem( new TQSpacerItem( 0, 0,  TQSizePolicy::Expanding, TQSizePolicy::Fixed ) );
	
	const VariantDataMap::iterator vaEnd = variantMap->end();
	for ( VariantDataMap::iterator vait = variantMap->begin(); vait != vaEnd; ++vait )
	{
		if ( vait.data()->isHidden() || vait.data()->isAdvanced() )
			continue;
		
		const Variant::Type::Value type = vait.data()->type();
		
		// common to all types apart from bool
		TQString toolbarCaption = vait.data()->toolbarCaption();
		if ( type != Variant::Type::Bool && !toolbarCaption.isEmpty() )
			configLayout->addWidget( new TQLabel( toolbarCaption, configWidget ) );
		
		TQWidget * editWidget = 0l; // Should be set to the created widget
		
		switch( type )
		{
			case Variant::Type::Port:
			case Variant::Type::Pin:
			case Variant::Type::VarName:
			case Variant::Type::Combo:
			case Variant::Type::Select:
			case Variant::Type::KeyPad:
			case Variant::Type::SevenSegment:
			{
				TQString value = vait.data()->value().toString();
				if ( !value.isEmpty() && !vait.data()->allowed().tqcontains(value) )
					vait.data()->appendAllowed(value);
				
				const TQStringList allowed = vait.data()->allowed();
				
				KComboBox * box = new KComboBox(configWidget);
				
				box->insertStringList(allowed);
				box->setCurrentItem(value);
				
				if ( type == Variant::Type::VarName || type == Variant::Type::Combo )
					box->setEditable( true );
				
				m_stringComboBoxMap[vait.key()] = box;
				connectMapWidget( box, TQT_SIGNAL(textChanged(const TQString &)));
				connectMapWidget( box, TQT_SIGNAL(activated(const TQString &)));
					
				editWidget = box;
				break;
			}
			case Variant::Type::FileName:
			{
				TQString value = vait.data()->value().toString();
				if ( !vait.data()->allowed().tqcontains(value) )
					vait.data()->appendAllowed(value);
				
				const TQStringList allowed = vait.data()->allowed();
				
				KURLComboRequester * urlreq = new KURLComboRequester( configWidget );
				urlreq->setFilter( vait.data()->filter() );
				connectMapWidget( urlreq, TQT_SIGNAL(urlSelected(const TQString &)) );
				m_stringURLReqMap[vait.key()] = urlreq;
				
				KComboBox * box = urlreq->comboBox();
				box->insertStringList(allowed);
				box->setEditable( true );
				
				// Note this has to be called after inserting the allowed list
				urlreq->setURL( vait.data()->value().toString() );
				
				// Generally we only want a file name once the user has finished typing out the full file name.
				connectMapWidget( box, TQT_SIGNAL(returnPressed(const TQString &)));
				connectMapWidget( box, TQT_SIGNAL(activated(const TQString &)));
				
				editWidget = urlreq;
				break;
			}
			case Variant::Type::String:
			{
				KLineEdit * edit = new KLineEdit( configWidget );
				
				edit->setText( vait.data()->value().toString() );
				connectMapWidget(edit,TQT_SIGNAL(textChanged(const TQString &)));
				m_stringLineEditMap[vait.key()] = edit;
				editWidget = edit;
				break;
			}
			case Variant::Type::Int:
			{
				KIntSpinBox *spin = new KIntSpinBox( (int)vait.data()->minValue(), (int)vait.data()->maxValue(), 1, vait.data()->value().toInt(), 10, configWidget );
				
				connectMapWidget( spin, TQT_SIGNAL(valueChanged(int)) );
				m_intSpinBoxMap[vait.key()] = spin;
				editWidget = spin;
				break;
			}
			case Variant::Type::Double:
			{
				DoubleSpinBox *spin = new DoubleSpinBox( vait.data()->minValue(), vait.data()->maxValue(), vait.data()->minAbsValue(), vait.data()->value().toDouble(), vait.data()->unit(), configWidget );
				
				connectMapWidget( spin, TQT_SIGNAL(valueChanged(double)));
				m_doubleSpinBoxMap[vait.key()] = spin;
				editWidget = spin;
				break;
			}
			case Variant::Type::Color:
			{
				TQColor value = vait.data()->value().toColor();
				
				ColorCombo * colorBox = new ColorCombo( (ColorCombo::ColorScheme)vait.data()->colorScheme(), configWidget );
				
				colorBox->setColor( value );
				connectMapWidget( colorBox, TQT_SIGNAL(activated(const TQColor &)));
				m_colorComboMap[vait.key()] = colorBox;
				
				editWidget = colorBox;
				break;
			}
			case Variant::Type::Bool:
			{
				const bool value = vait.data()->value().toBool();
				TQCheckBox * box = new TQCheckBox( vait.data()->toolbarCaption(), configWidget );
				
				box->setChecked(value);
				connectMapWidget( box, TQT_SIGNAL(toggled(bool)));
				m_boolCheckMap[vait.key()] = box;
				editWidget = box;
				break;
			}
			case Variant::Type::Raw:
			case Variant::Type::PenStyle:
			case Variant::Type::PenCapStyle:
			case Variant::Type::Multiline:
			case Variant::Type::None:
			{
				// Do nothing, as these data types are not handled in the toolbar
				break;
			}
		}
		
		if ( !editWidget )
			continue;
		
		// In the case of the toolbar, we don't want it too high
		if ( editWidget->height() > tqparent->height()-2 )
			editWidget->setMaximumHeight( tqparent->height()-2 );
		
		switch ( type )
		{
			case Variant::Type::VarName:
			case Variant::Type::Combo:
			case Variant::Type::String:
			{
				TQSizePolicy p( TQSizePolicy::MinimumExpanding, TQSizePolicy::Fixed, 1, 1 );
			
				editWidget->tqsetSizePolicy( p );
				editWidget->setMaximumWidth( 250 );
				break;
			}
			
			case Variant::Type::FileName:
			case Variant::Type::Port:
			case Variant::Type::Pin:
			case Variant::Type::Select:
			case Variant::Type::KeyPad:
			case Variant::Type::SevenSegment:
			case Variant::Type::Int:
			case Variant::Type::Double:
			case Variant::Type::Color:
			case Variant::Type::Bool:
			case Variant::Type::Raw:
			case Variant::Type::PenStyle:
			case Variant::Type::PenCapStyle:
			case Variant::Type::Multiline:
			case Variant::Type::None:
				break;
		}
		
		configLayout->addWidget( editWidget );
	}
	
	configLayout->addItem( new TQSpacerItem( 0, 0,  TQSizePolicy::Expanding, TQSizePolicy::Fixed ) );
	
	return configWidget;
}


void ItemInterface::connectMapWidget( TQWidget *widget, const char *_signal )
{
	connect( widget, _signal, this, TQT_SLOT(tbDataChanged()) );
}


void ItemInterface::tbDataChanged()
{
	// Manual string values
	const KLineEditMap::iterator m_stringLineEditMapEnd = m_stringLineEditMap.end();
	for ( KLineEditMap::iterator leit = m_stringLineEditMap.begin(); leit != m_stringLineEditMapEnd; ++leit )
	{
		slotSetData( leit.key(), leit.data()->text() );
	}
	
	// String values from comboboxes
	const KComboBoxMap::iterator m_stringComboBoxMapEnd = m_stringComboBoxMap.end();
	for ( KComboBoxMap::iterator cmit = m_stringComboBoxMap.begin(); cmit != m_stringComboBoxMapEnd; ++cmit )
	{
		slotSetData( cmit.key(), cmit.data()->currentText() );
	}

	// Colors values from colorcombos
	const ColorComboMap::iterator m_colorComboMapEnd = m_colorComboMap.end();
	for ( ColorComboMap::iterator ccit = m_colorComboMap.begin(); ccit != m_colorComboMapEnd; ++ccit )
	{
		slotSetData( ccit.key(), ccit.data()->color() );
	}
	
	// Bool values from checkboxes
	const TQCheckBoxMap::iterator m_boolCheckMapEnd = m_boolCheckMap.end();
	for ( TQCheckBoxMap::iterator chit = m_boolCheckMap.begin(); chit != m_boolCheckMapEnd; ++chit )
	{
		slotSetData( chit.key(), chit.data()->isChecked() );
	}
	
	const IntSpinBoxMap::iterator m_intSpinBoxMapEnd = m_intSpinBoxMap.end();
	for ( IntSpinBoxMap::iterator it = m_intSpinBoxMap.begin(); it != m_intSpinBoxMapEnd; ++it )
	{
		slotSetData( it.key(), it.data()->value() );
	}
	
	// (?) Combined values from spin boxes and combo boxes
	// (?) Get values from all spin boxes
	
	const DoubleSpinBoxMap::iterator m_doubleSpinBoxMapEnd = m_doubleSpinBoxMap.end();
	for ( DoubleSpinBoxMap::iterator sbit = m_doubleSpinBoxMap.begin(); sbit != m_doubleSpinBoxMapEnd; ++sbit )
	{
// 		VariantDataMap::iterator vait = variantData.tqfind(sbit.key());
		slotSetData( sbit.key(), sbit.data()->value() );
	}
	
	// Filenames from KURLRequesters
	const KURLReqMap::iterator m_stringURLReqMapEnd = m_stringURLReqMap.end();
	for ( KURLReqMap::iterator urlit = m_stringURLReqMap.begin(); urlit != m_stringURLReqMapEnd; ++urlit )
	{
		slotSetData( urlit.key(), urlit.data()->url() );
	}
	
	if (p_cvb)
		p_cvb->setModified(true);
}




void ItemInterface::slotSetData( const TQString &id, TQVariant value )
{
	if ( !p_itemGroup || (p_itemGroup->itemCount() == 0) )
		return;
	
	if ( !p_itemGroup->itemsAreSameType() )
	{
		kdDebug() << k_funcinfo << "Items are not the same type!"<<endl;
		return;
	}
	
	const ItemList itemList = p_itemGroup->items(true);
	const ItemList::const_iterator end = itemList.end();
	for ( ItemList::const_iterator it = itemList.begin(); it != end; ++it )
	{
		if (*it)
			(*it)->property(id)->setValue(value);
	}
	if (p_cvb)
		p_cvb->setModified(true);
	
	
	VariantDataMap * variantMap = (*itemList.begin())->variantMap();
	VariantDataMap::iterator it = variantMap->tqfind(id);
	if ( it == variantMap->end() )
		return;
	
	
	// setData might have been called from the PropertiesListView, so want
	// to see if the toolbar widgets want setting
	
	switch( it.data()->type() )
	{
		case Variant::Type::String:
		{
			KLineEditMap::iterator mit = m_stringLineEditMap.tqfind(id);
			if ( mit != m_stringLineEditMap.end() ) mit.data()->setText( it.data()->value().toString() );
			break;
		}
		case Variant::Type::FileName:
		{
			KURLReqMap::iterator mit = m_stringURLReqMap.tqfind(id);
			if ( mit != m_stringURLReqMap.end() ) mit.data()->setURL( it.data()->value().toString() );
			break;
		}
		case Variant::Type::PenCapStyle:
		case Variant::Type::PenStyle:
		case Variant::Type::Port:
		case Variant::Type::Pin:
		case Variant::Type::VarName:
		case Variant::Type::Combo:
		case Variant::Type::Select:
		case Variant::Type::SevenSegment:
		case Variant::Type::KeyPad:
		{
			KComboBoxMap::iterator mit = m_stringComboBoxMap.tqfind(id);
			if ( mit != m_stringComboBoxMap.end() ) mit.data()->setCurrentItem( it.data()->value().toString() );
			break;
		}
		case Variant::Type::Int:
		{
			IntSpinBoxMap::iterator mit = m_intSpinBoxMap.tqfind(id);
			if ( mit != m_intSpinBoxMap.end() ) {
				KIntSpinBox *sb = mit.data();
				sb->setValue( it.data()->value().toInt() );
			}
			break;
		}
		case Variant::Type::Double:
		{
			DoubleSpinBoxMap::iterator mit = m_doubleSpinBoxMap.tqfind(id);
			if ( mit != m_doubleSpinBoxMap.end() ) {
				DoubleSpinBox *sb = mit.data();
				sb->setValue( it.data()->value().toDouble() );
			}
			break;
		}
		case Variant::Type::Color:
		{
			ColorComboMap::iterator mit = m_colorComboMap.tqfind(id);
			if ( mit != m_colorComboMap.end() ) mit.data()->setColor( it.data()->value().toColor() );
			break;
		}
		case Variant::Type::Bool:
		{
			TQCheckBoxMap::iterator mit = m_boolCheckMap.tqfind(id);
			if ( mit != m_boolCheckMap.end() ) mit.data()->setChecked( it.data()->value().toBool() );
			break;
		}
		case Variant::Type::Raw:
		case Variant::Type::Multiline:
		case Variant::Type::None:
		{
			// This data will never be handled in the toolbar/PLV, so no need to worry about it
			break;
		}
	}
	
	ItemEditor::self()->updateMergeDefaults(p_itemGroup);
	
	if (p_cvb)
		p_cvb->requestStateSave(m_currentActionTicket);
}

#include "iteminterface.moc"