3
Copyright (C) 1998-1999 Stefan Westerfeld
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#include "drawutils.h"
24
#include "portablekde.h"
26
#include <arts/debug.h>
28
#include <kiconloader.h>
33
ModulePort::ModulePort(StructureComponent *owner, const string& description,
34
int drawsegment, Direction direction, Arts::PortDesc PortDesc)
38
this->owner = owner; // whats the syntax for that again?
39
this->drawsegment = drawsegment;
40
this->direction = direction;
41
this->PortDesc = PortDesc;
42
this->description = description.c_str();
46
isinitarg = (PortDesc.type().connType == Arts::conn_property);
47
if(isinitarg) arts_debug("port %s is an init arg",(const char *)this->description);
48
arts_debug("port %s created",(const char *)this->description);
53
bool ModulePort::down()
55
return (PortDesc.isConnected() || PortDesc.hasValue() || selected);
58
QColor ModulePort::color(bool isInterface)
60
if(selected) return QColor(255,165,0);
62
if(PortDesc.hasValue())
64
if(isinitarg) return QColor(180,180,180);
65
return QColor(100,100,255);
68
if(isinitarg) return QColor(128,128,128);
70
if(isInterface) return QColor(100,100,100);
71
return QColor(43,43,168);
74
Module::Module(Arts::ModuleDesc module, Arts::StructureDesc structuredesc,
75
StructureCanvas *canvas) : StructureComponent(canvas)
77
StructureDesc = structuredesc;
85
Module::Module(const Arts::ModuleInfo& minfo,Arts::StructureDesc structuredesc,
86
StructureCanvas *canvas) : StructureComponent(canvas)
88
StructureDesc = structuredesc;
89
ModuleDesc = StructureDesc.createModuleDesc(minfo);
94
void Module::initModule()
97
KIconLoader iconloader;
102
_name = ModuleDesc.name().c_str();
105
// test if pixmap available
107
iconname = _name+".xpm";
109
_pixmap = new QPixmap(iconloader.loadIcon(iconname, KIcon::User));
110
if(!_pixmap->height())
115
// try again without Arts:: - prefix
117
if(iconname.startsWith("Arts::"))
119
iconname = iconname.mid(6);
121
_pixmap = new QPixmap(iconloader.loadIcon(iconname, KIcon::User));
122
if(!_pixmap->height())
129
FILE *test=fopen(QFile::encodeName(iconname),"r");
132
pixmap = new QPixmap(iconname);
137
// create lists with inports & outports for this module
138
// and bind them to it ...
140
arts_debug("Getting ports...");
141
vector<Arts::PortDesc> *ports = ModuleDesc.ports();
142
unsigned long portpos;
143
long indraw = 1, outdraw = 2;
145
for(portpos=0; portpos<ports->size(); portpos++)
147
Arts::PortDesc pd = (*ports)[portpos];
150
arts_debug("CREATING %s",pd.name().c_str());
151
switch(pd.type().direction)
154
p = new ModulePort(this,pd.name(),indraw++,
156
inports.push_back(p);
159
p = new ModulePort(this,pd.name(),outdraw++,
161
outports.push_back(p);
164
assert(false); // shouldn't happen!
170
_width = 1+max(inports.size(),outports.size()+1);
172
mdID = ModuleDesc.ID();
173
isInterface = ModuleDesc.isInterface();
178
arts_debug("hide...");
180
arts_debug("sdfmd...");
181
list<ModulePort *>::iterator i;
183
for(i=inports.begin(); i != inports.end(); i++) delete *i;
186
for(i=outports.begin(); i != outports.end(); i++) delete *i;
189
StructureDesc.freeModuleDesc(ModuleDesc);
193
StructureComponent::ComponentType Module::type()
198
bool Module::move(int newx, int newy)
200
int oldx = _x, oldy = _y;
205
canvas->redrawRect(oldx,oldy,_width,_height);
206
canvas->redrawRect(_x,_y,_width,_height);
208
return ModuleDesc.moveTo(_x,_y);
231
bool Module::selected()
236
void Module::setSelected(bool newselection)
238
if(newselection != _selected)
240
_selected = newselection;
241
canvas->redrawRect(_x,_y,_width,_height);
245
bool Module::drawNeedsBackground(int segment)
250
void Module::drawSegment(QPainter *p, int cellsize, int segment)
252
int border = cellsize / 10; // for the logo
253
int ltop = (cellsize-border)/2;
254
int lbot = (cellsize+border)/2;
256
QColor mcolor(43,43,168);
257
QColor mcolorlight(164,176,242);
261
mcolor = QColor(100,100,100);
262
mcolorlight = QColor(160,160,160);
264
QColorGroup g( Qt::white, Qt::blue, mcolorlight, mcolor.dark(), mcolor,
265
Qt::black, Qt::black );
266
QBrush fill( mcolor );
267
QPen textpen(QColor(255,255,180),1);
271
qDrawShadePanel(p,border,border,cellsize-2*border+1,cellsize-2*border+1,
273
p->fillRect(cellsize-border-1,ltop,cellsize,lbot-ltop+1,fill);
274
p->setPen(g.light());
275
p->drawLine(cellsize-border,ltop-1,cellsize,ltop-1);
277
p->drawLine(cellsize-border,lbot+1,cellsize,lbot+1);
280
int destsize = (cellsize-4*border);
281
float sx = (float)destsize/(float)_pixmap->width();
282
float sy = (float)destsize/(float)_pixmap->height();
286
QPixmap pmscaled = _pixmap->xForm(matrix);
287
p->drawPixmap(border*2,border*2,pmscaled);
292
p->fillRect(0,0,cellsize,cellsize,fill);
295
* take care of the bevel lines around the module
298
p->setPen(g.light());
299
p->drawLine(0,0,cellsize-1,0);
301
p->drawLine(0,0,0,cellsize-1);
304
p->drawLine(cellsize-1,cellsize-1,0,cellsize-1);
305
if(segment == 0 || segment == width()-1)
306
p->drawLine(cellsize-1,cellsize-1,cellsize-1,0);
313
for(direction = 0;direction < 2; direction++)
315
ModulePort *port = findPort(segment, direction);
319
int border = cellsize/7;
321
char *label = DrawUtils::cropText(p, port->description.ascii(),
322
cellsize/2, textwidth);
324
QBrush pbrush(port->color(isInterface));
326
port->clickrect = QRect(border,direction * cellsize/2 + border,
327
cellsize/2-2*border, cellsize/2-2*border);
328
qDrawShadePanel(p, port->clickrect, g, port->down(), 2, &pbrush);
331
QBrush fillport(fill);
334
fillport = QColor(128,128,128);
339
QBrush fillorange(QColor(255,165,0));
340
qDrawShadePanel(p, port->clickrect, g,true,2,&fillorange);
344
if(port->PortDesc->isConnected())
346
qDrawShadePanel(p, port->clickrect, g,true,2,&fillport);
348
else if(port->PortDesc->hasValue())
350
QBrush fillp(QColor(100,100,255));
353
fillp = QColor(180,180,180);
355
qDrawShadePanel(p, port->clickrect, g,true,2,&fillp);
357
else // not connected and no value();
358
qDrawShadePanel(p, port->clickrect, g,false,2,&fillport);
363
p->drawText((cellsize-border)/2,
364
(1+direction) * (cellsize/2)-border,label);
369
* if it was the rightmost part of the module, it has the module name
370
* and the connection to the logo as well
377
char *label=DrawUtils::cropText(p, _name.ascii(), cellsize-4, textwidth);
380
p->fillRect(1,cellsize-16,textwidth+7,15,QBrush(g.dark()));
381
p->drawText(4,cellsize-5,label);
385
p->drawLine(0,ltop,0,lbot);
389
* when selected, draw a line of white dots around the module
394
QPen pen(Qt::white,1,Qt::DotLine);
397
p->drawLine(0,0,cellsize-1,0);
398
p->drawLine(0,cellsize-1,cellsize-1,cellsize-1);
400
p->drawLine(0,0,0,cellsize-1);
401
if (segment == _width-1)
402
p->drawLine(cellsize-1,0,cellsize-1,cellsize-1);
411
canvas->redrawRect(_x,_y,_width,_height);
420
canvas->redrawRect(_x,_y,_width,_height);
424
bool Module::visible()
429
ModulePort *Module::findPort(int xoffset, int direction)
431
list<ModulePort *> *ports;
432
list<ModulePort *>::iterator i;
436
if(direction == 0) ports = &inports; else ports = &outports;
439
n = xoffset-1-direction;
441
if(n<(long)ports->size() && n>=0)
443
while(n>0) { n--; i++; }
449
ModulePort *Module::portAt(int segment, int x, int y)
451
for(int direction = 0;direction < 2; direction++)
453
ModulePort *port = findPort(segment,direction);
456
QPoint clickpoint(x,y);
457
if(port->clickrect.contains(clickpoint)) return port;
463
void Module::dumpPorts(list<ModulePort *>& ports)
465
list<ModulePort *>::iterator i;
466
for(i=inports.begin();i != inports.end();i++) ports.push_back(*i);
467
for(i=outports.begin();i != outports.end();i++) ports.push_back(*i);
470
QPixmap *Module::pixmap()
475
const char *Module::name()