summaryrefslogtreecommitdiffstats
path: root/knetwalk/src/cell.cpp
blob: 2847f4d2c5e84acc8f729318034747fe2164d0e7 (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
/***************************************************************************
 *   Copyright (C) 2004, 2005 Andi Peredri                                 *
 *   [email protected]                                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation (see COPYING)            *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/

#include <tqpainter.h>
#include <tqimage.h>

#include <kglobal.h>
#include <kiconloader.h>
#include <kstandarddirs.h>

#include "cell.h"

Cell::PixmapMap Cell::connectedpixmap;
Cell::PixmapMap Cell::disconnectedpixmap;

void Cell::initPixmaps()
{
	typedef TQMap<int, TQString> NamesMap;
	NamesMap names;
	names[L]     = "0001";
	names[D]     = "0010";
	names[D|L]   = "0011";
	names[R]     = "0100";
	names[R|L]   = "0101";
	names[R|D]   = "0110";
	names[R|D|L] = "0111";
	names[U]     = "1000";
	names[U|L]   = "1001";
	names[U|D]   = "1010";
	names[U|D|L] = "1011";
	names[U|R]   = "1100";
	names[U|R|L] = "1101";
	names[U|R|D] = "1110";

	NamesMap::ConstIterator it;
	for(it = names.constBegin(); it != names.constEnd(); ++it)
	{
		connectedpixmap[it.key()]=new TQPixmap(KGlobal::iconLoader()->loadIcon(
					locate("data","knetwalk/cable"+it.data()+".png"), KIcon::NoGroup, 32) );

		TQImage image = connectedpixmap[it.key()]->convertToImage();
		for(int y = 0; y < image.height(); y++)
		{
			TQRgb* line = (TQRgb*)image.scanLine(y);
			for(int x = 0; x < image.width(); x++)
			{
				TQRgb pix = line[x];
				if(tqAlpha(pix) == 255)
				{
					int g = (255 + 4 * tqGreen(pix)) / 5;
					int b = (255 + 4 * tqBlue(pix)) / 5;
					int r = (255 + 4 * tqRed(pix)) / 5;
					line[x] = tqRgb(r, g, b);
				}
			}
		}
		disconnectedpixmap[it.key()] = new TQPixmap(image);
	}
}

Cell::Cell(TQWidget* parent, int i) : TQWidget(parent, 0, WNoAutoErase)
{
	angle     = 0;
	light     = 0;
	iindex    = i;
	ddirs     = Free;
	changed   = true;
	connected = false;
	root      = false;
	locked    = false;
}

int Cell::index() const
{
	return iindex;
}

Cell::Dirs Cell::dirs() const
{
	return ddirs;
}

bool Cell::isConnected() const
{
	return connected;
}

bool Cell::isRotated() const
{
	return angle;
}

bool Cell::isLocked() const
{
    return locked;
}

void Cell::setLocked( bool newlocked )
{
	if ( locked == newlocked ) return;
	locked = newlocked;
	changed = true;
	update();
}


void Cell::setDirs(Dirs d)
{
	if(ddirs == d) return;
	ddirs = d;
	changed = true;
	update();
}

void Cell::setConnected(bool b)
{
	if(connected == b) return;
	connected = b;
	changed = true;
	update();
}

void Cell::setRoot(bool b)
{
	if(root == b) return;
	root = b;
	changed = true;
	update();
}

void Cell::setLight(int l)
{
	light = l;
	changed = true;
	update();
}

void Cell::paintEvent(TQPaintEvent*)
{
	if(changed)
	{
		changed = false;
		if ( locked ) {
	    pixmap = KGlobal::iconLoader()->loadIcon(locate("data", "knetwalk/background_locked.png"), KIcon::NoGroup, 32);
		} else {
			pixmap = KGlobal::iconLoader()->loadIcon(locate("data", "knetwalk/background.png"), KIcon::NoGroup, 32);
		}

		TQPainter paint;
		paint.begin(&pixmap);

		if(light)
		{
			paint.setPen(TQPen(white, 5));
			paint.drawLine(0, width() - light, width(), 2 * width() - light);
		}

		if((ddirs != Free) && (ddirs != None))
		{
			double offset = 0;
			if(angle)
			{
				offset = width() / 2;
				paint.translate(offset, offset);
				paint.rotate(angle);
			}

			if(connected)
				paint.drawPixmap(int(-offset), int(-offset), *connectedpixmap[ddirs]);
			else paint.drawPixmap(int(-offset), int(-offset), *disconnectedpixmap[ddirs]);
			paint.resetXForm();

			TQPixmap pix;
			
			if(root)
			{
				pix=KGlobal::iconLoader()->loadIcon(locate("data", "knetwalk/server.png"), KIcon::NoGroup, 32);
			}
			else if(ddirs == U || ddirs == L || ddirs == D || ddirs == R)
			{
				if(connected) 
					pix=KGlobal::iconLoader()->loadIcon(locate("data","knetwalk/computer2.png"),KIcon::NoGroup,32);
				else 
					pix=KGlobal::iconLoader()->loadIcon(locate("data","knetwalk/computer1.png"),KIcon::NoGroup,32);
			}
			paint.drawPixmap(0, 0, pix);
		}
		paint.end();
	}
	bitBlt(this, 0, 0, &pixmap);
}

void Cell::mousePressEvent(TQMouseEvent* e)
{
	if(e->button() == Qt::LeftButton)
		emit lClicked(iindex);
	else if(e->button() == Qt::RightButton)
		emit rClicked(iindex);
	else if(e->button() == Qt::MidButton)
		emit mClicked(iindex);
}

void Cell::rotate(int a)
{
	angle += a;
	changed = true;
	while(angle >= 45)
	{
		angle -= 90;
		int newdirs = Free;
		if(ddirs & U) newdirs |= R;
		if(ddirs & R) newdirs |= D;
		if(ddirs & D) newdirs |= L;
		if(ddirs & L) newdirs |= U;
		setDirs(Dirs(newdirs));
	}
	while(angle < -45)
	{
		angle += 90;
		int newdirs = Free;
		if(ddirs & U) newdirs |= L;
		if(ddirs & R) newdirs |= U;
		if(ddirs & D) newdirs |= R;
		if(ddirs & L) newdirs |= D;
		setDirs(Dirs(newdirs));
	}
	update();
}

#include "cell.moc"