CGAL::Qt_widget

Definition

An object of type Qt_widget is a two-dimensional window for graphical IO. It is a class that is designed to help CGAL users to visualize easily CGAL objects and for advanced users to interact with them. This widget is designed for 2D CGAL objects only.

#include <CGAL/IO/Qt_widget.h>

Inherits From

QWidget

Types

The widget class also defines the following enum type to specify which kind of point representation do you want to use:

enum PointStyle { PIXEL, CROSS, PLUS, CIRCLE, DISC, RECT, BOX};

Creation

Qt_widget win ( QWidget *parent = 0, const char *name =);
Constructs a widget which is a child of parent, with the name name. The default visible area is between ranges xmin = -1, xmax = 1, ymin = -1, ymax = 1. By default the X and Y scale factors are equal.

Qt_widget provides scaling support. You can use a scaling factor for your objects as well as you can draw the objects with one scale and look at the objects with different scales. Also there is a possibility to tell the widget that you want that the visible area should be mapped to a certain interval, and the widget adjust the scaling factor according to that.

void win.set_window ( double x_min, double x_max, double y_min, double y_max, bool u = false)
Map the widget coordinates to the interval defined by the rectangle with given coordinates. This method should be called after resize() from QWidget. If u is true the X and Y scale factors will not be equal and the widget will not keep the aspect ratio.

void win.set_x_scale ( double xscale) Set the current X scaling factor. Actually the scales are recomputed when you resize, so this method is not used very often.

void win.set_y_scale ( double yscale) Set the current Y scaling factor. Actually the scales are recomputed when you resize, so this method is not used very often.


begin of advanced section  advanced  begin of advanced section
You should have the same scaling factor for X and Y if you want to keep the aspect ratio of your objects.
end of advanced section  advanced  end of advanced section

void win.zoom ( double ratio) Multiply the X and Y scaling factors by ratio, then calls redraw. The center of the visible area remains the same.

void win.zoom ( double ratio, double xc, double yc)
Multiply the X and Y scaling factors by ratio, then calls redraw. The center of the visible area becomes (xc, yc).

void win.move_center ( const double distx, const double disty)
Move the center of the widget with distx on the X axis and disty on the Y axis.

void win.set_center ( const double x, const double y)
Set the center of the widget to (x, y).

void win.add_to_history () Deprecated: This function adds the current viewport and transformations to history. You should use save() public slot from Qt_widget_history object.

void win.clear_history () Deprecated: Clears the history. This means that there are no elements in the history list. Call this method when you want to reinitialize the history list. You should use clear_history() method from Qt_widget_standard_toolbar or the clear() public slot from Qt_widget_history object.

public slots:

bool win.back ()
bool win.forth () Deprecated: This slots permit to walk into history. Return true if succeded. These slots are deprecated. You should use the backward() and forward() methods of Qt_widget_history object, or the back() and forward() public slots from Qt_widget_standard_toolbar object.

void win.clear () Clear the screen. The properties of the widget remain the same after calling this member function. You can see a list of the properties that you can set at the properties description.

void win.lock () Locks the widget, keeping the widget from being refreshed. If you lock the widget you should verify that you unlock it somewhere. The number of lock() calls should be the same with the number of unlock() calls. Lock and unlock calls can be nested.

void win.unlock () Unlocks the widget, and calls do_paint(). The widget is only unlocked if the number of unlock calls is equal to the number of lock calls.

void win.do_paint () Refresh the widget calling paintEvent(QPaintEvent *e) for the Qt_widget only if the widget is unlocked. This mean that redraw() is called if and only if the widget is unlocked.

void win.redraw () If you derive from Qt_widget you have to overload this function and put your code for drawing here if you don't use layers. The best way is to attach layers to the widget and call this method. This method redraws the layers attached and active. Before drawing the layers, redraw clear the screen and emit redraw_on_back(). Emit redraw_on_front() at the end.

