summaryrefslogtreecommitdiffstats
path: root/src/gui/propertieslistview.cpp
blob: 560deddab2df5d1c9a8f8078c3e10d1671dff7de (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
/***************************************************************************
 *   Copyright (C) 2003-2004 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 "item.h"
#include "cnitemgroup.h"
#include "itemgroup.h"
#include "iteminterface.h"
#include "pieditor.h"
#include "plvitem.h"
#include "propertieslistview.h"
#include "variant.h"


#include <kdebug.h>
#include <tdelocale.h>
#include <tqevent.h>
#include <tqheader.h>
#include <tqpushbutton.h>
#include <tqvariant.h>

PropertiesListView::PropertiesListView(TQWidget *parent, const char *name)
 : TDEListView(parent, name)
{
	addColumn(i18n("Property"));
	addColumn(i18n("Data"));
	setFullWidth(true);
	setColumnAlignment(1,TQt::AlignRight);
	
	p_lastItem = 0l;
	m_diffBt = 0L;
	
	connect(this,TQT_SIGNAL(selectionChanged(TQListViewItem*)),this,TQT_SLOT(slotSelectionChanged(TQListViewItem*)));
	m_editor = 0L;
	connect(header(),TQT_SIGNAL(sizeChange(int,int,int)),this,TQT_SLOT(headerSizeChanged(int,int,int)));
}

PropertiesListView::~PropertiesListView()
{
}

void PropertiesListView::slotClear()
{
	destroyEditor();
	delete m_diffBt;
	m_diffBt = 0L;
	clear();
	m_plvItemMap.clear();
}

void PropertiesListView::slotCreate( ItemGroup * itemGroup )
{
	if ( !itemGroup || !itemGroup->activeItem() )
	{
		slotClear();
		return;
	}
	
	Item *item = itemGroup->activeItem();
	
	VariantDataMap *vmap = item->variantMap();
	// Build the list
	for( VariantDataMap::iterator vait = vmap->begin(); vait != vmap->end(); ++vait )
	{
		if ( vait.data()->isHidden() )
			continue;
		
		switch( vait.data()->type() )
		{
			case Variant::Type::Int:
			case Variant::Type::Double:
			case Variant::Type::String:
			case Variant::Type::FileName:
			case Variant::Type::Port:
			case Variant::Type::Pin:
			case Variant::Type::VarName:
			case Variant::Type::Combo:
			case Variant::Type::Select:
			case Variant::Type::Bool:
			case Variant::Type::PenStyle:
			case Variant::Type::PenCapStyle:
			case Variant::Type::SevenSegment:
			case Variant::Type::KeyPad:
			{
				m_plvItemMap[vait.key()] = new PLVItem( this, vait.key(), vait.data() );
				break;
			}
			case Variant::Type::Color:
			{
				m_plvItemMap[vait.key()] = new PLVColorItem( this, vait.key(), vait.data() );
				break;
			}
			case Variant::Type::Raw:
			case Variant::Type::Multiline:
			case Variant::Type::None:
			{
				break;
			}
		}
	}
	
	slotUpdate(itemGroup);
}


void PropertiesListView::slotUpdate( ItemGroup * itemGroup )
{
	if ( !itemGroup )
	{
		slotClear();
		return;
	}
	
	const PLVItemMap::iterator end = m_plvItemMap.end();
	for ( PLVItemMap::iterator it = m_plvItemMap.begin(); it != end; ++it )
	{
		it.data()->setEnabled( itemGroup->itemsHaveSameDataValue( it.key() ) );
	}
}

void PropertiesListView::slotSelectionChanged(TQListViewItem *item)
{
	if (!item) return;
	destroyEditor();
	p_lastItem = dynamic_cast<PLVItem*>(item);
	if ( !p_lastItem->data() ) return;
	
	const Variant::Type::Value type = p_lastItem->data()->type();
	switch(type)
	{
		case Variant::Type::String:
		{
			m_editor = new PILineEdit(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Port:
		case Variant::Type::Pin:
		case Variant::Type::Combo:
		case Variant::Type::VarName:
		case Variant::Type::Select:
		case Variant::Type::PenStyle:
		case Variant::Type::PenCapStyle:
		case Variant::Type::SevenSegment:
		case Variant::Type::KeyPad:
		{
			m_editor = new PIStringCombo(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::FileName:
		{
			m_editor = new PIFilename(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Int:
		{
			m_editor = new PIInt(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Double:
		{
			m_editor = new PIDouble(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Color:
		{
 			m_editor = new PIColor(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Bool:
		{
			m_editor = new PIBool(p_lastItem->id(),p_lastItem->data(),this);
			break;
		}
		case Variant::Type::Raw:
		case Variant::Type::Multiline:
		case Variant::Type::None:
		{
			break;
		}
	}
	
	connect(p_lastItem->data(),TQT_SIGNAL(destroyed()),this,TQT_SLOT(destroyEditor())); 
	// Connect so that changes in the editor change the canvas item data.
	connect(m_editor,TQT_SIGNAL(editorDataChanged(const TQString&,TQVariant)),ItemInterface::self(),TQT_SLOT(slotSetData(const TQString&,TQVariant)));
	connect(m_editor,TQT_SIGNAL(editorDataChanged(const TQString&,TQVariant)),this,TQT_SLOT(slotDataChanged(const TQString&,TQVariant)));
	
	int x = columnWidth(0);
	int y = viewportToContents(TQPoint(0,itemRect(p_lastItem).y())).y();
	addChild(m_editor,x,y);
	m_editor->setFocus();
	m_editor->show();
	m_editor->setGeometry(TQRect(x,y,columnWidth(1),itemRect(p_lastItem).height()));
	
	if(p_lastItem->data()->type() == Variant::Type::FileName)
	{
		// The folder button in the KURLComboBox has a minimum size taller than
		// the height of the ListViewItems so this is a temporary kludge to
		// make it look slightly acceptable.
		m_editor->setGeometry(TQRect(x,y,columnWidth(1),itemRect(p_lastItem).height()+7));
	}
	
	// Active the editor as appropriate
	switch(type)
	{
		case Variant::Type::Port:
		case Variant::Type::Pin:
		case Variant::Type::Combo:
		case Variant::Type::VarName:
		case Variant::Type::Select:
		case Variant::Type::PenStyle:
		case Variant::Type::PenCapStyle:
		case Variant::Type::SevenSegment:
		case Variant::Type::KeyPad:
		{
			(static_cast<PIStringCombo*>(m_editor))->popup();
			break;
		}
		case Variant::Type::Color:
		{
			(static_cast<PIColor*>(m_editor))->popup();
			break;
		}
		case Variant::Type::Bool:
		{
			(static_cast<PIBool*>(m_editor))->popup();
			break;
		}
		case Variant::Type::FileName:
		{
			break;
		}
		case Variant::Type::Int:
		{
			break;
		}
		case Variant::Type::Double:
		{
			break;
		}
		case Variant::Type::String:
		{
			break;
		}
		case Variant::Type::Raw:
		case Variant::Type::Multiline:
		case Variant::Type::None:
		{
			break;
		}
	}
}

void PropertiesListView::destroyEditor()
{
	if( !m_editor ) return;
	
	removeChild( m_editor );
	delete m_editor;
	m_editor = 0L;
}

void PropertiesListView::headerSizeChanged(int section, int /*oldSize*/, int newSize)
{
	if( !m_editor || section != 1 ) return;
	
	// Resize the editor to the new column width
	// and move it to the right place.
	TQRect rect = m_editor->geometry();
	rect.setWidth(newSize);
	rect.setX( columnWidth(0) );
	m_editor->setGeometry(rect);
}

