summaryrefslogtreecommitdiffstats
path: root/arts/builder/createtool.cpp
blob: 9502688489895def04bc8b17f5e5ceff361363a6 (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
    /*

    Copyright (C) 1999 Stefan Westerfeld
                       stefan@space.twc.de

    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.

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    */

#include "createtool.h"

#include "mwidget.h"
#include "structureport.h"

//#include <arts/debug.h>
#include <tqpalette.h>
#include <tqpainter.h>
#include <tdemessagebox.h>
#include <tdelocale.h>

MWidgetTool::MWidgetTool(ModuleWidget *mwidget)
{
	this->mwidget = mwidget;
}

MWidgetTool::~MWidgetTool()
{
}

CreateTool::CreateTool(ModuleWidget *mwidget)
	: MWidgetTool(mwidget)
{
	oldCursor = mwidget->cursor();
	mwidget->setCursor(TQt::crossCursor);

	oldMouseTracking = mwidget->hasMouseTracking();
	mwidget->setMouseTracking(true);

	width = -1;
	height = -1;
	componentRect = TQRect(0, 0, 0, 0);
}

CreateTool::~CreateTool()
{
	mwidget->repaint(componentRect);
	mwidget->setCursor(oldCursor);
	mwidget->setMouseTracking(oldMouseTracking);
}

void CreateTool::mousePressEvent(TQMouseEvent *e)
{
	int x = mwidget->findCol(e->x());
	int y = mwidget->findRow(e->y());

	if(x < 0 || y < 0) return;

	if( e->button() == TQt::LeftButton )
		performCreate(x, y);

	// well, perhaps make this an option
	mwidget->leaveTool(this);
}

void CreateTool::mouseMoveEvent(TQMouseEvent *e)
{
	if(width == -1) estimateSize();

	int x = mwidget->findCol(e->x());
	int y = mwidget->findRow(e->y());

	if(x < 0 || y < 0) return;

	int cellx, celly, cellx2, celly2;

	bool posok = mwidget->colXPos(x, &cellx) && mwidget->rowYPos(y, &celly)
     && mwidget->colXPos(x + width, &cellx2) && mwidget->rowYPos(y + height, &celly2);
	if(!posok) return;

	mwidget->repaint(componentRect);
	componentRect = TQRect(cellx, celly, cellx2 - cellx, celly2 - celly);

	TQPainter *p = new TQPainter;

	p->begin(mwidget);
	p->save();
	p->drawRect(componentRect);
	p->restore();
	p->end();
        delete p;
}

void CreateTool::mouseReleaseEvent(TQMouseEvent *e)
{
}

// module creation

void CreateModuleTool::estimateSize()
{
// yes, this is a it ugly: we create here the new module for a microsecond,
// just to see how big it is, to be able to paint an accurate preview
//
// We delete it again, and if the user finally really creates the module
// we recreate it.

	mwidget->unselectAll();

	Module *newModule = mwidget->theStructure()->createModule(minfo);
	width = newModule->width();
	height = newModule->height();
	newModule->setSelected(true);

	mwidget->theStructure()->deleteSelected();
}

void CreateModuleTool::performCreate(int x, int y)
{
	mwidget->unselectAll();
	Module *newModule = mwidget->theStructure()->createModule(minfo);
	newModule->setSelected(true);
	newModule->move(x, y);

	if(!mwidget->insertModule(newModule))
		mwidget->theStructure()->deleteSelected();
}

CreateModuleTool::CreateModuleTool(ModuleWidget *mwidget,
     const Arts::ModuleInfo& minfo) : CreateTool(mwidget)
{
	this->minfo = minfo;
}

CreateModuleTool::~CreateModuleTool()
{
}

// interface creation

void CreateInterfaceTool::estimateSize()
{
	vector<Arts::PortType>::iterator pi;

	int input_width = 0;
	int output_width = 0;

	for(pi = minfo.ports.begin(); pi != minfo.ports.end(); ++pi)
	{
		if(pi->direction == Arts::input)
			input_width++;

		if(pi->direction == Arts::output)
			output_width++;
	}
	width = max(input_width, output_width);
	height = 5;
}

void CreateInterfaceTool::performCreate(int x, int y)
{
	vector<std::string>::iterator pni;
	vector<Arts::PortType>::iterator pi;

	mwidget->theStructure()->addInheritedInterface(minfo.name.c_str());

	int input_width = 0;
	int output_width = 0;

	for(pi = minfo.ports.begin(), pni = minfo.portnames.begin();
		pi != minfo.ports.end(); ++pi, pni++)
	{
		StructurePort *port = 0;
		/*
		 * watch out: input ports (as in ports that accept data) are on the lower
		 * side of the structure, as the data flows out trough them ;)
		 */
		if(pi->direction == Arts::input)
		{
			port = mwidget->insertPort(*pi, x + input_width, y + 4);
			input_width++;
		}
		else if(pi->direction == Arts::output)
		{
			port = mwidget->insertPort(*pi, x + output_width, y);
			output_width++;
		}

		assert(port);
		port->rename(pni->c_str());
		port->inheritedInterface(minfo.name.c_str());
	}
}

CreateInterfaceTool::CreateInterfaceTool(ModuleWidget *mwidget,
     const Arts::ModuleInfo& minfo) : CreateTool(mwidget)
{
	this->minfo = minfo;

	vector<Arts::PortType>::iterator pi;
	for(pi = this->minfo.ports.begin(); pi != this->minfo.ports.end(); ++pi)
	{
		/* reverse since we're inside the interface, not outside */
		if(pi->direction == Arts::input)
			pi->direction = Arts::output;
		else if(pi->direction == Arts::output)
			pi->direction = Arts::input;
	}
}

CreateInterfaceTool::~CreateInterfaceTool()
{
}

// port creation

void CreatePortTool::estimateSize()
{
	width = height = 1;
}

void CreatePortTool::performCreate(int x, int y)
{
	mwidget->insertPort(type, x, y);
}

CreatePortTool::CreatePortTool(ModuleWidget *mwidget,
	const Arts::PortType& type) : CreateTool(mwidget)
{
	this->type = type;
}

MoveComponentsTool::MoveComponentsTool(ModuleWidget *widget, TQMouseEvent *e)
	: MWidgetTool(widget),
	  lastPos(e->pos())
{
}

void MoveComponentsTool::mousePressEvent(TQMouseEvent *e)
{
}

void MoveComponentsTool::mouseMoveEvent(TQMouseEvent *e)
{
	int dx = (e->x() - lastPos.x())/mwidget->cellsize;
	int dy = (e->y() - lastPos.y())/mwidget->cellsize;

	if(dx == 0 && dy == 0)
		return;

	std::list<StructureComponent *>::iterator i;
	std::list<StructureComponent *> *ComponentList = mwidget->structure->getComponentList();

	for(i = ComponentList->begin();i != ComponentList->end();++i)
	{
		StructureComponent *c = *i;
		if(c->selected() && !mwidget->hasSpace(c, c->x() + dx, c->y() + dy, true))
			return;
	}

	mwidget->beginUpdate();
	for(i = ComponentList->begin();i != ComponentList->end();++i)
	{
		StructureComponent *c = *i;
		if(c->selected())
			c->move(c->x() + dx, c->y() + dy);
	}
	mwidget->endUpdate();

	lastPos.setX(lastPos.x() + dx*mwidget->cellsize);
	lastPos.setY(lastPos.y() + dy*mwidget->cellsize);
	mwidget->reRoute();
}

void MoveComponentsTool::mouseReleaseEvent(TQMouseEvent *e)
{
	mwidget->leaveTool(this, true);
}

ConnectPortsTool::ConnectPortsTool(ModuleWidget *widget, ModulePort *connectingPort)
	: MWidgetTool(widget),
	  connectingPort(connectingPort)
{
	firstPos = mwidget->portPos(connectingPort) + connectingPort->clickrect.center();
}

void ConnectPortsTool::mousePressEvent(TQMouseEvent *e)
{
}

void ConnectPortsTool::mouseMoveEvent(TQMouseEvent *e)
{
	TQPainter painter(mwidget);
	painter.setPen(TQt::white);
	mwidget->repaint(TQRect(firstPos, lastPos).normalize());
	painter.drawLine(firstPos, e->pos());
	lastPos = e->pos();
}

void ConnectPortsTool::mouseReleaseEvent(TQMouseEvent *e)
{
	StructureComponent *component;
	ModulePort *otherPort;
	mwidget->findAt(e->x(), e->y(), component, otherPort);
	if(otherPort && (otherPort!= connectingPort))
	{
		// user is trying to close a connection
		if(connectingPort->direction == otherPort->direction)
		{
			KMessageBox::sorry(mwidget,
							   i18n("You can only connect an IN-port with an OUT-port,\n"
									"not two ports with the same direction."));
		}
		else
		{
			if(connectingPort->direction == ModulePort::in)
			{
				if(otherPort->PortDesc.connectTo(connectingPort->PortDesc))
					mwidget->reRoute();
			} else {
				if(connectingPort->PortDesc.connectTo(otherPort->PortDesc))
					mwidget->reRoute();
			}
		}
	}
	mwidget->repaint(TQRect(firstPos, lastPos).normalize());
	mwidget->leaveTool(this);
}