honor horizontal alignment also when a mark is present
[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_point_widget->setAlphatest(1);
9         m_position = 0;
10         m_length = 0;
11         m_have_foreground_color = 0;
12 }
13
14 ePositionGauge::~ePositionGauge()
15 {
16         delete m_point_widget;
17 }
18
19 void ePositionGauge::setLength(const pts_t &len)
20 {
21         if (m_length == len)
22                 return;
23         m_length = len;
24         updatePosition();
25         invalidate();
26 }
27
28 void ePositionGauge::setPosition(const pts_t &pos)
29 {
30         if (m_position == pos)
31                 return;
32         m_position = pos;
33         updatePosition();
34 }
35
36 void ePositionGauge::setInColor(const gRGB &color)
37 {
38         invalidate();
39 }
40
41 void ePositionGauge::setPointer(gPixmap *pixmap, const ePoint &center)
42 {
43         m_point_center = center;
44         m_point_widget->setPixmap(pixmap);
45         m_point_widget->resize(pixmap->size());
46         updatePosition();
47 }
48
49 void ePositionGauge::setInOutList(PyObject *list)
50 {
51         if (!PyList_Check(list))
52                 return;
53         int size = PyList_Size(list);
54         int i;
55         
56         m_cue_entries.clear();
57         
58         for (i=0; i<size; ++i)
59         {
60                 PyObject *tuple = PyList_GetItem(list, i);
61                 if (!PyTuple_Check(tuple))
62                         continue;
63
64                 if (PyTuple_Size(tuple) != 2)
65                         continue;
66
67                 PyObject *ppts = PyTuple_GetItem(tuple, 0), *ptype = PyTuple_GetItem(tuple, 1);
68                 if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
69                         continue;
70
71                 pts_t pts = PyLong_AsLongLong(ppts);
72                 int type = PyInt_AsLong(ptype);
73                 m_cue_entries.insert(cueEntry(pts, type));
74         }
75         invalidate();
76 }
77
78 int ePositionGauge::event(int event, void *data, void *data2)
79 {
80         switch (event)
81         {
82         case evtPaint:
83         {
84                 ePtr<eWindowStyle> style;
85                 gPainter &painter = *(gPainter*)data2;
86
87                 eSize s(size());
88
89                 getStyle(style);
90                 
91                 eWidget::event(evtPaint, data, data2);
92
93                 style->setStyle(painter, eWindowStyle::styleLabel); // TODO - own style
94 //              painter.fill(eRect(0, 10, s.width(), s.height()-20));
95                 
96                 pts_t in = 0, out = 0;
97                 
98                 std::multiset<cueEntry>::iterator i(m_cue_entries.begin());
99                 
100                 while (1)
101                 {
102                         if (i == m_cue_entries.end())
103                                 out = m_length;
104                         else {
105                                 if (i->what == 0) /* in */
106                                 {
107                                         in = i++->where;
108                                         continue;
109                                 } else if (i->what == 1) /* out */
110                                         out = i++->where;
111                                 else /* mark */
112                                 {
113                                         int xm = scale(i->where);
114                                         painter.setForegroundColor(gRGB(0xFF8080));
115                                         painter.fill(eRect(xm - 2, 0, 4, s.height()));
116                                         i++;
117                                         continue;
118                                 }
119                         }
120                         
121                         if (m_have_foreground_color)
122                         {
123                                 painter.setForegroundColor(gRGB(m_foreground_color));
124                                 int xi = scale(in), xo = scale(out);
125                                 painter.fill(eRect(xi, 10, xo-xi, s.height()-14));
126                         }
127                         
128                         in = m_length;
129                         
130                         if (i == m_cue_entries.end())
131                                 break;
132                 }
133 //              painter.setForegroundColor(gRGB(0x00000000));
134
135                 if (m_have_foreground_color)
136                 {
137                         painter.setForegroundColor(gRGB(0x225b7395));
138                         painter.fill(eRect(s.width() - 2, 2, s.width() - 1, s.height() - 4));
139                         painter.fill(eRect(0, 2, 2, s.height() - 4));
140                 }
141                 
142 #if 0
143 // border
144                 if (m_have_border_color)
145                         painter.setForegroundColor(m_border_color);
146                 painter.fill(eRect(0, 0, s.width(), m_border_width));
147                 painter.fill(eRect(0, m_border_width, m_border_width, s.height()-m_border_width));
148                 painter.fill(eRect(m_border_width, s.height()-m_border_width, s.width()-m_border_width, m_border_width));
149                 painter.fill(eRect(s.width()-m_border_width, m_border_width, m_border_width, s.height()-m_border_width));
150 #endif
151
152                 return 0;
153         }
154         case evtChangedPosition:
155                 return 0;
156         default:
157                 return eWidget::event(event, data, data2);
158         }
159 }
160
161 void ePositionGauge::updatePosition()
162 {
163         m_pos = scale(m_position);
164         int base = (size().height() - 10) / 2;
165         
166         m_point_widget->move(ePoint(m_pos - m_point_center.x(), base - m_point_center.y()));
167 }
168
169 int ePositionGauge::scale(const pts_t &val)
170 {
171         if (!m_length)
172                 return 0;
173
174         int width = size().width();
175
176         return width * val / m_length;
177 }
178
179 void ePositionGauge::setForegroundColor(const gRGB &col)
180 {
181         if ((!m_have_foreground_color) || !(m_foreground_color == col))
182         {
183                 m_foreground_color = col;
184                 m_have_foreground_color = 1;
185                 invalidate();
186         }
187 }