Rev Author Line No. Line
178 kaklik 1 #include <boost/multi_array.hpp>
2 #include <GL/gl.h>
3 #include <QtGui/QMouseEvent>
4 #include "selectRectWidget.hh"
5  
6 using namespace boost;
7 using namespace mimas;
8  
9 SelectRectWidget::SelectRectWidget( QWidget *parent, Qt::WFlags f ):
10 VideoWidget( parent, f ), dragging(false), haveRect(false)
11 {
12 }
13  
14 void SelectRectWidget::setImage( const image< rgba< unsigned char > > &_img )
15 {
16 dragging = false;
17 haveRect = false;
18 VideoWidget::setImage( _img );
19 emit rectDefined( isRectDefined() );
20 update();
21 }
22  
23 void SelectRectWidget::paintGL(void)
24 {
25 VideoWidget::paintGL();
26 if ( haveRect ) {
27 qglColor(Qt::red);
28 // glColor4f(1,0,0,0.5);
29 glPushMatrix();
30 glScalef( zoom, zoom, 1.0 );
31 glBegin(GL_LINE_LOOP);
32 glVertex2f( x0, y0 );
33 glVertex2f( x1, y0 );
34 glVertex2f( x1, y1 );
35 glVertex2f( x0, y1 );
36 glEnd();
37 glPopMatrix();
38 }
39 }
40  
41 void SelectRectWidget::mousePressEvent( QMouseEvent *e )
42 {
43 // if left mouse-button was pressed:
44 if ( e->button() == Qt::LeftButton ) {
45 x0 = (int)floor( e->x() / zoom );
46 y0 = (int)floor( e->y() / zoom );
47 x1 = x0;
48 y1 = y0;
49 dragging = true;
50 haveRect = true;
51 update();
52 emit rectDefined( isRectDefined() );
53 };
54 }
55  
56 void SelectRectWidget::mouseReleaseEvent( QMouseEvent *e )
57 {
58 if ( e->button() == Qt::LeftButton ) {
59 x1 = (int)ceil( e->x() / zoom );
60 y1 = (int)ceil( e->y() / zoom );
61 if ( x0 > x1 ) {
62 x0 ^= x1;
63 x1 ^= x0;
64 x0 ^= x1;
65 };
66 if ( y0 > y1 ) {
67 y0 ^= y1;
68 y1 ^= y0;
69 y0 ^= y1;
70 }
71 dragging = false;
72 update();
73 emit rectDefined( isRectDefined() );
74 }
75 }
76  
77 void SelectRectWidget::mouseMoveEvent( QMouseEvent *e )
78 {
79 if ( dragging ) {
80 x1 = (int)ceil( e->x() / zoom );
81 y1 = (int)ceil( e->y() / zoom );
82 update();
83 };
84 }
85  
86 void SelectRectWidget::clearSelection(void)
87 {
88 haveRect = false;
89 emit rectDefined( isRectDefined() );
90 }
91  
92 multi_array< rgba< unsigned char >, 2 >::array_view< 2 >::type
93 view( multi_array_ref< rgba< unsigned char >, 2 > &in,
94 int x, int y, int w, int h )
95 {
96 typedef multi_array< rgba< unsigned char >, 2 >::index_range range;
97 typedef multi_array< rgba< unsigned char >, 2 >::array_view< 2 >::type
98 array_view;
99 multi_array< rgba< unsigned char >, 2 >::index_gen indices;
100 array_view out =
101 in[ indices[ range( y, y + h ) ][ range( x, x + w ) ] ];
102 return out;
103 }
104  
105 image< rgba< unsigned char > > SelectRectWidget::selectedImage()
106 {
107 image< rgba< unsigned char > > retVal;
108 if ( isRectDefined() ) {
109 int
110 w = x1 + 1 - x0,
111 h = y1 + 1 - y0;
112 retVal.init( w, h );
113 multi_array_ref< rgba< unsigned char >, 2 > imgData
114 ( img.rawData(), extents[ img.getHeight() ][ img.getWidth() ] );
115 multi_array_ref< rgba< unsigned char >, 2 >
116 ( retVal.rawData(),
117 extents[ retVal.getHeight() ][ retVal.getWidth() ] ) =
118 view( imgData, x0, y0, w, h );
119 };
120 return retVal;
121 }