/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

Copyright (C) 2009 Arvin Schnell
                   aschnell@suse.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.
*/


#ifndef GRAPHWIDGET_20090903_H_
#define GRAPHWIDGET_20090903_H_

#include "GraphWidgetBase.h"

#include <QString>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPathItem>
#include <QPicture>
#include <QContextMenuEvent>
#include <QMouseEvent>

#include <graphviz/gvc.h>

class GraphWidget : public GraphWidgetBase {
    Q_OBJECT

private:
	friend class QY2Node;
	friend class QY2Edge;

public:
    GraphWidget(const QString& filename, const QString& layout, QWidget* parent = 0);
    GraphWidget(GVC_t *gvc, graph_t *graph, const QString& layout, QWidget* parent = 0);

    virtual ~GraphWidget();

    virtual void renderGraph(const QString& filename, const QString& layout);
    virtual void renderGraph(GVC_t *gvc, graph_t *graph, const QString& layout);
	void clearGraph();

Q_SIGNALS:
    void backgroundContextMenuEvent(QContextMenuEvent* event);
    void nodeContextMenuEvent(QContextMenuEvent* event, const QString& name);
    void nodeDoubleClickEvent(QMouseEvent* event, const QString& name);

protected:
    void keyPressEvent(QKeyEvent* event);
    void wheelEvent(QWheelEvent* event);
    void contextMenuEvent(QContextMenuEvent* event);
    void mouseDoubleClickEvent(QMouseEvent* event);

private:
	void renderNode(graph_t *graph, node_t *node);
	void renderEdge(edge_t *edge);
	void renderSubGraph(GVC_t *gvc, graph_t *graph);
	
private:
	void drawGraphLabel(graph_t *graph);

private:
    void scaleView(qreal scaleFactor);  
    QRectF graphRect;

    QPointF gToQ(const pointf& p, bool upside_down = true) const;
	QPointF gToQ(const point& p, bool upside_down = true) const;

    QColor aggetToQColor(void *obj, const char *name, const QColor& fallback) const;
    Qt::PenStyle aggetToQPenStyle(void *obj, const char *name, const Qt::PenStyle fallback) const;
};

#endif