void win.print_to_ps () Redraws all the attached and active layers into a Postscript device. It could be a file or a printer. This method also use signals as redraw_on_back() or redraw_on_front().

Properties

You can set the properties of the functions through this functions as well as with the help of manipulators described later. The function naming convention has changed for this member functions, to Qt convention.

void win.setColor ( Qcolor c) Set the current pen color of the widget to be c.

void win.setBackgroundColor ( QColor c)
Set the current background color to be c.

void win.setFillColor ( QColor c) Set the current fill color of the widget to be c.

void win.setFilled ( bool f) Set the status of the widget to true or false concerning filling the objects: polygons, circles, rectangles ...

void win.setLineWidth ( unsigned int i)
Set the current line width of the widget.

void win.setPointSize ( unsigned int i)
Set the current point size of the widget.

void win.setPointStyle ( PointStyle s) Set the current point style of the widget to s. PointStyle is a enumeration declared in Qt_widget.

void win.setRasterOp ( RasterOp r) Set the current raster operation.

Layers

void win.attach ( Qt_widget_layer* s) Add the layer s in the list of layers.The last added will be on top of the screen. Also the events are forwarded to layers in the order they have been attached.

void win.detach ( Qt_widget_layer* s) Remove the layer s from the list. s it's a pointer to an existing layer.

New CGAL Objects

void win.new_object ( CGAL::Object obj)
This function should be called by the tools that create CGAL objects. It then emits the signal new_cgal_object(CGAL::Object). Slots of other components can be connected to this signal.

Access Functions

Note that we use also types from Qt here.

QColor win.color () Returns the current pen color. The color returned is a Qt class.

QColor win.backgroundColor () Returns the current widget background color. The color returned is a Qt class.

QColor win.fillColor () Returns the current color used for filling the objects. The color returned is a Qt class.

PointStyle win.pointStyle () Returns the current point style. PointStyle is a enumeration declared in Qt_widget.

uint win.pointSize () Returns the current point size.

uint win.lineWidth () Returns the current line width.

RasterOp win.rasterOp () Return the current raster operation.

double win.x_min () Returns the left x coordinate of the widget.

double win.y_min () Returns the lower y coordinate of the widget.

double win.x_max () Returns the right x coordinate of the widget.

double win.y_max () Returns the upper y coordinate of the widget.

begin of advanced section  advanced  begin of advanced section
The coordinates of the screen are maped to a certain interval that you can choose with set_window member function. The scale of the objects you can visualize is computed and maintained the same for both axes to keep the aspect ratio of the objects. You should use x_real or y_real to get the real world coordinates, for your screen coordinates. If you are using gmp you can use this functions with Gmpq as second parameter. You may need it when you work with rationals. The double from the other function could be more complex and can make you loose speed in computations. Computing with rational coordinates directly could increase the speed, rather than computing with complex double coordinates.

double win.x_real ( int x)
double win.y_real ( int y)

template<class FT>
void x_real ( int x, FT&) Returns the x real world coordinate of the Qt_widget.

template<class FT>
void y_real ( int y, FT&) Returns the y real world coordinate of the Qt_widget.

void win.x_real ( int, Gmpq&)
void win.y_real ( int, Gmpq&)
end of advanced section  advanced  end of advanced section

int win.x_pixel ( double x) This method is the opposite of x_real method. Converts the world coordinates in screen coordinates. If you want to get an integer between 0 and width, you should pass an x between xmin and xmax.

int win.y_pixel ( double y) This method is the opposite of y_real method. Converts the world coordinates in screen coordinates. If you want to get an integer between 0 and height, you should pass an y between ymin and ymax.


begin of advanced section  advanced  begin of advanced section

Painter and Pixmap

In order that layers can draw on the drawing area of a widget, they have to access the underlying pixmap and painter.

