/* * Moonlight|3D Copyright (C) 2005 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 Nov 13, 2005 */ package eu.moonlight3d.graph; import java.util.ArrayList; import com.trolltech.qt.core.QPointF; import com.trolltech.qt.core.QRectF; import com.trolltech.qt.gui.QColor; /** * A class representing a node in a graph model. Nodes can a variable * number of slots that serve as end points for links to other nodes. * * @author gregor */ public class Node { /** * The immutable name of this node. */ private String name; /** * Indicator if this node is active or not. */ private boolean active; /** * The list of slots for this node. */ private ArrayList slots; /** * The Qt GraphicsView item for this node. */ private NodeItem item; /** * Cached item bounding rectangle. */ private QRectF itemRect; /** * Cached item position. */ private QPointF itemPos; /** * Default constructor. The name of the node must be unique in the scope * of the graph model it belongs to and is immutable. * * @param name unique node name */ public Node(String name) { this.name=name; this.active=false; this.slots=new ArrayList(); } /** * Return the name of this node. * * @return node name */ public String getName() { return name; } /** * Add a new input or output slot to this node. * * @param slot the slot to be added */ public void addSlot(Slot slot) { slots.add(slot); } /** * Return the list of slots this node contains. * * @return a list of slots */ public ArrayList getSlots() { return slots; } /** * Set the node as active or inactive. An active node is rendered * highlighted. There is only one active node at any time. * * @param active */ public void setActive(boolean active) { this.active=active; } /** * Return if the node is currently active. * * @return true if active, false otherwise */ public boolean isActive() { return active; } /** * Internal method to pass a reference to the Qt Graphics View item * that represents this node. * * @param item the Graphics View item that represents this node */ void setItem(NodeItem item) { this.item=item; } /** * Internal method to get the Qt Graphics View item that represents * this node. * * @return the Graphics View item that represents the node */ NodeItem getItem() { return item; } /** * Search for the slot with the given name. * * @param name the name of the slot. * @return the slot with the given name if found, null otherwise */ public Slot getSlot(String name) { for(Slot slot : slots) { if(slot.getName().equals(name)) { return slot; } } return null; } /** * Get the position of the item in the graph. * * @return the position of the item in the graph */ public QPointF getPosition() { if(itemPos==null) { itemPos=item.pos(); } return itemPos; } public void setPosition(QPointF position ) { item.setPos(position); itemPos=position; itemRect=null; } public void setPosition(double x, double y) { itemPos=new QPointF(x,y); item.setPos(itemPos); itemRect=null; } /** * Get the position and size of the item in the graph. * * @return the position and size of the item in the graph */ public QRectF getRect() { if(itemRect==null) { itemRect=item.mapToScene(item.rect()).boundingRect(); } return itemRect; } public void setToolTip(String toolTip) { item.setToolTip(toolTip); } public void setNormalColor(QColor normalColor) { item.setNormalColor(normalColor); } public void setSelectedColor(QColor selectedColor) { item.setSelectedColor(selectedColor); } }