#include <CGAL/Cartesian.h>
#include <CGAL/Point_2.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/IO/Qt_widget_Delaunay_triangulation_2.h>
#include <CGAL/IO/Qt_widget.h>
#include <CGAL/IO/Qt_widget_layer.h>
#include <qapplication.h>

typedef CGAL::Cartesian<double>		    Rep;
typedef CGAL::Point_2<Rep>		    Point;
typedef CGAL::Delaunay_triangulation_2<Rep> Delaunay;


/////////////////////////////////////////////////////////
//  The displayed objects are declared here as global variables.
//  The display code is the "draw" member function below.

Delaunay dt;
Point    the_point = Point(-1, -1);

class My_layer : public CGAL::Qt_widget_layer {
  void draw() {
    *widget << CGAL::RED << dt << CGAL::PointSize(6) << CGAL::BLUE << the_point;
  }
};

class My_window : public CGAL::Qt_widget {
public:
  My_window(int x, int y)
  {
    resize(x, y);
    attach(&layer);
  }
private:
  // this method is called when the user presses the mouse
  void mousePressEvent(QMouseEvent *e)
  {
    Qt_widget::mousePressEvent(e);

    /////////////////////////////////////////////////////////////
    //  HERE IS WHAT IS EXECUTED ON A MOUSE CLICK !
    //
    if (e->button() == Qt::LeftButton)
      dt.insert(Point(x_real(e->x()), y_real(e->y())));
    if (e->button() == Qt::MidButton)
      std::cout << Point(x_real(e->x()), y_real(e->y())) << std::endl;
    if (e->button() == Qt::RightButton)
      the_point = Point(x_real(e->x()), y_real(e->y()));
    /////////////////////////////////////////////////////////////

    redraw();
  }
  My_layer layer;
};

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    My_window *W = new My_window(400, 400);
    app.setMainWidget(W);
    W->show();
    W->set_window(0, 400, 0, 400);
    return app.exec();
}
