-
Notifications
You must be signed in to change notification settings - Fork 0
/
myarea.hh
110 lines (86 loc) · 2.9 KB
/
myarea.hh
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef MY_AREA_H
#define MY_AREA_H
#include <gtkmm.h>
#include <iostream>
#include <glibmm.h>
#include <giomm.h>
#include "../daemon/ImageDetails.hh"
class MyArea : public Gtk::DrawingArea{
public:
MyArea();
void set_image(Glib::ustring image);
virtual ~MyArea();
Glib::RefPtr<Gdk::Pixbuf> pimage;
void set_coords(std::vector<FaceDetail>& coords);
protected:
double scale;
std::vector<FaceDetail> face_coords;
//override default signal handler
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
bool on_scroll_event(GdkEventScroll* ev);
};
void MyArea::set_image(Glib::ustring image){
pimage = Gdk::Pixbuf::create_from_file(image);
std::cout << "Set the pixbuf of image : in parameterized constructor of MyArea " << std::endl;
//show at lease a quarter of the image.
if (pimage)
set_size_request(pimage->get_width()/2, pimage->get_height()/2);
}
void MyArea::set_coords(std::vector<FaceDetail>& coords)
{
face_coords = coords;
}
MyArea::MyArea()
{
std::cout << "In default constructor of MyArea " << std::endl;
add_events(Gdk::BUTTON_PRESS_MASK | Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
scale = 0.8;
}
MyArea::~MyArea(){
}
bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr){
std::cout << "In on_draw function" << std::endl;
if (!pimage)
return false;
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
//draw the image in the middle of the drawing area, or (if the image is larger than the drawing area)
//draw the middle part of the image.
cr->scale(scale, scale);
Gdk::Cairo::set_source_pixbuf(cr, pimage, (width/2)/scale - pimage->get_width()/2, (height/2)/scale - pimage->get_height()/2);
cr->rectangle(0, 0, get_allocation().get_width() / scale, get_allocation().get_width()/scale);
cr->fill();
cr->restore();
cr->save();
cr->set_source_rgb(0.8, 0.0, 0.0); //red
for (auto face : face_coords)
{
cr->move_to(face.bounds.x / 2 / scale, face.bounds.y / 2 / scale);
cr->line_to(face.bounds.x / 2 / scale + face.bounds.width / 2 / scale, face.bounds.y / 2 / scale);
cr->line_to(face.bounds.x / 2 / scale + face.bounds.width / 2 / scale, face.bounds.y / 2 / scale + face.bounds.height / 2 / scale);
cr->line_to(face.bounds.x / 2 / scale, face.bounds.y / 2 / scale + face.bounds.height / 2 / scale);
cr->line_to(face.bounds.x / 2 / scale, face.bounds.y / 2 / scale);
cr->stroke();
cr->save();
}
cr->restore();
//cr->paint();
return true;
}
bool MyArea::on_scroll_event(GdkEventScroll *ev){
//update scale according to mouse scrolling
scale -= ev->delta_y / 10;
//no more scaling below 0.5
if (scale < 0.5)
scale = 0.5;
else if (scale > 2)
scale = 2;
std::cout << scale << std::endl;
std::cout.flush();
//update drawing
queue_draw();
//there is probably a good reason to do this
return true;
}
#endif