/* * Moonlight|3D Copyright (C) 2006 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on May 15, 2006 */ package eu.moonlight3d.graph; import com.trolltech.qt.core.QPointF; import com.trolltech.qt.gui.QGraphicsRectItem; /** * A graph model class representing a slot in a graph node. A slot is a named * end point for a link to another slot on another node. * * @author gregor */ public class Slot { /** * An enumeration of valid slot types. Slot types may be important to * the way the graph is laid out, deoending on the interpretation by * the layouting algorithm that is used. * * @author gregor */ public enum Type { /** * Input slot type. This slot type indicates that this slot receives * from another slot through a connecting link. */ Input, /** * Output slot type. This slot type indicates that this slot receives * from another slot through a connecting link. */ Output } /** * Referens to the node that this slot belongs to. */ private Node node; /** * The name of this node. This should never be changed. */ private String name; /** * The slot type */ private Type type; /** * The link which is currently connected to this slot. */ private Link link; /** * The Qt GraphicsView item for this slot. */ private QGraphicsRectItem item; /** * Default slot constructor. Its arguments are the node it belongs * to, its name and the slot type. The slot name and slot type are * immutable. The slot name must be unique in the scope of the node * it belongs to * * @param node the node this slot belongs to * @param name the unique name of this slot. * @param type the type of this slot. */ public Slot(Node node, String name, Type type) { this.node=node; this.name=name; this.type=type; } /** * Get the node to which this slot belongs * * @return the node that contains thsi slot */ public Node getNode() { return node; } /** * Get the name of this slot. * * @return the slot name */ public String getName() { return name; } /** * Get the type of the slot (input or output). * * @return slot type */ public Type getType() { return type; } /** * Connect a link to this slot as either input or output. * * @param link the link to connect */ public void setLink(Link link) { this.link=link; updateLinkEndpoint(); } /** * Get the link that is connected to this slot. * * @return the connected link */ public Link getLink() { return link; } /** * Internal method to pass a reference to the Qt Graphics View item * that represents this slot. * * @param item the Graphics View item that represents this slot */ void setItem(QGraphicsRectItem item) { this.item=item; } /** * Internal method to get the Qt Graphics View item that represents * this slot. * * @return the Graphics View item that represents the slot */ QGraphicsRectItem getItem() { return item; } void updateLinkEndpoint() { if(item==null) { return; } if(link!=null) { if(type==Type.Input) { QPointF endPoint=item.mapToScene(item.rect().left(), item.rect().y()+item.rect().height()/2); link.setEndPoint(endPoint.x(),endPoint.y()); } else { QPointF startPoint=item.mapToScene(item.rect().right(), item.rect().y()+item.rect().height()/2); link.setStartPoint(startPoint.x(),startPoint.y()); } } } }