Qt_widget use as a backbuffer for drawing a pixmap defined inside the class, i.e. an object of type QPixmap. The QPixmap class is an off-screen pixel-based paint device. One common use of the QPixmap class is to enable smooth updating of widgets. The QPainter class paints on paint devices. There is an object of type QPainter defined in Qt_widget that uses as a paint device the QPixmap object.

QPixmap& win.get_pixmap () Returns the current pixmap.

QPainter& win.get_painter () Returns the current painter.

end of advanced section  advanced  end of advanced section

Signals

void win.s_mousePressEvent ( QMouseEvent *e)
void win.s_mouseReleaseEvent ( QMouseEvent *e)
void win.s_mouseMoveEvent ( QMouseEvent *e)
void win.s_paintEvent ( QPaintEvent *e)
void win.s_resizeEvent ( QResizeEvent *e)
void win.s_wheelEvent ( QMouseEvent *e)
void win.s_mouseDoubleClickEvent ( QMouseEvent *e)
void win.s_keyPressEvent ( QKeyEvent *e)
void win.s_keyReleaseEvent ( QKeyEvent *e)
void win.s_enterEvent ( QEvent *e)
void win.s_leaveEvent ( QEvent *e)
void win.s_event ( QEvent *e) The Qt_widget receives the events through virtual functions. This is the mechanism that Qt offers for dispaching events. This signals are called every time an event is dispatched to a virtual function. For example if Qt_widget receives mousePressEvent(QMouseEvent *e) emits s_mousePressEvent(e). This is very useful when you have only a pointer to Qt_widget. It is enough to connect this slot to your function to receive the event.

void win.new_cgal_object ( CGAL::Object)
Triggered when a new object from a tool is received. The user should catch this signal if it's working with tools that provide CGAL objects as input.

void win.custom_redraw () Deprecated: Emitted in the redraw() function after the layers are drawn.

void win.redraw_on_back () Emmitted in redraw() method before calling layer's redraw.

void win.redraw_on_front () Emmitted in redraw() method after calling layer's redraw.

void win.rangesChanged () Emmitted each time (xmin, xmax) and (ymin, ymax) ranges are changed.

Operators for Output

The output operator is defined for all geometric classes in the CGAL kernel.

template<class R>
Qt_widget& & win << Point_2<R> p

template<class R>
Qt_widget& & win << Segment_2<R> s

template<class R>
Qt_widget& & win << Line_2<R> l

template<class R>
Qt_widget& & win << Ray_2<R> r

template<class R>
Qt_widget& & win << Triangle_2<R> t

template<class R>
Qt_widget& & win << Iso_rectangle_2<R> r

template<class R>
Qt_widget& & win << Circle_2<R> c

To use the other operators described you have to include the right header.

#include <CGAL/IO/Qt_widget_Polygon_2.h>

template<class Tr, class Co>
Qt_widget& & win << Polygon_2<Tr,Co> pol

#include <CGAL/IO/Qt_widget_Min_ellipse_2.h>

template<class Traits_>
Qt_widget& &win << Min_ellipse_2<Traits_> min_ellipse

#include <CGAL/IO/Qt_widget_Optimisation_ellipse_2.h>

template<class Traits_>
Qt_widget& &win << Optimisation_ellipse_2<Traits_> oe

#include <CGAL/IO/Qt_widget_Optimisation_circle_2.h>

template<class Traits>
Qt_widget& &win << Optimisation_circle_2<Traits_> oc

#include <CGAL/IO/Qt_widget_Conic_2.h>

template<class R>
Qt_widget& & win << Conic_2<R> c

#include <CGAL/IO/Qt_widget_Triangulation_2.h>

template <class Gt, class Tds>
Qt_widget& & win << Triangulation_2<Gt, Tds> t

#include <CGAL/IO/Qt_widget_Alpha_shape_2.h>

template <class Dt>
Qt_widget& & win << Alpha_shape_2<Dt> As

#include <CGAL/IO/Qt_widget_Delaunay_triangulation_2.h>

template<class Gt, class Tds>
Qt_widget& & win << Delaunay_triangulation_2<R> dt

