geom_drawing_area.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <geometry/gtk/geom_drawing_area.h>
00026 #include <geometry/hom_point.h>
00027 #include <geometry/gtk/hom_point_drawer.h>
00028 #include <geometry/hom_vector.h>
00029 #include <geometry/gtk/hom_vector_drawer.h>
00030 #include <geometry/line_segment.h>
00031 #include <geometry/gtk/line_segment_drawer.h>
00032 #include <geometry/bezier.h>
00033 #include <geometry/gtk/spline_drawer.h>
00034 #include <geometry/spline.h>
00035 #include <geometry/gtk/bezier_drawer.h>
00036 #include <geometry/gtk/drawing_manipulator.h>
00037 #include <cmath>
00038
00039 using namespace std;
00040
00041 namespace fawkes{
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 GeomDrawingArea::GeomDrawingArea( float max_x,
00076 float max_y,
00077 float min_x,
00078 float min_y )
00079 : m_max_x(max_x),
00080 m_max_y(max_y),
00081 m_min_x(min_x),
00082 m_min_y(min_y)
00083 {
00084 m_cur_drawing_manipulator = NULL;
00085 }
00086
00087 #ifdef HAVE_GLADEMM
00088
00089
00090
00091
00092 GeomDrawingArea::GeomDrawingArea( BaseObjectType* cobject,
00093 const Glib::RefPtr<Gnome::Glade::Xml>& ref_xml )
00094 : Gtk::DrawingArea(cobject)
00095 {
00096 m_max_x = 5.0;
00097 m_max_y = 5.0;
00098 m_min_x = -5.0;
00099 m_min_y = -5.0;
00100
00101 m_cur_drawing_manipulator = NULL;
00102 }
00103 #endif
00104
00105
00106 GeomDrawingArea::~GeomDrawingArea()
00107 {
00108 clear();
00109 }
00110
00111
00112 void
00113 GeomDrawingArea::clear()
00114 {
00115 for ( vector<GeomDrawer*>::iterator iter = m_drawers.begin();
00116 iter != m_drawers.end();
00117 ++iter )
00118 {
00119 delete *iter;
00120 }
00121
00122 m_drawers.clear();
00123
00124 m_cur_drawing_manipulator = NULL;
00125 }
00126
00127
00128
00129
00130
00131 GeomDrawingArea&
00132 GeomDrawingArea::operator<<(fawkes::HomPoint& p)
00133 {
00134 HomPointDrawer* d = new HomPointDrawer(p);
00135
00136 if (m_cur_drawing_manipulator)
00137 { d->set_point_size( m_cur_drawing_manipulator->get_point_size() ); }
00138
00139 m_drawers.push_back(d);
00140
00141 return *this;
00142 }
00143
00144
00145
00146
00147
00148 GeomDrawingArea&
00149 GeomDrawingArea::operator<<(const fawkes::HomPoint& p)
00150 {
00151 HomPointDrawer* d = new HomPointDrawer(p);
00152
00153 if (m_cur_drawing_manipulator)
00154 { d->set_point_size( m_cur_drawing_manipulator->get_point_size() ); }
00155
00156 m_drawers.push_back(d);
00157
00158 return *this;
00159 }
00160
00161
00162
00163
00164
00165 GeomDrawingArea&
00166 GeomDrawingArea::operator<<(std::pair<HomVector, HomPoint> vp)
00167 {
00168 const HomVector& v = vp.first;
00169 const HomPoint& offset = vp.second;
00170 HomVectorDrawer* d = new HomVectorDrawer(v, offset);
00171 m_drawers.push_back(d);
00172
00173 return *this;
00174 }
00175
00176
00177
00178
00179
00180 GeomDrawingArea&
00181 GeomDrawingArea::operator<<(fawkes::LineSegment& l)
00182 {
00183 LineSegmentDrawer* d = new LineSegmentDrawer(l);
00184 m_drawers.push_back(d);
00185
00186 return *this;
00187 }
00188
00189
00190
00191
00192
00193 GeomDrawingArea&
00194 GeomDrawingArea::operator<<(fawkes::Bezier& b)
00195 {
00196 BezierDrawer* d = new BezierDrawer(b);
00197 m_drawers.push_back(d);
00198
00199 return *this;
00200 }
00201
00202
00203
00204
00205
00206 GeomDrawingArea&
00207 GeomDrawingArea::operator<<(fawkes::Spline& s)
00208 {
00209 SplineDrawer* d = new SplineDrawer(s);
00210 m_drawers.push_back(d);
00211
00212 return *this;
00213 }
00214
00215
00216
00217
00218
00219 GeomDrawingArea&
00220 GeomDrawingArea::operator<<(const fawkes::Spline& s)
00221 {
00222 SplineDrawer* d = new SplineDrawer(s);
00223 m_drawers.push_back(d);
00224
00225 return *this;
00226 }
00227
00228
00229
00230
00231
00232
00233 GeomDrawingArea&
00234 GeomDrawingArea::operator<<(fawkes::DrawingManipulator* m)
00235 {
00236 if (m_cur_drawing_manipulator)
00237 { m->integrate(m_cur_drawing_manipulator); }
00238
00239 m_cur_drawing_manipulator = m;
00240 m_drawers.push_back(m);
00241
00242 return *this;
00243 }
00244
00245
00246
00247
00248
00249 bool
00250 GeomDrawingArea::on_expose_event(GdkEventExpose* event)
00251 {
00252 Glib::RefPtr<Gdk::Window> window = get_window();
00253 if (window)
00254 {
00255 Gtk::Allocation allocation = get_allocation();
00256 m_window_width = allocation.get_width();
00257 m_window_height = allocation.get_height();
00258
00259 Cairo::RefPtr<Cairo::Context> context = window->create_cairo_context();
00260
00261 if (event)
00262 {
00263 context->rectangle( event->area.x, event->area.y,
00264 event->area.width, event->area.height );
00265 context->clip();
00266 }
00267
00268 float unit_width = fabs(m_max_x) + fabs(m_min_x);
00269 float unit_height = fabs(m_max_y) + fabs(m_min_y);
00270 if ( (m_window_width / unit_width) <= (m_window_height / unit_height) )
00271 { m_unit = m_window_width / unit_width; }
00272 else
00273 { m_unit = m_window_height / unit_height; }
00274
00275 pre_draw(context);
00276
00277 for ( vector<GeomDrawer*>::iterator iter = m_drawers.begin();
00278 iter != m_drawers.end();
00279 ++iter )
00280 {
00281 GeomDrawer* d = *iter;
00282 d->draw(context);
00283 }
00284
00285 post_draw(context);
00286
00287 context->stroke();
00288 }
00289
00290 return true;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299 void
00300 GeomDrawingArea::to_drawing_coords( int window_x, int window_y,
00301 float& drawing_x, float& drawing_y )
00302 {
00303 float unit_width = fabs(m_max_x) + fabs(m_min_x);
00304
00305 float pixel_per_unit = m_window_width / unit_width;
00306
00307 drawing_x = window_x / pixel_per_unit + m_min_x;
00308 drawing_y = -(window_y / pixel_per_unit + m_min_y);
00309 }
00310
00311
00312
00313
00314
00315
00316
00317 void
00318 GeomDrawingArea::pre_draw(Cairo::RefPtr<Cairo::Context>& context)
00319 {
00320 context->translate( m_window_width / 2.0, m_window_height / 2.0 );
00321 context->scale(m_unit, -m_unit);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330 void
00331 GeomDrawingArea::post_draw(Cairo::RefPtr<Cairo::Context>& context)
00332 {
00333 }
00334
00335 }