summaryrefslogtreecommitdiffstats
path: root/src/picitem.cpp
blob: dfadedfcdeae806735fa9939a00bcac3d9e4a28d (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
/***************************************************************************
 *   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 "canvasitemparts.h"
#include "flowcodedocument.h"
#include "microinfo.h"
#include "microsettings.h"
#include "microsettingsdlg.h"
#include "micropackage.h"
#include "picitem.h"

#include <kdebug.h>
#include <kiconloader.h>
#include <klocale.h>
#include <tqpainter.h>

static const int InnerWidth = 88;
static const int SidePadding = 24;
static const int TopPadding = 36;
static const int BottomPadding = 42;
static const int PinWidth = 12;
static const int PinSeparation = 16 - PinWidth;
static const int PinLength = 8;
static const int ArcWidth = 22;
static const int PinDirArrow = 3;


//BEGIN class PinItem
PinItem::PinItem( FlowCodeDocument* _view, TQPoint position, bool _onLeft, PinSettings * pinSettings )
	: TQCanvasRectangle(0)
{
	m_pinSettings = pinSettings;
	view = _view;
	onLeft = _onLeft;
	
	connect( m_pinSettings, TQT_SIGNAL(settingsChanged()), this, TQT_SLOT(updateDrawing()) );
	
	if ( TQFontInfo(m_font).pixelSize() > 11 ) // It has to be > 11, not > 12, as (I think) pixelSize() rounds off the actual size
		m_font.setPixelSize(12);
	
	setCanvas( view->canvas() );
	
	move ( position.x(), position.y() );
	initItem();
	setZ( (ICNDocument::Z::RaisedItem + ICNDocument::Z::ResizeHandle)/2 + 1 ); // Hackish, but whatever
}


int PinItem::rtti() const
{
	return ItemDocument::RTTI::Pin;
}


void PinItem::updateDrawing()
{
	update();
}


void PinItem::initItem()
{
	setSize( PinLength, PinWidth );
	setSelected(false);
	setPen( TQt::black );
	calcTextRect();
	show();
}


void PinItem::drawShape( TQPainter& p )
{
	if (!m_pinSettings)
		return;
	
	if ( m_pinSettings->state() == PinSettings::ps_on )
	{
		if ( m_pinSettings->type() == PinSettings::pt_output )
			setBrush( TQColor( 255, 127, 127 ) );
		else
			setBrush( TQColor( 255, 191, 191 ) );
	}
	else
		setBrush( TQt::white );
	
	p.drawRect(rect());
	
	p.setFont(m_font);
	p.setBrush( TQt::NoBrush );
	TQRect r = m_textRect;
	if ( onLeft )
		p.drawText( r, TQt::AlignLeft, m_pinSettings->id() );
	else
		p.drawText( r, TQt::AlignRight, m_pinSettings->id() );
	TQRect br = p.boundingRect( r, TQt::AlignLeft, m_pinSettings->id() );
	
	int left;
	int right;
	if ( onLeft )
	{
		right = (int)x();
		left = right - 8;
	}
	else
	{
		left = (int)x() + PinLength;
		right = left + 8;
	}
	
	int midY = (int)y() + PinWidth/2;
	TQPointArray pa(3);
	int midLeft = left + (8-PinDirArrow)/2;
	int midRight = left + (8+PinDirArrow)/2;
	
	if ( onLeft )
	{
		midLeft--;
		midRight--;
	}
	else
	{
		midLeft++;
		midRight++;
	}
	
	p.setBrush( TQt::black );
	
	// Right facing arrow
	if ( (m_pinSettings->type() == PinSettings::pt_input && onLeft) ||
			 (m_pinSettings->type() == PinSettings::pt_output && !onLeft) )
	{
		pa[0] = TQPoint( midRight, midY );
		pa[1] = TQPoint( midLeft, midY - PinDirArrow );
		pa[2] = TQPoint( midLeft, midY + PinDirArrow );
		p.drawPolygon(pa);
		p.drawLine ( left, midY, right, midY );
	}
	else // Left facing arrow
	{
		pa[0] = TQPoint( midLeft, midY );
		pa[1] = TQPoint( midRight, midY - PinDirArrow );
		pa[2] = TQPoint( midRight, midY + PinDirArrow );
		p.drawPolygon(pa);
		p.drawLine ( left, midY, right, midY );
	}
}


TQRect PinItem::boundingRect () const
{
	TQRect r = m_textRect;
	if ( onLeft )
		r.setLeft( (int)x() - 10 );
	else
		r.setRight( (int)x() + PinLength + 10 );
	
	return r;
}


TQString PinItem::id()
{
	return m_pinSettings->id();
}


void PinItem::switchState()
{
	if ( m_pinSettings->state() == PinSettings::ps_on )
		m_pinSettings->setState(PinSettings::ps_off);
	else
		m_pinSettings->setState(PinSettings::ps_on);
	
	update();
}


void PinItem::dragged( int dx )
{
	if ( (onLeft && dx > 0) ||
	     (!onLeft && dx < 0) )
	{
		m_pinSettings->setType(PinSettings::pt_input);
	}
	else
		m_pinSettings->setType(PinSettings::pt_output);
	
	update();
}


void PinItem::moveBy ( double dx, double dy )
{
	TQCanvasRectangle::moveBy( dx, dy );
	calcTextRect();
}


void PinItem::calcTextRect()
{
	m_textRect = rect();
	m_textRect.moveTop( m_textRect.top()-2 );
	TQRect br;
	
	TQWidget tmpWidget;
	TQPainter p(&tmpWidget);

	p.setFont(m_font);

	if (!m_pinSettings)
	{
		kdDebug() << "PinItem::textRect: No pinSettings!"<<endl;
		return;
	}	
	if ( onLeft )
	{
		m_textRect.setLeft( (int)x() + PinLength + 2 );
		m_textRect.setRight( (int)x() + InnerWidth/2 );
		br = p.boundingRect( m_textRect, TQt::AlignLeft, m_pinSettings->id() );
	}
	else
	{
		m_textRect.setLeft( m_textRect.right() - InnerWidth/2 );
		m_textRect.setRight( (int)x() - 2 );
		br = p.boundingRect( m_textRect, TQt::AlignRight, m_pinSettings->id() );
	}
}
//END class PinItem



//BEGIN class PicItem
PicItem::PicItem( ICNDocument *icnDocument, bool newItem, const char *id, MicroSettings *_microSettings )
	: CNItem( icnDocument, newItem, id ? id : "picitem" )
{
	m_name = "PIC";
	m_type = typeString();
	p_icnDocument = icnDocument;
	icnDocument->registerItem(this);
	
	microSettings = _microSettings;
	const int numPins = microSettings->microInfo()->package()->pinCount( PicPin::type_bidir | PicPin::type_input | PicPin::type_open );
	const int numSide = (numPins/2) + (numPins%2);
	
	m_bExpanded = true;
	m_innerHeight = (numSide+2)*PinWidth + (numSide-1)*PinSeparation;
	updateVisibility();
	
	addButton( "settings", TQRect( SidePadding-8, m_innerHeight+TopPadding+(BottomPadding-24)/2-1, InnerWidth+16, 24 ), i18n("Advanced...") );
	addButton( "expandBtn", TQRect( (TopPadding-22)/2, (TopPadding-22)/2, 22, 22 ), TDEGlobal::iconLoader()->loadIcon( "down", TDEIcon::Small ), true );
	button("expandBtn")->setState(true);
	
	move( 12, 12 );
	
	TQStringList pinIDs = microSettings->microInfo()->package()->pinIDs( PicPin::type_bidir | PicPin::type_input | PicPin::type_open );
	TQStringList::iterator it = pinIDs.begin();
	
	for ( int i=0; i < numSide; ++i, ++it )
	{
		TQPoint position( int(this->x()) + SidePadding - PinLength+1, int(y()) + TopPadding + (i+1)*PinWidth + i*PinSeparation );
		const TQString id = *it;
		PinSettings *settings = microSettings->pinWithID(id);
		m_pinItemList.append( new PinItem( dynamic_cast<FlowCodeDocument*>(icnDocument), position, true, settings ) );
	}
	
	for ( int i=0; i < numPins/2; ++i, ++it )
	{
		TQPoint position( int(this->x()) + SidePadding + InnerWidth-1, int(y()) + TopPadding + m_innerHeight - ( (i+2)*PinWidth + i*PinSeparation ) );
		const TQString id = *it;
		PinSettings *settings = microSettings->pinWithID(id);
		m_pinItemList.append( new PinItem( dynamic_cast<FlowCodeDocument*>(icnDocument), position, false, settings ) );
	}

	setSelected(false);
	setPen( TQt::black );
	updateZ(-1);
	update();
	show();
}


PicItem::~PicItem()
{
	const PinItemList::iterator end = m_pinItemList.end();
	for ( PinItemList::iterator it = m_pinItemList.begin(); it != end; ++it )
		delete *it;
	
	m_pinItemList.clear();
}


void PicItem::updateZ( int baseZ )
{
	(void)baseZ;
	setZ( (ICNDocument::Z::RaisedItem + ICNDocument::Z::ResizeHandle)/2 ); // Hackish, but whatever
	button("settings")->setZ( z()+1 );
	button("expandBtn")->setZ( z()+1 );
}


void PicItem::drawShape( TQPainter & p )
{
	int _x = int(x());
	int _y = int(y());
	
	p.setBrush( TQColor( 0xef, 0xff, 0xef ) );
	p.setFont( font() );
	
	p.drawRoundRect( _x, _y, width(), height(), 2000/width(), 2000/height() );
	
	p.drawText( _x+TopPadding-2, _y, width()-TopPadding+2, TopPadding, TQt::AlignVCenter, i18n("PIC Settings") );
	
	if ( !m_bExpanded )
		return;
	
	// Draw rectangle to cut off pins
	p.setBrush( TQColor( 239, 255, 255 ) );
	TQRect r( _x+SidePadding, _y+TopPadding, InnerWidth, m_innerHeight );
	p.drawRect(r);
	
	// Draw dimple thingy at end of pic
	p.drawArc( r.x()+(r.width()-ArcWidth)/2, r.y()+1-ArcWidth/2, ArcWidth, ArcWidth, 180*16, 180*16 );
	
	// Draw vertical text centered in PIC
	p.translate( r.width()/2 + r.x(), r.height()/2 + r.y() );
	p.rotate(90);
	TQRect textRect( r.width()/-2, r.height()/-2, r.width(), r.height() );
	p.drawText( textRect, TQt::AlignCenter, microSettings->microInfo()->id() );
	
	p.rotate(-90);
	p.translate( r.width()/-2 - r.x(), r.height()/-2 - r.y() );
}


void PicItem::buttonStateChanged( const TQString &id, bool state )
{
	if ( id == "expandBtn" )
	{
		m_bExpanded = state;
		updateVisibility();
	}
	
	else if ( id == "settings" )
	{
		if (!state)
			return;
		
		// Redraw button
		button("settings")->setState(false);
		update();
	
		MicroSettingsDlg *dlg = new MicroSettingsDlg( microSettings, 0L, "microSettingsDlg" );
		connect( dlg, TQT_SIGNAL(okClicked()), this, TQT_SLOT(slotMicroSettingsDlgAccepted()) );
		connect( dlg, TQT_SIGNAL(applyClicked()), this, TQT_SLOT(slotMicroSettingsDlgAccepted()) );
		dlg->show();
		// At this point the PIC is selected but this does not appear to the
		// user so we must deselect it when done.
		p_icnDocument->unselectAll();	
	}
}


void PicItem::updateVisibility()
{
	if (m_bExpanded)
		setSize( 0, 0, InnerWidth+(2*SidePadding), m_innerHeight+TopPadding+BottomPadding, true );
		
	else
		setSize( 0, 0, InnerWidth+(2*SidePadding), TopPadding, true );
	
	const PinItemList::iterator end = m_pinItemList.end();
	for ( PinItemList::iterator it = m_pinItemList.begin(); it != end; ++it )
		(*it)->setVisible(m_bExpanded);
	
	if ( Button * btn = button("settings") )
		btn->setVisible(m_bExpanded);
}


void PicItem::slotMicroSettingsDlgAccepted()
{
	const PinItemList::iterator end = m_pinItemList.end();
	for ( PinItemList::iterator it = m_pinItemList.begin(); it != end; ++it )
		canvas()->setChanged( (*it)->boundingRect() );
	
	p_icnDocument->requestStateSave();
}
//END class PicItem

#include "picitem.moc"