void PropertiesListView::slotDataChanged(const TQString &/*id*/, TQVariant data)
{
	PLVItem *pItem = static_cast<PLVItem*>(currentItem());
	pItem->updateData(data);
}

void PropertiesListView::slotMergeProperties()
{
	for( TQListViewItemIterator it( this ); it.current(); ++it )
	{
		PLVItem * pItem = static_cast<PLVItem*>(it.current());
		if (pItem->isEnabled())
			continue;
		
		pItem->setEnabled(true);
		// manually call the updates on the canvas
		// and in the list
		pItem->updateData(pItem->data()->defaultValue());
		ItemInterface::self()->slotSetData(pItem->id(),pItem->data()->defaultValue());
	}
}


void PropertiesListView::slotSetDefaults()
{
	for( TQListViewItemIterator it( this ); it.current(); ++it )
	{
		PLVItem *pItem = static_cast<PLVItem*>(it.current());
		ItemInterface::self()->slotSetData(pItem->id(),pItem->data()->defaultValue());
	}
}


void PropertiesListView::wheelEvent( TQWheelEvent *e )
{
	TQPoint _pos = contentsToViewport(e->pos());
	_pos -= pos();
	_pos.setY( _pos.y()+header()->height() );
	TQListViewItem *item = itemAt(_pos);
	if ( item && item != dynamic_cast<TQListViewItem*>(p_lastItem) )
	{
		e->accept();
		if(!item->isSelected()) slotSelectionChanged(item);
	}
	else TDEListView::wheelEvent(e);
}

#include "propertieslistview.moc"