/* * 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 com.trolltech.qt.core.Qt; import com.trolltech.qt.gui.QGraphicsView; import com.trolltech.qt.gui.QKeyEvent; import com.trolltech.qt.gui.QMouseEvent; import com.trolltech.qt.gui.QPainter; import com.trolltech.qt.gui.QWheelEvent; import com.trolltech.qt.gui.QWidget; /** * The graph rendering Qt widget. It takes its data from a populated GraphModel * class and renders it. * * This widget itself is derived from a QScrollArea and provides automatic scrolling * if the graph becomes too large for the widget. The graph itself is rendered into a * child widget. * * @author gregor */ public class GraphView extends QGraphicsView { /** * The GraphModel instance from which all structural data on the graph is taken. */ private GraphModel graphModel; /** * A typical Qt widget constructor taking the parent widget as argument. * * @param parent the intended parent of this widget. */ public GraphView(QWidget parent) { super(parent); this.graphModel=null; setDragMode(QGraphicsView.DragMode.ScrollHandDrag); setRenderHint(QPainter.RenderHint.Antialiasing); } /** * Set the graph model which represents the graph data that is to be displayed. * * @param graphModel the graph model that should be rendered */ public void setGraphModel(GraphModel graphModel) { if(this.graphModel!=null) { this.graphModel.unregisterGraphView(this); } this.graphModel=graphModel; graphModel.registerGraphView(this); setScene(graphModel); } @Override public void wheelEvent(QWheelEvent event) { if(event.delta()>0) { for(int i=0;ievent.delta()/32;i--) { scale(1/1.1,1/1.1); } } } @Override public void keyPressEvent(QKeyEvent event) { if(event.modifiers().isSet(Qt.KeyboardModifier.ShiftModifier)) { setDragMode(QGraphicsView.DragMode.RubberBandDrag); } super.keyPressEvent(event); } @Override public void keyReleaseEvent(QKeyEvent event) { if(!event.modifiers().isSet(Qt.KeyboardModifier.ShiftModifier)) { setDragMode(QGraphicsView.DragMode.ScrollHandDrag); } super.keyReleaseEvent(event); } @Override public void disposed() { setScene(null); if(graphModel!=null) { graphModel.unregisterGraphView(this); } } }