#include <CGAL/IO/Qt_widget_Constrained_triangulation_2.h>

template<class Gt, class Tds>
Qt_widget& & win << Constrained_triangulation_2<Gt, Tds> t

#include <CGAL/IO/Qt_widget_Regular_triangulation_2.h>

template<class Gt, class Tds>
Qt_widget& & win << Regular_triangulation_2<Gt, Tds> t

Manipulators for Qt_widget

A manipulator is an object which can be inserted in the Qt_widget, via the operator <<, to change the context for further drawing.

Here, we simply document the use of these operators which is all the user needs to know to modify the state of a stream.

Qt_widget& & win << BackgroundColor Sets the color used for background color.
Qt_widget& & win << FillColor Sets the color used for filling the objects.
Qt_widget& & win << LineWidth Sets the width of the line for drawing objects.
Qt_widget& & win << PointSize Sets the size of the points.
Qt_widget& & win << noFill Sets the state of Qt_widget concerning filling the objects to be false.
Qt_widget& & win << Color c Sets the color used as the Qt_widget fillColor.
Qt_widget& & win << PointStyle ps Sets the point style for Qt_widget.

Example

In the given example, we create an object of type Qt_widget and then we use the operators for output and the manipulators to show some of the widget's functionality.

#include <CGAL/Cartesian.h>
#include <CGAL/Bbox_2.h>
#include <list>
#include <CGAL/Polygon_2.h>
#include <CGAL/IO/Qt_widget_Polygon_2.h>
#include <qapplication.h>
#include <CGAL/IO/Qt_widget.h>

typedef CGAL::Cartesian<int>   Rep;
typedef CGAL::Point_2<Rep>     Point;
typedef CGAL::Circle_2<Rep>    Circle;
typedef CGAL::Segment_2<Rep>   Segment;
typedef CGAL::Line_2<Rep>      Line;
typedef CGAL::Ray_2<Rep>       Ray;
typedef CGAL::Triangle_2<Rep>  Triangle;
typedef CGAL::Iso_rectangle_2<Rep>
                               Cgal_Rectangle;
typedef CGAL::Bbox_2           BBox;
typedef std::list<Point>       Container;
typedef CGAL::Polygon_2<Rep,Container>
                               Cgal_Polygon;

int main( int argc, char **argv )
{
  QApplication app( argc, argv );
  using namespace CGAL;
  CGAL::Qt_widget * W = new CGAL::Qt_widget();
  app.setMainWidget( W );
  W->resize(600, 600);
  W->set_window(0, 600, 0, 600);
  W->show();
  //painting something on the screen
  W->lock();

  *W << BackgroundColor(ORANGE) << RED <<
  LineWidth(3) << PointSize(3) << PointStyle(DISC);
  *W << Segment(Point(10,20),Point(300,400));
  *W << LineWidth(5) << GREEN << FillColor(BLACK) <<
    Circle(Point(400,400),50*50);
  *W << LineWidth(1) << noFill << Circle(Point(300,300),300*300);
  *W << BLUE << LineWidth(2);
  *W << Segment(Point(200,200),Point(400,400));
  *W << Segment(Point(200,400),Point(400,200));
  W->setFilled(TRUE);
  *W << RED << Triangle(Point(150,300),
                                  Point(150,350),
                                  Point(100,325));
  *W << FillColor(RED) << Cgal_Rectangle(Point(320,220),
                                              Point(350,240));
  *W << DEEPBLUE << BBox(100,80,260,140);
  Cgal_Polygon p;
  p.push_back(Point(300,30));
  p.push_back(Point(400,30));
  p.push_back(Point(500,130));
  p.push_back(Point(400,180));
  p.push_back(Point(300,130));
  *W << p;
  *W << Ray(Point(200,400), Point(180,430))
    << Ray(Point(200,400), Point(180,370));
  W->unlock();

  return app.exec();
}