cadview is a C++ library which provides a cross-platform GUI widget for 2D drafting. Presently, the following features are supported:
- Zoom/Pan
- Polyline tool
- Entering x,y co-ordinates via mouse selection or text entry
Installation
No installation packages are available yet and the executable has to be built from source. cadview depends on the following tools and libraries:
On a Fedora 16/17 machine, you can install the above dependencies as follows:
1 |
$ sudo yum install mercurial gcc-c++ gtk2-devel boost-devel cmake |
Obtain the source using Mercurial as follows:
1 |
$ hg clone https://code.google.com/p/cadview |
This should place the source in a directory named cadview
in your current directory. To build the binaries:
1 2 3 4 5 |
$ cd cadview $ mkdir build $ cd build $ cmake ../src $ make |
gladeui is a test application that uses cadview on GTK+. You can run gladeui as follows:
1 2 |
$ cd gladeui $ ./gladeui |
Select File>Polyline to activate the polyline tool. The UI controls are as follows:
- Click to add a point to the polyline
- A point can also be added by entering an x,y co-ordinate pair in the textbox and hitting <Enter>
- Pan by holding down the middle mouse button
- Zoom by turning the mouse wheel
- Right-click or hit <Esc> to end the polyline
Design Overview
The visible area of the widget – the view – is represented by the cad_gui_view
class. cad_gui_view
does not interact directly with the underlying GUI but instead depends on a pair of adaptor classes for GUI facilities.
The first adaptor class is the template parameter U
of cad_gui_view
and provides facilities like getting the position of the mouse cursor (get_mouse_position
) and invalidating the view (invalidate
). The second adaptor class is the U::graphics_type
type. This type provides facilities to draw on screen (draw_polyline
, move_to
, line_to
) and converting between screen and world co-ordinates (device_to_user
).
The cad_gtk_adaptor
is an implementation of the first adaptor class (the U
parameter) using GTK+. cad_cairo_graphics
implements the U::graphics_type
using cairo. In a similar way, we can create adaptors for other GUI platforms (e.g. cad_windows_adaptor
to wrap around Windows API and cad_windows_graphics
to talk to an HDC).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
template U& gui; public: typedef typename U::graphics_type graphics_type; // ... void paint(); void mouse_move(int x, int y); void mouse_down(Mouse_button button, int x, int y); void mouse_up(Mouse_button button, int x, int y); void device_to_user(point_2d }; template GtkWidget *widget; public: typedef cad_cairo_graphics // ... void create_graphics(graphics_type *graphics); void get_mouse_position(int *x, int *y); GtkWidget* get_widget(); void invalidate(); }; template cairo_t *cr; public: // ... void move_to(const T x, const T y); void line_to(const T x, const T y); void draw_polyline(const polyline_2d }; |