Merge remote branch 'origin/pootle-import'
[enigma2.git] / lib / gui / epositiongauge.cpp
1 #include <lib/gui/epositiongauge.h>
2 #include <lib/gui/epixmap.h>
3
4 ePositionGauge::ePositionGauge(eWidget *parent)
5         : eWidget(parent)
6 {
7         m_point_widget = new ePixmap(this);
8         m_seek_point_widget = new ePixmap(this);
9         m_seek_point_widget->hide();
10         m_point_widget->setAlphatest(1);
11         m_seek_point_widget->setAlphatest(1);
12         m_position = 0;
13         m_length = 0;
14         m_have_foreground_color = 0;
15         m_seek_position = 0;
16 }
17
18 ePositionGauge::~ePositionGauge()
19 {
20         delete m_point_widget;
21         delete m_seek_point_widget;
22 }
23
24 void ePositionGauge::setLength(const pts_t &len)
25 {
26         if (m_length == len)
27                 return;
28         m_length = len;
29         updatePosition();
30         invalidate();
31 }
32
33 void ePositionGauge::setPosition(const pts_t &pos)
34 {
35         if (m_position == pos)
36                 return;
37         m_position = pos;
38         updatePosition();
39 }
40
41 void ePositionGauge::setInColor(const gRGB &color)
42 {
43         invalidate();
44 }
45
46 void ePositionGauge::setPointer(int which, ePtr<gPixmap> &pixmap, const ePoint &center)
47 {
48         setPointer(which, pixmap.operator->(), center);
49 }
50
51 void ePositionGauge::setPointer(int which, gPixmap *pixmap, const ePoint &center)
52 {
53         if (which == 0)
54         {
55                 m_point_center = center;
56                 m_point_widget->setPixmap(pixmap);
57                 m_point_widget->resize(pixmap->size());
58         } else
59         {
60                 m_seek_point_center = center;
61                 m_seek_point_widget->setPixmap(pixmap);
62                 m_seek_point_widget->resize(pixmap->size());
63         }
64         updatePosition();
65 }
66
67 void ePositionGauge::setInOutList(ePyObject list)
68 {
69         if (!PyList_Check(list))
70                 return;
71         int size = PyList_Size(list);
72         int i;
73         
74         m_cue_entries.clear();
75         
76         for (i=0; i<size; ++i)
77         {
78                 ePyObject tuple = PyList_GET_ITEM(list, i);
79                 if (!PyTuple_Check(tuple))
80                         continue;
81
82                 if (PyTuple_Size(tuple) != 2)
83                         continue;
84
85                 ePyObject ppts = PyTuple_GET_ITEM(tuple, 0), ptype = PyTuple_GET_ITEM(tuple, 1);
86                 if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
87                         continue;
88
89                 pts_t pts = PyLong_AsLongLong(ppts);
90                 int type = PyInt_AsLong(ptype);
91                 m_cue_entries.insert(cueEntry(pts, type));
92         }
93         invalidate();
94 }
95
96 int ePositionGauge::event(int event, void *data, void *data2)
97 {
98         switch (event)
99         {
100         case evtPaint:
101         {
102                 ePtr<eWindowStyle> style;
103                 gPainter &painter = *(gPainter*)data2;
104
105                 eSize s(size());
106
107                 getStyle(style);
108                 
109                 eWidget::event(evtPaint, data, data2);
110
111                 style->setStyle(painter, eWindowStyle::styleLabel); // TODO - own style
112 //              painter.fill(eRect(0, 10, s.width(), s.height()-20));
113                 
114                 pts_t in = 0, out = 0;
115                 int xm, xm_last = -1;
116                 
117                 std::multiset<cueEntry>::iterator i(m_cue_entries.begin());
118                 
119                 while (1)
120                 {
121                         if (i == m_cue_entries.end())
122                                 out = m_length;
123                         else {
124                                 if (i->what == 0) /* in */
125                                 {
126                                         in = i++->where;
127                                         continue;
128                                 } else if (i->what == 1) /* out */
129                                         out = i++->where;
130                                 else /* mark or last */
131                                 {
132                                         xm = scale(i->where);
133                                         if (i->what == 2) {
134                                                 painter.setForegroundColor(gRGB(0xFF8080));
135                                                 if (xm - 2 < xm_last) /* Make sure last is not overdrawn */
136                                                         painter.fill(eRect(xm_last, 0, 2 + xm - xm_last, s.height()));
137                                                 else
138                                                         painter.fill(eRect(xm - 2, 0, 4, s.height()));
139                                         } else if (i->what == 3) {
140                                                 painter.setForegroundColor(gRGB(0x80FF80));
141                                                 painter.fill(eRect(xm - 1, 0, 3, s.height()));
142                                                 xm_last = xm + 2;
143                                         }
144                                         i++;
145                                         continue;
146                                 }
147                         }
148                         
149                         if (m_have_foreground_color)
150                         {
151                                 painter.setForegroundColor(gRGB(m_foreground_color));
152                                 int xi = scale(in), xo = scale(out);
153                                 painter.fill(eRect(xi, (s.height()-4) / 2, xo-xi, 4));
154                         }
155                         
156                         in = m_length;
157                         
158                         if (i == m_cue_entries.end())
159                                 break;
160                 }
161 //              painter.setForegroundColor(gRGB(0x00000000));
162
163                 if (m_have_foreground_color)
164                 {
165                         painter.setForegroundColor(gRGB(0x225b7395));
166                         painter.fill(eRect(s.width() - 2, 2, s.width() - 1, s.height() - 4));
167                         painter.fill(eRect(0, 2, 2, s.height() - 4));
168                 }
169                 
170 #if 0
171 // border
172                 if (m_have_border_color)
173                         painter.setForegroundColor(m_border_color);
174                 painter.fill(eRect(0, 0, s.width(), m_border_width));
175                 painter.fill(eRect(0, m_border_width, m_border_width, s.height()-m_border_width));
176                 painter.fill(eRect(m_border_width, s.height()-m_border_width, s.width()-m_border_width, m_border_width));
177                 painter.fill(eRect(s.width()-m_border_width, m_border_width, m_border_width, s.height()-m_border_width));
178 #endif
179
180                 return 0;
181         }
182         case evtChangedPosition:
183                 return 0;
184         default:
185                 return eWidget::event(event, data, data2);
186         }
187 }
188
189 void ePositionGauge::updatePosition()
190 {
191         m_pos = scale(m_position);
192         m_seek_pos = scale(m_seek_position);
193         int base = (size().height() - 10) / 2;
194         
195         m_point_widget->move(ePoint(m_pos - m_point_center.x(), base - m_point_center.y()));
196         m_seek_point_widget->move(ePoint(m_seek_pos - m_seek_point_center.x(), base - m_seek_point_center.y()));
197 }
198
199 int ePositionGauge::scale(const pts_t &val)
200 {
201         if (!m_length)
202                 return 0;
203
204         int width = size().width();
205
206         return width * val / m_length;
207 }
208
209 void ePositionGauge::setForegroundColor(const gRGB &col)
210 {
211         if ((!m_have_foreground_color) || !(m_foreground_color == col))
212         {
213                 m_foreground_color = col;
214                 m_have_foreground_color = 1;
215                 invalidate();
216         }
217 }
218
219 void ePositionGauge::enableSeekPointer(int enable)
220 {
221         if (enable)
222                 m_seek_point_widget->show();
223         else
224                 m_seek_point_widget->hide();
225 }
226
227 void ePositionGauge::setSeekPosition(const pts_t &pos)
228 {
229         if (m_seek_position == pos)
230                 return;
231         m_seek_position = pos;
232         updatePosition();
233 }